main.cpp 4.0 KB

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