Files
libakgl/tests/charviewer.c

126 lines
4.7 KiB
C

#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 <akerror.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>
#include <sdl3game/error.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, AKERR_SDL, "Couldn't initialize SDL: %s", SDL_GetError());
}
if (!SDL_CreateWindowAndRenderer("net/aklabs/libsdl3game/test_sprite", 640, 480, 0, &window, &renderer)) {
FAIL_BREAK(errctx, AKERR_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, AKERR_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;
}