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

ajout de la fonction pour dessiner les rayons

parent a8572d25
No related branches found
No related tags found
No related merge requests found
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 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
......@@ -34,7 +34,7 @@ void *EventLoop(void *arg){
while(running){
switch(game_state){
case MENU : gestMenu();break;
//case GAME : gestGame();break;
case GAME : gestMenu();break;
default:printf("game state fault");break;
}
}
......
......@@ -6,8 +6,10 @@ int game_state;
int main(){
running = 1;
game_state = MENU;
game_state = GAME;
readMapFromFile("map.txt");
printMap();
MainLoop();
mainLoop();
}
\ No newline at end of file
......@@ -16,6 +16,7 @@
#include "render.h"
#include "gest_event.h"
#include "map.h"
#define MENU 0
......
#include "map.h"
int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name){
FILE *file = fopen(file_name, "r");
if (file == NULL){
printf("Couldn't open map file.");
exit(EXIT_FAILURE);
}
int i, j;
for (i = 0; i < MAP_HEIGHT ; i++){
for (j = 0; j < MAP_WIDTH; j++){
fscanf(file, "%d", &map[i][j]);
}
}
fclose(file);
}
void printMap(){
int i, j;
for (i = 0; i < MAP_HEIGHT; i++){
for (j = 0; j < MAP_WIDTH; j++){
printf("%d ", map[i][j]);
}
printf("\n");
}
}
\ No newline at end of file
#ifndef MAP_HEADER_
#define MAP_HEADER_
#include "main.h"
#define MAP_WIDTH 31
#define MAP_HEIGHT 17
extern int map[MAP_HEIGHT][MAP_WIDTH];
void readMapFromFile(char *file_name);
void printMap();
#endif
\ No newline at end of file
......@@ -4,18 +4,30 @@ SDL_Window *window;
SDL_Renderer *renderer;
TTF_Font *RobotoFont;
SDL_DisplayMode screenDimension;
void CreateWindow(){
SDL_DisplayMode ScreenDimension;
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 = player->angle - DR * FOV_ANGLE/4;
// end ray casting variables
void createWindow(){
if (SDL_Init(SDL_INIT_VIDEO) != 0){
printf("Couldn't create window.");
exit(EXIT_FAILURE);
}
SDL_GetCurrentDisplayMode(0, &ScreenDimension);
SDL_GetCurrentDisplayMode(0, &screenDimension);
window = SDL_CreateWindow("Game Of Life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, ScreenDimension.w, ScreenDimension.h, SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
window = SDL_CreateWindow("Mat Le King", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenDimension.w, screenDimension.h, SDL_WINDOW_INPUT_GRABBED | SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
if (window == NULL){
printf("Couldn't create window");
......@@ -38,9 +50,159 @@ void CreateWindow(){
}
void endSDL(){
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
TTF_CloseFont(RobotoFont);
TTF_Quit();
SDL_Quit();
}
void drawRays(int map[][MAP_WIDTH]){
if (ra < 0) ra -= 2*pi;
if (ra > 2*pi) ra -= 2*pi;
for (r = 0; r<NB_RAYS; r++){
// check horizontal rays
dof = 0;
float disH = 100000, hx = player->x, hy = player->y;
float aTan = -1/tan(ra);
if (ra > pi){ // looking up
ry = (((int)player->y>>6)<<6) - 0.0001;
rx = (player->y - ry) * aTan + player->x;
yo = -BLOCK_SIZE;
xo = -yo*aTan;
}
if (ra<pi){ // looking down
ry = (((int)player->y>>6)<<6) + BLOCK_SIZE;
rx = (player->y - ry) * aTan + player->x;
yo = BLOCK_SIZE;
xo = -yo*aTan;
}
if (ra == pi){
ry = player->y;
rx = player->x;
dof = DOF;
}
while (dof < DOF){
mx = (int)rx>>6;
my = (int)ry>>6;
if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT){
if (map[my][mx] == 1){
hx = rx;
hy = ry;
disH = sqrt((rx-player->x)*(rx-player->x) + (ry-player->y)*(ry-player->y));
dof = DOF;
}
}
else {
rx += xo;
ry += yo;
dof++;
}
}
// check vertical rays
dof = 0;
float disV = 100000, vx = player->x, vy = player->y;
float nTan = -tan(ra);
if (ra > pi/2 && ra < 3*pi/2){ // looking left
rx = (((int)player->x>>6)<<6) - 0.0001;
ry = player->y + (player->x - rx) * nTan;
xo = -BLOCK_SIZE;
yo = -xo*nTan;
}
if (ra<pi/2 || ra > 3*pi/2){ // looking right
rx = (((int)player->x>>6)<<6) + BLOCK_SIZE;
ry = player->y + (player->x - rx) * nTan;
xo = BLOCK_SIZE;
yo = -xo*nTan;
}
while (dof < DOF){
mx = (int)rx>>6;
my = (int)ry>>6;
if (mx >= 0 && mx < MAP_WIDTH && my >= 0 && my < MAP_HEIGHT){
if (map[my][mx] == 1){
vx = rx;
vy = ry;
disV = sqrt((rx-player->x)*(rx-player->x) + (ry-player->y)*(ry-player->y));
dof = DOF;
}
}
else {
rx += xo;
ry += yo;
dof++;
}
}
if (disH < disV) {
rx = hx;
ry = hy;
distT = disH;
}
else {
rx = vx;
ry = vy;
distT = disV;
}
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;
rect.x = r;
rect.y = lineH;
rect.w = 1;
rect.h = lineH;
if (disH < disV) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
}
SDL_RenderFillRect(renderer, &rect);
}
}
void drawMap2D(int map[][MAP_WIDTH]){
int i, j;
rect.w = CELL_SIZE;
rect.h = CELL_SIZE;
rect.x = 0;
rect.y = 0;
for (i = 0; i < MAP_HEIGHT; i++){
for (j = 0; j < MAP_WIDTH; j++){
if (map[i][j] == 1){
SDL_SetRenderDrawColor(renderer, 5, 255, 255, 255);
SDL_RenderFillRect(renderer, &rect);
}
else {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
}
rect.x += CELL_SIZE;
}
rect.y += CELL_SIZE;
rect.x = 0;
}
}
void drawGame(){
SDL_RenderClear(renderer);
drawMap2D(map);
SDL_RenderPresent(renderer);
}
void MainLoop(){
CreateWindow();
void mainLoop(){
createWindow();
unsigned int a = SDL_GetTicks();
unsigned int b = SDL_GetTicks();
......@@ -54,22 +216,24 @@ void MainLoop(){
while (running){
a = SDL_GetTicks();
delta = (a - b) / 1000.0;
if (delta > 1/FPS_TO_GET){
delta = (a - b);
if (delta > 1000/FPS_TO_GET){
//printf("fps: %f\n", 1000/delta);
b = a;
switch (game_state){
case MENU:
//Menu();
break;
case GAME:
//Game();
drawGame();
break;
}
}
else {
// fait dormir le thread pour garder des ressources
usleep(1000 * (1/FPS_TO_GET - delta));
usleep(1000 * (1000/FPS_TO_GET - delta));
}
}
endSDL();
}
\ No newline at end of file
......@@ -3,13 +3,23 @@
#include "main.h"
#include "gest_event.h"
#include "map.h"
#define FPS_TO_GET 60
#define CELL_SIZE 10
#define DOF 8
#define BLOCK_SIZE 64
#define DR 0.0174533
#define FOV_ANGLE 60
#define pi 3.14159265358979323846
#define NB_RAYS (screenDimension.x)
#define ANGLE_INC ((DR * FOV_ANGLE) / NB_RAYS)
extern SDL_Window *window;
extern SDL_Renderer *renderer;
void MainLoop();
void mainLoop();
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment