comparison libmlk-core/mlk/core/game.h @ 645:83781cc87fca

core: rework game stack state mechanism The current model was fundamentally broken as the state could continue its execution when calling mlk_game_pop from itself (e.g. in update). The current model uses a sjlj mechanism with mlk_game_push/pop being disallowed in special state function like end, finish, suspend.
author David Demelier <markand@malikania.fr>
date Sun, 04 Feb 2024 15:24:00 +0100
parents 75944708c55c
children
comparison
equal deleted inserted replaced
644:6d0f4edb79f8 645:83781cc87fca
106 */ 106 */
107 void 107 void
108 mlk_game_init(void); 108 mlk_game_init(void);
109 109
110 /** 110 /**
111 * Try to append a new state into the game loop at the end unless the array is 111 * Append the state into the game stack and switch to it, suspending current
112 * full. 112 * state.
113 * 113 *
114 * The state is inserted as-is and ownership is left to the caller. 114 * The function takes ownership of the state and will be finalized later.
115 * 115 *
116 * \pre state != NULL 116 * \pre state != NULL
117 * \param state 117 * \param state the state to switch
118 * \return 0 on success or -1 on error
119 */ 118 */
120 int 119 _Noreturn void
121 mlk_game_push(struct mlk_state *state); 120 mlk_game_push(struct mlk_state *state);
122 121
123 /** 122 /**
124 * Pop the current state if any and resume the previous one. 123 * Pop the current state if any and resume the previous one.
125 */ 124 */
126 void 125 _Noreturn void
127 mlk_game_pop(void); 126 mlk_game_pop(void);
128
129 /**
130 * Call the current state's mlk_state::handle function unless it is inhibited
131 * or NULL.
132 *
133 * \pre event != NULL
134 * \param event the event
135 */
136 void
137 mlk_game_handle(const union mlk_event *event);
138
139 /**
140 * Call the current state's mlk_state::update function unless it is inhibited
141 * or NULL.
142 *
143 * \param ticks frame ticks
144 */
145 void
146 mlk_game_update(unsigned int ticks);
147
148 /**
149 * Call the current state's mlk_state::draw function unless it is inhibited
150 * or NULL.
151 */
152 void
153 mlk_game_draw(void);
154 127
155 /** 128 /**
156 * Enter a game loop until there is no more states. 129 * Enter a game loop until there is no more states.
157 * 130 *
158 * The current implementation will perform a loop capped to a 60 FPS rate and 131 * The current implementation will perform a loop capped to a 60 FPS rate and
159 * update the states with the appropriate number of ticks. 132 * update the states with the appropriate number of ticks.
133 *
134 * \pre state != NULL
135 * \param state the first state to run
160 */ 136 */
161 void 137 void
162 mlk_game_loop(void); 138 mlk_game_loop(struct mlk_state *state);
163 139
164 /** 140 /**
165 * Request the game loop to stop by removing all states. 141 * Request the game loop to stop by removing all states.
166 */ 142 */
167 void 143 void