main.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_ttf.h>
  6. int main ( int argc, char** argv )
  7. {
  8. /// [1] Démarrage
  9. // [1.1] Démarrages SDL et TTF
  10. if ( SDL_Init( SDL_INIT_VIDEO ) < 0 || TTF_Init() < 0 )
  11. {
  12. std::cout << "Impossible d'initialiser la SDL: " << SDL_GetError() << std::endl;
  13. std::cout << "Ou problème de TTf: " << TTF_GetError() << std::endl;
  14. return 1;
  15. }
  16. // [1.2] Préparation de fermeture
  17. atexit(SDL_Quit);
  18. atexit(TTF_Quit);
  19. // [1.3] Para-fenêtre
  20. SDL_WM_SetCaption("Gestion du texte avec SDL_ttf", 0);
  21. /// [2] Préparation des composants
  22. // [2.1] Préparation de la fenêtre
  23. SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
  24. SDL_HWSURFACE|SDL_DOUBLEBUF);
  25. if ( !screen )
  26. {
  27. printf("Unable to set 640x480 video: %s\n", SDL_GetError());
  28. return 1;
  29. }
  30. // [2.2] Préparation d'image(s)
  31. SDL_Surface *texte(0);
  32. SDL_Rect position;
  33. // [2.3] Préparation du texte
  34. TTF_Font *police(0);
  35. police = TTF_OpenFont("Polices/droid.ttf",65);
  36. SDL_Color couleurNoire = {0,0,0};
  37. std::string chaine(" ");
  38. /// [3] Boucle principale
  39. bool done = false;
  40. while (!done)
  41. {
  42. // [3.1] Gestion évènements
  43. SDL_Event event;
  44. while (SDL_PollEvent(&event))
  45. {
  46. switch (event.type)
  47. {
  48. case SDL_QUIT:
  49. done = true;
  50. break;
  51. case SDL_KEYDOWN:
  52. if (event.key.keysym.sym == SDLK_ESCAPE)
  53. done = true;
  54. break;
  55. case SDL_KEYUP:
  56. switch (event.key.keysym.sym)
  57. {
  58. case SDLK_BACKSPACE:
  59. if (chaine.size()!=1)
  60. chaine.erase(chaine.size()-1,1);
  61. break;
  62. case SDLK_a:
  63. chaine+='a';
  64. break;
  65. case SDLK_z:
  66. chaine+='z';
  67. break;
  68. case SDLK_e:
  69. chaine+='e';
  70. break;
  71. case SDLK_r:
  72. chaine+='r';
  73. break;
  74. case SDLK_t:
  75. chaine+='t';
  76. break;
  77. case SDLK_y:
  78. chaine+='y';
  79. break;
  80. case SDLK_u:
  81. chaine+='u';
  82. break;
  83. case SDLK_i:
  84. chaine+='i';
  85. break;
  86. case SDLK_o:
  87. chaine+='o';
  88. break;
  89. case SDLK_p:
  90. chaine+='p';
  91. break;
  92. case SDLK_q:
  93. chaine+='q';
  94. break;
  95. case SDLK_s:
  96. chaine+='s';
  97. break;
  98. case SDLK_d:
  99. chaine+='d';
  100. break;
  101. case SDLK_f:
  102. chaine+='f';
  103. break;
  104. case SDLK_g:
  105. chaine+='g';
  106. break;
  107. case SDLK_h:
  108. chaine+='h';
  109. break;
  110. case SDLK_j:
  111. chaine+='j';
  112. break;
  113. case SDLK_k:
  114. chaine+='k';
  115. break;
  116. case SDLK_l:
  117. chaine+='l';
  118. break;
  119. case SDLK_m:
  120. chaine+='m';
  121. break;
  122. case SDLK_w:
  123. chaine+='w';
  124. break;
  125. case SDLK_x:
  126. chaine+='x';
  127. break;
  128. case SDLK_c:
  129. chaine+='c';
  130. break;
  131. case SDLK_v:
  132. chaine+='v';
  133. break;
  134. case SDLK_b:
  135. chaine+='b';
  136. break;
  137. case SDLK_n:
  138. chaine+='n';
  139. break;
  140. case SDLK_SPACE:
  141. chaine+=' ';
  142. break;
  143. }
  144. break;
  145. } // end switch event type
  146. } // end of message processing
  147. // [3.2] Calculs
  148. texte = TTF_RenderText_Blended(police, chaine.c_str(),couleurNoire);
  149. // [3.3] Dessin des composants
  150. SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 128, 128));
  151. position.x = screen->w/2 - texte->w/2;
  152. position.y = screen->h/2 - texte->h/2;
  153. SDL_BlitSurface(texte, NULL, screen, &position);
  154. SDL_Flip(screen);
  155. } //fin bcl principale
  156. ///[4] Destruction des composants
  157. TTF_CloseFont(police);
  158. SDL_FreeSurface(texte);
  159. std::cout << "Aucune erreur détectée." << std::endl;
  160. return 0;
  161. }