#include #include #include #include "structures.h" #include "constantes.h" #include #include #include "prototypes.h" #include int main (int argc, char *argv[]) { srand (time(NULL)); SURFACES surfaces; POSITIONS positions; /* XXX argc and argv are required in some implementations of the SDL library */ (void) argc; (void) argv; Finitialisersurfaces (&surfaces); if (SDL_Init (SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Erreur lors de l'ouverture de la SDL : %s", SDL_GetError()); exit (EXIT_FAILURE); } surfaces.Pecran = SDL_SetVideoMode (XWIN,YWIN,32, SDL_HWSURFACE|SDL_DOUBLEBUF); if (surfaces.Pecran == NULL) { fprintf(stderr,"Impossible de charger la mémoire vidéo : %s", SDL_GetError()); exit (EXIT_FAILURE); } SDL_EnableUNICODE (1); TTF_Init(); SDL_WM_SetCaption ("WARCRAFT BATTLE",NULL); SDL_ShowCursor(SDL_DISABLE); if (Fchargerimages (&surfaces) < 0) { TTF_Quit(); SDL_Quit(); return -1; } Finitialiserpositions (&positions,&surfaces); Fmenuprincipal (&surfaces, &positions); //une fois le jeu quitté Fdechargerimages (&surfaces); SDL_EnableUNICODE (0); TTF_Quit(); SDL_Quit(); return 0; } void Finitialisersurfaces (SURFACES *surfaces) { int i; surfaces->Pcurseurennemis=NULL; surfaces->Pcurseurallies=NULL; surfaces->Pchoixvoleur=NULL; surfaces->Pchoixpretre=NULL; surfaces->Pchoixpaladin=NULL; surfaces->Pecran=NULL; surfaces->Pgobelin=NULL; surfaces->Ppaladin=NULL; surfaces->Ppretre=NULL; surfaces->Pvoleur=NULL; surfaces->Pmenujouer=NULL; surfaces->Pmenuoptions=NULL; surfaces->Pmenuquitter=NULL; surfaces->Pfondjeu=NULL; surfaces->Ppvpersos=NULL; surfaces->Ppmpersos=NULL; for (i=0;i<3;i++) surfaces->Tperso[i]=NULL; for (i=0;i<5;i++) surfaces->Tennemi[i]=NULL; for (i=0;i<3;i++) surfaces->Pnomactions[i]=NULL; surfaces->Ptextechoixmenu=NULL; surfaces->Pnbdegats=NULL; surfaces->Pcadrecible=NULL; surfaces->Pnomcible=NULL; surfaces->Pactive=NULL; surfaces->Pdesactive=NULL; surfaces->Ppvcible=NULL; surfaces->Ppmcible=NULL; surfaces->Pcadreactions=NULL; surfaces->Pactionselectionnee=NULL; surfaces->Pactiondesactivee=NULL; surfaces->Pmort=NULL; for (i=0;i<3;i++) surfaces->Pquantite[i]=NULL; } void Finitialiserpositions (POSITIONS *positions, SURFACES *surfaces) { int i,y=0,z=0; positions->Vpositionmenu.x = 0; positions->Vpositionmenu.y = 0; positions->Vpositionmenupretre.x = XWIN/2 - surfaces->Ppretre->w/2; positions->Vpositionmenupretre.y = YWIN - 20 - surfaces->Ppretre->h; positions->Vpositionmenupaladin.x = positions->Vpositionmenupretre.x/2 - surfaces->Ppaladin->w/2; positions->Vpositionmenupaladin.y = YWIN - 20 - surfaces->Ppaladin->h; positions->Vpositionmenuvoleur.x = XWIN/4*3 - surfaces->Pvoleur->w/2; positions->Vpositionmenuvoleur.y = YWIN - 20 - surfaces->Pvoleur->h; positions->Vpositiontextemenu.y = 362; positions->Vpositioncurseurennemis.x=1024; positions->Vpositioncurseurennemis.y=768; positions->Vpositioncurseurallies.x=1024; positions->Vpositioncurseurallies.y=768; positions->Vpositioncurseurennemis.w = surfaces->Pcurseurennemis->w; positions->Vpositioncurseurennemis.h = surfaces->Pcurseurennemis->h; positions->Vpositioncadrecible.y = 15; positions->Vpositioncadrecible.x = XWIN/2-50-surfaces->Pcadrecible->w/2; positions->Vpositioncadrecible.w = surfaces->Pcadrecible->w; positions->Vpositioncadrecible.h = surfaces->Pcadrecible->h; positions->Vpositioncadreactions.y = YWIN-surfaces->Pcadreactions->h; positions->Vpositioncadreactions.x = XWIN/2-50-surfaces->Pcadreactions->w/2; positions->Vpositioncadreactions.w = surfaces->Pcadreactions->w; positions->Vpositioncadreactions.h = surfaces->Pcadreactions->h; positions->Vpositionpvcible.x = 380; positions->Vpositionpvcible.y = 185; positions->Vpositionpmcible.x = 545; positions->Vpositionpmcible.y = 185; positions->Vpositiondegats.x=1024; positions->Vpositiondegats.y=768; for(i=0;i<3;i++) { positions->Vpositionactionselectionnee[i].x=positions->Vpositioncadreactions.x+5; positions->Vpositionactionselectionnee[i].y=positions->Vpositioncadreactions.y+5+44*i; positions->Vpositionnomactions[i].x=positions->Vpositioncadreactions.x+15; positions->Vpositionnomactions[i].y=positions->Vpositioncadreactions.y+12+44*i; positions->Vpositionquantite[i].y=positions->Vpositionnomactions[i].y; positions->Vpositionquantite[i].x=positions->Vpositioncadreactions.x+surfaces->Pcadreactions->w-45; } for (i=0;i < 16;i++) { positions->Vpositionactivedesactive[i].x = 471+51*y; positions->Vpositionactivedesactive[i].y = 101+19*z; if (y < 3) y++; else { y=0; z++; } } positions->last_cursor = (SDL_Rect){0, 0, 0, 0}; } static void colorize_surface(SDL_Surface *surf, const SDL_PixelFormat *fmt, Uint32 incr) { int buffer = 0; Uint8 r, g, b; Uint8 rincr, gincr, bincr; /* delta */ SDL_GetRGB(incr, fmt, &rincr, &gincr, &bincr); SDL_LockSurface(surf); for (int y = 0; y < surf->h; ++y) { for (int x = 0; x < surf->w; ++x) { Uint32 pixel = obtenirPixel(surf, x, y); SDL_GetRGB(pixel, fmt, &r, &g, &b); /* red increment */ buffer = (int)r + rincr; if (buffer > 0xff) buffer = 0xff; else if (buffer < 0x0) buffer = 0; r = buffer; /* green increment */ buffer = (int)g + gincr; if (buffer > 0xff) buffer = 0xff; else if (buffer < 0x0) buffer = 0; g = buffer; /* blue increment */ buffer = (int)b + bincr; if (buffer > 0xff) buffer = 0xff; else if (buffer < 0x0) buffer = 0; b = buffer; pixel = SDL_MapRGB(fmt, r, g, b); definirPixel(surf, x, y, pixel); } } SDL_UnlockSurface(surf); SDL_SetColorKey(surf, SDL_SRCCOLORKEY, SDL_MapRGB(fmt, r, g, b)); } static void colorize_every_red_surface(SURFACES *surfaces) { const SDL_PixelFormat *fmt = surfaces->Pecran->format; Uint32 red = SDL_MapRGB(fmt, 30, 0, 0); colorize_surface(surfaces->red_paladin, fmt, red); colorize_surface(surfaces->red_priest, fmt, red); colorize_surface(surfaces->red_thief, fmt, red); } int Fchargerimages (SURFACES *surfaces) { #define LOAD_IMAGE(_name, _path) \ if ((surfaces->_name = IMG_Load(_path)) == NULL) { \ fprintf(stderr, "Impossible d'ouvrir " _path); \ return -1; \ } int i; LOAD_IMAGE(Pgobelin, "images/gobelin.bmp"); LOAD_IMAGE(red_warrior_gobelin, "images/gobelin.bmp"); LOAD_IMAGE(Ppaladin, "images/paladin.bmp"); LOAD_IMAGE(Ppretre, "images/pretre.bmp"); LOAD_IMAGE(Pvoleur, "images/voleur.bmp"); LOAD_IMAGE(red_thief, "images/voleur.bmp"); LOAD_IMAGE(red_paladin, "images/paladin.bmp"); LOAD_IMAGE(red_priest, "images/pretre.bmp"); LOAD_IMAGE(Pmenujouer, "images/menu_principal/jouer.bmp"); LOAD_IMAGE(Pmenuoptions, "images/menu_principal/options.bmp"); LOAD_IMAGE(Pmenuquitter, "images/menu_principal/quitter.bmp"); LOAD_IMAGE(Pchoixvoleur, "images/menu_choix_classe/voleur.bmp"); LOAD_IMAGE(Pchoixpretre, "images/menu_choix_classe/pretre.bmp"); LOAD_IMAGE(Pchoixpaladin, "images/menu_choix_classe/paladin.bmp"); LOAD_IMAGE(Pcurseurennemis, "images/jeu/curseur.bmp"); LOAD_IMAGE(Pcurseurallies, "images/jeu/curseurpersos.bmp"); LOAD_IMAGE(Pcadrecible, "images/jeu/cadre_cible.bmp"); LOAD_IMAGE(Pactive, "images/jeu/active.bmp"); LOAD_IMAGE(Pdesactive, "images/jeu/desactive.bmp"); LOAD_IMAGE(Pcadreactions, "images/jeu/cadre_actions.bmp"); LOAD_IMAGE(Pactionselectionnee, "images/jeu/cadre_actions_select.bmp"); LOAD_IMAGE(Pactiondesactivee, "images/jeu/cadre_actions_unactive.bmp"); LOAD_IMAGE(Pmort, "images/jeu/mort.bmp"); for (i = 0;i < 5;i++) surfaces->Tennemi[i] = surfaces->Pgobelin; #undef LOAD_IMAGE //définition de la transparence alpha dans les surfaces SDL_SetColorKey(surfaces->Pgobelin,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,0,255,18)); SDL_SetColorKey(surfaces->red_warrior_gobelin, SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,0,255,18)); SDL_SetColorKey(surfaces->Ppretre,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,39,189,31)); SDL_SetColorKey(surfaces->Ppaladin,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,189,31,31)); SDL_SetColorKey(surfaces->Pvoleur,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,39,189,31)); SDL_SetColorKey(surfaces->red_priest,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,39,189,31)); SDL_SetColorKey(surfaces->red_paladin,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,189,31,31)); SDL_SetColorKey(surfaces->red_thief,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,39,189,31)); SDL_SetColorKey(surfaces->Pcurseurennemis,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pcurseurallies,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pactive,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pdesactive,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pmort,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); colorize_every_red_surface(surfaces); return 0; } void Fdechargerimages (SURFACES *surfaces)//dechargement des surfaces alouées { int i; SDL_FreeSurface (surfaces->Pgobelin); SDL_FreeSurface(surfaces->red_warrior_gobelin); SDL_FreeSurface (surfaces->Ppaladin); SDL_FreeSurface (surfaces->Ppretre); SDL_FreeSurface (surfaces->Pvoleur); SDL_FreeSurface(surfaces->red_paladin); SDL_FreeSurface(surfaces->red_priest); SDL_FreeSurface(surfaces->red_thief); SDL_FreeSurface (surfaces->Pmenujouer); SDL_FreeSurface (surfaces->Pmenuoptions); SDL_FreeSurface (surfaces->Pmenuquitter); SDL_FreeSurface (surfaces->Pchoixpretre); SDL_FreeSurface (surfaces->Pchoixpaladin); SDL_FreeSurface (surfaces->Pchoixvoleur); SDL_FreeSurface (surfaces->Pcurseurennemis); SDL_FreeSurface (surfaces->Pcurseurallies); SDL_FreeSurface (surfaces->Pcadrecible); SDL_FreeSurface (surfaces->Pactive); SDL_FreeSurface (surfaces->Pdesactive); SDL_FreeSurface (surfaces->Pcadreactions); SDL_FreeSurface (surfaces->Pactionselectionnee); SDL_FreeSurface (surfaces->Pactiondesactivee); SDL_FreeSurface (surfaces->Pmort); SDL_FreeSurface (surfaces->Pfondjeu); SDL_FreeSurface(surfaces->Ptextechoixmenu); SDL_FreeSurface(surfaces->Pnbdegats); SDL_FreeSurface(surfaces->Pnomcible); SDL_FreeSurface(surfaces->Ppvcible); SDL_FreeSurface(surfaces->Ppmcible); SDL_FreeSurface(surfaces->Ppvpersos); SDL_FreeSurface(surfaces->Ppmpersos); for(i=0;i<3;i++) { SDL_FreeSurface (surfaces->Pnomactions[i]); } for(i=0;i<3;i++) { SDL_FreeSurface (surfaces->Pquantite[i]); } } /* ********************************************************************* */ /*obtenirPixel : permet de récupérer la couleur d'un pixel Paramètres d'entrée/sortie : SDL_Surface *surface : la surface sur laquelle on va récupérer la couleur d'un pixel int x : la coordonnée en x du pixel à récupérer int y : la coordonnée en y du pixel à récupérer Uint32 resultat : la fonction renvoie le pixel aux coordonnées (x,y) dans la surface */ Uint32 obtenirPixel(SDL_Surface *surface, int x, int y) { /*nbOctetsParPixel représente le nombre d'octets utilisés pour stocker un pixel. En multipliant ce nombre d'octets par 8 (un octet = 8 bits), on obtient la profondeur de couleur de l'image : 8, 16, 24 ou 32 bits.*/ int nbOctetsParPixel = surface->format->BytesPerPixel; /* Ici p est l'adresse du pixel que l'on veut connaitre */ /*surface->pixels contient l'adresse du premier pixel de l'image*/ Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel; /*Gestion différente suivant le nombre d'octets par pixel de l'image*/ switch(nbOctetsParPixel) { case 1: return *p; case 2: return *(Uint16 *)p; case 3: /*Suivant l'architecture de la machine*/ if(SDL_BYTEORDER == SDL_BIG_ENDIAN) return p[0] << 16 | p[1] << 8 | p[2]; else return p[0] | p[1] << 8 | p[2] << 16; case 4: return *(Uint32 *)p; /*Ne devrait pas arriver, mais évite les erreurs*/ default: return 0; } } /* ********************************************************************* */ /*definirPixel : permet de modifier la couleur d'un pixel Paramètres d'entrée/sortie : SDL_Surface *surface : la surface sur laquelle on va modifier la couleur d'un pixel int x : la coordonnée en x du pixel à modifier int y : la coordonnée en y du pixel à modifier Uint32 pixel : le pixel à insérer */ void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel) { /*nbOctetsParPixel représente le nombre d'octets utilisés pour stocker un pixel. En multipliant ce nombre d'octets par 8 (un octet = 8 bits), on obtient la profondeur de couleur de l'image : 8, 16, 24 ou 32 bits.*/ int nbOctetsParPixel = surface->format->BytesPerPixel; /*Ici p est l'adresse du pixel que l'on veut modifier*/ /*surface->pixels contient l'adresse du premier pixel de l'image*/ Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * nbOctetsParPixel; /*Gestion différente suivant le nombre d'octets par pixel de l'image*/ switch(nbOctetsParPixel) { case 1: *p = pixel; break; case 2: *(Uint16 *)p = pixel; break; case 3: /*Suivant l'architecture de la machine*/ if(SDL_BYTEORDER == SDL_BIG_ENDIAN) { p[0] = (pixel >> 16) & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = pixel & 0xff; } else { p[0] = pixel & 0xff; p[1] = (pixel >> 8) & 0xff; p[2] = (pixel >> 16) & 0xff; } break; case 4: *(Uint32 *)p = pixel; break; } }