main.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 4
  10. #define MAX_LEVEL 1000 // * 10-3
  11. enum BlocType {WATER, MOUNTAIN, GRASS, DESERT};
  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. // [2.3] Create variables
  97. SDL_Rect pawnPos({0, 0, 0, 0});
  98. SDL_Rect cursorPos({0, 0, 0, 0}); // Bloc coordinates
  99. Seed seed;
  100. // [2.4] Create world
  101. float world[HEIGHT][WIDTH][BLOC_TOTAL];
  102. clearWorld(world);
  103. // [2.5] Surface containers
  104. SDL_Surface* tileSet[BLOC_TOTAL] = {water, mountain, grass, desert};
  105. SDL_Surface* map = SDL_CreateRGBSurface(SDL_HWSURFACE, WIDTH * BLOC_SIZE, HEIGHT * BLOC_SIZE, 32, 0x0, 0x0, 0x0, 0x0);
  106. SDL_Surface* tileCurrent = tileSet[0];
  107. // [2.6] First map render
  108. drawWorld(world, tileSet, map);
  109. /// [3] Main loop
  110. bool done(false);
  111. while (!done)
  112. {
  113. // [3.1] Events
  114. SDL_Event event;
  115. while (SDL_PollEvent(&event))
  116. {
  117. switch (event.type)
  118. {
  119. case SDL_QUIT:
  120. done = true;
  121. break;
  122. case SDL_KEYDOWN:
  123. switch(event.key.keysym.sym)
  124. {
  125. case SDLK_ESCAPE:
  126. done = true;
  127. break;
  128. case SDLK_BACKSPACE:
  129. clearWorld(world);
  130. drawWorld(world, tileSet, map);
  131. break;
  132. case SDLK_SPACE:
  133. seed.randomize();
  134. putSeed(seed, world);
  135. drawWorld(world, tileSet, map);
  136. break;
  137. }
  138. case SDL_MOUSEMOTION:
  139. cursorPos.x = event.motion.x / BLOC_SIZE;
  140. cursorPos.y = event.motion.y / BLOC_SIZE;
  141. pawnPos.x = cursorPos.x * BLOC_SIZE;
  142. pawnPos.y = cursorPos.y * BLOC_SIZE;
  143. break;
  144. case SDL_MOUSEBUTTONDOWN:
  145. if (event.button.button == 1) {
  146. seed.x = cursorPos.x;
  147. seed.y = cursorPos.y;
  148. putSeed(seed, world);
  149. drawWorld(world, tileSet, map);
  150. }
  151. if (event.button.button == 3) {
  152. int seedType = (int) seed.type;
  153. seedType++;
  154. if (seedType >= BLOC_TOTAL) seedType = 0;
  155. seed.type = (BlocType) seedType;
  156. }
  157. break;
  158. } // end switch event type
  159. } // end of message processing
  160. // [3.2] Compute
  161. // Nothing for now
  162. // [3.3] Draw phase
  163. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  164. SDL_BlitSurface(map, NULL, screen, NULL);
  165. SDL_BlitSurface(tileSet[seed.type], NULL, screen, &pawnPos);
  166. SDL_Flip(screen);
  167. SDL_Delay(16);
  168. } // end of main loop
  169. ///[4] Free components
  170. SDL_FreeSurface(grass);
  171. SDL_FreeSurface(water);
  172. SDL_FreeSurface(desert);
  173. SDL_FreeSurface(mountain);
  174. SDL_FreeSurface(screen);
  175. std::cout << "No error caught." << std::endl;
  176. return 0;
  177. }