main.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Author : Jovian HERSEMEULE
  2. #include <iostream>
  3. #include <SDL/SDL.h>
  4. int main()
  5. {
  6. /// [1] Start
  7. // [1.1] Start SDL
  8. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  9. {
  10. std::cout << "Can't initialize SDL: " << SDL_GetError() << std::endl;
  11. return 1;
  12. }
  13. // [1.2] Anticipate quit
  14. atexit(SDL_Quit);
  15. // [1.3] Set title
  16. SDL_WM_SetCaption("Application SDL", 0);
  17. /// [2] Create components
  18. // [2.1] Create window
  19. const int width(640), height(480);
  20. SDL_Surface* screen = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
  21. if ( !screen )
  22. {
  23. std::cout << "Unable to set " << width << "x" << height << " video: " << SDL_GetError() << std::endl;
  24. return 1;
  25. }
  26. // [2.2] Other components
  27. SDL_Surface* grass = SDL_LoadBMP("textures/grass.bmp");
  28. SDL_Surface* water = SDL_LoadBMP("textures/water.bmp");
  29. SDL_Surface* desert = SDL_LoadBMP("textures/desert.bmp");
  30. SDL_Surface* mountain = SDL_LoadBMP("textures/mountain.bmp");
  31. SDL_Surface* pawn = SDL_LoadBMP("textures/pawn.bmp");
  32. SDL_Rect pos({0, 0, 0, 0});
  33. SDL_Rect pawnPos({0, 0, 0, 0});
  34. /// [3] Main loop
  35. bool done(false);
  36. while (!done)
  37. {
  38. // [3.1] Events
  39. SDL_Event event;
  40. while (SDL_PollEvent(&event))
  41. {
  42. switch (event.type)
  43. {
  44. case SDL_QUIT:
  45. done = true;
  46. break;
  47. case SDL_KEYDOWN:
  48. if (event.key.keysym.sym == SDLK_ESCAPE)
  49. done = true;
  50. break;
  51. case SDL_MOUSEMOTION:
  52. pawnPos.x = (event.motion.x / pawn->w) * pawn->w;
  53. pawnPos.y = (event.motion.y / pawn->h) * pawn->h;
  54. break;
  55. } // end switch event type
  56. } // end of message processing
  57. // [3.2] Compute
  58. // Nothing for now
  59. // [3.3] Draw phase
  60. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  61. for (pos.y = 0; pos.y < height; pos.y += water->h)
  62. for (pos.x = 0; pos.x < width; pos.x += water->w)
  63. SDL_BlitSurface(water, NULL, screen, &pos);
  64. SDL_BlitSurface(pawn, NULL, screen, &pawnPos);
  65. SDL_Flip(screen);
  66. SDL_Delay(16);
  67. } // end of main loop
  68. ///[4] Free components
  69. SDL_FreeSurface(grass);
  70. SDL_FreeSurface(water);
  71. SDL_FreeSurface(desert);
  72. SDL_FreeSurface(mountain);
  73. SDL_FreeSurface(pawn);
  74. SDL_FreeSurface(screen);
  75. std::cout << "No error caught." << std::endl;
  76. return 0;
  77. }