Skip to content

Commit 4277b04

Browse files
committed
pokemon: refactor
This accomplishes a few things: - Retype vars introduced in previous commits to match format of existing project code. - Move common elements to a single struct for the whole application. This also removes the struct for each of the two current views and passes around the main struct as callback contexts. - Remove the use of view models as copies of data and instead point the model for each view at the main struct. All of the draw callbacks for existing views end up needing this data and only have access to a view model at runtime. - Partial cleanup of circular dependencies and unneeded variables. Some headers had circular includes which would compile but caused issues.
1 parent a75ac8f commit 4277b04

6 files changed

+196
-265
lines changed

pokemon_app.cpp

+42-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "pokemon_app.h"
2+
#include "views/trade.hpp"
3+
#include "views/select_pokemon.hpp"
24

3-
struct pokemon_lut pokemon_table[] = {
5+
const PokemonTable pokemon_table[] = {
46
{"Bulbasaur", &I_bulbasaur, 0x99},
57
{"Ivysaur", &I_ivysaur, 0x09},
68
{"Venusaur", &I_venusaur, 0x9A},
@@ -159,66 +161,77 @@ uint32_t pokemon_exit_confirm_view(void* context) {
159161
UNUSED(context);
160162
return AppViewExitConfirm;
161163
}
162-
App* pokemon_alloc() {
163-
App* app = (App*)malloc(sizeof(App));
164+
165+
PokemonFap* pokemon_alloc() {
166+
PokemonFap* pokemon_fap = (PokemonFap*)malloc(sizeof(PokemonFap));
164167

165168
// Gui
166-
app->gui = (Gui*)furi_record_open(RECORD_GUI);
169+
/* XXX: what is furi_record open for? It doesn't return a Gui handle. */
170+
pokemon_fap->gui = (Gui*)furi_record_open(RECORD_GUI);
171+
167172
// View dispatcher
168-
app->view_dispatcher = view_dispatcher_alloc();
173+
pokemon_fap->view_dispatcher = view_dispatcher_alloc();
169174

170-
view_dispatcher_enable_queue(app->view_dispatcher);
171-
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
172-
view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
175+
view_dispatcher_enable_queue(pokemon_fap->view_dispatcher);
176+
view_dispatcher_set_event_callback_context(pokemon_fap->view_dispatcher, pokemon_fap);
177+
view_dispatcher_attach_to_gui(
178+
pokemon_fap->view_dispatcher, pokemon_fap->gui, ViewDispatcherTypeFullscreen);
173179

174180
// Start Index first pokemon
175-
app->current_pokemon = 0;
181+
pokemon_fap->curr_pokemon = 0;
182+
183+
// Set up pointer to pokemon table
184+
pokemon_fap->pokemon_table = pokemon_table;
185+
176186
// Select Pokemon View
177-
app->select_pokemon = select_pokemon_alloc(app);
178-
view_set_previous_callback(select_pokemon_get_view(app), pokemon_exit_confirm_view);
187+
pokemon_fap->select_view = select_pokemon_alloc(pokemon_fap);
188+
view_set_previous_callback(select_pokemon_get_view(pokemon_fap), pokemon_exit_confirm_view);
179189
view_dispatcher_add_view(
180-
app->view_dispatcher, AppViewSelectPokemon, select_pokemon_get_view(app));
190+
pokemon_fap->view_dispatcher, AppViewSelectPokemon, select_pokemon_get_view(pokemon_fap));
181191

182192
// Trade View
183-
app->trade = trade_alloc(app);
184-
view_set_previous_callback(trade_get_view(app), pokemon_exit_confirm_view);
185-
view_dispatcher_add_view(app->view_dispatcher, AppViewTrade, trade_get_view(app));
193+
pokemon_fap->trade_view = trade_alloc(pokemon_fap);
194+
view_set_previous_callback(pokemon_fap->trade_view, pokemon_exit_confirm_view);
195+
view_dispatcher_add_view(pokemon_fap->view_dispatcher, AppViewTrade, pokemon_fap->trade_view);
186196

187-
view_dispatcher_switch_to_view(app->view_dispatcher, AppViewSelectPokemon);
197+
view_dispatcher_switch_to_view(pokemon_fap->view_dispatcher, AppViewSelectPokemon);
188198

189-
return app;
199+
return pokemon_fap;
190200
}
191201

192-
void free_app(App* app) {
193-
furi_assert(app);
202+
void free_app(PokemonFap* pokemon_fap) {
203+
furi_assert(pokemon_fap);
194204

195205
// Free views
196-
view_dispatcher_remove_view(app->view_dispatcher, AppViewSelectPokemon);
197-
select_pokemon_free(app);
198-
view_dispatcher_remove_view(app->view_dispatcher, AppViewTrade);
199-
trade_free(app);
206+
view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewSelectPokemon);
207+
/* XXX: Still need to deal with select_pokemon code */
208+
select_pokemon_free(pokemon_fap);
209+
view_dispatcher_remove_view(pokemon_fap->view_dispatcher, AppViewTrade);
210+
trade_free(pokemon_fap);
200211
// Close records
201212
furi_record_close(RECORD_GUI);
202-
app->gui = NULL;
213+
/* XXX: Since furi_record doesn't appear to be a Gui function, it wouldn't clear the pointer */
214+
pokemon_fap->gui = NULL;
203215

204216
// Free rest
205-
free(app);
217+
free(pokemon_fap);
218+
pokemon_fap = NULL;
206219
}
207220

208221
extern "C" int32_t pokemon_app(void* p) {
209222
UNUSED(p);
210223
//FURI_LOG_D(TAG, "init scene");
211-
App* app = (App*)pokemon_alloc();
224+
//App* app = (App*)pokemon_alloc();
225+
PokemonFap* pokemon_fap = pokemon_alloc();
212226

213227
furi_hal_light_set(LightRed, 0x00);
214228
furi_hal_light_set(LightGreen, 0x00);
215229
furi_hal_light_set(LightBlue, 0x00);
216230
//switch view and run dispatcher
217-
view_dispatcher_run(app->view_dispatcher);
231+
view_dispatcher_run(pokemon_fap->view_dispatcher);
218232

219233
// Free resources
220-
free_app(app);
221-
furi_record_close(RECORD_GUI);
234+
free_app(pokemon_fap);
222235

223236
return 0;
224237
}

pokemon_app.h

+35-18
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,16 @@
1010
#include <gui/icon.h>
1111
#include <pokemon_icons.h>
1212

13-
#include "views/select_pokemon.hpp"
14-
#include "views/trade.hpp"
15-
1613
#define TAG "Pokemon"
1714

18-
struct pokemon_lut {
15+
struct pokemon_data_table {
1916
const char* name;
2017
const Icon* icon;
2118
const uint8_t hex;
2219
};
2320

24-
typedef struct App App;
21+
typedef struct pokemon_data_table PokemonTable;
22+
2523
typedef enum {
2624
GAMEBOY_INITIAL,
2725
GAMEBOY_READY,
@@ -32,6 +30,7 @@ typedef enum {
3230
GAMEBOY_TRADING
3331
} render_gameboy_state_t;
3432

33+
#if 0
3534
struct App {
3635
Gui* gui;
3736
ViewDispatcher* view_dispatcher;
@@ -42,24 +41,42 @@ struct App {
4241
int current_pokemon = 0;
4342
char pokemon_hex_code = ' ';
4443
};
44+
#endif
45+
46+
struct pokemon_fap {
47+
Gui* gui;
48+
ViewDispatcher* view_dispatcher;
49+
50+
/* View ports for each of the application's steps */
51+
View* select_view;
52+
View* trade_view;
53+
54+
/* Table of pokemon data for Gen I */
55+
const PokemonTable* pokemon_table;
56+
57+
/* The currently selected pokemon */
58+
int curr_pokemon;
59+
60+
/* Some state tracking */
61+
/* This, combined with some globals in trade.cpp, can probably be better
62+
* consolidated at some point.
63+
*/
64+
bool trading;
65+
bool connected;
66+
render_gameboy_state_t gameboy_status;
67+
68+
/* TODO: Other variables will end up here, like selected level, EV/IV,
69+
* moveset, etc. Likely will want to be another sub struct similar to
70+
* the actual pokemon data structure.
71+
*/
72+
};
73+
74+
typedef struct pokemon_fap PokemonFap;
4575

4676
typedef enum {
4777
AppViewSelectPokemon,
4878
AppViewTrade,
4979
AppViewExitConfirm,
5080
} AppView;
5181

52-
typedef void (*SelectPokemonCallback)(void* context, uint32_t index);
53-
typedef struct SelectPokemonModel {
54-
int current_pokemon = 0;
55-
char pokemon_hex_code = ' ';
56-
bool trading = false;
57-
bool connected = false;
58-
render_gameboy_state_t gameboy_status = GAMEBOY_INITIAL;
59-
SelectPokemonCallback callback;
60-
void* callback_context;
61-
} SelectPokemonModel;
62-
63-
extern struct pokemon_lut pokemon_table[];
64-
6582
#endif /* POKEMON_APP_H */

0 commit comments

Comments
 (0)