Commit fca243b9 authored by Valentin MEUNIER's avatar Valentin MEUNIER
Browse files

option en plus

parent df12b202
......@@ -158,63 +158,98 @@ void affichage_fin(SDL_Texture *my_texture,SDL_Window *window,SDL_Renderer *rend
SDL_RenderCopy(renderer,my_texture,&state, &destination); // Préparation de l'affichage
}
void afficherImageBrouillard(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture, int pos_x, int pos_y)
{
int i1,j1,val=0;
int * voisin = malloc(5*sizeof(int));
for (int i=0; i<5; i++)
voisin[i]=-1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
int x=pos_x/taille_cell;
int y=pos_y/taille_cell;
voisin[4]=x+P*y;
void afficherImage(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture){
int i1,j1,x,noeud=0;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
for (int i=0;i<N;i++){
for (int j=0;j<P;j++){
x=tab[j][i];
i1=(noeud%P); //coordonee colonne du noeud
j1=((int)noeud/P); // coordonee ligne du noeud
affichage_texture(texture,window,renderer,x,i1,j1,taille_cell);
noeud+=1;
}
}
}
void point_minimap(SDL_Renderer * renderer, int taille_cell, int pos_x, int pos_y)
{
SDL_Rect rectangle;
SDL_SetRenderDrawColor(renderer,255,0,0,255);
if (tab[x][y] & FLAG_N)
voisin[0]=x+P*y -P;
if (tab[x][y] & FLAG_S)
voisin[1]=x+P*y+P ;
if (tab[x][y] & FLAG_O)
voisin[2]=x+P*y -1;
if (tab[x][y] & FLAG_E)
voisin[3]=x+P*y+1;
for (int j=0; j<5; j++)
{
if (voisin[j]!=-1)
{
val=tab[voisin[j]%P][voisin[j]/P];
i1=(voisin[j]%P); //coordonee colonne du noeud
j1=(voisin[j]/P); // coordonee ligne du noeud
affichage_texture(texture,window,renderer,val,i1,j1,taille_cell);
}
}
free(voisin);
rectangle.x=pos_x;
rectangle.y=pos_y;
rectangle.h=rectangle.w=taille_cell/2;
SDL_RenderFillRect(renderer, &rectangle);
SDL_SetRenderDrawColor(renderer,0,0,0,255);
}
void afficherImage(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture)
void afficherImageBrouillard(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture, int pos_x, int pos_y)
{
int i1,j1,x,noeud=0;
int i1,j1,j;
int * voisin = malloc(9*sizeof(int));
int x=pos_x/taille_cell;
int y=pos_y/taille_cell;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
SDL_RenderClear(renderer);
for (j=0;j<9;j++)
voisin[j]=-1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
for (int i=0;i<N;i++)
{
for (int j=0;j<P;j++)
{
x=tab[j][i];
i1=(noeud%P); //coordonee colonne du noeud
j1=((int)noeud/P); // coordonee ligne du noeud
if (tab[x][y] & FLAG_N)
voisin[0]=x+P*y-P;
if (tab[x][y] & FLAG_S)
voisin[1]=x+P*y+P ;
if (tab[x][y] & FLAG_O)
voisin[2]=x+P*y -1;
if (tab[x][y] & FLAG_E)
voisin[3]=x+P*y+1;
if (y>0){
if (x>0){
if (((tab[x][y] & FLAG_O) && (tab[x-1][y] & FLAG_N )) || ((tab[x][y] & FLAG_N) && (tab[x][y-1] & FLAG_O)))
voisin[5]=x+P*y-P-1;
}
if (x<P-1){
if (((tab[x][y] & FLAG_N) && (tab[x+1][y-1] & FLAG_E )) || ((tab[x][y] & FLAG_E) && (tab[x][y-1] & FLAG_N)))
voisin[6]=x+P*y-P+1;
}
}
affichage_texture(texture,window,renderer,x,i1,j1,taille_cell);
noeud+=1;
if (y<N-1){
if (x>0){
if (((tab[x][y] & FLAG_O) && (tab[x-1][y] & FLAG_S )) || ((tab[x][y] & FLAG_S) && (tab[x][y+1] & FLAG_O)))
voisin[7]=x+P*y+P-1;
}
if (x<P-1){
if (((tab[x][y] & FLAG_S) && (tab[x][y+1] & FLAG_E )) || ((tab[x][y] & FLAG_E) && (tab[x+1][y] & FLAG_S)))
voisin[8]=x+P*y+P+1;
}
}
voisin[4]=x+P*y;
for (j=0; j<9; j++)
{
if (voisin[j]!=-1)
{
x=tab[voisin[j]%P][voisin[j]/P];
i1=(voisin[j]%P); //coordonee colonne du noeud
j1=(voisin[j]/P); // coordonee ligne du noeud
affichage_texture(texture,window,renderer,x,i1,j1,taille_cell);
}
}
free(voisin);
}
void peindreMap(SDL_Texture * texture, SDL_Window* window ,SDL_Renderer *renderer, int noeuds, int taille_cell, int ** laby)
{
//printf("peindre case %d %d %d \n",noeuds%P,noeuds/P,laby[noeuds%P][noeuds/P]);
......
#ifndef _gard_affichage_image
#define _gard_affichage_image
#include "main.h"
#include "kruskal.h"
#include "main.h"
SDL_Texture* load_texture_from_image(char * file_image_name, SDL_Renderer *renderer);
void affichage_texture(SDL_Texture *my_texture,SDL_Window *window,SDL_Renderer *renderer,int x,int coord_colonne,int coord_ligne,int taille_cell);
void afficherImage(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture);
void afficherImageBrouillard(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture, int pos_x, int pos_y);
void point_minimap(SDL_Renderer * renderer, int taille_cell, int pos_x, int pos_y);
void affichage_fin(SDL_Texture *my_texture,SDL_Window *window,SDL_Renderer *renderer,int coord_colonne,int coord_ligne,int taille_cell);
void peindreMap(SDL_Texture * texture, SDL_Window* window ,SDL_Renderer *renderer, int noeuds, int taille_cell, int ** laby);
......
#include "affichage_txt.h"
void affichage_txt(SDL_Window *window,SDL_Renderer *renderer)
{
SDL_Rect window_dimensions = {0}, pos = {0, 0, 0, 0};
SDL_GetWindowSize(window,&window_dimensions.w,&window_dimensions.h);
if (TTF_Init() < 0) exit(EXIT_FAILURE);
TTF_Font* font = NULL; // la variable 'police de caractère'
font = TTF_OpenFont("Pacifico.ttf", 65); // La police à charger, la taille désirée
if (font == NULL) exit(EXIT_FAILURE);
SDL_Color color = {230, 200, 0, 255}; // la couleur du texte
SDL_Surface* text_surface = NULL; // la surface (uniquement transitoire)
text_surface = TTF_RenderText_Blended(font, "Vous avez atteint votre maman !", color); // création du texte dans la surface
if (text_surface == NULL) exit(EXIT_FAILURE);
SDL_Texture* text_texture = NULL; // la texture qui contient le texte
text_texture = SDL_CreateTextureFromSurface(renderer, text_surface); // transfert de la surface à la texture
if (text_texture == NULL) exit(EXIT_FAILURE);
SDL_FreeSurface(text_surface); // la texture ne sert plus à rien
SDL_QueryTexture(text_texture, NULL, NULL, &pos.w, &pos.h); // récupération de la taille (w, h) du texte
pos.x= (window_dimensions.w-pos.w)/4;
pos.y= 0;
SDL_RenderCopy(renderer, text_texture, NULL, &pos); // Ecriture du texte dans le renderer
SDL_DestroyTexture(text_texture);
TTF_CloseFont(font);
TTF_Quit();
//ne pas oublier le TTF_Quit() dans le programme principal
}
#ifndef _gard_text_
#define _gard_text_
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
void affichage_txt(SDL_Window *window,SDL_Renderer *renderer);
#endif
......@@ -70,6 +70,9 @@ graph_t * kruskal(graph_t * graph,int noeuds, int nb_aretes,int * cours,double p
*cours+=1;
}
}
free(graph->liste);
free(partA);
free(graph);
return A;
}
......
......@@ -20,7 +20,6 @@ void end_sdl(char ok,char const* msg,SDL_Window* window, SDL_Renderer* renderer)
if (renderer != NULL) SDL_DestroyRenderer(renderer);
if (window != NULL) SDL_DestroyWindow(window);
SDL_Quit();
if (!ok) exit(EXIT_FAILURE);
}
......@@ -104,7 +103,7 @@ int main (int argc, char** argv)
int **tab;
int cours=0;
int taille_cell=min(screen.w/(P+2),screen.h/(N+2));
//int * tab_parents; à décommenter pour l'utilisation de Dijkstra
// int * tab_parents; a decommenter pour l'utilisation de Dijkstra
//initialisation du graph
graph_t * graph=creer_graph(noeuds,nb_aretes);
generation(graph);
......@@ -127,13 +126,13 @@ int main (int argc, char** argv)
texture_fin = load_texture_from_image("roguelikeChar_transparent.png",renderer);
if (texture_fin==NULL) exit(EXIT_FAILURE);
//tab_parents=dijkstra(tab,graph->noeuds,ND_ARRIVEE);
SDL_Event event;
//on initialise une fenêtre 2 pour une mini-map
//on initialise une fenêtre 2 pour une mini-map
SDL_Window *window2;
window2 = SDL_CreateWindow("SDL2 Programme 0.1", screen.w-screen.w/3, 0,screen.w/3,screen.h/4, SDL_WINDOW_RESIZABLE);
window2 = SDL_CreateWindow("SDL2 Programme 0.1", screen.w-screen.w/3, 0,screen.w/3,screen.h/3, SDL_WINDOW_RESIZABLE);
if (window2 == 0) fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
......@@ -153,7 +152,7 @@ int main (int argc, char** argv)
texture_fin2 = load_texture_from_image("roguelikeChar_transparent.png",renderer2);
if (texture_fin2==NULL) exit(EXIT_FAILURE);
int pause=1;
int pause=0;
int noeud_dep=rand()%TAILLE;
int right=1;
int stand=0;
......@@ -165,127 +164,162 @@ int main (int argc, char** argv)
int noeud_arrive=rand()%TAILLE;
int colli;
int noeud_actuel;
int taille_cell2=min((screen.w/3)/(P+2),(screen.h/3)/(N+2));
int fin_mama=0;
int relancer=0;
int affiche_carte=0;
int dijkstra_ok;
int * minimap=malloc((TAILLE+1)*sizeof(int));
for (int i=0; i<(TAILLE +1); i++)
minimap[i]=0;
for (int iter=0;iter<TAILLE+1;iter++)
minimap[iter]=0;
int taille_cell2=min((screen.w/3)/(P+2),(screen.h/3)/(N+2));
affichage_fin(texture_fin2,window2,renderer2,noeud_arrive/P,noeud_arrive%P,taille_cell2);
printf("l : relancer \np,SPACE : pause \ncroix : quitter\n");
printf("l : relancer \np,SPACE : pause \ncroix : quitter \nm : afficher la carte complète du labyrinthe pendant 2 secondes\n");
while ((running)||(temps<50))
{
while (running)
{
noeud_actuel=pos_y*P/taille_cell+pos_x/taille_cell;
minimap[0]++;
minimap[minimap[0]]=noeud_actuel;
if (noeud_actuel==noeud_arrive)
running=0;
fin_mama=1;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
break;
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
temps=1000;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
screen.w = event.window.data1;
screen.h = event.window.data2;
break;
case SDL_WINDOWEVENT_EXPOSED:
break;
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDL_WINDOWEVENT:
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
break;
default:
break;
}
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_l:
noeud_dep=rand()%TAILLE;
right=1;
stand=0;
pos_x=noeud_dep%P*taille_cell;
pos_y=noeud_dep/P*taille_cell;
temps=1;
noeud_arrive=rand()%TAILLE;
relancer=1;
break;
case SDLK_SPACE:
case SDLK_p:
pause=!pause;
break;
case SDLK_SPACE:
case SDLK_p:
pause=1-pause;
break;
case SDLK_m: //la fonction devra afficher la totalite du labyrinthe sans brouillard de guerre
afficherImage(renderer,window,tab,taille_cell2,texture);
affichage_fin(texture_fin,window,renderer,noeud_arrive/P,noeud_arrive%P,taille_cell);
SDL_RenderPresent(renderer);
SDL_Delay(2000);
SDL_RenderClear(renderer);
affiche_carte=1;
break;
case SDLK_d:
dijkstra_ok=1;
break;
default:
break;
}
break;
default:
break;
}
}
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
if (pause){
default:
break;
}
break;
}
}
/* if (dijkstra_ok)
{
tab_parents=dijkstra(tab,graph->noeuds,noeuds_arrive);
}
*/ if (affiche_carte && !pause)
{
afficherImage(renderer,window,tab,taille_cell,texture);
affichage_fin(texture_fin,window,renderer,noeud_arrive/P,noeud_arrive%P,taille_cell);
SDL_RenderPresent(renderer);
temps++;
if (temps>50)
{
affiche_carte=0;
SDL_RenderClear(renderer);
temps=0;
}
}
if (relancer && !pause)
{
noeud_dep=rand()%TAILLE;
right=1;
stand=0;
pos_x=noeud_dep%P*taille_cell;
pos_y=noeud_dep/P*taille_cell;
temps=1;
noeud_arrive=rand()%TAILLE;
relancer=0;
SDL_RenderClear(renderer2);
SDL_RenderPresent(renderer2);
for (int i=1; i<=minimap[0]; i++)
peindreMap(texture2,window2,renderer2,minimap[i],taille_cell2,tab);
minimap[i]=0;
minimap[0]=1;
minimap[1]=pos_y*P/taille_cell+pos_x/taille_cell;
}
else if (!pause)
{
SDL_RenderClear(renderer2);
for (int iter=1;iter<minimap[0];iter++)
{
peindreMap(texture2,window2,renderer2,minimap[iter],taille_cell2,tab);
affichage_fin(texture_fin2,window2,renderer2,noeud_arrive/P,noeud_arrive%P,taille_cell2);
point_minimap(renderer2,taille_cell2,taille_cell2*(noeud_actuel%P),taille_cell2*noeud_actuel/P);
}
SDL_RenderPresent(renderer2);
if ((keystates[SDL_SCANCODE_UP]||keystates[SDL_SCANCODE_W])) {
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
if ((keystates[SDL_SCANCODE_UP]||keystates[SDL_SCANCODE_W]))
{
colli=collision_N(pos_x,pos_y,tab,taille_cell);
if(!colli){
if (right){
if(!colli)
{
if (right)
play_with_elve_N(texture_elve,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
}
else{
else
play_with_elve_N_l(texture_elve_reverse,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
}
pos_y=pos_y-deplacement;
stand=0;
}
}
if ((keystates[SDL_SCANCODE_DOWN]||keystates[SDL_SCANCODE_S])) {
if ((keystates[SDL_SCANCODE_DOWN]||keystates[SDL_SCANCODE_S]))
{
colli=collision_S(pos_x,pos_y,tab,taille_cell);
if(!colli){
if (right){
if(!colli)
{
if (right)
play_with_elve_S(texture_elve,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
}
else{
else
play_with_elve_S_l(texture_elve_reverse,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
}
pos_y=pos_y+deplacement;
stand=0;
}
}
}
if ((keystates[SDL_SCANCODE_LEFT]||keystates[SDL_SCANCODE_A])) {
if ((keystates[SDL_SCANCODE_LEFT]||keystates[SDL_SCANCODE_A]))
{
colli=collision_O(pos_x,pos_y,tab,taille_cell);
if(!colli){
if(!colli)
{
play_with_elve_O(texture_elve_reverse,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
pos_x=pos_x-deplacement;
stand=0;
right=0;
}
pos_x=pos_x-deplacement;
stand=0;
right=0;
}
}
if ((keystates[SDL_SCANCODE_RIGHT]||keystates[SDL_SCANCODE_D])) {
if ((keystates[SDL_SCANCODE_RIGHT]||keystates[SDL_SCANCODE_D]))
{
colli=collision_E(pos_x,pos_y,tab,taille_cell);
if(!colli){
if(!colli)
{
play_with_elve_E(texture_elve,texture,window,renderer,pos_x,pos_y,deplacement,zoom,tab,taille_cell,texture_fin,noeud_arrive);
pos_x=pos_x+deplacement;
stand=0;
right=1;
}
pos_x=pos_x+deplacement;
stand=0;
right=1;
}
}
if (right){
switch (stand){
if (right)
{
switch (stand){
case 0:
play_standstill_1(texture_elve,texture,window,renderer,pos_x,pos_y,zoom,tab,taille_cell,texture_fin,noeud_arrive);
stand=(stand+1)%7;
......@@ -330,20 +364,38 @@ int main (int argc, char** argv)
break;
}
}
SDL_Delay(30);
SDL_RenderPresent(renderer);
if (running==0){
}
if (fin_mama)
{
affichage_txt(window,renderer);
temps++;
}
}
if (temps>50)
running=0;
}
if (pause)
{
relancer=0;
affiche_carte=0;
affichage_txt(window, renderer);
}
SDL_Delay(30);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture_elve_reverse);
SDL_DestroyTexture(texture_elve);
SDL_DestroyTexture(texture);
SDL_DestroyTexture(texture_fin);
SDL_DestroyTexture(texture_fin2);
SDL_DestroyTexture(texture2);
free(minimap);
free(graph->liste);
free(graph);
for (int i=0; i<P; i++)
free(tab[i]);
free(tab);
end_sdl(1, "Normal ending", window, renderer);
end_sdl(1,"Normal ending",window2,renderer2);
free(minimap);
SDL_Quit();
return 1;
}
......@@ -4,15 +4,18 @@
#ifndef main_gard
#define main_gard
#include<SDL2/SDL.h>
#include <SDL2/SDL.h>
#include "kruskal.h"
#include <math.h>
#include "affichage_image.h"
#include "dijsktra.h"
#include "dijkstra.h"
#include "texture_perso.h"
#include "colision.h"
#include "DFS.h"
#include<SDL2/SDL_ttf.h>
#include <SDL2/SDL_ttf.h>
#include "affichage_txt.h"
#include "main.h"
void end_sdl(char ok,char const* msg,SDL_Window* window, SDL_Renderer* renderer);
void afficheEcranIntelligement(SDL_Renderer*,int**,int);
......
......@@ -2,8 +2,8 @@ CC= gcc
OPT = -c -Wextra -Wall -g
SDL2 = -lSDL2 -lSDL2_gfx -lSDL2_image -lSDL2_ttf -lSDL2_mixer -lSDL2_net -lm
prog : kruskal.o graph_aretes_tab.o partition.o tas.o main.o affichage_image.o dijkstra.o texture_perso.o colision.o DFS.o
$(CC) kruskal.o graph_aretes_tab.o partition.o tas.o main.o affichage_image.o dijkstra.o texture_perso.o colision.o DFS.o $(SDL2) -o prog
prog : kruskal.o graph_aretes_tab.o partition.o tas.o main.o affichage_image.o dijkstra.o texture_perso.o colision.o DFS.o affichage_txt.o
$(CC) kruskal.o graph_aretes_tab.o partition.o tas.o main.o affichage_image.o dijkstra.o texture_perso.o colision.o DFS.o affichage_txt.o $(SDL2) -o prog
main.o : main.c main.h
$(CC) $(OPT) main.c
......@@ -35,5 +35,8 @@ colision.o : colision.c colision.h
DFS.o : DFS.c DFS.h
$(CC) $(OPT) DFS.c
affichage_txt.o : affichage_txt.c affichage_txt.h
$(CC) $(OPT) affichage_txt.c
clear :
rm *.o
No preview for this file type
This diff is collapsed.