main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Author : Jovian HERSEMEULE
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. int main()
  5. {
  6. /// [1] Start
  7. // [1.1] Start SDL
  8. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  9. {
  10. std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl;
  11. return 1;
  12. }
  13. // [1.2] Anticipate quit
  14. atexit(SDL_Quit);
  15. // [1.3] Set title
  16. SDL_WM_SetCaption("Application SDL", 0);
  17. /// [2] Create components
  18. // [2.1] Create window
  19. const int blocSize(16);
  20. const int width(100), height(50);
  21. SDL_Surface* screen = SDL_SetVideoMode(width * blocSize, height * blocSize, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  22. if ( !screen )
  23. {
  24. std::cout << "Unable to set " << width << "x" << height << " video: " << SDL_GetError() << std::endl;
  25. return 1;
  26. }
  27. // [2.2] Load textures
  28. SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp");
  29. SDL_Surface* water = SDL_LoadBMP("textures/water.bmp");
  30. SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp");
  31. SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp");
  32. SDL_Surface* pawn = SDL_LoadBMP("textures/pawn.bmp");
  33. // [2.3] Create variables
  34. SDL_Rect pos({0, 0, 0, 0});
  35. SDL_Rect pawnPos({0, 0, 0, 0});
  36. // [2.4] Create world
  37. const int blocTotal = 4;
  38. enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
  39. float world[height][width][blocTotal];
  40. // Clear world
  41. for (int j(0); j < height; j++)
  42. for (int i(0); i < width; i++)
  43. for (int t(0); t < blocTotal; t++)
  44. world[j][i][t] = 0.0f;
  45. // Basic water
  46. const int waterLevel = 0.01f;
  47. for (int j(0); j < height; j++)
  48. for (int i(0); i < width; i++)
  49. world[j][i][WATER] = waterLevel;
  50. // Top mountain line
  51. const int mountainLevel = 10.0f;
  52. for (int j(0); j < height; j++)
  53. for (int i(0); i < width; i++)
  54. world[j][i][MOUNTAIN] = mountainLevel / (1 + j);
  55. // [2.5] Surface containers
  56. SDL_Surface* select; // Current surface selected ! DO NOT FREE !
  57. SDL_Surface* tileSet[blocTotal] = {water, mountain, grass, desert};
  58. /// [3] Main loop
  59. bool done(false);
  60. while (!done)
  61. {
  62. // [3.1] Events
  63. SDL_Event event;
  64. while (SDL_PollEvent(&event))
  65. {
  66. switch (event.type)
  67. {
  68. case SDL_QUIT:
  69. done = true;
  70. break;
  71. case SDL_KEYDOWN:
  72. if (event.key.keysym.sym == SDLK_ESCAPE)
  73. done = true;
  74. break;
  75. case SDL_MOUSEMOTION:
  76. pawnPos.x = (event.motion.x / pawn->w) * pawn->w;
  77. pawnPos.y = (event.motion.y / pawn->h) * pawn->h;
  78. break;
  79. } // end switch event type
  80. } // end of message processing
  81. // [3.2] Compute
  82. // Nothing for now
  83. // [3.3] Draw phase
  84. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  85. for (int y(0); y < height; y++)
  86. for (int x(0); x < width; x++) {
  87. select = tileSet[0];
  88. pos.x = x * blocSize;
  89. pos.y = y * blocSize;
  90. // Select the tile with highest level
  91. for (int k(1); k < blocTotal; k++)
  92. if (world[y][x][k] > world[y][x][k - 1])
  93. select = tileSet[k];
  94. SDL_BlitSurface(select, NULL, screen, &pos);
  95. }
  96. SDL_BlitSurface(pawn, NULL, screen, &pawnPos);
  97. SDL_Flip(screen);
  98. SDL_Delay(16);
  99. } // end of main loop
  100. ///[4] Free components
  101. SDL_FreeSurface(grass);
  102. SDL_FreeSurface(water);
  103. SDL_FreeSurface(desert);
  104. SDL_FreeSurface(mountain);
  105. SDL_FreeSurface(pawn);
  106. SDL_FreeSurface(screen);
  107. std::cout << "No error caught." << std::endl;
  108. return 0;
  109. }