main.cpp 5.3 KB

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