This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
101 lines (79 loc) · 1.99 KB
/
main.c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <locale.h>
#include <wiringPi.h>
#include <time.h>
#include <sys/ioctl.h>
#include "main.h"
#include "utils.h"
#include "game_object.h"
#include "painter.h"
#include "sprite.h"
#include "sprite_cards.h"
int main () {
game_t* game = (game_t*) malloc(sizeof(game_t));
setlocale(LC_ALL, "");
wprintf(L"[INFO] Setup...\n");
if (setup(game) < 0) {
return -1;
}
return game_start(game);
}
int setup (game_t* game) {
// check terminal size
struct winsize term_size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &term_size);
if (term_size.ws_col < 80 || term_size.ws_row < 24) {
printf("[CRIT] Terminal size is too small.\n");
printf("[CRIT] Please increase the size of the terminal and try again!\n");
printf("[CRIT] HINT: Minimum size is: 80 columns, 24 rows.\n");
return -1;
}
// Setup painter
game->painter = (painter_t*) malloc(sizeof(painter_t));
painter_update_screen(game->painter, term_size.ws_col, term_size.ws_row);
// Setup object list
game->object_list = create_game_object_list();
// Setup GPIO
if (wiringPiSetupGpio() == -1) {
printf("[CRIT] Failed to setup GPIO.");
return -1;
}
return 0;
}
int game_start (game_t* game) {
int last_update = 0;
int current_time = 0;
int delta_time = 0;
sprite_t* sprite = CREATE_CARD_SPRITE('S', 1);
if (sprite == NULL) {
printf("Failed to create sprite!\n");
return -1;
}
game_object_t* object = create_object(5, 3, sprite);
go_list_insert(game->object_list, object);
while (1) {
current_time = millis();
delta_time = current_time - last_update;
game_update(game, delta_time);
}
return 0;
}
int game_update (game_t* game, double delta_time) {
if (game_render(game) != 0) {
return -1;
}
return 0;
}
int game_render (game_t* game) {
cls();
game_object_list_node_t* object_node = game->object_list->first_node;
for (;;) {
if (object_node == NULL) {
break;
}
painter_draw_object(game->painter, object_node->object);
}
return 0;
}