|
- #include "IZ_app.h"
-
- IZ_ProcedureResult IZ_InitializeApp(IZ_App* app) {
- if (SDL_Init(
- SDL_INIT_VIDEO
- | SDL_INIT_GAMECONTROLLER
- | SDL_INIT_EVENTS
- ) < 0) {
- fprintf_s(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
- return 1;
- }
-
- char config_path[128];
- IZ_GetConfigPath(config_path, 128);
-
- if (IZ_InitializeVideo(config_path, &app->video_state)) {
- return 2;
- }
-
- IZ_InitializeInput(config_path, &app->input_state);
- IZ_InitializePool(&app->memory_pool);
- // void* p1 = IZ_PoolAllocate(&app->memory_pool, sizeof(u16), 0)->pointer;
- // void* p2 = IZ_PoolAllocate(&app->memory_pool, sizeof(u8), 0)->pointer;
- // void* p3 = IZ_PoolAllocate(&app->memory_pool, sizeof(u64), 0)->pointer;
- // void* p4 = IZ_PoolAllocate(&app->memory_pool, sizeof(u32), 0)->pointer;
- // printf("\n%p %p %p %p\n", p1, p2, p3, p4);
- // IZ_PoolDeallocate(p1);
- // void* p5 = IZ_PoolAllocate(&app->memory_pool, sizeof(u16), 0)->pointer;
- // printf("\n%p\n", p5);
- app->quit = false;
- app->ticks = 0;
- return 0;
- }
-
- void IZ_TeardownApp(IZ_App* app) {
- IZ_TeardownInput(&app->input_state);
- IZ_TeardownVideo(&app->video_state);
- SDL_Quit();
- }
-
- void IZ_HandleSDLEvents(IZ_App* app) {
- while (SDL_PollEvent(&app->sdl_event) != 0) {
- if (app->sdl_event.type == SDL_QUIT) {
- app->quit = true;
- break;
- }
-
- IZ_HandleSDLInputEvents(app->sdl_event, &app->input_state);
- }
- }
-
- void IZ_HandlePortMIDIEvents(IZ_App* app) {
- u8 player_index;
- i32* midi_events_count;
- u32 midi_event_index;
- for (player_index = 0; player_index < PLAYERS; player_index += 1) {
- midi_events_count = &app->input_state.midi_input_state[player_index].midi_events_count;
- *midi_events_count = Pm_Read(
- app->input_state.midi_input_state[player_index].stream,
- app->input_state.midi_input_state[player_index].event_buffer,
- 1024
- );
-
- if (*midi_events_count < 1) {
- continue;
- }
-
- for (midi_event_index = 0; midi_event_index < *midi_events_count; midi_event_index += 1) {
- IZ_HandlePortMIDIInputEvents(
- app->input_state.midi_input_state[player_index].event_buffer[midi_event_index],
- &app->input_state
- );
- }
- }
- }
-
- void IZ_HandleEvents(IZ_App* app) {
- IZ_HandleSDLEvents(app);
- IZ_HandlePortMIDIEvents(app);
- }
-
- IZ_ProcedureResult IZ_RunApp(IZ_App* app, u8 arg_count, char* arg_values[]) {
- printf_s("Args (%u):\n", arg_count);
- u8 arg_index;
- for (arg_index = 0; arg_index < arg_count; arg_index += 1) {
- printf_s(" %s\n", arg_values[arg_index]);
- }
-
-
- IZ_ProcedureResult init_result = IZ_InitializeApp(app);
- if (init_result) {
- return init_result;
- }
-
- while (true) {
- app->ticks = SDL_GetTicks64();
-
- // TODO do audio processing
- // TODO do networking?
-
- IZ_HandleEvents(app);
- if (app->quit) {
- break;
- }
-
- IZ_UpdateVideo(&app->video_state, &app->input_state, app->ticks);
- }
-
- IZ_TeardownApp(app);
- return 0;
- }
|