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_node.h"
11 #include "client/client_player.h"
12 #include "client/client_terrain.h"
13 #include "client/debug_menu.h"
14 #include "client/font.h"
15 #include "client/frustum.h"
16 #include "client/gui.h"
17 #include "client/input.h"
18 #include "client/sky.h"
19 #include "client/window.h"
21 #include "interrupt.h"
25 static void crosshair_init()
27 gui_add(NULL, (GUIElementDefinition) {
32 .align = {0.5f, 0.5f},
33 .scale = {1.0f, 1.0f},
34 .scale_type = SCALE_IMAGE,
35 .affect_parent_scale = false,
37 .image = texture_load(RESSOURCE_PATH "textures/crosshair.png", false),
38 .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
39 .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
43 static void render(f64 dtime)
45 glEnable(GL_DEPTH_TEST);
46 glEnable(GL_ALPHA_TEST);
48 glEnable(GL_MULTISAMPLE);
49 glEnable(GL_CULL_FACE);
51 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
52 glAlphaFunc(GL_GREATER, 0.1f);
56 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62 model_scene_render(dtime);
66 static void game_loop()
68 f64 fps_update_timer = 1.0f;
69 unsigned int frames = 0;
71 struct timespec ts, ts_old;
72 clock_gettime(CLOCK_REALTIME, &ts_old);
74 while (!glfwWindowShouldClose(window.handle) && !interrupt.set) {
75 clock_gettime(CLOCK_REALTIME, &ts);
76 f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
79 if ((fps_update_timer -= dtime) <= 0.0) {
80 debug_menu_changed(ENTRY_FPS);
82 fps_update_timer += 1.0;
89 client_player_tick(dtime);
91 debug_menu_changed(ENTRY_TIME);
92 debug_menu_changed(ENTRY_DAYLIGHT);
93 debug_menu_changed(ENTRY_SUN_ANGLE);
98 glfwSwapBuffers(window.handle);
103 bool game(Flag *gfx_init)
116 if (!terrain_gfx_init())
120 client_terrain_start();
122 camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
123 camera_set_angle(0.0f, 0.0f);
135 client_terrain_stop();
141 terrain_gfx_deinit();
146 char *game_take_screenshot()
148 // renderbuffer for depth & stencil buffer
150 glGenRenderbuffers(1, &rbo);
151 glBindRenderbuffer(GL_RENDERBUFFER, rbo);
152 glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, window.width, window.height);
154 // 2 textures, one with AA, one without
157 glGenTextures(2, txos);
159 glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, txos[0]);
160 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, window.width, window.height, GL_TRUE);
162 glBindTexture(GL_TEXTURE_2D, txos[1]);
163 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window.width, window.height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
165 // 2 framebuffers, one with AA, one without
168 glGenFramebuffers(2, fbos);
170 glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]);
171 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, txos[0], 0);
172 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
174 glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]);
175 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, txos[1], 0);
178 glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]);
180 glBindFramebuffer(GL_FRAMEBUFFER, 0);
182 // blit AA-buffer into no-AA buffer
183 glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]);
184 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]);
185 glBlitFramebuffer(0, 0, window.width, window.height, 0, 0, window.width, window.height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
188 GLubyte data[window.width * window.height * 3];
189 glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]);
190 glPixelStorei(GL_PACK_ALIGNMENT, 1);
191 glReadPixels(0, 0, window.width, window.height, GL_RGB, GL_UNSIGNED_BYTE, data);
194 char filename[BUFSIZ];
195 time_t timep = time(0);
196 strftime(filename, BUFSIZ, "screenshot-%Y-%m-%d-%H:%M:%S.png", localtime(&timep));
199 stbi_flip_vertically_on_write(true);
200 stbi_write_png(filename, window.width, window.height, 3, data, window.width * 3);
203 glDeleteRenderbuffers(1, &rbo);
204 glDeleteTextures(2, txos);
205 glDeleteFramebuffers(2, fbos);
207 return strdup(filename);