123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- // Author : Jovian HERSEMEULE
- #include <iostream>
- #include <SDL/SDL.h>
- 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("Application SDL", 0);
- /// [2] Create components
- // [2.1] Create window
- const int blocSize(16);
- const int width(100), height(50);
- SDL_Surface* screen = SDL_SetVideoMode(width * blocSize, height * blocSize, 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* pawn = SDL_LoadBMP("textures/pawn.bmp");
- // [2.3] Create variables
- SDL_Rect pos({0, 0, 0, 0});
- SDL_Rect pawnPos({0, 0, 0, 0});
- // [2.4] Create world
- const int blocTotal = 4;
- enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
- float world[height][width][blocTotal];
- // Clear world
- for (int j(0); j < height; j++)
- for (int i(0); i < width; i++)
- for (int t(0); t < blocTotal; t++)
- world[j][i][t] = 0.0f;
- // Basic water
- const int waterLevel = 0.01f;
- for (int j(0); j < height; j++)
- for (int i(0); i < width; i++)
- world[j][i][WATER] = waterLevel;
- // Top mountain line
- const int mountainLevel = 10.0f;
- for (int j(0); j < height; j++)
- for (int i(0); i < width; i++)
- world[j][i][MOUNTAIN] = mountainLevel / (1 + j);
- // [2.5] Surface containers
- SDL_Surface* select; // Current surface selected ! DO NOT FREE !
- SDL_Surface* tileSet[blocTotal] = {water, mountain, grass, desert};
- /// [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:
- if (event.key.keysym.sym == SDLK_ESCAPE)
- done = true;
- break;
- case SDL_MOUSEMOTION:
- pawnPos.x = (event.motion.x / pawn->w) * pawn->w;
- pawnPos.y = (event.motion.y / pawn->h) * pawn->h;
- 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));
- for (int y(0); y < height; y++)
- for (int x(0); x < width; x++) {
- select = tileSet[0];
- pos.x = x * blocSize;
- pos.y = y * blocSize;
- // Select the tile with highest level
- for (int k(1); k < blocTotal; k++)
- if (world[y][x][k] > world[y][x][k - 1])
- select = tileSet[k];
- SDL_BlitSurface(select, NULL, screen, &pos);
- }
- SDL_BlitSurface(pawn, 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(pawn);
- SDL_FreeSurface(screen);
- std::cout << "No error caught." << std::endl;
- return 0;
- }
|