]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/game.c
Add diffuse lighting, improve skybox and add timelapse
[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                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
68                 glAlphaFunc(GL_GREATER, 0.0f);
69                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70
71                 input_tick();
72                 client_player_tick(dtime);
73
74                 scene_render();
75                 sky_render();
76                 gui_render();
77
78                 glfwSwapBuffers(window.handle);
79                 glfwPollEvents();
80         }
81 }
82
83 bool game(Client *client)
84 {
85         int width, height;
86         width = 1250;
87         height = 750;
88
89         if (! window_init(width, height))
90                 return false;
91
92         if (! font_init())
93                 return false;
94
95         if (! scene_init())
96                 return false;
97
98         scene_on_resize(width, height);
99
100         if (! sky_init())
101                 return false;
102
103         client_node_init();
104         client_map_start();
105
106         camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
107         camera_set_angle(0.0f, 0.0f);
108
109         if (! gui_init())
110                 return false;
111
112         gui_on_resize(width, height);
113
114         debug_menu_init();
115         debug_menu_toggle();
116         debug_menu_update_fps(0);
117         debug_menu_update_version();
118         debug_menu_update_seed();
119         debug_menu_update_flight();
120         debug_menu_update_collision();
121         debug_menu_update_timelapse();
122         debug_menu_update_fullscreen();
123         debug_menu_update_opengl();
124         debug_menu_update_gpu();
125
126         crosshair_init();
127
128         input_init();
129
130         client_player_add_to_scene();
131
132         game_loop(client);
133
134         client_map_stop();
135
136         font_deinit();
137         gui_deinit();
138         scene_deinit();
139         sky_deinit();
140
141         return true;
142 }