main.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_Rect pos({0, 0, 0, 0});
  29. /// [3] Main loop
  30. bool done(false);
  31. while (!done)
  32. {
  33. // [3.1] Events
  34. SDL_Event event;
  35. while (SDL_PollEvent(&event))
  36. {
  37. switch (event.type)
  38. {
  39. case SDL_QUIT:
  40. done = true;
  41. break;
  42. case SDL_KEYDOWN:
  43. if (event.key.keysym.sym == SDLK_ESCAPE)
  44. done = true;
  45. break;
  46. } // end switch event type
  47. } // end of message processing
  48. // [3.2] Compute
  49. // Nothing for now
  50. // [3.3] Draw phase
  51. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 255));
  52. for (pos.y = 0; pos.y < height; pos.y += grass->h)
  53. for (pos.x = 0; pos.x < width; pos.x += grass->w)
  54. SDL_BlitSurface(grass, NULL, screen, &pos);
  55. SDL_Flip(screen);
  56. SDL_Delay(16);
  57. } // end of main loop
  58. ///[4] Free components
  59. SDL_FreeSurface(grass);
  60. SDL_FreeSurface(screen);
  61. std::cout << "No error caught." << std::endl;
  62. return 0;
  63. }