main.cpp 1.5 KB

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