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