Skip to content

Commit f9c7073

Browse files
authored
Merge pull request #2 from firefly-zero/stats
Add add_progress and add_score
2 parents 7cb1b60 + fe5699e commit f9c7073

4 files changed

Lines changed: 80 additions & 7 deletions

File tree

Taskfile.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,12 @@ tasks:
1212
cmds:
1313
- task: install-doxygen
1414
- doxygen Doxyfile
15+
16+
release:
17+
desc: publish release
18+
cmds:
19+
- test {{.CLI_ARGS}}
20+
- git tag {{.CLI_ARGS}}
21+
- git push
22+
- git push --tags
23+
- gh release create --generate-notes {{.CLI_ARGS}}

examples/image-c/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ BOOT void boot()
99
// You could also use `get_file_size` to detect the file size
1010
// in runtime but that requires using an allocator.
1111
const size_t fileSize = 97;
12-
Buffer buf;
1312
char head[fileSize];
14-
buf.head = head;
15-
buf.size = fileSize;
13+
Buffer buf = {.head = head, .size = fileSize};
1614
image = load_file("img", buf);
1715
}
1816

src/firefly.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,9 @@ uintptr_t get_random()
283283
Buffer get_name(Buffer buf)
284284
{
285285
int32_t size = _ffb_get_name((int)buf.head, buf.size);
286-
File name;
287-
name.size = size;
288-
name.head = buf.head;
286+
File name = {
287+
.size = size,
288+
.head = buf.head};
289289
return name;
290290
}
291291

@@ -301,6 +301,48 @@ void quit()
301301
_ffb_quit();
302302
}
303303

304+
// -- STATS -- //
305+
306+
/// @brief Add the given value to the progress for the badge.
307+
///
308+
/// @details May be negative if you want to decrease the progress.
309+
/// If zero, does not change the progress.
310+
///
311+
/// If the Peer is COMBINED, the progress is added to every peer
312+
/// and the returned value is the lowest progress.
313+
Progress add_progress(Peer p, Badge b, int16_t v)
314+
{
315+
uint32_t r = _ffb_add_progress(p, b, v);
316+
Progress progress = {
317+
.done = r >> 16,
318+
.goal = r};
319+
return progress;
320+
}
321+
322+
/// @brief Get the progress of earning the badge.
323+
Progress get_progress(Peer p, Badge b)
324+
{
325+
return add_progress(p, b, 0);
326+
}
327+
328+
/// @brief Add the given score to the board.
329+
///
330+
/// @details May be negative if you want the lower scores
331+
/// to rank higher. Zero value is not added to the board.
332+
///
333+
/// If the Peer is COMBINED, the score is added for every peer
334+
/// and the returned value is the lowest of their best scores.
335+
Score add_score(Peer p, Board b, Score v)
336+
{
337+
return _ffb_add_score(p, b, v);
338+
}
339+
340+
/// @brief Get the personal best of the player.
341+
Score get_score(Peer p, Board b)
342+
{
343+
return add_score(p, b, 0);
344+
}
345+
304346
// -- AUDIO -- //
305347

306348
/// @brief Add sine AudioNode as a child node for the given node.

src/firefly.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
/// @brief Mark a "render_line" callback function.
1717
#define RENDER_LINE __attribute__((export_name("render_line")))
1818

19+
/// @brief Mark a "cheat" callback function.
20+
#define CHEAT __attribute__((export_name("cheat")))
21+
1922
/// @brief Screen width.
2023
#define WIDTH 240
2124
/// @brief Screen height.
@@ -222,7 +225,20 @@ typedef struct Peers Peers;
222225
typedef int32_t Peer;
223226

224227
/// @brief A peer ID representing all peers at once.
225-
#define COMBINED = 0xFF
228+
const Peer COMBINED = 0xFF;
229+
230+
// -- STATS -- //
231+
232+
typedef uint32_t Badge;
233+
typedef uint32_t Board;
234+
typedef int32_t Score;
235+
236+
struct Progress
237+
{
238+
uint16_t done;
239+
uint16_t goal;
240+
};
241+
typedef struct Progress Progress;
226242

227243
// -- AUDIO -- //
228244

@@ -237,6 +253,9 @@ struct AudioNode
237253
};
238254
typedef struct AudioNode AudioNode;
239255

256+
/// @brief The root audio node. Its child nodes are mixed and played on the device output.
257+
const AudioNode OUT = {.id = 0};
258+
240259
/// @brief A parameter of an audio node that can be modulated.
241260
enum ModParam
242261
{
@@ -368,6 +387,11 @@ Buffer get_name(Buffer buf);
368387
void restart();
369388
void quit();
370389

390+
Progress add_progress(Peer p, Badge b, int16_t v);
391+
Progress get_progress(Peer p, Badge b);
392+
Score add_score(Peer p, Badge b, Score v);
393+
Score get_score(Peer p, Badge b);
394+
371395
AudioNode add_sine(AudioNode parent, float freq, float phase);
372396
AudioNode add_square(AudioNode parent, float freq, float phase);
373397
AudioNode add_sawtooth(AudioNode parent, float freq, float phase);

0 commit comments

Comments
 (0)