commit twrice a week

This commit is contained in:
KikooDX 2022-04-01 18:31:34 +02:00
parent 9ea7bf73bb
commit 692beeca17
15 changed files with 165 additions and 40 deletions

View File

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

View File

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

View File

@ -4,3 +4,5 @@
#define CHR_HEIGHT 16
#define DISPLAY_WIDTH 400
#define DISPLAY_HEIGHT 224
#define TSET_LINE 13
#define TILE_SIZE 16

10
inc/input.h Normal file
View File

@ -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);

5
inc/level.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
void level_deinit(void);
void level_load(int id);
void level_find(int tile, int *x, int *y);

View File

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

BIN
res/0.kble Normal file

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.8 KiB

6
sle.sh Executable file
View File

@ -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 $@

38
src/input.c Normal file
View File

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

48
src/level.c Normal file
View File

@ -0,0 +1,48 @@
#include "level.h"
#include "conf.h"
#include "levels_bin.h"
#include "lzy.h"
#include "player.h"
#include <stdint.h>
#include <stdlib.h>
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;
}
}
}

View File

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

View File

@ -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();
}

View File

@ -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);
}