diff options
Diffstat (limited to '07-july/src/util')
| -rw-r--r-- | 07-july/src/util/util.c | 49 | ||||
| -rw-r--r-- | 07-july/src/util/util.h | 19 | ||||
| -rw-r--r-- | 07-july/src/util/util_time.c | 52 | ||||
| -rw-r--r-- | 07-july/src/util/util_time.h | 13 |
4 files changed, 133 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; +} diff --git a/07-july/src/util/util.h b/07-july/src/util/util.h new file mode 100644 index 0000000..5f82f30 --- /dev/null +++ b/07-july/src/util/util.h @@ -0,0 +1,19 @@ +#ifndef UTIL_H +#define UTIL_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 void Util_FatalError( const char* error ); +extern char* Util_LoadFile( const char* path ); +extern float Util_RandomF(float min, float max); +extern int Util_RandomI(int min, int max); + +#endif // UTIL_H diff --git a/07-july/src/util/util_time.c b/07-july/src/util/util_time.c new file mode 100644 index 0000000..b601379 --- /dev/null +++ b/07-july/src/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 = TIME.counted_frames / ( (float)(SDL_GetTicks() - TIME.beg_ticks) / 1000.f ); + + 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/07-july/src/util/util_time.h b/07-july/src/util/util_time.h new file mode 100644 index 0000000..3c3e470 --- /dev/null +++ b/07-july/src/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 |
