main.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //Basiques
  2. #include <iostream>
  3. #include <cmath>
  4. //SDL
  5. #include <SDL/SDL.h>
  6. #undef main
  7. //#include <SDL/SDL_rotozoom.h>
  8. int main ( int argc, char** argv )
  9. {
  10. /// [1] Démarrage
  11. // [1.1] Démarrages SDL
  12. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  13. {
  14. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  15. return 1;
  16. }
  17. // [1.2] Préparation de fermeture
  18. atexit(SDL_Quit);
  19. // [1.3] Para-fenêtre
  20. SDL_WM_SetCaption("Sword_Finder", 0);
  21. /// [2] Préparation des composants
  22. // [2.1] Préparation de la fenêtre
  23. SDL_Surface* screen = SDL_SetVideoMode(1280, 720, 32,
  24. SDL_HWSURFACE|SDL_DOUBLEBUF);
  25. if ( !screen )
  26. {
  27. std::cout << "Bug à l'initialisation: " << SDL_GetError() << std::endl;
  28. return 1;
  29. }
  30. // [2.2] Préparation variables
  31. int range(100) ;
  32. SDL_Rect mouse({0, 0, 2*range, 2*range});
  33. SDL_Rect pos({0,0,0,0});
  34. bool grip(false);
  35. // [2.3] Préparation surface
  36. SDL_Surface* text = 0x0;
  37. text = SDL_LoadBMP("pierres.bmp");
  38. if ( !text )
  39. std::cout << "Pb avec la texture." << std::endl;
  40. SDL_Surface* obj = 0x0;
  41. obj = SDL_LoadBMP("epee.bmp");
  42. SDL_SetColorKey(obj, SDL_SRCCOLORKEY, 0xff00ff);
  43. SDL_Surface* full = 0x0;
  44. full = SDL_CreateRGBSurface(SDL_HWSURFACE, 1280, 720, 32, 0, 0, 0, 0) ;
  45. SDL_Surface* calc = 0x0;
  46. calc = SDL_CreateRGBSurface(SDL_HWSURFACE, 1280, 720, 32, 0, 0, 0, 0) ;
  47. // [2.4] Remplissage
  48. for ( int x(0); x < 1280; x += text->w )
  49. for ( int y(0); y < 720; y += text->h )
  50. {
  51. pos.x = x ;
  52. pos.y = y ;
  53. SDL_BlitSurface(text, 0x0, full, &pos) ;
  54. }
  55. pos.x = 200;
  56. pos.y = 400;
  57. /// [3] Boucle principale
  58. bool done = false;
  59. SDL_Event event;
  60. while (!done)
  61. {
  62. // [3.1] Gestion évènements
  63. while (SDL_PollEvent(&event))
  64. {
  65. switch (event.type)
  66. {
  67. case SDL_QUIT:
  68. done = true;
  69. break;
  70. case SDL_KEYDOWN:
  71. if (event.key.keysym.sym == SDLK_ESCAPE)
  72. done = true;
  73. if (event.key.keysym.sym == SDLK_UP)
  74. range += 5;
  75. if (event.key.keysym.sym == SDLK_DOWN)
  76. range -= 5;
  77. break;
  78. case SDL_MOUSEMOTION:
  79. mouse.x = event.motion.x - range ;
  80. mouse.y = event.motion.y - range ;
  81. mouse.w = 2*range ;
  82. mouse.h = 2*range ;
  83. if (grip) {
  84. pos.x += event.motion.xrel;
  85. pos.y += event.motion.yrel;
  86. }
  87. break;
  88. case SDL_MOUSEBUTTONDOWN:
  89. grip = true;
  90. break;
  91. case SDL_MOUSEBUTTONUP:
  92. grip = false;
  93. break;
  94. } // end switch event type
  95. } // end of message processing
  96. // [3.2] Calculs
  97. // [3.3] Dessin des composants
  98. SDL_FillRect(screen, 0, 0x000000);
  99. SDL_BlitSurface(full, 0x0, calc, 0x0);
  100. SDL_BlitSurface(obj, 0x0, calc, &pos);
  101. SDL_BlitSurface(calc, &mouse, screen, &mouse);
  102. SDL_Flip(screen);
  103. // [3.4] Temps
  104. SDL_Delay(30);
  105. } //fin bcl principale
  106. ///Performance
  107. /*SDL_Surface* m_surface[1000];
  108. Uint32 temps(SDL_GetTicks());///Chrono START
  109. for (int i(0); i<1000; i++) m_surface[i] = rotozoomSurface(epee, 35.0, 1.0, 1);
  110. temps = SDL_GetTicks() - temps;///Chrono END
  111. for (int i(0); i<1000; i++) SDL_FreeSurface(m_surface[i]);
  112. std::cout << "Temps de calcul : " << temps << std::endl;*/
  113. ///[4] Destruction des composants
  114. SDL_FreeSurface(screen);
  115. SDL_FreeSurface(text);
  116. SDL_FreeSurface(full);
  117. return 0;
  118. }