Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "IHM.h"
- #include <stdlib.h>
- #include <SDL2/SDL.h>
- // variables globales nécessaire
- // la fenetre
- // Notre fenêtre et le clavier
- SDL_Window* sdlWindow;
- SDL_Renderer *sdlRenderer;
- SDL_Event evenements;
- // type pour les couleurs
- typedef struct
- {
- unsigned char R;
- unsigned char V;
- unsigned char B;
- unsigned char A;
- } TYPE_IHM_RVB;
- typedef union
- {
- TYPE_IHM_RVB rvb;
- unsigned long int val;
- } TYPE_IHM_COUL;
- // pour ne pas recharger le logo ...
- SDL_Texture * logo;
- // guestion des evenements
- char _quitter_;
- char _lastchar; // clavier
- int _mouse_x;
- int _mouse_y;
- unsigned char _mouse_b;
- void __IHM_traiter_event();
- void IHM_init()
- {
- _quitter_ = 0;
- if(SDL_Init(SDL_INIT_VIDEO) < 0)
- {
- // std::cout << "Erreur lors de l'initialisation de la SDL : " << SDL_GetError() << std::endl;
- SDL_Quit();
- //return -1;
- }
- SDL_CreateWindowAndRenderer( LARGEUR_FEN, HAUTEUR_FEN, SDL_WINDOW_SHOWN , &sdlWindow, &sdlRenderer);
- SDL_Surface * image = SDL_LoadBMP("geii.bmp");
- if (image == NULL)
- SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Image init error", SDL_GetError(),sdlWindow);
- logo = SDL_CreateTextureFromSurface(sdlRenderer,image);
- if (logo == NULL)
- SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Texture init error", SDL_GetError(), sdlWindow);
- // c'est bon on peu la libérer
- SDL_FreeSurface(image);
- IHM_efface();
- }
- char IHM_quitter()
- {
- __IHM_traiter_event();
- return _quitter_;
- }
- void IHM_efface()
- {
- // centre the bitmap on screen
- SDL_SetRenderDrawColor(sdlRenderer,255, 255, 255, 255);
- SDL_RenderClear(sdlRenderer);
- // draw logo
- SDL_Rect dstrect = { 0, 0, 143, 120 };
- SDL_RenderCopy(sdlRenderer, logo, NULL, &dstrect);
- }
- void IHM_affiche()
- {
- // on affiche tout ce qui a été dessiné
- SDL_RenderPresent(sdlRenderer);
- }
- unsigned long int IHM_couleur(unsigned char R, unsigned char V, unsigned char B)
- {
- TYPE_IHM_COUL coul;
- coul.rvb.R = R;
- coul.rvb.V = V;
- coul.rvb.B = B;
- coul.rvb.A = 255;
- return coul.val;
- }
- void Affiche_Maison(int bottom, int left, unsigned int coul)
- {
- int i = 0;
- IHM_rectangle(left,bottom-40,40,40,coul);
- for(i; i < 20; i++) {
- IHM_rectangle(left+i, bottom-40-i, 1, 40-2*i, coul);
- }
- }
- void Affiche_Palette(int nb, unsigned int*Pal)
- {
- int i;
- int pas = LARGEUR_FEN/5;
- for(i = 0; i < nb; i++){
- IHM_rectangle(i*pas, HAUTEUR_FEN-20, 20, pas, Pal[i]);
- }
- }
- int getIndiceCouleur(int x, int nbcoul)
- {
- }
- void RandomPalette(int nb, float*Pal)
- {
- int i;
- Pal[i] = IHM_couleur(200,rand()%255,rand()%255);
- }
- void IHM_setPixel(unsigned int x, unsigned int y, unsigned long int c)
- {
- TYPE_IHM_COUL coul;
- coul.val = c;
- SDL_SetRenderDrawColor(sdlRenderer, coul.rvb.R, coul.rvb.V, coul.rvb.B, coul.rvb.A);
- SDL_Rect rectangle;
- rectangle.x = x;
- rectangle.y = y;
- rectangle.w = 1;
- rectangle.h = 1;
- SDL_RenderFillRect(sdlRenderer, &rectangle);
- }
- void IHM_rectangle(unsigned int x, unsigned int y, unsigned int H, unsigned int L,unsigned long int c)
- {
- TYPE_IHM_COUL coul;
- coul.val = c;
- SDL_SetRenderDrawColor(sdlRenderer, coul.rvb.R, coul.rvb.V, coul.rvb.B, coul.rvb.A);
- SDL_Rect rect;
- rect.x = x ;
- rect.y = y ;
- rect.h = H;
- rect.w = L;
- SDL_RenderFillRect(sdlRenderer, &rect);
- }
- char IHM_getChar()
- {
- char c = _lastchar;
- _lastchar = 0;
- return c;
- }
- int IHM_souris_x()
- {
- return _mouse_x;
- }
- int IHM_souris_y()
- {
- return _mouse_y;
- }
- char IHM_souris_boutonG()
- {
- char ok = _mouse_b&SDL_BUTTON(1);
- _mouse_b = 0;
- return ok;
- }
- void __IHM_traiter_event()
- {
- // message processing loop
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- // check for messages
- switch (event.type)
- {
- // exit if the window is closed
- case SDL_QUIT:
- _quitter_ = 1;
- break;
- // check for keypresses
- case SDL_KEYDOWN:
- {
- // exit if ESCAPE is pressed
- if (event.key.keysym.sym == SDLK_ESCAPE)
- _quitter_= 1;
- else if(event.key.keysym.sym > SDLK_SPACE && event.key.keysym.sym < SDLK_z)
- {
- _lastchar = event.key.keysym.sym;
- }
- break;
- }
- case SDL_MOUSEMOTION:
- {
- _mouse_x = event.motion.x;
- _mouse_y = event.motion.y;
- break;
- }
- case SDL_MOUSEBUTTONDOWN:
- {
- _mouse_b = event.motion.state;
- break;
- }
- } // end switch
- } // end of message processing
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement