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
|
#include "engine.h"
#include "block.h"
#include "camera.h"
#include "cglm/io.h"
#include "chunk.h"
#include "input.h"
#include "texture.h"
#include "window.h"
#include "world.h"
#include <SDL2/SDL_render.h>
#include <bits/types/timer_t.h>
#include <junk/vector.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
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;
// Load Textures
struct texture* texture = { 0 };
texture_init(&engine->texture);
texture_load(engine->texture, "textures/grass.jpg");
// Setup Objects to draw
// memset(engine->loaded_chunks, 0, (1 + CHUNK_DISTANCE * 2) * (1 + CHUNK_DISTANCE * 2));
// Setup camera
camera_init(&engine->camera);
vec3 camera_pos = { 0.0f, 15.0f, 0.0f };
camera_set_position(engine->camera, camera_pos);
// Setup root chunk
struct world* world;
world_init(time(NULL), &world);
engine->world = world;
//TODO: Move this loop to a function and flip chunk_coord sign correctly ONCE
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// Load chunk
chunk_load(world, chunk, chunk_coord);
}
}
// Final step - Start the game
engine->game_loop = 1;
return 0;
}
void engine_update(struct engine* engine) {
//NOTE: OpenGL FLIP
int curr_chunk[2] = { floorf(engine->camera->position[0] / (float)CHUNK_WIDTH), floorf(-engine->camera->position[2] / (float)CHUNK_LENGTH) };
// Chunk update
struct chunk* c = {0};
world_get_chunk(engine->world, curr_chunk, &c);
// We moved a chunk - load new chunks with chunk_load
if (engine->curr_chunk[0] != curr_chunk[0] || engine->curr_chunk[1] != curr_chunk[1]) {
fprintf(stderr, "CHUNK Update! From (%d, %d) to (%d, %d)\n",
engine->curr_chunk[0],
engine->curr_chunk[1],
curr_chunk[0],
curr_chunk[1]);
// Unload existing chunks
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// unload chunk
// TODO: Fix some VAO/VBO bug when negative y
chunk_unload(chunk);
}
}
// Update the curr_chunk
memcpy(engine->curr_chunk, curr_chunk, sizeof(vec2));
// Load chunks of CHUNK_DISTANCE around curr_chunk
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk;
int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
// Load chunk
chunk_load(engine->world, chunk, chunk_coord);
}
}
}
}
void engine_fps(struct engine* engine, float fps) {
// TTF_Font* font = TTF_OpenFont("fonts/PixelifySans-Regular.ttf", 8.0f);
// SDL_RenderClear(engine->window->renderer);
// SDL_Color black = {0, 0, 0};
// char fps_text[36];
// snprintf(fps_text, 36, "FPS: %.2f", fps);
// SDL_Surface* text = TTF_RenderText_Solid(font, fps_text, black);
// SDL_Texture* texture = SDL_CreateTextureFromSurface(engine->window->renderer, text);
// SDL_RenderCopy(engine->window->renderer, texture, NULL, &rect);
}
void engine_start(struct engine* engine) {
float frames = 0;
time_t frame_last_time = time(NULL);
float fps = 0.0f;
while (engine->game_loop) {
time_t now = time(NULL);
time_t diff = now - frame_last_time;
// Calculate FPS every second
if (diff >= 1.0f) {
fps = frames / diff;
frames = 0;
frame_last_time = now;
fprintf(stderr, "FPS: %.2f\n", fps);
glm_vec3_print(engine->camera->position, stderr);
fprintf(stderr, "x: %d, y: %d\n", engine->curr_chunk[0], engine->curr_chunk[1]);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.529f, 0.808f, 0.922f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
//glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
glUseProgram(engine->shader->program);
// Update engine managed objects
engine_fps(engine, fps);
engine_update(engine);
camera_update(engine->camera, engine->shader);
for (int i = -CHUNK_DISTANCE; i <= CHUNK_DISTANCE; i++) {
for (int j = -CHUNK_DISTANCE; j <= CHUNK_DISTANCE; j++) {
struct chunk* chunk = {0};
int chunk_coord[2] = { engine->curr_chunk[0] + i, engine->curr_chunk[1] + j };
world_get_chunk(engine->world, chunk_coord, &chunk);
chunk_draw(chunk, engine->shader, engine->texture);
}
}
SDL_RenderPresent(engine->window->renderer);
frames += 1;
}
}
|