Skip to content
Snippets Groups Projects
Commit 0f8e4f50 authored by belkhiritaha's avatar belkhiritaha
Browse files

début du raytracing et affichage des rays dans la minimap

parent 4b05b274
No related branches found
No related tags found
No related merge requests found
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
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 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 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
......
...@@ -30,11 +30,51 @@ void gestMenu(){ ...@@ -30,11 +30,51 @@ 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;
}
else if (event.motion.xrel < 0){
player.angle -= 0.01;
if (player.angle < 0) player.angle += 2*pi;
}
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
running = 0;
continue;
default:
continue;
}
break;
default:
continue;
}
}
SDL_Delay(5);
}
void *EventLoop(void *arg){ void *EventLoop(void *arg){
while(running){ while(running){
switch(game_state){ switch(game_state){
case MENU : gestMenu();break; case MENU : gestMenu();break;
case GAME : gestMenu();break; case GAME : gestGame();break;
default:printf("game state fault");break; default:printf("game state fault");break;
} }
} }
......
...@@ -9,6 +9,7 @@ int main(){ ...@@ -9,6 +9,7 @@ int main(){
game_state = GAME; game_state = GAME;
readMapFromFile("map.txt"); readMapFromFile("map.txt");
printMap(); printMap();
initPlayer();
mainLoop(); mainLoop();
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
player_t player; player_t player;
void initPlayer(){ void initPlayer(){
player.x= 8*BLOCK_SIZE; player.x= 2*BLOCK_SIZE;
player.y= 8*BLOCK_SIZE; player.y= 2*BLOCK_SIZE;
player.speed = 1; player.speed = 1;
player.isMoving = 0; player.isMoving = 0;
player.HPMax = 3; player.HPMax = 3;
......
...@@ -10,11 +10,33 @@ SDL_Rect rect; ...@@ -10,11 +10,33 @@ SDL_Rect rect;
SDL_Rect sky; SDL_Rect sky;
SDL_Rect ground; SDL_Rect ground;
// ray casting variables int ** rays;
float htexture; int raysListLength = 0;
int r, mx, my, dof;
double rx, ry, xo, yo, distT; void initRays(){
double ra; int i;
rays = malloc(sizeof(int*) * NB_RAYS);
for (i = 0; i < NB_RAYS; i++){
rays[i] = malloc(sizeof(int) * 2);
}
}
void addRayToList(int x, int y){
if (raysListLength < NB_RAYS){
*rays[raysListLength] = x;
*(rays[raysListLength] + 1) = y;
raysListLength++;
}
}
void resetRayList(){
int i;
for (i = 0; i < NB_RAYS; i++){
*rays[i] = 0;
*(rays[i] + 1) = 0;
}
raysListLength = 0;
}
// end ray casting variables // end ray casting variables
...@@ -59,6 +81,12 @@ void endSDL(){ ...@@ -59,6 +81,12 @@ void endSDL(){
} }
void drawRays(int map[][MAP_WIDTH]){ void drawRays(int map[][MAP_WIDTH]){
// ray casting variables
float htexture;
int r, mx, my, dof;
double rx, ry, xo, yo, distT;
double ra;
resetRayList();
ra = player.angle - DR * FOV_ANGLE/4; ra = player.angle - DR * FOV_ANGLE/4;
if (ra < 0) ra -= 2*pi; if (ra < 0) ra -= 2*pi;
if (ra > 2*pi) ra -= 2*pi; if (ra > 2*pi) ra -= 2*pi;
...@@ -87,13 +115,12 @@ void drawRays(int map[][MAP_WIDTH]){ ...@@ -87,13 +115,12 @@ void drawRays(int map[][MAP_WIDTH]){
while (dof < DOF){ while (dof < DOF){
mx = (int)rx>>6; mx = (int)rx>>6;
my = (int)ry>>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] == 1){
if (map[my][mx] == 1){
hx = rx; hx = rx;
hy = ry; hy = ry;
disH = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); disH = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y));
dof = DOF; dof = DOF;
}
} }
else { else {
rx += xo; rx += xo;
...@@ -118,16 +145,19 @@ void drawRays(int map[][MAP_WIDTH]){ ...@@ -118,16 +145,19 @@ void drawRays(int map[][MAP_WIDTH]){
xo = BLOCK_SIZE; xo = BLOCK_SIZE;
yo = -xo*nTan; yo = -xo*nTan;
} }
if (ra == pi || ra == 0){
ry = player.y;
rx = player.x;
dof = DOF;
}
while (dof < DOF){ while (dof < DOF){
mx = (int)rx>>6; mx = (int)rx>>6;
my = (int)ry>>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] == 1){
if (map[my][mx] == 1){ vx = rx;
vx = rx; vy = ry;
vy = ry; disV = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y));
disV = sqrt((rx-player.x)*(rx-player.x) + (ry-player.y)*(ry-player.y)); dof = DOF;
dof = DOF;
}
} }
else { else {
rx += xo; rx += xo;
...@@ -157,9 +187,9 @@ void drawRays(int map[][MAP_WIDTH]){ ...@@ -157,9 +187,9 @@ void drawRays(int map[][MAP_WIDTH]){
float lineH = (screenDimension.h/2)/distT; float lineH = (screenDimension.h/2)/distT;
rect.x = r; rect.x = r;
rect.y = lineH; rect.y = screenDimension.h/2 - lineH;
rect.w = 1; rect.w = 1;
rect.h = lineH; rect.h = (2 * screenDimension.w * lineH/200);
if (disH < disV) { if (disH < disV) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
...@@ -168,6 +198,10 @@ void drawRays(int map[][MAP_WIDTH]){ ...@@ -168,6 +198,10 @@ void drawRays(int map[][MAP_WIDTH]){
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
} }
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
// draw the ray in the minimap
addRayToList(rx, ry);
} }
} }
...@@ -184,18 +218,30 @@ void drawMap2D(int map[][MAP_WIDTH]){ ...@@ -184,18 +218,30 @@ void drawMap2D(int map[][MAP_WIDTH]){
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
} }
else { else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); if (i == player.x/BLOCK_SIZE && j == player.y/BLOCK_SIZE){
SDL_RenderFillRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
} }
rect.x += CELL_SIZE; rect.x += CELL_SIZE;
} }
rect.y += CELL_SIZE; rect.y += CELL_SIZE;
rect.x = 0; rect.x = 0;
} }
SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255);
for (i = 0; i < NB_RAYS; 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);
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
} }
void drawGame(){ void drawGame(){
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
drawRays(map);
drawMap2D(map); drawMap2D(map);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
...@@ -204,6 +250,7 @@ void drawGame(){ ...@@ -204,6 +250,7 @@ void drawGame(){
void mainLoop(){ void mainLoop(){
createWindow(); createWindow();
initRays();
unsigned int a = SDL_GetTicks(); unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks(); unsigned int b = SDL_GetTicks();
......
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