-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.cc
361 lines (302 loc) · 10.5 KB
/
game.cc
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "game.h"
#include "platform/platform.h"
#include "engine/util.h"
#include <string.h>
#include <cstdlib>
#include <ctime>
game_state_t game_state;
const double PLAYER_WIDTH_METERS = 0.8;
const double PLAYER_WIDTH = PLAYER_WIDTH_METERS * METERS_TO_PIXELS;
const double PLAYER_HEIGHT_METERS = 1.0;
const double PLAYER_HEIGHT = PLAYER_HEIGHT_METERS * METERS_TO_PIXELS;
const color_t PLAYER_COLOR = rgb(150.3, 250.5, 20.6);
const double PLAYER_SPEED_METERS_PER_SECOND = 3.0;
const double PLAYER_SPEED = PLAYER_SPEED_METERS_PER_SECOND * METERS_TO_PIXELS;
const uint TILE_CHUNK_COUNT_X = 128;
const uint TILE_CHUNK_COUNT_Y = 128;
const uint KILOBYTE = 1024;
const uint MEGABYTE = 1024 * KILOBYTE;
const uint MEMORY_POOL_BYTES = 64 * MEGABYTE;
char memory_pool[MEMORY_POOL_BYTES] = {};
uint allocated_bytes = 0;
void assert(bool expression, const char* message) {
if (!expression) {
std::cout << message << std::endl;
exit(1);
}
}
void* allocate_memory(uint bytes) {
assert(bytes <= MEMORY_POOL_BYTES - allocated_bytes, "Tried to allocate more than MEMORY_POOL_BYTES bytes");
char* memory = memory_pool + allocated_bytes;
allocated_bytes += bytes;
return memory;
}
tile_chunk_t* make_tile_chunks() {
uint num_tile_chunks = TILE_CHUNK_COUNT_X * TILE_CHUNK_COUNT_Y;
tile_chunk_t* tile_chunks = (tile_chunk_t*) allocate_memory(num_tile_chunks * sizeof(tile_chunk_t));
for (uint i = 0; i < num_tile_chunks; i++) {
tile_chunks[i].tiles = (tile_t*) allocate_memory(TILE_CHUNK_SIZE * TILE_CHUNK_SIZE * sizeof(tile_t));
}
return tile_chunks;
}
tile_chunk_t* get_tile_chunk(world_t* world, uint tile_x, uint tile_y) {
uint tile_chunk_x = tile_x >> TILE_CHUNK_LOWER_BITS;
uint tile_chunk_y = tile_y >> TILE_CHUNK_LOWER_BITS;
if (tile_chunk_x >= TILE_CHUNK_COUNT_X || tile_chunk_y >= TILE_CHUNK_COUNT_Y) {
return nullptr;
}
return world->tile_chunks + tile_chunk_y*TILE_CHUNK_COUNT_X + tile_chunk_x;
}
void set_tile(world_t* world, uint tile_x, uint tile_y, tile_t value) {
std::cout << "set_tile(" << tile_x << ", " << tile_y << ")" << std::endl;
tile_chunk_t* tile_chunk = get_tile_chunk(world, tile_x, tile_y);
// TODO: Lazily instantiate tiles
assert(tile_chunk->tiles, "Tile chunk does not have any tiles allocated.");
uint tile_x_relative_to_chunk = tile_x & TILE_CHUNK_LOWER_BITS_MASK;
uint tile_y_relative_to_chunk = tile_y & TILE_CHUNK_LOWER_BITS_MASK;
tile_chunk->tiles[tile_y_relative_to_chunk*TILE_CHUNK_SIZE + tile_x_relative_to_chunk] = value;
}
tile_t get_tile(world_t* world, uint tile_x, uint tile_y) {
tile_chunk_t* tile_chunk = get_tile_chunk(world, tile_x, tile_y);
if (!tile_chunk || !tile_chunk->tiles) {
return 0; // We haven't defined anything at this location
}
uint tile_x_relative_to_chunk = tile_x & TILE_CHUNK_LOWER_BITS_MASK;
uint tile_y_relative_to_chunk = tile_y & TILE_CHUNK_LOWER_BITS_MASK;
tile_t tile = tile_chunk->tiles[tile_y_relative_to_chunk*TILE_CHUNK_SIZE + tile_x_relative_to_chunk];
return tile;
}
void create_room(
world_t* world,
uint room_index_x,
uint room_index_y,
bool door_left,
bool door_right,
bool door_top,
bool door_bottom) {
uint top_left_y = room_index_y * TILES_PER_SCREEN_Y;
uint top_left_x = room_index_x * TILES_PER_SCREEN_X;
// Top wall
uint vertical_door_x = TILES_PER_SCREEN_X / 2;
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
if (door_top && (i == vertical_door_x || i == vertical_door_x - 1)) {
// Leave a door
} else {
set_tile(world, top_left_x + i, top_left_y, 1);
}
}
// Bottom wall
for (int i = 0; i < TILES_PER_SCREEN_X; i++) {
if (door_bottom && (i == vertical_door_x || i == vertical_door_x - 1)) {
// Leave a door
} else {
set_tile(world, top_left_x + i, top_left_y + TILES_PER_SCREEN_Y - 1, 1);
}
}
// Left wall
uint horizontal_door_y = TILES_PER_SCREEN_Y / 2;
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
if (door_left && i == horizontal_door_y) {
// Leave a door
} else {
set_tile(world, top_left_x, top_left_y + i, 1);
}
}
// Right wall
for (int i = 0; i < TILES_PER_SCREEN_Y; i++) {
if (door_right && i == horizontal_door_y) {
// Leave a door
} else {
set_tile(world, top_left_x + TILES_PER_SCREEN_X - 1, top_left_y + i, 1);
}
}
// A pattern in the center
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
if (rand() % 3 == 0) {
set_tile(world, top_left_x + vertical_door_x - 2 + i, top_left_y + horizontal_door_y - 1 + j, 1);
}
}
}
}
void fill_world_with_rooms(world_t* world) {
std::srand(std::time(nullptr));
bool door_top = false;
bool door_bottom = false;
bool door_right = false;
bool door_left = false;
uint room_index_x = 0;
uint room_index_y = 0;
uint next_room_index_x = 0;
uint next_room_index_y = 0;
for (uint room = 0; room < 10; room++) {
int direction = std::rand() % 2;
switch (direction) {
case (0):
door_right = true;
next_room_index_x = room_index_x + 1;
break;
case (1):
door_bottom = true;
next_room_index_y = room_index_y + 1;
break;
}
create_room(world, room_index_x, room_index_y, door_left, door_right, door_top, door_bottom);
door_top = door_bottom;
door_bottom = false;
door_left = door_right;
door_right = false;
room_index_x = next_room_index_x;
room_index_y = next_room_index_y;
}
// Craziness at the end
uint top_left_y = room_index_y * TILES_PER_SCREEN_Y;
uint top_left_x = room_index_x * TILES_PER_SCREEN_X;
for (int i = 1; i < TILES_PER_SCREEN_X-1; i++) {
for (int j = 1; j < TILES_PER_SCREEN_Y-1; j++) {
if (rand() % 3 == 0) {
set_tile(world, top_left_x + i, top_left_y + j, 1);
}
}
}
}
void put_pixel(pixel_buffer_t* pixel_buffer, uint x, uint y, color_t color) {
uint offset = pixel_buffer->width * y + x;
pixel_buffer->data[offset] = color;
}
void draw_box(pixel_buffer_t* pixel_buffer, double x, double y, double width, double height, color_t color) {
int x1 = clip(x, 0, pixel_buffer->width);
int x2 = clip(x+width, 0, pixel_buffer->width);
int y1 = clip(y, 0, pixel_buffer->height);
int y2 = clip(y+height, 0, pixel_buffer->height);
for (int x_to_fill=x1; x_to_fill<x2; x_to_fill++) {
for (int y_to_fill=y1; y_to_fill<y2; y_to_fill++) {
put_pixel(pixel_buffer, x_to_fill, y_to_fill, color);
}
}
}
void initialize_game_state(game_state_t &game_state) {
game_state.player_location.tile_x = 2;
game_state.player_location.tile_y = 5;
game_state.player_location.x = 0;
game_state.player_location.y = 0;
game_state.world.tile_chunks = make_tile_chunks();
fill_world_with_rooms(&game_state.world);
game_state.initialized = true;
}
void clear_screen(pixel_buffer_t* pixel_buffer) {
draw_box(pixel_buffer, 0, 0, pixel_buffer->width, pixel_buffer->height, BLACK);
}
#ifdef DEBUG
void print_location(location_t location) {
std::cout << "location(" << location.tile_x + location.x << ", " << location.tile_y + location.y << ")" << std::endl;
}
void print_screen_location(screen_location_t location) {
std::cout << "screen_location(" << location.x << ", " << location.y << ")" << std::endl;
}
#endif
screen_location_t get_screen_location(location_t location) {
screen_location_t screen_location;
screen_location.x = (location.tile_x % TILES_PER_SCREEN_X + location.x) * METERS_TO_PIXELS;
screen_location.y = (location.tile_y % TILES_PER_SCREEN_Y + location.y) * METERS_TO_PIXELS;
return screen_location;
}
tile_t get_tile(world_t* world, location_t location) {
return get_tile(world, location.tile_x, location.tile_y);
}
bool location_occupied(world_t* world, uint tile_x, uint tile_y) {
return get_tile(world, tile_x, tile_y);
}
bool location_occupied(world_t* world, location_t location) {
return get_tile(world, location.tile_x, location.tile_y);
}
location_t update_location(location_t location, double dx, double dy) {
// Should only change locations using this method so x and y stay within bounds of a tile map
location.x += dx;
location.y += dy;
while (location.x < 0) {
location.x += 1;
location.tile_x -= 1;
}
while (location.x >= 1) {
location.x -= 1;
location.tile_x += 1;
}
while (location.y < 0) {
location.y += 1;
location.tile_y -= 1;
}
while (location.y >= 1) {
location.y -= 1;
location.tile_y += 1;
}
return location;
}
bool valid_player_location(world_t* world, location_t location) {
location_t bottom_left = update_location(location, PLAYER_WIDTH_METERS / -2, 0);
location_t bottom_right = update_location(location, PLAYER_WIDTH_METERS / 2, 0);
return !location_occupied(world, bottom_left) &&
!location_occupied(world, bottom_right);
}
void update(double dt, pixel_buffer_t* pixel_buffer, controller_t &controller) {
if (!game_state.initialized) {
initialize_game_state(game_state);
}
location_t player_location = game_state.player_location;
clear_screen(pixel_buffer);
// Move player
meters dx = 0, dy = 0;
if (controller.right_pressed) {
dx += PLAYER_SPEED_METERS_PER_SECOND * dt;
}
if (controller.left_pressed) {
dx -= PLAYER_SPEED_METERS_PER_SECOND * dt;
}
location_t new_location = update_location(player_location, dx, 0);
if (valid_player_location(&(game_state.world), new_location)) {
player_location = new_location;
}
if (controller.down_pressed) {
dy += PLAYER_SPEED_METERS_PER_SECOND * dt;
}
if (controller.up_pressed) {
dy -= PLAYER_SPEED_METERS_PER_SECOND * dt;
}
new_location = update_location(player_location, 0, dy);
if (valid_player_location(&(game_state.world), new_location)) {
player_location = new_location;
}
game_state.player_location = player_location;
// Render tile map
uint top_left_x = game_state.player_location.tile_x / TILES_PER_SCREEN_X * TILES_PER_SCREEN_X;
uint top_left_y = game_state.player_location.tile_y / TILES_PER_SCREEN_Y * TILES_PER_SCREEN_Y;
for (uint row=0; row < TILES_PER_SCREEN_Y; row++) {
for (int col=0; col < TILES_PER_SCREEN_X; col++) {
if (location_occupied(
&(game_state.world),
top_left_x + col,
top_left_y + row
)
) {
draw_box(
pixel_buffer,
col*TILE_WIDTH,
row*TILE_HEIGHT,
TILE_WIDTH,
TILE_HEIGHT,
TILE_COLOR
);
}
}
}
// Render Player
screen_location_t screen_location = get_screen_location(player_location);
draw_box(
pixel_buffer,
screen_location.x - PLAYER_WIDTH/2,
screen_location.y - PLAYER_HEIGHT,
PLAYER_WIDTH,
PLAYER_HEIGHT,
PLAYER_COLOR
);
}