diff --git a/.vscode/settings.json b/.vscode/settings.json index e99eb3d341fcd628102db037900fadfb48dc0e24..2ad6ae8644395aeb2086735309e89a3799379de1 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,7 +7,8 @@ "sprites.h": "c", "sdl2-ttf-light.h": "c", "stdlib.h": "c", - "utility.h": "c" + "utility.h": "c", + "cmath": "c" }, "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/library/Display/Display.c b/library/Display/Display.c index 56393ddf625850cfd9b9c473901bb2bfab5f358d..69465d855bd1db924d5e0e27921fad970dd0542b 100644 --- a/library/Display/Display.c +++ b/library/Display/Display.c @@ -1,6 +1,7 @@ #include "Display.h" #include "../../constante.c" #include "../utility/utility.h" +#include <math.h> void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ textures->background = load_image( "ressources/space-background.bmp",renderer); @@ -9,41 +10,49 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ textures->finishLine = load_image( "ressources/finish_line.bmp",renderer); textures->font = load_font("ressources/font/arial.ttf", 14); textures->color = (SDL_Color){255, 255, 255, 255}; + textures->center = (SDL_Point){SCREEN_WIDTH/2, SCREEN_HEIGHT/2}; + textures->angle = 0; } -void apply_background(SDL_Renderer *renderer, SDL_Texture *texture){ +void apply_background(SDL_Renderer *renderer, SDL_Texture *texture, ressources_t *res){ if(texture != NULL){ - apply_texture(texture, renderer, 0, 0); + //printf("%f %f\n", (SCREEN_WIDTH/2+(0-SCREEN_WIDTH/2)*cos(angle)-(0-SCREEN_HEIGHT/2)*sin(angle)), (SCREEN_HEIGHT/2+(0-SCREEN_WIDTH/2)*sin(angle)+(0-SCREEN_HEIGHT/2)*cos(angle))); + apply_texture(texture, renderer, 0, 0, res->angle*180/M_PI, &res->center); } } -void apply_sprite(SDL_Renderer * renderer, SDL_Texture *texture, sprite_t *sprite){ +void apply_sprite(SDL_Renderer * renderer, SDL_Texture *texture, sprite_t *sprite, ressources_t *res){ if(texture != NULL){ SDL_Rect rect; - rect.x = sprite->x; - rect.y = sprite->y; + rect.x = SCREEN_WIDTH/2 + (sprite->x - SCREEN_WIDTH/2) * cos(res->angle) - (sprite->y - SCREEN_HEIGHT/2) * sin(res->angle); + rect.y = SCREEN_HEIGHT/2 + (sprite->x - SCREEN_WIDTH/2) * sin(res->angle) + (sprite->y - SCREEN_HEIGHT/2) * cos(res->angle); + rect.w = sprite->w; rect.h = sprite->h; - SDL_RenderCopy(renderer, texture, NULL, &rect); + + SDL_RenderCopyEx(renderer, texture, NULL, &rect, res->angle*180/M_PI, NULL, SDL_FLIP_NONE); } } -void apply_wall(SDL_Renderer * renderer, SDL_Texture *texture, int x, int y){ +void apply_wall(SDL_Renderer * renderer, SDL_Texture *texture, int x, int y, ressources_t *res){ if(texture != NULL){ SDL_Rect rect; - rect.x = x; - rect.y = y; + rect.x = SCREEN_WIDTH/2 + (x - SCREEN_WIDTH/2) * cos(res->angle) - (y - SCREEN_HEIGHT/2) * sin(res->angle); + rect.y = SCREEN_HEIGHT/2 + (x - SCREEN_WIDTH/2) * sin(res->angle) + (y - SCREEN_HEIGHT/2) * cos(res->angle); rect.w = METEORITE_SIZE; rect.h = METEORITE_SIZE; - SDL_RenderCopy(renderer, texture, NULL, &rect); + + if (SDL_RenderCopyEx(renderer, texture, NULL, &rect, res->angle*180/M_PI, NULL, SDL_FLIP_NONE) != 0){ + printf("ok\n"); + } } } -void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world){ +void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world, ressources_t *res){ for (int i = 0; i < world->nb_murs; i++){ for (int i3 = 0; i3 < world->murs[i]->w/METEORITE_SIZE ; i3++){ for (int i2 = 0; i2 < world->murs[i]->h/METEORITE_SIZE ; i2++){ - apply_wall(renderer, texture, world->murs[i]->x+i3*METEORITE_SIZE, world->murs[i]->y+i2*METEORITE_SIZE); + apply_wall(renderer, texture, world->murs[i]->x+i3*METEORITE_SIZE, world->murs[i]->y+i2*METEORITE_SIZE, res); } } } @@ -51,25 +60,36 @@ void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world){ void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ - char * str = malloc(sizeof(char)*100); //on vide le renderer clear_renderer(renderer); //application des textures dans le renderer - apply_background(renderer, textures->background); + apply_background(renderer, textures->background, textures); + + apply_sprite(renderer, textures->ship, world->vaisseau, textures); - apply_sprite(renderer, textures->ship, world->vaisseau); + apply_sprite(renderer, textures->finishLine, world->ligneArriver, textures); - apply_sprite(renderer, textures->finishLine, world->ligneArriver); + apply_walls(renderer, textures->meteorite, world, textures); - apply_walls(renderer, textures->meteorite, world); - apply_text(renderer, 10, 10, 100, 33, strcats(str, 3, "temps: ",int_to_str((int)world->timer/1000), "s"), textures->font, textures->color); + if (timer_update_s(world) != 0){ + world->str[0] = '\0'; + world->str = strcats(world->str, 3, "temps: ",int_to_str((int)world->timer/1000), "s"); + } + + apply_text(renderer, 10, 10, 100, 33, world->str, textures->font, textures->color); - // on met à jour l'écran + update_screen(renderer); } +int timer_update_s(world_t *world){ + if (world->timer%1000 <= 110|| world->timer%1000 >= 985){ + return world->timer%1000; + } + return 0; +} void clean(SDL_Window *window, SDL_Renderer * renderer, ressources_t *textures, world_t * world){ clean_data(world); diff --git a/library/Display/Display.h b/library/Display/Display.h index 378a89069608874eb4bf6c266b697ba45bcf77ae..f535cbce8c53af52d570419b7cf53e9a74cb7f69 100644 --- a/library/Display/Display.h +++ b/library/Display/Display.h @@ -38,8 +38,8 @@ struct ressources_s{ SDL_Texture* finishLine; /*!< Texture liée à l'image de la ligne d'arrivée. */ TTF_Font *font; // Font SDL_Color color; // Color - SDL_Texture* framebuffer; - uint32_t* pixels; + SDL_Point center; /*!< Point central de la fenetre. */ + long double angle; /*!< Angle de rotation de l'image. */ }; typedef struct ressources_s ressources_t; @@ -57,7 +57,7 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures); * \param renderer le renderer * \param texture la texture liée au fond */ -void apply_background(SDL_Renderer *renderer, SDL_Texture *texture); +void apply_background(SDL_Renderer *renderer, SDL_Texture *texture, ressources_t *res); /** * \brief La fonction qui applique la texture \a texture sur le renderer \a renderer en fonction des données du sprite \a sprite @@ -66,7 +66,7 @@ void apply_background(SDL_Renderer *renderer, SDL_Texture *texture); * \param renderer * \param sprite */ -void apply_sprite(SDL_Renderer * renderer, SDL_Texture *texture, sprite_t *sprite); +void apply_sprite(SDL_Renderer * renderer, SDL_Texture *texture, sprite_t *sprite, ressources_t *res); /** * \brief La fonction qui applique la texture \a texture sur le renderer \a renderer en fonction des coordonnées \a x et \a y @@ -76,8 +76,17 @@ void apply_sprite(SDL_Renderer * renderer, SDL_Texture *texture, sprite_t *sprit * \param x * \param y */ -void apply_wall(SDL_Renderer * renderer, SDL_Texture *texture, int x, int y); +void apply_wall(SDL_Renderer * renderer, SDL_Texture *texture, int x, int y, ressources_t *res); +/** + * \brief La fonction qui applique les textures des murs sur le renderer \a renderer en fonction des données du monde \a world + * + * \param renderer + * \param texture + * \param world + * \param res + */ +void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world, ressources_t *res); /** * \brief La fonction rafraichit l'écran en fonction de l'état des données du monde @@ -102,4 +111,11 @@ void clean_textures(ressources_t *textures); */ void clean(SDL_Window *window, SDL_Renderer * renderer, ressources_t *textures, world_t * world); +/** + * \brief La fonction qui affiche le temps restant sur l'écran + * + * \param world + * \return int + */ +int timer_update_s(world_t *world); #endif \ No newline at end of file diff --git a/library/Display/Display.o b/library/Display/Display.o index 8d84e1021647c5cffad8470d0f89af7124b68d16..5e770405a1658dd3711a9eda4836cf29edde849f 100644 Binary files a/library/Display/Display.o and b/library/Display/Display.o differ diff --git a/library/Sprites/sprites.c b/library/Sprites/sprites.c index 75df60aa5ae18762a99ca11936523e9c3ec187d3..52eb7766420c08d245abc4572345003d3dac5343 100644 --- a/library/Sprites/sprites.c +++ b/library/Sprites/sprites.c @@ -12,7 +12,7 @@ void print_sprite(sprite_t *sprite){ } -sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h){ +sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, int id){ sprite = malloc(sizeof(sprite_t)); sprite->x = x; sprite->y = y; diff --git a/library/Sprites/sprites.h b/library/Sprites/sprites.h index 68bc41e6c954981a310c989f809296907c571b9b..a19ee01911675d7fd97d6638cb43773edba98824 100644 --- a/library/Sprites/sprites.h +++ b/library/Sprites/sprites.h @@ -28,6 +28,7 @@ struct sprite_s{ int y; int w; int h; + int id; }; typedef struct sprite_s sprite_t; @@ -40,6 +41,11 @@ typedef struct sprite_s sprite_t; */ int isOverScreen(sprite_t *sprite); +/** + * \brief La fonction affiche le sprite + * + * \param sprite + */ void print_sprite(sprite_t *sprite); /** @@ -52,7 +58,7 @@ void print_sprite(sprite_t *sprite); * \param h * \return sprite_t* */ -sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h); +sprite_t *init_sprite(sprite_t *sprite, int x, int y, int w, int h, int id); /** * \brief La fonction libère les données du sprite diff --git a/library/Sprites/sprites.o b/library/Sprites/sprites.o index 0b0819682368140de557fc3cd415794ee36ff8d5..44f78ba2d9121312eebac520db0000a731a95c1b 100644 Binary files a/library/Sprites/sprites.o and b/library/Sprites/sprites.o differ diff --git a/library/World/world.c b/library/World/world.c index ccc4f08cc6c89eec524573103147f2e4b3ce5a7f..038ce06409490f259ddaec5492ba5959f10eac23 100644 --- a/library/World/world.c +++ b/library/World/world.c @@ -13,6 +13,9 @@ void update_data(world_t *world){ for(int i = 0; i < world->nb_murs; i++){ if (handle_sprite_collide(world->vaisseau, world->murs[i], world, 0) == 1){ break; + }else if (handle_sprite_collide(world->vaisseau, world->murs[i], world, 0) == 2){ + printf("CHangement de sens\n"); + break; } } @@ -33,13 +36,14 @@ void init_data(world_t * world){ world->gameover = 0; world->speed_h = (float)INITIAL_SPEED; // Initialisation du vaisseau - world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE, SHIP_SIZE, SHIP_SIZE); - // world->mur = init_sprite(world->mur, 0, 0, 3*METEORITE_SIZE, 7*METEORITE_SIZE); - world->ligneArriver = init_sprite(world->ligneArriver, 0, -960 , SCREEN_WIDTH, FINISH_LINE_HEIGHT); + world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE, SHIP_SIZE, SHIP_SIZE, 0); init_walls(world); + world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 0); + print_sprite(world->vaisseau); world->startTimer = SDL_GetTicks(); world->timer = SDL_GetTicks(); + world->str = malloc(sizeof(char)*100); } @@ -48,7 +52,8 @@ void clean_data(world_t *world){ free(world->vaisseau); free(world->ligneArriver); free(world->murs); - + free(world->str); + printf("clean_data"); } @@ -58,6 +63,7 @@ int handle_sprite_collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make world->gameover = 1; printf("collision"); return 1; + }else{ return 0; } @@ -67,17 +73,27 @@ int handle_sprite_collide(sprite_t *sp1, sprite_t *sp2, world_t *world, int make void init_walls(world_t *world){ world->nb_murs = 0; world->murs = malloc(sizeof(sprite_t) * MAX_LENGTH*MAX_LINES); - int nblignes = 0; - char **txt = lirefile("maps/default.txt", &nblignes); - printf("aaiaa"); - for (int i = 0; i < nblignes; i++) { - for (int j = 0; j < 26; j++) { - if (txt[i][j] == '1'){ - world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, i*METEORITE_SIZE, METEORITE_SIZE, METEORITE_SIZE); + world->nb_lines_murs = 0; + char **txt = lirefile("maps/default.txt", &world->nb_lines_murs); + + for (int i = 0; i < world->nb_lines_murs; i++) { + for (int j = 0; j < MAX_LENGTH; j++) { + switch (txt[i][j]) + { + case '1': + world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, (i*METEORITE_SIZE)-(METEORITE_SIZE*world->nb_lines_murs), METEORITE_SIZE, METEORITE_SIZE, 1); world->nb_murs++; + break; + case '2': + world->murs[world->nb_murs] = init_sprite(world->murs[world->nb_murs], j*METEORITE_SIZE, (i*METEORITE_SIZE)-(METEORITE_SIZE*world->nb_lines_murs), METEORITE_SIZE, METEORITE_SIZE, 2); + world->nb_murs++; + break; + + default: + break; } + } - printf("%s\n", txt[i]); } printf("aaaa"); @@ -87,4 +103,4 @@ void update_walls(world_t *world){ for (int i = 0; i < world->nb_murs; i++){ world->murs[i]->y += world->speed_h; } -} \ No newline at end of file +} diff --git a/library/World/world.h b/library/World/world.h index 6f0fafa6e47e3021b0b3f9443d6ed9d78fff791d..d89d094f7549446f9a6b9672791f2cf6ed8d60e2 100644 --- a/library/World/world.h +++ b/library/World/world.h @@ -35,12 +35,14 @@ struct world_s{ sprite_t *vaisseau ; /*!< Représentation du vaisseau */ sprite_t **murs; /*<Représentation des météorites>*/ - int nb_murs; + int nb_murs; // Nombre de météorites + int nb_lines_murs; // Nombre de lignes de météorites sprite_t *ligneArriver; int gameover; /*!< Champ indiquant si l'on est à la fin du jeu */ float speed_h; /*!< Vitesse de déplacement horizontal des éléments du jeu */ unsigned int startTimer; /*!< Timer de départ */ unsigned int timer; /*!< Timer de jeu */ + char * str; // String affichant le temps sur le jeu }; typedef struct world_s world_t; @@ -97,4 +99,6 @@ void init_walls(world_t *world); * \param world */ void update_walls(world_t *world); + + #endif diff --git a/library/World/world.o b/library/World/world.o index 2675f57fe1da88dbd25d5bbf1553385a8194833c..6a14e8f9ee177293bb490d21cc72124caa42a914 100644 Binary files a/library/World/world.o and b/library/World/world.o differ diff --git a/library/utility/utility.h b/library/utility/utility.h index 0cc74088edd88c02f5a16766b9ec7a3f5d856a3d..34cbc63c006a31fbfa3ca5dfc282f594221c3fda 100644 --- a/library/utility/utility.h +++ b/library/utility/utility.h @@ -20,7 +20,7 @@ * * \param dest * \param num_args - * \param ... + * \param ... Nombre d'arguments variables * \return char* */ char * strcats(char* dest, int num_args, ...); diff --git a/main.c b/main.c index 308c20058a753135c7cbfb59254b84cec83541b9..4be00d67ed2760da1fe0609648380ff04cc1f657 100644 --- a/main.c +++ b/main.c @@ -53,10 +53,10 @@ void handle_events(SDL_Event *event,world_t *world){ break; case SDLK_ESCAPE: world->gameover = 1; - break; - default: break; - } + default: + break; + } // print_sprite(world->vaisseau); } } diff --git a/main.o b/main.o index 62d9bd3a91b616192ecb039b1f72a9e2ef18f099..e9257108b45be6160b70ccc6e098b75bc18ab630 100644 Binary files a/main.o and b/main.o differ diff --git a/maps/default.txt b/maps/default.txt index 363f4e6991832f637feb6fe31b6b4f415512649e..06238f54a0ae40e1e07640a210f3e5aad76af6c1 100644 --- a/maps/default.txt +++ b/maps/default.txt @@ -1,2 +1,49 @@ 010000000011110000000000 -001001111100011100000110 \ No newline at end of file +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 + +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001001111100011100000110 +001201111100011100000110 +001001111100011100000110 + diff --git a/sdl2-light.c b/sdl2-light.c index 78d7c046c918631a7ce6524501f2292900948081..c50e484cf4e76b4dc4713c0bc41fa2c893e52cb7 100644 --- a/sdl2-light.c +++ b/sdl2-light.c @@ -52,13 +52,14 @@ SDL_Texture *load_image(const char path[], SDL_Renderer *renderer) } -void apply_texture(SDL_Texture *texture,SDL_Renderer *renderer,int x, int y){ +void apply_texture(SDL_Texture *texture,SDL_Renderer *renderer,int x, int y, int angle, SDL_Point *center){ SDL_Rect dst = {0, 0, 0, 0}; SDL_QueryTexture(texture, NULL, NULL, &dst.w, &dst.h); dst.x = x; dst.y=y; - SDL_RenderCopy(renderer, texture, NULL, &dst); + SDL_RenderCopyEx(renderer, texture, NULL, &dst, angle, center, SDL_FLIP_NONE); + } diff --git a/sdl2-light.h b/sdl2-light.h index 7132f7dc1f78cf95a0c0373ae3f4186d6fb604ea..861dfb885e908aee040c2c75b98597f81fea98e5 100644 --- a/sdl2-light.h +++ b/sdl2-light.h @@ -61,7 +61,7 @@ void clean_texture(SDL_Texture *texture); * \param y l'ordonnée sur le renderer de l'endroit où est appliquée texture (point en haut à gauche de la surface) */ -void apply_texture(SDL_Texture *texture,SDL_Renderer *renderer,int x, int y); +void apply_texture(SDL_Texture *texture,SDL_Renderer *renderer,int x, int y, int angle, SDL_Point *center); diff --git a/sdl2-light.o b/sdl2-light.o index 019689e41b1fb71209161e4a73782236f5a35122..01450226850a60cb6c6dc2ff5cef821c7d1e1486 100644 Binary files a/sdl2-light.o and b/sdl2-light.o differ diff --git a/spacecorridor.exe b/spacecorridor.exe index 6a484f4a588f79e7d5ee6d443579c062b26bc88a..08f9dc9790ee6f1b974f730e6dc8abb9ac3cafb7 100644 Binary files a/spacecorridor.exe and b/spacecorridor.exe differ