From ba6d14a7737ef7cd02b61586e39ca2162df3018d Mon Sep 17 00:00:00 2001 From: Kasullian Date: Mon, 8 Sep 2025 23:20:06 -0400 Subject: glew, map parsing for windows --- src/util/config/config.cpp | 103 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 79 insertions(+), 24 deletions(-) (limited to 'src/util') diff --git a/src/util/config/config.cpp b/src/util/config/config.cpp index 1601afe..6c2d021 100644 --- a/src/util/config/config.cpp +++ b/src/util/config/config.cpp @@ -12,20 +12,24 @@ void cfg_seterr( CFG_PARSER* p, const char* fmt, ... ) { } inline U8 is_whitespace( char c ) { - return c == ' ' || c == '\t' || c == '\n'; + return c == ' ' || c == '\t' || c == '\n' || c == '\r'; // Added \r } inline void trim_whitespace( char* buf ) { - U32 i; - for( i = 0; !!buf[i]; ++i ) - if( !is_whitespace( buf[i] ) ) break; - for( U32 i2 = i; !!buf[i2]; ++i2 ) { - if( is_whitespace( buf[i2] ) ) { - buf[i2 - i] = 0; - return; - } - buf[i2 - i] = buf[i2]; - } + 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'; } inline void init_cfg_node( CFG_NODE* node, const char* name, CFG_NODE* parent, U8 type ) { @@ -179,65 +183,116 @@ void parse_section( CFG_PARSER* parser, CFG_SECTION* current_section ) { char* next_token; while( fgets( line, sizeof(line), parser->file ) ) { - token = strtok( line, " \t\n" ); + 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 + char line_copy[8192]; + strcpy( line_copy, line ); + + token = strtok( line_copy, " \t\r\n" ); // Added \r if( !token ) continue; - if( strcmp( token, "{" ) == 0 ) + + if( strcmp( token, "{" ) == 0 ) { continue; - else if( strcmp( token, "}" ) == 0 ) { + } else if( strcmp( token, "}" ) == 0 ) { return; } else if( strcmp( token, cfg_types[CFGT_SECTION].def ) == 0 ) { - next_token = strtok( NULL, " \t\n" ); + next_token = strtok( NULL, " \t\r\n" ); + if( !next_token ) { + cfg_seterr( parser, "Missing section name at line %d", parser->linen ); + return; + } + char sectname[64]; - strcpy( sectname, next_token ); + 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 ); - strtok( NULL, " \t\n" ); + + // 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 ); + return; + } + parse_section( parser, new_section ); } else { + // Parse variable declaration char name[64]; - strcpy( name, token ); + 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 ) + if( !token ) { + cfg_seterr( parser, "Invalid variable declaration at line %d", parser->linen ); continue; + } char varname[64]; - strcpy( varname, token ); + strncpy( varname, token, sizeof(varname) - 1 ); + varname[sizeof(varname) - 1] = '\0'; trim_whitespace( varname ); + // Find matching parser + bool found = false; for( I32 i = 0; i < sizeof(cfg_types) / sizeof(CFG_TYPE); ++i ) { const CFG_TYPE* fn = &cfg_types[i]; - if( strncmp( name, fn->def, strlen( fn->def ) ) == 0 ) { + 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 + fn->parser( parser, current_section, varname ); + found = true; break; } } + + if( !found ) { + cfg_seterr( parser, "Unknown type '%s' at line %d", name, parser->linen ); + return; + } if( parser->iserr ) { dlog( "parse_section() : %s parse error:\n - %s\n", name, parser->err ); return; } } - - parser->linen++; } } CFG_SECTION* cfg_load( const char* path ) { - FILE* f = fopen( path, "rb" ); + FILE* f = fopen( path, "r" ); // Changed from "rb" to "r" for text mode if( !f ) return 0; CFG_PARSER p; + memset( &p, 0, sizeof(p) ); // Initialize all fields 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 From 235926dadb686589f0b5480162c3ab929159e570 Mon Sep 17 00:00:00 2001 From: navewindre Date: Wed, 10 Sep 2025 12:25:00 +0200 Subject: unfuck --- src/util/allocator.h | 12 +++++++++ src/util/config/config.cpp | 67 +++++++++++++++++++--------------------------- 2 files changed, 40 insertions(+), 39 deletions(-) (limited to 'src/util') 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 From ae694bc0da98e45c5def20ac1d92f9d8aad65fd5 Mon Sep 17 00:00:00 2001 From: Kasullian Date: Wed, 10 Sep 2025 12:08:59 -0400 Subject: 3d view mouse control --- src/util/input.cpp | 9 +++++++++ src/util/input.h | 4 ++++ 2 files changed, 13 insertions(+) (limited to 'src/util') diff --git a/src/util/input.cpp b/src/util/input.cpp index 4a786aa..7e94c47 100644 --- a/src/util/input.cpp +++ b/src/util/input.cpp @@ -27,6 +27,8 @@ void input_on_event( SDL_Event* e ) { case SDL_MOUSEMOTION: { input.mouse.pos.x = (F32)e->motion.x; input.mouse.pos.y = (F32)e->motion.y; + input.mouse.pos_delta.x = (F32)e->motion.xrel; + input.mouse.pos_delta.y = (F32)e->motion.yrel; } break; case SDL_KEYDOWN: { input.keys[e->key.keysym.sym & 0xff] = 1; @@ -71,4 +73,11 @@ void input_on_mouse( I32 type, I32 x, I32 y ) { void input_frame_end() { input.mouse.wheel = 0; + input.mouse.pos_delta.x = 0; + input.mouse.pos_delta.y = 0; +} + +void input_capture_mouse( bool capture ) { + input.mouse_captured = capture; + SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE ); } diff --git a/src/util/input.h b/src/util/input.h index e174fe6..4885f0f 100644 --- a/src/util/input.h +++ b/src/util/input.h @@ -16,6 +16,7 @@ const U32 MOUSE_WHEEL = 0x4; struct MOUSE_DATA { VEC2 pos; + VEC2 pos_delta; U8 left; U8 right; U8 middle; @@ -27,6 +28,8 @@ using ON_INPUT_FN = std::function; struct INPUT_DATA { MOUSE_DATA mouse; U8 keys[0xff]; + bool mouse_captured; + F32 mouse_sensitivity = 1.0f; LIST on_input; }; @@ -37,5 +40,6 @@ extern void input_frame_end(); extern void input_on_event( SDL_Event* e ); extern void input_on_mouse( I32 type, I32 x, I32 y ); extern void input_is_key_down( U32 key ); +extern void input_capture_mouse( bool capture ); #define kb_down( key ) input_is_key_down( key ) -- cgit v1.2.3 From 0db9c91742a6ab17c3d8a8b86a34392c1797740b Mon Sep 17 00:00:00 2001 From: navewindre Date: Sun, 28 Sep 2025 02:45:10 +0200 Subject: input bettr --- src/util/input.cpp | 11 +++++++---- src/util/input.h | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'src/util') diff --git a/src/util/input.cpp b/src/util/input.cpp index 7e94c47..225b696 100644 --- a/src/util/input.cpp +++ b/src/util/input.cpp @@ -27,8 +27,8 @@ void input_on_event( SDL_Event* e ) { case SDL_MOUSEMOTION: { input.mouse.pos.x = (F32)e->motion.x; input.mouse.pos.y = (F32)e->motion.y; - input.mouse.pos_delta.x = (F32)e->motion.xrel; - input.mouse.pos_delta.y = (F32)e->motion.yrel; + input.mouse.pos_delta.x += (F32)e->motion.xrel; + input.mouse.pos_delta.y += (F32)e->motion.yrel; } break; case SDL_KEYDOWN: { input.keys[e->key.keysym.sym & 0xff] = 1; @@ -39,6 +39,10 @@ void input_on_event( SDL_Event* e ) { } } +extern void input_reset_mouse_accumulator() { + input.mouse.pos_delta.x = 0; + input.mouse.pos_delta.y = 0; +} void input_on_mouse( I32 type, I32 x, I32 y ) { if( type == MOUSEEV_MOVE ) { @@ -73,11 +77,10 @@ void input_on_mouse( I32 type, I32 x, I32 y ) { void input_frame_end() { input.mouse.wheel = 0; - input.mouse.pos_delta.x = 0; - input.mouse.pos_delta.y = 0; } void input_capture_mouse( bool capture ) { + input_reset_mouse_accumulator(); input.mouse_captured = capture; SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE ); } diff --git a/src/util/input.h b/src/util/input.h index 4885f0f..ec901ab 100644 --- a/src/util/input.h +++ b/src/util/input.h @@ -23,15 +23,27 @@ struct MOUSE_DATA { U8 wheel; }; +struct INPUT_KEYBINDS { + U8 fwd = 'w'; + U8 back = 's'; + U8 left = 'a'; + U8 right = 'd'; + + U8 jump = ' '; + U8 crouch = SDL_SCANCODE_LCTRL; +}; + using ON_INPUT_FN = std::function; struct INPUT_DATA { MOUSE_DATA mouse; U8 keys[0xff]; bool mouse_captured; - F32 mouse_sensitivity = 1.0f; + F32 mouse_sensitivity = .3f; + INPUT_KEYBINDS binds; LIST on_input; + }; extern INPUT_DATA input; @@ -41,5 +53,6 @@ extern void input_on_event( SDL_Event* e ); extern void input_on_mouse( I32 type, I32 x, I32 y ); extern void input_is_key_down( U32 key ); extern void input_capture_mouse( bool capture ); +extern void input_reset_mouse_accumulator(); #define kb_down( key ) input_is_key_down( key ) -- cgit v1.2.3 From db702f3197fadf8529789303a472f503100c715c Mon Sep 17 00:00:00 2001 From: navewindre Date: Thu, 2 Oct 2025 06:13:17 +0200 Subject: fix --- src/util/aabb.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/util') diff --git a/src/util/aabb.h b/src/util/aabb.h index f2fd251..4d74c88 100644 --- a/src/util/aabb.h +++ b/src/util/aabb.h @@ -14,6 +14,7 @@ inline F32 aabb_support_radius( const AABB& aabb, const VEC3& dir ) { return fabsf(dir.x) * half.x + fabsf(dir.y) * half.y + fabsf(dir.z) * half.z; } +// returns positive radius in X and negative in Y inline VEC2 aabb_extend_at_feet( const AABB& aabb, const VEC3& dir ) { VEC3 half = aabb_half( aabb ); -- cgit v1.2.3 From e3de3ba5162f7ddd5005911124d4333e140fd984 Mon Sep 17 00:00:00 2001 From: navewindre Date: Thu, 27 Nov 2025 17:19:02 +0100 Subject: bunch o stuff --- src/util/allocator.h | 18 ++++++++++++++++-- src/util/input.cpp | 10 +++++++--- src/util/input.h | 8 +++++--- 3 files changed, 28 insertions(+), 8 deletions(-) (limited to 'src/util') diff --git a/src/util/allocator.h b/src/util/allocator.h index 7927b9c..27b21c0 100644 --- a/src/util/allocator.h +++ b/src/util/allocator.h @@ -165,8 +165,12 @@ struct LIST { if( size > capacity ) reserve( size * 2 ); - if( size < capacity ) { - for( U32 i = size; i < capacity; ++i ) + if( size < capacity / 4 ) + shrink(); + + if( this->size < size ) { + memset( &data[this->size], 0, sizeof(T) * (size - this->size) ); + for( U32 i = this->size; i < size; ++i ) data[i] = T(); } @@ -262,6 +266,16 @@ struct LIST { return -1; } + I32 idx_of( const T* what ) { + for( U32 i = 0; i < size; ++i ) { + if( &data[i] == what ) { + return i; + } + } + + return -1; + } + I32 idx_where( ON_SEARCH_FN what ) { for( U32 i = 0; i < size; ++i ) { if( what( &data[i] ) ) { diff --git a/src/util/input.cpp b/src/util/input.cpp index 225b696..284ebfe 100644 --- a/src/util/input.cpp +++ b/src/util/input.cpp @@ -27,8 +27,8 @@ void input_on_event( SDL_Event* e ) { case SDL_MOUSEMOTION: { input.mouse.pos.x = (F32)e->motion.x; input.mouse.pos.y = (F32)e->motion.y; - input.mouse.pos_delta.x += (F32)e->motion.xrel; - input.mouse.pos_delta.y += (F32)e->motion.yrel; + input.mouse.pos_delta.x += (F32)e->motion.xrel * input.mpitch; + input.mouse.pos_delta.y += (F32)e->motion.yrel * input.myaw; } break; case SDL_KEYDOWN: { input.keys[e->key.keysym.sym & 0xff] = 1; @@ -79,8 +79,12 @@ void input_frame_end() { input.mouse.wheel = 0; } +U8 input_is_key_down( U32 key ) { + return input.keys[key]; +} + void input_capture_mouse( bool capture ) { input_reset_mouse_accumulator(); - input.mouse_captured = capture; + input.mouselock = capture; SDL_SetRelativeMouseMode( capture ? SDL_TRUE : SDL_FALSE ); } diff --git a/src/util/input.h b/src/util/input.h index ec901ab..86d715c 100644 --- a/src/util/input.h +++ b/src/util/input.h @@ -38,8 +38,10 @@ using ON_INPUT_FN = std::function; struct INPUT_DATA { MOUSE_DATA mouse; U8 keys[0xff]; - bool mouse_captured; - F32 mouse_sensitivity = .3f; + bool mouselock; + F32 msens = .3f; + F32 myaw = 1.f; + F32 mpitch = 1.f; INPUT_KEYBINDS binds; LIST on_input; @@ -51,7 +53,7 @@ extern INPUT_DATA input; extern void input_frame_end(); extern void input_on_event( SDL_Event* e ); extern void input_on_mouse( I32 type, I32 x, I32 y ); -extern void input_is_key_down( U32 key ); +extern U8 input_is_key_down( U32 key ); extern void input_capture_mouse( bool capture ); extern void input_reset_mouse_accumulator(); -- cgit v1.2.3