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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <stdio.h>
#include <stdlib.h>
#include <string>
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;
SDL_Thread *thread= NULL;
int xpos=0,ypos=0;
int done=0;
//initialisation des images
int InitImages()
{
back = SDL_LoadBMP("bleu.bmp");
image=SDL_LoadBMP("loup5.bmp");
Uint32 colorkey = SDL_MapRGB( image->format,0,0xFF,0xFF);
SDL_SetColorKey( image, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey);
return 0;
}
//fonctions d'affichage
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}
//dessin du BG
void DrawBG()
{
DrawIMG(back, 0, 0);
}
//fonction de dessin
void DrawScene()
{
DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2);
DrawIMG(image, xpos, ypos);
SDL_Flip(screen);
}
//le thread
int my_thread( void *data )
{
//on dessine la scene
while(done == 0)
{
DrawScene();
}
return 0;
}
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO);
atexit(SDL_Quit);
screen=SDL_SetVideoMode(1024,768,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
InitImages();
DrawBG();
//on crée le thread
thread = SDL_CreateThread( my_thread, NULL );
Uint8* keys;
//boucle qui gere les events clavier
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { ypos -= 1; }
if ( keys[SDLK_DOWN] ) { ypos += 1; }
if ( keys[SDLK_LEFT] ) { xpos -= 1; }
if ( keys[SDLK_RIGHT] ) { xpos += 1; }
}
return 0;
} |
Partager