-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaperplane.c
208 lines (175 loc) · 6.76 KB
/
paperplane.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <furi.h>
#include <furi_hal.h>
#include <furi_hal_random.h>
#include <gui/gui.h>
#include <gui/icon_i.h>
#include <gui/elements.h>
#include <input/input.h>
#include <stdlib.h>
#include <stdio.h>
#include "game_state.h"
#include "paper_plane_icons.h"
#define FPS 20
#define SPRITE_SIZE 8
typedef enum {
EventTypeTick,
EventTypeKey,
} EventType;
typedef struct {
EventType type;
InputEvent input;
} PluginEvent;
static void timer_callback(void* ctx) {
GameState* game_state = ctx;
furi_mutex_acquire(game_state->mutex, FuriWaitForever);
if(game_state == NULL) {
return;
}
uint32_t ticks_elapsed = furi_get_tick() - game_state->last_tick;
game_state->last_tick = furi_get_tick();
int delta_time_ms = ticks_elapsed * 1000 / furi_kernel_get_tick_frequency();
if(!game_state->crash_flag) {
update_position(game_state->paper, delta_time_ms);
check_collision(game_state);
}
furi_mutex_release(game_state->mutex);
}
static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
furi_assert(event_queue);
PluginEvent event = {.type = EventTypeKey, .input = *input_event};
furi_message_queue_put(event_queue, &event, FuriWaitForever);
}
static void render_callback(Canvas* const canvas, void* ctx) {
const GameState* game_state = ctx;
furi_mutex_acquire(game_state->mutex, FuriWaitForever);
if(game_state == NULL) {
return;
}
// draw map (this should probably be it's own function)
float background_position = game_state->paper->y;
for(int i = background_position; i < background_position + 10; i++) {
/*
using a u_int32_t here so that bits
that have been shifted out can still
be read.
*/
u_int32_t currentRow = game_state->map[i];
for(unsigned int j = 0; j < sizeof(uint16_t) * 8; j++) {
/*
0x8000 is 1 with 15 zeros
00000000000000001000000000000000 - 0x8000
00000000000000001111111001111111 - map data (currentRow)
using & will result in:
00000000000000001000000000000000
the above number will evaluate to true
OR
00000000000000001000000000000000 - 0x8000
00000000000000000111111001111111 - map data (currentRow)
using & will result in:
00000000000000000000000000000000
the above number will result in false
*/
if(currentRow & 0x8000) {
const Icon* ground_to_draw = &I_Ground;
// if the bit to the left is 0, use the right facing ground sprite
if(!(currentRow & 0x4000)) {
ground_to_draw = &I_GroundRight;
}
// if the bit to the right is 0, use the left facing ground tile
if(!(currentRow & 0x10000)) {
ground_to_draw = &I_GroundLeft;
}
canvas_draw_icon(
canvas,
j * SPRITE_SIZE,
i * SPRITE_SIZE - background_position * SPRITE_SIZE,
ground_to_draw);
}
// bit shift currentRow to the left, so the bit to the right will be drawn
currentRow <<= 1;
}
}
// draw plane
canvas_draw_icon(
canvas, game_state->paper->x * SPRITE_SIZE, PAPER_START_Y, game_state->paper->icon);
// Show score
char score_string[11]; // length is 11 b/c: Score: xxx\0
canvas_draw_icon(canvas, 77, 2, &I_Score);
snprintf(
score_string, 11, "Score: %d", (int)game_state->paper->y); // copy score into score_string
canvas_draw_str_aligned(canvas, 80, 5, AlignLeft, AlignTop, score_string);
furi_mutex_release(game_state->mutex);
}
int32_t paperplane_app() {
FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
GameState* game_state = malloc(sizeof(GameState));
game_state_init(game_state);
if(!game_state->mutex) {
FURI_LOG_E("Paper Plane", "cannot create mutex\r\n");
// game crash, all initialized items must be freed.
furi_message_queue_free(event_queue);
//furi_timer_free(game_state->timer); this causes a null pointer dereference
free(game_state->paper);
free(game_state->map);
free(game_state);
return 255;
}
// Set system callbacks
ViewPort* view_port = view_port_alloc();
view_port_draw_callback_set(view_port, render_callback, game_state);
view_port_input_callback_set(view_port, input_callback, event_queue);
game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
furi_timer_start(game_state->timer, (uint32_t)furi_kernel_get_tick_frequency() / FPS);
// Open GUI and register view_port
Gui* gui = furi_record_open("gui");
gui_add_view_port(gui, view_port, GuiLayerFullscreen);
PluginEvent event;
for(bool processing = true; processing;) {
FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
if(event_status == FuriStatusOk) {
// press events
if(event.type == EventTypeKey) {
if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
event.input.type == InputTypeRepeat) {
switch(event.input.key) {
case InputKeyUp:
break;
case InputKeyDown:
break;
case InputKeyLeft:
rotate_left(game_state->paper);
break;
case InputKeyRight:
rotate_right(game_state->paper);
break;
case InputKeyOk:
if(game_state->crash_flag) {
game_state_reinit(game_state);
break;
}
break;
case InputKeyMAX:
break;
case InputKeyBack:
// Exit the app
processing = false;
break;
}
}
}
}
furi_mutex_release(game_state->mutex);
view_port_update(view_port);
}
view_port_enabled_set(view_port, false);
gui_remove_view_port(gui, view_port);
furi_record_close("gui");
view_port_free(view_port);
furi_message_queue_free(event_queue);
furi_mutex_free(game_state->mutex);
furi_timer_free(game_state->timer);
free(game_state->paper);
free(game_state->map);
free(game_state);
return 0;
}