-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.c
160 lines (120 loc) · 2.97 KB
/
level.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include "level.h"
#include "list.h"
#include "brick.h"
#include "panic.h"
static const float H_DISTANCE = 6.0;
static const float V_DISTANCE = 4.0;
static const float DEPTH = 0.0;
static const float X_ORIG = -60.0;
static const float Y_ORIG = 60.0;
static brick_type parse_brick_type(char ch)
{
switch (ch) {
case '1':
return NORMAL;
case '2':
return HARD;
}
assert(0);
}
void load_level_matrix(const char *path,
char matrix[LEVEL_ROWS][LEVEL_COLUMNS + 1])
{
int i;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
panic("Cannot open level file: %s", strerror(errno));
for (i = 0; i < LEVEL_ROWS; ++i) {
read(fd, &matrix[i][0], LEVEL_COLUMNS + 1);
}
close (fd);
}
struct level * level_from_file(const char *path)
{
int i;
int j;
char type;
struct brick *b;
struct level *l;
/* 21 accounts for '\n' */
static char matrix[LEVEL_ROWS][LEVEL_COLUMNS + 1];
load_level_matrix(path, matrix);
l = malloc(sizeof *l);
l->bricks = list_new();
for (i = 0; i < LEVEL_ROWS; ++i) {
for (j = 0; j < LEVEL_COLUMNS; ++j) {
/* because the world origin is on the
* bottom left corner, from the downmost
* rows towards the upmost ones
*/
type = matrix[LEVEL_ROWS - i - 1][j];
if (type == '0')
continue;
b = brick_new();
b->x = X_ORIG + j * H_DISTANCE;
b->y = Y_ORIG + i * V_DISTANCE;
b->z = DEPTH;
b->type = parse_brick_type(type);
list_add(l->bricks, (void *) b);
}
}
return l;
}
struct level *
level_new(void)
{
int i;
int j;
struct brick *b;
struct level *l;
l = malloc(sizeof *l);
l->bricks = list_new();
for (i = 0; i < LEVEL_ROWS; ++i) {
for (j = 0; j < LEVEL_COLUMNS; ++j) {
b = brick_new();
b->x = X_ORIG + j * H_DISTANCE;
b->y = Y_ORIG + i * V_DISTANCE;
b->z = DEPTH;
list_add(l->bricks, (void *) b);
}
}
return l;
}
void
level_free(struct level *l)
{
list_free(l->bricks, (list_free_func) brick_free);
free(l);
}
void
level_draw(void *object, GLuint shader_program)
{
struct level *level;
struct node *n;
struct brick *brick;
level = (struct level *) object;
for (n = level->bricks->first; n; n = n->next) {
brick = (struct brick *) n->data;
if (!brick->alive)
continue;
brick_draw((struct brick *) n->data, shader_program);
}
}
void
level_update(void *object)
{
struct level *level;
struct node *n;
level = (struct level *) object;
for (n = level->bricks->first; n; n = n->next)
brick_update((struct brick *) n->data);
}