/
arkanoid.c
107 lines (98 loc) · 2.67 KB
/
arkanoid.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* arkanoid.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jbahus <jbahus@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/05/02 17:17:10 by mbourdel #+# #+# */
/* Updated: 2015/05/03 23:18:30 by jbahus ### ########.fr */
/* */
/* ************************************************************************** */
#include "arkanoid.h"
void env_init(t_env *env)
{
env->squa.x = -0.918f;
env->squa.y = 0.92f;
env->bar = 0.f;
env->ball.x = 0.f;
env->ball.y = -0.895f;
env->ball.move_x = 0.008f;
env->ball.move_y = 0.008f;
env->score = 0;
env->life = 2;
}
void key_callback(GLFWwindow *win, int key, int scancode, int action,
int mods)
{
if (!scancode && !win && !mods)
scancode = scancode + 0;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(win, GL_TRUE);
}
void get_level(t_env *env, char *path)
{
int fd;
if (-1 == (fd = open(path, O_RDONLY)))
{
ft_error(path);
exit (-1);
}
if (-1 == read(fd, env->map, 140))
{
ft_error(path);
exit (-1);
}
env->map[139] = '\0';
}
static void do_window(GLFWwindow *win, t_env *env)
{
int state;
while (!glfwWindowShouldClose(win))
{
if (GLFW_PRESS == (state = glfwGetKey(win, GLFW_KEY_LEFT))
&& (env->bar > -0.87f))
env->bar -= 0.03f;
if (GLFW_PRESS == (state = glfwGetKey(win, GLFW_KEY_RIGHT))
&& (env->bar < 0.87f))
env->bar += 0.03f;
glClear(GL_COLOR_BUFFER_BIT);
square_create(env);
the_ball(env);
the_bar(env);
aff_score(env);
glfwPollEvents();
glfwSwapBuffers(win);
if (env->life == -1)
{
ft_putendl("A looser is YOU ! D:");
glfwSetWindowShouldClose(win, GL_TRUE);
}
if (check_win(env))
glfwSetWindowShouldClose(win, GL_TRUE);
}
}
int main(int ac, char **av)
{
GLFWwindow *win;
t_env env;
if (ac != 2)
return (0);
get_level(&env, av[1]);
if (!glfwInit())
exit(EXIT_FAILURE);
win = glfwCreateWindow(2560, 1440, "Arkanoid",
glfwGetPrimaryMonitor(), NULL);
if (!win)
{
glfwTerminate();
return (-1);
}
env_init(&env);
glfwMakeContextCurrent(win);
glfwSetKeyCallback(win, key_callback);
do_window(win, &env);
glfwDestroyWindow(win);
glfwTerminate();
return (0);
}