diff --git a/CMakeLists.txt b/CMakeLists.txt index fce70ab..f7b2911 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,9 @@ include_directories(inc) set(SOURCES src/main.c src/lzy.c + src/input.c + src/player.c + src/level.c ) set(ASSETS diff --git a/Makefile b/Makefile index e371044..e46d005 100644 --- a/Makefile +++ b/Makefile @@ -1,23 +1,27 @@ CC ?= gcc -CFLAGS = -std=c99 -Wall -Wextra -O3 -I./inc -MMD $(shell sdl2-config --cflags) +CFLAGS = -std=c99 -Wall -Wextra -Os -I./inc -MMD $(shell sdl2-config --cflags) LDFLAGS = -lSDL2 -lSDL2_image -lSDL2_mixer $(shell sdl2-config --libs) OBJ_NAME = wehfou OBJS := $(patsubst %.c,%.o,$(wildcard src/*.c)) +LVLS_BIN = inc/levels_bin.h +LVLS := $(wildcard res/*.kble) -all: $(OBJ_NAME) +all: $(LVLS_BIN) $(OBJ_NAME) $(OBJ_NAME): $(OBJS) $(CC) $(LDFLAGS) $(LIBRARIES) -o $(OBJ_NAME) $(OBJS) - strip $(OBJ_NAME) %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -cg: +$(LVLS_BIN): $(LVLS) + cembed -o $(LVLS_BIN) -t levels $(LVLS) + +cg: $(LVLS_BIN) fxsdk build-cg -run: $(OBJ_NAME) +run: all ./$(OBJ_NAME) format: @@ -26,7 +30,7 @@ format: clean: rm -f $(OBJ_NAME).g3a $(OBJ_NAME) - rm -f $(OBJS) src/*.d + rm -f $(LVLS_BIN) $(OBJS) src/*.d rm -Rf build-cg .PHONY: cg run run-txt format clean diff --git a/inc/conf.h b/inc/conf.h index 30fceb0..759a9b1 100644 --- a/inc/conf.h +++ b/inc/conf.h @@ -4,3 +4,5 @@ #define CHR_HEIGHT 16 #define DISPLAY_WIDTH 400 #define DISPLAY_HEIGHT 224 +#define TSET_LINE 13 +#define TILE_SIZE 16 diff --git a/inc/input.h b/inc/input.h new file mode 100644 index 0000000..73452a4 --- /dev/null +++ b/inc/input.h @@ -0,0 +1,10 @@ +#pragma once + +enum Key { K_LEFT, K_RIGHT, K_UP, K_DOWN, K_O, K_X }; + +enum KeyState { KS_UP, KS_DOWN, KS_PRESSED }; + +void input_update(void); +int input_up(unsigned int); +int input_down(unsigned int); +int input_pressed(unsigned int); diff --git a/inc/level.h b/inc/level.h new file mode 100644 index 0000000..8b4e542 --- /dev/null +++ b/inc/level.h @@ -0,0 +1,5 @@ +#pragma once + +void level_deinit(void); +void level_load(int id); +void level_find(int tile, int *x, int *y); diff --git a/inc/player.h b/inc/player.h index c1d6d7a..d0fa202 100644 --- a/inc/player.h +++ b/inc/player.h @@ -1,6 +1,7 @@ #pragma once void player_init(float x, float y); +void player_update(void); void player_draw(void); #define PLAYER_WIDTH 12 diff --git a/res/0.kble b/res/0.kble new file mode 100644 index 0000000..d1ba8be Binary files /dev/null and b/res/0.kble differ diff --git a/res/font.png b/res/font.png index c6d3af0..6c06089 100644 Binary files a/res/font.png and b/res/font.png differ diff --git a/res/tset.png b/res/tset.png index f963f41..b1920b6 100644 Binary files a/res/tset.png and b/res/tset.png differ diff --git a/sle.sh b/sle.sh new file mode 100755 index 0000000..a50ad16 --- /dev/null +++ b/sle.sh @@ -0,0 +1,6 @@ +#!/bin/sh +# https://sr.ht/~kikoodx/sle +sle -tile-width 16 -tile-height 16 -level-width 25 -level-height 14 \ +-editor-width 400 -editor-height 224 -editor-off-x 0 -editor-off-y 0 \ +-editor-bg-color '#000000' -picker-bg-color '#000005' \ +-tileset res/tset.png $@ diff --git a/src/input.c b/src/input.c new file mode 100644 index 0000000..86264b3 --- /dev/null +++ b/src/input.c @@ -0,0 +1,38 @@ +#include "input.h" +#include "lzy.h" + +static const unsigned int keys[6] = {LZYK_LEFT, LZYK_RIGHT, LZYK_UP, + LZYK_DOWN, LZYK_O, LZYK_X}; +static int states[6] = {0}; + +void input_update(void) +{ + int i = 6; + while (i-- > 0) + if (LZY_KeyDown(keys[i])) + states[i] = + (states[i] == KS_UP) ? (KS_PRESSED) : (KS_DOWN); + else + states[i] = KS_UP; +} + +int input_up(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_UP; +} + +int input_down(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_DOWN; +} + +int input_pressed(unsigned int k) +{ + if (k >= 6) + return 0; + return states[k] == KS_PRESSED; +} diff --git a/src/level.c b/src/level.c new file mode 100644 index 0000000..72e60d1 --- /dev/null +++ b/src/level.c @@ -0,0 +1,48 @@ +#include "level.h" +#include "conf.h" +#include "levels_bin.h" +#include "lzy.h" +#include "player.h" +#include +#include + +static int width, height, id; +static uint8_t *data = NULL; + +void level_deinit(void) +{ + if (data != NULL) { + free(data); + data = NULL; + } +} + +void level_load(int nid) +{ + const uint8_t *const s = levels[nid].data; + + width = s[3]; + height = s[5]; + data = calloc(width * height, sizeof(uint8_t)); + id = nid; + + for (int i = 0; i < width * height; i++) + data[i] = s[6 + i]; + + int px = 0, py = 0; + level_find(2, &px, &py); + player_init(px, py); +} + +void level_find(int tile, int *x, int *y) +{ + for (int i = 0; i < width * height; i++) { + if (data[i] == tile) { + if (x != NULL) + *x = i % width * TILE_SIZE; + if (y != NULL) + *y = i % height * TILE_SIZE; + return; + } + } +} diff --git a/src/lzy.c b/src/lzy.c index c4e903b..a05166d 100644 --- a/src/lzy.c +++ b/src/lzy.c @@ -7,4 +7,5 @@ #define LZY_DISPLAY_WIDTH DISPLAY_WIDTH #define LZY_DISPLAY_HEIGHT DISPLAY_HEIGHT #define LZY_FIRST_CHR ' ' +#define LZY_TILE_SIZE TILE_SIZE #include "lzy.h" diff --git a/src/main.c b/src/main.c index acd2aba..7cb99e3 100644 --- a/src/main.c +++ b/src/main.c @@ -1,50 +1,44 @@ #include "conf.h" +#include "input.h" +#include "level.h" #include "lzy.h" +#include "player.h" -int main(int argc, const char **argv) +static void deinit(void); + +int main(int argc, char **argv) { - int x = 0; - int y = 0; - - if (LZY_Init(argc, argv, "wehfou official goty", 30, "res/tset.png", - "res/font.png")) { + if (LZY_Init(argc, (const char **)argv, "wehfou official goty", 30, + "res/tset.png", "res/font.png")) { LZY_Log(LZY_GetError()); - LZY_Quit(); + deinit(); return 1; } + level_load(0); + player_init(0, 0); + while (!LZY_ShouldQuit()) { - /* update */ LZY_CycleEvents(); + input_update(); + player_update(); - if (LZY_KeyDown(LZYK_LEFT)) - x -= 2; - if (LZY_KeyDown(LZYK_RIGHT)) - x += 2; - if (LZY_KeyDown(LZYK_UP)) - y -= 2; - if (LZY_KeyDown(LZYK_DOWN)) - y += 2; - - /* draw */ LZY_DrawBegin(); - { - /* draw background */ - LZY_DrawTileEx(0, 0, 0, 13, 7); - LZY_DrawTileEx(0, DISPLAY_WIDTH / 2, 0, 13, 7); - LZY_DrawTileEx(0, 0, DISPLAY_HEIGHT / 2, 13, 7); - LZY_DrawTileEx(0, DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2, - 13, 7); - - /* draw player */ - if (LZY_DrawChar('s', x, y)) - LZY_Log(LZY_GetError()); - } + LZY_DrawTileEx(TSET_LINE, 0, 0, 13, 7); + LZY_DrawTileEx(TSET_LINE, DISPLAY_WIDTH / 2, 0, 13, 7); + LZY_DrawTileEx(TSET_LINE, 0, DISPLAY_HEIGHT / 2, 13, 7); + LZY_DrawTileEx(TSET_LINE, DISPLAY_WIDTH / 2, DISPLAY_HEIGHT / 2, + 13, 7); + player_draw(); LZY_DrawEnd(); } - LZY_Log("cya"); - LZY_Quit(); - + deinit(); return 0; } + +static void deinit(void) +{ + level_deinit(); + LZY_Quit(); +} diff --git a/src/player.c b/src/player.c index 85bf2a7..0bff092 100644 --- a/src/player.c +++ b/src/player.c @@ -1,4 +1,5 @@ #include "player.h" +#include "input.h" #include "lzy.h" static float x, y, spd_x, spd_y; @@ -11,8 +12,20 @@ void player_init(float nx, float ny) spd_y = 0.0f; } +void player_update(void) +{ + if (input_down(K_LEFT)) + x -= 2; + if (input_down(K_RIGHT)) + x += 2; + if (input_down(K_UP)) + y -= 2; + if (input_down(K_DOWN)) + y += 2; +} + void player_draw(void) { LZY_DrawSetColor(255, 0, 255); - LZY_DrawFillRect(x, y, PLAYER_WIDTH, PLAYER_HEIGHT); + LZY_DrawTile(2, x, y); }