Skip to content
Snippets Groups Projects
Commit 36ec91c9 authored by belkhiritaha's avatar belkhiritaha
Browse files

Merge branch 'main' into qlearn

parents fcb3f2ad d9e783b6
No related branches found
No related tags found
No related merge requests found
Showing
with 753 additions and 142 deletions
File added
travail_de_groupe/jeu_appren_par_renfo/Res/crowd.png

1.19 KiB

travail_de_groupe/jeu_appren_par_renfo/Res/net.png

294 B

travail_de_groupe/jeu_appren_par_renfo/Res/player_sprite.png

1.03 KiB

#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
#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
......@@ -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
......
......@@ -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:
......
......@@ -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();
......
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
#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;
}
}
......
......@@ -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
......@@ -11,7 +11,9 @@ int main(){
running = 1;
game_state = GAME;
readMapFromFile("map.txt");
printMap();
//printMap();
initPlayer();
initKeys();
mainLoop();
......
......@@ -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
......@@ -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
......@@ -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:
......
......@@ -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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment