main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. //Projet
  9. #include "Vecteur.h"
  10. int main ( int argc, char** argv )
  11. {
  12. /// [1] Démarrage
  13. // [1.1] Démarrages SDL
  14. if ( SDL_Init( SDL_INIT_VIDEO ) < 0)
  15. {
  16. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  17. return 1;
  18. }
  19. // [1.2] Préparation de fermeture
  20. atexit(SDL_Quit);
  21. // [1.3] Para-fenêtre
  22. SDL_WM_SetCaption("Rotator", 0);
  23. /// [2] Préparation des composants
  24. // [2.1] Préparation de la fenêtre
  25. SDL_Surface* screen = SDL_SetVideoMode(1280, 720, 32,
  26. SDL_HWSURFACE|SDL_DOUBLEBUF);
  27. if ( !screen )
  28. {
  29. std::cout << "Unable to set 640x480 video: " << SDL_GetError() << std::endl;
  30. return 1;
  31. }
  32. // [2.2] Préparation surface
  33. SDL_Rect pos;
  34. SDL_Surface* epee = SDL_LoadBMP("Perso.bmp");
  35. SDL_SetColorKey(epee, SDL_SRCCOLORKEY, SDL_MapRGBA(epee->format, 0, 0, 0, 255));
  36. SDL_Surface* rotation = rotozoomSurface(epee, 0.0, 1.0, 1);
  37. double angle(0.0);
  38. Vec dir;
  39. Vec axeY( 0.0f, 1.0f );
  40. /// [3] Boucle principale
  41. bool done = false;
  42. SDL_Event event;
  43. while (!done)
  44. {
  45. // [3.1] Gestion évènements
  46. while (SDL_PollEvent(&event))
  47. {
  48. switch (event.type)
  49. {
  50. case SDL_QUIT:
  51. done = true;
  52. break;
  53. case SDL_KEYDOWN:
  54. if (event.key.keysym.sym == SDLK_ESCAPE)
  55. done = true;
  56. case SDL_MOUSEMOTION:
  57. dir.setX( event.motion.x - screen->w/2 );
  58. dir.setY( screen->h/2 - event.motion.y );
  59. angle = atan((double)(dir.getY()) / (dir.getX()+0.5f)); // Angle en radians
  60. angle = angle * 180.0 / M_PI; // Angle en ° + correction pointage
  61. if ( dir.getX() < 0 ) angle += 180;
  62. break;
  63. } // end switch event type
  64. } // end of message processing
  65. // [3.2] Calculs
  66. SDL_FreeSurface(rotation);
  67. rotation = rotozoomSurface(epee, angle, 1.0, 0);
  68. pos.x = screen->w/2 - rotation->w/2;
  69. pos.y = screen->h/2 - rotation->h/2;
  70. // [3.3] Dessin des composants
  71. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 255, 0));
  72. SDL_BlitSurface(rotation, 0, screen, &pos);
  73. SDL_Flip(screen);
  74. // [3.4] Temps
  75. SDL_Delay(30);
  76. } //fin bcl principale
  77. ///Performance
  78. /*SDL_Surface* m_surface[1000];
  79. Uint32 temps(SDL_GetTicks());///Chrono START
  80. for (int i(0); i<1000; i++) m_surface[i] = rotozoomSurface(epee, 35.0, 1.0, 1);
  81. temps = SDL_GetTicks() - temps;///Chrono END
  82. for (int i(0); i<1000; i++) SDL_FreeSurface(m_surface[i]);
  83. std::cout << "Temps de calcul : " << temps << std::endl;*/
  84. ///[4] Destruction des composants
  85. SDL_FreeSurface(screen);
  86. SDL_FreeSurface(epee);
  87. SDL_FreeSurface(rotation);
  88. return 0;
  89. }