-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.h
More file actions
77 lines (64 loc) · 2.67 KB
/
Copy pathcontext.h
File metadata and controls
77 lines (64 loc) · 2.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
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
// context.h — Global system state container.
//
// Header-only by design (the manifest lists no context.c): the struct
// definitions and the two tiny accessors below are small enough to live as
// `static inline` functions. Storage for g_context itself is defined once,
// in main.c.
#ifndef CONTEXT_H
#define CONTEXT_H
#include <stdint.h>
#include <stdbool.h>
// Top-level states for the hierarchical state machine. Each maps to an
// Enter/Update/Draw triplet dispatched from main.c's loop.
typedef enum {
SCREEN_BOOT = 0,
SCREEN_MENU,
SCREEN_SETTINGS,
SCREEN_GAME_LAUNCHER,
SCREEN_NAME_ENTRY,
SCREEN_GAME_SNAKE,
SCREEN_GAME_TETRIS,
SCREEN_GAME_BRICKS,
} ScreenState;
#define GAME_ID_SNAKE 0
#define GAME_ID_TETRIS 1
#define GAME_ID_BRICKS 2
// Live, user-adjustable settings, edited from the Settings screen.
typedef struct {
uint8_t brightness; // 0-100 (%) -> drives backlight.c's software PWM
uint8_t volume; // 0-100 (%) -> TODO: not yet wired into audio.c's
// output duty; see audio.c.
} SystemSettings;
// Single global state container threaded through every state handler.
typedef struct {
ScreenState screen; // state currently being dispatched
ScreenState nextScreen; // requested transition target
bool screenEntered; // true once the current screen's Enter() ran
uint32_t lastTickMs; // systemGetTimeMs() at the last frame
uint16_t deltaMs; // ms elapsed since the previous frame
// Global state shared across screens
uint8_t selectedGameId;
bool hasState[3]; // snake, tetris, bricks
SystemSettings settings;
} SystemContext;
extern SystemContext g_context; // defined once, in main.c
static inline void contextInit(void) {
g_context.screen = SCREEN_BOOT;
g_context.nextScreen = SCREEN_BOOT;
g_context.screenEntered = false;
g_context.lastTickMs = 0;
g_context.deltaMs = 0;
g_context.selectedGameId = 0;
g_context.settings.brightness = 50;
g_context.settings.volume = 75;
}
// ----------------------------------------------------------------------------
// Screen Management
// ----------------------------------------------------------------------------
// Any state handler calls this to ask main.c's loop to switch screens on
// the next frame. Transition itself (calling the new screen's Enter()) is
// handled centrally in main.c so it only ever happens once per switch.
static inline void contextRequestScreen(ScreenState next) {
g_context.nextScreen = next;
}
#endif // CONTEXT_H