ef1547dfb6c30b50941d5f33e81ac7a546473205.svn-base (4909B)
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 bool ReadBTree(Node* root, char* str) 108 { 109 if (root->IsActive()) 110 { 111 char buff[64]; 112 memset(buff, 0, sizeof(buff)); 113 114 _itoa(root->GetItem(), buff, 10); 115 strcat(str, buff); 116 strcat(str, "("); 117 ReadBTree(root->mLeft, str); 118 ReadBTree(root->mRight, str); 119 strcat(str, ")"); 120 return false; 121 } 122 else 123 { 124 return true; 125 } 126 } 127 128 void PrintBTree(Node* root) 129 { 130 char buff[256]; 131 memset(buff, 0, sizeof(buff)); 132 133 ReadBTree(root, buff); 134 strcat(buff, "\n"); 135 136 gCons->ConsPrintf(buff); 137 } 138 139 float GetSign(float x) 140 { 141 if (x > 0) { return 1; } 142 if (x < 0) { return -1; } 143 return 0; 144 } 145 146 bool DetectRectIntersect(SDL_Rect* r1, SDL_Rect* r2) 147 { 148 //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 149 150 int r1x1 = r1->x; 151 int r1x2 = r1->x + r1->w; 152 int r2x1 = r2->x; 153 int r2x2 = r2->x + r2->w; 154 155 int r1y1 = r1->y; 156 int r1y2 = r1->y + r1->h; 157 int r2y1 = r2->y; 158 int r2y2 = r2->y + r2->h; 159 160 //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 161 if 162 ( 163 (r1x1 <= r2x2 && r1x1 >= r2x1) || 164 (r1x2 <= r2x2 && r1x2 >= r2x1) || 165 (r1x1 <= r2x1 && r1x2 >= r2x2) 166 ) 167 { 168 if 169 ( 170 (r1y1 <= r2y2 && r1y1 >= r2y1) || 171 (r1y2 <= r2y2 && r1y2 >= r2y1) || 172 (r1y1 <= r2y1 && r1y2 >= r2y2) 173 ) 174 { 175 //gCons->ConsPrintf("Rect Intersect Detected!\n"); 176 return true; 177 } 178 } 179 //gCons->ConsPrintf("No Rect Intersect Detected!\n"); 180 return false; 181 } 182 183 bool DetectCenterPointIntersect(SDL_Rect* zone, SDL_Rect* point) //this is a likely culprit 184 { 185 gDiagDraw->LogDiagRect(*point); 186 int pointX = point->x + (int)((float)point->w / 2); 187 int pointY = point->y + (int)((float)point->h / 2); 188 if ( (zone->x <= pointX) && (pointX <= (zone->x + zone->w)) ) 189 { 190 if ((zone->y <= pointY) && (pointY <= (zone->y + zone->h))) 191 { 192 //gCons->ConsPrintf("Center point intersect detected\n"); 193 return true; 194 } 195 } 196 //gCons->ConsPrintf("center point not intersecting\n"); 197 return false; 198 } 199 200 int CalcDistance(int x1, int y1, int x2, int y2) 201 { 202 int Xdelt = abs(x1 - x2); 203 int Ydelt = abs(y1 - y2); 204 205 return (int)sqrt((Xdelt*Xdelt) + (Ydelt*Ydelt)); 206 } 207 208 int EvenOutcomes(int NumberofOutcomes) 209 { 210 int i = (rand() % (NumberofOutcomes)) + 1; 211 //gCons->ConsPrintf("Roll : %i\n", i); 212 return i; 213 } 214 215 int RandRange(int min, int max) 216 { 217 int i = (min + (rand() % (max-min))); 218 //gCons->ConsPrintf("Range Roll : %i\n", i); 219 return i; 220 } 221 222 223 bool IsEven(int i) //Not working alawys return true 224 { 225 if ( (i % 2) == 0) 226 { 227 return true; 228 } 229 else 230 { 231 return false; 232 } 233 }