main.cpp 3.5 KB

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