Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. #include "IHM.h"
  2. #include <stdlib.h>
  3. #include <SDL2/SDL.h>
  4.  
  5. // variables globales nécessaire
  6.  
  7. // la fenetre
  8. // Notre fenêtre et le clavier
  9. SDL_Window* sdlWindow;
  10. SDL_Renderer *sdlRenderer;
  11. SDL_Event evenements;
  12.  
  13. // type pour les couleurs
  14. typedef struct
  15. {
  16. unsigned char R;
  17. unsigned char V;
  18. unsigned char B;
  19. unsigned char A;
  20. } TYPE_IHM_RVB;
  21.  
  22. typedef union
  23. {
  24. TYPE_IHM_RVB rvb;
  25. unsigned long int val;
  26. } TYPE_IHM_COUL;
  27.  
  28. // pour ne pas recharger le logo ...
  29. SDL_Texture * logo;
  30. // guestion des evenements
  31. char _quitter_;
  32. char _lastchar; // clavier
  33. int _mouse_x;
  34. int _mouse_y;
  35. unsigned char _mouse_b;
  36.  
  37.  
  38. void __IHM_traiter_event();
  39.  
  40. void IHM_init()
  41. {
  42. _quitter_ = 0;
  43.  
  44. if(SDL_Init(SDL_INIT_VIDEO) < 0)
  45. {
  46. // std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
  47. SDL_Quit();
  48. //return -1;
  49. }
  50.  
  51.  
  52. SDL_CreateWindowAndRenderer( LARGEUR_FEN, HAUTEUR_FEN, SDL_WINDOW_SHOWN , &sdlWindow, &sdlRenderer);
  53. SDL_Surface * image = SDL_LoadBMP("geii.bmp");
  54. if (image == NULL)
  55. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Image init error", SDL_GetError(),sdlWindow);
  56.  
  57. logo = SDL_CreateTextureFromSurface(sdlRenderer,image);
  58. if (logo == NULL)
  59. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Texture init error", SDL_GetError(), sdlWindow);
  60. // c'est bon on peu la libérer
  61. SDL_FreeSurface(image);
  62.  
  63.  
  64. IHM_efface();
  65. }
  66.  
  67. char IHM_quitter()
  68. {
  69. __IHM_traiter_event();
  70. return _quitter_;
  71. }
  72.  
  73. void IHM_efface()
  74. {
  75. // centre the bitmap on screen
  76. SDL_SetRenderDrawColor(sdlRenderer,255, 255, 255, 255);
  77. SDL_RenderClear(sdlRenderer);
  78. // draw logo
  79. SDL_Rect dstrect = { 0, 0, 143, 120 };
  80. SDL_RenderCopy(sdlRenderer, logo, NULL, &dstrect);
  81. }
  82.  
  83. void IHM_affiche()
  84. {
  85. // on affiche tout ce qui a été dessiné
  86. SDL_RenderPresent(sdlRenderer);
  87. }
  88.  
  89. unsigned long int IHM_couleur(unsigned char R, unsigned char V, unsigned char B)
  90. {
  91. TYPE_IHM_COUL coul;
  92. coul.rvb.R = R;
  93. coul.rvb.V = V;
  94. coul.rvb.B = B;
  95. coul.rvb.A = 255;
  96. return coul.val;
  97. }
  98.  
  99. void Affiche_Maison(int bottom, int left, unsigned int coul)
  100. {
  101. int i = 0;
  102. IHM_rectangle(left,bottom-40,40,40,coul);
  103.  
  104. for(i; i < 20; i++) {
  105. IHM_rectangle(left+i, bottom-40-i, 1, 40-2*i, coul);
  106. }
  107. }
  108.  
  109. void Affiche_Palette(int nb, unsigned int*Pal)
  110. {
  111. int i;
  112. int pas = LARGEUR_FEN/5;
  113. for(i = 0; i < nb; i++){
  114.  
  115. IHM_rectangle(i*pas, HAUTEUR_FEN-20, 20, pas, Pal[i]);
  116. }
  117. }
  118.  
  119. int getIndiceCouleur(int x, int nbcoul)
  120. {
  121.  
  122. }
  123.  
  124. void RandomPalette(int nb, float*Pal)
  125. {
  126. int i;
  127. Pal[i] = IHM_couleur(200,rand()%255,rand()%255);
  128. }
  129.  
  130. void IHM_setPixel(unsigned int x, unsigned int y, unsigned long int c)
  131. {
  132. TYPE_IHM_COUL coul;
  133. coul.val = c;
  134. SDL_SetRenderDrawColor(sdlRenderer, coul.rvb.R, coul.rvb.V, coul.rvb.B, coul.rvb.A);
  135. SDL_Rect rectangle;
  136. rectangle.x = x;
  137. rectangle.y = y;
  138. rectangle.w = 1;
  139. rectangle.h = 1;
  140. SDL_RenderFillRect(sdlRenderer, &rectangle);
  141. }
  142.  
  143. void IHM_rectangle(unsigned int x, unsigned int y, unsigned int H, unsigned int L,unsigned long int c)
  144. {
  145. TYPE_IHM_COUL coul;
  146. coul.val = c;
  147. SDL_SetRenderDrawColor(sdlRenderer, coul.rvb.R, coul.rvb.V, coul.rvb.B, coul.rvb.A);
  148. SDL_Rect rect;
  149. rect.x = x ;
  150. rect.y = y ;
  151. rect.h = H;
  152. rect.w = L;
  153. SDL_RenderFillRect(sdlRenderer, &rect);
  154. }
  155.  
  156. char IHM_getChar()
  157. {
  158. char c = _lastchar;
  159. _lastchar = 0;
  160. return c;
  161. }
  162.  
  163. int IHM_souris_x()
  164. {
  165. return _mouse_x;
  166. }
  167.  
  168. int IHM_souris_y()
  169. {
  170. return _mouse_y;
  171. }
  172.  
  173. char IHM_souris_boutonG()
  174. {
  175. char ok = _mouse_b&SDL_BUTTON(1);
  176. _mouse_b = 0;
  177. return ok;
  178. }
  179.  
  180.  
  181. void __IHM_traiter_event()
  182. {
  183.  
  184. // message processing loop
  185. SDL_Event event;
  186. while (SDL_PollEvent(&event))
  187. {
  188. // check for messages
  189. switch (event.type)
  190. {
  191. // exit if the window is closed
  192. case SDL_QUIT:
  193. _quitter_ = 1;
  194. break;
  195.  
  196. // check for keypresses
  197. case SDL_KEYDOWN:
  198. {
  199. // exit if ESCAPE is pressed
  200. if (event.key.keysym.sym == SDLK_ESCAPE)
  201. _quitter_= 1;
  202. else if(event.key.keysym.sym > SDLK_SPACE && event.key.keysym.sym < SDLK_z)
  203. {
  204. _lastchar = event.key.keysym.sym;
  205. }
  206. break;
  207. }
  208.  
  209. case SDL_MOUSEMOTION:
  210. {
  211. _mouse_x = event.motion.x;
  212. _mouse_y = event.motion.y;
  213. break;
  214. }
  215. case SDL_MOUSEBUTTONDOWN:
  216. {
  217. _mouse_b = event.motion.state;
  218. break;
  219. }
  220. } // end switch
  221. } // end of message processing
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement