-
Notifications
You must be signed in to change notification settings - Fork 0
/
GRID.C
72 lines (57 loc) · 1.32 KB
/
GRID.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
#include <stdlib.h>
#include "GRID.H"
struct grid grid;
int init_grid(int width, int height, int cell_size) {
int x, y;
struct grid_cell current;
grid.cells =
(struct grid_cell**)malloc(height*sizeof(struct grid_cell*));
if (grid.cells == NULL) {
printf("%s", "Out of memory!");
return 1;
}
grid.width = width;
grid.height = height;
grid.cell_size = cell_size;
grid.friction = 0.4f;
grid.gravity = 0.5f;
for (y = 0; y < height; y++) {
grid.cells[y] = (struct grid_cell*)malloc(width*sizeof(struct grid_cell));
if (grid.cells[y] == NULL) {
printf("%s", "Out of memory!");
return 1;
}
if (y == height - 1) {
for (x = 0; x < width; x++) {
current.solid = 1;
grid.cells[y][x] = current;
}
} else {
for (x = 0; x < width; x++) {
if (y == height - 2 &&
x == 10) {
current.solid = 1;
} else {
current.solid = 0;
}
grid.cells[y][x] = current;
}
}
}
return 0;
}
void destroy_grid() {
int y;
for (y = 0; y < grid.height; y++) {
free(grid.cells[y]);
grid.cells[y] = NULL;
}
free(grid.cells);
grid.cells = NULL;
}
struct grid_cell *get_cell(int x, int y) {
int cell_x, cell_y;
cell_x = x / grid.cell_size;
cell_y = y / grid.cell_size;
return &grid.cells[cell_y][cell_x];
}