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