Tommy Jean Guest
|
Posted: Tue Mar 08, 2005 8:25 pm Post subject: Jeux en sdl |
|
|
BOnjour,
Je suis entrain d'essayer de faire un Juez sous win avec la SDL. J'ai
quelques difficulté de logique pour la SDL. Mon probleme c'est mon
personnage qui ce déplace pas. ET j'aimerais que vous examiner le code
suivant et apporter des amilioration si besoins s.v.p
/////////////////////////
//* Include Lib //
////////////////////////
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//////////////////////////////////////
//* Variable global et pointeur //
/////////////////////////////////////
int EXIT = 0; //Controle la fin du programme
int show; //Étape du jeux en cours
int mouseX, mouseY; //Variables de souris
int x;
int y;
int s;
SDL_Surface* screen;
SDL_Surface* fond;
SDL_Surface* perso;
SDL_Rect fond1, perso1;
///////////////////////////////////
//* Déclaration des fonction //
//////////////////////////////////
void init_sdl(void);
void LoadImage();
void game(void);
void Rect ();
//##################################################################//
//## Bloque principal ##//
//##################################################################/
int main(int argc, char *argv[])
{
init_sdl();
SDL_GetMouseState(&mouseX, &mouseY);
while (!EXIT)
{
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
//Touche pressée
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
SDL_FreeSurface(screen);
EXIT=1;
break;
//Selement ici quw wj fait le déplacement.
case SDLK_LEFT:
fond1.x = +10;
fond1.y = fond1.y;
fond1.w = fond1.w;
fond1.h = fond1.h;
SDL_BlitSurface(fond,NULL,screen,&fond1); // colle
l'image sur l'écran, a la position rect1, donc surtout a x pour la
coordonnée x.
SDL_Flip(screen); // affiche la prochaine fr
break;
case SDLK_RIGHT:
SDL_FreeSurface(screen);
//persox=persox - 10;
break;
case SDLK_UP:
SDL_FreeSurface(screen);
//persoy = persoy + 10;
SDL_UpdateRect(screen, 0, 0, 0, 0);
break;
case SDLK_DOWN:
SDL_FreeSurface(screen);
//persoy = persoy - 10;
break;
case SDLK_RETURN:
//show=2;
break;
default:break;
}
break;
case SDL_MOUSEMOTION:
break;
//exit
case SDL_QUIT:
EXIT=1;
break;
}
game();
}//fin boucle while
}
//##################################################################//
//## Function : Rect ##//
//##################################################################//
SDL_Rect Rect(int x,int y,int w,int h) // pour faire un rectangle
plus vite
{
SDL_Rect r;
r.x=x;
r.y=y;
r.w=w;
r.h=h;
return r;
}
//##################################################################//
//## Function Sdl_init ##//
//##################################################################/
void init_sdl()
{
//Init a la sdl
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Impossible d'initialiser SDL: %sn",
SDL_GetError());
exit(1);
}
//quitter proprement
atexit(SDL_Quit);
//Création de la fenetre sdl
screen = SDL_SetVideoMode(800, 600, 32,
SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
if ( screen == NULL ) {
fprintf(stderr, "Impossible de crée la fenetre de jeux: %sn",
SDL_GetError());
exit(1);
}
//titre de la fenetre sdl
SDL_WM_SetCaption ("test", NULL);
}
//##################################################################//
//## Function : Loadimage ##//
//##################################################################//
SDL_Surface* LoadImage(char* fichier,int vram=1) // 3e forme
{
SDL_Surface* f = IMG_Load(fichier); // charge l'image dans f en RAM
SDL_Surface* r=NULL;
if (vram)
r=SDL_CreateRGBSurface(SDL_HWSURFACE, f->w, f->h, 32, 0, 0, 0,
0);// cree une image en VRAM
if (r==NULL) vram=0; // Si plus de place en VRAM, alors r= NULL.
if (!vram)
r=SDL_CreateRGBSurface(SDL_SWSURFACE, f->w, f->h, 32, 0, 0, 0, 0);
// cree une image en VRAM
SDL_Rect R=Rect(0,0,f->w,f->h);
SDL_BlitSurface(f,NULL,r,&R); // copie l'image f de la RAM vers
firstscreen en VRAM
SDL_FreeSurface(f); // supprime la surface f : inutile
maintenant --> libere la mémoire
return r;
}
//##################################################################//
//## Function : Game ##//
//##################################################################//
void game(void) {
switch (show)
{
//exit
case(1):
EXIT=1;
break;
//Menu
default:
SDL_Rect fond1, perso1;
//Affiche le fond
fond1.x = 0;
fond1.y = 0;
fond1.w = 1024;
fond1.h = 768;
//affiche le perso
perso1.x = 400;
perso1.y = 300;
perso1.w = 1024;
perso1.h = 768;
SDL_Surface* fond;
SDL_Surface* perso;
fond = LoadImage("./IMG/menu/menu.jpg");
perso = LoadImage("./IMG/menu/perso.jpg");
SDL_BlitSurface(fond,NULL,screen,&fond1); // colle l'image sur
l'écran, NULL car on affiche TOUTE l'image
SDL_BlitSurface(perso,NULL,screen,&perso1);
SDL_Flip(screen);
break;
}
}
|
|