diff --git a/library/Display/Display.c b/library/Display/Display.c index 74057358b650612cda893ac438276632c1e44ac0..36da019fd615a00b2d85cf1da4537b41d5d16a24 100644 --- a/library/Display/Display.c +++ b/library/Display/Display.c @@ -5,6 +5,7 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ textures->bmenu = load_image( "ressources/image-menu.bmp",renderer); + textures->bshop = load_image( "ressources/Elements/shop.png",renderer); textures->ship = load_image( "ressources/spaceship.bmp",renderer); textures->background = load_image( "ressources/Elements/backgrounds/1.png",renderer); textures->background2 = load_image( "ressources/Elements/backgrounds/3.png",renderer); @@ -20,6 +21,11 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ textures->soleilBarre = load_image( "ressources/Elements/soleil.png",renderer); textures->soleil = load_image( "ressources/Elements/soleil.png",renderer); + + textures->buy = load_image( "ressources/Elements/shop/buy.png",renderer); + textures->nomoney = load_image( "ressources/Elements/shop/nomoney.png",renderer); + textures->sell = load_image( "ressources/Elements/shop/sell.png",renderer); + textures->nb_init = 9; init_ressource_element(renderer, textures); @@ -102,12 +108,12 @@ void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world, void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ //on vide le renderer clear_renderer(renderer); - if (world->isMenu == 0){ - + if (world->gamestate == 0){ ingame(renderer,world,textures); - }else if(world -> isMenu==1){ - + }else if(world ->gamestate==1){ inmenu(renderer,world,textures); + }else if(world ->gamestate==2){ + inshop(renderer,world,textures); } update_screen(renderer); @@ -115,7 +121,30 @@ void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textu } void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ apply_background(renderer, textures->bmenu, world); + printf("%d\n", number_of_numbers(world->money)); + apply_text(renderer, (SCREEN_WIDTH-90)-(25*number_of_numbers(world->money)), 20, 25*number_of_numbers(world->money), 50, world->coin_menu_str, textures->font, textures->color); } + +void inshop(SDL_Renderer *renderer, world_t *world, ressources_t *textures){ + apply_background(renderer, textures->bshop, world); + + apply_text(renderer, 150, 150, 50, 25, "200$", textures->font, textures->color); + apply_text(renderer, 360, 145, 50, 25, "500$", textures->font, textures->color); + apply_text(renderer, 560, 140, 50, 25, "1000$", textures->font, textures->color); + apply_text(renderer, 370, 315, 50, 25, "2000$", textures->font, textures->color); + + for (int i = 0; i<4; i++){ + if (world->shopPrice[i] <= world->money){ + apply_sprite_fixed(renderer, textures->buy, world->Spr_ship[i], world); + }else{ + apply_sprite_fixed(renderer, textures->nomoney, world->Spr_ship[i], world); + } + } + + apply_text(renderer, (SCREEN_WIDTH-90)-(25*number_of_numbers(world->money)), 20, 25*number_of_numbers(world->money), 50, world->coin_menu_str, textures->font, textures->color); + +} + void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ //application des textures dans le renderer diff --git a/library/Display/Display.h b/library/Display/Display.h index e9024bd3d74d6fd7d29d01713f397ff200ec6cc8..c304190f4c74ba8ea64f56c4c74af53884566471 100644 --- a/library/Display/Display.h +++ b/library/Display/Display.h @@ -35,6 +35,7 @@ struct ressources_s{ SDL_Texture* background; /*!< Texture liée à l'image du fond de l'écran. */ SDL_Texture* bmenu;/*!< Texture liée à l'image du fond du menu. */ + SDL_Texture* bshop; /*!< Texture liée à l'image du fond du gameover. */ SDL_Texture* background2; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */ SDL_Texture* background3; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */ SDL_Texture* ship; /*!< Texture liée à l'image du vaisseau. */ @@ -49,6 +50,10 @@ struct ressources_s{ SDL_Texture* soleilBarre; /*!< Texture liée à l'image du soleil. */ SDL_Texture* soleil; /*!< Texture liée à l'image du soleil. */ + SDL_Texture* buy; /*!< Texture liée à l'image du bouton buy. */ + SDL_Texture* nomoney; /*!< Texture liée à l'image du bouton nomoney. */ + SDL_Texture* sell; /*!< Texture liée à l'image du bouton sell. */ + TTF_Font *font; // Font SDL_Color color; // Color long double angle; /*!< Angle de rotation de l'image. */ @@ -149,8 +154,30 @@ 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 jeu (en jeu) + * + * @param renderer + * @param world + * @param textures + */ void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures); +/** + * @brief La fonction qui affiche le jeu (en menu) + * + * @param renderer + * @param world + * @param textures + */ void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures); +/** + * @brief La fonction qui affiche le jeu (en magasin) + * + * @param renderer + * @param world + * @param textures + */ +void inshop(SDL_Renderer *renderer, world_t *world, ressources_t *textures); #endif \ No newline at end of file diff --git a/library/Display/Display.o b/library/Display/Display.o index c04feae259ff2f07116c47263f6dc1fff53a8a8d..f6628b920713b6da0d2dd10fd31bca11c32f563c 100644 Binary files a/library/Display/Display.o and b/library/Display/Display.o differ diff --git a/library/World/world.c b/library/World/world.c index ab67db0fb39d69e0afee9a97b0a9ecf615915102..f76770313101402ba0375f86277adbfe4c4ec086 100644 --- a/library/World/world.c +++ b/library/World/world.c @@ -2,7 +2,7 @@ #include "../utility/utility.h" void update_data(world_t *world){ - if (world->isMenu==0){ + if (world->gamestate==0){ world->ligneArriver->y += (int)world->speed_h; world->soleil->y += (int)world->speed_h; @@ -27,8 +27,10 @@ void update_data(world_t *world){ } allEvents(world); world->timer = SDL_GetTicks() - world->startTimer;; - }else if(world->isMenu == 1){ + }else if(world->gamestate == 1){ world->startTimer = SDL_GetTicks(); + }else if(world->gamestate == 2){ + printf("%d\n", world->money); } } @@ -38,8 +40,14 @@ void init_data(world_t * world){ world->speed_h = (float)INITIAL_SPEED; init_walls(world); world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0); - InitMenu(world); - print_sprite(world->vaisseau); + + world->money = 2000; // Initialisation de l'argent ici car necessaire dans init_menu + + init_menu(world); + + init_shop(world); + + printf("test\n"); // Initialisation du vaisseau world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE-15, SHIP_SIZE, SHIP_SIZE, '0', 0); world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_REAL_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0); @@ -54,24 +62,25 @@ void init_data(world_t * world){ world->vaisseauMini->dy = SCREEN_HEIGHT - (110); world->timer = SDL_GetTicks(); world->temps_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string temps - - world->coins_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string coins world->coins_str[0] = '\0'; world->coins_str = strcats(world->coins_str, 1, "0"); + world->angle = 0.0; world->isFlipping = 0; - world->isMenu = 1; + world->gamestate = 1; world->mouseX = 0; world->mouseY = 0; - world->money = 0; + world->parallax = 0; world->invicibility = false; -} + print_sprite(world->vaisseau); +} + void outBorder(world_t *world){ if (isOverScreen(world->vaisseau)){ @@ -93,13 +102,35 @@ int timer_update_s(world_t *world){ return 0; } -void InitMenu(world_t * world){ +void init_menu(world_t * world){ world->play = init_btn(203,129, 218, 97); world->exit = init_btn(202,294, 218, 97); world->magasin = init_btn(436, 217, 218, 97); world->sound = init_btn(0, 0, 100, 100); + + world->coin_menu_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string coins + world->coin_menu_str[0] = '\0'; + world->coin_menu_str = strcats(world->coin_menu_str, 1, int_to_str(world->money)); } +void init_shop(world_t * world){ + world->ship1 = init_btn(74,145, 191, 139); + world->ship2 = init_btn(287,143, 191, 139); + world->ship3 = init_btn(491,138, 191, 139); + world->ship4 = init_btn(290,308, 191, 139); + + world->Spr_ship = malloc(sizeof(sprite_t*)*4); + world->Spr_ship[0] = init_sprite(world->Spr_ship[0], 74,145, 191, 139, 'x', 0); + world->Spr_ship[1] = init_sprite(world->Spr_ship[1], 287,143, 191, 139, 'x', 0); + world->Spr_ship[2] = init_sprite(world->Spr_ship[2], 491,138, 191, 139, 'x', 0); + world->Spr_ship[3] = init_sprite(world->Spr_ship[3], 290,308, 191, 139, 'x', 0); + + world->shopPrice = malloc(sizeof(int)*4); + world->shopPrice[0] = 200; + world->shopPrice[1] = 500; + world->shopPrice[2] = 1000; + world->shopPrice[3] = 2000; +} void collide(sprite_t *sp1, sprite_t *sp2, world_t *world){ if (sprites_collide(sp1, sp2)){ @@ -204,6 +235,13 @@ void clean_data(world_t *world){ free(world->exit); free(world->magasin); free(world->sound); + free(world->ship1); + free(world->ship2); + free(world->ship3); + free(world->ship4); + free(world->Spr_ship); + free(world->shopPrice); + free(world->coin_menu_str); printf("clean_data"); } \ No newline at end of file diff --git a/library/World/world.h b/library/World/world.h index c60ba3a71d6e5bd9e82779381dcb5f03e7fa43db..5c1df510d0224394a1ac82d5e00a843e2d67c3ec 100644 --- a/library/World/world.h +++ b/library/World/world.h @@ -44,12 +44,20 @@ struct world_s{ btn_t *magasin; btn_t *sound; + btn_t *ship1; + btn_t *ship2; + btn_t *ship3; + btn_t *ship4; + sprite_t *BarreProgression; sprite_t *vaisseauMini; sprite_t *soleilBarre; sprite_t *soleil; sprite_t *air; sprite_t *coins; + + sprite_t **Spr_ship; + int *shopPrice; int nb_murs; // Nombre de météorites int nb_lines_murs; // Nombre de lignes de météorites sprite_t *ligneArriver; @@ -59,10 +67,12 @@ struct world_s{ unsigned int timer; /*!< Timer de jeu */ char * temps_str; // String affichant le temps sur le jeu char * coins_str; + char * coin_menu_str; + double angle; // Angle de rotation de la map int isFlipping; // Indique si l'on est en train de faire une rotation de l'écran et dans quelle sens (0 : non droite, 1 : vers la droite, -1 : vers la gauche, -2 : non gauche) - int isMenu; + int gamestate; int mouseX; int mouseY; @@ -71,6 +81,8 @@ struct world_s{ int parallax; bool invicibility; + + }; typedef struct world_s world_t; @@ -168,5 +180,12 @@ void allEvents(world_t *world); * * @param world */ -void InitMenu(world_t *world); +void init_menu(world_t *world); + +/** + * @brief La fonction qui initialise le shop + * + * @param world + */ +void init_shop(world_t * world); #endif diff --git a/library/World/world.o b/library/World/world.o index bed725f70d951aa874e65dcf01f1deb1b6267f03..5fbe69606a5c0baa3bfccc98b79428e8559c6cb7 100644 Binary files a/library/World/world.o and b/library/World/world.o differ diff --git a/library/utility/utility.c b/library/utility/utility.c index 492d747cf9b3e2d9e91b255de3007dcaf3ebf525..d876c38b9b1eb503f349ae07dd9c572e4930f24f 100644 --- a/library/utility/utility.c +++ b/library/utility/utility.c @@ -44,10 +44,18 @@ char **lirefile(char *filename, int *num_lines) { } int number_of_numbers(int n){ + if (n == 0){ + return 1; + } int count = 0; while (n != 0){ n /= 10; count++; } return count; +} + +void modify_str(char *str, char *str2){ + str[0] = '\0'; + strcat(str, str2); } \ No newline at end of file diff --git a/library/utility/utility.h b/library/utility/utility.h index 5d6567d9a9dedac457be3144ff8a079c51ab90f6..3d462b128a118657b2a850e0f021b2f956f30379 100644 --- a/library/utility/utility.h +++ b/library/utility/utility.h @@ -24,6 +24,7 @@ */ char * strcats(char* dest, int num_args, ...); +void modify_str(char *str, char *str2); /** * \brief Convertit un entier en chaine de caractère * @@ -42,5 +43,11 @@ char * int_to_str(int n); char ** lirefile(char *filename, int *num_lines); +/** + * @brief Retourne le nombre de chiffres d'un entier + * + * @param n + * @return int + */ int number_of_numbers(int n); #endif \ No newline at end of file diff --git a/library/utility/utility.o b/library/utility/utility.o index 7133de4d09f644f9a7255773bb41e3367c335008..6d8022279078ab3e448c8e8a3e13ec2d6306e722 100644 Binary files a/library/utility/utility.o and b/library/utility/utility.o differ diff --git a/main.c b/main.c index af313de129f5937394a431d8ad1ffee6e66a01d3..c58cf0270df255896c078f3b9595817909cc76e3 100644 --- a/main.c +++ b/main.c @@ -24,7 +24,7 @@ void handle_events(SDL_Event *event,world_t *world){ const Uint8 *keystates = SDL_GetKeyboardState(NULL); - if (!world->isMenu){ + if (world->gamestate == 0){ if (keystates[SDL_SCANCODE_A]){ world->vaisseau->x -= MOVING_STEP; } @@ -46,7 +46,7 @@ void handle_events(SDL_Event *event,world_t *world){ //si une touche est appuyée if(event->type == SDL_KEYDOWN){ //si la touche appuyée est 'D' - if (!world->isMenu){ + if (!world->gamestate){ switch (event->key.keysym.sym){ case SDLK_z: world->speed_h = 4; @@ -65,23 +65,46 @@ void handle_events(SDL_Event *event,world_t *world){ // print_sprite(world->vaisseau); } if (event->type == SDL_MOUSEBUTTONDOWN){ - printf("aaa"); - if (world->isMenu){ + if (world->gamestate == 1){ if (event->button.button == SDL_BUTTON_LEFT){ printf("mouse(%d, %d) world rect (%d %d %d %d)\n", world->mouseX, world->mouseY, world->play->rect.x,world->play->rect.y,world->play->rect.w,world->play->rect.h ); if (collidePoint(world->play, world->mouseX, world->mouseY)){ printf("tesy"); - world->isMenu = false; + world->gamestate = 0; } if (collidePoint(world->magasin, world->mouseX, world->mouseY)){ printf("tesy"); - world->isMenu = false; + world->gamestate = 2; } if (collidePoint(world->exit, world->mouseX, world->mouseY)){ printf("tesy"); world->gameover = true; } } + }else if(world->gamestate == 2){ + if (event->button.button == SDL_BUTTON_LEFT){ + if (collidePoint(world->ship1, world->mouseX, world->mouseY)){ + printf("ship1"); + world->money -= world->shopPrice[0]; + modify_str(world->coins_str, int_to_str(world->money)); + printf("%s\n", world->coins_str); + } + if (collidePoint(world->ship2, world->mouseX, world->mouseY)){ + printf("ship2"); + world->money -= world->shopPrice[1]; + modify_str(world->coins_str, int_to_str(world->money)); + } + if (collidePoint(world->ship3, world->mouseX, world->mouseY)){ + printf("ship3"); + world->money -= world->shopPrice[2]; + modify_str(world->coins_str, int_to_str(world->money)); + } + if (collidePoint(world->ship4, world->mouseX, world->mouseY)){ + printf("ship4"); + world->money -= world->shopPrice[3]; + modify_str(world->coins_str, int_to_str(world->money)); + } + } } } } @@ -103,9 +126,11 @@ void handle_events(SDL_Event *event,world_t *world){ void init(SDL_Window **window, SDL_Renderer ** renderer, ressources_t *textures, world_t * world){ init_sdl(window,renderer,SCREEN_WIDTH, SCREEN_HEIGHT); + init_data(world); // Initialisation du ttf init_ttf(); + init_ressource(*renderer,textures); } diff --git a/main.o b/main.o index 8b76c46dce9498fb4c8f9694e369af6bd6752e7b..934d1dada2a4d289b2ab2bc031b202faaebc9669 100644 Binary files a/main.o and b/main.o differ diff --git a/ressources/Elements/shop.png b/ressources/Elements/shop.png new file mode 100644 index 0000000000000000000000000000000000000000..4cf01ef13aafc8309cff62e9cee574d3dcaaab3f Binary files /dev/null and b/ressources/Elements/shop.png differ diff --git a/ressources/Elements/shop/buy.png b/ressources/Elements/shop/buy.png new file mode 100644 index 0000000000000000000000000000000000000000..2a1d8a927faed6cc77a5541d3c296fffd5d9900b Binary files /dev/null and b/ressources/Elements/shop/buy.png differ diff --git a/ressources/Elements/shop/nomoney.png b/ressources/Elements/shop/nomoney.png new file mode 100644 index 0000000000000000000000000000000000000000..a025038c24084db75b7543213613366ad33aa7e3 Binary files /dev/null and b/ressources/Elements/shop/nomoney.png differ diff --git a/ressources/Elements/shop/sell.png b/ressources/Elements/shop/sell.png new file mode 100644 index 0000000000000000000000000000000000000000..0fc53b118c402372fa6f8cb5c3ce7cbd10ca6bf3 Binary files /dev/null and b/ressources/Elements/shop/sell.png differ diff --git a/spacecorridor.exe b/spacecorridor.exe index d1c7cc94b14c68e5f53630feae929233191f50c8..cd303255b2cfd0766a46d5773199598f2e5a079f 100644 Binary files a/spacecorridor.exe and b/spacecorridor.exe differ diff --git a/test.c b/test.c index e50f539b50b5edc96c16a9670ded25bacd3b7ca2..24fdf7c2276a2da4706c4e15cea4901a58ed6eb4 100644 --- a/test.c +++ b/test.c @@ -136,7 +136,7 @@ int test_allEvents(world_t *world){ } int test_InitMenu(world_t *world){ - InitMenu(world); + init_menu(world); return 1; // Pas d'erreur } diff --git a/test.exe b/test.exe index 594c398f31ae9c6433cec186efc00ceae8b29857..41f8a1a53a23cafa4f9bc3c3dfe8719e911ca4c2 100644 Binary files a/test.exe and b/test.exe differ diff --git a/test.o b/test.o index ab5695a3e4ffab7011ac8f268e1a4877e558db6c..6c64885833d69c700b81838bcc628bb9189dce9c 100644 Binary files a/test.o and b/test.o differ