From 1e8c0804c31371af6ac113777efd95d5ee8a2927 Mon Sep 17 00:00:00 2001 From: Martin CHAUVELIERE Date: Thu, 11 May 2023 15:47:23 +0200 Subject: [PATCH] Derniere correction, à finir demain --- Space Invader/Envahisseurs/Graphique/Lutins/Coeur.bmp | Bin 0 -> 2638 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_bombe.bmp | Bin 0 -> 1042 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_bouclier.bmp | Bin 0 -> 7514 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_canon.bmp | Bin 0 -> 3818 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_canon_ferraille.bmp | Bin 0 -> 4610 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_missile.bmp | Bin 0 -> 510 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_1.bmp | Bin 0 -> 3422 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_2.bmp | Bin 0 -> 3422 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_1.bmp | Bin 0 -> 3950 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_2.bmp | Bin 0 -> 3950 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_1.bmp | Bin 0 -> 3950 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_2.bmp | Bin 0 -> 3950 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre_bouillie.bmp | Bin 0 -> 4610 bytes Space Invader/Envahisseurs/Graphique/Lutins/invader_ovni.bmp | Bin 0 -> 4826 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_champi.bmp | Bin 0 -> 5738 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_bas.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_droite.bmp | Bin 0 -> 6890 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_gauche.bmp | Bin 0 -> 6890 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_haut.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_bas.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_droite.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_gauche.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_haut.bmp | Bin 0 -> 6906 bytes Space Invader/Envahisseurs/Graphique/src/Graphique/Makefile | 29 +++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.c | 239 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.h | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.c | 361 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.h | 31 +++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Main/Makefile | 34 ++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Main/init.c | 234 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Main/init.h | 30 ++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Main/main.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.h | 15 +++++++++++++++ 36 files changed, 1508 insertions(+), 0 deletions(-) create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/Coeur.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_bombe.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_bouclier.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_canon.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_canon_ferraille.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_missile.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_1.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_2.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_1.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_2.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_1.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_2.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre_bouillie.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/invader_ovni.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_champi.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_bas.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_droite.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_gauche.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_haut.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_bas.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_droite.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_gauche.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_haut.bmp create mode 100644 Space Invader/Envahisseurs/Graphique/src/Graphique/Makefile create mode 100644 Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.h create mode 100644 Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.h create mode 100644 Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.h create mode 100644 Space Invader/Envahisseurs/Graphique/src/Main/Makefile create mode 100644 Space Invader/Envahisseurs/Graphique/src/Main/init.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/Main/init.h create mode 100644 Space Invader/Envahisseurs/Graphique/src/Main/main.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.c create mode 100644 Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.h diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/Coeur.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/Coeur.bmp new file mode 100644 index 0000000..823e8a0 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/Coeur.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_bombe.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_bombe.bmp new file mode 100644 index 0000000..0b3e39b Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_bombe.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_bouclier.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_bouclier.bmp new file mode 100644 index 0000000..f71e3f3 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_bouclier.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon.bmp new file mode 100644 index 0000000..2b450dd Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon_ferraille.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon_ferraille.bmp new file mode 100644 index 0000000..575ec72 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_canon_ferraille.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_missile.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_missile.bmp new file mode 100644 index 0000000..592caa4 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_missile.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_1.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_1.bmp new file mode 100644 index 0000000..cf56472 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_1.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_2.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_2.bmp new file mode 100644 index 0000000..cf56472 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre1_2.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_1.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_1.bmp new file mode 100644 index 0000000..40bbf1f Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_1.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_2.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_2.bmp new file mode 100644 index 0000000..f99ec39 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre2_2.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_1.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_1.bmp new file mode 100644 index 0000000..a17556a Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_1.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_2.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_2.bmp new file mode 100644 index 0000000..a17556a Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre3_2.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre_bouillie.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre_bouillie.bmp new file mode 100644 index 0000000..fa89282 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_monstre_bouillie.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/invader_ovni.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/invader_ovni.bmp new file mode 100644 index 0000000..da5fbd8 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/invader_ovni.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_champi.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_champi.bmp new file mode 100644 index 0000000..6126777 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_champi.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_bas.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_bas.bmp new file mode 100644 index 0000000..ed4f660 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_bas.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_droite.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_droite.bmp new file mode 100644 index 0000000..36b049d Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_droite.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_gauche.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_gauche.bmp new file mode 100644 index 0000000..a67092c Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_gauche.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_haut.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_haut.bmp new file mode 100644 index 0000000..92dd3c7 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_corps_haut.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_bas.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_bas.bmp new file mode 100644 index 0000000..c582ab3 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_bas.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_droite.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_droite.bmp new file mode 100644 index 0000000..7bce015 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_droite.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_gauche.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_gauche.bmp new file mode 100644 index 0000000..f94c70b Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_gauche.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_haut.bmp b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_haut.bmp new file mode 100644 index 0000000..c74c0c1 Binary files /dev/null and b/Space Invader/Envahisseurs/Graphique/Lutins/millepatte_tete_haut.bmp differ diff --git a/Space Invader/Envahisseurs/Graphique/src/Graphique/Makefile b/Space Invader/Envahisseurs/Graphique/src/Graphique/Makefile new file mode 100644 index 0000000..5791d72 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Graphique/Makefile @@ -0,0 +1,29 @@ +# +# Makefile pour la bibliotheque graphique +# + +SOURCES = $(wildcard *.c) +OBJETS = $(SOURCES:.c=.o) +CIBLE = libgraph.a + +# +# Nom de la cible principale +# + +all: $(CIBLE) + +# +# Cible de nettoyage +# + +clean: + rm -f core *.o $(CIBLE) + +# +# Dependances pour la bibliotheque +# + +$(CIBLE): $(OBJETS) + $(AR) rs $@ $? + +$(CIBLE:.a=).o: $(CIBLE:.a=).c $(CIBLE:.a=).h diff --git a/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.c b/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.c new file mode 100644 index 0000000..99658e1 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.c @@ -0,0 +1,239 @@ +/**** Bibliotheque graphique ****/ + +/** Fichiers d'inclusion **/ + +#include +#include +#include "libgraph.h" + +/** Types **/ + +typedef struct +{ + int r, v, b; +} couleur; + +/** Constantes **/ + +#define BITS_PAR_PIXEL 32 +#define TAILLE_POLICE 20 + +static const couleur couleurs[] = { { 255, 255, 255 }, { 0, 0, 0 }, { 255, 0, 0 }, + { 0, 255, 0 }, { 0, 0, 255 }, { 255, 105, 180 }, + { 150, 150, 150 }, { -1, -1, -1 } }; + +static const char policeDefaut[]="/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + +/** Variables globales **/ + +static SDL_Surface *surface; +TTF_Font* police; + +/** Fonctions **/ + +void choisirPolice(const char *chemin, int taille){ + police=TTF_OpenFont(chemin, taille); +} + +void initialiserTexte() { + TTF_Init(); + choisirPolice(policeDefaut, TAILLE_POLICE); +} + +/* Initialisation de la surface dessinable */ +unsigned char creerSurface (int largeur, int hauteur, char *titre) +{ + SDL_Init (SDL_INIT_VIDEO); + SDL_WM_SetCaption (titre, titre); + surface = SDL_SetVideoMode (largeur, hauteur, BITS_PAR_PIXEL, SDL_DOUBLEBUF); + initialiserTexte(); + + return (surface != NULL && police != NULL); +} + +/* Fermeture de la surface dessinable */ + +void fermerSurface (void) +{ + if (surface != NULL) SDL_FreeSurface (surface); + if (police != NULL) TTF_CloseFont(police); + TTF_Quit(); + SDL_Quit (); +} + +/* Creation d'une couleur */ + +static int creerCouleur (int ncouleur) +{ + couleur c = couleurs[ncouleur]; + return SDL_MapRGB (surface->format, c.r, c.v, c.b); +} + +/* Dessin d'un rectangle plein */ + +void rectanglePlein (int x, int y, int l, int h, int c) +{ + SDL_Rect rectangle = { x, y, l, h }; + SDL_FillRect (surface, &rectangle, creerCouleur (c)); + // SDL_Flip(surface); +} + +/* Manipulation de lutins */ + +static SDL_Surface *lutins[MAX_LUTINS]; +static int lutins_nb = 0; + +int lutinTexte(char* texte, int couleurTexte) { + couleur c=couleurs[couleurTexte]; + SDL_Color couleur={c.r, c.v, c.b}; + SDL_Surface* lutin=TTF_RenderText_Solid(police, texte, couleur); + if (lutin != NULL) + { + lutins[lutins_nb++] = lutin; + return lutins_nb - 1; + } + return -1; +} + +static void configurerLutin (SDL_Surface *lutin, int ncouleur) +{ + couleur c = couleurs[ncouleur]; + int fond = SDL_MapRGB (lutin->format, c.r, c.v, c.b); + SDL_SetColorKey (lutin, SDL_SRCCOLORKEY | SDL_RLEACCEL, fond); +} + +int chargerLutin (char *fichier, int couleur) +{ + if (lutins_nb >= MAX_LUTINS) return -2; + SDL_Surface *lutin = SDL_LoadBMP (fichier); + if (lutin != NULL) + { + lutins[lutins_nb++] = lutin; + if (couleur >= 0) configurerLutin (lutin, couleur); + return lutins_nb - 1; + } + return -1; +} + +void afficherLutin (int lutin, int x, int y) +{ + SDL_Rect position; + position.x = x; + position.y = y; + SDL_BlitSurface (lutins[lutin], NULL, surface, &position); +} + +int creerLutin (int x, int y, int largeur, int hauteur, int couleur) +{ + if (lutins_nb >= MAX_LUTINS) return -2; + int rmask, gmask, bmask, amask; +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + rmask = 0xff000000; + gmask = 0x00ff0000; + bmask = 0x0000ff00; + amask = 0x000000ff; +#else + rmask = 0x000000ff; + gmask = 0x0000ff00; + bmask = 0x00ff0000; + amask = 0xff000000; +#endif + if (couleur < 0) amask = 0x00000000; + SDL_Surface *lutin = + SDL_CreateRGBSurface (0, largeur, hauteur, BITS_PAR_PIXEL, rmask, gmask, bmask, amask); + SDL_Rect fenetre; + fenetre.x = x; + fenetre.y = y; + fenetre.h = hauteur; + fenetre.w = largeur; + SDL_BlitSurface (surface, &fenetre, lutin, NULL); + lutins[lutins_nb++] = lutin; + if (couleur >= 0) configurerLutin (lutin, couleur); + return lutins_nb - 1; +} + +void tailleLutin (int lutin, int *largeur, int *hauteur) +{ + *largeur = lutins[lutin]->w; + *hauteur = lutins[lutin]->h; +} + +int sauverLutin (int lutin, char *nom) { return SDL_SaveBMP (lutins[lutin], nom); } + +/* Manipulation de copie de surface en BMP */ + +int sauverSurface (char *fichier) { return SDL_SaveBMP (surface, fichier); } + +unsigned char chargerSurface (char *fichier) +{ + SDL_Surface *image = SDL_LoadBMP (fichier); + if (image != NULL) + { + SDL_BlitSurface (image, NULL, surface, NULL); + SDL_Flip (surface); + } + return (image != NULL); +} + +void majSurface (void) { SDL_Flip (surface); } + +/* Trouver la couleur d'un pixel */ + +int couleurPixel (int x, int y) +{ + int bpp = surface->format->BytesPerPixel; + Uint32 *p = (Uint32 *)(surface->pixels + y * surface->pitch + x * bpp); + Uint8 r, v, b; + SDL_GetRGB (*p, surface->format, &r, &v, &b); + int i = 0; + while (1) + { + if (couleurs[i].r < 0) break; + if (r == couleurs[i].r && v == couleurs[i].v && b == couleurs[i].b) break; + i++; + } + if (couleurs[i].r < 0) + return -1; + else + return i; +} + +/* Fonction de traitement des événements */ + +void lireEvenement (evenement *evt, char *touche, void **detail) +{ + static SDL_keysym _detail; + SDL_Event event; + while (SDL_PollEvent (&event)) + { + if (event.type == SDL_QUIT) *evt = quitter; + if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) + { + *evt = (event.type == SDL_KEYDOWN) ? toucheBas : toucheHaut; + char *nom = SDL_GetKeyName (event.key.keysym.sym); + if (strlen (nom) == 1 && nom[0] >= 32 && nom[0] < 128) + *touche = nom[0]; + else + *touche = 0; + if (detail != NULL) + { + _detail = event.key.keysym; + *detail = &_detail; + } + break; + } + } +} + +void attendreEvenement (void) +{ + SDL_Event event; + while (SDL_WaitEvent (&event)) switch (event.type) + { + case SDL_QUIT: + exit (0); + case SDL_KEYDOWN: + case SDL_MOUSEBUTTONDOWN: + return; + } +} diff --git a/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.h b/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.h new file mode 100644 index 0000000..f518cff --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Graphique/libgraph.h @@ -0,0 +1,156 @@ +/**** Bibliotheque graphique (definitions) ****/ + +/** Constantes **/ + +#define COULEUR_BLANC 0 +#define COULEUR_NOIR 1 +#define COULEUR_ROUGE 2 +#define COULEUR_VERT 3 +#define COULEUR_BLEU 4 +#define COULEUR_ROSE 5 +#define COULEUR_GRIS 6 + +#define MAX_LUTINS 16 + +typedef enum {toucheBas, toucheHaut, quitter} evenement; + +/** Prototypes **/ + +/** + * @brief cree une fenetre 2D + * + * @param largeur en pixels de la fenetre + * @param hauteur en pixels de la fenetre + * @param titre de la fenetre (chaine de caractere) + */ +unsigned char creerSurface (int largeur, int hauteur, char *titre); + +/** + * @brief permet de charger un fichier image au format bmp (bitmap) + * + * @param fichier nom du fichier + */ +unsigned char chargerSurface (char *fichier); + + +/** + * @brief permet de sauvegarder une surface en image (format bmp) + * + * @param fichier nom du fichier + * @return 0 si OK, valeur negative sinon + */ +int sauverSurface (char *fichier); + +/** + * @brief met a jour la surface d'affichage + */ +void majSurface (void); + + +/** + * @brief libere la surface d'affichage + * a faire lors de la fermeture + * du programme + */ +void fermerSurface (void); + +/** + * @brief choisit la police de caractères à utiliser pour afficher du texte + * @param chemin nom du fichier de police (format .ttf, voir /usr/share/fonts/truetype) + * @param taille taille de la police + */ +void choisirPolice(const char *chemin, int taille); + +/** + * @brief dessine un rectange de taille (l,h) aux coordonnêes + * (x,y) et de couleur c + * + * @param x 0 <= x <= l_surface + * @param y 0 <= y <= h_surface + * @param l largeur en pixels + * @param h longueur en pixels + * @param c indice de couleur voir variable couleurs dans le fichier .c + */ +void rectanglePlein (int x, int y, int l, int h, int c); + + +/** + * @brief permet de determiner l'indice du tableau de couleur du + * pixel aux coordonnees (x,y) + * + * @param x 0 <= x <= l_surface + * @param y 0 <= y <= h_surface + * @return indice de couleur voire variable couleurs dans le fichier .c + */ +int couleurPixel (int x, int y); + +/** + * @brief crée un lutin à partir d'un texte + * + * @param texte le texte + * @param couleur indice de couleur du texte + * @return numero de lutin dans le tableau dynamique de lutin (< MAX_LUTINS) + */ +int lutinTexte(char *texte, int couleur); + +/** + * @brief charge un lutin à partir du fichier + * + * @param fichier image bitmap du lutin à charger + * @param couleur indice de couleurs à charger + * @return numero de lutin dans le tableau dynamique de lutin (< MAX_LUTINS) + */ +int chargerLutin (char *fichier, int couleur); + +/** + * @brief afficher un lutin aux coordonnées (x,y) + * + * @param lutin numero du lutin à afficher (< MAX_LUTINS) + * @param x abscisse de départ + * @param y ordonnée de départ + */ +void afficherLutin (int lutin, int x, int y); + +/** + * @brief creer un lutin de taille (l,h) aux coordonnées (x,y) + * + * @param x abscisse de départ + * @param y ordonnée de départ + * @param largeur largeur du lutin + * @param hauteur hauteur du lutin + * @param couleur indice de couleur à partir du tableau _couleurs_ + * @return indice du lutin dans le tableau global (< MAX_LUTINS) + */ +int creerLutin (int x, int y, int largeur, int hauteur, int couleur); + +/** + * @brief sauvegarde un lutin dans un fichier + * + * @param lutin numero de lutin à sauvegarder (< MAX_LUTINS) + * @param nom fichier pour la sauvegarde + * @return 0 si OK valeur négative sinon + */ +int sauverLutin (int lutin, char *nom); + +/** + * @brief calcule la taille (largeur,hauteur) d'un lutin + * + * @param lutin index du lutin (< MAX_LUTINS) + * @param largeur pointeur sur la largeur + * @param hauteur pointeur sur la hauteur + */ +void tailleLutin (int lutin, int *largeur, int *hauteur); + +/** + * @brief lire une touche au clavier + * + * @param evt pointeur sur evenement + * @param touche pointeur sur la touche pressée + * @param detail NULL ou keysim + */ +void lireEvenement (evenement *evt, char *touche, void **detail); + +/** + * @brief attente d'un evenement bouton, souris, fin de programme + */ +void attendreEvenement (void); diff --git a/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.c b/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.c new file mode 100644 index 0000000..b7a788d --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.c @@ -0,0 +1,361 @@ +#include +#include +#include +#include "../Graphique/libgraph.h" +#include "../ListeC/Liste.h" +#include "Interactif.h" +#include "../Main/init.h" + +#define TailleX 500 +#define TailleY 500 +#define Sol 475 +#define ErreurHitbox 2 +#define ValeurDeplacementTire 5 +#define ValeurDeplacementJoueur 3 +#define ValeurDeplacementBombe 2 +#define TailleX9_10 9 * TailleX / 10 +#define TailleX1_10 TailleX / 10 + +int CheckCollisionEntiteEntite (struct entite entite1, + int L1, + int H1, + struct entite entite2, + int L2, + int H2) +{ + //CheckX + int gauche1 = entite1.posx - L1/2 + ErreurHitbox; + int droite1 = entite1.posx + L1/2 - ErreurHitbox; + int gauche2 = entite2.posx - L2/2 + ErreurHitbox; + int droite2 = entite2.posx + L2/2 - ErreurHitbox; + //Tout les cas possibles de collision + int CheckX = (gauche1 >= gauche2 && gauche1 <= droite2) || + (droite1 >= gauche2 && droite1 <= droite2) || + (gauche1 >= gauche2 && droite1 <= droite2) || + (gauche2 >= gauche1 && droite2 <= droite1); + + //CheckY + int haut1 = entite1.posy - H1/2 + ErreurHitbox; + int bas1 = entite1.posy + H1/2 - ErreurHitbox; + int haut2 = entite2.posy - H2/2 + ErreurHitbox; + int bas2 = entite2.posy + H2/2 - ErreurHitbox; + int CheckY = (haut1 <= bas2 && haut1 >= haut2) || + (bas1 <= bas2 && bas1 >= haut2) || + (haut1 <= haut2 && bas1 >= bas2) || + (haut2 <= haut1 && bas2 >= bas1); + + + return CheckX && CheckY; +} + +//La fonction renvoie l'entite de la Liste1 si il y a collision +struct entite* CheckCollisionListeEntite (struct liste_entite* Liste1, + int L1, + int H1, + struct entite entite2, + int L2, + int H2) +{ + + struct liste_entite *pListe1 = Liste1; + while (pListe1 != NULL) + { + + if(CheckCollisionEntiteEntite (pListe1->entite, + L1, + H1, + entite2, + L2, + H2) == 1) + { + return &pListe1->entite; + } + + pListe1 = pListe1->suivant; + } + return NULL; +} + + + +//La fonction renvoie une liste d'entite avec les deux entites à supprimer si il y a collision +struct liste_entite* CheckCollisionListeListe (struct liste_entite* Liste1, + int L1, + int H1, + struct liste_entite* Liste2, + int L2, + int H2) +{ + + struct liste_entite *pListe2 = Liste2; + while (pListe2 != NULL) + { + + struct entite* collision = CheckCollisionListeEntite (Liste1, + L1, + H1, + pListe2->entite, + L2, + H2); + if (collision != NULL) + { + // Création des structures pour les deux entités + struct liste_entite* Entite1 = malloc(sizeof(struct liste_entite)); + struct liste_entite* Entite2 = malloc(sizeof(struct liste_entite)); + + // Remplissage des structure avec les entités correspondantes + Entite1->entite = *collision; + Entite2->entite = pListe2->entite; + + // Relier les structures entre elles + Entite1->suivant = Entite2; + Entite2->suivant = NULL; + + return Entite1; + } + + else + pListe2 = pListe2->suivant; + } + + return NULL; +} + +//Tire un missile, il ne peux y en avoir que un à la fois +void Tirer (struct entite joueur, + struct liste_entite** pl) +{ + if (*pl == NULL) + { + ajout_tete(pl, + creer_entite(joueur.posx, + joueur.posy, + -1)); + } +} + + +void DeplacementTire(struct liste_entite** Liste) +{ + struct entite* Entite = &(*Liste)->entite; + if (Entite != NULL) + { + + if (Entite->posy <= 0) + { + afficherLutin(bouillie, + Entite->posx - hitboxbouillieL/2 + ErreurHitbox, + Entite->posy); + SupprimerEntite(Liste, Entite); + } + + else + { + Entite->posy -= ValeurDeplacementTire; + //Je divise ErreurHitbox par 2 car l'erreur du missile est plus petite que pour les autres images + afficherLutin(missile, + Entite->posx - hitboxmissileL/2 + ErreurHitbox/2, + Entite->posy - hitboxmissileH/2 + ErreurHitbox/2); + } + } +} + + + +//La fonction renvoie la touche préssée +char touche() +{ + char touche; + evenement even; + lireEvenement (&even,&touche,NULL); + return touche; +} + + +//Fais une action soit au joueur soit aux tires selon la touche préssée +void action(struct entite* joueur, + char c, + struct liste_entite** tires) +{ + switch (c) + { + case 'd': + if (joueur->posx <= TailleX9_10) + { + joueur->posx += ValeurDeplacementJoueur; + } + break; + case 'q': + if (joueur->posx >= TailleX1_10) + { + joueur->posx -= ValeurDeplacementJoueur; + } + break; + case 't': + Tirer(*joueur, + tires); + break; + default: + break; + } +} + +//La fonction crée une liste de tout les enemies pouvant drop des bombes +//Puis ajoute à la liste bombe, une bombe random provenant d'un des enemies pouvant drop des bombes +void MakeBombeDroppable (struct liste_entite* enemies, + struct liste_entite** bombes) +{ + + struct liste_entite* pListe = enemies; + struct liste_entite* Dropable = NULL; + int taille = 0; + + while (pListe != NULL) + { + + if (pListe->entite.dropbombe == 1) + { + ajout_tete(&Dropable,pListe->entite); + taille += 1; + } + + pListe = pListe->suivant; + } + + if(Dropable == NULL) + { + return; + } + + int randomIndex = rand() % taille-1; + struct liste_entite* pDropable = Dropable; + + for (int i = 0; i <= randomIndex; i++) + { + pDropable = pDropable->suivant; + } + + ajout_tete(bombes, + creer_entite(pDropable->entite.posx, + pDropable->entite.posy, + -1)); +} + +// +void DeplacementBombe(struct liste_entite** Liste) +{ + struct liste_entite* pListe = *Liste; + struct liste_entite* precedent = NULL; + + while (pListe != NULL) + { + + if (pListe->entite.posy + hitboxbombeH/2 - ErreurHitbox >= Sol) + { + struct entite* a_supprimer = &pListe->entite; + + pListe = pListe->suivant; + SupprimerEntite(Liste,a_supprimer); + } + + else + { + pListe->entite.posy += ValeurDeplacementBombe; + afficherLutin(bombe, + pListe->entite.posx - hitboxbombeL/2 + ErreurHitbox, + pListe->entite.posy - hitboxbombeH/2 + ErreurHitbox); + precedent = pListe; + pListe = pListe->suivant; + } + } +} + + +void NouveauDroppeurBombe (struct liste_entite** liste, + struct entite* entite) +{ + + int posx = entite->posx; + int posy = entite->posy; + struct liste_entite* pListe = *liste; + struct entite* entite_basse = NULL; + + // On parcourt la liste et on cherche l'entité la plus basse ayant la même position x + while (pListe != NULL) + { + + if (pListe->entite.posy != posy) + { + + if (pListe->entite.posx == posx && + entite_basse == NULL) + { + entite_basse = &pListe->entite; + } + + else if (pListe->entite.posx == posx && + pListe->entite.posy > entite_basse->posy) + { + entite_basse = &pListe->entite; + } + } + + pListe = pListe->suivant; + } + + // Si aucune entité n'est située plus bas que l'entité en question, on ne peut pas dropper la bombe + if (entite_basse == NULL) + { + return; + } + + entite_basse->dropbombe = 1; +} + + + + +int SupprimerEntitesEnCollision (struct liste_entite** Liste1, + int L1, + int H1, + struct liste_entite** Liste2, + int L2, + int H2) +{ + + struct liste_entite* collision = CheckCollisionListeListe(*Liste1, + L1, + H1, + *Liste2, + L2, + H2); + if (collision != NULL) + { + // Récupération des entités impliquées + struct entite* Entite1 = &collision->entite; + struct entite* Entite2 = &collision->suivant->entite; + + if (Entite1->dropbombe == 1) + { + NouveauDroppeurBombe(Liste1,Entite1); + } + + if (Entite2->dropbombe == 1) + { + NouveauDroppeurBombe(Liste2,Entite2); + } + + // Suppression de l'entité 1 de la liste 1 + SupprimerEntite(Liste1, Entite1); + // Suppression de l'entité 2 de la liste 2 + SupprimerEntite(Liste2, Entite2); + + afficherLutin(bouillie, + Entite2->posx - hitboxbouillieL/2 + ErreurHitbox, + Entite2->posy - hitboxbouillieH/2 + ErreurHitbox); + return 1; + } + return 0; +} + + diff --git a/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.h b/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.h new file mode 100644 index 0000000..faa140b --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Interactif/Interactif.h @@ -0,0 +1,50 @@ +#include +#include + + +int CheckCollisionEntiteEntite (struct entite entite1, + int L1, + int H1, + struct entite entite2, + int L2, + int H2); + +struct entite* CheckCollisionListeEntite (struct liste_entite* Liste1, + int L1, + int H1, + struct entite entite2, + int L2, + int H2); + +struct liste_entite* CheckCollisionListeListe (struct liste_entite* Liste1, + int L1, + int H1, + struct liste_entite* Liste2, + int L2, + int H2); + +void Tirer (struct entite joueur, + struct liste_entite** pl); + +void DeplacementTire(struct liste_entite** Liste); + +char touche(); + +void action(struct entite* joueur, + char c, + struct liste_entite** tires); + +void MakeBombeDroppable (struct liste_entite* enemies, + struct liste_entite** bombes); + +void DeplacementBombe(struct liste_entite** Liste) ; + +void NouveauDroppeurBombe (struct liste_entite** liste, + struct entite* entite); + +int SupprimerEntitesEnCollision (struct liste_entite** Liste1, + int L1, + int H1, + struct liste_entite** Liste2, + int L2, + int H2); diff --git a/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.c b/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.c new file mode 100644 index 0000000..2dc0257 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.c @@ -0,0 +1,69 @@ +#include +#include +#include + +#include "Liste.h" + +//Crée une entité +struct entite creer_entite (int x, + int y, + int bombe) +{ + struct entite e; + + e.posx = x; + e.posy = y; + e.dropbombe = bombe; + + return e; +} + + +//Ajout en tete une entité dans une liste +void ajout_tete (struct liste_entite** Liste, + struct entite x ) +{ + struct liste_entite *Listetmp=NULL; + + Listetmp = malloc(sizeof(struct liste_entite)); + Listetmp->entite = x; + Listetmp->suivant = *Liste; + + *Liste = Listetmp; +} + + +//Supprime une entite d'une liste +void SupprimerEntite (struct liste_entite** Liste, + struct entite* entite) +{ + struct liste_entite* courant = *Liste; + struct liste_entite* precedent = NULL; + + while (courant != NULL) + { + + if (memcmp (&courant->entite, + entite, + sizeof(struct entite)) == 0) + { + + if (precedent == NULL) + { + *Liste = courant->suivant; + } + + else + { + precedent->suivant = courant->suivant; + } + + free(courant); + break; + } + + precedent = courant; + courant = courant->suivant; + } +} + diff --git a/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.h b/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.h new file mode 100644 index 0000000..490bba6 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/ListeC/Liste.h @@ -0,0 +1,31 @@ +#include +#include + +//dropbombe concerne les entités enemies +//1 les enemies peuvent drop des bombes, 0 ils ne peuvent pas +//Les entites non concernées vallent ont un dropbombe = -1 +struct entite +{ + int posx; + int posy; + int dropbombe; +}; + + +struct liste_entite +{ + struct entite entite; + struct liste_entite *suivant; +}; + + +struct entite creer_entite (int x, + int y, + int bombe); + +void ajout_tete (struct liste_entite** Liste, + struct entite x ); + +void SupprimerEntite (struct liste_entite** Liste, + struct entite* entite); + diff --git a/Space Invader/Envahisseurs/Graphique/src/Main/Makefile b/Space Invader/Envahisseurs/Graphique/src/Main/Makefile new file mode 100644 index 0000000..f209a83 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Main/Makefile @@ -0,0 +1,34 @@ +CC=clang +TARGET=exec +CFLAGS=-g -W -Wall -Wextra +LDFLAGS=-I Graphique -l graph -L ../Graphique -l SDL -l SDL_ttf + +default: $(TARGET) + +Liste.o : ../ListeC/Liste.c ../ListeC/Liste.h + clang $(CFLAGS) -c ../ListeC/Liste.c + +Monstre.o : ../Monstre/Monstre.c ../Monstre/Monstre.h ../ListeC/Liste.h + clang $(CFLAGS) -c ../Monstre/Monstre.c + +Interactif.o : ../Interactif/Interactif.c ../Interactif/Interactif.h ../ListeC/Liste.h + clang $(CFLAGS) -c ../Interactif/Interactif.c + +init.o : init.c init.h ../ListeC/Liste.h + clang $(CFLAGS) -c init.c + +main.o : main.c ../ListeC/Liste.h + clang $(CFLAGS) -c main.c + + +$(TARGET): Liste.o main.o Monstre.o Interactif.o init.o + clang main.o Liste.o Monstre.o Interactif.o init.o -o $(TARGET) $(LDFLAGS) + +.PHONY: clean +clean: + rm -f *.o + rm -f $(TARGET) + +tidy : main.c ../ListeC/Liste.c ../Monstre/Monstre.c ../Interactif/Interactif.c init.c + $(CC)-tidy ../Interactif/Interactif.c --checks="readability-*" -header-filter=.* + diff --git a/Space Invader/Envahisseurs/Graphique/src/Main/init.c b/Space Invader/Envahisseurs/Graphique/src/Main/init.c new file mode 100644 index 0000000..4b9d011 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Main/init.c @@ -0,0 +1,234 @@ +#include +#include + +#include "../Graphique/libgraph.h" +#include "../ListeC/Liste.h" +#include "../Interactif/Interactif.h" +#include "init.h" + +#define TailleX 500 +#define TailleY 500 +#define PositionX_1 TailleX / 2 +#define PositionY_1 TailleY / 2 +#define PositionY_2 TailleY / 4 + +#define Sol 475 +#define ErreurHitbox 2 +#define TailletexteMax 30 + +#define JoueurX TailleX/2 +#define JoueurY 9*TailleY/10 + +#define Nom "Space Invader" +#define TaillePolice1 TailleX / 10 +#define TaillePolice2 TailleX / 20 + +//Ces variables sont globales car utilisées dans plusieurs .c +//Toutes les hitbox sont initialisées 1 fois puis sont des constantes +struct entite joueur; + +int canon = 0; +int missile = 0; +int sbire = 0; +int bouillie = 0; +int bombe = 0; + +int hitboxcanonL = 0; +int hitboxcanonH = 0; +int hitboxmissileL = 0; +int hitboxmissileH = 0; +int hitboxsbireL = 0; +int hitboxsbireH = 0; +int hitboxbouillieL = 0; +int hitboxbouillieH = 0; +int hitboxbombeL = 0; +int hitboxbombeH = 0; + +//Initialisation des variables globales pour le main +void initialiser() +{ + canon = chargerLutin ("../../Lutins/invader_canon.bmp", + COULEUR_NOIR); + missile = chargerLutin ("../../Lutins/invader_missile.bmp", + COULEUR_NOIR); + sbire = chargerLutin ("../../Lutins/invader_monstre1_1.bmp", + COULEUR_NOIR); + bouillie = chargerLutin ("../../Lutins/invader_monstre_bouillie.bmp", + COULEUR_NOIR); + bombe = chargerLutin ("../../Lutins/invader_bombe.bmp", + COULEUR_NOIR); + + tailleLutin (canon, + &hitboxcanonL, + &hitboxcanonH); + + tailleLutin (missile, + &hitboxmissileL, + &hitboxmissileH); + + tailleLutin (sbire, + &hitboxsbireL, + &hitboxsbireH); + + tailleLutin (bouillie, + &hitboxbouillieL, + &hitboxbouillieH); + + tailleLutin (bombe, + &hitboxbombeL, + &hitboxbombeH); + +} + +//Initialisation des coordonnées du joueur pour le main +void initialiserjoueur(struct entite* joueur) +{ + joueur->posx = JoueurX; + joueur->posy = JoueurY; + joueur->dropbombe = -1; +} + +//Page de démarage du jeu +char pagedemarrage() +{ + static const char policeDefaut[] = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + char input = '\0'; + int Largeur = 0; + int Hauteur = 0; + char jouer[] = "Appuyer sur j pour Jouer"; + char quitter[] = "Appuyer ailleurs pour Quitter"; + + choisirPolice (policeDefaut, TaillePolice2); + int LutinJouer = lutinTexte (jouer, COULEUR_BLANC); + int LutinQuitter = lutinTexte (quitter, COULEUR_BLANC); + + choisirPolice (policeDefaut, TaillePolice1); + int LutinBienvenue = lutinTexte (Nom, COULEUR_VERT); + + rectanglePlein (0, + 0, + TailleX, + TailleY, + COULEUR_NOIR); + + tailleLutin (LutinBienvenue, + &Largeur, + &Hauteur); + afficherLutin (LutinBienvenue, + PositionX_1 - Largeur / 2, + PositionY_2 + Hauteur / 2); + + tailleLutin (LutinJouer, + &Largeur, + &Hauteur); + afficherLutin (LutinJouer, + PositionX_1 - Largeur / 2, + PositionY_1 - Hauteur / 2); + + tailleLutin (LutinQuitter, + &Largeur, + &Hauteur); + afficherLutin (LutinQuitter, + PositionX_1 - Largeur / 2, + PositionY_1 + Hauteur / 2); + + majSurface(); + attendreEvenement (); + + input = touche(); + while (input == '\0') + { + input = touche(); + } + return input; +} + +//Page en cas de mort dans le jeu +void pagemort (int nbr_vie) +{ + static const char policeDefaut[] = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + int Largeur = 0; + int Hauteur = 0; + char mort[] = "Vous etes mort"; + char vie[TailletexteMax] = "\0"; + sprintf(vie, "Nombre de vie restante : %d", nbr_vie); + //sprint_f crée un warning mais celui-ci ne peut pas crée d'erreur + + choisirPolice (policeDefaut, TaillePolice1); + int LutinMort = lutinTexte(mort, COULEUR_ROUGE); + + choisirPolice (policeDefaut, TaillePolice2); + int LutinVie = lutinTexte(vie, COULEUR_BLANC); + + rectanglePlein (0, + 0, + TailleX, + TailleY, + COULEUR_NOIR); + + tailleLutin (LutinMort, + &Largeur, + &Hauteur); + afficherLutin (LutinMort, + PositionX_1 - Largeur / 2, + PositionY_2 + Hauteur / 2); + + tailleLutin (LutinVie, + &Largeur, + &Hauteur); + afficherLutin (LutinVie, + PositionX_1 - Largeur / 2, + PositionY_1 - Hauteur / 2); +} + +//Page de GameOver du jeu +void pageGameOver() +{ + static const char policeDefaut[] = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + int Largeur = 0; + int Hauteur = 0; + char fin[] = "GAME OVER"; + + choisirPolice(policeDefaut, TaillePolice1); + int LutinFin = lutinTexte(fin, COULEUR_ROUGE); + + rectanglePlein (0, + 0, + TailleX, + TailleY, + COULEUR_NOIR); + + tailleLutin (LutinFin, + &Largeur, + &Hauteur); + afficherLutin (LutinFin, + PositionX_1 - Largeur / 2, + PositionY_1 - Hauteur / 2); + +} + +//Page de Victoire du jeu +void pageVictoire() +{ + static const char policeDefaut[] = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"; + int Largeur = 0; + int Hauteur = 0; + char fin[] = "VICTOIRE"; + + choisirPolice(policeDefaut, TaillePolice1); + int LutinFin = lutinTexte(fin, COULEUR_VERT); + + rectanglePlein (0, + 0, + TailleX, + TailleY, + COULEUR_NOIR); + + tailleLutin (LutinFin, + &Largeur, + &Hauteur); + afficherLutin (LutinFin, + PositionX_1 - Largeur / 2, + PositionY_1 - Hauteur / 2); + +} diff --git a/Space Invader/Envahisseurs/Graphique/src/Main/init.h b/Space Invader/Envahisseurs/Graphique/src/Main/init.h new file mode 100644 index 0000000..dfdcd83 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Main/init.h @@ -0,0 +1,30 @@ +#include +#include + +extern int canon; +extern int missile; +extern int sbire; +extern int bouillie; +extern int bombe; + +extern struct entite joueur; +extern char Nom[]; +extern char input; + +extern int hitboxcanonL; +extern int hitboxcanonH; +extern int hitboxmissileL; +extern int hitboxmissileH; +extern int hitboxsbireL; +extern int hitboxsbireH; +extern int hitboxbouillieL; +extern int hitboxbouillieH; +extern int hitboxbombeL; +extern int hitboxbombeH; + +void initialiser(); +void initialiserjoueur(struct entite* joueur); +char pagedemarrage(); +void pagemort(int nbr_vie); +void pageGameOver(); +void pageVictoire(); diff --git a/Space Invader/Envahisseurs/Graphique/src/Main/main.c b/Space Invader/Envahisseurs/Graphique/src/Main/main.c new file mode 100644 index 0000000..deed538 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Main/main.c @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include "../Graphique/libgraph.h" +#include "../ListeC/Liste.h" +#include "../Monstre/Monstre.h" +#include "../Interactif/Interactif.h" +#include "init.h" + +#define TailleX 500 +#define TailleY 500 +#define Sol 475 +#define ErreurHitbox 2 +#define Nom "Space Invader" + +int main() +{ + creerSurface(TailleX,TailleY,Nom); + + initialiser(); + initialiserjoueur(&joueur); + + struct liste_entite *enemies = NULL; + struct liste_entite *tires = NULL; + struct liste_entite *bombes = NULL; + //joueur est dans une liste pour que je puisse utiliser des fonctions deja créé + struct liste_entite* Ljoueur = NULL; + ajout_tete(&Ljoueur,joueur); + + + LigneSbire(&enemies,8,3); + int SensVague=1; + + char input = '\0'; + int compteur = 0; + int DropAlea = 0; + int CheckAlea = 0; + int mort = 0; + int nbr_vie = 3; + + int coeur = chargerLutin ("../../Lutins/Coeur.bmp", + COULEUR_NOIR); + int hitboxcoeurL,hitboxcoeurH; + tailleLutin (coeur, + &hitboxcoeurL, + &hitboxcoeurH); + + if ( pagedemarrage() != 'j') + { + return 0; + } + SDL_Delay(500); + + //Bouble principale + while(input!='m') + { + if (mort == 1) + { + nbr_vie-=1; + if (nbr_vie > 0) + { + pagemort(nbr_vie); + majSurface(); + SDL_Delay(2000); + mort = 0; + } + else + { + pageGameOver(); + majSurface(); + SDL_Delay(2000); + return 0; + } + ajout_tete(&Ljoueur,joueur); + tires = NULL; + bombes = NULL; + } + + rectanglePlein(0,0,TailleX,TailleY,COULEUR_NOIR); + + if (nbr_vie == 3) + { + afficherLutin(coeur,Sol-2*hitboxcoeurL,Sol); + } + if (nbr_vie >= 2) + { + afficherLutin(coeur,Sol-hitboxcoeurL,Sol); + } + afficherLutin(coeur,Sol,Sol); + + rectanglePlein(0,Sol,TailleX,2,COULEUR_VERT); + + + afficherLutin(canon,Ljoueur->entite.posx - hitboxcanonL/2 + ErreurHitbox,Ljoueur->entite.posy); + AfficherSbire(enemies,sbire,hitboxsbireL,hitboxsbireH); + + if (DropAlea == 0) + { + DropAlea = rand() % 31 + 100; + } + if (CheckAlea == DropAlea) + { + MakeBombeDroppable(enemies,&bombes); + DropAlea=0; + CheckAlea=0; + } + + + input = touche(); + action(&Ljoueur->entite,input,&tires); + + + if (compteur==2) + { + DeplacementSbire(enemies,&SensVague,1); + compteur=0; + } + + DeplacementTire(&tires); + DeplacementBombe(&bombes); + + SupprimerEntitesEnCollision(&tires,hitboxmissileL,hitboxmissileH,&enemies,hitboxsbireL,hitboxsbireH); + + if (SupprimerEntitesEnCollision(&bombes,hitboxbombeL,hitboxbombeH,&Ljoueur,hitboxcanonL,hitboxcanonH) == 1) + { + mort = 1; + majSurface(); + SDL_Delay(200); + } + if (SupprimerEntitesEnCollision(&enemies,hitboxsbireL,hitboxsbireH,&Ljoueur,hitboxcanonL,hitboxcanonH) == 1) + { + pageGameOver(); + majSurface(); + SDL_Delay(2000); + return 0; + } + + if (enemies == NULL) + { + pageVictoire(); + majSurface(); + SDL_Delay(2000); + return 0; + } + + majSurface(); + SDL_Delay(20); + + compteur += 1; + CheckAlea += 1; + + } + return 0; +} diff --git a/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.c b/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.c new file mode 100644 index 0000000..a285fca --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.c @@ -0,0 +1,105 @@ +#include +#include + +#include "../Graphique/libgraph.h" +#include "../ListeC/Liste.h" +#include "Monstre.h" + +#define TailleX 500 +#define TailleY 500 +#define ErreurHitbox 2 + +#define Taille1_10 TailleX / 10 +#define Taille9_10 9 * TailleX / 10 +#define TailleJump 30 + +//Sens = 1 -> Va vers la droite +//Sens = 0 -> Va vers la gauche +void DeplacementSbire(struct liste_entite* Liste, + int* SensDeplacement, + int Vitesse) +{ + + int ind = 0; + struct liste_entite* pListe = Liste; + + while (pListe != NULL) + { + pListe->entite.posx += (*SensDeplacement == 1) ? Vitesse : -Vitesse; + if (pListe->entite.posx >= Taille9_10) + { + ind = 1; + } + + else if (pListe->entite.posx <= Taille1_10) + { + ind = 2; + } + + pListe = pListe->suivant; + } + + if (ind != 0) + { + *SensDeplacement = (ind == 1) ? 0 : 1; + struct liste_entite* p2Liste = Liste; + + while (p2Liste != NULL) + { + p2Liste->entite.posy += TailleJump; + p2Liste = p2Liste->suivant; + } + } +} + +//Création de lignes d'entités enemies dans la liste enemies +void LigneSbire (struct liste_entite** ListeSbire, + int nbr_enemies, + int nbr_rangee) +{ + + for (int j = 1; j <= nbr_rangee; j++) + { + int compteurY = j * Taille1_10; + int compteurX = TailleX / (nbr_enemies+1); + + for (int i = 0; i < nbr_enemies; i++) + { + if (j == nbr_rangee) + { + ajout_tete(ListeSbire, + creer_entite(compteurX, + compteurY, + 1)); + compteurX += 2 * TailleX / (3 * nbr_enemies); + } + + else + { + ajout_tete(ListeSbire, + creer_entite(compteurX, + compteurY, + 0)); + compteurX += 2 * TailleX / (3 * nbr_enemies); + } + } + } +} + +//Affichage des enemies centrés dans leur hitbox +void AfficherSbire (struct liste_entite* Liste, + int lutin, + int Largeur, + int Hauteur) +{ + + struct liste_entite* pListe = Liste; + + while (pListe != NULL) + { + afficherLutin(lutin, + pListe->entite.posx - Largeur / 2 + ErreurHitbox, + pListe->entite.posy - Hauteur / 2 + ErreurHitbox); + pListe=pListe->suivant; + } +} diff --git a/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.h b/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.h new file mode 100644 index 0000000..4a8f800 --- /dev/null +++ b/Space Invader/Envahisseurs/Graphique/src/Monstre/Monstre.h @@ -0,0 +1,15 @@ +#include +#include + +void DeplacementSbire(struct liste_entite* Liste, + int* SensDeplacement, + int Vitesse); + +void LigneSbire (struct liste_entite** ListeSbire, + int nbr_enemies, + int nbr_rangee); + +void AfficherSbire (struct liste_entite* Liste, + int lutin, + int Largeur, + int Hauteur); -- libgit2 0.21.2