main.cpp 4.0 KB

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