Преглед на файлове

Optimize MIDI events handling

Declare memory before processing.
feature/data-structs
TheoryOfNekomata преди 2 години
родител
ревизия
effcd08fcb
променени са 6 файла, в които са добавени 43 реда и са изтрити 29 реда
  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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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 Целия файл

@@ -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*);



Зареждане…
Отказ
Запис