main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Author : Jovian HERSEMEULE
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #define BLOC_SIZE 16
  7. #define WIDTH 100
  8. #define HEIGHT 50
  9. #define BLOC_TOTAL 5
  10. #define MAX_LEVEL 1000 // * 10-3
  11. enum BlocType {WATER, MOUNTAIN, GRASS, DESERT, PLAIN};
  12. class Seed {
  13. public:
  14. BlocType type;
  15. int y, x;
  16. float level;
  17. public:
  18. Seed():type(GRASS), y(HEIGHT / 2), x(WIDTH / 2), level(1.0f) {}
  19. void randomize() {
  20. type = (BlocType) (rand() % BLOC_TOTAL);
  21. y = rand() % HEIGHT;
  22. x = rand() % WIDTH;
  23. level = 0.001f * (rand() % MAX_LEVEL);
  24. }
  25. };
  26. void putSeed(Seed s, float world[HEIGHT][WIDTH][BLOC_TOTAL]) {
  27. int dist;
  28. for (int j(0); j < HEIGHT; j++)
  29. for (int i(0); i < WIDTH; i++) {
  30. dist = (j - s.y)*(j - s.y) + 1 + (i - s.x)*(i - s.x);
  31. world[j][i][s.type] += s.level / dist;
  32. }
  33. }
  34. void putHorizonLine(int y, BlocType bloc, float world[HEIGHT][WIDTH][BLOC_TOTAL], float level = 1.0f) {
  35. for (int j(0); j < HEIGHT; j++)
  36. for (int i(0); i < WIDTH; i++)
  37. world[j][i][(int) bloc] = level / (abs(y - j) + 1);
  38. }
  39. void clearWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL]) {
  40. // Erase everything
  41. for (int j(0); j < HEIGHT; j++)
  42. for (int i(0); i < WIDTH; i++)
  43. for (int bloc(0); bloc < BLOC_TOTAL; bloc++)
  44. world[j][i][bloc] = 0.0f;
  45. // Bottom water line
  46. const float waterLevel = 1.0f;
  47. putHorizonLine(HEIGHT - 1, WATER, world, waterLevel);
  48. // Pseudo road
  49. putHorizonLine(HEIGHT / 2, PLAIN, world);
  50. // Top mountain line
  51. const float mountainLevel = 1.0f;
  52. putHorizonLine(0, MOUNTAIN, world, mountainLevel);
  53. }
  54. void drawWorld(float world[HEIGHT][WIDTH][BLOC_TOTAL], SDL_Surface* tileSet[], SDL_Surface* out) {
  55. SDL_Rect pos({0, 0, 0, 0});
  56. int indexOfMax;
  57. for (int y(0); y < HEIGHT; y++)
  58. for (int x(0); x < WIDTH; x++) {
  59. indexOfMax = 0;
  60. pos.x = x * BLOC_SIZE;
  61. pos.y = y * BLOC_SIZE;
  62. // Select the tile with highest level
  63. for (int k(1); k < BLOC_TOTAL; k++)
  64. if (world[y][x][k] > world[y][x][indexOfMax]) {
  65. indexOfMax = k;
  66. }
  67. SDL_BlitSurface(tileSet[indexOfMax], NULL, out, &pos);
  68. }
  69. }
  70. int main()
  71. {
  72. /// [1] Start
  73. // [1.1] Start SDL
  74. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  75. {
  76. std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl;
  77. return 1;
  78. }
  79. // [1.2] Anticipate quit
  80. atexit(SDL_Quit);
  81. // [1.3] Set title
  82. SDL_WM_SetCaption("Biome Box", 0);
  83. // [1.4] Init random
  84. srandom(time(0));
  85. /// [2] Create components
  86. // [2.1] Create window
  87. SDL_Surface* screen = SDL_SetVideoMode(WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  88. if ( !screen )
  89. {
  90. std::cout << "Unable to set " << WIDTH << "x" << HEIGHT << " video: " << SDL_GetError() << std::endl;
  91. return 1;
  92. }
  93. // [2.2] Load textures
  94. SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp");
  95. SDL_Surface* water = SDL_LoadBMP("textures/water.bmp");
  96. SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp");
  97. SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp");
  98. SDL_Surface* plain = SDL_LoadBMP("textures/plain.bmp");
  99. // [2.3] Create variables
  100. SDL_Rect pawnPos({0, 0, 0, 0});
  101. SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates
  102. Seed seed;
  103. // [2.4] Create world
  104. float world[HEIGHT][WIDTH][BLOC_TOTAL];
  105. clearWorld(world);
  106. // [2.5] Surface containers
  107. SDL_Surface* tileSet[BLOC_TOTAL] = {water, mountain, grass, desert, plain};
  108. SDL_Surface* map = SDL_CreateRGBSurface(SDL_HWSURFACE, WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, 0x0, 0x0, 0x0, 0x0);
  109. SDL_Surface* tileCurrent = tileSet[0];
  110. // [2.6] First map render
  111. drawWorld(world, tileSet, map);
  112. /// [3] Main loop
  113. bool done(false);
  114. while (!done)
  115. {
  116. // [3.1] Events
  117. SDL_Event event;
  118. while (SDL_PollEvent(&event))
  119. {
  120. switch (event.type)
  121. {
  122. case SDL_QUIT:
  123. done = true;
  124. break;
  125. case SDL_KEYDOWN:
  126. switch(event.key.keysym.sym)
  127. {
  128. case SDLK_ESCAPE:
  129. done = true;
  130. break;
  131. case SDLK_BACKSPACE:
  132. clearWorld(world);
  133. drawWorld(world, tileSet, map);
  134. break;
  135. case SDLK_RETURN:
  136. for (int k(0); k < 10; k++) {
  137. seed.randomize();
  138. putSeed(seed, world);
  139. }
  140. drawWorld(world, tileSet, map);
  141. break;
  142. case SDLK_SPACE:
  143. seed.randomize();
  144. putSeed(seed, world);
  145. drawWorld(world, tileSet, map);
  146. break;
  147. }
  148. case SDL_MOUSEMOTION:
  149. cursorPos.x = event.motion.x / BLOC_SIZE;
  150. cursorPos.y = event.motion.y / BLOC_SIZE;
  151. pawnPos.x = cursorPos.x * BLOC_SIZE;
  152. pawnPos.y = cursorPos.y * BLOC_SIZE;
  153. break;
  154. case SDL_MOUSEBUTTONDOWN:
  155. if (event.button.button == 1) {
  156. seed.x = cursorPos.x;
  157. seed.y = cursorPos.y;
  158. putSeed(seed, world);
  159. drawWorld(world, tileSet, map);
  160. }
  161. if (event.button.button == 3) {
  162. int seedType = (int) seed.type;
  163. seedType++;
  164. if (seedType >= BLOC_TOTAL) seedType = 0;
  165. seed.type = (BlocType) seedType;
  166. }
  167. break;
  168. } // end switch event type
  169. } // end of message processing
  170. // [3.2] Compute
  171. // Nothing for now
  172. // [3.3] Draw phase
  173. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  174. SDL_BlitSurface(map, NULL, screen, NULL);
  175. SDL_BlitSurface(tileSet[seed.type], NULL, screen, &pawnPos);
  176. SDL_Flip(screen);
  177. SDL_Delay(16);
  178. } // end of main loop
  179. ///[4] Free components
  180. SDL_FreeSurface(grass);
  181. SDL_FreeSurface(water);
  182. SDL_FreeSurface(desert);
  183. SDL_FreeSurface(mountain);
  184. SDL_FreeSurface(plain);
  185. SDL_FreeSurface(screen);
  186. std::cout << "No error caught." << std::endl;
  187. return 0;
  188. }