]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/game.c
Add fog and face culling
[dragonblocks_alpha.git] / src / client / game.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <GL/glew.h>
4 #include <GL/gl.h>
5 #include <GLFW/glfw3.h>
6 #include "client/camera.h"
7 #include "client/client.h"
8 #include "client/client_map.h"
9 #include "client/client_node.h"
10 #include "client/client_player.h"
11 #include "client/debug_menu.h"
12 #include "client/font.h"
13 #include "client/gui.h"
14 #include "client/input.h"
15 #include "client/sky.h"
16 #include "client/window.h"
17 #include "day.h"
18 #include "signal_handlers.h"
19
20 static void crosshair_init()
21 {
22         gui_add(&gui_root, (GUIElementDefinition) {
23                 .pos = {0.5f, 0.5f},
24                 .z_index = 0.0f,
25                 .offset = {0, 0},
26                 .margin = {0, 0},
27                 .align = {0.5f, 0.5f},
28                 .scale = {1.0f, 1.0f},
29                 .scale_type = GST_IMAGE,
30                 .affect_parent_scale = false,
31                 .text = NULL,
32                 .image = texture_get(RESSOURCEPATH "textures/crosshair.png"),
33                 .text_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.0f},
34                 .bg_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.0f},
35         });
36 }
37
38 static void game_loop(Client *client)
39 {
40         f64 fps_update_timer = 1.0f;
41         int frames = 0;
42
43         struct timespec ts, ts_old;
44         clock_gettime(CLOCK_REALTIME, &ts_old);
45
46         while (! glfwWindowShouldClose(window.handle) && client->state != CS_DISCONNECTED && ! interrupted) {
47                 clock_gettime(CLOCK_REALTIME, &ts);
48                 f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
49                 ts_old = ts;
50
51                 if ((fps_update_timer -= dtime) <= 0.0) {
52                         debug_menu_update_fps(frames);
53                         fps_update_timer += 1.0;
54                         frames = 0;
55                 }
56
57                 frames++;
58
59                 debug_menu_update_time();
60                 debug_menu_update_daylight();
61                 debug_menu_update_sun_angle();
62
63                 glEnable(GL_DEPTH_TEST);
64                 glEnable(GL_ALPHA_TEST);
65                 glEnable(GL_BLEND);
66                 glEnable(GL_MULTISAMPLE);
67                 glEnable(GL_CULL_FACE);
68                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
69                 glAlphaFunc(GL_GREATER, 0.0f);
70                 glCullFace(GL_BACK);
71                 glFrontFace(GL_CCW);
72                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
73
74                 input_tick();
75                 client_player_tick(dtime);
76
77                 scene_render();
78
79                 glDisable(GL_CULL_FACE);
80                 sky_render();
81
82                 glDisable(GL_DEPTH_TEST);
83                 gui_render();
84
85                 glfwSwapBuffers(window.handle);
86                 glfwPollEvents();
87         }
88 }
89
90 bool game(Client *client)
91 {
92         int width, height;
93         width = 1250;
94         height = 750;
95
96         if (! window_init(width, height))
97                 return false;
98
99         if (! font_init())
100                 return false;
101
102         if (! scene_init())
103                 return false;
104
105         scene_on_resize(width, height);
106
107         if (! sky_init())
108                 return false;
109
110         client_node_init();
111         client_map_start();
112
113         camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
114         camera_set_angle(0.0f, 0.0f);
115
116         if (! gui_init())
117                 return false;
118
119         gui_on_resize(width, height);
120
121         debug_menu_init();
122         debug_menu_toggle();
123         debug_menu_update_fps(0);
124         debug_menu_update_version();
125         debug_menu_update_seed();
126         debug_menu_update_flight();
127         debug_menu_update_collision();
128         debug_menu_update_timelapse();
129         debug_menu_update_fullscreen();
130         debug_menu_update_opengl();
131         debug_menu_update_gpu();
132
133         crosshair_init();
134
135         input_init();
136
137         client_player_add_to_scene();
138
139         game_loop(client);
140
141         client_map_stop();
142
143         font_deinit();
144         gui_deinit();
145         scene_deinit();
146         sky_deinit();
147
148         return true;
149 }