Compare commits

...

3 Commits

Author SHA1 Message Date
En Yi 5b7ed2f3e5 Add prototype level map render 2025-01-18 15:51:13 +08:00
En Yi 1e584c1dc3 Add hover on select text 2025-01-18 10:23:23 +08:00
En Yi 15162c64e8 Add font loading and use font for menu 2025-01-18 09:39:36 +08:00
7 changed files with 241 additions and 5 deletions

View File

@ -374,6 +374,14 @@ void UI_button(const UIComp_t* comp, const char* text)
}
void hover_text(const UIComp_t* comp, Font font, const char* text, Vector2 pos, int font_size, int spacing, Color colour) {
if (comp->state == STATE_FOCUSED) {
DrawTextEx(font, text, pos, font_size, spacing, Fade(colour, 0.1));
pos.y -= font_size >> 2;
}
DrawTextEx(font, text, pos, font_size, spacing, colour);
}
// Slider control with pro parameters
// NOTE: Other GuiSlider*() controls use this one

View File

@ -46,4 +46,5 @@ static inline void ScrollAreaRenderBegin(VertScrollArea_t* scroll)
void UI_button(const UIComp_t* bbox, const char* text);
float UI_slider(const UIComp_t* comp, const char *textLeft, const char *textRight, float *value, float minValue, float maxValue);
float UI_vert_slider(const UIComp_t* comp, const char *textTop, const char *textBottom, float* value, float minValue, float maxValue);
void hover_text(const UIComp_t* comp, Font font, const char* text, Vector2 pos, int font_size, int spacing, Color colour);
#endif

3
res/.gitignore vendored
View File

@ -1,9 +1,10 @@
*
!.gitignore
!CMakeLists.txt
!ldtk_repacker.py
!*.py
!pack_ldtk.sh
!pack_resources.sh
!test_assets.info
!testLevels.lvldata.zst
!rres_packer.c
!requirements.txt

202
res/level_render.py 100644
View File

@ -0,0 +1,202 @@
import sys
import argparse
import pprint
import json
import struct
from PIL import Image, ImageDraw
parser = argparse.ArgumentParser()
parser.add_argument('filename')
args = parser.parse_args()
print("Rendering", args.filename)
with open(args.filename, 'r') as f:
level_pack_data = json.load(f)
#pprint.pprint(level_pack_data)
ENUMIDS_TILETYPE_MAPPING = {
'Solid': 1,
'WoodenPlat': 2,
'Ladder': 3,
'LSpike': 4,
'RSpike': 5,
'USpike': 6,
'DSpike': 7,
'EmptyWCrate': 8,
'LArrowWCrate': 9,
'RArrowWCrate': 10,
'UArrowWCrate': 11,
'DArrowWCrate': 12,
'BombWCrate': 13,
'EmptyMCrate': 14,
'LArrowMCrate': 15,
'RArrowMCrate': 16,
'UArrowMCrate': 17,
'DArrowMCrate': 18,
'BombMCrate': 19,
'Boulder': 20,
'Runner': 21,
'Player': 22,
'Chest': 23,
'Exit': 24,
'Urchin': 25,
}
REC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s, y+s)), c)
HALFREC_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.rectangle(((x,y), (x+s, y+s//2)), c)
CIRCLE_DRAW_FUNCTION = lambda ctx,x,y,s,c : ctx.circle((x+s//2, y+s//2), s//2, c)
TILETYPE_SHAPE_MAP = {
'Solid': (REC_DRAW_FUNCTION, (0,0,0,255)),
'WoodenPlat': (HALFREC_DRAW_FUNCTION, (128,64,0,255)),
'Ladder': (REC_DRAW_FUNCTION, (214,141,64,255)),
#'LSpike': 4,
#'RSpike': 5,
'USpike': (HALFREC_DRAW_FUNCTION, (64,0,0,255)),
#'DSpike': 7,
'EmptyWCrate': (REC_DRAW_FUNCTION, (160,117,48,255)),
#'LArrowWCrate': 9,
#'RArrowWCrate': 10,
#'UArrowWCrate': 11,
#'DArrowWCrate': 12,
#'BombWCrate': 13,
'EmptyMCrate': (REC_DRAW_FUNCTION, (225,225,225,255)),
#'LArrowMCrate': 15,
#'RArrowMCrate': 16,
#'UArrowMCrate': 17,
#'DArrowMCrate': 18,
#'BombMCrate': 19,
'Boulder': (CIRCLE_DRAW_FUNCTION, (45,45,45,255)),
#'Runner': 21,
#'Player': 22,
'Player': (REC_DRAW_FUNCTION, (255,0,255,255)),
'Chest': (REC_DRAW_FUNCTION, (255,255,0,255)),
'Exit': (REC_DRAW_FUNCTION, (0,255,0,255)),
#'Urchin': 25,
}
# First go to tilesets and find Simple_tiles identifier, then find enumTags to identifier which tile type is what tileid
ids_tiletype_map = {}
tileset_defs = level_pack_data["defs"]["tilesets"]
for ts_def in tileset_defs:
if ts_def["identifier"] != "Items_spritesheet":
continue
for tag in ts_def["enumTags"]:
ids_tiletype_map[tag["tileIds"][0]] = tag["enumValueId"]
if not ids_tiletype_map:
print("No tileset definition")
sys.exit(1)
pprint.pprint(ids_tiletype_map)
def get_level_order(lvl) -> int:
order = 65535;
for data in lvl['fieldInstances']:
if data["__identifier"] == "Order":
order = data["__value"]
return order
all_levels = level_pack_data["levels"]
all_levels.sort(key=get_level_order)
# Number of levels is the length of the levels
n_levels = len(all_levels)
print("Number of levels:", n_levels)
fileparts = args.filename.split('.')
if len(fileparts) == 1:
fileparts.append("lvldat")
else:
fileparts[-1] = "lvldata"
converted_filename = '.'.join(fileparts)
# First run the entire level pack to figure out the largest dimensions required.
# tile size needs to be dynamic. Fix the output dimensions (and therefore aspect ratio)
# figure out width and height of level
# Get the tile size that fit within the dimensions
# Error out if tile size is zero
# Figure out the offset to center the render
TILE_SIZE = 8
# Each level should be packed as: [width, 2 bytes][height, 2 bytes][tile_type,entity,water,padding 1,1,1,1 bytes][tile_type,entity,water,padding 1,1,1,1 bytes]...
# Then loop the levels. Read the layerIndstances
for level in all_levels[3:4]:
n_chests : int = 0
# Search for __identifier for the level layout
level_name = ""
level_metadata = level['fieldInstances']
level_tileset = 0;
for data in level_metadata:
if data["__identifier"] == "TileSet":
level_tileset = data["__value"]
if data["__identifier"] == "Name":
level_name = data["__value"]
print("Parsing level", level_name)
level_layout = {}
entity_layout = {}
water_layout = {}
for layer in level['layerInstances']:
if layer["__identifier"] == "Tiles":
level_layout = layer
elif layer["__identifier"] == "Entities":
entity_layout = layer
elif layer["__identifier"] == "Water":
water_layout = layer
# Dimensions of each level is obtained via __cWid and __cHei. Get the __gridSize as well
width = level_layout["__cWid"]
height = level_layout["__cHei"]
print(f"Dim.: {width}x{height}. N Tiles: {width * height}")
IMG_WIDTH = width * TILE_SIZE
IMG_HEIGHT = height * TILE_SIZE
# Create a W x H array of tile information
n_tiles = width * height
lvl_render = Image.new("RGBA", (IMG_WIDTH, IMG_HEIGHT), (255,255,255,0))
render_ctx = ImageDraw.Draw(lvl_render)
# Loop through gridTiles, get "d" as the index to fill the info
for i, tile in enumerate(level_layout["gridTiles"]):
x, y= (tile["d"][0] % width * TILE_SIZE, tile["d"][0] // width * TILE_SIZE)
try:
if ids_tiletype_map[tile["t"]] in TILETYPE_SHAPE_MAP:
draw_func, colour = TILETYPE_SHAPE_MAP[ids_tiletype_map[tile["t"]]]
draw_func(render_ctx, x, y, TILE_SIZE, colour)
else:
print(ids_tiletype_map[tile["t"]], "is not mapped");
except Exception as e:
print("Error on tile", i, x, y)
print(e)
lvl_render.show()
#for i, water_level in enumerate(water_layout["intGridCsv"]):
# tiles_info[i][2] = water_level
## Subject to change
#for ent in entity_layout["entityInstances"]:
# x,y = ent["__grid"]
# tiles_info[y*width + x][0] = ENUMIDS_TILETYPE_MAPPING[ent["__identifier"]]
# if ent["__identifier"] == "Urchin":
# spd_encoding = 0
# for urchin_data in ent['fieldInstances']:
# if urchin_data["__identifier"] == "Direction":
# spd_encoding |= urchin_data["__value"] << 2
# elif urchin_data["__identifier"] == "SpeedLevel":
# spd_encoding |= urchin_data["__value"]
# tiles_info[y*width + x][0] += spd_encoding
break
#for y in range(height):
# for x in range(width):
# print(tiles_info[y*width + x], end=" ")
# print()

View File

@ -0,0 +1 @@
Pillow

View File

@ -7,6 +7,7 @@ typedef enum AssetInfoType
TEXTURE_INFO,
SPRITE_INFO,
SOUND_INFO,
FONT_INFO,
EMITTER_INFO,
LEVELPACK_INFO,
INVALID_INFO
@ -80,6 +81,8 @@ static inline AssetInfoType_t get_asset_type(const char* str)
if (strcmp(str, "LevelPack") == 0) return LEVELPACK_INFO;
if (strcmp(str, "Font") == 0) return FONT_INFO;
return INVALID_INFO;
}
@ -272,12 +275,22 @@ bool load_from_infofile(const char* file, Assets_t* assets)
{
if (add_sound(assets, name, info_str) == NULL)
{
printf("Unable to add texture at line %lu\n", line_num);
printf("Unable to add sound at line %lu\n", line_num);
break;
}
printf("Added sound %s as %s\n", info_str, name);
}
break;
case FONT_INFO:
{
if (add_font(assets, name, info_str) == NULL)
{
printf("Unable to add font at line %lu\n", line_num);
break;
}
printf("Added font %s as %s\n", info_str, name);
}
break;
case LEVELPACK_INFO:
{
//if (add_level_pack(assets, name, info_str) == NULL)

View File

@ -11,12 +11,17 @@ static void menu_scene_render_func(Scene_t* scene)
Sprite_t* title_spr = get_sprite(&scene->engine->assets, "title_board");
Sprite_t* title_select = get_sprite(&scene->engine->assets, "title_select");
Rectangle render_rec = scene->layers.render_layers[0].render_area;
Font* menu_font = get_font(&scene->engine->assets, "MenuFont");
BeginTextureMode(scene->layers.render_layers[0].layer_tex);
ClearBackground(RAYWHITE);
draw_sprite(spr, 0, (Vector2){0, 0}, 0, false);
draw_sprite(title_spr, 0, (Vector2){32, 10}, 0, false);
int title_width = MeasureText("Bunny's Spelunking Adventure", 32);
DrawText("Bunny's Spelunking Adventure", (render_rec.width - title_width) / 2, 20, 32, BLACK);
Vector2 font_sz = MeasureTextEx(*menu_font, "Bunny's Spelunking Adventure", 56, 0);
Vector2 title_pos = {
.x = (render_rec.width - font_sz.x) / 2,
.y = 32 + (title_spr->frame_size.y - font_sz.y) / 2
};
DrawTextEx(*menu_font, "Bunny's Spelunking Adventure", title_pos, 56, 0, BLACK);
const char* OPTIONS[3] = {"Start", "Credits", "Exit"};
for (uint8_t i = 0; i < 3; ++i)
@ -33,7 +38,12 @@ static void menu_scene_render_func(Scene_t* scene)
)
);
draw_sprite(title_select, 0, pos, 0, false);
UI_button(data->buttons + i, OPTIONS[i]);
font_sz = MeasureTextEx(*menu_font, OPTIONS[i], 32, 6);
Vector2 title_pos = {
.x = pos.x + (title_select->frame_size.x - font_sz.x) / 2,
.y = pos.y + (title_spr->frame_size.y) / 2 - font_sz.y
};
hover_text(data->buttons + i, *menu_font, OPTIONS[i], title_pos, 32, 6, BLACK);
}
EndTextureMode();
}