Skip to content

Commit

Permalink
add: super canard qui bouge
Browse files Browse the repository at this point in the history
  • Loading branch information
savalet committed Nov 19, 2024
1 parent 8535a6d commit f0b7dcf
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 44 deletions.
10 changes: 7 additions & 3 deletions include/hunter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,35 @@
#define EXIT_FAILURE 84
#define EXIT_SUCCESS 0
#define CURSOR_SIZE 128
#define DUCK_NBR 4

typedef struct {
sfTexture *texture;
sfSprite *sprite;
sfVector2f pos;
sfVector2u size;
int version;
} sprite_t;

typedef struct {
sfRenderWindow *window;
sfClock *clock;
sprite_t background;
sprite_t duck;
sprite_t cursor;
sprite_t ducks[DUCK_NBR];
sfEvent *event;
int score;
int shoot;
} hunterinfo_t;

int hunter(void);
int create_window(size_t x, size_t y, hunterinfo_t *);
int draw_background(hunterinfo_t *);
int draw_duck(hunterinfo_t *);
void event_manager(hunterinfo_t *);
int move_duck(hunterinfo_t *, sfVector2f, int);
int create_clock(hunterinfo_t *);
int draw_cursor(hunterinfo_t *);
int move_cursor(hunterinfo_t *, sfVector2f);
void handle_shoot(hunterinfo_t *);
int fill_ducks(hunterinfo_t *);
void display_ducks(hunterinfo_t *);
#endif
49 changes: 32 additions & 17 deletions src/hunter.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,54 @@
*/

#include "hunter.h"
#include "lib.h"
#include <stdbool.h>
#include <stdlib.h>

int hunter(void)
static
void handle_hunter_loop(hunterinfo_t *hf)
{
hunterinfo_t hf = { 0 };
int version = 0;
sfTime time;
float seconds;

event_manager(hf);
time = sfClock_getElapsedTime(hf->clock);
sfRenderWindow_clear(hf->window, sfBlack);
sfRenderWindow_drawSprite(hf->window, hf->background.sprite, NULL);
display_ducks(hf);
sfRenderWindow_drawSprite(hf->window, hf->cursor.sprite, NULL);
sfRenderWindow_display(hf->window);
for (int i = 0; i < DUCK_NBR; i++)
move_duck(hf, (sfVector2f){ 1, 0 }, i);
seconds = time.microseconds / 1000000.0;
if (seconds > 0.5) {
for (int i = 0; i < DUCK_NBR; i++)
hf->ducks[i].version++;
sfClock_restart(hf->clock);
}
}

int hunter(void)
{
hunterinfo_t hf = { 0 };

create_window(1920, 1080, &hf);
sfRenderWindow_setMouseCursorVisible(hf.window, false);
draw_background(&hf);
draw_duck(&hf);
fill_ducks(&hf);
draw_cursor(&hf);
create_clock(&hf);
sfRenderWindow_setFramerateLimit(hf.window, 300);
while (sfRenderWindow_isOpen(hf.window)) {
event_manager(&hf);
time = sfClock_getElapsedTime(hf.clock);
sfRenderWindow_clear(hf.window, sfBlack);
sfRenderWindow_drawSprite(hf.window, hf.background.sprite, NULL);
sfRenderWindow_drawSprite(hf.window, hf.duck.sprite, NULL);
sfRenderWindow_drawSprite(hf.window, hf.cursor.sprite, NULL);
sfRenderWindow_display(hf.window);
move_duck(&hf, (sfVector2f){ 1, 0 }, version);
seconds = time.microseconds / 1000000.0;
if (seconds > 0.5) {
version++;
if (version == 3)
version = 0;
sfClock_restart(hf.clock);
handle_hunter_loop(&hf);
if (hf.score == DUCK_NBR && hf.shoot) {
my_printf("WIN SCORE: %d, AC: %d\n", hf.score, hf.score /
hf.shoot);
return (EXIT_SUCCESS);
}
}
if (hf.shoot)
my_printf("SHOOT: %d, AC: %d\n", hf.shoot, hf.score / hf.shoot);
return EXIT_SUCCESS;
}
52 changes: 36 additions & 16 deletions src/renderer/duck.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,49 @@
** _
*/

#include "hunter.h"
#include <SFML/Graphics.h>
#include <stdio.h>
#include <stdlib.h>

#include "hunter.h"
void display_ducks(hunterinfo_t *hf)
{
for (int i = 0; i < DUCK_NBR; i++)
if (hf->ducks[i].sprite != NULL)
sfRenderWindow_drawSprite(hf->window, hf->ducks[i].sprite, NULL);
}

int draw_duck(hunterinfo_t *hf)
int fill_ducks(hunterinfo_t *hf)
{
hf->duck.texture = sfTexture_createFromFile("assets/duck.png", NULL);
if (!hf->duck.texture)
return EXIT_FAILURE;
hf->duck.sprite = sfSprite_create();
sfSprite_setTexture(hf->duck.sprite,
hf->duck.texture, sfTrue);
sfSprite_setTextureRect(hf->duck.sprite, (sfIntRect){ 0, 0, 110, 110 });
hf->duck.size = sfTexture_getSize(hf->duck.texture);
hf->duck.size.x /= 3;
hf->ducks[DUCK_NBR - 1].sprite = NULL;
for (int i = 0; i < DUCK_NBR; i++) {
hf->ducks[i].texture = sfTexture_createFromFile("assets/duck.png",
NULL);
if (!hf->ducks[i].texture)
return EXIT_FAILURE;
hf->ducks[i].sprite = sfSprite_create();
sfSprite_setTexture(hf->ducks[i].sprite,
hf->ducks[i].texture, sfTrue);
sfSprite_setTextureRect(hf->ducks[i].sprite,
(sfIntRect){ 110, 0, 110, 110 });
hf->ducks[i].size = sfTexture_getSize(hf->ducks[i].texture);
hf->ducks[i].size.x /= 3;
sfSprite_setPosition(hf->ducks[i].sprite, (sfVector2f){
rand() % 600 - 600, rand() % 1000 + 50});
hf->ducks[i].version = rand() % 4;
}
return EXIT_SUCCESS;
}

int move_duck(hunterinfo_t *hf, sfVector2f pos, int duck_version)
int move_duck(hunterinfo_t *hf, sfVector2f pos, int i)
{
sfSprite_move(hf->duck.sprite, pos);
hf->duck.pos = sfSprite_getPosition(hf->duck.sprite);
sfSprite_setTextureRect(hf->duck.sprite, (sfIntRect){ 110 * duck_version,
0, 110, 110 });
if (hf->ducks[i].sprite == NULL)
return EXIT_SUCCESS;
if (hf->ducks[i].version == 3)
hf->ducks[i].version = 0;
sfSprite_move(hf->ducks[i].sprite, pos);
hf->ducks[i].pos = sfSprite_getPosition(hf->ducks[i].sprite);
sfSprite_setTextureRect(hf->ducks[i].sprite, (sfIntRect){ 110 *
hf->ducks[i].version, 0, 110, 110 });
return EXIT_SUCCESS;
}
21 changes: 13 additions & 8 deletions src/window/window_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
** _
*/

#include "lib.h"
#include <SFML/Graphics.h>
#include <stdio.h>
#include "hunter.h"

int create_window(size_t x, size_t y, hunterinfo_t *hunterinfo)
Expand All @@ -25,13 +25,18 @@ void shoot(hunterinfo_t *hf)
{
sfVector2i pos = sfMouse_getPositionRenderWindow(hf->window);

printf("SHOOT %d, %d\n", pos.x, pos.y);
if (pos.x < hf->duck.pos.x || pos.x >
(hf->duck.pos.x + hf->duck.size.x) || pos.y <
hf->duck.pos.y || pos.y >
(hf->duck.pos.y + hf->duck.size.y))
return;
printf("SHOOTED\n");
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 >
(hf->ducks[i].pos.x + hf->ducks[i].size.x) || pos.y <
hf->ducks[i].pos.y || pos.y >
(hf->ducks[i].pos.y + hf->ducks[i].size.y))
continue;
hf->ducks[i].sprite = NULL;
hf->score++;
my_printf("SHOOTED SCORE: %d\n", hf->score);
}
}

void event_manager(hunterinfo_t *hf)
Expand Down

0 comments on commit f0b7dcf

Please sign in to comment.