aboutsummaryrefslogtreecommitdiff
path: root/07-july/src/util/util.c
blob: fcdd665ee34fefe6f46cb2e13c51a14f86e82f21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
}