forked from NotaSurgele/Bootstrap_CSFML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
42 lines (40 loc) · 1.67 KB
/
main.c
File metadata and controls
42 lines (40 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <SFML/Graphics.h>
#include "struct.h"
int main(void)
{
sfVideoMode mode = {800, 600, 32};
sfRenderWindow* window = sfRenderWindow_create(mode, "Space invaders", sfClose, NULL);
sfEvent event = {0};
player_t *player = create_player("assets/ship_1.png", V2F(400, 200));
cursor_t *cursor = create_cursor("assets/laser.png");
sfClock *clock = sfClock_create();
sfTime time = {0};
sfRenderWindow_setMouseCursorVisible(window, sfFalse);
while (sfRenderWindow_isOpen(window)) {
time = sfClock_restart(clock);
float deltaTime = sfTime_asSeconds(time);
while (sfRenderWindow_pollEvent(window, &event)) {
cursor->mouse_pos = sfMouse_getPositionRenderWindow(window);
if (event.type == sfEvtClosed)
sfRenderWindow_close(window);
if (sfKeyboard_isKeyPressed(sfKeyD))
sfSprite_move(player->sprite, V2F(50 * deltaTime, 0));
if (sfKeyboard_isKeyPressed(sfKeyQ))
sfSprite_move(player->sprite, V2F(-50 * deltaTime, 0));
if (sfMouse_isButtonPressed(sfMouseLeft)) {
if (sfFloatRect_contains(&player->collision_box,
cursor->mouse_pos.x, cursor->mouse_pos.y)) {
player->texture = sfTexture_createFromFile("assets/ship_2.png", NULL);
sfSprite_setTexture(player->sprite, player->texture, sfTrue);
}
}
}
sfRenderWindow_clear(window, sfBlack);
draw_player(player, window);
draw_cursor(cursor, window);
sfRenderWindow_display(window);
}
sfRenderWindow_destroy(window);
return 0;
}