diff --git a/.gitignore b/.gitignore index be59904..1077e4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ build 2D_Engine_Casio_Tool GameData.bin -assets/maps/__pycache__ +assets/__pycache__ diff --git a/Makefile b/Makefile index f029ba4..3cca5c0 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ LDFLAGS = -m32 -no-pie # Converter flags FXCONV_FLAGS = --cg --toolchain= --arch=i386 --outputtarget=elf32-x86-64 -FXCONV_CONVERTERS = assets/maps/map_converter.py +FXCONV_CONVERTER = assets/converters.py # Change output location OUTPUT := $(BUILD_DIR)/$(OUTPUT) @@ -78,7 +78,7 @@ $(BUILD_DIR)/%.o: % $(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1)))) $(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_FILES))))) @echo "[$(PERCENTAGE)%] $(GREEN)Building FXCONV object $@$(RESET)" - $(Q)fxconv $(FXCONV_FLAGS) --converter=$(FXCONV_CONVERTERS) $< -o $@ + $(Q)fxconv $(FXCONV_FLAGS) --converter=$(FXCONV_CONVERTER) $< -o $@ $(Q)objcopy --add-section .note.GNU-stack=/dev/null $@ # Source files dependencies diff --git a/assets/converters.py b/assets/converters.py new file mode 100644 index 0000000..30e07a4 --- /dev/null +++ b/assets/converters.py @@ -0,0 +1,75 @@ +import fxconv +from fxconv import u8, u16, u32, ref, string +import json + +def convert(input, output, params, target): + if params["custom-type"] == "map": + convert_map(input, output, params, target) + return 0 + else: + return 1 + +def convert_map(input, output, params, target): + with open(input, "r") as fp: + map = json.load(fp) + + TRANSFORM_COMPONENT = 0 + SPRITE_COMPONENT = 1 + ANIMATION_SYSTEM = 2 + + background_layer1 = b"".join(u16(tile) for tile in map["BackgroundLayer1"]) + background_layer2 = b"".join(u16(tile) for tile in map["BackgroundLayer2"]) + background_layer3 = b"".join(u16(tile) for tile in map["BackgroundLayer3"]) + foreground = b"".join(u16(tile) for tile in map["Foreground"]) + + entities = fxconv.Structure() + + for e in map["Entities"]: + entities += u16(e["ID"]) + entities += u32(len(e["Components"])) + + components = fxconv.Structure() + + for c in e["Components"]: + component_data = fxconv.Structure() + + match c["Type"]: + case "TransformComponent": + components += u32(TRANSFORM_COMPONENT) + component_data += u32(c["x"]) + component_data += u32(c["y"]) + component_data += u32(c["w"]) + component_data += u32(c["h"]) + component_data += u32(c["Speed"]) + + case "SpriteComponent": + components += u32(SPRITE_COMPONENT) + component_data += string(c["TextureName"]) + + case "AnimationSystem": + components += u32(ANIMATION_SYSTEM) + component_data += u32(c["NbFrames"]) + component_data += u32(c["ActualFrame"]) + component_data += u32(c["FrameDelayMs"]) + component_data += u8(c["Play"]) + component_data += u8(c["Loop"]) + component_data += u8(c["Reverse"]) + + case _: + raise fxconv.FxconvError(f"unknown component type {c['Type']}") + + components += ref(component_data) + + entities += ref(components) + + o = fxconv.ObjectData() + o += string(params["name"]) + o += u32(map["MapWidth"]) + u32(map["MapHeight"]) + o += ref(background_layer1) + o += ref(background_layer2) + o += ref(background_layer3) + o += ref(foreground) + o += u32(len(map["Entities"])) + o += ref(entities) + + fxconv.elf(o, output, params["name"], **target) diff --git a/assets/maps/example.json b/assets/maps/example.json index 808da26..f554eee 100644 --- a/assets/maps/example.json +++ b/assets/maps/example.json @@ -47,7 +47,7 @@ ] }, { - "ID": 2034, + "ID": 345, "Components": [ { "Type": "TransformComponent", diff --git a/assets/maps/map_converter.py b/assets/maps/map_converter.py deleted file mode 100644 index 94035a9..0000000 --- a/assets/maps/map_converter.py +++ /dev/null @@ -1,79 +0,0 @@ -import fxconv -from fxconv import u16, u32, ref, chars, string -import json - -def convert(input, output, params, target): - if params["custom-type"] == "map": - convert_map(input, output, params, target) - return 0 - else: - return 1 - -def convert_map(input, output, params, target): - with open(input, "r") as fp: - map = json.load(fp) - - TRANSFORM_COMPONENT = 0 - SPRITE_COMPONENT = 1 - ANIMATION_SYSTEM = 2 - - background_layer1 = b''.join(u16(tile) for tile in map["BackgroundLayer1"]) - background_layer2 = b''.join(u16(tile) for tile in map["BackgroundLayer2"]) - background_layer3 = b''.join(u16(tile) for tile in map["BackgroundLayer3"]) - foreground = b''.join(u16(tile) for tile in map["Foreground"]) - - entities = fxconv.Structure() - - for entity in map["Entities"]: - e = fxconv.Structure() - e += u16(entity["ID"]) - e += u32(len(entity["Components"])) - - components = fxconv.Structure() - - for component in entity["Components"]: - c = fxconv.Structure() - c_data = fxconv.Structure() - - match component["Type"]: - case "TransformComponent": - c += u32(TRANSFORM_COMPONENT) - c_data += u32(component["x"]) - c_data += u32(component["y"]) - c_data += u32(component["w"]) - c_data += u32(component["h"]) - c_data += u32(component["Speed"]) - - case "SpriteComponent": - c += u32(SPRITE_COMPONENT) - c_data += string(component["TextureName"]) - - case "AnimationSystem": - c += u32(SPRITE_COMPONENT) - c_data += u32(component["NbFrames"]) - c_data += u32(component["ActualFrame"]) - c_data += u32(component["FrameDelayMs"]) - c_data += u32(component["Play"]) - c_data += u32(component["Loop"]) - c_data += u32(component["Reverse"]) - - case _: - raise fxconv.FxconvError(f"unknown component type {component['Type']}") - - c += ref(c_data) - components += c - - e += ref(components) - entities += e - - o = fxconv.ObjectData() - o += string(params["name"]) - o += u32(map["MapWidth"]) + u32(map["MapHeight"]) - o += ref(background_layer1) - o += ref(background_layer2) - o += ref(background_layer3) - o += ref(foreground) - o += u32(len(map["Entities"])) - o += ref(entities) - - fxconv.elf(o, output, params["name"], **target) diff --git a/src/game_data/ecs/animation_system.hpp b/src/game_data/ecs/animation_system.hpp index 5a8d7d9..70ec833 100644 --- a/src/game_data/ecs/animation_system.hpp +++ b/src/game_data/ecs/animation_system.hpp @@ -4,13 +4,13 @@ #include typedef struct animation_system_data_t { - bool play :1; - bool loop :1; - bool reverse :1; - - float frame_delay_ms; - uint32_t actual_frame_nb; uint32_t nb_frames; + uint32_t actual_frame_nb; + uint32_t frame_delay_ms; + + bool play; + bool loop; + bool reverse; } animation_system_data_t; #endif // SPRITE_COMPONENT_H \ No newline at end of file diff --git a/src/game_data/maps.cpp b/src/game_data/maps.cpp index a03ee0f..442bb1d 100644 --- a/src/game_data/maps.cpp +++ b/src/game_data/maps.cpp @@ -69,6 +69,7 @@ void verify_maps_integrity(void) { const uint32_t NB_COMPONENTS = ENTITIES[entity_index].nb_component; const component_t *COMPONENTS = ENTITIES[entity_index].components; + std::cout << " Entity " << entity_index + 1 << std::endl; std::cout << " - ID : " << ENTITIES[entity_index].id << std::endl; std::cout << " - Nb Components : " << NB_COMPONENTS << std::endl; @@ -105,12 +106,12 @@ void verify_maps_integrity(void) { const animation_system_data_t *system_data = COMPONENT->component_data.animation_system_data; std::cout << " - AnimationSystem :" << std::endl; - std::cout << " - NbFrames : " << system_data->nb_frames; - std::cout << " - FrameNb : " << system_data->actual_frame_nb; - std::cout << " - FrameDelay : " << system_data->frame_delay_ms; - std::cout << " - Play : " << system_data->play; - std::cout << " - Loop : " << system_data->loop; - std::cout << " - Reverse : " << system_data->reverse; + std::cout << " - NbFrames : " << system_data->nb_frames << std::endl; + std::cout << " - FrameNb : " << system_data->actual_frame_nb << std::endl; + std::cout << " - FrameDelay : " << system_data->frame_delay_ms << std::endl; + std::cout << " - Play : " << system_data->play << std::endl; + std::cout << " - Loop : " << system_data->loop << std::endl; + std::cout << " - Reverse : " << system_data->reverse << std::endl; break; } }