From f4844fd7fcb0924a87809e46b6c5567f463022d3 Mon Sep 17 00:00:00 2001 From: Zack Buhman Date: Wed, 25 Mar 2026 14:51:31 -0500 Subject: [PATCH] standalone application --- .gitignore | 3 +- Makefile | 11 ++-- include/declarations.h | 14 ---- include/lua_api.h | 18 ----- include/pixel_line_art.h | 12 ---- include/test.h | 36 +++++----- include/window.h | 4 +- sdl3-build.sh | 3 + src/collada/scene.cpp | 1 - src/flame.cpp | 1 - src/line_art.cpp | 1 - src/lua_api.cpp | 45 ------------- src/main.c | 53 --------------- src/main.cpp | 80 +++++++++++++++++++++++ src/minecraft.cpp | 1 - src/non_block.cpp | 1 - src/pixel_line_art.cpp | 138 --------------------------------------- src/test.cpp | 17 ++--- 18 files changed, 118 insertions(+), 321 deletions(-) delete mode 100644 include/declarations.h delete mode 100644 include/lua_api.h delete mode 100644 include/pixel_line_art.h create mode 100644 sdl3-build.sh delete mode 100644 src/lua_api.cpp delete mode 100644 src/main.c create mode 100644 src/main.cpp delete mode 100644 src/pixel_line_art.cpp diff --git a/.gitignore b/.gitignore index c7c3865..8885f53 100644 --- a/.gitignore +++ b/.gitignore @@ -21,4 +21,5 @@ minecraft/region*.lights.vtx minecraft/region*.dump minecraft/gen/map.txt test.pack -pack_main \ No newline at end of file +pack_main +.gdb_history \ No newline at end of file diff --git a/Makefile b/Makefile index df9e6ca..56fb9de 100644 --- a/Makefile +++ b/Makefile @@ -5,14 +5,13 @@ OBJCOPY=$(PREFIX)objcopy OBJARCH = elf64-x86-64 -OPT = -O2 -march=x86-64-v3 +OPT = -Og -march=x86-64-v3 -#DEBUG = -g +DEBUG = -g CSTD = -std=gnu23 CXXSTD = -std=gnu++23 CFLAGS += -fpic -CFLAGS += -I./include CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -Wno-error=unused-but-set-variable CFLAGS += -Wno-error=unknown-pragmas -Wno-unknown-pragmas CFLAGS += -Wno-error=unused-function @@ -21,6 +20,8 @@ CFLAGS += -fno-strict-aliasing ifdef READ_PACK_FILE CFLAGS += -DREAD_PACK_FILE endif +CFLAGS += -I./include +CFLAGS += -I../SDL3-dist/include LDFLAGS += -lm @@ -67,7 +68,7 @@ ifdef READ_PACK_FILE OBJS += test.pack.o endif -all: test.so +all: main %.o: %.c $(CC) $(ARCH) $(CSTD) $(CFLAGS) $(OPT) $(DEBUG) -c $< -o $@ @@ -88,7 +89,7 @@ test.so: $(OBJS) test.dll: $(OBJS) $(CXX) $(ARCH) $(OPT) -mthreads -static -mdll -static-libstdc++ -static-libgcc $(DEBUG) $^ -o $@ -L. -lSDL3 $(WINDOWS) -main: $(OBJS) src/main.o +main: $(OBJS) src/main.o ../SDL3-dist/lib64/libSDL3.a $(CC) $(ARCH) $(LDFLAGS) $(OPT) $(DEBUG) $^ -o $@ clean: diff --git a/include/declarations.h b/include/declarations.h deleted file mode 100644 index 5f75844..0000000 --- a/include/declarations.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#ifdef _WIN32 -#ifdef _MSC_VER -#define EXPORT __declspec(dllexport) -#define DECL __cdecl -#else -#define EXPORT __attribute__((dllexport)) -#define DECL __attribute__((__cdecl__)) -#endif -#else -#define EXPORT -#define DECL -#endif diff --git a/include/lua_api.h b/include/lua_api.h deleted file mode 100644 index 5d78dab..0000000 --- a/include/lua_api.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - - int draw_font_start(); - int draw_font(int font_ix, char const * text, int x, int y); - - void draw_line_quad_start(); - void draw_line(int x1, int y1, int x2, int y2); - void draw_set_color(float r, float g, float b); - void draw_quad(int x1, int y1, int x2, int y2, - int x3, int y3, int x4, int y4); - -#ifdef __cplusplus -} -#endif diff --git a/include/pixel_line_art.h b/include/pixel_line_art.h deleted file mode 100644 index 8621b9a..0000000 --- a/include/pixel_line_art.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -namespace pixel_line_art { - - void load(); - - void draw_line_quad_start(); - void draw_line(int x1, int y1, int x2, int y2); - void draw_set_color(float r, float g, float b); - void draw_quad(int x1, int y1, int x2, int y2, - int x3, int y3, int x4, int y4); -} diff --git a/include/test.h b/include/test.h index b93b346..94e80e7 100644 --- a/include/test.h +++ b/include/test.h @@ -1,28 +1,26 @@ #pragma once -#include "declarations.h" - #ifdef __cplusplus extern "C" { #endif - EXPORT void DECL load(const char * source_path); - EXPORT void DECL draw(); - EXPORT void DECL love2d_state_load(); - EXPORT void DECL love2d_state_restore(); - EXPORT void DECL update_keyboard(int up, int down, int left, int right, - int w, int s, int a, int d, - int t, int g, int f, int h, - int i, int k, int j, int l, - int q, int e); - EXPORT void DECL update_mouse(int x, int y); - EXPORT void DECL update_joystick(int joystick_index, - float lx, float ly, float rx, float ry, float tl, float tr, - int up, int down, int left, int right, - int a, int b, int x, int y, - int leftshoulder, int rightshoulder, - int start); - EXPORT void DECL update(float time); + void load(const char * source_path); + void draw(); + void love2d_state_load(); + void love2d_state_restore(); + void update_keyboard(int up, int down, int left, int right, + int w, int s, int a, int d, + int t, int g, int f, int h, + int i, int k, int j, int l, + int q, int e); + void update_mouse(int x, int y); + void update_joystick(int joystick_index, + float lx, float ly, float rx, float ry, float tl, float tr, + int up, int down, int left, int right, + int a, int b, int x, int y, + int leftshoulder, int rightshoulder, + int start); + void update(float time); #ifdef __cplusplus } diff --git a/include/window.h b/include/window.h index 70467ff..a956ffa 100644 --- a/include/window.h +++ b/include/window.h @@ -1,7 +1,5 @@ #pragma once -#include "declarations.h" - namespace window { extern float width; extern float height; @@ -10,7 +8,7 @@ namespace window { #ifdef __cplusplus extern "C" { #endif - EXPORT void DECL update_window(int width, int height); + void update_window(int width, int height); #ifdef __cplusplus } #endif diff --git a/sdl3-build.sh b/sdl3-build.sh new file mode 100644 index 0000000..5993798 --- /dev/null +++ b/sdl3-build.sh @@ -0,0 +1,3 @@ +cmake ../SDL3-3.4.2/ -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=../SDL3-dist +cmake --build . --config Debug +cmake --install . --config Debug diff --git a/src/collada/scene.cpp b/src/collada/scene.cpp index 89703e8..0a42eed 100644 --- a/src/collada/scene.cpp +++ b/src/collada/scene.cpp @@ -506,7 +506,6 @@ namespace collada::scene { glUniform1i(layout.uniform.specular_sampler, 3); } - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); glDisable(GL_CULL_FACE); //glCullFace(GL_FRONT); diff --git a/src/flame.cpp b/src/flame.cpp index 1794bc6..1659674 100644 --- a/src/flame.cpp +++ b/src/flame.cpp @@ -83,7 +83,6 @@ namespace flame { glUseProgram(program); glBlendFunc(GL_ONE, GL_ZERO); - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); glActiveTexture(GL_TEXTURE0); diff --git a/src/line_art.cpp b/src/line_art.cpp index 548c9cb..a78868e 100644 --- a/src/line_art.cpp +++ b/src/line_art.cpp @@ -244,7 +244,6 @@ namespace line_art { glUseProgram(program); glBlendFunc(GL_ONE, GL_ZERO); - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_ALWAYS); glBindVertexArray(vertex_array_object); diff --git a/src/lua_api.cpp b/src/lua_api.cpp deleted file mode 100644 index aa891a2..0000000 --- a/src/lua_api.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "font/bitmap.h" -#include "pixel_line_art.h" - -#include "lua_api.h" - -extern font::bitmap::font * terminus_fonts; -extern unsigned int empty_vertex_array_object; -extern unsigned int quad_index_buffer; - -int draw_font_start() -{ - int font_ix = font::bitmap::best_font(font::bitmap::terminus, font::bitmap::terminus_length); - font::bitmap::font const& ter_best = terminus_fonts[font_ix]; - font::bitmap::draw_start(ter_best, empty_vertex_array_object, quad_index_buffer); - return font_ix; -} - -int draw_font(int font_ix, char const * text, int x, int y) -{ - font::bitmap::font const& ter_best = terminus_fonts[font_ix]; - font::bitmap::draw_string(ter_best, text, x, y); - return ter_best.desc->glyph_height; -} - -void draw_line_quad_start() -{ - pixel_line_art::draw_line_quad_start(); -} - -void draw_line(int x1, int y1, int x2, int y2) -{ - pixel_line_art::draw_line(x1, y1, x2, y2); -} - -void draw_set_color(float r, float g, float b) -{ - pixel_line_art::draw_set_color(r, g, b); -} - -void draw_quad(int x1, int y1, int x2, int y2, - int x3, int y3, int x4, int y4) -{ - pixel_line_art::draw_quad(x1, y1, x2, y2, - x3, y3, x4, y4); -} diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 56fcec2..0000000 --- a/src/main.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "glad/gl.h" -#include - -extern void load(); -extern void draw(); - -int main() -{ - glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11); - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); - - GLFWwindow* window = glfwCreateWindow(1024, 1024, "LearnOpenGL", NULL, NULL); - if (window == NULL) { - const char* description; - glfwGetError(&description); - printf("Failed to create GLFW window: %s\n", description); - glfwTerminate(); - return -1; - } - glfwMakeContextCurrent(window); - gladLoadGL(glfwGetProcAddress); - - glViewport(0, 0, 1024, 1024); - - load(); - - while(!glfwWindowShouldClose(window)) { - if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(window, true); - - draw(); - - glfwSwapBuffers(window); - glfwPollEvents(); - } - - glfwTerminate(); - - return 0; -} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..4f37ba2 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,80 @@ +#include + +#include + +#include "glad/gl.h" + +#include "test.h" +#include "window.h" +#include "view.h" + +int main() +{ + SDL_SetAppMetadata("Bibliotheca", "1.0", "st.idk.bibliotheca"); + + bool ret; + ret = SDL_Init(SDL_INIT_VIDEO); + if (!ret) { + fprintf(stderr, "SDL_Init(SDL_INIT_VIDEO): %s\n", SDL_GetError()); + return 1; + } + + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + + SDL_WindowFlags window_flags + = SDL_WINDOW_RESIZABLE + | SDL_WINDOW_OPENGL + ; + SDL_Window * window = SDL_CreateWindow("Bibliotheca", 1024, 1024, window_flags); + if (window == NULL) { + fprintf(stderr, "SDL_CreateWindow: %s\n", SDL_GetError()); + return 1; + } + + SDL_GLContext context = SDL_GL_CreateContext(window); + if (context == NULL) { + fprintf(stderr, "SDL_GL_CreateContext: %s\n", SDL_GetError()); + return 1; + } + int version = gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress); + if (version == 0) { + fprintf(stderr, "gladLoadGL\n"); + } + + load("."); + + update_window(1024, 1024); + + while (true) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_EVENT_QUIT: + goto exit; + case SDL_EVENT_KEY_DOWN: + if (event.key.key == SDLK_ESCAPE) + goto exit; + break; + case SDL_EVENT_WINDOW_RESIZED: + printf("%d %d\n", event.window.data1, event.window.data2); + break; + default: + break; + } + } + + view::update_transforms(); + draw(); + + SDL_GL_SwapWindow(window); + } + + exit: + SDL_GL_DestroyContext(context); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; +} diff --git a/src/minecraft.cpp b/src/minecraft.cpp index f531d2c..3e12ec1 100644 --- a/src/minecraft.cpp +++ b/src/minecraft.cpp @@ -284,7 +284,6 @@ namespace minecraft { glUseProgram(program); glBlendFunc(GL_ONE, GL_ZERO); - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); glActiveTexture(GL_TEXTURE0); diff --git a/src/non_block.cpp b/src/non_block.cpp index d732578..3d29a23 100644 --- a/src/non_block.cpp +++ b/src/non_block.cpp @@ -109,7 +109,6 @@ namespace non_block { glUseProgram(program); glBlendFunc(GL_ONE, GL_ZERO); - glEnable(GL_DEPTH_TEST); glDepthFunc(GL_GREATER); XMVECTOR offset = view::state.at; diff --git a/src/pixel_line_art.cpp b/src/pixel_line_art.cpp deleted file mode 100644 index 4d98296..0000000 --- a/src/pixel_line_art.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "glad/gl.h" -#include "directxmath/directxmath.h" -#include - -#include "opengl.h" -#include "window.h" - -#include "pixel_line_art.h" - -extern unsigned int quad_index_buffer; - -namespace pixel_line_art { - - struct layout { - struct { - unsigned int position; - } attribute; - struct { - unsigned int transform; - unsigned int base_color; - } uniform; - }; - - const layout layout = { - .attribute = { - .position = 0, - }, - .uniform = { - .transform = 0, - .base_color = 1, - }, - }; - - static unsigned int program; - - static unsigned int vertex_array_object; - static unsigned int per_vertex_buffer; - static int const per_vertex_size = (sizeof (float)) * 2; - - static void load_program() - { - program = compile_from_files("shader/pixel_line_art.vert", - nullptr, - "shader/pixel_line_art.frag"); - } - - static void load_vertex_attributes() - { - glGenVertexArrays(1, &vertex_array_object); - glBindVertexArray(vertex_array_object); - - glVertexBindingDivisor(0, 0); - - glEnableVertexAttribArray(layout.attribute.position); - glVertexAttribFormat(layout.attribute.position, 2, GL_FLOAT, GL_FALSE, 0); - glVertexAttribBinding(layout.attribute.position, 0); - - glBindVertexArray(0); - } - - static void load_per_vertex_buffer(int x1, int y1, int x2, int y2) - { - float vertex_data[] = { - (float)x1, (float)y1, (float)x2, (float)y2, - }; - int vertex_data_size = (sizeof (vertex_data)); - - glBindBuffer(GL_ARRAY_BUFFER, per_vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertex_data_size, vertex_data, GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - - static void load_per_vertex_buffer2(int x1, int y1, int x2, int y2, - int x3, int y3, int x4, int y4) - { - float vertex_data[] = { - (float)x1, (float)y1, (float)x2, (float)y2, - (float)x3, (float)y3, (float)x4, (float)y4, - }; - int vertex_data_size = (sizeof (vertex_data)); - - glBindBuffer(GL_ARRAY_BUFFER, per_vertex_buffer); - glBufferData(GL_ARRAY_BUFFER, vertex_data_size, vertex_data, GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); - } - - void load() - { - load_program(); - load_vertex_attributes(); - - glGenBuffers(1, &per_vertex_buffer); - } - - static void set_transform(XMMATRIX const & transform) - { - XMFLOAT4X4 float_transform; - XMStoreFloat4x4(&float_transform, transform); - glUniformMatrix4fv(layout.uniform.transform, 1, false, (float *)&float_transform); - } - - void draw_line_quad_start() - { - glUseProgram(program); - - glBlendFunc(GL_ONE, GL_ZERO); - glEnable(GL_DEPTH_TEST); - glDepthFunc(GL_ALWAYS); - - glBindVertexArray(vertex_array_object); - glBindVertexBuffer(0, per_vertex_buffer, 0, per_vertex_size); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_index_buffer); - - XMMATRIX transform - = XMMatrixScaling(2.0f / window::width, -2.0f / window::height, 0) - * XMMatrixTranslation(-1, 1, 0); - set_transform(transform); - } - - void draw_line(int x1, int y1, int x2, int y2) - { - load_per_vertex_buffer(x1, y1, x2, y2); - glDrawArrays(GL_LINES, 0, 2); - } - - void draw_quad(int x1, int y1, int x2, int y2, - int x3, int y3, int x4, int y4) - { - load_per_vertex_buffer2(x1, y1, x2, y2, - x3, y3, x4, y4); - glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (void *)0); - } - - void draw_set_color(float r, float g, float b) - { - glUniform3f(layout.uniform.base_color, r, g, b); - } -} diff --git a/src/test.cpp b/src/test.cpp index 6b6d526..1c882c1 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -2,6 +2,8 @@ #include #include +#include + #include "geometry_buffer.h" #include "glad/gl.h" #include "opengl.h" @@ -29,7 +31,6 @@ #include "flame.h" #include "new.h" #include "popcount.h" -#include "declarations.h" #include "world/entry_table.h" #include "world/world.h" @@ -116,17 +117,12 @@ void load_quad_program() quad_program = program; } -extern "C" void * DECL SDL_GL_GetProcAddress(const char *proc); - void load(const char * source_path) { file::source_path_length = strlen(source_path); assert(source_path[file::source_path_length - 1] != '/'); file::source_path = source_path; - fprintf(stderr, "getproc %p\n", SDL_GL_GetProcAddress); - gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress); - // glBindVertexArray(0); @@ -213,6 +209,13 @@ void load(const char * source_path) flame::load_program(); flame::load_texture(); + + ////////////////////////////////////////////////////////////////////// + // opengl state + ////////////////////////////////////////////////////////////////////// + + glEnable(GL_DEPTH_TEST); + glEnable(GL_BLEND); } void check_collisions(collision::Sphere const & sphere, XMVECTOR const & direction, @@ -413,8 +416,6 @@ void update(float time) else view::state.at = XMVector3Transform(XMVectorZero(), node_at->world); */ - - view::update_transforms(); } void draw_quad()