int main(int argc, char *argv[]) { // Define the window. If the window definition fails, return a failure. CWindow Window; if (!Window.DefineWindow(WINDOW_WIDTH, WINDOW_HEIGHT, 32, false, "Tanks and Tactics - Tech Demo (build 0004)")) return -1; // Create a GameController instance and allow it to run CGameController pGame; pGame.SetWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); return pGame.Run(); }
// PROGRAM START int main(int argc, char *argv[]) { srand((unsigned int)(time(0))); #if SDL_BYTEORDER == SDL_BIG_ENDIAN RMASK = 0xff000000; GMASK = 0x00ff0000; BMASK = 0x0000ff00; AMASK = 0x000000ff; #else RMASK = 0x000000ff; GMASK = 0x0000ff00; BMASK = 0x00ff0000; AMASK = 0xff000000; #endif // Load in the target image and create the two additional surfaces Target = IMG_Load("TestImage.png"); if (Target->w > IMAGE_MAX_W) return -1; if (Target->h > IMAGE_MAX_H) return -2; Best = SDL_CreateRGBSurface(SDL_SWSURFACE, Target->w, Target->h, 32, RMASK, GMASK, BMASK, AMASK); Evolve = SDL_CreateRGBSurface(SDL_SWSURFACE, Target->w, Target->h, 32, RMASK, GMASK, BMASK, AMASK); // If the surfaces are wider or taller than the maximum render area, decide how to scale it to fit RenderScale = 1.0; if ((Target->w > RENDER_MAX_W) || (Target->h > RENDER_MAX_H)) RenderScale = ((Target->w > Target->h) ? (double)(Target->w) / (double)(RENDER_MAX_W) : (double)(Target->h) / (double)(RENDER_MAX_H)); // Find the pixel byte size (to avoid dividing over and over later (probably 4) TargetPixelSize = Target->pitch / Target->w; EvolvePixelSize = Evolve->pitch / Evolve->w; // Create bit shifting elements for later based on the system endian-ness and whether the image is 24-bit or 32-bit #if SDL_BYTEORDER == SDL_BIG_ENDIAN TargetBitMask_R = 0xff << (8 * (TargetPixelSize - 1)); TargetPixelShift_R = 8 * (TargetPixelSize - 1); TargetBitMask_G = 0xff << (8 * (TargetPixelSize - 2)); TargetPixelShift_G = 8 * (TargetPixelSize - 2); TargetBitMask_B = 0xff << (8 * (TargetPixelSize - 3)); TargetPixelShift_B = 8 * (TargetPixelSize - 3); EvolveBitMask_R = 0xff << (8 * (EvolvePixelSize - 1)); EvolvePixelShift_R = 8 * (EvolvePixelSize - 1); EvolveBitMask_G = 0xff << (8 * (EvolvePixelSize - 2)); EvolvePixelShift_G = 8 * (EvolvePixelSize - 2); EvolveBitMask_B = 0xff << (8 * (EvolvePixelSize - 3)); EvolvePixelShift_B = 8 * (EvolvePixelSize - 3); #else TargetBitMask_R = 0xff << (8 * (TargetPixelSize - 3)); TargetPixelShift_R = 8 * (TargetPixelSize - 3); TargetBitMask_G = 0xff << (8 * (TargetPixelSize - 2)); TargetPixelShift_G = 8 * (TargetPixelSize - 2); TargetBitMask_B = 0xff << (8 * (TargetPixelSize - 1)); TargetPixelShift_B = 8 * (TargetPixelSize - 1); EvolveBitMask_R = 0xff << (8 * (EvolvePixelSize - 3)); EvolvePixelShift_R = 8 * (EvolvePixelSize - 3); EvolveBitMask_G = 0xff << (8 * (EvolvePixelSize - 2)); EvolvePixelShift_G = 8 * (EvolvePixelSize - 2); EvolveBitMask_B = 0xff << (8 * (EvolvePixelSize - 1)); EvolvePixelShift_B = 8 * (EvolvePixelSize - 1); #endif // Load in the parts of the render rects that don't change SourceRect.x = SourceRect.y = 0; DestinationRect.w = SourceRect.w = Target->w; DestinationRect.h = SourceRect.h = Target->h; // Create the window if(!Window.DefineWindow(SCREEN_W, SCREEN_H, 32, false, "Triangle Image Evolution")) return -1; OpeningTick = SDL_GetTicks(); TotalTime = 0.0; // Define all ListPolygon instances to a RANDOM state for (unsigned int i = 0; i < POLYGON_COUNT; i++) RandPolygon(&PolygonList[i]); // Calculate the starting fitness TempFitness = 0.0; Fitness = CalculateFitness(); // Set the mutation information to NULL Attempts = 0; Mutations = 0; GoodMutations = 0; RenderSwitch = true; // Define the mutation type MutationType = Mutate1; while (true) { // Poll for input if(SDL_PollEvent(&event) > 0) { if(event.type & (SDL_KEYUP | SDL_KEYDOWN)) { nKeyArray = SDL_GetKeyState(&nKeys); if (nKeyArray[SDLK_ESCAPE]) break; if (nKeyArray[SDLK_RETURN]) RenderSwitch = !RenderSwitch; if (nKeyArray[SDLK_1]) ExportBinary(); if (nKeyArray[SDLK_2]) ImportBinary(); if (nKeyArray[SDLK_3]) MutationType = Mutate1; if (nKeyArray[SDLK_4]) MutationType = Mutate2; } } // Go through a mutation sequence MutationType(PolygonList); if (RenderSwitch) RenderScreen(RenderScale); } SDL_FreeSurface(Target); SDL_FreeSurface(Best); SDL_FreeSurface(Evolve); Window.Undefine(); printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); return 0; }