-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwolf.c
More file actions
140 lines (131 loc) · 4.74 KB
/
wolf.c
File metadata and controls
140 lines (131 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* wolf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bturcott <bturcott@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/19 21:10:19 by bturcott #+# #+# */
/* Updated: 2019/04/26 12:16:03 by mbartole ### ########.fr */
/* */
/* ************************************************************************** */
#include "wolf.h"
static void rotations(t_sdl *sdl, SDL_Event e)
{
if (e.motion.type == SDL_MOUSEMOTION && sdl->flags[2])
{
if (e.motion.yrel > 2 && sdl->cam.horiz > 5 * MOV_STEP)
sdl->cam.horiz -= 2 * MOV_STEP;
else if (e.motion.yrel < -2 && sdl->cam.horiz < WIN_H - 5 * MOV_STEP)
sdl->cam.horiz += 2 * MOV_STEP;
if (e.motion.xrel > 5)
sdl->cam.angle -= ROT_STEP;
else if (e.motion.xrel < -5)
sdl->cam.angle += ROT_STEP;
fit_angle(&(sdl->cam.angle));
}
if (e.key.keysym.scancode >= 79 && e.key.keysym.scancode <= 82)
{
if (e.key.keysym.scancode == 79)
sdl->cam.angle -= ROT_STEP;
else if (e.key.keysym.scancode == 80)
sdl->cam.angle += ROT_STEP;
else if (e.key.keysym.scancode == 81 && sdl->cam.horiz > 5 * MOV_STEP)
sdl->cam.horiz -= 2 * MOV_STEP;
else if (e.key.keysym.scancode == 82 &&
sdl->cam.horiz < WIN_H - 5 * MOV_STEP)
sdl->cam.horiz += 2 * MOV_STEP;
fit_angle(&(sdl->cam.angle));
}
}
static void sdl_loop(t_sdl *sdl)
{
SDL_Event e;
while (1)
{
if (SDL_PollEvent(&e))
{
rotations(sdl, e);
if (e.key.type == SDL_KEYDOWN || e.type == SDL_QUIT)
{
sounds(sdl, e);
movements(sdl, e.key.keysym.scancode, sdl->cam.angle);
if (e.key.keysym.scancode == 41 || e.type == SDL_QUIT)
exit(clean_all(sdl, ""));
if (e.key.keysym.scancode == SWITCH_MAP)
sdl->flags[0] = sdl->flags[0] ? 0 : 1;
if (e.key.keysym.scancode == SWITCH_TEXT)
sdl->flags[1] = sdl->flags[1] ? 0 : 1;
if (e.key.keysym.scancode == SWITCH_MOUSE)
sdl->flags[2] = sdl->flags[2] ? 0 : 1;
}
reprint_all(sdl);
}
}
}
static void load_textures(t_sdl *sdl)
{
SDL_Surface *sf;
if (!(sdl->texture_pack = ft_memalloc(sizeof(SDL_Texture *) * 4)))
exit(clean_all(sdl, "Malloc fail\n"));
if (!(sf = SDL_LoadBMP("textures/1.bmp")) ||
!(sdl->texture_pack[0] = SDL_CreateTextureFromSurface(sdl->render, sf)))
exit(clean_all(sdl, "Texture loading fail\n"));
SDL_FreeSurface(sf);
if (!(sf = SDL_LoadBMP("textures/2.bmp")) ||
!(sdl->texture_pack[1] = SDL_CreateTextureFromSurface(sdl->render, sf)))
exit(clean_all(sdl, "Texture loading fail\n"));
SDL_FreeSurface(sf);
if (!(sf = SDL_LoadBMP("textures/3.bmp")) ||
!(sdl->texture_pack[2] = SDL_CreateTextureFromSurface(sdl->render, sf)))
exit(clean_all(sdl, "Texture loading fail\n"));
SDL_FreeSurface(sf);
if (!(sf = SDL_LoadBMP("textures/4.bmp")) ||
!(sdl->texture_pack[3] = SDL_CreateTextureFromSurface(sdl->render, sf)))
exit(clean_all(sdl, "Texture loading fail\n"));
SDL_FreeSurface(sf);
if (!(sf = SDL_LoadBMP("textures/floor.bmp")))
exit(clean_all(sdl, "No Floor texture"));
sdl->floor = SDL_ConvertSurfaceFormat(sf, TXT_FORMAT, 0);
SDL_FreeSurface(sf);
}
static void init_sdl(t_sdl *sdl)
{
if (SDL_Init(SDL_INIT_EVERYTHING))
exit(clean_all(sdl, "Cant initialize SDL\n"));
if (!(sdl->window = SDL_CreateWindow(NAME, WIN_POS_X, WIN_POS_Y, WIN_W,
WIN_H, WIN_FLAGS)))
exit(clean_all(sdl, "Cant create window\n"));
if (!(sdl->render = SDL_CreateRenderer(sdl->window, -1, REN_FLAGS)))
exit(clean_all(sdl, "Cant create renderer\n"));
if (!(sdl->text = SDL_CreateTexture(sdl->render, TXT_FORMAT, TXT_ACCESS,
WIN_W, WIN_H)))
exit(clean_all(sdl, "Cant create texture\n"));
if (!(sdl->mapa = SDL_CreateTexture(sdl->render, TXT_FORMAT, TXT_ACCESS,
sdl->map->offset * 30, MAP_H(sdl->map) * 30)))
exit(clean_all(sdl, "Cant create the map\n"));
load_textures(sdl);
sdl->flags[0] = 0;
sdl->flags[1] = 0;
sdl->flags[2] = 0;
}
int main(int argc, char **argv)
{
t_sdl sdl;
int fd;
sdl.cam.x = 30;
sdl.cam.y = 30;
sdl.cam.angle = 0.0;
sdl.cam.horiz = WIN_H / 2;
if (argc == 1 && (fd = open(MAP_DEFAULT, O_RDONLY)) == -1)
return (0);
else if (argc == 2 && (fd = open(argv[1], O_RDONLY)) == -1)
return (0);
else if (argc > 2)
return (clean_all(&sdl, USAGE));
read_map(&sdl, fd);
init_sdl(&sdl);
init_music(&sdl);
sdl_loop(&sdl);
return (0);
}