Skip to content
Snippets Groups Projects
Commit 635ea96c authored by belkhiritaha's avatar belkhiritaha
Browse files

Merge branch 'render'

parents 74e0488f 1072b771
No related branches found
No related tags found
No related merge requests found
Showing
with 437 additions and 65 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

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 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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
......@@ -8,7 +8,9 @@ int main(){
running = 1;
game_state = GAME;
readMapFromFile("map.txt");
printMap();
//printMap();
initPlayer();
initKeys();
mainLoop();
......
......@@ -2,13 +2,37 @@
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;
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
......@@ -2,15 +2,17 @@
#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 speed;
int isMoving;
int direction;
......@@ -18,10 +20,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
......@@ -6,15 +6,50 @@ 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;
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 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 +93,58 @@ void endSDL(){
SDL_Quit();
}
void drawRays(int map[][MAP_WIDTH]){
void drawRayColumn(float ra, float distT, int r, int isTransparent, int direction, float htexture){
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;
rect.x = r;
rect.y = (screenDimension.h/2 + player.viewAngle) - lineH;
rect.w = 1;
rect.h = (2 * screenDimension.w * lineH/20);
destRect.x = htexture;
destRect.y = 0;
destRect.w = 1;
destRect.h = 64;
if (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 (direction){
SDL_RenderCopy(renderer, crowdTexture, &destRect, &rect);
}
else {
SDL_RenderCopy(renderer, crowdTexture, &destRect, &rect);
}
}
}
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();
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 +172,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 {
......@@ -107,7 +195,7 @@ void drawRays(int map[][MAP_WIDTH]){
// 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 +209,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 +244,164 @@ 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
if (foundTransparentWallV){
if (foundSolidWallV){
drawRayColumn(ra, distT2, r, 0, direction2 , htexture2);
}
else {
drawRayColumn(ra, distT2, r, 0, direction, htexture2);
}
}
drawRayColumn(ra, distT, r, foundTransparentWallV, direction, htexture);
// 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 - player.y, ennemy.x - player.x);
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 +409,35 @@ 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);
drawMap2D(map);
drawFPS();
drawEnnemy();
SDL_RenderPresent(renderer);
}
......@@ -217,6 +445,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 +465,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,17 @@
#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 ANGLE_INC ((DR * FOV_ANGLE) / NB_RAYS)
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