Skip to content

Commit

Permalink
add: destroys
Browse files Browse the repository at this point in the history
  • Loading branch information
savalet committed Nov 25, 2024
1 parent 2afdd0a commit 901bdee
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 58 deletions.
10 changes: 8 additions & 2 deletions include/hunter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ typedef struct {
sfVector2u size;
int version;
int touched;
int hide;
} sprite_t;

typedef struct {
sfText *text;
sfFont *font;
} text_t;

typedef struct {
sfClock *clock;
sfTime time;
Expand All @@ -44,8 +50,8 @@ typedef struct {
int score;
int shoot;
int ammo;
sfText *score_text;
sfText *pause_text;
text_t score_text;
text_t pause_text;
int paused;
int remaining_ducks;
float clock2_time;
Expand Down
47 changes: 35 additions & 12 deletions src/hunter.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,38 @@
#include <stdbool.h>
#include <stdlib.h>

static
void destroy_all2(hunterinfo_t *hf)
{
for (int i = 0; i < DUCK_NBR; i++)
if (hf->ducks[i].sprite != NULL)
sfSprite_destroy(hf->ducks[i].sprite);
for (int i = 0; i < AMMO_COUNT; i++)
if (hf->ammos[i].sprite != NULL)
sfSprite_destroy(hf->ammos[i].sprite);
for (int i = 0; i < AMMO_COUNT; i++)
if (hf->ducks[i].texture != NULL)
sfTexture_destroy(hf->ducks[i].texture);
for (int i = 0; i < AMMO_COUNT; i++)
if (hf->ammos[i].texture != NULL)
sfTexture_destroy(hf->ammos[i].texture);
}

void destroy_all(hunterinfo_t *hf)
{
destroy_all2(hf);
sfText_destroy(hf->score_text.text);
sfFont_destroy(hf->score_text.font);
sfSprite_destroy(hf->background.sprite);
sfTexture_destroy(hf->background.texture);
sfSprite_destroy(hf->tree.sprite);
sfTexture_destroy(hf->tree.texture);
sfSprite_destroy(hf->cursor.sprite);
sfTexture_destroy(hf->cursor.texture);
sfClock_destroy(hf->clock.clock);
sfClock_destroy(hf->clock2.clock);
sfRenderWindow_destroy(hf->window);
}

static
void display_all(hunterinfo_t *hf)
Expand All @@ -20,7 +52,7 @@ void display_all(hunterinfo_t *hf)
sfRenderWindow_drawSprite(hf->window, hf->tree.sprite, NULL);
display_ammos(hf);
sfRenderWindow_drawSprite(hf->window, hf->cursor.sprite, NULL);
sfRenderWindow_drawText(hf->window, hf->score_text, NULL);
sfRenderWindow_drawText(hf->window, hf->score_text.text, NULL);
sfRenderWindow_display(hf->window);
}

Expand Down Expand Up @@ -58,12 +90,6 @@ void handle_hunter_loop(hunterinfo_t *hf)
sfClock_restart(hf->clock.clock);
}
if (MICRO_TO_SEC(hf->clock2.time) > hf->clock2_time) {
if (hf->move_count == 1000) {
hf->move_count = 0;
hf->clock2_time /= 2;
fill_ducks(hf);
hf->round++;
}
second_clock(hf);
hf->move_count++;
sfClock_restart(hf->clock2.clock);
Expand All @@ -79,7 +105,6 @@ void draw_all(hunterinfo_t *hf)
fill_ammos(hf);
draw_cursor(hf);
draw_score(hf);
draw_pause(hf);
create_clock(hf);
}

Expand All @@ -98,10 +123,8 @@ int hunter(void)
event_manager(&hf);
handle_hunter_loop(&hf);
if (!hf.remaining_ducks)
return (my_printf("WIN SCORE: %d, AC: %.2f\n", hf.score, hf.score /
hf.shoot), EXIT_SUCCESS);
break;
}
if (hf.shoot)
my_printf("SHOOT: %d, AC: %.2f\n", hf.shoot, hf.score / hf.shoot);
destroy_all(&hf);
return EXIT_SUCCESS;
}
8 changes: 4 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
static
void print_usages(void)
{
my_printf("My_hunter is a litle game like duck hunt\n");
my_printf("Usage: ./my_hunter [-h]\nThe goal of the game is to shoot the");
my_printf(" ducks by clicking on them and try to achieve the highest");
my_printf(" score.\n");
my_putstr("My_hunter is a litle game like duck hunt\n");
my_putstr("Usage: ./my_hunter [-h]\nThe goal of the game is to shoot the");
my_putstr(" ducks by clicking on them and try to achieve the highest");
my_putstr(" score.\n");
}

int main(int ac, char **av)
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/duck.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
void display_ducks(hunterinfo_t *hf)
{
for (int i = 0; i < DUCK_NBR; i++)
if (hf->ducks[i].sprite != NULL)
if (hf->ducks[i].sprite != NULL && !hf->ducks[i].hide)
sfRenderWindow_drawSprite(hf->window, hf->ducks[i].sprite, NULL);
}

Expand Down Expand Up @@ -46,7 +46,7 @@ int move_duck(hunterinfo_t *hf, sfVector2f pos, int i)
if (hf->ducks[i].sprite == NULL)
return EXIT_SUCCESS;
if (hf->ducks[i].pos.y > 1100) {
hf->ducks[i].sprite = NULL;
hf->ducks[i].hide = 1;
return EXIT_SUCCESS;
}
sfSprite_move(hf->ducks[i].sprite, pos);
Expand Down
25 changes: 0 additions & 25 deletions src/renderer/menu.c

This file was deleted.

19 changes: 9 additions & 10 deletions src/renderer/score.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@

int draw_score(hunterinfo_t *hf)
{
sfFont *font = sfFont_createFromFile("assets/Jersey25-Regular.ttf");

if (!font)
hf->score_text.font = sfFont_createFromFile("assets/Jersey25-Regular.ttf");
if (!hf->score_text.font)
return EXIT_FAILURE;
hf->score_text = sfText_create();
sfText_setString(hf->score_text, "0");
sfText_setFont(hf->score_text, font);
sfText_setCharacterSize(hf->score_text, 50);
sfText_setPosition(hf->score_text, (sfVector2f){ 10, 0 });
hf->score_text.text = sfText_create();
sfText_setString(hf->score_text.text, "0");
sfText_setFont(hf->score_text.text, hf->score_text.font);
sfText_setCharacterSize(hf->score_text.text, 50);
sfText_setPosition(hf->score_text.text, (sfVector2f){ 10, 0 });
return EXIT_SUCCESS;
}

int inc_score(hunterinfo_t *hf)
{
char score[10];

if (hf->score_text == NULL)
if (hf->score_text.text == NULL)
return EXIT_FAILURE;
my_numstr(score, hf->score);
sfText_setString(hf->score_text, score);
sfText_setString(hf->score_text.text, score);
return EXIT_SUCCESS;
}
3 changes: 0 additions & 3 deletions src/window/window_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void reload(hunterinfo_t *hf)
{
hf->ammo = AMMO_COUNT;
fill_ammos(hf);
my_printf("RELOADED AMMO COUNT: %d\n", hf->ammo);
}

static
Expand All @@ -48,7 +47,6 @@ void kill_duck(hunterinfo_t *hf, int i)
hf->ammos[hf->ammo].sprite = NULL;
hf->remaining_ducks--;
inc_score(hf);
my_printf("DUCK KILLED: %d\n", hf->score);
}

static
Expand All @@ -58,7 +56,6 @@ void shoot(hunterinfo_t *hf)

if (hf->ammo <= 0)
return;
my_printf("SHOOT %d, %d\n", pos.x, pos.y);
hf->shoot++;
for (int i = 0; i < DUCK_NBR; i++) {
if (pos.x < hf->ducks[i].pos.x || pos.x >
Expand Down

0 comments on commit 901bdee

Please sign in to comment.