1 #define STB_IMAGE_WRITE_IMPLEMENTATION
4 #include <GLFW/glfw3.h>
5 #include <stb/stb_image_write.h>
8 #include "client/camera.h"
9 #include "client/client.h"
10 #include "client/client_entity.h"
11 #include "client/client_node.h"
12 #include "client/client_player.h"
13 #include "client/client_terrain.h"
14 #include "client/debug_menu.h"
15 #include "client/font.h"
16 #include "client/frustum.h"
17 #include "client/game.h"
18 #include "client/gl_debug.h"
19 #include "client/gui.h"
20 #include "client/input.h"
21 #include "client/sky.h"
22 #include "client/window.h"
24 #include "interrupt.h"
28 static void crosshair_init()
30 gui_add(NULL, (GUIElementDefinition) {
35 .align = {0.5f, 0.5f},
36 .scale = {1.0f, 1.0f},
37 .scale_type = SCALE_IMAGE,
38 .affect_parent_scale = false,
40 .image = texture_load(RESSOURCE_PATH "textures/crosshair.png", false),
41 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
42 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
46 static void render(f64 dtime)
48 glEnable(GL_DEPTH_TEST); GL_DEBUG
49 glEnable(GL_BLEND); GL_DEBUG
50 glEnable(GL_MULTISAMPLE); GL_DEBUG
51 glEnable(GL_CULL_FACE); GL_DEBUG
53 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL_DEBUG
54 glCullFace(GL_BACK); GL_DEBUG
55 glFrontFace(GL_CCW); GL_DEBUG
57 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL_DEBUG
61 client_entity_gfx_update();
64 model_scene_render(dtime);
68 static void game_loop()
70 f64 fps_update_timer = 1.0f;
71 unsigned int frames = 0;
73 struct timespec ts, ts_old;
74 clock_gettime(CLOCK_REALTIME, &ts_old);
76 while (!glfwWindowShouldClose(window.handle) && !interrupt.set) {
77 clock_gettime(CLOCK_REALTIME, &ts);
78 f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
81 if ((fps_update_timer -= dtime) <= 0.0) {
82 debug_menu_changed(ENTRY_FPS);
84 fps_update_timer += 1.0;
91 client_player_tick(dtime);
93 debug_menu_changed(ENTRY_TIME);
94 debug_menu_changed(ENTRY_DAYLIGHT);
95 debug_menu_changed(ENTRY_SUN_ANGLE);
100 glfwSwapBuffers(window.handle);
105 bool game(Flag *gfx_init)
118 if (!terrain_gfx_init())
121 if (!client_entity_gfx_init())
124 client_player_gfx_init();
127 client_terrain_start();
129 camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
130 camera_set_angle(0.0f, 0.0f);
142 client_terrain_stop();
148 terrain_gfx_deinit();
149 client_entity_gfx_deinit();
150 client_player_gfx_deinit();
155 char *game_take_screenshot()
157 // renderbuffer for depth & stencil buffer
159 glGenRenderbuffers(1, &rbo); GL_DEBUG
160 glBindRenderbuffer(GL_RENDERBUFFER, rbo); GL_DEBUG
161 glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, window.width, window.height); GL_DEBUG
163 // 2 textures, one with AA, one without
166 glGenTextures(2, txos); GL_DEBUG
168 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, txos[0]); GL_DEBUG
169 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, window.width, window.height, GL_TRUE); GL_DEBUG
171 glBindTexture(GL_TEXTURE_2D, txos[1]); GL_DEBUG
172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window.width, window.height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); GL_DEBUG
174 // 2 framebuffers, one with AA, one without
177 glGenFramebuffers(2, fbos); GL_DEBUG
179 glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
180 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, txos[0], 0); GL_DEBUG
181 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); GL_DEBUG
183 glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
184 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, txos[1], 0); GL_DEBUG
187 glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
189 glBindFramebuffer(GL_FRAMEBUFFER, 0); GL_DEBUG
191 // blit AA-buffer into no-AA buffer
192 glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]); GL_DEBUG
193 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]); GL_DEBUG
194 glBlitFramebuffer(0, 0, window.width, window.height, 0, 0, window.width, window.height, GL_COLOR_BUFFER_BIT, GL_NEAREST); GL_DEBUG
197 GLubyte data[window.width * window.height * 3];
198 glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
199 glPixelStorei(GL_PACK_ALIGNMENT, 1); GL_DEBUG
200 glReadPixels(0, 0, window.width, window.height, GL_RGB, GL_UNSIGNED_BYTE, data); GL_DEBUG
203 char filename[BUFSIZ];
204 time_t timep = time(0);
205 strftime(filename, BUFSIZ, "screenshot-%Y-%m-%d-%H:%M:%S.png", localtime(&timep));
208 stbi_flip_vertically_on_write(true);
209 stbi_write_png(filename, window.width, window.height, 3, data, window.width * 3);
212 glDeleteRenderbuffers(1, &rbo); GL_DEBUG
213 glDeleteTextures(2, txos); GL_DEBUG
214 glDeleteFramebuffers(2, fbos); GL_DEBUG
216 return strdup(filename);