aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/util/util.c
diff options
context:
space:
mode:
authorThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
committerThomas Guillermo Albers Raviola <thomas@thomaslabs.org>2026-01-16 23:02:32 +0100
commit6b8af9cf83851c075c6c9514b1deaa931c2b19a4 (patch)
tree428986b49c32e21d3f7a3c2dfa41858ae0153209 /07-july/src/util/util.c
Initial commit
Diffstat (limited to '07-july/src/util/util.c')
-rw-r--r--07-july/src/util/util.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/07-july/src/util/util.c b/07-july/src/util/util.c
new file mode 100644
index 0000000..fcdd665
--- /dev/null
+++ b/07-july/src/util/util.c
@@ -0,0 +1,49 @@
+#include "util.h"
+
+#include <SDL2/SDL.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+void Util_FatalError( const char* error )
+{
+ fprintf(stderr, "Fatal Error:\n%s", error);
+ SDL_Quit();
+ exit(1);
+}
+
+char* Util_LoadFile( const char* path )
+{
+ FILE* file = fopen( path, "r" );
+
+ if(file == NULL)
+ {
+ const char* temp = " could not be found\n";
+ size_t length = strlen(temp);
+ length += strlen(path);
+ char buffer[length];
+ strcpy(buffer, path);
+ strcat(buffer, temp);
+ Util_FatalError(buffer);
+ }
+
+ fseek( file, 0, SEEK_END );
+ size_t sizeOfFile = ftell( file );
+ fseek( file, 0, SEEK_SET );
+ char* file_data = (char*) 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;
+}