diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 23:02:32 +0100 |
| commit | 6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch) | |
| tree | 428986b49c32e21d3f7a3c2dfa41858ae0153209 /09-september/tomcat/util | |
Initial commit
Diffstat (limited to '09-september/tomcat/util')
| -rw-r--r-- | 09-september/tomcat/util/array.c | 102 | ||||
| -rw-r--r-- | 09-september/tomcat/util/array.h | 23 | ||||
| -rw-r--r-- | 09-september/tomcat/util/str.c | 121 | ||||
| -rw-r--r-- | 09-september/tomcat/util/str.h | 29 | ||||
| -rw-r--r-- | 09-september/tomcat/util/util.c | 138 | ||||
| -rw-r--r-- | 09-september/tomcat/util/util.h | 27 | ||||
| -rw-r--r-- | 09-september/tomcat/util/util_time.c | 52 | ||||
| -rw-r--r-- | 09-september/tomcat/util/util_time.h | 13 |
8 files changed, 505 insertions, 0 deletions
diff --git a/09-september/tomcat/util/array.c b/09-september/tomcat/util/array.c new file mode 100644 index 0000000..13660e0 --- /dev/null +++ b/09-september/tomcat/util/array.c @@ -0,0 +1,102 @@ +#include "array.h" + +#include <stdlib.h> +#include <string.h> + +#define ARRAY_GROWTH_FACTOR 2 +#define MAX(x, y) ( (x > y) ? x : y ) + +typedef struct _RealArray +{ + char *data; + unsigned int length; /* Number of elements */ + + unsigned int type_size; + unsigned int capacity; /* Array capacity in elements number */ + bool clear; +} RealArray; + +static void array_check_for_expand(RealArray *arr, unsigned int length); + +Array *array_create(unsigned int type_size) +{ + return array_create_by_size(type_size, 0); +} + +Array *array_create_by_size(unsigned int type_size, unsigned int reserved_size) +{ + RealArray *arr = malloc( sizeof(RealArray) ); + arr->capacity = 0; + arr->data = NULL; + arr->length = 0; + arr->type_size = type_size; + + if(reserved_size != 0) + { + array_check_for_expand(arr, reserved_size); + } + + return (Array *)arr; +} + +void array_append(Array *arr, void *data) +{ + RealArray *r_arr = (RealArray *)arr; + array_check_for_expand(r_arr, 1); + + memcpy(r_arr->data + r_arr->length * r_arr->type_size, data, r_arr->type_size); + r_arr->length += 1; +} + +void array_insert(Array *arr, int index, void *data) +{ + RealArray *r_arr = (RealArray *)arr; + array_check_for_expand(r_arr, 1); + + /* Shift everything one place */ + memmove(r_arr->data + (index + 1) * r_arr->type_size, + r_arr->data + index * r_arr->type_size, + r_arr->length * r_arr->type_size - index * r_arr->type_size); + + /* Insert the new data */ + memcpy(r_arr->data + index * r_arr->type_size, data, r_arr->type_size); + + r_arr->length += 1; +} + +void array_remove(Array *arr, int index) +{ + +} + +void array_reserve(Array *arr, unsigned int length) +{ + +} + +unsigned int array_get_type_size(Array *arr) +{ + RealArray *r_arr = (RealArray *)arr; + return r_arr->type_size; +} + +void array_free(Array *arr) +{ + RealArray *r_arr = (RealArray *)arr; + + free(r_arr->data); + free(r_arr); +} + +static void array_check_for_expand(RealArray *arr, unsigned int length) +{ + RealArray *r_arr = (RealArray *)arr; + + unsigned int expected_size = r_arr->length + length; + + if(r_arr->capacity < expected_size) + { + r_arr->capacity = MAX(ARRAY_GROWTH_FACTOR * r_arr->capacity, expected_size); + r_arr->data = realloc(r_arr->data, r_arr->capacity * r_arr->type_size); + } +} diff --git a/09-september/tomcat/util/array.h b/09-september/tomcat/util/array.h new file mode 100644 index 0000000..1444a74 --- /dev/null +++ b/09-september/tomcat/util/array.h @@ -0,0 +1,23 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include <stdbool.h> + +typedef struct +{ + char *data; + unsigned int length; +} Array; + +extern Array *array_create(unsigned int type_size); +extern Array *array_create_by_size(unsigned int type_size, unsigned int reserved_size); + +extern void array_append(Array *arr, void *data); +extern void array_insert(Array *arr, int index, void *data); +extern void array_remove(Array *arr, int index); +extern void array_reserve(Array *arr, unsigned int length); +extern unsigned int array_get_type_size(Array *arr); + +extern void array_free(Array *arr); + +#endif // ARRAY_H diff --git a/09-september/tomcat/util/str.c b/09-september/tomcat/util/str.c new file mode 100644 index 0000000..295b390 --- /dev/null +++ b/09-september/tomcat/util/str.c @@ -0,0 +1,121 @@ +#include "str.h" + +#include <string.h> +#include <stdlib.h> + +/* Note, we add always 1 to length for the \0 character */ + +static void string_check_for_expand( String *string_, unsigned int length ) +{ + if(string_->length + length >= string_->allocated_length) + { + string_->allocated_length = string_->length + length + 1; + string_->data = realloc(string_->data, string_->allocated_length * sizeof(char) ); + } +} + +String *string_create( const char *init ) +{ + String *str; + + if(init == NULL || *init == '\0') + { + str = string_create_by_size(1); + } + else + { + unsigned int len = strlen(init); + str = string_create_by_size(len + 1); + string_append(str, init); + } + + return str; +} + +String *string_create_by_size( unsigned int reserved_size) +{ + String *str = malloc( sizeof(String) ); + + str->data = NULL; + str->length = 0; + str->allocated_length = 0; + + string_check_for_expand(str, reserved_size); + + str->data[0] = 0; + return str; +} + +void string_assign( String *string_, const char *val ) +{ + +} + +void string_append( String *string_, const char *val ) +{ + string_insert(string_, string_->length, val); +} + +void string_append_char( String *string_, char c ) +{ + string_insert(string_, string_->length, &c); +} + +void string_insert( String *string_, int index, const char *val) +{ + if(index > string_->length) + return; + + unsigned int length = strlen(val); + string_check_for_expand(string_, length); + + if(index == string_->length - 1) + { + strcpy(string_->data + string_->length, val); + } + else + { + memmove(string_->data + index + length, + string_->data + index, + (string_->length - index) * sizeof(char) ); + + memcpy(string_->data + index, val, length * sizeof(char) ); + } + + string_->length += length; + string_->data[string_->length] = '\0'; +} + +void string_insert_char( String *string_, int index, char val) +{ + string_insert(string_, index, &val); +} + +void string_free( String *string_ ) +{ + if(string_) + { + free(string_->data); + free(string_); + } +} + +unsigned int string_hash( String *string_ ) +{ + unsigned int hash, i; + for(hash = i = 0; i < string_->length; ++i) + { + hash += string_->data[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + return hash; +} + +bool string_equal( String *a, String *b ) +{ + return !strcmp(a->data, b->data); +} diff --git a/09-september/tomcat/util/str.h b/09-september/tomcat/util/str.h new file mode 100644 index 0000000..d8182e1 --- /dev/null +++ b/09-september/tomcat/util/str.h @@ -0,0 +1,29 @@ +#ifndef STR_H +#define STR_H + +#include <stdbool.h> + +typedef struct _String +{ + char *data; + unsigned int length; + unsigned int allocated_length; +} String; + +extern String *string_create( const char *init ); +extern String *string_create_by_size( unsigned int reserved_size); + +extern void string_assign( String *string_, const char *val ); + +extern void string_append( String *string_, const char *val ); +extern void string_append_char( String *string_, char c ); + +extern void string_insert( String *string_, int index, const char *val); +extern void string_insert_char( String *string_, int index, char val); + +extern void string_free( String *string_ ); + +extern unsigned int string_hash( String *string_ ); +extern bool string_equal( String *a, String *b ); + +#endif // STR_H diff --git a/09-september/tomcat/util/util.c b/09-september/tomcat/util/util.c new file mode 100644 index 0000000..22eb0fc --- /dev/null +++ b/09-september/tomcat/util/util.c @@ -0,0 +1,138 @@ +#include "util.h" + +#include <SDL2/SDL.h> + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> + +unsigned int Util_Hash( const char *str ) +{ + unsigned int hash, i, length; + length = strlen(str); + + for(hash = i = 0; i < length; ++i) + { + hash += str[i]; + hash += (hash << 10); + hash ^= (hash >> 6); + } + + hash += (hash << 3); + hash ^= (hash >> 11); + hash += (hash << 15); + + return hash; +} + +void Util_FatalError( const char *fmt, ... ) +{ + fprintf(stderr, "Fatal Error:\n"); + + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); + + SDL_Quit(); + exit(1); +} + +void Util_CheckGLError() +{ + GLenum error = glGetError(); + switch(error) + { + case GL_INVALID_ENUM: + fprintf(stderr, "WARNING: GL_INVALID_ENUM\n"); + break; + case GL_INVALID_VALUE: + fprintf(stderr, "WARNING: GL_INVALID_VALUE\n"); + break; + case GL_INVALID_OPERATION: + fprintf(stderr, "WARNING: GL_INVALID_OPERATION\n"); + break; + case GL_INVALID_FRAMEBUFFER_OPERATION: + fprintf(stderr, "WARNING: GL_INVALID_FRAMEBUFFER_OPERATION\n"); + break; + case GL_OUT_OF_MEMORY: + fprintf(stderr, "WARNING: GL_OUT_OF_MEMORY\n"); + break; + case GL_STACK_UNDERFLOW: + fprintf(stderr, "WARNING: GL_STACK_UNDERFLOW\n"); + break; + case GL_STACK_OVERFLOW: + fprintf(stderr, "WARNING: GL_STACK_OVERFLOW\n"); + break; + + default: + break; + } +} + +char *Util_LoadFile( const char *path ) +{ + FILE* file = fopen( path, "r" ); + + if(file == NULL) + { + Util_FatalError("File %s could not be found!\n", path); + } + + fseek( file, 0, SEEK_END ); + size_t sizeOfFile = ftell( file ); + fseek( file, 0, SEEK_SET ); + char* file_data = malloc( sizeof(char) * sizeOfFile + 1 ); + fread( file_data, sizeof(char), sizeOfFile, file ); + file_data[sizeOfFile] = '\0'; + fclose(file); + return file_data; +} + +float Util_RandomF(float min, float max) +{ + return ( min + (float)rand() ) / ( (float)RAND_MAX / (max-min) ); +} + +int Util_RandomI(int min, int max) +{ + return ( rand() % (max-min) ) + min; +} + +Vec3 +Util_GetMouseRay(int screenWidth, int screenHeigth, Mat4 *viewMatrix, Mat4 *projectionMatrix, + int mouseX, int mouseY) +{ + Vec4 eyeCoords; + Vec3 mouseRay; + /* Normalized device coords NOTE: -y becouse for SDL y = 0 is the top of the screen */ + GLfloat normalX = ( 2.0f * (GLfloat)mouseX ) / (GLfloat) screenWidth - 1.0f; + GLfloat normalY = 1.0f - (2.0f * (GLfloat)mouseY) / (GLfloat) screenHeigth; + + /* clipCoords include 4th component */ + Vec4 clipCoords = { normalX, normalY, -1.0f, 1.0f }; + + /* Remove perpective */ + { + Mat4 invertedProjection = mat4_inverse(projectionMatrix); + eyeCoords = mat4_mul_vec4(&invertedProjection, &clipCoords); + eyeCoords.z = -1.0f; + eyeCoords.w = 0.0f; + } + + /* Remove view matrix*/ + { + Mat4 invertedViewMatrix = mat4_inverse(viewMatrix); + Vec4 temp = mat4_mul_vec4(&invertedViewMatrix, &eyeCoords); + + mouseRay.x = temp.x; + mouseRay.y = temp.y; + mouseRay.z = temp.z; + + mouseRay = vec3_normalize(&mouseRay); + } + + /* Return the ray in world coordinates */ + return mouseRay; +} diff --git a/09-september/tomcat/util/util.h b/09-september/tomcat/util/util.h new file mode 100644 index 0000000..f09f7cb --- /dev/null +++ b/09-september/tomcat/util/util.h @@ -0,0 +1,27 @@ +#ifndef UTIL_H +#define UTIL_H + +#include "../math/matrix4x4.h" + +#define toRadians(degrees) (degrees * 3.1415926 / 180.0f) +#define toDegrees(radians) (radians * 180.0f / 3.1415926) + +#ifdef DEBUG +#include <stdio.h> +#define myAssert(expr) expr ? 1==1 : fprintf(stderr, "The expresion was not true\n") +#else +#define myAssert(expr) +#endif // DEBUG + +extern unsigned int Util_Hash( const char *str ); +extern void Util_FatalError( const char* fmt, ... ); +extern void Util_CheckGLError(); +extern char* Util_LoadFile( const char* path ); +extern float Util_RandomF(float min, float max); +extern int Util_RandomI(int min, int max); + +extern Vec3 +Util_GetMouseRay(int screenWidth, int screenHeigth, Mat4 *viewMatrix, Mat4 *projectionMatrix, + int mouseX, int mouseY); + +#endif // UTIL_H diff --git a/09-september/tomcat/util/util_time.c b/09-september/tomcat/util/util_time.c new file mode 100644 index 0000000..e56d997 --- /dev/null +++ b/09-september/tomcat/util/util_time.c @@ -0,0 +1,52 @@ +#include "util_time.h" + +static struct +{ + float max_ticks_per_frame; //< cuantos ticks (tiempo demora) un frame + Uint32 counted_frames; //< cuantos frames han pasado + Uint32 start_ticks; //< ticks al iniciar la iteracion del loop + Uint32 beg_ticks; //< ticks desde el inicio del juego + Uint32 time_per_frame; +} TIME = { + 1000.0f / 60.0f, + 0, 0, 0, 0 +}; + +void Time_Init() +{ + TIME.beg_ticks = SDL_GetTicks(); +} + +void Time_Begin() +{ + TIME.start_ticks = SDL_GetTicks(); +} + +float Time_End() +{ + TIME.counted_frames += 1; + float FPS = (float)TIME.counted_frames / ( (float)(SDL_GetTicks() - TIME.beg_ticks) / 1000.0f ); + + float frameTicks = (float)(SDL_GetTicks() - TIME.start_ticks); + TIME.time_per_frame = frameTicks; + if(frameTicks < TIME.max_ticks_per_frame){ + SDL_Delay( (Uint32)(TIME.max_ticks_per_frame - frameTicks) ); + } + + return FPS; +} + +float Time_GetFrameTime() +{ + return (float)TIME.time_per_frame / 1000.0f; +} + +void Time_SetMaxFramesPerSecond(Uint32 frames) +{ + TIME.max_ticks_per_frame = 1000.0f / (float)frames; +} + +Uint32 Time_GetCountedFrames() +{ + return TIME.counted_frames; +} diff --git a/09-september/tomcat/util/util_time.h b/09-september/tomcat/util/util_time.h new file mode 100644 index 0000000..3c3e470 --- /dev/null +++ b/09-september/tomcat/util/util_time.h @@ -0,0 +1,13 @@ +#ifndef UTIL_TIME_H +#define UTIL_TIME_H + +#include <SDL2/SDL.h> + +extern void Time_Init( void ); +extern void Time_Begin( void ); +extern float Time_End( void ); +extern void Time_SetMaxFramesPerSecond(Uint32 frames); +extern float Time_GetFrameTime( void ); +extern Uint32 Time_GetCountedFrames( void ); + +#endif // UTIL_TIME_H |
