Commit 2a2e534c authored by Thibaud LEROY's avatar Thibaud LEROY
Browse files

ajout d'un renderer clear

parent a36a6594
No preview for this file type
......@@ -2,7 +2,7 @@
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include "kruskal.h"
#include "affichage_image.h"
/*
*chargement des textures
......@@ -177,6 +177,76 @@ void afficherImage(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taill
}
}
}
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);
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 afficherImageBrouillard(SDL_Renderer *renderer,SDL_Window *window,int **tab,int taille_cell,SDL_Texture* texture, int pos_x, int pos_y)
{
int i1,j1,j;
int * voisin = malloc(9*sizeof(int));
int x=pos_x/taille_cell;
int y=pos_y/taille_cell;
for (j=0;j<9;j++)
voisin[j]=-1;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
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;
}
}
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)
{
......
#ifndef _gard_affichage_image
#define _gard_affichage_image
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 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 "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 affichage_fin(SDL_Texture *my_texture, SDL_Window *window, SDL_Renderer *renderer, int coord_colonne, int coord_ligne, int taille_cell);
void point_minimap(SDL_Renderer *renderer, int taille_cell, int pos_x, int pos_y);
void afficherImageBrouillard(SDL_Renderer *renderer, SDL_Window *window, int **tab, int taille_cell, SDL_Texture *texture, int pos_x, int pos_y);
void peindreMap(SDL_Texture *texture, SDL_Window *window, SDL_Renderer *renderer, int noeuds, int taille_cell, int **laby);
#endif
No preview for this file type
#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
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -78,7 +78,10 @@ int main(int argc, char **argv)
{
double p;
SDL_Rect source = {0};
const int FPS = 60;
const int frameDelay = 1000 / FPS;
Uint32 frameStart;
int frameTime;
//l'argument p regit l'affichage des murs : s'il vaut 0 il n'y a aucun mur, s'il vaut un on a un arbre
if (argc == 2)
p = atof(argv[1]);
......@@ -179,25 +182,38 @@ int main(int argc, char **argv)
int hauteur_elve = source.h * zoom;
int pause = 1;
int pause = 0;
int temps = 1;
int noeud_dep = rand() % TAILLE;
int right = 1;
int pos_x = noeud_dep % P * taille_cell;
int pos_y = noeud_dep / P * taille_cell;
int temps = 1;
int deplacement = taille_cell;
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 still = 0;
int affiche_carte = 0;
int dijkstra_ok;
int fin_mama = 0;
int relancer = 0;
int *minimap = malloc((TAILLE + 1) * sizeof(int));
for (int iter = 0; iter < TAILLE + 1; iter++)
minimap[iter] = 0;
affichage_fin(texture_fin2, window2, renderer2, noeud_arrive / P, noeud_arrive % P, taille_cell2);
printf("l : relancer \np,SPACE : pause \ncroix : quitter\n");
while ((running) || (temps < 50))
printf("l : relancer \np,SPACE : pause \ncroix : quitter \nm : afficher la carte complète du labyrinthe pendant 2 secondes\n");
while (running)
{
noeud_actuel=pos_x/taille_cell+(pos_y/taille_cell)*P;
frameStart = SDL_GetTicks();
noeud_actuel = pos_x / taille_cell + (pos_y / taille_cell) * P;
minimap[0]++;
minimap[minimap[0]] = noeud_actuel;
if (noeud_actuel == noeud_arrive)
running = 0;
fin_mama = 1;
while (SDL_PollEvent(&event))
{
switch (event.type)
......@@ -210,7 +226,6 @@ int main(int argc, char **argv)
{
case SDL_WINDOWEVENT_CLOSE:
running = 0;
temps = 1000;
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
screen.w = event.window.data1;
......@@ -224,24 +239,19 @@ int main(int argc, char **argv)
switch (event.key.keysym.sym)
{
case SDLK_l:
noeud_dep = rand() % TAILLE;
right = 1;
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 = 1 - pause;
pause = !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;
}
......@@ -250,11 +260,54 @@ int main(int argc, char **argv)
break;
}
}
const Uint8 *keystates = SDL_GetKeyboardState(NULL);
if (pause)
/* if (dijkstra_ok)
{
tab_parents=dijkstra(tab,graph->noeuds,noeuds_arrive);
}
*/
if (affiche_carte && !pause)
{
peindreMap(texture2, window2, renderer2, noeud_actuel, taille_cell2, tab);
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;
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++)
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);
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, largeur_elve, deplacement);
......@@ -262,13 +315,22 @@ int main(int argc, char **argv)
{
if (right)
{
play_with_elve_N(texture_elve, texture, window, renderer, pos_x, pos_y, deplacement, zoom, tab, taille_cell, texture_fin, noeud_arrive);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_N(texture_elve, renderer, pos_x, pos_y, deplacement, zoom);
}
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);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_N(texture_elve_reverse, renderer, pos_x, pos_y, deplacement, zoom);
}
pos_y = pos_y - deplacement / 10; //deplacement de deplacement/nb_image_aniamtion a chaque frame
still = 0;
}
}
if ((keystates[SDL_SCANCODE_DOWN] || keystates[SDL_SCANCODE_S]))
......@@ -278,13 +340,22 @@ int main(int argc, char **argv)
{
if (right)
{
play_with_elve_S(texture_elve, texture, window, renderer, pos_x, pos_y, deplacement, zoom, tab, taille_cell, texture_fin, noeud_arrive);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_S(texture_elve, renderer, pos_x, pos_y, deplacement, zoom);
}
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);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_S_l(texture_elve_reverse, renderer, pos_x, pos_y, deplacement, zoom);
}
pos_y = pos_y + deplacement / 10;
still = 0;
}
}
if ((keystates[SDL_SCANCODE_LEFT] || keystates[SDL_SCANCODE_A]))
......@@ -292,9 +363,14 @@ int main(int argc, char **argv)
colli = collision_O(pos_x, pos_y, tab, taille_cell, hauteur_elve, deplacement);
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);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_O(texture_elve_reverse, renderer, pos_x, pos_y, deplacement, zoom);
pos_x = pos_x - deplacement / 10;
right = 0;
still = 0;
}
}
if ((keystates[SDL_SCANCODE_RIGHT] || keystates[SDL_SCANCODE_D]))
......@@ -302,30 +378,58 @@ int main(int argc, char **argv)
colli = collision_E(pos_x, pos_y, tab, taille_cell, largeur_elve, hauteur_elve, deplacement);
if (!colli)
{
play_with_elve_E(texture_elve, texture, window, renderer, pos_x, pos_y, deplacement, zoom, tab, taille_cell, texture_fin, noeud_arrive);
//afficherImage(renderer, window, tab, taille_cell, texture);
SDL_RenderClear(renderer);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_with_elve_E(texture_elve, renderer, pos_x, pos_y, deplacement, zoom);
pos_x = pos_x + deplacement / 10;
right = 1;
still = 0;
}
}
/*
else
{
if (right)
{
play_standstill(texture_elve, texture, window, renderer, pos_x, pos_y, zoom, tab, taille_cell, texture_fin, noeud_arrive);
}
else
if (still >= 10)
{
play_standstill_l(texture_elve_reverse, texture, window, renderer, pos_x, pos_y, zoom, tab, taille_cell, texture_fin, noeud_arrive);
if (right)
{
//afficherImage(renderer, window, tab, taille_cell, texture);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_standstill(texture_elve, renderer, pos_x, pos_y, zoom);
}
else
{
//afficherImage(renderer, window, tab, taille_cell, texture);
afficherImageBrouillard(renderer,window,tab,taille_cell,texture,pos_x,pos_y);
affichage_fin(texture_fin, window, renderer, noeud_arrive / P, noeud_arrive % P, taille_cell);
play_standstill_l(texture_elve_reverse, renderer, pos_x, pos_y, zoom);
}
}
still = (still + 1) % 11;
}
*/
SDL_RenderPresent(renderer);
SDL_Delay(30);
if (running == 0)
{
temps++;
}
}
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_RenderPresent(renderer);
frameTime = SDL_GetTicks() - frameStart;
if (frameDelay > frameTime)
{
SDL_Delay(frameDelay - frameTime);
}
}
......
......@@ -13,6 +13,7 @@
#include "colision.h"
#include "DFS.h"
#include<SDL2/SDL_ttf.h>
#include "affichage_txt.h"
void end_sdl(char ok,char const* msg,SDL_Window* window, SDL_Renderer* renderer);
void afficheEcranIntelligement(SDL_Renderer*,int**,int);
......
No preview for this file type
......@@ -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
......@@ -34,6 +34,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
our-awesome-project @ 8ebb2c87
Subproject commit 8ebb2c872be808eeda7470d86421b0d91822f721
No preview for this file type
No preview for this file type
No preview for this file type
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment