Added charviewer

This commit is contained in:
2026-01-05 08:57:38 -05:00
parent 0601a8d767
commit 7cff27f035
14 changed files with 342 additions and 0 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
./build/* ./build/*
.aider*
*~

12
include/controller.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef _GAMEPAD_H_
#define _GAMEPAD_H_
#include <SDL3/SDL.h>
#include <sdlerror.h>
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_

124
tests/charviewer.c Normal file
View File

@@ -0,0 +1,124 @@
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_properties.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <sdlerror.h>
#include <sdl3game/assets.h>
#include <sdl3game/iterator.h>
#include <sdl3game/tilemap.h>
#include <sdl3game/heap.h>
#include <sdl3game/game.h>
#include <sdl3game/controller.h>
#include <sdl3game/draw.h>
#include <sdl3game/sprite.h>
#include <sdl3game/actor.h>
#include <sdl3game/registry.h>
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;
}

BIN
util/assets/Actor1.png Executable file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

BIN
util/assets/charviewer Executable file

Binary file not shown.

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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
]
}

View File

@@ -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"
}
]
}