···11#ifndef __kf_actor__
22#define __kf_actor__
3344+45#include <keraforge/_header.h>
56#include <keraforge/math.h>
67#include <keraforge/world.h>
···89#include <keraforge/sprites.h>
910#include <raylib.h>
10111212+1313+/* Represents any kinematic body in the world (players, NPCs, etc). */
1114struct kf_actor
1215{
1616+ /* Spritesheet for the actor. */
1317 struct kf_spritesheet sprites;
1414- struct kf_vec2(f32) pos, vel, size, sizeoffset;
1515- f32 speed, speedmod, friction;
1616- bool collide, controlled;
1818+ /* The actor's position. */
1919+ struct kf_vec2(f32) pos;
2020+ /* The actor's velocity. */
2121+ struct kf_vec2(f32) vel;
2222+ /* The actor's size. Default origin is 0,0. */
2323+ struct kf_vec2(f32) size;
2424+ /* An offset to apply to the actor's collision rect. */
2525+ struct kf_vec2(f32) sizeoffset;
2626+ /* Actor's speed. */
2727+ f32 speed;
2828+ /* Speed modifier for the actor. Gets multiplied by the actor's speed when moving. */
2929+ f32 speedmod;
3030+ /* Friction to apply to the actor while moving. */
3131+ f32 friction;
3232+ /* Indicates if collision processing should be applied to this actor. */
3333+ bool collide;
3434+ /* When true, the actor's `pointing` field is expected to be updated manually. */
3535+ bool controlled;
3636+ /* The direction that the actor is pointing. */
1737 enum kf_direction pointing;
3838+ /* Called every frame to update the actor. Don't forget about deltatime (kf_dts, kf_dtms)! */
1839 void (*tick)(struct kf_world *world, struct kf_actor *self);
4040+ /* Called every frame to render the actor. */
1941 void (*draw)(struct kf_world *world, struct kf_actor *self);
2042};
21434444+4545+/* Create a new actor. */
2246struct kf_actor *kf_actor_new(struct kf_spritesheet sprites, f32 width, f32 height, bool collides);
23472448/* Load a spritesheet, filling in the details for loading character spritesheets. */
···3256void kf_actor_move(struct kf_world *world, struct kf_actor *actor, f32 deltatime);
3357/* Draw the actor. */
3458void kf_actor_draw(struct kf_actor *actor);
5959+35603661#endif
···11#ifndef __kf_graphics__
22#define __kf_graphics__
3344+45#include <keraforge/_header.h>
66+5768/* Number of frames since the game opened.
79 You should only use this for animations!
···1113extern f32 kf_dtms;
1214/* Deltatime in seconds. */
1315extern f32 kf_dts;
1616+14171518#endif
+26-2
include/keraforge/input.h
···11#ifndef __kf_input__
22#define __kf_input__
3344+45#include <keraforge/_header.h>
56#include <raylib.h>
77+6879#define MOUSE_BUTTON_UNKNOWN ((MouseButton)-1)
810#define GAMEPAD_AXIS_UNKNOWN ((GamepadAxis)-1)
···1113typedef u8 kf_inputbind_t;
1214#define KF_INPUTBIND_NONE ((kf_inputbind_t)0)
13151616+1717+/* Struct-of-Arrays for keybindings. */
1418struct _kf_inputbinds
1519{
1620 kf_inputbind_t count; /* must start at 1. 0 is the `none` keybind. */
1721 char *id[KF_INPUTBIND_MAX];
1822 KeyboardKey key[KF_INPUTBIND_MAX];
2323+ KeyboardKey alt[KF_INPUTBIND_MAX];
1924 MouseButton mouse[KF_INPUTBIND_MAX];
2025 GamepadButton gamepad[KF_INPUTBIND_MAX];
2126 GamepadAxis axis[KF_INPUTBIND_MAX];
2227};
23282929+2430/* Deadzone for gamepads. Default is 0.2f. */
2531extern f32 kf_deadzone;
2626-3232+/* Input binding struct-of-arrays. You should not mutate this directly (use kf_addinput). */
2733extern struct _kf_inputbinds kf_inputbinds;
3434+28352936/* Add a new input binding. */
3030-kf_inputbind_t kf_addinput(char *id, KeyboardKey key, MouseButton mouse, GamepadButton gamepad, GamepadAxis axis);
3737+kf_inputbind_t kf_addinput(char *id, KeyboardKey key, KeyboardKey alt, MouseButton mouse, GamepadButton gamepad, GamepadAxis axis);
3138/* Get an input's index by it's translation key. */
3239kf_inputbind_t kf_getinput(char *id);
33404141+/* Check if the given key was pressed this frame. */
3442int kf_checkkeypress(kf_inputbind_t id);
4343+/* Check if the given key was released this frame. */
3544int kf_checkkeyrelease(kf_inputbind_t id);
4545+/* Check if the given key is down. */
3646int kf_checkkeydown(kf_inputbind_t id);
4747+/* Check if the given key is up. */
3748int kf_checkkeyup(kf_inputbind_t id);
4949+/* Check if the given mouse button was pressed this frame. */
3850int kf_checkmousepress(kf_inputbind_t id);
5151+/* Check if the given mouse button was released this frame. */
3952int kf_checkmouserelease(kf_inputbind_t id);
5353+/* Check if the given mouse button is down. */
4054int kf_checkmousedown(kf_inputbind_t id);
5555+/* Check if the given mouse button is up. */
4156int kf_checkmouseup(kf_inputbind_t id);
5757+/* Check if the given gamepad button was pressed this frame. */
4258int kf_checkgamepadpress(kf_inputbind_t id);
5959+/* Check if the given gamepad button was released this frame. */
4360int kf_checkgamepadrelease(kf_inputbind_t id);
6161+/* Check if the given gamepad button is down. */
4462int kf_checkgamepaddown(kf_inputbind_t id);
6363+/* Check if the given gamepad button is up. */
4564int kf_checkgamepadup(kf_inputbind_t id);
6565+/* Get the given gamepad axis' value (-1 to +1). */
4666float kf_getgamepadaxis(kf_inputbind_t id);
47676868+/* Check if the given input binding was pressed this frame. */
4869int kf_checkinputpress(kf_inputbind_t id);
7070+/* Check if the given input binding was released this frame. */
4971int kf_checkinputrelease(kf_inputbind_t id);
7272+/* Check if the given input binding is down. */
5073int kf_checkinputdown(kf_inputbind_t id);
7474+/* Check if the given input binding is up. */
5175int kf_checkinputup(kf_inputbind_t id);
52765377
+9
include/keraforge/math.h
···11#ifndef __kf_math__
22#define __kf_math__
3344+45#include <keraforge/_header.h>
56#include <raylib.h>
67#include <raymath.h>
88+79810#define _kf_mathdef(x) \
911 x(i8); \
···5153_kf_mathdef(x)
5254#undef x
53555656+5757+/* Represents a NESW direction. */
5458enum kf_direction
5559{
5660 kf_north,
···5963 kf_west,
6064};
61656666+6767+/* Rotate a direction clockwise. */
6268enum kf_direction kf_rotatecw(enum kf_direction dir);
6969+/* Rotate a direction counterclockwise. */
6370enum kf_direction kf_rotateccw(enum kf_direction dir);
7171+/* Get a vec2(f32) representing the direction. */
6472struct kf_vec2(f32) kf_dtov2(enum kf_direction dir);
7373+65746675#endif
+7
include/keraforge/sprites.h
···11#ifndef __kf_sprites__
22#define __kf_sprites__
3344+45#include <keraforge/_header.h>
56#include <raylib.h>
6788+99+/* Represents a single texture containing multiple sprites.
1010+ Can be used for animations or texture atlases. */
711struct kf_spritesheet
812{
913 Texture2D texture;
···1115 u32 nsprites;
1216};
13171818+1919+/* Load a sprite sheet with the given sprite width/height. */
1420struct kf_spritesheet kf_loadspritesheet(char *filename, int spritewidth, int spriteheight);
2121+/* Draw a single sprite from the sheet at the given coordinates. */
1522void kf_drawsprite(struct kf_spritesheet *sheet, f32 x, f32 y, int spritex, int spritey);
16231724#endif
+28-1
include/keraforge/ui.h
···11#ifndef __kf_ui__
22#define __kf_ui__
3344+45#include <keraforge/input.h>
5677+88+/* Holds global config settings for UIs. */
69struct kf_uiconfig
710{
1111+ /* Keybindings to navigate UIs. */
812 kf_inputbind_t select, cancel, up, down;
99- char *fmt, *selectfmt;
1313+ /* Format string for choices. */
1414+ char *fmt;
1515+ /* Format string for selected choices. */
1616+ char *selectfmt;
1017 int fontsize;
1118 Color bg, fg;
1919+ /* Height of the UI panel. */
1220 int panelheight;
2121+ /* Padding for the panel. */
1322 int xpadding, ypadding;
1423};
15242525+/* Get a pointer to the global UI config. */
1626struct kf_uiconfig *kf_ui_getconfig(void);
17272828+/* Draw a panel with the given title. Returns the Y position for the next line of text.
2929+ title: Title of the panel, drawn at the top of the panel. Must be null-terminated. */
1830int kf_ui_panel(char *title);
19313232+/* Present the player with a choice menu.
3333+ title: Panel title. Must be null-terminated.
3434+ choices: Array of possible choices. Must be null-terminated.
3535+ nchoices: Length of the array.
3636+ choice: Pointer to the selected choice. This will be mutated.
3737+ Returns: 1 if the user has made a choice, otherwise 0. */
2038int kf_ui_choice(char *title, char **choices, int nchoices, int *choice);
3939+/* Present the player with a yes/no menu.
4040+ title: Panel title. Must be null-terminated.
4141+ choice: Pointer to the selected choice. This will be mutated. 1 is yes, 0 is no.
4242+ Returns: 1 if the user has made a choice, otherwise 0. */
2143int kf_ui_yesno(char *title, int *choice);
4444+/* Present the player with a text input menu.
4545+ title: Panel title. Must be null-terminated.
4646+ text: The inputted text. If NULL, it will be malloc'd automatically.
4747+ If it needs to be grown, it will be realloc'd.
4848+ Returns: 1 if the user has made a choice, otherwise 0. */
2249int kf_ui_textinput(char *title, char *text);
23502451#endif
+23-3
include/keraforge/world.h
···11#ifndef __kf_world__
22#define __kf_world__
3344+45#include <keraforge/_header.h>
66+#include <keraforge/math.h>
57#include <raylib.h>
6899+710#define KF_TILEID_MAX UINT16_MAX
811typedef u16 kf_tileid_t;
912#define KF_TILE_SIZE_PX 16
10131414+1515+struct kf_actor; /* Forward declaration */
1616+1717+1818+/* Represents a world (or a subworld, often called "rooms"). */
1119struct kf_world
1220{
1313- /* Never ever reorder `revision` to be after anything.
2121+ /* Marks the version of this map, used for migrating from one engine version to another.
2222+ Never ever reorder `revision` to be after anything.
1423 If you add something before it or move it then the
1524 version checker will compare the first u32 in the
1625 map's binary to the expected revision, which will
1726 almost always be wrong. */
1827 u32 revision;
2828+ /* Width of the map. */
1929 u32 width;
3030+ /* Height of the map. */
2031 u32 height;
3232+ /* Array of tiles in the map. Use kf_gettile to get a tile using an X/Y position. */
2133 kf_tileid_t map[];
2234};
23353636+/* Struct-of-arrays for tiles. See: kf_tiles */
2437struct _kf_tiles
2538{
2639 kf_tileid_t count;
4040+ /* Translation key of the tile. */
2741 char *key[KF_TILEID_MAX];
2828- Color color[KF_TILEID_MAX];
4242+ /* The tile's colour on a world map. */
4343+ Color mapcol[KF_TILEID_MAX];
4444+ /* The spritesheet this tile's sprite belongs to. */
4545+ struct kf_spritesheet *sheet[KF_TILEID_MAX];
4646+ /* Spritesheet coords. */
4747+ struct kf_vec2(u32) sprite[KF_TILEID_MAX];
4848+ /* Whether or not this tile has collision. */
2949 bool collide[KF_TILEID_MAX];
3050};
5151+/* Struct-of-arrays for tiles. */
3152extern struct _kf_tiles kf_tiles;
32533354/* Create a world using the given width and height.
···4162kf_tileid_t *kf_world_gettile(struct kf_world *world, u32 x, u32 y);
42634364/* Draw visible collision rectangles. */
4444-struct kf_actor;
4565void kf_world_drawcolliders(struct kf_world *world, struct kf_actor *player, Camera2D camera);
46664767/* Draw the part of the world visible to the given camera. */