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/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 7ca6b39f5741c10f6c1052e6074791e7a3f0f337..82eba25d7d18c55adee39cc74c1240fd551a7c18 100644 --- a/travail_de_groupe/jeu_appren_par_renfo/src/main.c +++ b/travail_de_groupe/jeu_appren_par_renfo/src/main.c @@ -8,7 +8,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..83c93a2f6317b414f05cc2e96ee927a6498b8dee 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,41 @@ player_t player; +player_t ennemy; + 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; 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..2fbe0dc80363cb300f830491ca25ce2092cc8b11 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,15 @@ 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; 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..0a719e01aca3fd5f7659f2ecc6ab08b2ffbd8318 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,84 @@ 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; + +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 +127,71 @@ 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 drawRays(){ + rayInfo_t * current = raysListHead.next; + while (current != NULL){ + //printf("%p\n", current); + fflush(stdout); + 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 +219,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 +238,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 +256,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 +291,167 @@ 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); - } +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.y = (screenDimension.h/2 + player.angle) - MAP_WIDTH * ennemyDistance/200000; + rect.w = (ennemyWidth * screenDimension.w) / (ennemyDistance/BLOCK_SIZE); + rect.h = (ennemyHeight * screenDimension.h)/(ennemyDistance/BLOCK_SIZE); + + //printf("%d %d %d %d\n", rect.x, rect.y, rect.w, rect.h); + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_RenderFillRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); } } +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 +459,36 @@ 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); + drawEnnemy(); + drawRays(); + drawMap2D(map); + drawFPS(); SDL_RenderPresent(renderer); } @@ -217,6 +496,10 @@ void drawGame(){ void mainLoop(){ createWindow(); + initRays(); + + netTexture = loadTexture("Res/net.png"); + crowdTexture = loadTexture("Res/crowd.png"); unsigned int a = SDL_GetTicks(); unsigned int b = SDL_GetTicks(); @@ -233,6 +516,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();