@@ -18,7 +18,12 @@ 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); | |||
app->quit = false; | |||
return 0; | |||
} | |||
@@ -75,8 +80,9 @@ IZ_ProcedureResult IZ_RunApp(IZ_App* app, u8 argc, char* argv[]) { | |||
return init_result; | |||
} | |||
u64 ticks; | |||
while (true) { | |||
u64 ticks = SDL_GetTicks64(); | |||
ticks = SDL_GetTicks64(); | |||
// TODO do audio processing | |||
// TODO do networking? | |||
@@ -7,7 +7,7 @@ | |||
typedef struct { | |||
IZ_Object as_object; | |||
f32 hp; | |||
u16 hp; | |||
} IZ_Creature; | |||
#endif |
@@ -27,8 +27,9 @@ void IZ_InitializeInput(const char* config_path, IZ_InputState* state) { | |||
fprintf_s(stderr, "Error committing MIDI input config. Code: %u.\n", midi_input_result); | |||
} | |||
for (u8 p = 0; p < PLAYERS; p += 1) { | |||
state->action[p] = 0; | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
state->action[player_index] = 0; | |||
} | |||
} | |||
@@ -65,9 +65,10 @@ void IZ_HandleJoystickHatEvents(SDL_Event e, IZ_Action* action) { | |||
} | |||
void IZ_HandleJoystickButtonEvents(SDL_Event e, IZ_JoystickState* state, IZ_Action* action) { | |||
for (u8 i = 4; i < CONTROLS; i += 1) { | |||
if (e.jbutton.button == state->config.control_mapping[i]) { | |||
const u16 bitflag = (0x1 << i); | |||
u8 control_index; | |||
for (control_index = 4; control_index < CONTROLS; control_index += 1) { | |||
if (e.jbutton.button == state->config.control_mapping[control_index]) { | |||
const u16 bitflag = (0x1 << control_index); | |||
if (e.type == SDL_JOYBUTTONDOWN) { | |||
*action |= bitflag; | |||
@@ -83,7 +84,8 @@ void IZ_HandleJoystickButtonEvents(SDL_Event e, IZ_JoystickState* state, IZ_Acti | |||
} | |||
void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState(* state)[PLAYERS], IZ_Action(* action)[PLAYERS]) { | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
IZ_HandleJoystickDeviceEvents(e, state[player_index]); | |||
IZ_HandleJoystickAxisEvents(e, state[player_index], action[player_index]); | |||
IZ_HandleJoystickHatEvents(e, action[player_index]); | |||
@@ -94,14 +96,17 @@ void IZ_HandleJoystickEvents(SDL_Event e, IZ_JoystickState(* state)[PLAYERS], IZ | |||
void IZ_LoadJoystickConfig(const char* config_path, IZ_JoystickState(* state)[PLAYERS]) { | |||
char control_mapping_section_name[26]; | |||
char main_section_name[11]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(control_mapping_section_name, 26, "Joystick.%d.ControlMapping", player_index); | |||
for (u8 i = 4; i < CONTROLS; i += 1) { | |||
state[player_index]->config.control_mapping[i] = ini_getl( | |||
for (control_index = 4; control_index < CONTROLS; control_index += 1) { | |||
state[player_index]->config.control_mapping[control_index] = ini_getl( | |||
control_mapping_section_name, | |||
ACTION_NAMES[i], | |||
IZ_DEFAULT_JOYSTICK_STATE[player_index].config.control_mapping[i], | |||
ACTION_NAMES[control_index], | |||
IZ_DEFAULT_JOYSTICK_STATE[player_index].config.control_mapping[control_index], | |||
config_path | |||
); | |||
} | |||
@@ -118,13 +123,15 @@ IZ_ProcedureResult IZ_SaveJoystickConfig(const char* config_path, IZ_JoystickSta | |||
char control_mapping_section_name[26]; | |||
char main_section_name[11]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(control_mapping_section_name, 26, "Joystick.%d.ControlMapping", player_index); | |||
for (u8 i = 4; i < CONTROLS; i += 1) { | |||
for (control_index = 4; control_index < CONTROLS; control_index += 1) { | |||
if (!ini_putl( | |||
control_mapping_section_name, | |||
ACTION_NAMES[i], | |||
state[player_index]->config.control_mapping[i], | |||
ACTION_NAMES[control_index], | |||
state[player_index]->config.control_mapping[control_index], | |||
config_path | |||
)) { | |||
return 1; | |||
@@ -163,7 +170,8 @@ IZ_ProcedureResult IZ_InitializeJoystickState(const char* config_path, IZ_Joysti | |||
} | |||
u8 joysticks_count = SDL_NumJoysticks(); | |||
for (u8 player_index = 0; player_index < joysticks_count; player_index += 1) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < joysticks_count; player_index += 1) { | |||
state[player_index]->device = SDL_JoystickOpen(state[player_index]->config.device_id); | |||
} | |||
@@ -171,7 +179,8 @@ IZ_ProcedureResult IZ_InitializeJoystickState(const char* config_path, IZ_Joysti | |||
} | |||
void IZ_TeardownJoystickState(IZ_JoystickState(* state)[PLAYERS]) { | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
if (!state[player_index]->device) { | |||
continue; | |||
} | |||
@@ -1,9 +1,10 @@ | |||
#include "IZ_keyboard.h" | |||
void IZ_HandleKewyboardKeyUpDownEvents(SDL_Event e, IZ_KeyboardState* state, IZ_Action* action) { | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
if (e.key.keysym.sym == state->config.control_mapping[i]) { | |||
const u16 bitflag = (0x1 << i); | |||
u8 control_index; | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
if (e.key.keysym.sym == state->config.control_mapping[control_index]) { | |||
const u16 bitflag = (0x1 << control_index); | |||
if (e.type == SDL_KEYDOWN) { | |||
*action |= bitflag; | |||
return; | |||
@@ -25,13 +26,16 @@ void IZ_HandleKeyboardEvents(SDL_Event e, IZ_KeyboardState(* state)[PLAYERS], IZ | |||
IZ_ProcedureResult IZ_SaveKeyboardConfig(const char* config_path, IZ_KeyboardState(* state)[PLAYERS]) { | |||
u8 problem = 0; | |||
char control_mapping_section_name[26]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(control_mapping_section_name, 26, "Keyboard.%d.ControlMapping", player_index); | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
if (!ini_puts( | |||
control_mapping_section_name, | |||
ACTION_NAMES[i], | |||
SDL_GetKeyName(state[player_index]->config.control_mapping[i]), | |||
ACTION_NAMES[control_index], | |||
SDL_GetKeyName(state[player_index]->config.control_mapping[control_index]), | |||
config_path | |||
)) { | |||
problem |= (1 << player_index); | |||
@@ -45,19 +49,22 @@ IZ_ProcedureResult IZ_SaveKeyboardConfig(const char* config_path, IZ_KeyboardSta | |||
void IZ_LoadKeyboardConfig(const char* config_path, IZ_KeyboardState(* state)[PLAYERS]) { | |||
char buffer[128]; | |||
char keyboard_section_name[26]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(keyboard_section_name, 26, "Keyboard.%d.ControlMapping", player_index); | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
ini_gets( | |||
keyboard_section_name, | |||
ACTION_NAMES[i], | |||
SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_STATE[player_index].config.control_mapping[i]), | |||
ACTION_NAMES[control_index], | |||
SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_STATE[player_index].config.control_mapping[control_index]), | |||
buffer, | |||
128, | |||
config_path | |||
); | |||
state[player_index]->config.control_mapping[i] = SDL_GetKeyFromName(buffer); | |||
state[player_index]->config.control_mapping[control_index] = SDL_GetKeyFromName(buffer); | |||
} | |||
} | |||
} | |||
@@ -33,9 +33,10 @@ u8 IZ_GetMIDINoteFromName(char* name) { | |||
if (strlen(name_copy) == 2) { | |||
octave = name_copy[1] - '0'; | |||
for (u8 i = 0; i < 12; i += 1) { | |||
if (base_pitch_name[i] == name_copy[0]) { | |||
return (octave * 12) + i; | |||
u8 pitch_index; | |||
for (pitch_index = 0; pitch_index < 12; pitch_index += 1) { | |||
if (base_pitch_name[pitch_index] == name_copy[0]) { | |||
return (octave * 12) + pitch_index; | |||
} | |||
} | |||
return 255u; | |||
@@ -67,15 +68,16 @@ void IZ_HandleMIDINoteOnOffEvents(PmEvent e, IZ_MIDIInputState* state, IZ_Action | |||
u8 data1 = (message >> 8) & 0xFFu; | |||
// u8 data2 = (message >> 16) & 0xFFu; | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
u8 control_index; | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
if ( | |||
data1 == state->config.control_mapping[i] | |||
data1 == state->config.control_mapping[control_index] | |||
&& ( | |||
(state->config.channel < 16 && channel == state->config.channel) | |||
|| state->config.channel >= 16 | |||
) | |||
) { | |||
const u16 bitflag = (0x1 << i); | |||
const u16 bitflag = (0x1 << control_index); | |||
if (status == IZ_MIDI_NOTE_ON) { | |||
*action |= bitflag; | |||
return; | |||
@@ -89,7 +91,8 @@ void IZ_HandleMIDINoteOnOffEvents(PmEvent e, IZ_MIDIInputState* state, IZ_Action | |||
} | |||
void IZ_HandleMIDIInputEvents(PmEvent e, IZ_MIDIInputState(* state)[PLAYERS], IZ_Action(* action)[PLAYERS]) { | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
IZ_HandleMIDINoteOnOffEvents(e, state[player_index], action[player_index]); | |||
} | |||
} | |||
@@ -100,13 +103,15 @@ IZ_ProcedureResult IZ_SaveMIDIInputConfig(const char* config_path, IZ_MIDIInputS | |||
char control_mapping_section_name[27]; | |||
char main_section_name[12]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(control_mapping_section_name, 27, "MIDIInput.%d.ControlMapping", player_index); | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
if (!ini_puts( | |||
control_mapping_section_name, | |||
ACTION_NAMES[i], | |||
IZ_GetMIDINoteName(state[player_index]->config.control_mapping[i]), | |||
ACTION_NAMES[control_index], | |||
IZ_GetMIDINoteName(state[player_index]->config.control_mapping[control_index]), | |||
config_path | |||
)) { | |||
problem |= (1 << player_index); | |||
@@ -141,19 +146,21 @@ void IZ_LoadMIDIInputConfig(const char* config_path, IZ_MIDIInputState(* state)[ | |||
char control_mapping_section_name[27]; | |||
char main_section_name[12]; | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
u8 control_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
sprintf_s(control_mapping_section_name, 27, "MIDIInput.%d.ControlMapping", player_index); | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
for (control_index = 0; control_index < CONTROLS; control_index += 1) { | |||
ini_gets( | |||
control_mapping_section_name, | |||
ACTION_NAMES[i], | |||
IZ_GetMIDINoteName(IZ_DEFAULT_MIDI_INPUT_STATE[player_index].config.control_mapping[i]), | |||
ACTION_NAMES[control_index], | |||
IZ_GetMIDINoteName(IZ_DEFAULT_MIDI_INPUT_STATE[player_index].config.control_mapping[control_index]), | |||
buffer, | |||
128, | |||
config_path | |||
); | |||
state[player_index]->config.control_mapping[i] = IZ_GetMIDINoteFromName(buffer); | |||
state[player_index]->config.control_mapping[control_index] = IZ_GetMIDINoteFromName(buffer); | |||
} | |||
sprintf_s(main_section_name, 12, "MIDIInput.%d", player_index); | |||
@@ -173,7 +180,8 @@ IZ_ProcedureResult IZ_InitializeMIDIInput(const char* config_path, IZ_MIDIInputS | |||
return 2; | |||
} | |||
for (u8 player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
state[player_index]->device_info = Pm_GetDeviceInfo(state[player_index]->config.device_id); | |||
state[player_index]->stream = NULL; | |||
Pm_OpenInput( | |||
@@ -190,10 +198,11 @@ IZ_ProcedureResult IZ_InitializeMIDIInput(const char* config_path, IZ_MIDIInputS | |||
} | |||
void IZ_TeardownMIDIInput(IZ_MIDIInputState(* state)[PLAYERS]) { | |||
for (u8 i = 0; i < PLAYERS; i += 1) { | |||
if (!state[i]->stream) { | |||
u8 player_index; | |||
for (player_index = 0; player_index < PLAYERS; player_index += 1) { | |||
if (!state[player_index]->stream) { | |||
continue; | |||
} | |||
Pm_Close(state[i]->stream); | |||
Pm_Close(state[player_index]->stream); | |||
} | |||
} |
@@ -1 +1,46 @@ | |||
#include "IZ_pool.h" | |||
void IZ_InitializePool(IZ_Pool* pool) { | |||
SDL_memset(pool->items, 0, POOL_MAX_ITEMS * sizeof(IZ_PoolItem)); | |||
pool->top = 0; | |||
pool->memory = SDL_malloc(POOL_MAX_SIZE); | |||
SDL_memset(pool->memory, 0, POOL_MAX_SIZE); | |||
pool->next_address = 0; | |||
} | |||
IZ_PoolItem* IZ_PoolAllocate(IZ_Pool* pool, size_t size) { | |||
// 1. check next free allocation for size | |||
// 2. if 1. returns non-null, | |||
// u64 alloc_end = pool->next_address + size; | |||
// | |||
// for (u64 i = 0; i < POOL_MAX_ALLOCATIONS; i += 1) { | |||
// if (pool->allocation[i].length == 0) { | |||
// continue; | |||
// } | |||
// } | |||
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, | |||
}; | |||
pool->top = (pool->top + 1) % POOL_MAX_ITEMS; | |||
pool->next_address = (pool->next_address + size) % POOL_MAX_SIZE; | |||
return next_allocation; | |||
} | |||
void IZ_PoolDeallocate(IZ_PoolItem* item) { | |||
u64 i; | |||
for (i = 0; i < POOL_MAX_ITEMS; i += 1) { | |||
if (&item->pool->items[i] != item) { | |||
continue; | |||
} | |||
item->pool->items[i].pool = NULL; | |||
item->pool->items[i].size = 0; | |||
item->pool->items[i].pointer = NULL; | |||
return; | |||
} | |||
} |
@@ -1,10 +1,31 @@ | |||
#ifndef IZ_POOL_H | |||
#define IZ_POOL_H | |||
#define POOL_MAX_SIZE 256 | |||
#include <SDL_stdinc.h> | |||
#include "../IZ_common.h" | |||
#define POOL_MAX_ITEMS (1000) // 16 million allocations max | |||
#define POOL_MAX_SIZE (1l << 23) // 16MB | |||
struct IZ_Pool; | |||
typedef struct { | |||
// TODO allocate and implement pool memory management! | |||
void* pointer; | |||
size_t size; | |||
struct IZ_Pool* pool; | |||
} IZ_PoolItem; | |||
typedef struct IZ_Pool { | |||
u16 top; | |||
IZ_PoolItem items[POOL_MAX_ITEMS]; | |||
u64 next_address; | |||
void* memory; | |||
} IZ_Pool; | |||
void IZ_InitializePool(IZ_Pool*); | |||
IZ_PoolItem* IZ_PoolAllocate(IZ_Pool*, size_t); | |||
void IZ_PoolDeallocate(IZ_PoolItem*); | |||
#endif |
@@ -47,49 +47,68 @@ IZ_ProcedureResult IZ_InitializeVideo(const char* config_path, IZ_VideoState* st | |||
return 0; | |||
} | |||
void IZ_UpdateVideo(IZ_VideoState* video_state, IZ_InputState* input_states, uint64_t ticks) { | |||
if (ticks - video_state->last_update_at > 1000 / video_state->config.max_fps) { | |||
// Update window | |||
SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0x00, 0x00, 0xff); | |||
SDL_RenderClear(video_state->renderer); | |||
void IZ_UpdateVideoForDebugTicks(IZ_VideoState* video_state, uint64_t ticks) { | |||
SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0xff, 0xff, 0xff); | |||
u64 the_ticks = ticks; | |||
u8 column; | |||
u8 row; | |||
const u8 size = 4; | |||
u8 i; | |||
for (i = 0; i < 64; i += 1) { | |||
column = i % 32; | |||
row = i / 32; | |||
if (the_ticks & 0x1) { | |||
SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) { | |||
(f32) (video_state->config.width - ((column + 1) * size)), | |||
(f32) (video_state->config.height - ((row + 1) * size)), | |||
size, | |||
size | |||
}); | |||
} | |||
the_ticks >>= 1; | |||
} | |||
} | |||
void IZ_UpdateVideoForDebugInput(IZ_VideoState* video_state, IZ_InputState* input_states) { | |||
SDL_SetRenderDrawColor(video_state->renderer, 0xff, 0xff, 0x00, 0xff); | |||
u8 column; | |||
u8 row; | |||
const u8 size = 4; | |||
SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0xff, 0xff, 0xff); | |||
uint64_t the_ticks = ticks; | |||
for (u8 i = 0; i < 64; i += 1) { | |||
const u8 column = i % 32; | |||
const u8 row = i / 32; | |||
const u8 size = 4; | |||
u8 p; | |||
u8 i; | |||
for (p = 0; p < PLAYERS; p += 1) { | |||
IZ_Action the_action = input_states->action[p]; | |||
for (i = 0; i < CONTROLS; i += 1) { | |||
column = (i % 4) + (p * 4); | |||
row = i / 4; | |||
if (the_ticks & 0x1) { | |||
if (the_action & 0x1) { | |||
SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) { | |||
video_state->config.width - ((column + 1) * size), | |||
video_state->config.height - ((row + 1) * size), | |||
(f32) (column * size), | |||
(f32) (row * size), | |||
size, | |||
size | |||
}); | |||
} | |||
the_ticks >>= 1; | |||
the_action >>= 1; | |||
} | |||
} | |||
} | |||
SDL_SetRenderDrawColor(video_state->renderer, 0xff, 0xff, 0x00, 0xff); | |||
// TODO refer to app's state | |||
for (u8 p = 0; p < PLAYERS; p += 1) { | |||
for (u8 i = 0; i < CONTROLS; i += 1) { | |||
const u8 column = (i % 4) + (p * 4); | |||
const u8 row = i / 4; | |||
const IZ_Action bitflag = (0x1 << i); | |||
const u8 size = 4; | |||
if (input_states->action[p] & bitflag) { | |||
SDL_RenderFillRectF(video_state->renderer, &(SDL_FRect) { | |||
column * size, | |||
row * size, | |||
size, | |||
size | |||
}); | |||
} | |||
} | |||
} | |||
void IZ_UpdateVideoForDebug(IZ_VideoState* video_state, IZ_InputState* input_states, uint64_t ticks) { | |||
IZ_UpdateVideoForDebugTicks(video_state, ticks); | |||
IZ_UpdateVideoForDebugInput(video_state, input_states); | |||
} | |||
void IZ_UpdateVideo(IZ_VideoState* video_state, IZ_InputState* input_states, uint64_t ticks) { | |||
if (ticks - video_state->last_update_at > 1000 / video_state->config.max_fps) { | |||
// Update window | |||
SDL_SetRenderDrawColor(video_state->renderer, 0x00, 0x00, 0x00, 0xff); | |||
SDL_RenderClear(video_state->renderer); | |||
IZ_UpdateVideoForDebug(video_state, input_states, ticks); | |||
SDL_RenderPresent(video_state->renderer); | |||
video_state->last_update_at = ticks; | |||
} | |||