diff --git a/.gitignore b/.gitignore index d8e882d..2d73969 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ ./build/* +.aider* +*~ diff --git a/include/controller.h b/include/controller.h new file mode 100644 index 0000000..c26f905 --- /dev/null +++ b/include/controller.h @@ -0,0 +1,12 @@ +#ifndef _GAMEPAD_H_ +#define _GAMEPAD_H_ + +#include +#include + +ErrorContext ERROR_NOIGNORE *gamepad_handle_button_down(void *appstate, SDL_Event *event); +ErrorContext ERROR_NOIGNORE *gamepad_handle_button_up(void *appstate, SDL_Event *event); +ErrorContext ERROR_NOIGNORE *gamepad_handle_added(void *appstate, SDL_Event *event); +ErrorContext ERROR_NOIGNORE *gamepad_handle_removed(void *appstate, SDL_Event *event); + +#endif // _GAMEPAD_H_ diff --git a/tests/charviewer.c b/tests/charviewer.c new file mode 100644 index 0000000..60fdf18 --- /dev/null +++ b/tests/charviewer.c @@ -0,0 +1,124 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int numsprites = 8; +char *spritepaths[] = { + "assets/sprites/little_guy_walking_left.json", + "assets/sprites/little_guy_walking_right.json", + "assets/sprites/little_guy_walking_up.json", + "assets/sprites/little_guy_walking_down.json", + "assets/sprites/little_guy_facing_left.json", + "assets/sprites/little_guy_facing_right.json", + "assets/sprites/little_guy_facing_up.json", + "assets/sprites/little_guy_facing_down.json" +}; + +int main(void) +{ + PREPARE_ERROR(errctx); + SDL3GControlMap *controlmap; + actor *actorptr = NULL; + + ATTEMPT { + + SDL_SetAppMetadata("SDL3-GameTest", "0.1", "net.aklabs.sdl3-gametest"); + + if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO )) { + FAIL_BREAK(errctx, ERR_SDL, "Couldn't initialize SDL: %s", SDL_GetError()); + } + + if (!SDL_CreateWindowAndRenderer("net/aklabs/libsdl3game/test_sprite", 640, 480, 0, &window, &renderer)) { + FAIL_BREAK(errctx, ERR_SDL, "Couldn't create window/renderer: %s", SDL_GetError()); + } + + CATCH(errctx, heap_init()); + CATCH(errctx, registry_init()); + + strcpy((char *)&game.name, "charviewer"); + strcpy((char *)&game.version, "0.0.1"); + strcpy((char *)&game.uri, "net.aklabs.libsdl3game.charviewer"); + game.screenwidth = 640; + game.screenheight = 480; + + CATCH(errctx, GAME_init()); + + for ( int i = 0; i < numsprites ; i++) { + CATCH(errctx, sprite_load_json(spritepaths[i])); + } + CATCH(errctx, character_load_json("assets/characters/littleguy.json")); + CATCH(errctx, heap_next_actor(&actorptr)); + CATCH(errctx, actor_initialize((actor *)actorptr, "player")); + actorptr->basechar = SDL_GetPointerProperty( + REGISTRY_CHARACTER, + "little guy", + NULL); + FAIL_ZERO_BREAK(errctx, actorptr->basechar, ERR_REGISTRY, "Can't load character 'little guy' from the registry"); + actorptr->movement_controls_face = false; + actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT); + actorptr->x = 320; + actorptr->y = 240; + actorptr->visible = true; + + // set up the control map + controlmap = &GAME_ControlMaps[0]; + controlmap->kbid = 0; + controlmap->target = SDL_GetPointerProperty(REGISTRY_ACTOR, "player", NULL); + // Move down + controlmap->controls[0].key = SDLK_DOWN; + //controlmap->controls[0].target_state_gate = ACTOR_STATE_MOVING_DOWN; + controlmap->controls[0].target_add_state_on = ACTOR_STATE_MOVING_DOWN | ACTOR_STATE_FACE_DOWN; + controlmap->controls[0].target_del_state_on = ACTOR_STATE_MOVING_UP | ACTOR_STATE_FACE_ALL; + controlmap->controls[0].target_del_state_off = ACTOR_STATE_MOVING_DOWN; + controlmap->controls[0].event_on = SDL_EVENT_KEY_DOWN; + controlmap->controls[0].event_off = SDL_EVENT_KEY_UP; + + // Move up + controlmap->controls[1].key = SDLK_UP; + //controlmap->controls[1].target_state_gate = ACTOR_STATE_MOVING_UP; + controlmap->controls[1].target_add_state_on = ACTOR_STATE_MOVING_UP | ACTOR_STATE_FACE_UP; + controlmap->controls[1].target_del_state_on = ACTOR_STATE_MOVING_DOWN | ACTOR_STATE_FACE_ALL; + controlmap->controls[1].target_del_state_off = ACTOR_STATE_MOVING_UP; + controlmap->controls[1].event_on = SDL_EVENT_KEY_DOWN; + controlmap->controls[1].event_off = SDL_EVENT_KEY_UP; + + // Move left + controlmap->controls[2].key = SDLK_LEFT; + //controlmap->controls[2].target_state_gate = ACTOR_STATE_MOVING_LEFT; + controlmap->controls[2].target_add_state_on = ACTOR_STATE_MOVING_LEFT | ACTOR_STATE_FACE_LEFT; + controlmap->controls[2].target_del_state_on = ACTOR_STATE_MOVING_RIGHT | ACTOR_STATE_FACE_ALL; + controlmap->controls[2].target_del_state_off = ACTOR_STATE_MOVING_LEFT; + controlmap->controls[2].event_on = SDL_EVENT_KEY_DOWN; + controlmap->controls[2].event_off = SDL_EVENT_KEY_UP; + + // Move right + controlmap->controls[3].key = SDLK_RIGHT; + //controlmap->controls[3].target_state_gate = ACTOR_STATE_MOVING_RIGHT; + controlmap->controls[3].target_add_state_on = ACTOR_STATE_MOVING_RIGHT | ACTOR_STATE_FACE_RIGHT; + controlmap->controls[3].target_del_state_on = ACTOR_STATE_MOVING_LEFT | ACTOR_STATE_FACE_ALL; + controlmap->controls[3].target_del_state_off = ACTOR_STATE_MOVING_RIGHT; + controlmap->controls[3].event_on = SDL_EVENT_KEY_DOWN; + controlmap->controls[3].event_off = SDL_EVENT_KEY_UP; + } CLEANUP { + } PROCESS(errctx) { + } HANDLE_DEFAULT(errctx) { + LOG_ERROR(errctx); + return 1; + } FINISH_NORETURN(errctx); + + return 0; +} diff --git a/util/assets/Actor1.png b/util/assets/Actor1.png new file mode 100755 index 0000000..51b9004 Binary files /dev/null and b/util/assets/Actor1.png differ diff --git a/util/assets/charviewer b/util/assets/charviewer new file mode 100755 index 0000000..11dea06 Binary files /dev/null and b/util/assets/charviewer differ diff --git a/util/assets/little_guy_facing_down.json b/util/assets/little_guy_facing_down.json new file mode 100644 index 0000000..ac0acb5 --- /dev/null +++ b/util/assets/little_guy_facing_down.json @@ -0,0 +1,16 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy facing down", + "width": 48, + "height": 48, + "speed": 0, + "loop": false, + "loopReverse": false, + "frames": [ + 1 + ] +} diff --git a/util/assets/little_guy_facing_left.json b/util/assets/little_guy_facing_left.json new file mode 100644 index 0000000..5778336 --- /dev/null +++ b/util/assets/little_guy_facing_left.json @@ -0,0 +1,16 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy facing left", + "width": 48, + "height": 48, + "speed": 0, + "loop": false, + "loopReverse": false, + "frames": [ + 13 + ] +} diff --git a/util/assets/little_guy_facing_right.json b/util/assets/little_guy_facing_right.json new file mode 100644 index 0000000..5727a02 --- /dev/null +++ b/util/assets/little_guy_facing_right.json @@ -0,0 +1,16 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy facing right", + "width": 48, + "height": 48, + "speed": 0, + "loop": false, + "loopReverse": false, + "frames": [ + 25 + ] +} diff --git a/util/assets/little_guy_facing_up.json b/util/assets/little_guy_facing_up.json new file mode 100644 index 0000000..d6b5aaf --- /dev/null +++ b/util/assets/little_guy_facing_up.json @@ -0,0 +1,16 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy facing up", + "width": 48, + "height": 48, + "speed": 0, + "loop": false, + "loopReverse": false, + "frames": [ + 37 + ] +} diff --git a/util/assets/little_guy_walking_down.json b/util/assets/little_guy_walking_down.json new file mode 100644 index 0000000..3540ae1 --- /dev/null +++ b/util/assets/little_guy_walking_down.json @@ -0,0 +1,18 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy walking down", + "width": 48, + "height": 48, + "speed": 1000, + "loop": true, + "loopReverse": true, + "frames": [ + 0, + 1, + 2 + ] +} diff --git a/util/assets/little_guy_walking_left.json b/util/assets/little_guy_walking_left.json new file mode 100644 index 0000000..e7348f9 --- /dev/null +++ b/util/assets/little_guy_walking_left.json @@ -0,0 +1,18 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy walking left", + "width": 48, + "height": 48, + "speed": 1000, + "loop": true, + "loopReverse": true, + "frames": [ + 12, + 13, + 14 + ] +} diff --git a/util/assets/little_guy_walking_right.json b/util/assets/little_guy_walking_right.json new file mode 100644 index 0000000..a0fd1ca --- /dev/null +++ b/util/assets/little_guy_walking_right.json @@ -0,0 +1,18 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy walking right", + "width": 48, + "height": 48, + "speed": 1000, + "loop": true, + "loopReverse": true, + "frames": [ + 24, + 25, + 26 + ] +} diff --git a/util/assets/little_guy_walking_up.json b/util/assets/little_guy_walking_up.json new file mode 100644 index 0000000..1024c7e --- /dev/null +++ b/util/assets/little_guy_walking_up.json @@ -0,0 +1,18 @@ +{ + "spritesheet": { + "filename": "../assets/Actor1.png", + "frame_width": 48, + "frame_height": 48 + }, + "name": "little guy walking up", + "width": 48, + "height": 48, + "speed": 1000, + "loop": true, + "loopReverse": true, + "frames": [ + 36, + 37, + 38 + ] +} diff --git a/util/assets/littleguy.json b/util/assets/littleguy.json new file mode 100644 index 0000000..789687c --- /dev/null +++ b/util/assets/littleguy.json @@ -0,0 +1,68 @@ +{ + "name": "little guy", + "movementspeed": 1, + "velocity_x": 0.02, + "velocity_y": 0.02, + "sprite_mappings": [ + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_LEFT", + "ACTOR_STATE_MOVING_LEFT" + ], + "sprite": "little guy walking left" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_RIGHT", + "ACTOR_STATE_MOVING_RIGHT" + ], + "sprite": "little guy walking right" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_UP", + "ACTOR_STATE_MOVING_UP" + ], + "sprite": "little guy walking up" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_DOWN", + "ACTOR_STATE_MOVING_DOWN" + ], + "sprite": "little guy walking down" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_UP" + ], + "sprite": "little guy facing up" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_RIGHT" + ], + "sprite": "little guy facing right" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_LEFT" + ], + "sprite": "little guy facing left" + }, + { + "state": [ + "ACTOR_STATE_ALIVE", + "ACTOR_STATE_FACE_DOWN" + ], + "sprite": "little guy facing down" + } + ] +}