From 235926dadb686589f0b5480162c3ab929159e570 Mon Sep 17 00:00:00 2001 From: navewindre Date: Wed, 10 Sep 2025 12:25:00 +0200 Subject: unfuck --- CMakeLists.txt | 13 ++++---- assets/shaders/2d_texcoord.fsh | 5 ++-- assets/shaders/3d.fsh | 5 ++-- src/render/gl.cpp | 26 ++++------------ src/render/gl_2d_font.cpp | 28 +++++++----------- src/util/allocator.h | 12 ++++++++ src/util/config/config.cpp | 67 ++++++++++++++++++------------------------ 7 files changed, 67 insertions(+), 89 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e99269a..fc2ce17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,11 @@ pkg_check_modules(FREETYPE REQUIRED freetype2) pkg_check_modules(GLEW REQUIRED glew) find_package(OpenGL REQUIRED) +# release build settings +set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wno-nontrivial-memcall") +# debug build settings +set(CMAKE_CXX_FLAGS_DEBUG "-g -DDEBUG=1 -fsanitize=address -Wno-nontrivial-memcall") -target_compile_options(app PRIVATE -Wall) target_include_directories(app PRIVATE ${SDL2_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIRS} @@ -47,10 +50,8 @@ else() ) endif() -# release build settings -set(CMAKE_CXX_FLAGS_RELEASE "-O3 -Wno-nontrivial-memcall") -# debug build settings -set(CMAKE_CXX_FLAGS_DEBUG "-g -DDEBUG=1 -fsanitize=address -Wno-nontrivial-memcall") +target_compile_options(app PRIVATE -Wall) +target_compile_options(app PRIVATE -Wno-nontrivial-memcall) add_custom_target(debug COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=Debug ${CMAKE_BINARY_DIR} @@ -63,4 +64,4 @@ add_custom_target(release COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMENT "Building ~~**RELEASE**~~" -) \ No newline at end of file +) diff --git a/assets/shaders/2d_texcoord.fsh b/assets/shaders/2d_texcoord.fsh index cf94a68..cbb9be0 100644 --- a/assets/shaders/2d_texcoord.fsh +++ b/assets/shaders/2d_texcoord.fsh @@ -11,10 +11,9 @@ flat in int g_sampler; void main() { vec4 color = g_color; if( g_sampler != SAMPLER_ID_NONE ) { - // Always use sampler 0 to avoid dynamic indexing issues - vec4 tex_color = texture2D( g_samplers[0], g_texcoord ); + vec4 tex_color = texture2D( g_samplers[g_sampler], g_texcoord ); color.a = g_color.a * tex_color.a; if (color.a < 0.01) discard; } gl_FragColor = color; -} \ No newline at end of file +} diff --git a/assets/shaders/3d.fsh b/assets/shaders/3d.fsh index 7660204..276f305 100644 --- a/assets/shaders/3d.fsh +++ b/assets/shaders/3d.fsh @@ -11,9 +11,8 @@ flat in uint g_sampler; void main() { vec4 color = g_color; if( g_sampler != SAMPLER_ID_NONE ) { - // Always use sampler 0 for fonts - color = texture2D( g_samplers[0], g_texcoord ); + color = texture2D( g_samplers[g_sampler], g_texcoord ); color *= g_color; } gl_FragColor = color; -} \ No newline at end of file +} diff --git a/src/render/gl.cpp b/src/render/gl.cpp index 6654dd4..c084db0 100644 --- a/src/render/gl.cpp +++ b/src/render/gl.cpp @@ -43,7 +43,7 @@ GL_DATA* gl_create( I32* _canvas ) { GL_DATA* gl = new GL_DATA; gl->window = SDL_CreateWindow( "game", - SDL_WINDOWPOS_CENTERED, // Use centered position + SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _canvas[0], _canvas[1], @@ -64,6 +64,7 @@ GL_DATA* gl_create( I32* _canvas ) { return 0; } + SDL_GL_SetSwapInterval(0); SDL_GL_MakeCurrent( gl->window, gl->ctx ); GLenum glewError = glewInit(); @@ -75,17 +76,8 @@ GL_DATA* gl_create( I32* _canvas ) { return 0; } - dlog( "OpenGL Version: %s\n", glGetString(GL_VERSION) ); - dlog( "GLSL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION) ); - dlog( "Renderer: %s\n", glGetString(GL_RENDERER) ); - dlog( "Vendor: %s\n", glGetString(GL_VENDOR) ); - - // Set up V-Sync (0 = off, 1 = on, -1 = adaptive) - SDL_GL_SetSwapInterval(1); - - // Clear any OpenGL errors that might have occurred during initialization - while( glGetError() != GL_NO_ERROR ); - + while( glGetError() != GL_NO_ERROR ); // clear errors + // glGetIntegerv( GL_MAX_TEXTURE_IMAGE_UNITS, &gl->shader_texture_limit ); if( gl->shader_texture_limit > 255 ) gl->shader_texture_limit = 255; @@ -96,28 +88,22 @@ GL_DATA* gl_create( I32* _canvas ) { memcpy( gl->canvas_size, _canvas, sizeof(I32) * 2 ); gl->clip_start = { 0, 0 }; gl->clip_dim = { (F32)gl->canvas_size[0], (F32)gl->canvas_size[1] }; - - // Set initial viewport glViewport( 0, 0, gl->canvas_size[0], gl->canvas_size[1] ); - - // Set initial clear color (so you can see if clearing is working) - glClearColor( 0.2f, 0.2f, 0.2f, 1.0f ); gl_inst = gl; return gl; } void gl_gen_buffers( GL_DATA* gl ) { - // Generate and bind VAO first glGenVertexArrays( 1, &gl->vao ); glBindVertexArray( gl->vao ); - + glGenBuffers( 1, &gl->vbuffer ); glBindBuffer( GL_ARRAY_BUFFER, gl->vbuffer ); glBufferData( GL_ARRAY_BUFFER, 8192, 0, GL_STATIC_DRAW ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); - glBindVertexArray( 0 ); // Unbind VAO + glBindVertexArray( 0 ); } void gl_destroy( GL_DATA *gl ) { diff --git a/src/render/gl_2d_font.cpp b/src/render/gl_2d_font.cpp index a104124..329f0be 100644 --- a/src/render/gl_2d_font.cpp +++ b/src/render/gl_2d_font.cpp @@ -211,31 +211,23 @@ void gl_font_draw( GL_FONT* font, GL_SHADER_PROGRAM* shader, VEC2 origin, const glBindBuffer( GL_ARRAY_BUFFER, shader->gl->vbuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(VERTEX) * 6 * len, vertices, GL_STATIC_DRAW ); - + I32 position = glGetAttribLocation( shader->id, "in_pos" ); I32 color = glGetAttribLocation( shader->id, "in_clr" ); I32 texcoord = glGetAttribLocation( shader->id, "in_texcoord" ); I32 sampler = glGetAttribLocation( shader->id, "in_sampler" ); - - if (position >= 0) { - glEnableVertexAttribArray( position ); - glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->pos ); - } - if (color >= 0) { - glEnableVertexAttribArray( color ); - glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr ); - } + glEnableVertexAttribArray( position ); + glVertexAttribPointer( position, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->pos ); - if (texcoord >= 0) { - glEnableVertexAttribArray( texcoord ); - glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv ); - } + glEnableVertexAttribArray( color ); + glVertexAttribPointer( color, 4, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->clr ); - if (sampler >= 0) { - glEnableVertexAttribArray( sampler ); - glVertexAttribPointer( sampler, 1, GL_UNSIGNED_BYTE, 1, sizeof(VERTEX), &( (VERTEX*)nullptr)->sampler ); - } + glEnableVertexAttribArray( texcoord ); + glVertexAttribPointer( texcoord, 2, GL_FLOAT, 0, sizeof(VERTEX), &( (VERTEX*)nullptr)->uv ); + + glEnableVertexAttribArray( sampler ); + glVertexAttribPointer( sampler, 1, GL_UNSIGNED_BYTE, 1, sizeof(VERTEX), &( (VERTEX*)nullptr)->sampler ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, font->atlas->id ); diff --git a/src/util/allocator.h b/src/util/allocator.h index ee0ddd8..7927b9c 100644 --- a/src/util/allocator.h +++ b/src/util/allocator.h @@ -161,6 +161,18 @@ struct LIST { return &data[size - 1]; } + void resize( U32 size ) { + if( size > capacity ) + reserve( size * 2 ); + + if( size < capacity ) { + for( U32 i = size; i < capacity; ++i ) + data[i] = T(); + } + + this->size = size; + } + // does not call copy constructors, raw memcpy void emplace_list( const LIST& list ) { if( !list.size ) diff --git a/src/util/config/config.cpp b/src/util/config/config.cpp index 6c2d021..a41e455 100644 --- a/src/util/config/config.cpp +++ b/src/util/config/config.cpp @@ -12,21 +12,18 @@ void cfg_seterr( CFG_PARSER* p, const char* fmt, ... ) { } inline U8 is_whitespace( char c ) { - return c == ' ' || c == '\t' || c == '\n' || c == '\r'; // Added \r + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } inline void trim_whitespace( char* buf ) { if (!buf || !*buf) return; - - // Find first non-whitespace character + U32 start = 0; while( buf[start] && is_whitespace( buf[start] ) ) start++; - - // Find last non-whitespace character + U32 end = strlen( buf ); while( end > start && is_whitespace( buf[end - 1] ) ) end--; - - // Move string to beginning and null-terminate + U32 len = end - start; memmove( buf, buf + start, len ); buf[len] = '\0'; @@ -184,20 +181,17 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) { while( fgets( line, sizeof(line), parser->file ) ) { parser->linen++; - - // Remove all whitespace including \r\n + trim_whitespace( line ); - - // Skip empty lines - if( strlen(line) == 0 ) continue; - - // Use a copy for tokenization to preserve original + if( strlen( line ) == 0 ) + continue; + char line_copy[8192]; strcpy( line_copy, line ); - - token = strtok( line_copy, " \t\r\n" ); // Added \r + + token = strtok( line_copy, " \t\r\n" ); if( !token ) continue; - + if( strcmp( token, "{" ) == 0 ) { continue; } else if( strcmp( token, "}" ) == 0 ) { @@ -205,36 +199,33 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) { } else if( strcmp( token, cfg_types[CFGT_SECTION].def ) == 0 ) { next_token = strtok( NULL, " \t\r\n" ); if( !next_token ) { - cfg_seterr( parser, "Missing section name at line %d", parser->linen ); + cfg_seterr( parser, "missing section name at line %d", parser->linen ); return; } - + char sectname[64]; strncpy( sectname, next_token, sizeof(sectname) - 1 ); sectname[sizeof(sectname) - 1] = '\0'; trim_whitespace( sectname ); CFG_SECTION* new_section = cfg_section_new( sectname, (CFG_NODE*)current_section ); - - // Look for opening brace + char* brace = strtok( NULL, " \t\r\n" ); if( !brace || strcmp( brace, "{" ) != 0 ) { - cfg_seterr( parser, "Expected '{' after section name at line %d", parser->linen ); + cfg_seterr( parser, "expected '{' after section name at line %d", parser->linen ); return; } - + parse_section( parser, new_section ); } else { - // Parse variable declaration char name[64]; strncpy( name, token, sizeof(name) - 1 ); name[sizeof(name) - 1] = '\0'; trim_whitespace( name ); - // Find the variable name (between type and =) token = strtok( NULL, "=[" ); if( !token ) { - cfg_seterr( parser, "Invalid variable declaration at line %d", parser->linen ); + cfg_seterr( parser, "invalid variable declaration at line %d", parser->linen ); continue; } @@ -243,24 +234,22 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) { varname[sizeof(varname) - 1] = '\0'; trim_whitespace( varname ); - // Find matching parser - bool found = false; + U8 found = 0; for( I32 i = 0; i < sizeof(cfg_types) / sizeof(CFG_TYPE); ++i ) { const CFG_TYPE* fn = &cfg_types[i]; if( strcmp( name, fn->def ) == 0 ) { - // Reset strtok to work on original line for parser strcpy( line_copy, line ); - strtok( line_copy, " \t\r\n" ); // Skip type - strtok( NULL, "=[" ); // Skip variable name - + strtok( line_copy, " \t\r\n" ); + strtok( NULL, "=[" ); + fn->parser( parser, current_section, varname ); found = true; break; } } - + if( !found ) { - cfg_seterr( parser, "Unknown type '%s' at line %d", name, parser->linen ); + cfg_seterr( parser, "unknown type '%s' at line %d", name, parser->linen ); return; } @@ -273,26 +262,26 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) { } CFG_SECTION* cfg_load( const char* path ) { - FILE* f = fopen( path, "r" ); // Changed from "rb" to "r" for text mode + FILE* f = fopen( path, "r" ); if( !f ) return 0; CFG_PARSER p; - memset( &p, 0, sizeof(p) ); // Initialize all fields + memset( &p, 0, sizeof(p) ); p.iserr = 0; p.file = f; p.linen = 0; p.root = cfg_section_new( "root", 0 ); - + parse_section( &p, p.root ); fclose( f ); - + if( p.iserr ) { cfg_free( (CFG_NODE*)p.root ); return 0; } - + return p.root; } -- cgit v1.2.3