d7b818c102e3a23ca5fbccb35e32026c0b4b99d0.svn-base (4631B)
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 const char* error = TTF_GetError(); 67 68 // int msgboxID = MessageBox( 69 // NULL, 70 // (LPCSTR)error, 71 // (LPCSTR)L"Account Details", 72 // MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2 73 // ); 74 75 assert(font != NULL); 76 77 //gCons->ConsPrintf(fullpath); 78 79 return font; 80 } 81 cJSON * LoadJSON(const char* filename) 82 { 83 #ifdef DEBUG 84 char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\json\\"; 85 #endif 86 87 #ifdef RELEASE 88 char* basepath = ".\\json\\"; 89 #endif 90 91 char pathbuffer[1024]; 92 strcpy(pathbuffer, basepath); 93 strcat(pathbuffer, filename); 94 95 struct stat filestats; 96 stat(pathbuffer, &filestats); 97 98 char* filecontent = (char*)malloc( (filestats.st_size + 1) ); 99 100 FILE* fp; 101 fp = fopen(pathbuffer, "rt"); 102 103 size_t size = fread(filecontent, 1, filestats.st_size, fp); 104 fclose(fp); 105 106 filecontent[size] = 0; 107 108 //gCons->ConsPrintf("%s\n", filecontent); 109 110 return cJSON_Parse(filecontent); 111 } 112 113 void PrintJSON(cJSON* json) 114 { 115 gCons->ConsPrintf(cJSON_Print(json)); 116 } 117 118 float GetSign(float x) 119 { 120 if (x > 0) { return 1; } 121 if (x < 0) { return -1; } 122 return 0; 123 } 124 125 bool DetectRectIntersect(SDL_Rect* r1, SDL_Rect* r2) 126 { 127 //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 128 129 int r1x1 = r1->x; 130 int r1x2 = r1->x + r1->w; 131 int r2x1 = r2->x; 132 int r2x2 = r2->x + r2->w; 133 134 int r1y1 = r1->y; 135 int r1y2 = r1->y + r1->h; 136 int r2y1 = r2->y; 137 int r2y2 = r2->y + r2->h; 138 139 //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 140 if 141 ( 142 (r1x1 <= r2x2 && r1x1 >= r2x1) || 143 (r1x2 <= r2x2 && r1x2 >= r2x1) || 144 (r1x1 <= r2x1 && r1x2 >= r2x2) 145 ) 146 { 147 if 148 ( 149 (r1y1 <= r2y2 && r1y1 >= r2y1) || 150 (r1y2 <= r2y2 && r1y2 >= r2y1) || 151 (r1y1 <= r2y1 && r1y2 >= r2y2) 152 ) 153 { 154 //gCons->ConsPrintf("Rect Intersect Detected!\n"); 155 return true; 156 } 157 } 158 //gCons->ConsPrintf("No Rect Intersect Detected!\n"); 159 return false; 160 } 161 162 bool DetectCenterPointIntersect(SDL_Rect* zone, SDL_Rect* point) 163 { 164 //gDiagDraw->LogDiagRect(*point); 165 int pointX = point->x + (int)((float)point->w / 2); 166 int pointY = point->y + (int)((float)point->h / 2); 167 if ( (zone->x <= pointX) && (pointX <= (zone->x + zone->w)) ) 168 { 169 if ((zone->y <= pointY) && (pointY <= (zone->y + zone->h))) 170 { 171 //gCons->ConsPrintf("Center point intersect detected\n"); 172 return true; 173 } 174 } 175 //gCons->ConsPrintf("center point not intersecting\n"); 176 return false; 177 } 178 179 int CalcDistance(int x1, int y1, int x2, int y2) 180 { 181 int Xdelt = abs(x1 - x2); 182 int Ydelt = abs(y1 - y2); 183 184 return (int)sqrt((Xdelt*Xdelt) + (Ydelt*Ydelt)); 185 } 186 187 int EvenOutcomes(int NumberofOutcomes) 188 { 189 int i = (rand() % (NumberofOutcomes)) + 1; 190 //gCons->ConsPrintf("Roll : %i\n", i); 191 return i; 192 } 193 194 int RandRange(int min, int max) 195 { 196 int i = (min + (rand() % (max-min))); 197 //gCons->ConsPrintf("Range Roll : %i\n", i); 198 return i; 199 } 200 201 202 bool IsEven(int i) //Not working alawys return true 203 { 204 if ( (i % 2) == 0) 205 { 206 return true; 207 } 208 else 209 { 210 return false; 211 } 212 }