sdl bouton code example

Example: sdl bouton

#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

int creabouton(int positionx, int positiony, int w, int h, SDL_Surface* ecran, int couleura, int couleurb, int couleurc)
{
    int continuer = 1, i = 0, resultat = 0, j = 0, resultat2 = 0, j2 = 0;
    SDL_Event event;
    SDL_Surface *bouton = NULL, *degrader = NULL;
    SDL_Rect position, position2;
    position.x = positionx;
    position.y = positiony;
    position2.x = positionx;
    position2.y = positiony;

    bouton = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32, 0, 0, 0, 0);
    SDL_FillRect(bouton, NULL,  SDL_MapRGB(ecran->format, couleura, couleurb, couleurc));
    SDL_BlitSurface(bouton, NULL, ecran, &position);
    resultat2 = (4*h)/100;
    resultat = (4*w)/100;
    for(i = 0; i < resultat || i < resultat2; i++)
    {
      if(i < resultat)
      {
      degrader = SDL_CreateRGBSurface(SDL_HWSURFACE, 1, h, 32, 0, 0, 0,0);
      SDL_FillRect(degrader, NULL, SDL_MapRGB(bouton->format, j, j, j));
      SDL_BlitSurface(degrader, NULL, ecran, &position);
      position.x++;
      j += 255/resultat;
      }
      if(i < resultat2)
      {
      degrader = SDL_CreateRGBSurface(SDL_HWSURFACE, w, 1, 32, 0, 0, 0,0);
      SDL_FillRect(degrader, NULL, SDL_MapRGB(bouton->format, j2, j2, j2));
      SDL_BlitSurface(degrader, NULL, ecran, &position2);
      position2.y++;
      j2 += 255/resultat2;
      }
    }

    SDL_Flip(ecran);
    while(continuer)
    {
     SDL_WaitEvent(&event);
     switch(event.type)
     {
     case SDL_QUIT:
     continuer = 0;
     break;
     case SDL_MOUSEBUTTONDOWN:
     if(event.button.x >= positionx && event.button.x <= (positionx + w) && event.button.y >= positiony && event.button.y <= (positiony + h))
     {
     return 1;
     continuer = 0;
     }
     break;
     }
    }
}

int main ( int argc, char** argv )
{
   int fenetre = 0;
   SDL_Surface *ecran = NULL;
   SDL_Init(SDL_INIT_VIDEO);
   SDL_Event event;
   ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
   SDL_WM_SetIcon(SDL_LoadBMP("icon.bmp"), NULL);
   SDL_WM_SetCaption("Fenetre avec bouton a martin", NULL);
   SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
   fenetre = creabouton(200, 300, 210, 100, ecran, 0, 0, 0);
   SDL_Flip(ecran);

      if(fenetre == 1)
   {
   fenetre = 0;
   SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 033, 50));
   fenetre = creabouton(640/2, 480/2, 40, 40, ecran, 25, 250, 0);
   SDL_Flip(ecran);
   }
   if(fenetre == 1)
   {
    fenetre = 0;
    fenetre = creabouton(0, 0, 400, 360, ecran, 255, 0, 255);
   }

   SDL_Quit();
}

Tags:

Misc Example