Unify the library on an akgl_ namespace
This commit is contained in:
126
src/controller.c
126
src/controller.c
@@ -5,26 +5,26 @@
|
||||
#include <sdl3game/game.h>
|
||||
#include <sdl3game/controller.h>
|
||||
|
||||
SDL3GControlMap GAME_ControlMaps[MAX_CONTROL_MAPS];
|
||||
akgl_ControlMap GAME_ControlMaps[AKGL_MAX_CONTROL_MAPS];
|
||||
|
||||
akerr_ErrorContext *controller_handle_event(void *appstate, SDL_Event *event)
|
||||
akerr_ErrorContext *akgl_controller_handle_event(void *appstate, SDL_Event *event)
|
||||
{
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int eventButtonComboMatch = 0;
|
||||
SDL3GControlMap *curmap = NULL;
|
||||
SDL3GControl *curcontrol = NULL;
|
||||
akgl_ControlMap *curmap = NULL;
|
||||
akgl_Control *curcontrol = NULL;
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
ATTEMPT {
|
||||
for ( i = 0 ; i < MAX_CONTROL_MAPS; i++ ) {
|
||||
for ( i = 0 ; i < AKGL_MAX_CONTROL_MAPS; i++ ) {
|
||||
curmap = &GAME_ControlMaps[i];
|
||||
if ( curmap->target == NULL ) {
|
||||
continue;
|
||||
}
|
||||
//SDL_Log("Control map %d maps to actor %s", i, curmap->target->name);
|
||||
//SDL_Log("event from keyboard %d", event->key.which);
|
||||
for ( j = 0; j < MAX_CONTROLS; j++ ) {
|
||||
for ( j = 0; j < AKGL_MAX_CONTROLS; j++ ) {
|
||||
curcontrol = &curmap->controls[j];
|
||||
//SDL_Log("button/key is processed by controlmap %d control %d", i, j);
|
||||
//SDL_Log("event %d -> control on %d off %d", event->type, curcontrol->event_on, curcontrol->event_off);
|
||||
@@ -41,14 +41,14 @@ akerr_ErrorContext *controller_handle_event(void *appstate, SDL_Event *event)
|
||||
);
|
||||
if ( event->type == curcontrol->event_on && eventButtonComboMatch) {
|
||||
CATCH(errctx, curcontrol->handler_on(curmap->target, event));
|
||||
goto _controller_handle_event_success;
|
||||
goto _akgl_controller_handle_event_success;
|
||||
} else if ( event->type == curcontrol->event_off && eventButtonComboMatch ) {
|
||||
CATCH(errctx, curcontrol->handler_off(curmap->target, event));
|
||||
goto _controller_handle_event_success;
|
||||
goto _akgl_controller_handle_event_success;
|
||||
}
|
||||
}
|
||||
}
|
||||
_controller_handle_event_success:
|
||||
_akgl_controller_handle_event_success:
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
@@ -57,49 +57,49 @@ _controller_handle_event_success:
|
||||
|
||||
akerr_ErrorContext *gamepad_handle_button_down(void *appstate, SDL_Event *event)
|
||||
{
|
||||
actor *player = NULL;
|
||||
akgl_Actor *player = NULL;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate");
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "NULL event");
|
||||
player = SDL_GetPointerProperty(REGISTRY_ACTOR, "player", NULL);
|
||||
player = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "player", NULL);
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "Player actor does not exist");
|
||||
|
||||
if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_DOWN ||
|
||||
event->key.key == SDLK_DOWN ) {
|
||||
SDL_Log("Processing dpad down : state %d", player->state);
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_MOVING_DOWN);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_MOVING_DOWN);
|
||||
if ( !player->movement_controls_face ) {
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_FACE_ALL);
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_FACE_DOWN);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_FACE_ALL);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_FACE_DOWN);
|
||||
}
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_UP ||
|
||||
event->key.key == SDLK_UP ) {
|
||||
SDL_Log("Processing dpad up");
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_MOVING_UP);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_MOVING_UP);
|
||||
if ( !player->movement_controls_face ) {
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_FACE_ALL);
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_FACE_UP);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_FACE_ALL);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_FACE_UP);
|
||||
}
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_LEFT ||
|
||||
event->key.key == SDLK_LEFT ) {
|
||||
SDL_Log("Processing dpad left");
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_MOVING_LEFT);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_MOVING_LEFT);
|
||||
if ( !player->movement_controls_face ) {
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_FACE_ALL);
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_FACE_LEFT);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_FACE_ALL);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_FACE_LEFT);
|
||||
}
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_RIGHT ||
|
||||
event->key.key == SDLK_RIGHT ) {
|
||||
SDL_Log("Processing dpad right");
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_MOVING_RIGHT);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_MOVING_RIGHT);
|
||||
if ( !player->movement_controls_face ) {
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_FACE_ALL);
|
||||
BITMASK_ADD(player->state, ACTOR_STATE_FACE_RIGHT);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_FACE_ALL);
|
||||
AKGL_BITMASK_ADD(player->state, AKGL_ACTOR_STATE_FACE_RIGHT);
|
||||
}
|
||||
SDL_Log("New state : %d", player->state);
|
||||
}
|
||||
@@ -108,37 +108,37 @@ akerr_ErrorContext *gamepad_handle_button_down(void *appstate, SDL_Event *event)
|
||||
|
||||
akerr_ErrorContext *gamepad_handle_button_up(void *appstate, SDL_Event *event)
|
||||
{
|
||||
actor *player = NULL;
|
||||
akgl_Actor *player = NULL;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate");
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "NULL event");
|
||||
player = SDL_GetPointerProperty(REGISTRY_ACTOR, "player", NULL);
|
||||
player = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, "player", NULL);
|
||||
FAIL_ZERO_RETURN(errctx, appstate, AKERR_NULLPOINTER, "Player actor does not exist");
|
||||
|
||||
if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_DOWN ||
|
||||
event->key.key == SDLK_DOWN ) {
|
||||
SDL_Log("processing down release");
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_MOVING_DOWN);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_MOVING_DOWN);
|
||||
player->curSpriteFrameId = 0;
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_UP ||
|
||||
event->key.key == SDLK_UP ) {
|
||||
SDL_Log("processing up release");
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_MOVING_UP);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_MOVING_UP);
|
||||
player->curSpriteFrameId = 0;
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_RIGHT ||
|
||||
event->key.key == SDLK_RIGHT) {
|
||||
SDL_Log("processing right release");
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_MOVING_RIGHT);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_MOVING_RIGHT);
|
||||
player->curSpriteFrameId = 0;
|
||||
SDL_Log("New state : %d", player->state);
|
||||
} else if ( event->gbutton.button == SDL_GAMEPAD_BUTTON_DPAD_LEFT ||
|
||||
event->key.key == SDLK_LEFT ) {
|
||||
SDL_Log("processing left release");
|
||||
BITMASK_DEL(player->state, ACTOR_STATE_MOVING_LEFT);
|
||||
AKGL_BITMASK_DEL(player->state, AKGL_ACTOR_STATE_MOVING_LEFT);
|
||||
player->curSpriteFrameId = 0;
|
||||
SDL_Log("New state : %d", player->state);
|
||||
}
|
||||
@@ -194,16 +194,16 @@ akerr_ErrorContext *gamepad_handle_removed(void *appstate, SDL_Event *event)
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *SDL3G_controller_pushmap(int controlmapid, SDL3GControl *control)
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgl_controller_pushmap(int controlmapid, akgl_Control *control)
|
||||
{
|
||||
int newmapid = 0;
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_RETURN(errctx, control, AKERR_NULLPOINTER, "NULL Control");
|
||||
FAIL_NONZERO_RETURN(errctx, (controlmapid >= MAX_CONTROL_MAPS), AKERR_OUTOFBOUNDS, "ID %d exceeds maximum %d", controlmapid, MAX_CONTROL_MAPS);
|
||||
FAIL_NONZERO_RETURN(errctx, (controlmapid >= AKGL_MAX_CONTROL_MAPS), AKERR_OUTOFBOUNDS, "ID %d exceeds maximum %d", controlmapid, AKGL_MAX_CONTROL_MAPS);
|
||||
newmapid = GAME_ControlMaps[controlmapid].nextMap;
|
||||
FAIL_ZERO_RETURN(errctx, (MAX_CONTROLS - newmapid), AKERR_OUTOFBOUNDS, "Control map ID %d is full", controlmapid);
|
||||
memcpy((void *)&GAME_ControlMaps[controlmapid].controls[newmapid], control, sizeof(SDL3GControl));
|
||||
FAIL_ZERO_RETURN(errctx, (AKGL_MAX_CONTROLS - newmapid), AKERR_OUTOFBOUNDS, "Control map ID %d is full", controlmapid);
|
||||
memcpy((void *)&GAME_ControlMaps[controlmapid].controls[newmapid], control, sizeof(akgl_Control));
|
||||
GAME_ControlMaps[controlmapid].nextMap = newmapid + 1;
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
@@ -211,21 +211,21 @@ akerr_ErrorContext AKERR_NOIGNORE *SDL3G_controller_pushmap(int controlmapid, SD
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *SDL3G_controller_default(int controlmapid, char *actorname, int kbid, int jsid)
|
||||
akerr_ErrorContext AKERR_NOIGNORE *akgl_controller_default(int controlmapid, char *actorname, int kbid, int jsid)
|
||||
{
|
||||
SDL3GControlMap *controlmap;
|
||||
SDL3GControl control;
|
||||
akgl_ControlMap *controlmap;
|
||||
akgl_Control control;
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
// set up the control map
|
||||
FAIL_NONZERO_RETURN(errctx, (controlmapid >= MAX_CONTROL_MAPS), AKERR_OUTOFBOUNDS, "ID %d exceeds maximum %d", controlmapid, MAX_CONTROL_MAPS);
|
||||
memset((void *)&control, 0x00, sizeof(SDL3GControl));
|
||||
FAIL_NONZERO_RETURN(errctx, (controlmapid >= AKGL_MAX_CONTROL_MAPS), AKERR_OUTOFBOUNDS, "ID %d exceeds maximum %d", controlmapid, AKGL_MAX_CONTROL_MAPS);
|
||||
memset((void *)&control, 0x00, sizeof(akgl_Control));
|
||||
controlmap = &GAME_ControlMaps[controlmapid];
|
||||
controlmap->kbid = kbid;
|
||||
controlmap->jsid = jsid;
|
||||
|
||||
controlmap->target = SDL_GetPointerProperty(REGISTRY_ACTOR, actorname, NULL);
|
||||
controlmap->target = SDL_GetPointerProperty(AKGL_REGISTRY_ACTOR, actorname, NULL);
|
||||
FAIL_ZERO_BREAK(errctx, controlmap->target, AKERR_REGISTRY, "Actor %s not found in registry", actorname);
|
||||
|
||||
// ---- KEYBOARD CONTROLS ----
|
||||
@@ -234,33 +234,33 @@ akerr_ErrorContext AKERR_NOIGNORE *SDL3G_controller_default(int controlmapid, ch
|
||||
control.key = SDLK_DOWN;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.event_off = SDL_EVENT_KEY_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_down_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_down_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_down_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_down_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move up
|
||||
control.key = SDLK_UP;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.event_off = SDL_EVENT_KEY_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_up_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_up_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_up_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_up_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move left
|
||||
control.key = SDLK_LEFT;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.event_off = SDL_EVENT_KEY_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_left_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_left_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_left_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_left_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move right
|
||||
control.key = SDLK_RIGHT;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.event_off = SDL_EVENT_KEY_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_right_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_right_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_right_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_right_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
control.key = 0;
|
||||
// ----- GAMEPAD CONTROLS
|
||||
@@ -268,33 +268,33 @@ akerr_ErrorContext AKERR_NOIGNORE *SDL3G_controller_default(int controlmapid, ch
|
||||
control.button = SDL_GAMEPAD_BUTTON_DPAD_DOWN;
|
||||
control.event_on = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
|
||||
control.event_off = SDL_EVENT_GAMEPAD_BUTTON_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_down_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_down_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_down_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_down_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move up
|
||||
control.button = SDL_GAMEPAD_BUTTON_DPAD_UP;
|
||||
control.event_on = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
|
||||
control.event_off = SDL_EVENT_GAMEPAD_BUTTON_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_up_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_up_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_up_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_up_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move left
|
||||
control.button = SDL_GAMEPAD_BUTTON_DPAD_LEFT;
|
||||
control.event_on = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
|
||||
control.event_off = SDL_EVENT_GAMEPAD_BUTTON_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_left_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_left_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_left_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_left_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
// Move right
|
||||
control.button = SDL_GAMEPAD_BUTTON_DPAD_RIGHT;
|
||||
control.event_on = SDL_EVENT_GAMEPAD_BUTTON_DOWN;
|
||||
control.event_off = SDL_EVENT_GAMEPAD_BUTTON_UP;
|
||||
control.handler_on = &SDL3GActor_cmhf_right_on;
|
||||
control.handler_off = &SDL3GActor_cmhf_right_off;
|
||||
CATCH(errctx, SDL3G_controller_pushmap(controlmapid, &control));
|
||||
control.handler_on = &akgl_Actor_cmhf_right_on;
|
||||
control.handler_off = &akgl_Actor_cmhf_right_off;
|
||||
CATCH(errctx, akgl_controller_pushmap(controlmapid, &control));
|
||||
|
||||
SUCCEED_RETURN(errctx);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user