]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/game.c
Tweak and extend debug info
[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/hud.h"
13 #include "client/input.h"
14 #include "client/font.h"
15 #include "client/window.h"
16 #include "signal_handlers.h"
17
18 static void crosshair_init()
19 {
20         hud_add((HUDElementDefinition) {
21                 .type = HUD_IMAGE,
22                 .pos = {0.0f, 0.0f, 0.0f},
23                 .offset = {0, 0},
24                 .type_def = {
25                         .image = {
26                                 .texture = texture_get(RESSOURCEPATH "textures/crosshair.png"),
27                                 .scale = {1.0f, 1.0f},
28                                 .scale_type = HUD_SCALE_TEXTURE,
29                         },
30                 },
31         });
32 }
33
34 static void game_loop(Client *client)
35 {
36         f64 fps_update_timer = 1.0f;
37         int frames = 0;
38
39         struct timespec ts, ts_old;
40         clock_gettime(CLOCK_REALTIME, &ts_old);
41
42         while (! glfwWindowShouldClose(window.handle) && client->state != CS_DISCONNECTED && ! interrupted) {
43                 clock_gettime(CLOCK_REALTIME, &ts);
44                 f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
45                 ts_old = ts;
46
47                 if ((fps_update_timer -= dtime) <= 0.0) {
48                         debug_menu_update_fps(frames);
49                         fps_update_timer += 1.0;
50                         frames = 0;
51                 }
52
53                 frames++;
54
55                 glEnable(GL_DEPTH_TEST);
56                 glEnable(GL_ALPHA_TEST);
57                 glEnable(GL_BLEND);
58                 glEnable(GL_MULTISAMPLE);
59                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
60                 glAlphaFunc(GL_GREATER, 0.0f);
61                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
62                 glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
63
64                 input_tick();
65                 client_player_tick(dtime);
66
67                 scene_render();
68                 hud_render();
69
70                 glfwSwapBuffers(window.handle);
71                 glfwPollEvents();
72         }
73 }
74
75 bool game(Client *client)
76 {
77         int width, height;
78         width = 1250;
79         height = 750;
80
81         if (! window_init(width, height))
82                 return false;
83
84         if (! font_init())
85                 return false;
86
87         if (! scene_init())
88                 return false;
89
90         scene_on_resize(width, height);
91
92         client_node_init();
93         client_map_start();
94
95         camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
96         camera_set_angle(0.0f, 0.0f);
97
98         if (! hud_init())
99                 return false;
100
101         hud_on_resize(width, height);
102
103         debug_menu_init();
104         debug_menu_toggle();
105         debug_menu_update_fps(0);
106         debug_menu_update_version();
107         debug_menu_update_flight();
108         debug_menu_update_collision();
109         debug_menu_update_fullscreen();
110         debug_menu_update_opengl();
111         debug_menu_update_gpu();
112         debug_menu_update_glsl();
113
114         crosshair_init();
115
116         input_init();
117
118         client_player_add_to_scene();
119
120         game_loop(client);
121
122         client_map_stop();
123
124         font_deinit();
125         hud_deinit();
126         scene_deinit();
127
128         return true;
129 }