diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-10 19:47:32 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-10 20:01:52 +0100 |
commit | f508e7191bd2f3eb7616e315e7e0d4a4b94c7914 (patch) | |
tree | 7bc1ed6498b3e2b7393ce791667bb9b50037e5fd | |
parent | 5ab5f72ba931e1d29a18a6b42e2cc2d73e291c4e (diff) |
cleaned the initialization / shutdown a little
The initialized surfaces are converted to display format so that they
are ready for fastblit.
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
-rw-r--r-- | blits.c | 1 | ||||
-rw-r--r-- | prototypes.h | 5 | ||||
-rw-r--r-- | rpg.c (renamed from main.c) | 371 | ||||
-rw-r--r-- | rpg.h | 21 | ||||
-rw-r--r-- | structures.h | 2 |
5 files changed, 227 insertions, 173 deletions
@@ -2,7 +2,6 @@ #include <stdio.h> #include <SDL/SDL.h> #include <SDL/SDL_ttf.h> -#include <SDL/SDL_image.h> #include <string.h> #include "constantes.h" diff --git a/prototypes.h b/prototypes.h index 698bc46..30baa57 100644 --- a/prototypes.h +++ b/prototypes.h @@ -12,11 +12,6 @@ void Fmenuchoixpersos (SURFACES *surfaces, POSITIONS *positions); void Fchangersurlignage2 (int Vperso, SURFACES *surfaces, POSITIONS *positions); int Fjouer(SURFACES *surfaces, POSITIONS *positions, struct team_t *ally, struct team_t *enemy); -void Finitialisersurfaces (SURFACES *surfaces); -int Fchargerimages (SURFACES *surfaces); -void Fdechargerimages (SURFACES *surfaces); -void Finitialiserpositions (POSITIONS *positions, SURFACES *surfaces); - Uint32 obtenirPixel(SDL_Surface *surface, int x, int y); void definirPixel(SDL_Surface *surface, int x, int y, Uint32 pixel); void Fgetmap (int map[][11]); @@ -8,149 +8,9 @@ #include "prototypes.h" #include <time.h> -int main (int argc, char *argv[]) -{ - srand (time(NULL)); - SURFACES surfaces; - POSITIONS positions; +#include "rpg.h" - /* 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.screen = 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}; -} +struct rpg_t rpg_g; static void colorize_surface(SDL_Surface *surf, const SDL_PixelFormat *fmt, Uint32 incr) { @@ -207,7 +67,7 @@ static void colorize_every_red_surface(SURFACES *surfaces) colorize_surface(surfaces->red_warrior_gobelin, fmt, red); } -int Fchargerimages (SURFACES *surfaces) +static int rpg_load_images(SURFACES *surfaces) { #define LOAD_IMAGE(_name, _path) \ if ((surfaces->_name = IMG_Load(_path)) == NULL) { \ @@ -215,8 +75,6 @@ int Fchargerimages (SURFACES *surfaces) return -1; \ } - int i; - LOAD_IMAGE(Pgobelin, "images/gobelin.bmp"); LOAD_IMAGE(red_warrior_gobelin, "images/gobelin.bmp"); LOAD_IMAGE(Ppaladin, "images/paladin.bmp"); @@ -240,37 +98,193 @@ int Fchargerimages (SURFACES *surfaces) 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; - LOAD_IMAGE(background, "images/jeu/textures/background.jpg"); - surfaces->Pfondjeu = surfaces->background; #undef LOAD_IMAGE + return 0; +} + +static int rpg_initialize_sprites(void) +{ +#define set_color_key(surf_, r_, g_, b_) \ + SDL_SetColorKey((surf_), SDL_SRCCOLORKEY, SDL_MapRGB(rpg_g.surfaces.screen->format, (r_), (g_), (b_))) + +#define load_sprite(surf_) \ + tmp = SDL_DisplayFormat((surf_)); \ + if (tmp == NULL) return -1; \ + SDL_FreeSurface((surf_)); \ + (surf_) = tmp; + + SDL_Surface *tmp; + + set_color_key(rpg_g.surfaces.Pgobelin, 0, 255, 18); + set_color_key(rpg_g.surfaces.Ppretre, 39, 189, 31); + set_color_key(rpg_g.surfaces.Ppaladin, 189, 31, 31); + set_color_key(rpg_g.surfaces.Pvoleur, 39, 189, 31); + set_color_key(rpg_g.surfaces.red_priest, 39, 189, 31); + set_color_key(rpg_g.surfaces.red_paladin, 189, 31, 31); + set_color_key(rpg_g.surfaces.red_thief, 39, 189, 31); + + set_color_key(rpg_g.surfaces.Pcurseurennemis, 255, 255, 255); + set_color_key(rpg_g.surfaces.Pcurseurallies, 255, 255, 255); + set_color_key(rpg_g.surfaces.Pactive, 255, 255, 255); + set_color_key(rpg_g.surfaces.Pdesactive, 255, 255, 255); + set_color_key(rpg_g.surfaces.Pmort, 255, 255, 255); + + load_sprite(rpg_g.surfaces.Pgobelin); + load_sprite(rpg_g.surfaces.Ppretre); + load_sprite(rpg_g.surfaces.Ppaladin); + load_sprite(rpg_g.surfaces.Pvoleur); + + load_sprite(rpg_g.surfaces.Pmenuoptions); + load_sprite(rpg_g.surfaces.Pmenujouer); + load_sprite(rpg_g.surfaces.Pmenuquitter); + + load_sprite(rpg_g.surfaces.Pchoixpaladin); + load_sprite(rpg_g.surfaces.Pchoixpretre); + load_sprite(rpg_g.surfaces.Pchoixvoleur); + load_sprite(rpg_g.surfaces.Pcurseurallies); + load_sprite(rpg_g.surfaces.Pcurseurennemis); + + load_sprite(rpg_g.surfaces.Pcadrecible); + load_sprite(rpg_g.surfaces.Pactive); + load_sprite(rpg_g.surfaces.Pdesactive); + load_sprite(rpg_g.surfaces.Pcadreactions); + load_sprite(rpg_g.surfaces.Pactionselectionnee); + load_sprite(rpg_g.surfaces.Pactiondesactivee); + load_sprite(rpg_g.surfaces.Pmort); + + load_sprite(rpg_g.surfaces.background); + rpg_g.surfaces.Pfondjeu = rpg_g.surfaces.background; + + rpg_g.surfaces.red_warrior_gobelin = SDL_DisplayFormat(rpg_g.surfaces.Pgobelin); + if (rpg_g.surfaces.red_warrior_gobelin == NULL) + return -1; + + rpg_g.surfaces.red_priest = SDL_DisplayFormat(rpg_g.surfaces.Ppretre); + if (rpg_g.surfaces.red_priest == NULL) + return -1; + + rpg_g.surfaces.red_thief = SDL_DisplayFormat(rpg_g.surfaces.Pvoleur); + if (rpg_g.surfaces.red_thief == NULL) + return -1; + + colorize_every_red_surface(&rpg_g.surfaces); + +#undef load_sprite +#undef set_color_key + return 0; +} - //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)); +static void Finitialiserpositions (POSITIONS *positions, const 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 int rpg_init(void) +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + fprintf(stderr, "SDL_Init: %s\n", SDL_GetError()); + return -1; + } + + /* avoid the need of closing the SDL library manually */ + atexit(SDL_Quit); + + if (TTF_Init() < 0) { + fprintf(stderr, "TTF_Init: %s\n", TTF_GetError()); + return -1; + } + + /* avoid the need of closing the SDL_ttf library manually */ + atexit(TTF_Quit); + + rpg_g.font = TTF_OpenFont("fonts/default.ttf", 20); + if (rpg_g.font == NULL) { + fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError()); + return -1; + } + + if (rpg_load_images(&rpg_g.surfaces) < 0) { + return -1; + } + + rpg_g.screen = SDL_SetVideoMode(XWIN, YWIN, 0, SDL_HWSURFACE | SDL_DOUBLEBUF); + + if (rpg_g.screen == NULL) { + fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError()); + return -1; + } + + /* set the deprecated pointers to screen */ + rpg_g.surfaces.screen = rpg_g.surfaces.Pecran = rpg_g.screen; + + rpg_initialize_sprites(); - 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)); + Finitialiserpositions(&rpg_g.positions, &rpg_g.surfaces); - 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)); + /* XXX not sure this is useful */ + SDL_EnableUNICODE (1); - colorize_every_red_surface(surfaces); + SDL_WM_SetCaption ("RPG", NULL); + SDL_ShowCursor(SDL_DISABLE); return 0; } -void Fdechargerimages (SURFACES *surfaces)//dechargement des surfaces alouées +static void Fdechargerimages(SURFACES *surfaces) { int i; @@ -319,6 +333,33 @@ void Fdechargerimages (SURFACES *surfaces)//dechargement des surfaces alouées } } +static void rpg_shutdown(void) +{ + Fdechargerimages(&rpg_g.surfaces); + + if (TTF_WasInit()) { + TTF_CloseFont(rpg_g.font); + } +} + +int main (int argc, char *argv[]) +{ + /* XXX argc and argv are required in some implementations of the SDL library */ + (void) argc; + (void) argv; + + srand(time(NULL)); + + if (rpg_init() >= 0) { + Fmenuprincipal(&rpg_g.surfaces, &rpg_g.positions); + } + + rpg_shutdown(); + + return 0; +} + + /* ********************************************************************* */ /*obtenirPixel : permet de récupérer la couleur d'un pixel Paramètres d'entrée/sortie : @@ -0,0 +1,21 @@ +#ifndef RPG_H +#define RPG_H + +#include <SDL/SDL.h> +#include <SDL/SDL_ttf.h> + +#include "structures.h" + +struct rpg_t { + SDL_Surface *screen; + + TTF_Font *font; + + /* XXX those are deprecated */ + SURFACES surfaces; + POSITIONS positions; +}; + +extern struct rpg_t rpg_g; + +#endif /* RPG_H */ diff --git a/structures.h b/structures.h index f3cb306..2c5710e 100644 --- a/structures.h +++ b/structures.h @@ -32,8 +32,6 @@ struct surfaces SDL_Surface *Pchoixpretre; SDL_Surface *Pchoixvoleur; - SDL_Surface *Tperso[3]; - SDL_Surface *Tennemi[5]; SDL_Surface *Ptextechoixmenu; SDL_Surface *Pcurseurennemis; SDL_Surface *Pcurseurallies; |