]> git.lizzy.rs Git - dragonblocks_alpha.git/blobdiff - src/client/game.c
You can now see other players
[dragonblocks_alpha.git] / src / client / game.c
index 182bae751810f2cf834f8e73f573c803c7ed9229..8cd2b7484f081ef755df614b313c8e5a2885ed24 100644 (file)
@@ -1,78 +1,86 @@
-#include <stdio.h>
-#include <unistd.h>
+#define STB_IMAGE_WRITE_IMPLEMENTATION
 #include <GL/glew.h>
 #include <GL/gl.h>
 #include <GLFW/glfw3.h>
-#define STB_IMAGE_WRITE_IMPLEMENTATION
 #include <stb/stb_image_write.h>
+#include <stdio.h>
+#include <unistd.h>
 #include "client/camera.h"
 #include "client/client.h"
-#include "client/client_map.h"
+#include "client/client_entity.h"
 #include "client/client_node.h"
 #include "client/client_player.h"
+#include "client/client_terrain.h"
 #include "client/debug_menu.h"
 #include "client/font.h"
+#include "client/frustum.h"
+#include "client/game.h"
+#include "client/gl_debug.h"
 #include "client/gui.h"
 #include "client/input.h"
 #include "client/sky.h"
 #include "client/window.h"
 #include "day.h"
-#include "signal_handlers.h"
+#include "interrupt.h"
 
-int window_width, window_height;
+int game_fps = 0;
 
 static void crosshair_init()
 {
-       gui_add(&gui_root, (GUIElementDefinition) {
+       gui_add(NULL, (GUIElementDefinition) {
                .pos = {0.5f, 0.5f},
                .z_index = 0.0f,
                .offset = {0, 0},
                .margin = {0, 0},
                .align = {0.5f, 0.5f},
                .scale = {1.0f, 1.0f},
-               .scale_type = GST_IMAGE,
+               .scale_type = SCALE_IMAGE,
                .affect_parent_scale = false,
                .text = NULL,
-               .image = texture_load(RESSOURCEPATH "textures/crosshair.png", false),
-               .text_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.0f},
-               .bg_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.0f},
+               .image = texture_load(RESSOURCE_PATH "textures/crosshair.png", false),
+               .text_color = {0.0f, 0.0f, 0.0f, 0.0f},
+               .bg_color = {0.0f, 0.0f, 0.0f, 0.0f},
        });
 }
 
 static void render(f64 dtime)
 {
-       glEnable(GL_DEPTH_TEST);
-       glEnable(GL_ALPHA_TEST);
-       glEnable(GL_BLEND);
-       glEnable(GL_MULTISAMPLE);
-       glEnable(GL_CULL_FACE);
-
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glAlphaFunc(GL_GREATER, 0.1f);
-       glCullFace(GL_BACK);
-       glFrontFace(GL_CCW);
-       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+       glEnable(GL_DEPTH_TEST); GL_DEBUG
+       glEnable(GL_BLEND); GL_DEBUG
+       glEnable(GL_MULTISAMPLE); GL_DEBUG
+       glEnable(GL_CULL_FACE); GL_DEBUG
+
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL_DEBUG
+       glCullFace(GL_BACK); GL_DEBUG
+       glFrontFace(GL_CCW); GL_DEBUG
+
+       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); GL_DEBUG
+
+       frustum_update();
+       terrain_gfx_update();
+       client_entity_gfx_update();
 
        sky_render();
-       scene_render(dtime);
+       model_scene_render(dtime);
        gui_render();
 }
 
-static void game_loop(Client *client)
+static void game_loop()
 {
        f64 fps_update_timer = 1.0f;
-       int frames = 0;
+       unsigned int frames = 0;
 
        struct timespec ts, ts_old;
        clock_gettime(CLOCK_REALTIME, &ts_old);
 
-       while (! glfwWindowShouldClose(window.handle) && client->state != CS_DISCONNECTED && ! interrupted) {
+       while (!glfwWindowShouldClose(window.handle) && !interrupt.set) {
                clock_gettime(CLOCK_REALTIME, &ts);
                f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
                ts_old = ts;
 
                if ((fps_update_timer -= dtime) <= 0.0) {
-                       debug_menu_update_fps(frames);
+                       debug_menu_changed(ENTRY_FPS);
+                       game_fps = frames;
                        fps_update_timer += 1.0;
                        frames = 0;
                }
@@ -82,9 +90,10 @@ static void game_loop(Client *client)
                input_tick(dtime);
                client_player_tick(dtime);
 
-               debug_menu_update_time();
-               debug_menu_update_daylight();
-               debug_menu_update_sun_angle();
+               debug_menu_changed(ENTRY_TIME);
+               debug_menu_changed(ENTRY_DAYLIGHT);
+               debug_menu_changed(ENTRY_SUN_ANGLE);
+               debug_menu_update();
 
                render(dtime);
 
@@ -93,112 +102,102 @@ static void game_loop(Client *client)
        }
 }
 
-bool game(Client *client)
+bool game(Flag *gfx_init)
 {
-       window_width = 1250;
-       window_height = 750;
-
-       if (! window_init(window_width, window_height))
+       if (!window_init())
                return false;
 
-       if (! font_init())
+       if (!font_init())
                return false;
 
-       if (! scene_init())
+       model_init();
+
+       if (!sky_init())
                return false;
 
-       scene_on_resize(window_width, window_height);
+       if (!terrain_gfx_init())
+               return false;
 
-       if (! sky_init())
+       if (!client_entity_gfx_init())
                return false;
 
+       client_player_gfx_init();
+
        client_node_init();
-       client_map_start();
+       client_terrain_start();
 
        camera_set_position((v3f32) {0.0f, 0.0f, 0.0f});
        camera_set_angle(0.0f, 0.0f);
 
-       if (! gui_init())
+       if (!gui_init())
                return false;
 
-       gui_on_resize(window_width, window_height);
-
        debug_menu_init();
-       debug_menu_toggle();
-       debug_menu_update_fps(0);
-       debug_menu_update_version();
-       debug_menu_update_seed();
-       debug_menu_update_flight();
-       debug_menu_update_collision();
-       debug_menu_update_timelapse();
-       debug_menu_update_fullscreen();
-       debug_menu_update_opengl();
-       debug_menu_update_gpu();
-
        crosshair_init();
-
        input_init();
 
-       client_player_add_to_scene();
-
-       game_loop(client);
+       flag_set(gfx_init);
+       game_loop();
 
-       client_map_stop();
+       client_terrain_stop();
 
        font_deinit();
        gui_deinit();
-       scene_deinit();
+       model_deinit();
        sky_deinit();
+       terrain_gfx_deinit();
+       client_entity_gfx_deinit();
+       client_player_gfx_deinit();
 
        return true;
 }
 
-char *take_screenshot()
+char *game_take_screenshot()
 {
        // renderbuffer for depth & stencil buffer
-       GLuint RBO;
-       glGenRenderbuffers(1, &RBO);
-       glBindRenderbuffer(GL_RENDERBUFFER, RBO);
-       glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, window_width, window_height);
+       GLuint rbo;
+       glGenRenderbuffers(1, &rbo); GL_DEBUG
+       glBindRenderbuffer(GL_RENDERBUFFER, rbo); GL_DEBUG
+       glRenderbufferStorageMultisample(GL_RENDERBUFFER, 8, GL_DEPTH24_STENCIL8, window.width, window.height); GL_DEBUG
 
        // 2 textures, one with AA, one without
 
-       GLuint textures[2];
-       glGenTextures(2, textures);
+       GLuint txos[2];
+       glGenTextures(2, txos); GL_DEBUG
 
-       glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textures[0]);
-       glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, window_width, window_height, GL_TRUE);
+       glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, txos[0]); GL_DEBUG
+       glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB, window.width, window.height, GL_TRUE); GL_DEBUG
 
-       glBindTexture(GL_TEXTURE_2D, textures[1]);
-       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window_width, window_height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
+       glBindTexture(GL_TEXTURE_2D, txos[1]); GL_DEBUG
+       glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, window.width, window.height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); GL_DEBUG
 
        // 2 framebuffers, one with AA, one without
 
-       GLuint FBOs[2];
-       glGenFramebuffers(2, FBOs);
+       GLuint fbos[2];
+       glGenFramebuffers(2, fbos); GL_DEBUG
 
-       glBindFramebuffer(GL_FRAMEBUFFER, FBOs[0]);
-       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, textures[0], 0);
-       glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);
+       glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
+       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D_MULTISAMPLE, txos[0], 0); GL_DEBUG
+       glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo); GL_DEBUG
 
-       glBindFramebuffer(GL_FRAMEBUFFER, FBOs[1]);
-       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
+       glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
+       glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, txos[1], 0); GL_DEBUG
 
        // render scene
-       glBindFramebuffer(GL_FRAMEBUFFER, FBOs[0]);
+       glBindFramebuffer(GL_FRAMEBUFFER, fbos[0]); GL_DEBUG
        render(0.0);
-       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+       glBindFramebuffer(GL_FRAMEBUFFER, 0); GL_DEBUG
 
        // blit AA-buffer into no-AA buffer
-       glBindFramebuffer(GL_READ_FRAMEBUFFER, FBOs[0]);
-       glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBOs[1]);
-       glBlitFramebuffer(0, 0, window_width, window_height, 0, 0, window_width, window_height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
+       glBindFramebuffer(GL_READ_FRAMEBUFFER, fbos[0]); GL_DEBUG
+       glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbos[1]); GL_DEBUG
+       glBlitFramebuffer(0, 0, window.width, window.height, 0, 0, window.width, window.height, GL_COLOR_BUFFER_BIT, GL_NEAREST); GL_DEBUG
 
        // read data
-       GLubyte data[window_width * window_height * 3];
-       glBindFramebuffer(GL_FRAMEBUFFER, FBOs[1]);
-       glPixelStorei(GL_PACK_ALIGNMENT, 1);
-       glReadPixels(0, 0, window_width, window_height, GL_RGB, GL_UNSIGNED_BYTE, data);
+       GLubyte data[window.width * window.height * 3];
+       glBindFramebuffer(GL_FRAMEBUFFER, fbos[1]); GL_DEBUG
+       glPixelStorei(GL_PACK_ALIGNMENT, 1); GL_DEBUG
+       glReadPixels(0, 0, window.width, window.height, GL_RGB, GL_UNSIGNED_BYTE, data); GL_DEBUG
 
        // create filename
        char filename[BUFSIZ];
@@ -207,18 +206,12 @@ char *take_screenshot()
 
        // save screenshot
        stbi_flip_vertically_on_write(true);
-       stbi_write_png(filename, window_width, window_height, 3, data, window_width * 3);
+       stbi_write_png(filename, window.width, window.height, 3, data, window.width * 3);
 
        // delete buffers
-       glDeleteRenderbuffers(1, &RBO);
-       glDeleteTextures(2, textures);
-       glDeleteFramebuffers(2, FBOs);
+       glDeleteRenderbuffers(1, &rbo); GL_DEBUG
+       glDeleteTextures(2, txos); GL_DEBUG
+       glDeleteFramebuffers(2, fbos); GL_DEBUG
 
        return strdup(filename);
 }
-
-void game_on_resize(int width, int height)
-{
-       window_width = width;
-       window_height = height;
-}