// Author : Jovian HERSEMEULE #include #include #include #include #define BLOC_SIZE 16 #define WIDTH 100 #define HEIGHT 50 #define BLOC_TOTAL 5 #define MAX_LEVEL 1000 // * 10-3 enum BlocType {WATER, MOUNTAIN, GRASS, DESERT, PLAIN}; class Seed { public: BlocType type; int y, x; float level; public: Seed():type(GRASS), y(HEIGHT / 2), x(WIDTH / 2), level(1.0f) {} void randomize() { type = (BlocType) (rand() % BLOC_TOTAL); y = rand() % HEIGHT; x = rand() % WIDTH; level = 0.001f * (rand() % MAX_LEVEL); } }; void putSeed(Seed s, float world[HEIGHT][WIDTH][BLOC_TOTAL]) { int dist; for (int j(0); j < HEIGHT; j++) for (int i(0); i < WIDTH; i++) { dist = (j - s.y)*(j - s.y) + 1 + (i - s.x)*(i - s.x); world[j][i][s.type] += s.level / dist; } } void putHorizonLine(int y, BlocType bloc, float world[HEIGHT][WIDTH][BLOC_TOTAL], float level = 1.0f) { for (int j(0); j < HEIGHT; j++) for (int i(0); i < WIDTH; i++) world[j][i][(int) bloc] = level / (abs(y - j) + 1); } void clearWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL]) { // Erase everything for (int j(0); j < HEIGHT; j++) for (int i(0); i < WIDTH; i++) for (int bloc(0); bloc < BLOC_TOTAL; bloc++) world[j][i][bloc] = 0.0f; // Top mountain line const float mountainLevel = 1.0f; putHorizonLine(0, MOUNTAIN, world, mountainLevel); // Pseudo road const float roadLevel = 0.5f; putHorizonLine(HEIGHT / 2, PLAIN, world, roadLevel); // Bottom water line const float waterLevel = 1.0f; putHorizonLine(HEIGHT - 1, WATER, world, waterLevel); } void drawWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL], SDL_Surface* tileSet[], SDL_Surface* out) { SDL_Rect pos({0, 0, 0, 0}); int indexOfMax; for (int y(0); y < HEIGHT; y++) for (int x(0); x < WIDTH; x++) { indexOfMax = 0; pos.x = x * BLOC_SIZE; pos.y = y * BLOC_SIZE; // Select the tile with highest level for (int k(1); k < BLOC_TOTAL; k++) if (world[y][x][k] > world[y][x][indexOfMax]) { indexOfMax = k; } SDL_BlitSurface(tileSet[indexOfMax], NULL, out, &pos); } } int main() { /// [1] Start // [1.1] Start SDL if ( SDL_Init( SDL_INIT_VIDEO ) < 0) { std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl; return 1; } // [1.2] Anticipate quit atexit(SDL_Quit); // [1.3] Set title SDL_WM_SetCaption("Biome Box", 0); // [1.4] Init random srandom(time(0)); /// [2] Create components // [2.1] Create window SDL_Surface* screen = SDL_SetVideoMode(WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, SDL_HWSURFACE|SDL_DOUBLEBUF); if ( !screen ) { std::cout << "Unable to set " << WIDTH << "x" << HEIGHT << " video: " << SDL_GetError() << std::endl; return 1; } // [2.2] Load textures SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp"); SDL_Surface* water = SDL_LoadBMP("textures/water.bmp"); SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp"); SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp"); SDL_Surface* plain = SDL_LoadBMP("textures/plain.bmp"); // [2.3] Create variables SDL_Rect pawnPos({0, 0, 0, 0}); SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates Seed seed; // [2.4] Create world float world[HEIGHT][WIDTH][BLOC_TOTAL]; clearWorld(world); // [2.5] Surface containers SDL_Surface* tileSet[BLOC_TOTAL] = {water, mountain, grass, desert, plain}; SDL_Surface* map = SDL_CreateRGBSurface(SDL_HWSURFACE, WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, 0x0, 0x0, 0x0, 0x0); SDL_Surface* tileCurrent = tileSet[0]; // [2.6] First map render drawWorld(world, tileSet, map); /// [3] Main loop bool done(false); while (!done) { // [3.1] Events SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = true; break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: done = true; break; case SDLK_BACKSPACE: clearWorld(world); drawWorld(world, tileSet, map); break; case SDLK_RETURN: for (int k(0); k < 10; k++) { seed.randomize(); putSeed(seed, world); } drawWorld(world, tileSet, map); break; case SDLK_SPACE: seed.randomize(); putSeed(seed, world); drawWorld(world, tileSet, map); break; } case SDL_MOUSEMOTION: cursorPos.x = event.motion.x / BLOC_SIZE; cursorPos.y = event.motion.y / BLOC_SIZE; pawnPos.x = cursorPos.x * BLOC_SIZE; pawnPos.y = cursorPos.y * BLOC_SIZE; break; case SDL_MOUSEBUTTONDOWN: if (event.button.button == 1) { seed.x = cursorPos.x; seed.y = cursorPos.y; putSeed(seed, world); drawWorld(world, tileSet, map); } if (event.button.button == 3) { int seedType = (int) seed.type; seedType++; if (seedType >= BLOC_TOTAL) seedType = 0; seed.type = (BlocType) seedType; } break; } // end switch event type } // end of message processing // [3.2] Compute // Nothing for now // [3.3] Draw phase SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255)); SDL_BlitSurface(map, NULL, screen, NULL); SDL_BlitSurface(tileSet[seed.type], NULL, screen, &pawnPos); SDL_Flip(screen); SDL_Delay(16); } // end of main loop ///[4] Free components SDL_FreeSurface(grass); SDL_FreeSurface(water); SDL_FreeSurface(desert); SDL_FreeSurface(mountain); SDL_FreeSurface(plain); SDL_FreeSurface(screen); std::cout << "No error caught." << std::endl; return 0; }