main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Author : Jovian HERSEMEULE
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. #define BLOC_SIZE 16
  5. #define WIDTH 100
  6. #define HEIGHT 50
  7. #define BLOC_TOTAL 4
  8. enum blocType {WATER, MOUNTAIN, GRASS, DESERT};
  9. void putBloc(blocType bloc, float world[HEIGHT][WIDTH][BLOC_TOTAL], int y, int x, float level = 1.0f) {
  10. int dist;
  11. for (int j(0); j < HEIGHT; j++)
  12. for (int i(0); i < WIDTH; i++) {
  13. dist = (j - y)*(j - y) + 1 + (i - x)*(i - x);
  14. world[j][i][bloc] += level / dist;
  15. }
  16. }
  17. void drawWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL], SDL_Surface* tileSet[], SDL_Surface* out) {
  18. SDL_Rect pos({0, 0, 0, 0});
  19. int indexOfMax;
  20. for (int y(0); y < HEIGHT; y++)
  21. for (int x(0); x < WIDTH; x++) {
  22. indexOfMax = 0;
  23. pos.x = x * BLOC_SIZE;
  24. pos.y = y * BLOC_SIZE;
  25. // Select the tile with highest level
  26. for (int k(1); k < BLOC_TOTAL; k++)
  27. if (world[y][x][k] > world[y][x][indexOfMax]) {
  28. indexOfMax = k;
  29. }
  30. SDL_BlitSurface(tileSet[indexOfMax], NULL, out, &pos);
  31. }
  32. }
  33. int main()
  34. {
  35. /// [1] Start
  36. // [1.1] Start SDL
  37. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  38. {
  39. std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl;
  40. return 1;
  41. }
  42. // [1.2] Anticipate quit
  43. atexit(SDL_Quit);
  44. // [1.3] Set title
  45. SDL_WM_SetCaption("Application SDL", 0);
  46. /// [2] Create components
  47. // [2.1] Create window
  48. SDL_Surface* screen = SDL_SetVideoMode(WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  49. if ( !screen )
  50. {
  51. std::cout << "Unable to set " << WIDTH << "x" << HEIGHT << " video: " << SDL_GetError() << std::endl;
  52. return 1;
  53. }
  54. // [2.2] Load textures
  55. SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp");
  56. SDL_Surface* water = SDL_LoadBMP("textures/water.bmp");
  57. SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp");
  58. SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp");
  59. SDL_Surface* pawn = SDL_LoadBMP("textures/pawn.bmp");
  60. // [2.3] Create variables
  61. SDL_Rect pawnPos({0, 0, 0, 0});
  62. SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates
  63. // [2.4] Create world
  64. float world[HEIGHT][WIDTH][BLOC_TOTAL];
  65. // Clear world
  66. for (int j(0); j < HEIGHT; j++)
  67. for (int i(0); i < WIDTH; i++)
  68. for (int t(0); t < BLOC_TOTAL; t++)
  69. world[j][i][t] = 0.0f;
  70. // Basic water
  71. const float waterLevel = 1.0f;
  72. for (int j(0); j < HEIGHT; j++)
  73. for (int i(0); i < WIDTH; i++)
  74. world[j][i][WATER] = waterLevel / (HEIGHT - j);
  75. // Top mountain line
  76. const float mountainLevel = 1.0f;
  77. for (int j(0); j < HEIGHT; j++)
  78. for (int i(0); i < WIDTH; i++)
  79. world[j][i][MOUNTAIN] = mountainLevel / (1 + j);
  80. // [2.5] Surface containers
  81. SDL_Surface* tileSet[BLOC_TOTAL] = {water, mountain, grass, desert};
  82. SDL_Surface* map = SDL_CreateRGBSurface(SDL_HWSURFACE, WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, 0x0, 0x0, 0x0, 0x0);
  83. // [2.6] First map render
  84. drawWorld(world, tileSet, map);
  85. /// [3] Main loop
  86. bool done(false);
  87. while (!done)
  88. {
  89. // [3.1] Events
  90. SDL_Event event;
  91. while (SDL_PollEvent(&event))
  92. {
  93. switch (event.type)
  94. {
  95. case SDL_QUIT:
  96. done = true;
  97. break;
  98. case SDL_KEYDOWN:
  99. if (event.key.keysym.sym == SDLK_ESCAPE)
  100. done = true;
  101. break;
  102. case SDL_MOUSEMOTION:
  103. cursorPos.x = event.motion.x / BLOC_SIZE;
  104. cursorPos.y = event.motion.y / BLOC_SIZE;
  105. pawnPos.x = cursorPos.x * BLOC_SIZE;
  106. pawnPos.y = cursorPos.y * BLOC_SIZE;
  107. break;
  108. case SDL_MOUSEBUTTONDOWN:
  109. if (event.button.button == 1) {
  110. putBloc(DESERT, world, cursorPos.y, cursorPos.x);
  111. drawWorld(world, tileSet, map);
  112. }
  113. if (event.button.button == 3) {
  114. putBloc(GRASS, world, cursorPos.y, cursorPos.x);
  115. drawWorld(world, tileSet, map);
  116. }
  117. break;
  118. } // end switch event type
  119. } // end of message processing
  120. // [3.2] Compute
  121. // Nothing for now
  122. // [3.3] Draw phase
  123. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  124. SDL_BlitSurface(map, NULL, screen, NULL);
  125. SDL_BlitSurface(pawn, NULL, screen, &pawnPos);
  126. SDL_Flip(screen);
  127. SDL_Delay(16);
  128. } // end of main loop
  129. ///[4] Free components
  130. SDL_FreeSurface(grass);
  131. SDL_FreeSurface(water);
  132. SDL_FreeSurface(desert);
  133. SDL_FreeSurface(mountain);
  134. SDL_FreeSurface(pawn);
  135. SDL_FreeSurface(screen);
  136. std::cout << "No error caught." << std::endl;
  137. return 0;
  138. }