diff --git a/travail_de_groupe/jeu_appren_par_renfo/Res/Roboto-Black.ttf b/travail_de_groupe/jeu_appren_par_renfo/Res/Roboto-Black.ttf new file mode 100644 index 0000000000000000000000000000000000000000..86ec2b29ba56a3d6c45f1a8584ff3780fa70c60e Binary files /dev/null and b/travail_de_groupe/jeu_appren_par_renfo/Res/Roboto-Black.ttf differ diff --git a/travail_de_groupe/jeu_appren_par_renfo/Res/crowd.png b/travail_de_groupe/jeu_appren_par_renfo/Res/crowd.png new file mode 100755 index 0000000000000000000000000000000000000000..5fdf319af7f2949b5140ee9ddb7af70a0bb6c411 Binary files /dev/null and b/travail_de_groupe/jeu_appren_par_renfo/Res/crowd.png differ diff --git a/travail_de_groupe/jeu_appren_par_renfo/Res/net.png b/travail_de_groupe/jeu_appren_par_renfo/Res/net.png new file mode 100755 index 0000000000000000000000000000000000000000..f9394652290e8804fda47da8902c7fb4e1cea98e Binary files /dev/null and b/travail_de_groupe/jeu_appren_par_renfo/Res/net.png differ diff --git a/travail_de_groupe/jeu_appren_par_renfo/Res/player_sprite.png b/travail_de_groupe/jeu_appren_par_renfo/Res/player_sprite.png new file mode 100755 index 0000000000000000000000000000000000000000..8447510ba0888073e187f8a1859a2b325b00c0d4 Binary files /dev/null and b/travail_de_groupe/jeu_appren_par_renfo/Res/player_sprite.png differ diff --git a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.c b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.c index 9828fcaccd5460c0fdf0f38127e4925f02580ae1..0be83169cf086c36035b8877815bcbbfd5a772d1 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.c +++ b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.c @@ -1,7 +1,87 @@ #include "ball.h" -ball_t ball; +ball_t ball; +laGrange_t coefLagrange; +int trajectory[NUMBERPOINT][2]; -void initBall(){ +void initBall (){ + + +} + + +double f(int x, float xc, float yc, float xf, float yf, float xt, float yt){ + double returnValue = yf * ((x - xc)/(xf - xc)) * ((x - xt)/(xf - xt)) + yc * ((x - xf)/(xc - xf)) * ((x - xt)/(xc - xt)) + yt * ((x - xc)/(xt - xc)) * ((x - xf)/(xt - xf)); + return returnValue; +} + +void laGrange (float beta[3],float Z[3]){ // y , x + printf("x canon : %f, x chute : %f, x filet : %f\n", Z[0], Z[1], Z[2]); + printf("y canon : %f, y chute : %f, y filet : %f\n", beta[0], beta[1], beta[2]); + + coefLagrange.a= Z[0]/((beta[0]-beta[1])*(beta[0]-beta[2])) + +Z[1]/((beta[1]-beta[0])*(beta[1]-beta[2])) + +Z[2]/((beta[2]-beta[0])*(beta[2]-beta[1])); + + coefLagrange.b= -(Z[0]*(beta[1]+beta[2]))/((beta[0]-beta[1])*(beta[0]-beta[2])) + -(Z[1]*(beta[0]+beta[2]))/((beta[1]-beta[0])*(beta[1]-beta[2])) + -(Z[2]*(beta[0]+beta[1]))/((beta[2]-beta[0])*(beta[2]-beta[1])); + + coefLagrange.c= (Z[0]*beta[1]*beta[2])/((beta[0]-beta[1])*(beta[0]-beta[2])) + +(Z[1]*beta[0]*beta[2])/((beta[1]-beta[0])*(beta[1]-beta[2])) + +(Z[2]*beta[0]*beta[1])/((beta[2]-beta[0])*(beta[2]-beta[1])); +} + +float defineAngle (canon_t canon, int xDropPoint, int yDropPoint){ + float distance; + float angleSin; + distance= sqrtf( powf((float)(xDropPoint-canon.x),2)+powf((float)(yDropPoint-canon.y),2)); + angleSin = asinf(distance/(xDropPoint-canon.x)); + return angleSin; +} + + +void calculationTrajectory(canon_t canon, int xDropPoint, int yDropPoint){ + int i; + float setUp[2][3]; + float distance= (float)(yDropPoint-canon.y); + + + printf("test : %f\n", y); + + trajectory[0][0]=canon.y; + trajectory[0][1]=canon.x; + + trajectory[NUMBERPOINT-1][0]=yDropPoint; + trajectory[NUMBERPOINT-1][1]=xDropPoint; + + setUp[1][0]=(float)canon.y; + setUp[0][0]=canon.x; + setUp[1][1]=(float)yDropPoint; + setUp[0][1]=xDropPoint; + //setUp[0][2]=(13.40/2);// distance du filet dans le repère de frappe + //filet X + //terrain.w = 5.20 * zoom; + // setUp[0][2] = terrain.x + terrain.w + 50 + (13.40 * zoom)/2 -2; + setUp[0][2] = 50 + 5.20 * 40 + 50 + (13.40 * 40)/2 - 2; + + //filet Y + // drawerTerrainSideView.y - 1,55 * zoom + // drawerTerrainSideView.y = terrain.y + terrain.h; + //terrain.h = 13.40 * zoom + //setUp[1][2] = 1.55*40; /// hauteur du filet + setUp[1][2] = 50 + 13.40 * 40 - 3.55 * 40; + laGrange(setUp[1],setUp[0]); //laGrange(y, x); + + //printf("coef a:%f; coef b:%f; coef c:%f\n", coefLagrange.a, coefLagrange.b, coefLagrange.c); + + float step= distance / (NUMBERPOINT-1); + + for (i=1; i<NUMBERPOINT-1; i++){ + trajectory[i][0]=(int)((float)canon.y + i*step); + trajectory[i][1]=(int)(coefLagrange.c+ + coefLagrange.b*((float)canon.y + i*step)+ + coefLagrange.a*(powf((float)canon.y + i*step,2))); + } } \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.h b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.h index 5938cb0d5d7b04e24bfd6d3e4646187362b7c3a7..334afad9b47511dbdfb3af4a56d71673e2aba21a 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.h +++ b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/ball.h @@ -1,13 +1,40 @@ #ifndef BALL_H #define BALL_H -typedef struct ball{ - int x; - int y; -} ball_t; +#include <stdlib.h> +#include <time.h> +#include <stdio.h> +#include <math.h> +#include "canon.h" + +#define NUMBERPOINT 200 + +typedef enum { + false,true +}booleen_t; + +typedef struct ball { + int x; + int y; + int z; + float angle; + float speed; +}ball_t; + +typedef struct laGrange{ + float a; + float b; + float c; +}laGrange_t; extern ball_t ball; +extern laGrange_t coefLagrange; +extern int trajectory[NUMBERPOINT][2]; -void initBall(); +void initBall(); +void laGrange(float *, float *); +float defineAngle(canon_t, int, int); +//void calculationTrajectory(canon_t, int, int, int, int); +void calculationTrajectory(canon_t, int, int); -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/canon.c b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/canon.c index e0041d2e1ef82b6f779cade895bde4309c50fb2d..95c0e1f9db7e9ee25b6dd7aab4d71c40b31c0e88 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/canon.c +++ b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/canon.c @@ -8,23 +8,21 @@ void initCanon(){ canon.length = 20; canon.x = 100; canon.y = 20; - canon.zone = 1; + canon.zone = -1; } int getZone(int terrainX, int terrainY, int terrainW, int terrainH){ int z = -1; - int xCanonTopView = terrainX + canon.x; - int yCanonTopView = terrainY + canon.y; //en haut à gauche => 1 - if(canon.x >= 0 && canon.x<terrainW/2 && yCanonTopView>=0 && canon.y<terrainH/4){ + if(canon.x<terrainW/2 && canon.y<terrainH/4){ z = 1; } //en haut à droite => 2 - else if(xCanonTopView>=terrainX+terrainW/2 && xCanonTopView<=terrainX+terrainW && yCanonTopView>=terrainY && yCanonTopView<terrainX+terrainH/4){ + else if(canon.x>=terrainW/2 && canon.y<terrainH/4){ z = 2; } //en bas à gauche => 3 - else if(xCanonTopView >= terrainX && xCanonTopView<terrainX+terrainW/2 && yCanonTopView>=terrainY+terrainH/4 && yCanonTopView<=terrainH+terrainH/2){ + else if(canon.x<terrainW/2 && canon.y>=terrainH/4){ z = 3; } //en bas à droite => 4 diff --git a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/gest_event.c b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/gest_event.c index 98c449f39feda6a738c608b2af8053b52a84f04c..41ac58e9b55ae21db50f298ab2ae5398d9958615 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/gest_event.c +++ b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/gest_event.c @@ -37,7 +37,7 @@ void manageGame(){ case SDLK_r: newCanon(); - zone_canon = getZone(terrain.x, terrain.y, terrain.w, terrain.h); + canon.zone = getZone(terrain.x, terrain.y, terrain.w, terrain.h); break; default: diff --git a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/render.c b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/render.c index 3bbe9889ba8dd240dc6d3040753f1e023c732ae7..0a682387ac387f6f0700f95a44fbee409cee7740 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/render.c +++ b/travail_de_groupe/jeu_appren_par_renfo/antoi_render_2d/source/render.c @@ -8,16 +8,22 @@ int window_height = 700; TTF_Font *robotoFont; int zoom = 40; +//terrain devrait etre un .c et .h comme canon +// on aurait un SDL_Rect en terrain TopView et SideView +//là mélange bizarre mais fonctionnel SDL_Rect terrain; +SDL_Rect drawerTerrainSideView; // drawerTerrain pour les deux vues SDL_Rect filet; -SDL_Rect canon_rect; -SDL_Rect point_de_chute; +//drawerFilet + +//canon est un .c +SDL_Rect drawerCanon; -int point_x_rand; -int point_y_rand; +//point de chute devrait etre un .c et .h +SDL_Rect point_de_chute; +SDL_Rect drawerPointDeChute; -int zone_canon = -1; int zone_chute = -1; void createWindow(){ @@ -64,41 +70,37 @@ void initTerrain(){ } void initPointDeChute(){ - initTerrain(); + point_de_chute.w = 5; + point_de_chute.h = 5; srand(time(NULL)); - point_x_rand = (int)rand()%terrain.w; - point_y_rand = (int)rand()%(terrain.h/2); + point_de_chute.x = (int)rand()%terrain.w; + point_de_chute.y = (int)rand()%(terrain.h/2); } - int getZoneChute(int terrainX, int terrainY, int terrainW, int terrainH){ int z = -1; // pdc = point de chute - int pdc_x = terrainX + point_x_rand; - int pdc_y = terrainY + terrainH/2 + point_y_rand; + int pdc_y = terrainY + terrainH/2 + point_de_chute.y; //en haut à gauche => 1 - if(point_x_rand>=0 && point_x_rand<terrainW/2 && pdc_y<terrainY+(3*terrainH)/4){ + if(point_de_chute.x<terrainW/2 && pdc_y<terrainY+(3*terrainH)/4){ z = 1; } //en haut à droite => 2 - /*else if(){ + else if(point_de_chute.x>=terrainW/2 && pdc_y<terrainY+(3*terrainH)/4){ z = 2; } //en bas à gauche => 3 - else if(point_x_rand>=0 && point_x_rand<terrainW/2 && ){ + else if(point_de_chute.x<terrainW/2 && pdc_y>=terrainY+(3*terrainH)/4){ z = 3; } //en bas à droite => 4 - */else{ - z = -6; + else{ + z = 4; } - printf("point_x_rand : %d, terrainW/2: %d\n", point_x_rand, terrainW/2); - printf("pdc_x : %d, pdc_y : %d, tX : %d, tY : %d\n", pdc_x, pdc_y, terrainX, terrainY); return z; } void newCanon(){ - initTerrain(); srand(time(NULL)); canon.x = (int)rand()%terrain.w; canon.y = (int)rand()%(terrain.h/2); @@ -115,9 +117,6 @@ void drawString(char *text, int x, int y, int w, int h, int r, int g, int b, int } void drawTerrainTopView(){ - //terrain - initTerrain(); - //filet filet.x = terrain.x; filet.h = 4; @@ -148,20 +147,20 @@ void drawTerrainTopView(){ void drawTerrainSideView(){ //terrain - terrain.x = terrain.x + terrain.w + 50; - terrain.y = terrain.y + terrain.h; - terrain.h = 4; - terrain.w = 13.40 * zoom; + drawerTerrainSideView.x = terrain.x + terrain.w + 50; + drawerTerrainSideView.y = terrain.y + terrain.h; + drawerTerrainSideView.h = 4; + drawerTerrainSideView.w = 13.40 * zoom; //filet filet.w = 4; - filet.x = terrain.x + terrain.w/2 - filet.w/2; + filet.x = drawerTerrainSideView.x + drawerTerrainSideView.w/2 - filet.w/2; filet.h = -1.55 * zoom; - filet.y = terrain.y; + filet.y = drawerTerrainSideView.y; //terrain en blanc SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderFillRect(renderer, &terrain); + SDL_RenderFillRect(renderer, &drawerTerrainSideView); //filet en vert SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); @@ -170,50 +169,50 @@ void drawTerrainSideView(){ void drawCanonTopView(){ //canon - canon_rect.w = canon.width; - canon_rect.h = canon.length; - canon_rect.x = terrain.x + canon.x; - canon_rect.y = terrain.y + canon.y; + drawerCanon.w = canon.width; + drawerCanon.h = canon.length; + drawerCanon.x = terrain.x + canon.x; + drawerCanon.y = terrain.y + canon.y; //canon en noir SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderFillRect(renderer, &canon_rect); + SDL_RenderFillRect(renderer, &drawerCanon); } void drawPointDeChuteTopView(){ //point de chute de la balle - point_de_chute.w = 5; - point_de_chute.h = 5; - point_de_chute.x = (int)terrain.x + point_x_rand; - point_de_chute.y = (int)terrain.y + terrain.h/2 + point_y_rand; + drawerPointDeChute.x = terrain.x + point_de_chute.x; + drawerPointDeChute.y = terrain.y + terrain.h/2 + point_de_chute.y; + drawerPointDeChute.w = point_de_chute.w; + drawerPointDeChute.h = point_de_chute.h; //point de chute de la balle SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - SDL_RenderFillRect(renderer, &point_de_chute); + SDL_RenderFillRect(renderer, &drawerPointDeChute); } void drawCanonSideView(){ //canon - canon_rect.w = canon.length; - canon_rect.h = canon.height; - canon_rect.x = terrain.x + canon.y; - canon_rect.y = terrain.y - canon.height; + drawerCanon.w = canon.length; + drawerCanon.h = canon.height; + drawerCanon.x = drawerTerrainSideView.x + canon.y; + drawerCanon.y = drawerTerrainSideView.y - canon.height; //canon en noir SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderFillRect(renderer, &canon_rect); + SDL_RenderFillRect(renderer, &drawerCanon); } void drawPointDeChuteSideView(){ //point de chute de la balle - point_de_chute.w = 5; - point_de_chute.h = 5; - point_de_chute.x = terrain.x + terrain.w/2 + point_y_rand; - point_de_chute.y = terrain.y; + drawerPointDeChute.x = drawerTerrainSideView.x + drawerTerrainSideView.w/2 + point_de_chute.y; + drawerPointDeChute.y = drawerTerrainSideView.y; + drawerPointDeChute.w = point_de_chute.w; + drawerPointDeChute.h = point_de_chute.h; //point de chute de la balle SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - SDL_RenderFillRect(renderer, &point_de_chute); + SDL_RenderFillRect(renderer, &drawerPointDeChute); } void drawInformations(){ @@ -229,14 +228,33 @@ void drawInformations(){ strcat(zoneChuteChaine, str2); drawString(zoneChuteChaine, window_width-texte_width, texte_height*2, texte_width, texte_height, 255, 255, 255, 255); drawString("r : new canon", window_width-texte_width, texte_height*4, texte_width, texte_height, 255, 255, 255, 255); - sprintf(str, "%d", zone_canon); + sprintf(str, "%d", canon.zone); strcat(zoneCanonChaine, str); drawString(zoneCanonChaine, window_width-texte_width, texte_height*5, texte_width, texte_height, 255, 255, 255, 255); } void drawTrajectoireTopView(){ SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - SDL_RenderDrawLine(renderer, terrain.x+canon.x, terrain.y+canon.y, point_de_chute.x, point_de_chute.y); + SDL_RenderDrawLine(renderer, terrain.x+canon.x, terrain.y+canon.y, drawerPointDeChute.x, drawerPointDeChute.y); +} + +void drawTrajectoireSideView(){ + canon_t canon_trajectoire_sideview; + canon_trajectoire_sideview.y = drawerTerrainSideView.x + canon.y; + canon_trajectoire_sideview.x = drawerTerrainSideView.y; + + drawerPointDeChute.y = terrain.y + terrain.h; + + calculationTrajectory(canon_trajectoire_sideview, + drawerPointDeChute.y, + drawerPointDeChute.x + ); + + for(int i=0; i<NUMBERPOINT-1; i++){ + //printf("point %d (%d, %d)\n", i, trajectory[i][0], trajectory[i][1]); + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + SDL_RenderDrawLine(renderer, trajectory[i][0], trajectory[i][1], trajectory[i+1][0], trajectory[i+1][1]); + } } void drawBall(){ @@ -261,11 +279,7 @@ void drawBall(){ } //draw hero mana - SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - for(i=0; i<hero.mana; i++){ - SDL_RenderFillRect(renderer, &rect_mana); - rect_mana.x = rect_mana.x + rect_mana.w + 20; - }*/ + */ } @@ -273,6 +287,7 @@ void drawBall(){ void mainLoop(){ createWindow(); initCanon(); + initTerrain(); initPointDeChute(); pthread_t eventThread; @@ -290,14 +305,14 @@ void mainLoop(){ drawCanonTopView(); drawPointDeChuteTopView(); drawTrajectoireTopView(); - initTerrain(); - zone_canon = getZone(terrain.x, terrain.y, terrain.w, terrain.h); + canon.zone = getZone(terrain.x, terrain.y, terrain.w, terrain.h); zone_chute = getZoneChute(terrain.x, terrain.y, terrain.w, terrain.h); //side view drawTerrainSideView(); drawCanonSideView(); drawPointDeChuteSideView(); + drawTrajectoireSideView(); // drawInformations(); diff --git a/travail_de_groupe/jeu_appren_par_renfo/map.txt b/travail_de_groupe/jeu_appren_par_renfo/map.txt index d6e01f65207317b588d271d05e59c8c595a4dd00..d5439ec567915a31da80250987d35294005debe3 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/map.txt +++ b/travail_de_groupe/jeu_appren_par_renfo/map.txt @@ -1,17 +1,17 @@ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c index aefb6558ba9509d558cb62a57d485f09e56fa6f2..d8b9658623878b099f1d3e4a25c7c9fe92965991 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.c @@ -1,6 +1,16 @@ #include "gest_event.h" +int Keys[NB_KEYS]; + +void initKeys(){ + int i; + for(i = 0; i < NB_KEYS; i++) + { + Keys[i] = 0; + } +} + void gestMenu(){ SDL_Event event; while (SDL_PollEvent(&event)){ @@ -30,11 +40,101 @@ void gestMenu(){ } + +void gestGame(){ + SDL_Event event; + while (SDL_PollEvent(&event)){ + switch(event.type) + { + case SDL_QUIT: + running = 0; + break; + + case SDL_MOUSEMOTION: + if (event.motion.xrel > 0){ + player.angle += 0.01; + if (player.angle > 2*pi) player.angle -= 2*pi; + } + if (event.motion.xrel < 0){ + player.angle -= 0.01; + if (player.angle < 0) player.angle += 2*pi; + } + if (event.motion.yrel > 0){ + player.viewAngle -= 1; + } + if (event.motion.yrel < 0){ + player.viewAngle += 1; + } + player.deltax = cos(player.angle); + player.deltay = sin(player.angle); + + case SDL_KEYUP: + switch (event.key.keysym.sym) + { + case SDLK_ESCAPE: + running = 0; + continue; + + case SDLK_z: + Keys[2] = 1; + continue; + + case SDLK_d: + Keys[3] = 1; + continue; + + case SDLK_s: + Keys[0] = 1; + continue; + + case SDLK_q: + Keys[1] = 1; + continue; + + + default: + break; + } + + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_z: + Keys[2] = 0; + continue; + + case SDLK_d: + Keys[3] = 0; + continue; + + case SDLK_s: + Keys[0] = 0; + continue; + + case SDLK_q: + Keys[1] = 0; + continue; + + default: + break; + } + + default: + break; + + + } + } + managePlayer(); + SDL_Delay(5); +} + + void *EventLoop(void *arg){ while(running){ switch(game_state){ case MENU : gestMenu();break; - case GAME : gestMenu();break; + case GAME : gestGame();break; default:printf("game state fault");break; } } diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.h b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.h index 6b2a7ad2c07d35b89720c64e22dd00e0888b43d1..9256d7dd965c715db4fa68367bbcd0e589793475 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/gest_event.h @@ -5,6 +5,11 @@ #include "main.h" #include "render.h" +#define NB_KEYS 10 + +extern int Keys[NB_KEYS]; + void *EventLoop(void *arg); +void initKeys(); #endif \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/main.c b/travail_de_groupe/jeu_appren_par_renfo/src/main.c index 84d20974b1956ce17f5d74a5070e01967eb051ff..37ab94803c41d82953c7ea3e7f2f57246d7df6f7 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/main.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/main.c @@ -11,7 +11,9 @@ int main(){ running = 1; game_state = GAME; readMapFromFile("map.txt"); - printMap(); + //printMap(); + initPlayer(); + initKeys(); mainLoop(); diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.c b/travail_de_groupe/jeu_appren_par_renfo/src/player.c index a013abfb1568f7c768d80ccb94b1b49829be846d..ac6ae73c79707739665d2a4f3023a9c64d188ced 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/player.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.c @@ -2,13 +2,47 @@ player_t player; +player_t ennemy; + +player_t ball; + void initPlayer(){ - player.x= 8*BLOCK_SIZE; - player.y= 8*BLOCK_SIZE; + player.x= 6*BLOCK_SIZE; + player.y= 6*BLOCK_SIZE; + player.h = 2 * BLOCK_SIZE; + player.w = 2 * BLOCK_SIZE; + ennemy.h = 2 * BLOCK_SIZE; + ennemy.w = 2 * BLOCK_SIZE; + ennemy.x = 20 * BLOCK_SIZE; + ennemy.y = 15 * BLOCK_SIZE; + ball.h = 0.5 * BLOCK_SIZE; + ball.w = 0.5 * BLOCK_SIZE; + ball.x = 10 * BLOCK_SIZE; + ball.y = 10 * BLOCK_SIZE; player.speed = 1; player.isMoving = 0; player.HPMax = 3; player.currentHP = player.HPMax; player.coins = 0; player.angle=0; + player.deltax = 0; + player.deltay = 1; + player.viewAngle = 0; +} + +void manageMovement(){ + float x_increment = (Keys[0] - Keys[2]) * player.deltax + (Keys[3] - Keys[1]) * sin(player.angle); + float y_increment = (Keys[0] - Keys[2]) * player.deltay + (Keys[1] - Keys[3]) * cos(player.angle); + float newpos_x = (player.x + x_increment)/BLOCK_SIZE; + float newpos_y = (player.y + y_increment)/BLOCK_SIZE; + if (newpos_x >= 0 && newpos_x < MAP_WIDTH && newpos_y >= 0 && newpos_y < MAP_HEIGHT){ + if (map[(int)newpos_y][(int)newpos_x] != 1){ + player.x += x_increment * MOVEMENT_SPEED; + player.y += y_increment * MOVEMENT_SPEED; + } + } +} + +void managePlayer(){ + manageMovement(); } \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/player.h b/travail_de_groupe/jeu_appren_par_renfo/src/player.h index d145184f083ea77030c47e489ec1595d8b861c79..947f3bccced758a487bd020200baf68c3c884a8e 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/player.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/player.h @@ -2,15 +2,19 @@ #define PLAYER_H #include "map.h" +#include "render.h" #define ENTITIES_UP 0 #define ENTITIES_DOWN 1 #define ENTITIES_LEFT 2 #define ENTITIES_RIGHT 3 +#define MOVEMENT_SPEED 2 typedef struct player{ - int x; - int y; + float x; + float y; + int h; + int w; int speed; int isMoving; int direction; @@ -18,10 +22,16 @@ typedef struct player{ int currentHP; int coins; float angle; + float deltax; + float deltay; + float viewAngle; } player_t; extern player_t player; +extern player_t ennemy; +extern player_t ball; void initPlayer(); +void managePlayer(); #endif \ No newline at end of file diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/render.c b/travail_de_groupe/jeu_appren_par_renfo/src/render.c index 72fb75b2953e12691d146bd754c51025a2ca333e..af7310fc1b5fbd5a6f330984103aeee4bcf8c3a7 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/render.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/render.c @@ -6,15 +6,85 @@ SDL_Renderer *renderer; TTF_Font *RobotoFont; SDL_DisplayMode screenDimension; +SDL_Rect destRect; SDL_Rect rect; SDL_Rect sky; SDL_Rect ground; -// ray casting variables -float htexture; -int r, mx, my, dof; -double rx, ry, xo, yo, distT; -double ra; +SDL_Texture * netTexture; +SDL_Texture * crowdTexture; +SDL_Texture * playerTexture; + +int ** rays; +int raysListLength = 0; + + +rayInfo_t raysListHead; + +float fps; + +SDL_Texture * loadTexture(char * path) { + SDL_Surface * surface = IMG_Load(path); + SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + return texture; +} + +void addRayInfoToList(rayInfo_t * rayInfoHead, rayInfo_t * rayInfo) { + rayInfo->next = rayInfoHead->next; + rayInfoHead->next = rayInfo; +} + +void freeRayInfoList(rayInfo_t * rayInfoHead) { + rayInfo_t * rayInfo = rayInfoHead->next; + while (rayInfo != NULL) { + //printf("freeing : %p\n", rayInfo); + rayInfo_t * next = rayInfo->next; + free(rayInfo); + rayInfo = next; + } +} + +rayInfo_t * allocRayInfo(float ra, float distT, int r, int isTransparent, int direction, float htexture){ + rayInfo_t * rayInfo = malloc(sizeof(rayInfo_t)); + if (rayInfo == NULL) { + printf("Error: malloc failed\n"); + exit(1); + } + rayInfo->ra = ra; + rayInfo->distT = distT; + rayInfo->r = r; + rayInfo->isTransparent = isTransparent; + rayInfo->direction = direction; + rayInfo->htexture = htexture; + rayInfo->next = NULL; + return rayInfo; +} + +void initRays(){ + int i; + rays = malloc(sizeof(int*) * 2 * NB_RAYS); + for (i = 0; i < NB_RAYS * 2; i++){ + rays[i] = malloc(sizeof(int) * 2); + } +} + +void addRayToList(int x, int y){ + if (raysListLength < 2 * NB_RAYS){ + *rays[raysListLength] = x; + *(rays[raysListLength] + 1) = y; + raysListLength++; + } +} + +void resetRayList(){ + int i; + for (i = 0; i < 2 * NB_RAYS; i++){ + *rays[i] = 0; + *(rays[i] + 1) = 0; + } + raysListLength = 0; +} // end ray casting variables @@ -58,15 +128,83 @@ void endSDL(){ SDL_Quit(); } -void drawRays(int map[][MAP_WIDTH]){ + +void drawRayColumn(rayInfo_t * rayInfo){ + float ca = player.angle - rayInfo->ra; + if (ca < 0) ca += 2*pi; + if (ca > 2*pi) ca -= 2*pi; + rayInfo->distT = rayInfo->distT * cos(ca); + + float lineH = (screenDimension.h/2)/rayInfo->distT; + rect.x = rayInfo->r; + rect.y = (screenDimension.h/2 + player.viewAngle) - lineH; + rect.w = 1; + rect.h = (2 * screenDimension.w * lineH/20); + + destRect.x = rayInfo->htexture; + destRect.y = 0; + destRect.w = 1; + destRect.h = 64; + + if (rayInfo->isTransparent){ + rect.h *= 1.75; + rect.y -= rect.h/3; + SDL_RenderCopy(renderer, netTexture, &destRect, &rect); + + } + else { + destRect.x += + 64 * (SDL_GetTicks()/200 % 4); + if (rayInfo->direction){ + SDL_RenderCopy(renderer, crowdTexture, &destRect, &rect); + } + else { + SDL_RenderCopy(renderer, crowdTexture, &destRect, &rect); + } + } +} + +void drawVerticalRays(){ + rayInfo_t * current = raysListHead.next; + while (current != NULL){ + //printf("%p\n", current); + if (current->direction){ + drawRayColumn(current); + } + current = current->next; + } +} + +void drawHorizentalRays(){ + rayInfo_t * current = raysListHead.next; + while (current != NULL){ + //printf("%p\n", current); + if (!current->direction){ + drawRayColumn(current); + } + current = current->next; + } +} + +void castRays(int map[][MAP_WIDTH]){ + // ray casting variables + float htexture, htexture2; + int r, mx, my, dof; + double rx, ry, rx2, ry2, xo, yo, distT, distT2; + double ra; + mx = 0; + my = 0; + resetRayList(); + freeRayInfoList(&raysListHead); + raysListHead.next = NULL; ra = player.angle - DR * FOV_ANGLE/4; if (ra < 0) ra -= 2*pi; if (ra > 2*pi) ra -= 2*pi; for (r = 0; r<NB_RAYS; r++){ // check horizontal rays - printf("ray %d\n", r); + //int foundTransparentWallH = 0; + int foundSolidWallH = 0; dof = 0; - float disH = 100000, hx = player.x, hy = player.y; + float disH = 100000, disH2 = 100000, hx = player.x, hy = player.y , hx2 = player.x, hy2 = player.y; float aTan = -1/tan(ra); if (ra > pi){ // looking up ry = (((int)player.y>>6)<<6) - 0.0001; @@ -94,6 +232,16 @@ void drawRays(int map[][MAP_WIDTH]){ hy = ry; disH = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); dof = DOF; + foundSolidWallH = 1; + } + else { + hx2 = rx; + hy2 = ry; + disH2 = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); + //foundTransparentWallH = 1; + dof++; + rx += xo; + ry += yo; } } else { @@ -103,11 +251,11 @@ void drawRays(int map[][MAP_WIDTH]){ } } - printf("hx %f hy %f\n", hx, hy); + //printf("hx %f hy %f\n", hx, hy); // check vertical rays dof = 0; - float disV = 100000, vx = player.x, vy = player.y; + float disV = 100000, disV2 = 100000 , vx = player.x, vy = player.y, vx2, vy2; float nTan = -tan(ra); if (ra > pi/2 && ra < 3*pi/2){ // looking left rx = (((int)player.x>>6)<<6) - 0.0001; @@ -121,21 +269,33 @@ void drawRays(int map[][MAP_WIDTH]){ xo = BLOCK_SIZE; yo = -xo*nTan; } - if (ra == pi || ra == 0){ // looking horizontally - rx = player.x; + if (ra == pi || ra == 0){ ry = player.y; + rx = player.x; dof = DOF; } + int foundSolidWallV = 0; + int foundTransparentWallV = 0; while (dof < DOF){ mx = (int)rx>>6; my = (int)ry>>6; - if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT){ + if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT && map[my][mx]){ if (map[my][mx] == 1){ vx = rx; vy = ry; disV = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); + foundSolidWallV = 1; dof = DOF; } + else { + vx2 = rx; + vy2 = ry; + disV2 = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); + foundTransparentWallV = 1; + dof++; + rx += xo; + ry += yo; + } } else { rx += xo; @@ -144,59 +304,196 @@ void drawRays(int map[][MAP_WIDTH]){ } } - printf("vx %f vy %f\n", vx, vy); - - if (disH < disV) { - rx = hx; - ry = hy; - distT = disH; + int direction, direction2; + + if (foundTransparentWallV){ + if (disH < disV2){ + rx = hx2; + ry = hy2; + distT = disH2; + direction = 0; + htexture = (int)(rx)%BLOCK_SIZE; + } + else { + rx = vx2; + ry = vy2; + distT = disV2; + direction = 1; + htexture = (int)(ry)%BLOCK_SIZE; + } + if (foundSolidWallV){ + if (disH < disV){ + rx2 = hx; + ry2 = hy; + distT2 = disH; + direction2 = 0; + htexture2 = (int)(rx2)%BLOCK_SIZE; + } + else { + rx2 = vx; + ry2 = vy; + distT2 = disV; + direction2 = 1; + htexture2 = (int)(ry2)%BLOCK_SIZE; + } + } + if (foundSolidWallH){ + if (disH < disV){ + rx2 = hx; + ry2 = hy; + distT2 = disH; + direction2 = 0; + htexture2 = (int)(rx2)%BLOCK_SIZE; + } + else { + rx2 = vx; + ry2 = vy; + distT2 = disV; + direction2 = 1; + htexture2 = (int)(ry2)%BLOCK_SIZE; + } + } } + else { - rx = vx; - ry = vy; - distT = disV; + if (disH < disV) { + rx = hx; + ry = hy; + distT = disH; + direction = 0; + htexture = (int)(rx)%BLOCK_SIZE; + } + else { + rx = vx; + ry = vy; + distT = disV; + direction = 1; + htexture = (int)(ry)%BLOCK_SIZE; + } } + + ra = ra + ANGLE_INC/2; if (ra > 2*pi) ra -= 2*pi; if (ra < 0) ra += 2*pi; - // draw column - float ca = player.angle - ra; - if (ca < 0) ca += 2*pi; - if (ca > 2*pi) ca -= 2*pi; - distT = distT * cos(ca); - float lineH = (screenDimension.h/2)/distT; + // draw ray + rayInfo_t * column = allocRayInfo(ra, distT, r, foundTransparentWallV, direction, htexture); + addRayInfoToList(&raysListHead, column); + if (foundTransparentWallV){ + if (foundSolidWallV){ + rayInfo_t * column = allocRayInfo(ra, distT2, r, 0, direction2 , htexture2); + addRayInfoToList(&raysListHead, column); + } + else { + rayInfo_t * column = allocRayInfo(ra, distT2, r, 0, direction, htexture2); + addRayInfoToList(&raysListHead, column); + } + } + // draw the ray in the minimap + addRayToList(rx, ry); + addRayToList(rx2, ry2); - rect.x = r; - rect.y = screenDimension.h/2 - lineH; - rect.w = 1; - rect.h = (int)(2 * screenDimension.h * lineH/200); + } +} - if (disH < disV) { - SDL_SetRenderDrawColor(renderer, 255, rand() % 255, 0, 255); - } - else { - SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); - } - SDL_RenderFillRect(renderer, &rect); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); +void drawEnnemy(){ + float ennemyAngle = atan2((ennemy.y + ennemy.w/2) - (player.y + player.w/2) , (ennemy.x + ennemy.w/2) - (player.x + player.w/2)); + if (ennemyAngle < 0) ennemyAngle += 2*pi; + if (ennemyAngle > 2*pi) ennemyAngle -= 2*pi; + float ennemyDistance = sqrt((ennemy.x - player.x)*(ennemy.x - player.x) + (ennemy.y - player.y)*(ennemy.y - player.y)) * BLOCK_SIZE; + float ennemyBaseWidth = BLOCK_SIZE; + float ennemyDistanceX = ennemyDistance * cos(ennemyAngle - player.angle) * BLOCK_SIZE; + float ennemyDistanceY = ennemyDistance * fabs(sin(ennemyAngle - player.angle)) * BLOCK_SIZE; + float scaledEnnemyWidth = ennemyBaseWidth / sqrt(3); + int ennemyWidth = 50; + int ennemyHeight = 200; + + //printf("%f %f\n", ennemyAngle, player.angle - (FOV_ANGLE)/2 * DR); + + + if (ennemyAngle >= player.angle - (FOV_ANGLE)/2 * DR && ennemyAngle <= player.angle + (FOV_ANGLE)/2 * DR){ + rect.x = screenDimension.w/2 + (screenDimension.w * tan(ennemyAngle - player.angle)) * sqrt(3) * 0.5; + rect.w = (ennemyWidth * screenDimension.w) / (ennemyDistance/BLOCK_SIZE); + rect.h = (ennemyHeight * screenDimension.h)/(ennemyDistance/BLOCK_SIZE); + rect.y = (screenDimension.h/2 + player.viewAngle) - rect.h/5; + + destRect.x = 0; + destRect.y = 0; + destRect.w = 64; + destRect.h = 64; + printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); + SDL_RenderCopy(renderer, playerTexture, &destRect, &rect); } } +void drawBall(){ + float ballAngle = atan2((ball.y + ball.w/2) - (player.y + player.w/2) , (ball.x + ball.w/2) - (player.x + player.w/2)); + if (ballAngle < 0) ballAngle += 2*pi; + if (ballAngle > 2*pi) ballAngle -= 2*pi; + float ballDistance = sqrt((ball.x - player.x)*(ball.x - player.x) + (ball.y - player.y)*(ball.y - player.y)) * BLOCK_SIZE; + float ballBaseWidth = BLOCK_SIZE/2; + float ballDistanceX = ballDistance * cos(ballAngle - player.angle) * BLOCK_SIZE; + float ballDistanceY = ballDistance * fabs(sin(ballAngle - player.angle)) * BLOCK_SIZE; + float scaledBallWidth = ballBaseWidth / sqrt(3); + int ballWidth = 25; + int ballHeight = 25; + + if (ballAngle >= player.angle - (FOV_ANGLE)/2 * DR && ballAngle <= player.angle + (FOV_ANGLE)/2 * DR){ + rect.x = screenDimension.w/2 + (screenDimension.w * tan(ballAngle - player.angle)) * sqrt(3) * 0.5; + rect.w = (ballWidth * screenDimension.w) / (ballDistance/BLOCK_SIZE); + rect.h = (ballHeight * screenDimension.h)/(ballDistance/BLOCK_SIZE); + rect.y = (screenDimension.h/2 + player.viewAngle) - rect.h/5; + + destRect.x = 0; + destRect.y = 0; + destRect.w = 64; + destRect.h = 64; + printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); + SDL_RenderCopy(renderer, playerTexture, &destRect, &rect); + } +} + +void drawSkyAndGround(){ + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); + SDL_RenderFillRect(renderer, NULL); + + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + sky.x = 0; + sky.y = 0; + sky.w = screenDimension.w; + sky.h = screenDimension.h/2 + player.viewAngle; + SDL_RenderFillRect(renderer, &sky); + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); +} + void drawMap2D(int map[][MAP_WIDTH]){ int i, j; rect.w = CELL_SIZE; rect.h = CELL_SIZE; rect.x = 0; rect.y = 0; + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + for (i = 0; i < raysListLength; i++){ + SDL_RenderDrawLine(renderer, player.x * CELL_SIZE / BLOCK_SIZE , player.y * CELL_SIZE / BLOCK_SIZE, rays[i][0] * CELL_SIZE / BLOCK_SIZE, rays[i][1] * CELL_SIZE / BLOCK_SIZE); + } for (i = 0; i < MAP_HEIGHT; i++){ for (j = 0; j < MAP_WIDTH; j++){ - if (map[i][j] == 1){ + switch (map[i][j]) + { + case 1: SDL_SetRenderDrawColor(renderer, 5, 255, 255, 255); SDL_RenderFillRect(renderer, &rect); + break; + + case 2: + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 100); + SDL_RenderFillRect(renderer, &rect); + break; } - else { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + if ((i == player.x/BLOCK_SIZE && j == player.y/BLOCK_SIZE) || (i == ennemy.x/BLOCK_SIZE && j == ennemy.y/BLOCK_SIZE)){ + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_RenderFillRect(renderer, &rect); } rect.x += CELL_SIZE; @@ -204,12 +501,38 @@ void drawMap2D(int map[][MAP_WIDTH]){ rect.y += CELL_SIZE; rect.x = 0; } + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); +} + +void drawString(char *str, int x, int y, int w, int h, int r, int g, int b, int a){ + SDL_Color color = {r, g, b, a}; + SDL_Surface *text = TTF_RenderText_Solid(RobotoFont, str, color); + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, text); + rect.x = x; + rect.y = y; + rect.w = w; + rect.h = h; + SDL_RenderCopy(renderer, texture, NULL, &rect); + SDL_FreeSurface(text); + SDL_DestroyTexture(texture); +} + +void drawFPS(){ + char str[10]; + sprintf(str, "%d", (int)fps); + drawString(str, screenDimension.w - 50, 0, 50, 50, 255, 255, 255, 255); } void drawGame(){ SDL_RenderClear(renderer); - drawRays(map); - //drawMap2D(map); + drawSkyAndGround(); + castRays(map); + drawHorizentalRays(); + drawEnnemy(); + drawVerticalRays(); + drawBall(); + drawMap2D(map); + drawFPS(); SDL_RenderPresent(renderer); } @@ -217,6 +540,11 @@ void drawGame(){ void mainLoop(){ createWindow(); + initRays(); + + netTexture = loadTexture("Res/net.png"); + crowdTexture = loadTexture("Res/crowd.png"); + playerTexture = loadTexture("Res/player_sprite.png"); unsigned int a = SDL_GetTicks(); unsigned int b = SDL_GetTicks(); @@ -233,6 +561,7 @@ void mainLoop(){ delta = (a - b); if (delta > 1000/FPS_TO_GET){ //printf("fps: %f\n", 1000/delta); + fps = 1000/delta; b = a; switch (game_state){ case MENU: diff --git a/travail_de_groupe/jeu_appren_par_renfo/src/render.h b/travail_de_groupe/jeu_appren_par_renfo/src/render.h index 2afb116728a3699583eb34b7589f33b828489131..0bc2bb8a397f0732a4be0dec094aedc7abb9c3b2 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/render.h +++ b/travail_de_groupe/jeu_appren_par_renfo/src/render.h @@ -9,16 +9,27 @@ #define FPS_TO_GET 60 #define CELL_SIZE 10 -#define DOF 8 +#define DOF 100 #define BLOCK_SIZE 64 #define DR 0.0174533 -#define FOV_ANGLE 60 +#define FOV_ANGLE 120 #define pi 3.14159265358979323846 -#define NB_RAYS (screenDimension.w/40) +#define NB_RAYS (screenDimension.w) #define ANGLE_INC ((DR * FOV_ANGLE) / NB_RAYS) +typedef struct rayInfo{ + float ra; + float distT; + int r; + int isTransparent; + int direction; + float htexture; + struct rayInfo * next; +} rayInfo_t; + extern SDL_Window *window; extern SDL_Renderer *renderer; +extern SDL_DisplayMode screenDimension; void mainLoop();