1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <sdl.h>
#include <stdlib.h>
#include <stdio.h>
#include <sdl_image.h>
int main(int argc, char* args[])
{
SDL_Surface *image = NULL, *image2 = NULL;
SDL_Texture * texture,*texture2;
bool exec = true;
int i = 0, j = 0, L = 800, H = 600, Li=32, Hi=32;
SDL_Rect Fond = { 0, 0, L, H }, img = { i, j, Li, Hi };
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *fenertre = SDL_CreateWindow("Jeu alignement", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_SWSURFACE);
SDL_Renderer * Rendu = SDL_CreateRenderer(fenertre, -1, 0);
image = IMG_Load("..//images//fond.bmp");
texture = SDL_CreateTextureFromSurface(Rendu, image);
SDL_RenderCopy(Rendu, texture, NULL, &Fond);
image2 = IMG_Load("..//images//sourir.bmp");
texture2 = SDL_CreateTextureFromSurface(Rendu, image2);
SDL_RenderCopy(Rendu, texture2, NULL, &img);
SDL_RenderPresent(Rendu);
SDL_Event event;
int CG = 0; bool mov = false;
while (exec)
{
SDL_WaitEvent(&event);
if ((event.key.keysym.sym == SDLK_ESCAPE) || (event.type == SDL_QUIT)) exec = false;
if (event.type == SDL_MOUSEBUTTONDOWN)
{
CG = 1;
if ((event.button.x >= i) && (event.button.x <= Li - i) && (event.button.y >= j) && (event.button.y <= Hi - j)) mov = true;
}
if (event.type == SDL_MOUSEBUTTONUP)
{
CG = 0; mov = false; i = event.motion.x; j = event.motion.y;
}
switch (event.key.keysym.sym)
{
case SDLK_DOWN: if (j<(H-Hi))j=j+1; break;
case SDLK_UP: if (j > 0) j=j-1; break;
case SDLK_LEFT: if (i > 0)i=i-1; break;
case SDLK_RIGHT: if (i < (L-Li))i=i+1; break;
}
switch (event.type)
{
case SDL_MOUSEMOTION:
if ((CG == 1)&&(mov == true))
{ i = event.motion.x - Li / 2; j = event.motion.y - Hi / 2;
} break;
}
//Fonction
img = { i, j, Li, Hi };
SDL_RenderCopy(Rendu, texture, NULL, &Fond);
SDL_RenderCopy(Rendu, texture2, NULL, &img);
SDL_RenderPresent(Rendu);
//fin fonction
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(Rendu);
SDL_DestroyWindow(fenertre);
SDL_FreeSurface(image);
SDL_Quit();
return EXIT_SUCCESS;
} |
Partager