Browse Source

Optimize MIDI events handling

Declare memory before processing.
feature/data-structs
TheoryOfNekomata 2 years ago
parent
commit
effcd08fcb
6 changed files with 43 additions and 29 deletions
  1. +31
    -22
      src/packages/game/IZ_app.c
  2. +1
    -0
      src/packages/game/IZ_app.h
  3. +1
    -0
      src/packages/game/input/IZ_midi.h
  4. +2
    -2
      src/packages/game/main.c
  5. +5
    -3
      src/packages/game/memory/IZ_pool.c
  6. +3
    -2
      src/packages/game/memory/IZ_pool.h

+ 31
- 22
src/packages/game/IZ_app.c View File

@@ -19,12 +19,16 @@ IZ_ProcedureResult IZ_InitializeApp(IZ_App* app) {

IZ_InitializeInput(config_path, &app->input_state);
IZ_InitializePool(&app->memory_pool);
void* p1 = IZ_PoolAllocate(&app->memory_pool, sizeof(u16))->pointer;
void* p2 = IZ_PoolAllocate(&app->memory_pool, sizeof(u8))->pointer;
void* p3 = IZ_PoolAllocate(&app->memory_pool, sizeof(u64))->pointer;
void* p4 = IZ_PoolAllocate(&app->memory_pool, sizeof(u32))->pointer;
printf("\n%p %p %p %p\n", p1, p2, p3, p4);
// 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;
}

@@ -46,20 +50,26 @@ void IZ_HandleSDLEvents(IZ_App* app) {
}

void IZ_HandlePortMIDIEvents(IZ_App* app) {
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) {
i32 midi_events_count = Pm_Read(
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 > 0) {
for (i32 midi_event_index = 0; midi_event_index < midi_events_count; midi_event_index += 1) {
IZ_HandlePortMIDIInputEvents(
app->input_state.midi_input_state->event_buffer[midi_event_index],
&app->input_state
);
}
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
);
}
}
}
@@ -69,10 +79,11 @@ void IZ_HandleEvents(IZ_App* app) {
IZ_HandlePortMIDIEvents(app);
}

IZ_ProcedureResult IZ_RunApp(IZ_App* app, u8 argc, char* argv[]) {
printf_s("Args (%u):\n", argc);
for (u8 i = 0; i < argc; i += 1) {
printf_s(" %s", argv[i]);
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);
@@ -80,20 +91,18 @@ IZ_ProcedureResult IZ_RunApp(IZ_App* app, u8 argc, char* argv[]) {
return init_result;
}

u64 ticks;
while (true) {
ticks = SDL_GetTicks64();
app->ticks = SDL_GetTicks64();

// TODO do audio processing
// TODO do networking?

// process events
IZ_HandleEvents(app);
if (app->quit) {
break;
}

IZ_UpdateVideo(&app->video_state, &app->input_state, ticks);
IZ_UpdateVideo(&app->video_state, &app->input_state, app->ticks);
}

IZ_TeardownApp(app);


+ 1
- 0
src/packages/game/IZ_app.h View File

@@ -14,6 +14,7 @@ typedef struct {
IZ_VideoState video_state;

IZ_Pool memory_pool;
u64 ticks;
bool quit;
} IZ_App;



+ 1
- 0
src/packages/game/input/IZ_midi.h View File

@@ -26,6 +26,7 @@ typedef struct {
const PmDeviceInfo* device_info;
PmStream* stream;
PmEvent event_buffer[MIDI_EVENT_BUFFER_SIZE];
i32 midi_events_count;
} IZ_MIDIInputState;

static const IZ_MIDIInputState IZ_DEFAULT_MIDI_INPUT_STATE[PLAYERS] = {


+ 2
- 2
src/packages/game/main.c View File

@@ -1,7 +1,7 @@
#include <SDL.h>
#include "IZ_app.h"

int main(int argc, char* argv[]) {
int main(int arg_count, char* arg_values[]) {
IZ_App app;
return IZ_RunApp(&app, argc, argv);
return IZ_RunApp(&app, arg_count, arg_values);
}

+ 5
- 3
src/packages/game/memory/IZ_pool.c View File

@@ -8,7 +8,7 @@ void IZ_InitializePool(IZ_Pool* pool) {
pool->next_address = 0;
}

IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, size_t size) {
IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, size_t size, u64 created_at) {
// 1. check next free allocation for size
// 2. if 1. returns non-null,

@@ -23,9 +23,10 @@ IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, size_t size) {
void* pointer = &pool->memory[pool->next_address];
IZ_PoolItem* next_allocation = &pool->items[pool->top];
pool->items[pool->top] = (IZ_PoolItem) {
.pool = pool,
.size = size,
.pointer = pointer,
.size = size,
.created_at = created_at,
.pool = pool,
};
pool->top = (pool->top + 1) % POOL_MAX_ITEMS;
pool->next_address = (pool->next_address + size) % POOL_MAX_SIZE;
@@ -41,6 +42,7 @@ void IZ_PoolDeallocate(IZ_PoolItem* item) {
item->pool->items[i].pool = NULL;
item->pool->items[i].size = 0;
item->pool->items[i].pointer = NULL;
item->pool->items[i].created_at = 0;
return;
}
}

+ 3
- 2
src/packages/game/memory/IZ_pool.h View File

@@ -12,19 +12,20 @@ struct IZ_Pool;
typedef struct {
void* pointer;
size_t size;
u64 created_at;
struct IZ_Pool* pool;
} IZ_PoolItem;

typedef struct IZ_Pool {
u16 top;
IZ_PoolItem items[POOL_MAX_ITEMS];
u64 next_address;
IZ_PoolItem items[POOL_MAX_ITEMS];
void* memory;
} IZ_Pool;

void IZ_InitializePool(IZ_Pool*);

IZ_PoolItem* IZ_PoolAllocate(IZ_Pool*, size_t);
IZ_PoolItem* IZ_PoolAllocate(IZ_Pool*, size_t, u64);

void IZ_PoolDeallocate(IZ_PoolItem*);



Loading…
Cancel
Save