TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

21f6bef80bec8c3f3af81670071069ed17ab1882.svn-base (4951B)


      1 #include "Utils.h"
      2 #include "DiagnosticDraw.h"
      3 #include "Console.h"
      4 
      5 ParticleTypeData NullPTdata()
      6 {
      7 	ParticleTypeData ptData;
      8 	ptData.NoData = true;
      9 	return ptData;
     10 }
     11 
     12 Time* gTime = NULL;
     13 Random* gRandom = NULL;
     14 
     15 //int GetTimeF() //measures in frames
     16 //{
     17 //	int time = (clock() / CLOCKS_PER_FRAME);
     18 //	return time;
     19 //}
     20 
     21 int GetRealTimeMS() //measures in milliseconds
     22 {
     23 	int time = (clock() / (CLOCKS_PER_SEC / 1000));
     24 	return time;
     25 }
     26 
     27 SDL_Surface* LoadSurfaceBMP(const char* filename)
     28 {
     29 #ifdef DEBUG
     30 	char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\imgs\\";
     31 #endif
     32 
     33 #ifdef RELEASE
     34 	char* basepath = ".\\imgs\\";
     35 #endif
     36 
     37 	char pathbuffer[1024];
     38 	strcpy(pathbuffer, basepath);
     39 
     40 	SDL_Surface* surface = SDL_LoadBMP(strcat(pathbuffer, filename));
     41 	if (surface == NULL)
     42 	{
     43 		gCons->ConsPrintf("SDL_LoadBMP Error: %s", SDL_GetError());
     44 	}
     45 
     46 	SDL_SetColorKey(surface, SDL_TRUE, SDL_MapRGB(surface->format, 0xFF, 0x00, 0xFF));
     47 
     48 	return surface;
     49 }
     50 
     51 TTF_Font* LoadFont(char* filename, int ptsize)
     52 {
     53 #ifdef DEBUG
     54 	char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\fonts\\";
     55 #endif
     56 
     57 #ifdef RELEASE
     58 	char* basepath = ".\\fonts\\";
     59 #endif
     60 
     61 	char pathbuffer[1024];
     62 	strcpy(pathbuffer, basepath);
     63 	char* fullpath = strcat(pathbuffer, filename);
     64 
     65 	TTF_Font* font = TTF_OpenFont(fullpath, ptsize);
     66 
     67 	const char* error = TTF_GetError();
     68 
     69 //	int msgboxID = MessageBox(
     70 //		NULL,
     71 //		(LPCSTR)error,
     72 //		(LPCSTR)L"Account Details",
     73 //		MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
     74 //	);
     75 
     76 	assert(font != NULL);
     77 	
     78 	//gCons->ConsPrintf(fullpath);
     79 
     80 	return font;
     81 }
     82 cJSON * LoadJSON(const char* filename)
     83 {
     84 #ifdef DEBUG
     85 	char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\json\\";
     86 #endif
     87 
     88 #ifdef RELEASE
     89 	char* basepath = ".\\json\\";
     90 #endif
     91 
     92 	char pathbuffer[1024];
     93 	strcpy(pathbuffer, basepath);
     94 	strcat(pathbuffer, filename);
     95 
     96 	struct stat filestats;
     97 	stat(pathbuffer, &filestats);
     98 
     99 	char* filecontent = (char*)malloc( (filestats.st_size + 1) );
    100 
    101 	FILE* fp;
    102 	fp = fopen(pathbuffer, "rt");
    103 	
    104 	size_t size = fread(filecontent, 1, filestats.st_size, fp);
    105 	fclose(fp);
    106 
    107 	filecontent[size] = 0;
    108 
    109 	//gCons->ConsPrintf("%s\n", filecontent);
    110 
    111 	return cJSON_Parse(filecontent);
    112 }
    113 
    114 void PrintJSON(cJSON* json)
    115 {
    116 	gCons->ConsPrintf(cJSON_Print(json));
    117 }
    118 
    119 float GetSign(float x)
    120 {
    121 	if (x > 0) { return  1; }
    122 	if (x < 0) { return -1; }
    123 	return 0;
    124 }
    125 
    126 bool DetectRectIntersect(SDL_Rect* r1, SDL_Rect* r2)
    127 {
    128 	//if ( ((r1->x <= r2->x) && (r2->x <= (r1->x + r1->w))) || ( (r1->x <= (r2->x + r2->w)) && ((r2->x + r2->w) <= (r1->x + r1->w)) )) //if r1 and r2 intersect in x
    129 
    130 	int r1x1 = r1->x;
    131 	int r1x2 = r1->x + r1->w;
    132 	int r2x1 = r2->x;
    133 	int r2x2 = r2->x + r2->w;
    134 
    135 	int r1y1 = r1->y;
    136 	int r1y2 = r1->y + r1->h;
    137 	int r2y1 = r2->y;
    138 	int r2y2 = r2->y + r2->h;
    139 
    140 	//if (((r1->x <= r2->x) && (r2->x <= (r1->x + r1->w))) || ((r1->x <= (r2->x + r2->w)) && ((r2->x + r2->w) <= (r1->x + r1->w))) || ((r1->x <= r2->x) && (r2->x + r2->w) >= (r1->x + r1->w)) || ( (r1->x >= r2->x)&&( (r1->x + r1->w) >= (r2->x + r2->w) ) ) ) //if r1 and r2 intersect in x
    141 	if
    142 	(
    143 	(r1x1 <= r2x2 && r1x1 >= r2x1) ||
    144 	(r1x2 <= r2x2 && r1x2 >= r2x1) ||
    145 	(r1x1 <= r2x1 && r1x2 >= r2x2)
    146 	)
    147 	{
    148 		if
    149 		(
    150 		(r1y1 <= r2y2 && r1y1 >= r2y1) ||
    151 		(r1y2 <= r2y2 && r1y2 >= r2y1) ||
    152 		(r1y1 <= r2y1 && r1y2 >= r2y2)
    153 		)
    154 		{
    155 			//gCons->ConsPrintf("Rect Intersect Detected!\n");
    156 			return true;
    157 		}
    158 	}
    159 	//gCons->ConsPrintf("No Rect Intersect Detected!\n");
    160 	return false;
    161 }
    162 
    163 bool DetectCenterPointIntersect(SDL_Rect* zone, SDL_Rect* point)
    164 {
    165 	//gDiagDraw->LogDiagRect(*point);
    166 	int pointX = point->x + (int)((float)point->w / 2);
    167 	int pointY = point->y + (int)((float)point->h / 2);
    168 	if ( (zone->x <= pointX) && (pointX <= (zone->x + zone->w)) )
    169 	{
    170 		if ((zone->y <= pointY) && (pointY <= (zone->y + zone->h)))
    171 		{
    172 			//gCons->ConsPrintf("Center point intersect detected\n");
    173 			return true;
    174 		}
    175 	}
    176 	//gCons->ConsPrintf("center point not intersecting\n");
    177 	return false;
    178 }
    179 
    180 int CalcDistance(int x1, int y1, int x2, int y2)
    181 {
    182 	int Xdelt = abs(x1 - x2);
    183 	int Ydelt = abs(y1 - y2);
    184 
    185 	return (int)sqrt((Xdelt*Xdelt) + (Ydelt*Ydelt));
    186 }
    187 
    188 int EvenOutcomes(int NumberofOutcomes)
    189 {
    190 	int	i = (rand() % (NumberofOutcomes)) + 1;
    191 	//gCons->ConsPrintf("Roll : %i\n", i);
    192 	return i;
    193 }
    194 
    195 Random::Random()
    196 {
    197 	mSeed = (int)time(NULL);
    198 	srand(mSeed);
    199 }
    200 
    201 int Random::RandRange(int v1, int v2)
    202 {
    203 	int i;
    204 	if (v1 < v2) //v1 is min
    205 	{
    206 		i = (v1 + (rand() % (1 + v2 - v1)) );
    207 		assert(i <= v2);
    208 		assert(i >= v1);
    209 	}
    210 	else if (v2 < v1) //v2 is min
    211 	{
    212 		i = (v2 + (rand() % (1 + v1 - v2)) );
    213 		assert(i <= v1);
    214 		assert(i >= v2);
    215 	}
    216 	else// v1 == v2
    217 	{
    218 		i = v1;
    219 	}
    220 	//gCons->ConsPrintf("Range Roll : %i\n", i);
    221 	return i;
    222 }
    223 
    224 
    225 bool IsEven(int i) //Not working alawys return true
    226 {
    227 	if ( (i % 2) == 0)
    228 	{
    229 		return true;
    230 	}
    231 	else
    232 	{
    233 		return false;
    234 	}
    235 }