main.cpp 5.4 KB

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