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
|
#include "engine.h"
#include "block.h"
#include "chunk.h"
#include "window.h"
#include "world.h"
#include <junk/vector.h>
#include <time.h>
void _engine_insert_chunk_ptrs(struct engine* engine, struct chunk* chunk);
int engine_init(struct engine *engine) {
// Setup the Window
struct window* window = malloc(sizeof(struct window));
memset(window, 0, sizeof(struct window));
if (window_init(window) != 0) {
free(window);
return -1;
}
engine->window = window;
// Setup Shader pipeline
struct shader* shader = malloc(sizeof(struct shader));
memset(shader, 0, sizeof(struct shader));
if (shader_init(shader)) {
free(window);
free(shader);
return -1;
};
engine->shader = shader;
// Setup Objects to draw
vector_init(&engine->objects);
// Setup root chunk
struct world* world;
world_init(time(NULL), &world);
vec2 curr_chunk = { 0, 0 };
int chunk_distance = 2;
for (int i = 0; i < chunk_distance; i++) {
for (int j = 0; j < chunk_distance; j++) {
struct chunk* chunk;
vec2 chunk_coord = { curr_chunk[0] + i, curr_chunk[1] + j };
world_get_chunk(world, chunk_coord, &chunk);
_engine_insert_chunk_ptrs(engine, chunk);
}
}
engine->game_loop = 1;
return 0;
}
void _engine_insert_chunk_ptrs(struct engine* engine, struct chunk* chunk) {
int counter = 0;
for (int i = 0; i < CHUNK_WIDTH; i++) {
for (int j = 0; j < CHUNK_LENGTH; j++) {
for (int k = 0; k < CHUNK_HEIGHT; k++) {
struct block* blk = chunk->blocks[i][j][k];
if (blk == NULL) {
continue;
}
if (VECTOR_INSERT(engine->objects, (void*)blk) == -1) exit(1);
// block_debug(blk);
counter += 1;
}
}
}
}
void engine_draw(struct engine* engine) {
while (engine->game_loop) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
// Quit game
if (event.type == SDL_QUIT) {
engine->game_loop = 0;
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.529f, 0.808f, 0.922f, 1.0f);
glEnable(GL_DEPTH_TEST);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glUseProgram(engine->shader->program);
for (int i = 0; i < vector_length(engine->objects); i++) {
struct block* block = vector_get(engine->objects, i);
block_draw(block, engine->shader);
}
SDL_RenderPresent(engine->window->renderer);
}
}
|