Compare commits

..

No commits in common. "b8a542f60101c4dfece56ab8ade2fa813ee06fce" and "071c68811f1381da52dc64f673c21872d7769cd7" have entirely different histories.

97 changed files with 1025 additions and 688 deletions

View File

@ -13,8 +13,8 @@
"/usr/include/spa-0.2",
"/usr/include/libdrm",
"/usr/include/libdecor-0",
"/usr/include/x86_64-linux-gnu",
"${env:HOME}/.local/share/fxsdk/sysroot/sh3eb-elf/include"
"/usr/include/x86_64-linux-gnu"//,
// "${env:HOME}/.local/share/fxsdk/sysroot/sh3eb-elf/include"
],
"compilerPath": "/usr/bin/gcc",
"defines": [

View File

@ -89,7 +89,7 @@ clean-all:
$(Q)rm -rf $(dir $(BUILD_DIR))
run: all
$(Q)$(RUN_COMMAND)
$(Q)cd $(BUILD_DIR) && ./$(notdir $(OUTPUT))
print-var-% :
@echo $($*)

View File

@ -24,8 +24,6 @@
### In progress
- [ ] Add support for fxcg50
### Done
- [x] Make functionnal hitboxes between entities and map

View File

@ -24,7 +24,7 @@ $(BUILD_DIR)/$(notdir $(ASSET_BUILDER_DIR))/%.o: $(ASSET_BUILDER_DIR)/%
$(Q)mkdir -p $(dir $@)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building C object $@$(RESET)"
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(HOST_CC) $(ASSET_BUILDER_CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@
-include $(ASSET_BUILDER_OBJECTS:.o=.d)

View File

@ -1,5 +1,5 @@
#ifndef _ERRORS_H
#define _ERRORS_H
#ifndef ERRORS_H
#define ERRORS_H
#include <stdio.h>
#include <errno.h>
@ -54,4 +54,4 @@
fputc('\n', stderr); \
} while (0)
#endif // _ERRORS_H
#endif // ERRORS_H

View File

@ -20,12 +20,7 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
// Check png autenticity.
uint8_t sig[8];
if(fread(sig, sizeof(sig[0]), ARRAY_SIZE(sig), input_file) != ARRAY_SIZE(sig))
{
error_printf("Failed to read output file : %d", errno);
goto failure;
}
if(file_read(sig, sizeof(*sig), sizeof(sig), input_file)) goto_failure;
if(!png_check_sig(sig, 8))
{
error_printf("Png file is not valid.");
@ -58,10 +53,10 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
size_t row_length = png_get_rowbytes(img, img_info);
// Write image size.
if(file_write(&target_host_16b(width), sizeof(width), 1, output_file)) goto_failure;
if(file_write(&width, sizeof(width), 1, output_file)) goto_failure;
*asset_size += sizeof(width);
if(file_write(&target_host_16b(height), sizeof(height), 1, output_file)) goto_failure;
if(file_write(&height, sizeof(height), 1, output_file)) goto_failure;
*asset_size += sizeof(height);
// Prepare storage.
@ -113,14 +108,12 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
{
dst_pixels[i * width + j] = 0x0001;
}
dst_pixels[i * width + j] = target_host_16b(dst_pixels[i * width + j]);
}
}
free(src_pixels);
if(file_write(dst_pixels, sizeof(dst_pixels[0]), width * height, output_file))
if(file_write(dst_pixels, sizeof(*dst_pixels), width * height, output_file))
{
free(dst_pixels);
goto_failure;

View File

@ -1,9 +1,9 @@
#ifndef _IMAGE_H
#define _IMAGE_H
#ifndef IMAGE_H
#define IMAGE_H
#include <stdio.h>
#include <stdint.h>
int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_size);
#endif // _IMAGE_H
#endif // IMAGE_H

View File

@ -54,9 +54,9 @@ int main(int argc, char *argv[])
{
uint32_t header_size = 0;
const uint32_t nb_input_object_files = argc - 2;
const int nb_input_object_files = argc - 2;
if(file_write(&target_host_32b(nb_input_object_files), sizeof(nb_input_object_files), 1, output_file)) goto_failure;
if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file)) goto_failure;
header_size += sizeof(nb_input_object_files);
FILE *input_object_files[nb_input_object_files];
@ -81,10 +81,8 @@ int main(int argc, char *argv[])
goto_failure;
}
if(fread(&input_object_files_sizes[i], sizeof(input_object_files_sizes[0]), 1, input_object_files[i]) != 1)
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i]))
{
error_printf("Failed to read output file : %d", errno);
for(; i >= 0; i--)
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
@ -114,7 +112,7 @@ int main(int argc, char *argv[])
input_object_file_ext[0] = '\00';
uint32_t input_object_file_base_name_length = strlen(input_object_file_name) - strlen(input_object_file_ext) + 1;
if(file_write(input_object_file_name, sizeof(input_object_file_name[0]), input_object_file_base_name_length, output_file))
if(file_write(input_object_file_name, sizeof(*input_object_file_name), input_object_file_base_name_length, output_file))
{
for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i]))
@ -123,7 +121,7 @@ int main(int argc, char *argv[])
goto_failure;
}
if(file_write(&target_host_32b(input_object_files_offsets[i]), sizeof(input_object_files_offsets[0]), 1, output_file))
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file))
{
for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i]))
@ -133,14 +131,12 @@ int main(int argc, char *argv[])
}
}
for(uint32_t i = 0; i < nb_input_object_files; i++)
for(int i = 0; i < nb_input_object_files; i++)
{
char buffer[input_object_files_sizes[i]];
if(fread(buffer, sizeof(buffer[0]), input_object_files_sizes[i], input_object_files[i]) != input_object_files_sizes[i])
if(file_read(buffer, sizeof(*buffer), input_object_files_sizes[i], input_object_files[i]))
{
error_printf("Failed to read input file : %d", errno);
for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
@ -148,10 +144,8 @@ int main(int argc, char *argv[])
goto_failure;
}
if(fwrite(buffer, sizeof(buffer[0]), input_object_files_sizes[i], output_file) != input_object_files_sizes[i])
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file))
{
error_printf("Failed to write output file : %d", errno);
for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);

View File

@ -66,10 +66,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj)) goto_failure;
uint16_t map_height = json_object_get_int(tmp_obj);
if(file_write(&target_host_16b(map_width), sizeof(map_width), 1, output_file)) goto_failure;
if(file_write(&map_width, sizeof(map_width), 1, output_file)) goto_failure;
*asset_size += sizeof(map_width);
if(file_write(&target_host_16b(map_height), sizeof(map_height), 1, output_file)) goto_failure;
if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure;
*asset_size += sizeof(map_height);
// BackgroundLayers
@ -91,10 +91,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++)
{
if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj)) goto_failure;
layer_data[j] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) });
layer_data[j] = json_object_get_int(tmp_obj);
}
if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data);
}
@ -105,10 +105,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
{
if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj)) goto_failure;
layer_data[i] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) });
layer_data[i] = json_object_get_int(tmp_obj);
}
if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data);
}
// Hitboxes
@ -124,7 +124,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
hitboxes_layer_data[i] = json_object_get_int(tmp_obj);
}
if(file_write(&hitboxes_layer_data, sizeof(hitboxes_layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
if(file_write(&hitboxes_layer_data, sizeof(*hitboxes_layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(hitboxes_layer_data);
}
@ -134,7 +134,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure;
uint16_t entities_nb = json_object_array_length(entities);
if(file_write(&target_host_16b(entities_nb), sizeof(entities_nb), 1, output_file)) goto_failure;
if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(entities_nb);
for(uint32_t i = 0; i < entities_nb; i++)
@ -145,7 +145,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
uint32_t entity_id = json_object_get_int(tmp_obj);
if(file_write(&target_host_32b(entity_id), sizeof(entity_id), 1, output_file)) goto_failure;
if(file_write(&entity_id, sizeof(entity_id), 1, output_file)) goto_failure;
*asset_size += sizeof(entity_id);
json_object *components;
@ -154,7 +154,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
uint16_t components_nb = json_object_array_length(components);
if(file_write(&target_host_16b(components_nb), sizeof(components_nb), 1, output_file)) goto_failure;
if(file_write(&components_nb, sizeof(components_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(components_nb);
for(uint32_t j = 0; j < components_nb; j++)
@ -164,14 +164,14 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure;
uint16_t component_type = -1;
uint16_t component_type;
#define COMPONENT(NAME, ...) \
if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \
component_type = NAME;
#include "components.def"
if(file_write(&target_host_16b(component_type), sizeof(component_type), 1, output_file)) goto_failure;
if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure;
*asset_size += sizeof(component_type);
@ -188,8 +188,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
{
case json_type_int:
{
int32_t value = json_object_get_int(attribute);
if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
uint32_t value = json_object_get_int(attribute);
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value);
break;
}
@ -197,7 +197,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_double:
{
float value = json_object_get_double(attribute);
if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value);
break;
}
@ -213,8 +213,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_string:
{
const char *str = json_object_get_string(attribute);
if(file_write(str, sizeof(str[0]), strlen(str) + 1, output_file)) goto_failure;
*asset_size += (strlen(str) + 1) * sizeof(str[0]);
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure;
*asset_size += (strlen(str) + 1) * sizeof(*str);
break;
}

View File

@ -1,9 +1,9 @@
#ifndef _MAP_H
#define _MAP_H
#ifndef MAP_H
#define MAP_H
#include <stdio.h>
#include <stdint.h>
int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_size);
#endif // _MAP_H
#endif // MAP_H

View File

@ -38,7 +38,11 @@ int build_object(const char *input_file_path, FILE *output_file)
return EXIT_FAILURE;
}
rewind(output_file);
if(fseek(output_file, 0, SEEK_SET))
{
error_printf("Failed to seek in output file : %d", errno);
return EXIT_FAILURE;
}
if(file_write(&asset_size, sizeof(asset_size), 1, output_file)) return_failure_int;

View File

@ -1,8 +1,8 @@
#ifndef _OBJECT_H
#define _OBJECT_H
#ifndef OBJECT_H
#define OBJECT_H
#include <stdio.h>
int build_object(const char *input_file_path, FILE *output_file);
#endif // _OBJECT_H
#endif // OBJECT_H

View File

@ -4,7 +4,18 @@
#include <stdlib.h>
#include "errors.h"
int file_write(const void *ptr, size_t size, size_t nb, FILE *file)
inline int file_read(void *ptr, size_t size, size_t nb, FILE *file)
{
if(fread(ptr, size, nb, file) != nb)
{
error_printf("Failed to read output file : %d", errno);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
inline int file_write(const void *ptr, size_t size, size_t nb, FILE *file)
{
if(fwrite(ptr, size, nb, file) != nb)
{

View File

@ -1,21 +1,10 @@
#ifndef _UTILS_H
#define _UTILS_H
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
int file_read(void *ptr, size_t size, size_t nb, FILE *file);
int file_write(const void *ptr, size_t size, size_t nb, FILE *file);
#define file_write_stack_array(A, F) file_write(A, sizeof(A[0]), sizeof(A) / sizeof(A[0]), F)
#ifdef BIGENDIAN
#include <endian.h>
#define target_host_16b(V) (uint16_t){ htobe16(*(uint16_t *)&V) }
#define target_host_32b(V) (uint32_t){ htobe32(*(uint32_t *)&V) }
#else
#define target_host_16b(V) (*(uint16_t *)&V)
#define target_host_32b(V) (*(uint32_t *)&V)
#endif
#endif // _UTILS_H
#endif // UTILS_H

View File

@ -26,7 +26,7 @@ $(BUILD_DIR)/$(notdir $(ASSETS_DIR))/%.o: $(ASSETS_DIR)/% $(ASSET_BUILDER_OUTPUT
$(Q)mkdir -p $(dir $@)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE) + 1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE) * 100 / $(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building ASSET object $@$(RESET)"
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(GREEN)Building ASSET object $@$(RESET)"
$(Q)$(ASSET_BUILDER_OUTPUT) $@ -c $<
$(ASSET_BUILDER_OUTPUT): FORCE

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

77
configure vendored
View File

@ -158,7 +158,6 @@ case "${TARGET_HOST}" in
TARGET_HOST_CC=gcc
TARGET_HOST_AR=ar
TARGET_HOST_PKG_CONFIG=pkg-config
TARGET_HOST_CFLAGS=(
"--std=c17" "-Os"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\""
@ -166,11 +165,9 @@ case "${TARGET_HOST}" in
TARGET_HOST_LDFLAGS=(
"-lm"
)
OUTPUT_EXT=""
DYNAMIC_LIB_OUTPUT_EXT=".so"
STATIC_LIB_OUTPUT_EXT=".a"
RUN_COMMAND="cd \$(BUILD_DIR) && ./\$(notdir \$(OUTPUT))"
;;
*)
@ -187,20 +184,18 @@ case "${TARGET_HOST}" in
echo "Configuring for target: ${TARGET_HOST}"
case "${HOST}" in
windows)
TARGET_HOST_CC=gcc
TARGET_HOST_AR=ar
TARGET_HOST_PKG_CONFIG=pkg-config
RUN_COMMAND="cd \$(BUILD_DIR) && ./\$(notdir \$(OUTPUT))"
;;
linux)
windows|linux)
TARGET_HOST_CC=x86_64-w64-mingw32-gcc
TARGET_HOST_AR=x86_64-w64-mingw32-gcc-ar
TARGET_HOST_PKG_CONFIG=x86_64-w64-mingw32-pkg-config
RUN_COMMAND="cd \$(BUILD_DIR) && wine \$(notdir \$(OUTPUT))"
TARGET_HOST_CFLAGS=(
"--std=c17" "-Os"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-D_GNU_SOURCE=1"
)
TARGET_HOST_LDFLAGS=()
OUTPUT_EXT=".exe"
DYNAMIC_LIB_OUTPUT_EXT=".dll"
STATIC_LIB_OUTPUT_EXT=".dll.a"
;;
*)
@ -208,17 +203,6 @@ case "${TARGET_HOST}" in
;;
esac
TARGET_HOST_CFLAGS=(
"--std=c17" "-Os"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\""
)
TARGET_HOST_LDFLAGS=()
OUTPUT_EXT=".exe"
STATIC_LIB_OUTPUT_EXT=".dll.a"
POST_BUILD_COMMAND="cp /usr/x86_64-w64-mingw32/bin/SDL3.dll \$(BUILD_DIR)"
TARGET_HOST_LIBS=(
"sdl3|3.3.4"
)
@ -231,35 +215,17 @@ case "${TARGET_HOST}" in
linux)
TARGET_HOST_CC=sh-elf-gcc
TARGET_HOST_AR=sh-elf-ar
TARGET_HOST_CFLAGS=(
"--std=c17" "-Os"
"-DFXCG50" "-DTARGET_FXCG50"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\""
"-mb"
"-m4-nofpu"
"-ffreestanding" "-nostdlib"
"-fstrict-volatile-bitfields"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-DFXCG50" "-DTARGET_FXCG50"
"-mb" "-m4-nofpu"
"-ffreestanding" "-nostdlib" "-fstrict-volatile-bitfields"
)
TARGET_HOST_LDFLAGS=(
"-Wl,--no-warn-rwx-segments"
"-T $(fxsdk path sysroot)/sh3eb-elf/lib/fxcg50.ld" "-lc"
"-lgint-cg" "-lgcc" "-lc"
"-lopenlibm"
"-flto"
)
TARGET_HOST_LDFLAGS="-T fxcg50.ld"
OUTPUT_EXT=""
DYNAMIC_LIB_OUTPUT_EXT=".so"
STATIC_LIB_OUTPUT_EXT=".a"
POST_BUILD_COMMAND="sh-elf-objcopy -O binary -R .bss -R .gint_bss \$(OUTPUT) \$(OUTPUT).bin && fxgxa --g3a -n 2D_Engine --icon-uns=\$(ROOT)/assets/icons/fxcg50-icon-uns.png --icon-sel=\$(ROOT)/assets/icons/fxcg50-icon-sel.png \$(OUTPUT).bin -o \$(OUTPUT).g3a"
RUN_COMMAND="fxlink -isw \$(OUTPUT).g3a \$(ASSET_OUTPUT)"
ASSET_BUILDER_CFLAGS+=(
"-DBIGENDIAN" "-D_DEFAULT_SOURCE"
)
;;
*)
exit_error_message "Cross compiling from ${HOST} for ${TARGET_HOST} is not supported yet."
;;
@ -337,6 +303,7 @@ tee "${CONFIG_FILE}" <<EOF
TARGET_HOST = ${TARGET_HOST}
OUTPUT_EXT = ${OUTPUT_EXT}
DYNAMIC_LIB_OUTPUT_EXT = ${DYNAMIC_LIB_OUTPUT_EXT}
STATIC_LIB_OUTPUT_EXT = ${STATIC_LIB_OUTPUT_EXT}
TARGET_HOST_CC = ${TARGET_HOST_CC}
@ -351,18 +318,6 @@ ASSET_BUILDER_CFLAGS = ${ASSET_BUILDER_CFLAGS[@]} $(${HOST_PKG_CONFIG} --cflags
ASSET_BUILDER_LDFLAGS = ${ASSET_BUILDER_LDFLAGS[@]} $(${HOST_PKG_CONFIG} --libs $(echo ${ASSET_BUILDER_LIBS[@]} | tr " " "\n" | cut -d "|" -f 1))
EOF
if [ -n "${POST_BUILD_COMMAND}" ]; then
tee -a "${CONFIG_FILE}" <<EOF
POST_BUILD_COMMAND = ${POST_BUILD_COMMAND}
EOF
fi
tee -a "${CONFIG_FILE}" <<EOF
RUN_COMMAND = ${RUN_COMMAND}
EOF
if [ "${HOST}" = "windows" ]; then
tee -a "${CONFIG_FILE}" <<EOF

View File

@ -23,14 +23,14 @@ endif
$(LIBCORE): $(OBJECTS)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE) + 1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE) * 100 / $(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(YELLOW)Linking $(notdir $(LIBCORE))$(RESET)"
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(YELLOW)Linking Static Lib $@$(RESET)"
$(Q)$(TARGET_HOST_AR) rcs $@ $(OBJECTS)
$(BUILD_DIR)/$(notdir $(CORE_DIR))/%.o: $(CORE_DIR)/%
$(Q)mkdir -p $(dir $@)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE) + 1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE) * 100 / $(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building C object $@$(RESET)"
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(TARGET_HOST_CC) $(TARGET_HOST_CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@
-include $(OBJECTS:.o=.d)

View File

@ -2,12 +2,10 @@
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "game.h"
#include "linked_list.h"
#include "texture.h"
#include "map.h"
#include "utils.h"
#include "memory_alloc.h"
#include "errors.h"
@ -18,7 +16,7 @@ int create_asset(asset_t *asset, va_list args)
asset->type = ASSET_NULL;
asset->nb_refs = 0;
return SUCCESS;
return EXIT_SUCCESS;
}
int destroy_asset(asset_t *asset)
@ -38,10 +36,10 @@ int destroy_asset(asset_t *asset)
default:
error_printf("Asset type not valid : %d", asset->type);
return FAILURE;
return EXIT_FAILURE;
}
return SUCCESS;
return EXIT_SUCCESS;
}
// No args
@ -49,14 +47,14 @@ int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args _
{
asset_header_def->name = NULL;
return SUCCESS;
return EXIT_SUCCESS;
}
int destroy_asset_header_def(asset_header_def_t *asset_header_def)
{
free(asset_header_def->name);
return SUCCESS;
return EXIT_SUCCESS;
}
int asset_manager_init(void)
@ -68,11 +66,15 @@ int asset_manager_init(void)
if(!game.asset_manager.asset_file)
{
error_printf("Failed to open asset file : %d", errno);
return FAILURE;
return EXIT_FAILURE;
}
uint32_t nb_assets;
if(file_read(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file)) return_failure_int;
if(fread(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return EXIT_FAILURE;
}
if(linked_list_reserve(&game.asset_manager.assets_header_defs, nb_assets)) return_failure_int;
@ -96,7 +98,7 @@ int asset_manager_init(void)
if(ch == EOF)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return FAILURE;
return EXIT_FAILURE;
}
asset_header_def->name[name_size - 1] = ch;
@ -106,14 +108,17 @@ int asset_manager_init(void)
asset_header_def->name[name_size] = '\0';
}
if(file_read(&asset_header_def->start, sizeof(asset_header_def->start), 1, game.asset_manager.asset_file)) return_failure_int;
if(fread(&asset_header_def->start, sizeof(asset_header_def->start), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return EXIT_FAILURE;
}
linked_list_push_back(&game.asset_manager.assets_header_defs, asset_header_def_elem);
}
debug_printf("Initialized asset manager.");
return SUCCESS;
return EXIT_SUCCESS;
}
int asset_manager_exit(void)
@ -126,12 +131,12 @@ int asset_manager_exit(void)
if(fclose(game.asset_manager.asset_file))
{
error_printf("Failed to close asset file : %d", errno);
return FAILURE;
return EXIT_FAILURE;
}
}
debug_printf("Exited asset manager.");
return SUCCESS;
return EXIT_SUCCESS;
}
// Only for this file
@ -145,7 +150,7 @@ inline int asset_manager_collect_garbage(void)
{
if(linked_list_remove_if(&game.asset_manager.assets, condition_is_asset_unused, NULL)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
// Only for this file
@ -232,7 +237,7 @@ int asset_manager_let_go_asset(char *asset_name)
if(!asset_elem)
{
error_printf("Asset doesn't exists : %s", asset_name);
return FAILURE;
return EXIT_FAILURE;
}
asset_t *asset = asset_elem->data;
@ -240,7 +245,7 @@ int asset_manager_let_go_asset(char *asset_name)
debug_printf("Asset \"%s\" has been let go, nb refs : %d", asset->name, asset->nb_refs);
return SUCCESS;
return EXIT_SUCCESS;
}
asset_t *asset_manager_get_asset(char *asset_name)

View File

@ -1,5 +1,5 @@
#ifndef _ASSET_MANAGER_H
#define _ASSET_MANAGER_H
#ifndef ASSET_MANAGER_H
#define ASSET_MANAGER_H
#include <stdio.h>
#include <stdint.h>
@ -46,4 +46,4 @@ asset_t *asset_manager_load_asset(char *asset_name, asset_type_t asset_type);
int asset_manager_let_go_asset(char *asset_name);
asset_t *asset_manager_get_asset(char *asset_name);
#endif // _ASSET_MANAGER_H
#endif // ASSET_MANAGER_H

View File

@ -1,5 +1,5 @@
#ifndef _CAMERA_H
#define _CAMERA_H
#ifndef CAMERA_H
#define CAMERA_H
#include "vector2d.h"
@ -13,4 +13,4 @@ void camera_init(void);
void camera_update(void);
#endif // _CAMERA_H
#endif // CAMERA_H

View File

@ -1,7 +1,6 @@
#include "ecs.h"
#include <stdarg.h>
#include <errno.h>
#include "game.h"
#include "components.h"
#include "memory_alloc.h"
@ -37,7 +36,7 @@ int create_component(component_t *component, va_list args)
if(!component->data) \
{ \
error_printf("Failed to allocate memory : %d", errno); \
return FAILURE; \
return EXIT_FAILURE; \
} \
component->deleter = PREFIX##_destroy; \
break;
@ -45,17 +44,17 @@ int create_component(component_t *component, va_list args)
default:
error_printf("Component type doesn't exists : %d", component->type);
return FAILURE;
return EXIT_FAILURE;
}
return SUCCESS;
return EXIT_SUCCESS;
}
inline int destroy_component(component_t *component)
{
if(component->deleter(component)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
int create_entity(entity_t *entity, va_list args)
@ -65,14 +64,14 @@ int create_entity(entity_t *entity, va_list args)
linked_list_init(&entity->components, sizeof(component_t), (allocator_t)create_component, (deleter_t)destroy_component);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int destroy_entity(entity_t *entity)
{
if(linked_list_exit(&entity->components)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
component_t *entity_new_component(entity_t *entity, component_type_t component_type)
@ -116,14 +115,14 @@ static inline int action_update_component(elem_t *elem, void *arg __attribute__(
if(component->update(component)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int update_entity(entity_t *entity)
{
if(linked_list_for_each(&entity->components, action_update_component, NULL)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
// No args
@ -134,14 +133,14 @@ static inline int action_draw_component(elem_t *elem, void *arg __attribute__((u
if(component->draw)
if(component->draw(component)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int draw_entity(entity_t *entity)
{
if(linked_list_for_each(&entity->components, action_draw_component, NULL)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline void entity_manager_init(void)
@ -173,7 +172,7 @@ inline int entity_manager_update(void)
{
if(linked_list_for_each(&game.entity_manager.entities, action_update_entity, NULL)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
// No args
@ -194,7 +193,7 @@ inline int entity_manager_draw(void)
if(linked_list_for_each(&game.entity_manager.entities, action_draw_entity, NULL)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
// Only for this file
@ -210,7 +209,7 @@ inline int entity_manager_remove_entity(size_t id)
debug_printf("Removed entity %lu from entity manager.", id);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int entity_manager_exit(void)
@ -219,5 +218,5 @@ inline int entity_manager_exit(void)
debug_printf("Exited entity manager.");
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _ECS_H
#define _ECS_H
#ifndef ECS_H
#define ECS_H
#include <stdarg.h>
#include <stddef.h>
@ -68,4 +68,4 @@ int entity_manager_update(void);
int entity_manager_draw(void);
int entity_manager_remove_entity(size_t id);
#endif // _ECS_H
#endif // ECS_H

View File

@ -1,10 +1,9 @@
#ifndef _ERRORS_H
#define _ERRORS_H
#ifndef ERRORS_H
#define ERRORS_H
#include <stdio.h>
#define SUCCESS 0
#define FAILURE 1
#include <errno.h>
#include <assert.h>
#ifndef DEBUG
#define DEBUG 0
@ -30,7 +29,7 @@
fputc('\n', stderr); \
} while (0)
#define return_failure_int \
#define return_failure_int \
do { \
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
return EXIT_FAILURE; \
@ -41,12 +40,12 @@
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
return NULL; \
} while (0);
#define goto_failure \
do { \
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
goto failure; \
} while (0);
#else
#define debug_printf(...)
@ -67,4 +66,4 @@
#define goto_failure goto failure;
#endif
#endif // _ERRORS_H
#endif // ERRORS_H

View File

@ -12,7 +12,7 @@ inline int create_subscription(subscription_t *subscription, va_list args)
subscription->callback = va_arg(args, callback_t);
subscription->data = va_arg(args, void *);
return SUCCESS;
return EXIT_SUCCESS;
}
// Arg : size_t id
@ -22,14 +22,14 @@ inline int create_topic(topic_t *topic, va_list args)
linked_list_init(&topic->subscriptions, sizeof(subscription_t), (allocator_t)create_subscription, NULL);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int destroy_topic(topic_t *topic)
{
if(linked_list_exit(&topic->subscriptions)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline void event_bus_init(void)
@ -44,7 +44,7 @@ inline int event_bus_exit(void)
if(linked_list_exit(&game.event_bus.topics)) return_failure_int;
debug_printf("Exited event bus.");
return SUCCESS;
return EXIT_SUCCESS;
}
// Arg : size_t id
@ -55,7 +55,7 @@ static inline bool condition_is_topic_id(elem_t *elem, void *arg)
int event_bus_new_topic(size_t id)
{
if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return SUCCESS;
if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return EXIT_SUCCESS;
elem_t *topic_elem = create_elem(&game.event_bus.topics, id);
if(!topic_elem) return_failure_int;
@ -64,7 +64,7 @@ int event_bus_new_topic(size_t id)
debug_printf("Created new topic with id : %lu", id);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int event_bus_remove_topic(size_t id)
@ -73,7 +73,7 @@ inline int event_bus_remove_topic(size_t id)
debug_printf("Removed topic with id : %lu", id);
return SUCCESS;
return EXIT_SUCCESS;
}
// Args : va_list args
@ -83,7 +83,7 @@ static int action_execute_callback(elem_t *elem, void *arg)
if(subscription->callback(arg, subscription->data)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
int event_bus_publish(size_t id, void *arg)
@ -97,7 +97,7 @@ int event_bus_publish(size_t id, void *arg)
debug_printf("Published event on topic with id : %lu", id);
return SUCCESS;
return EXIT_SUCCESS;
}
int event_bus_subscribe(size_t id, callback_t callback, void *data)
@ -106,7 +106,7 @@ int event_bus_subscribe(size_t id, callback_t callback, void *data)
if(!topic_elem)
{
error_printf("Topic with id : %lu doesn't exists.", id);
return FAILURE;
return EXIT_FAILURE;
}
topic_t *topic = topic_elem->data;
@ -118,7 +118,7 @@ int event_bus_subscribe(size_t id, callback_t callback, void *data)
debug_printf("Subscribed to topic with id : %lu", id);
return SUCCESS;
return EXIT_SUCCESS;
}
// Arg : callback_t callback
@ -136,5 +136,5 @@ int event_bus_unsubscribe(size_t id, callback_t callback)
if(linked_list_remove_if(&topic->subscriptions, condition_is_callback, &callback)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _EVENT_BUS_H
#define _EVENT_BUS_H
#ifndef EVENT_BUS_H
#define EVENT_BUS_H
#include <stdarg.h>
#include "linked_list.h"
@ -35,4 +35,4 @@ int event_bus_publish(size_t id, void *arg);
int event_bus_subscribe(size_t id, callback_t callback, void *data);
int event_bus_unsubscribe(size_t id, callback_t callback);
#endif // _EVENT_BUS_H
#endif // EVENT_BUS_H

View File

@ -1,5 +1,5 @@
#ifndef _GAME_H
#define _GAME_H
#ifndef GAME_H
#define GAME_H
#include <stdbool.h>
#include "display.h"
@ -42,4 +42,4 @@ void game_handle_event(void);
int game_update(void);
int game_render(void);
#endif // _GAME_H
#endif // GAME_H

View File

@ -1,7 +1,4 @@
SOURCES += $(addprefix host/$(TARGET_HOST)/, \
utils.c \
display.c \
texture.c \
vector2d.c \
inputs.c \
update_time.c \

View File

@ -1,44 +0,0 @@
#include "display.h"
#include <gint/display.h>
#include <stdlib.h>
#include "game.h"
#include "errors.h"
int vpos = 0;
inline int display_init(void)
{
return SUCCESS;
}
inline void display_exit(void)
{
#include <gint/keyboard.h>
dupdate();
getkey();
}
inline int display_clear(void)
{
dclear(C_WHITE);
return SUCCESS;
}
inline int display_update(void)
{
dupdate();
return SUCCESS;
}
inline int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip)
{
dsubimage_rgb16_effect((int)dst_rect->x, (int)dst_rect->y, texture, (int)src_rect->x, (int)src_rect->y, (int)src_rect->w, (int)src_rect->h, flip ? IMAGE_HFLIP : DIMAGE_NONE);
return SUCCESS;
}
inline int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
{
drect((int)frect->x, (int)frect->y, (int)(frect->x + frect->w), (int)(frect->y + frect->h), (r >> 3 ) | (g >> 2) | (g >> 3));
return SUCCESS;
}

View File

@ -1,20 +1,15 @@
#ifndef _DISPLAY_H
#define _DISPLAY_H
#ifndef DISPLAY_H
#define DISPLAY_H
#include <gint/display.h>
#include "texture.h"
#include "vector2d.h"
#define COLOR_BLACK 0, 0, 0
#define COLOR_RED 255, 0, 0
#define COLOR_GREEN 0, 255, 0
#define COLOR_BLUE 0, 0, 255
#define COLOR_WHITE 255, 255, 255
#define DISPLAY_WIDTH DWIDTH
#define DISPLAY_HEIGHT DHEIGHT
typedef int display_t;
typedef struct display_t {
} display_t;
int display_init(void);
void display_exit(void);
@ -25,4 +20,4 @@ int display_update(void);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip);
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
#endif // _DISPLAY_H
#endif // DISPLAY_H

View File

@ -1,80 +0,0 @@
#ifndef _ERRORS_H
#define _ERRORS_H
#include <gint/display.h>
#include <gint/keyboard.h>
#define SUCCESS 0
#define FAILURE 1
#ifndef DEBUG
#define DEBUG 0
#endif
extern int vpos;
#if DEBUG > 0
#define debug_printf(...) \
do { \
dprint_opt(0, vpos, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "INFO: " __VA_ARGS__); \
vpos = vpos % DHEIGHT + 10; \
dupdate(); \
} while (0)
#define warning_printf(...) \
do { \
dprint_opt(0, vpos, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "WARNING: " __VA_ARGS__); \
vpos = vpos % DHEIGHT + 10; \
dupdate(); \
} while (0)
#define error_printf(...) \
do { \
dprint_opt(0, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "ERROR: " __VA_ARGS__); \
vpos = vpos % DHEIGHT + 10; \
dupdate(); \
getkey(); \
} while (0)
#define return_failure_int \
do { \
dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \
vpos = vpos % DHEIGHT + 10; \
return EXIT_FAILURE; \
} while (0);
#define return_failure_ptr \
do { \
dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \
vpos = vpos % DHEIGHT + 10; \
return NULL; \
} while (0);
#define goto_failure \
do { \
dprint_opt(-250, vpos, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "in %s:%d", __FILE__, __LINE__); \
vpos = vpos % DHEIGHT + 10; \
goto failure; \
} while (0);
#else
#define debug_printf(...)
#define warning_printf(...) \
do { \
dprint_opt(0, 0, C_BLACK, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "WARNING: " __VA_ARGS__); \
dupdate(); \
} while (0)
#define error_printf(...) \
do { \
dprint_opt(0, 10, C_RED, C_WHITE, DTEXT_LEFT, DTEXT_TOP, "ERROR: " __VA_ARGS__); \
dupdate(); \
getkey(); \
} while (0)
#define return_failure_int return FAILURE;
#define return_failure_ptr return NULL;
#define goto_failure goto failure;
#endif
#endif // _ERRORS_H

View File

@ -6,14 +6,11 @@ inline int poll_event(input_event_t *event)
{
key_event_t gint_event = pollevent();
if(gint_event.type == KEYEV_NONE)
if(gint_event.type = KEYEV_NONE)
return 0;
event->type = gint_event.type;
event->key = gint_event.key;
if(event->key == KEY_EXIT)
event->type = INPUT_EVENT_QUIT;
return 1;
}

View File

@ -1,5 +1,5 @@
#ifndef _INPUTS_H
#define _INPUTS_H
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "keycodes.h"
@ -14,4 +14,4 @@ typedef struct inputs_t {
bool keys[MAX_KEYCODES];
} inputs_t;
#endif // _INPUTS_H
#endif // KEYBOARD_H

View File

@ -1,5 +1,5 @@
#ifndef _KEYCODES_H
#define _KEYCODES_H
#ifndef KEYCODES_H
#define KEYCODES_H
#include <gint/keyboard.h>
@ -12,4 +12,4 @@
#define INPUT_EVENT_KEY_DOWN KEYEV_DOWN
#define INPUT_EVENT_QUIT 0xFF
#endif // _KEYCODES_H
#endif // KEYCODES_H

View File

@ -1,6 +1,6 @@
#ifndef _MEMORY_ALLOC_H
#define _MEMORY_ALLOC_H
#ifndef MEMORY_ALLOC_H
#define MEMORY_ALLOC_H
#include <stdlib.h>
#endif // _MEMORY_ALLOC_H
#endif // MEMORY_ALLOC_H

View File

@ -1,41 +0,0 @@
#include "texture.h"
#include <gint/image.h>
#include "asset_manager.h"
#include "game.h"
#include "display.h"
#include "utils.h"
#include "errors.h"
texture_t *load_texture(void)
{
uint16_t width, height;
if(file_read(&width, sizeof(width), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(file_read(&height, sizeof(height), 1, game.asset_manager.asset_file)) return_failure_ptr;
texture_t *texture = image_alloc(width, height, IMAGE_RGB565A);
if(!texture)
{
error_printf("Failed to create texture.");
return NULL;
}
for(uint16_t line = 0; line < height; line++)
{
if(file_read(&((uint8_t *)texture->data)[line * texture->stride], sizeof(uint16_t), width, game.asset_manager.asset_file)) goto_failure;
}
return texture;
failure:
image_free(texture);
return NULL;
}
void destroy_texture(texture_t *texture)
{
image_free(texture);
}

View File

@ -1,5 +1,5 @@
#ifndef _TEXTURE_H
#define _TEXTURE_H
#ifndef TEXTURE_H
#define TEXTURE_H
#include <gint/display.h>
@ -7,10 +7,10 @@
typedef bopti_image_t texture_t;
#define texture_get_width(T) T->width
#define texture_get_height(T) T->height
#define get_width(T) T->width
#define get_height(T) T->height
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);
#endif // _TEXTURE_H
#endif // TEXTURE_H

View File

@ -1,31 +1,13 @@
#include "update_time.h"
#include <gint/timer.h>
#include <gint/clock.h>
#include <time.h>
#include "game.h"
#define ENGINE_TICK 25
static int callback_tick(volatile int *tick)
{
*tick = 1;
return TIMER_CONTINUE;
}
static volatile int tick = 1;
static int t;
inline void update_time(void)
{
if(t == 0)
{
t = timer_configure(TIMER_ANY, ENGINE_TICK*1000, GINT_CALL(callback_tick, &tick));
if(t >= 0) timer_start(t);
}
game.delta_time_ms = 25.0f;
while(!tick) sleep();
tick = 0;
static clock_t last_clock_state = 0;
clock_t start_clock = clock();
game.delta_time_ms = (float)(start_clock - last_clock_state) / (float)CLOCKS_PER_SEC;
if(game.delta_time_ms > 100.0f) game.delta_time_ms = 100.0f;
last_clock_state = start_clock;
}

View File

@ -1,6 +1,6 @@
#ifndef _UPDATE_TIME_H
#define _UPDATE_TIME_H
#ifndef TIME_H
#define TIME_H
void update_time(void);
#endif // _UPDATE_TIME_H
#endif // TIME_H

View File

@ -1,14 +0,0 @@
#include "utils.h"
#include "errors.h"
int file_read(void *ptr, size_t size, size_t nb, FILE *file)
{
if(fread(ptr, size, nb, file) != size * nb)
{
error_printf("Failed to read file : %d", ferror(file));
return FAILURE;
}
return SUCCESS;
}

View File

@ -1,8 +0,0 @@
#ifndef _UTILS_H
#define _UTILS_H
#include <stdio.h>
int file_read(void *ptr, size_t size, size_t nb, FILE *file);
#endif // _UTILS_H

View File

@ -1,5 +1,5 @@
#ifndef _VECTOR2D_H
#define _VECTOR2D_H
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <stdbool.h>
#include <math.h>
@ -160,4 +160,4 @@ float lerp(float a, float b, float s);
*/
float lerp_time(float a, float b, float s, float t);
#endif // _VECTOR2D_H
#endif // VECTOR2D_H

View File

@ -1,5 +1,4 @@
SOURCES += $(addprefix host/$(TARGET_HOST)/, \
utils.c \
display.c \
vector2d.c \
texture.c \

View File

@ -2,7 +2,6 @@
#include <SDL3/SDL.h>
#include <stdlib.h>
#include <errno.h>
#include "asset_manager.h"
#include "game.h"
#include "texture.h"
@ -15,20 +14,20 @@ inline int display_init(void)
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
{
error_printf("Failed to initialise SDL3 : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
if(!SDL_SetAppMetadata("2D_Engine_SDL3_Core", "1.0", "2D_Engine"))
{
error_printf("Failed to set app metadata : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
game.display.window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH, DISPLAY_HEIGHT, 0);
if(!game.display.window)
{
error_printf("Failed to create window : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
#ifdef DRIVERS
@ -47,19 +46,19 @@ inline int display_init(void)
if(!game.display.renderer)
{
error_printf("Failed to create renderer : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
if(!SDL_SetRenderVSync(game.display.renderer, 1))
{
error_printf("Failed to activate adaptative vsync : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
if(!SDL_SetRenderDrawBlendMode(game.display.renderer, SDL_BLENDMODE_BLEND))
{
error_printf("Failed to set renderer draw blend mode : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
/*
if(!SDL_SetDefaultTextureScaleMode(game.display.renderer, SDL_SCALEMODE_NEAREST))
@ -70,7 +69,7 @@ inline int display_init(void)
*/
debug_printf("Initialized display.");
return SUCCESS;
return EXIT_SUCCESS;
}
inline void display_exit(void)
@ -84,19 +83,19 @@ inline void display_exit(void)
inline int display_clear(void)
{
if(!SDL_SetRenderDrawColor(game.display.renderer, COLOR_WHITE, 255))
if(!SDL_SetRenderDrawColor(game.display.renderer, C_WHITE, 255))
{
error_printf("Failed to change renderer draw color : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
if(!SDL_RenderClear(game.display.renderer))
{
error_printf("Failed to clear renderer : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
return SUCCESS;
return EXIT_SUCCESS;
}
inline int display_update(void)
@ -104,7 +103,7 @@ inline int display_update(void)
if(!SDL_RenderPresent(game.display.renderer))
{
error_printf("Failed to present renderer : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
#if DEBUG >= 1
@ -125,12 +124,12 @@ inline int display_update(void)
if(sprintf(title, "2D_Engine | cur: %.2ffps | max: %.2ffps | low: %.2ffps", fps, max_fps_10s, low_fps_10s) == -1)
{
error_printf("Sprintf failed : %d", errno);
return FAILURE;
return EXIT_FAILURE;
}
if(!SDL_SetWindowTitle(game.display.window, title))
{
error_printf("Failed to set window title : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
}
@ -145,7 +144,7 @@ inline int display_update(void)
}
#endif // DEBUG >= 1
return SUCCESS;
return EXIT_SUCCESS;
}
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip)
@ -165,10 +164,10 @@ int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_
if(!SDL_RenderTextureRotated(game.display.renderer, texture, src_rect ? &src_frect : NULL, dst_rect ? &dst_frect : NULL, 0, NULL, flip))
{
error_printf("Failed to copy texture to renderer : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
return SUCCESS;
return EXIT_SUCCESS;
}
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
@ -176,7 +175,7 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns
if(!SDL_SetRenderDrawColor(game.display.renderer, r, g, b, a))
{
error_printf("Failed to change renderer draw color : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
frect_t display_frect = {
@ -189,8 +188,8 @@ int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, uns
if(!SDL_RenderRect(game.display.renderer, &display_frect))
{
error_printf("Failed to draw rectangle to renderer : %s", SDL_GetError());
return FAILURE;
return EXIT_FAILURE;
}
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,15 +1,15 @@
#ifndef _DISPLAY_H
#define _DISPLAY_H
#ifndef DISPLAY_H
#define DISPLAY_H
#include <SDL3/SDL_render.h>
#include "texture.h"
#include "vector2d.h"
#define COLOR_BLACK 0, 0, 0
#define COLOR_RED 255, 0, 0
#define COLOR_GREEN 0, 255, 0
#define COLOR_BLUE 0, 0, 255
#define COLOR_WHITE 255, 255, 255
#define C_BLACK 0, 0, 0
#define C_RED 255, 0, 0
#define C_GREEN 0, 255, 0
#define C_BLUE 0, 0, 255
#define C_WHITE 255, 255, 255
#define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480
@ -28,4 +28,4 @@ int display_update(void);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip);
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
#endif // _DISPLAY_H
#endif // DISPLAY_H

View File

@ -1,5 +1,5 @@
#ifndef _INPUTS_H
#define _INPUTS_H
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "keycodes.h"
@ -14,4 +14,4 @@ typedef struct inputs_t {
bool keys[MAX_KEYCODES];
} inputs_t;
#endif // _INPUTS_H
#endif // KEYBOARD_H

View File

@ -1,5 +1,5 @@
#ifndef _KEYCODES_H
#define _KEYCODES_H
#ifndef KEYCODES_H
#define KEYCODES_H
#include <SDL3/SDL_events.h>
@ -16,4 +16,4 @@
#define INPUT_EVENT_KEY_DOWN SDL_EVENT_KEY_DOWN
#define INPUT_EVENT_QUIT SDL_EVENT_QUIT
#endif // _KEYCODES_H
#endif // KEYCODES_H

View File

@ -1,6 +1,6 @@
#ifndef _MEMORY_ALLOC_H
#define _MEMORY_ALLOC_H
#ifndef MEMORY_ALLOC_H
#define MEMORY_ALLOC_H
#include <stdlib.h>
#endif // _MEMORY_ALLOC_H
#endif // MEMORY_ALLOC_H

View File

@ -3,16 +3,23 @@
#include "asset_manager.h"
#include "game.h"
#include "display.h"
#include "utils.h"
#include "errors.h"
texture_t *load_texture(void)
{
uint16_t width, height;
if(file_read(&width, sizeof(width), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(file_read(&height, sizeof(height), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
if(!tmp_surface)
@ -27,7 +34,11 @@ texture_t *load_texture(void)
goto failure;
}
if(file_read(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file)) goto_failure;
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
goto failure;
}
if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR))
{
@ -52,9 +63,8 @@ texture_t *load_texture(void)
return texture;
failure:
failure:
SDL_DestroySurface(tmp_surface);
return NULL;
}

View File

@ -1,5 +1,5 @@
#ifndef _TEXTURE_H
#define _TEXTURE_H
#ifndef TEXTURE_H
#define TEXTURE_H
#include <SDL3/SDL_render.h>
@ -13,4 +13,4 @@ typedef SDL_Texture texture_t;
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);
#endif // _TEXTURE_H
#endif // TEXTURE_H

View File

@ -5,9 +5,9 @@
inline void update_time(void)
{
static uint64_t last_clock = 0;
static uint64_t last_clock_state = 0;
uint64_t start_clock = SDL_GetTicksNS();
game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock));
game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock_state));
if(game.delta_time_ms > 100.0f) game.delta_time_ms = 100.0f;
last_clock = start_clock;
last_clock_state = start_clock;
}

View File

@ -1,6 +1,6 @@
#ifndef _UPDATE_TIME_H
#define _UPDATE_TIME_H
#ifndef TIME_H
#define TIME_H
void update_time(void);
#endif // _UPDATE_TIME_H
#endif // TIME_H

View File

@ -1,14 +0,0 @@
#include "utils.h"
#include "errors.h"
int file_read(void *ptr, size_t size, size_t nb, FILE *file)
{
if(fread(ptr, size, nb, file) != nb)
{
error_printf("Failed to read file : %d", ferror(file));
return FAILURE;
}
return SUCCESS;
}

View File

@ -1,8 +0,0 @@
#ifndef _UTILS_H
#define _UTILS_H
#include <stdio.h>
int file_read(void *ptr, size_t size, size_t nb, FILE *file);
#endif // _UTILS_H

View File

@ -1,5 +1,5 @@
#ifndef _VECTOR2D_H
#define _VECTOR2D_H
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <SDL3/SDL_rect.h>
#include <math.h>
@ -150,4 +150,4 @@ float lerp(float a, float b, float s);
*/
float lerp_time(float a, float b, float s, float t);
#endif // _VECTOR2D_H
#endif // VECTOR2D_H

View File

@ -1 +0,0 @@
linux

View File

@ -0,0 +1,7 @@
SOURCES += $(addprefix host/$(TARGET_HOST)/, \
display.c \
vector2d.c \
texture.c \
inputs.c \
update_time.c \
)

193
core/host/windows/display.c Normal file
View File

@ -0,0 +1,193 @@
#include "display.h"
#include <SDL3/SDL.h>
#include <stdlib.h>
#include "asset_manager.h"
#include "game.h"
#include "texture.h"
#include "errors.h"
inline int display_init(void)
{
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
{
error_printf("Failed to initialise SDL3 : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetAppMetadata("2D_Engine_SDL3_Core", "1.0", "2D_Engine"))
{
error_printf("Failed to set app metadata : %s", SDL_GetError());
return EXIT_FAILURE;
}
game.display.window = SDL_CreateWindow("2D_Engine", DISPLAY_WIDTH, DISPLAY_HEIGHT, 0);
if(!game.display.window)
{
error_printf("Failed to create window : %s", SDL_GetError());
return EXIT_FAILURE;
}
#ifdef DRIVERS
debug_printf("Listing available render drivers.");
const int NB_DRIVERS = SDL_GetNumRenderDrivers();
for(int i = 0; i < NB_DRIVERS; i++)
debug_printf("%d : %s", i, SDL_GetRenderDriver(i));
warning_printf("This build of 2D_Engine_C only support the following drivers : %s", DRIVERS);
game.display.renderer = SDL_CreateRenderer(game.display.window, DRIVERS);
#else
game.display.renderer = SDL_CreateRenderer(game.display.window, NULL);
#endif // DRIVERS
if(!game.display.renderer)
{
error_printf("Failed to create renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetRenderVSync(game.display.renderer, 1))
{
error_printf("Failed to activate adaptative vsync : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetRenderDrawBlendMode(game.display.renderer, SDL_BLENDMODE_BLEND))
{
error_printf("Failed to set renderer draw blend mode : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_SetDefaultTextureScaleMode(game.display.renderer, SDL_SCALEMODE_NEAREST))
{
error_printf("Failed to set default texture scale mode : %s", SDL_GetError());
return EXIT_FAILURE;
}
debug_printf("Initialized display.");
return EXIT_SUCCESS;
}
inline void display_exit(void)
{
if(game.display.renderer) SDL_DestroyRenderer(game.display.renderer);
if(game.display.window) SDL_DestroyWindow(game.display.window);
SDL_Quit();
debug_printf("Exited display.");
}
inline int display_clear(void)
{
if(!SDL_SetRenderDrawColor(game.display.renderer, C_WHITE, 255))
{
error_printf("Failed to change renderer draw color : %s", SDL_GetError());
return EXIT_FAILURE;
}
if(!SDL_RenderClear(game.display.renderer))
{
error_printf("Failed to clear renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
inline int display_update(void)
{
if(!SDL_RenderPresent(game.display.renderer))
{
error_printf("Failed to present renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
#if DEBUG >= 1
float fps = 1000.0f / game.delta_time_ms;
static float max_fps_10s = 0.0f;
static float low_fps_10s = 10000000.0f;
if(fps > max_fps_10s) max_fps_10s = fps;
if(fps < low_fps_10s) low_fps_10s = fps;
static float fps_update_delta_time = 0.0f;
fps_update_delta_time += game.delta_time_ms;
if(fps_update_delta_time >= 100.0f)
{
fps_update_delta_time = 0.0f;
char title[100];
if(sprintf(title, "2D_Engine | cur: %.2ffps | max: %.2ffps | low: %.2ffps", fps, max_fps_10s, low_fps_10s) == -1)
{
error_printf("Sprintf failed : %d", errno);
return EXIT_FAILURE;
}
if(!SDL_SetWindowTitle(game.display.window, title))
{
error_printf("Failed to set window title : %s", SDL_GetError());
return EXIT_FAILURE;
}
}
static float fps_max_low_update_delta_time = 0.0f;
fps_max_low_update_delta_time += game.delta_time_ms;
if(fps_max_low_update_delta_time > 10000.0f)
{
fps_max_low_update_delta_time = 0.0f;
max_fps_10s = 0.0f;
low_fps_10s = 10000000.0f;
}
#endif // DEBUG >= 1
return EXIT_SUCCESS;
}
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip)
{
frect_t src_frect;
if(src_rect) rect_to_frect(src_rect, &src_frect);
frect_t dst_frect;
if(dst_rect)
dst_frect = (frect_t) {
.x = (dst_rect->x - game.camera.view_rect.x) * (DISPLAY_WIDTH / game.camera.view_rect.w),
.y = (dst_rect->y - game.camera.view_rect.y) * (DISPLAY_HEIGHT / game.camera.view_rect.h),
.w = dst_rect->w * (DISPLAY_WIDTH / game.camera.view_rect.w),
.h = dst_rect->h * (DISPLAY_HEIGHT / game.camera.view_rect.h)
};
if(!SDL_RenderTextureRotated(game.display.renderer, texture, src_rect ? &src_frect : NULL, dst_rect ? &dst_frect : NULL, 0, NULL, flip))
{
error_printf("Failed to copy texture to renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
{
if(!SDL_SetRenderDrawColor(game.display.renderer, r, g, b, a))
{
error_printf("Failed to change renderer draw color : %s", SDL_GetError());
return EXIT_FAILURE;
}
frect_t display_frect = {
.x = (frect->x - game.camera.view_rect.x) * (DISPLAY_WIDTH / game.camera.view_rect.w),
.y = (frect->y - game.camera.view_rect.y) * (DISPLAY_HEIGHT / game.camera.view_rect.h),
.w = frect->w * (DISPLAY_WIDTH / game.camera.view_rect.w),
.h = frect->h * (DISPLAY_HEIGHT / game.camera.view_rect.h)
};
if(!SDL_RenderRect(game.display.renderer, &display_frect))
{
error_printf("Failed to draw rectangle to renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -0,0 +1,31 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include <SDL3/SDL_render.h>
#include "texture.h"
#include "vector2d.h"
#define C_BLACK 0, 0, 0
#define C_RED 255, 0, 0
#define C_GREEN 0, 255, 0
#define C_BLUE 0, 0, 255
#define C_WHITE 255, 255, 255
#define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480
typedef struct display_t {
SDL_Window *window;
SDL_Renderer *renderer;
} display_t;
int display_init(void);
void display_exit(void);
int display_clear(void);
int display_update(void);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip);
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
#endif // DISPLAY_H

View File

@ -0,0 +1,14 @@
#include "inputs.h"
#include <SDL3/SDL_events.h>
inline int poll_event(input_event_t *event)
{
SDL_Event sdl_event;
int ret = SDL_PollEvent(&sdl_event);
event->type = sdl_event.type;
event->key = sdl_event.key.scancode;
return ret;
}

View File

@ -0,0 +1,17 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include "keycodes.h"
typedef struct input_event_t {
uint32_t type;
int key;
} input_event_t;
int poll_event(input_event_t *event);
typedef struct inputs_t {
bool keys[MAX_KEYCODES];
} inputs_t;
#endif // KEYBOARD_H

View File

@ -0,0 +1,19 @@
#ifndef KEYCODES_H
#define KEYCODES_H
#include <SDL3/SDL_events.h>
#define MAX_KEYCODES SDL_SCANCODE_COUNT
#define KEY_UP SDL_SCANCODE_W
#define KEY_DOWN SDL_SCANCODE_S
#define KEY_LEFT SDL_SCANCODE_A
#define KEY_RIGHT SDL_SCANCODE_D
#define KEY_MOD SDL_SCANCODE_LCTRL
#define KEY_INTERACT SDL_SCANCODE_E
#define INPUT_EVENT_KEY_UP SDL_EVENT_KEY_UP
#define INPUT_EVENT_KEY_DOWN SDL_EVENT_KEY_DOWN
#define INPUT_EVENT_QUIT SDL_EVENT_QUIT
#endif // KEYCODES_H

View File

@ -0,0 +1,6 @@
#ifndef MEMORY_ALLOC_H
#define MEMORY_ALLOC_H
#include <stdlib.h>
#endif // MEMORY_ALLOC_H

View File

@ -0,0 +1,67 @@
#include "texture.h"
#include "asset_manager.h"
#include "game.h"
#include "display.h"
#include "errors.h"
texture_t *load_texture(void)
{
uint16_t width, height;
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
if(!tmp_surface)
{
error_printf("Failed to create temporary surface : %s", SDL_GetError());
return NULL;
}
if(!SDL_LockSurface(tmp_surface))
{
error_printf("Failed to lock temporary surface : %s", SDL_GetError());
goto_failure;
}
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
goto_failure;
}
if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR))
{
error_printf("Failed to set color key of temporary surface : %s", SDL_GetError());
goto_failure;
}
texture_t *texture = SDL_CreateTextureFromSurface(game.display.renderer, tmp_surface);
if(!texture)
{
error_printf("Failed to create texture from temporary surface : %s", SDL_GetError());
goto_failure;
}
SDL_DestroySurface(tmp_surface);
return texture;
failure:
SDL_DestroySurface(tmp_surface);
return NULL;
}
void destroy_texture(texture_t *texture)
{
SDL_DestroyTexture(texture);
}

View File

@ -0,0 +1,16 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#include <SDL3/SDL_render.h>
#define TEXTURE_TRANSPARENT_COLOR 0x0001
typedef SDL_Texture texture_t;
#define texture_get_width(T) T->w
#define texture_get_height(T) T->h
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);
#endif // TEXTURE_H

View File

@ -0,0 +1,13 @@
#include "update_time.h"
#include <SDL3/SDL.h>
#include "game.h"
inline void update_time(void)
{
static uint64_t last_clock_state = 0;
uint64_t start_clock = SDL_GetTicksNS();
game.delta_time_ms = SDL_NS_TO_MS((float)(start_clock - last_clock_state));
if(game.delta_time_ms > 100.0f) game.delta_time_ms = 100.0f;
last_clock_state = start_clock;
}

View File

@ -0,0 +1,6 @@
#ifndef TIME_H
#define TIME_H
void update_time(void);
#endif // TIME_H

View File

@ -0,0 +1,19 @@
#include "vector2d.h"
void frect_to_rect(const frect_t *frect, rect_t *rect)
{
rect->x = (int)frect->x;
rect->y = (int)frect->y;
rect->w = (int)frect->w;
rect->h = (int)frect->h;
}
inline float lerp(float a, float b, float s)
{
return a * (1 - s) + b * s;
}
inline float lerp_time(float a, float b, float s, float t)
{
return lerp(a, b, 1 - powf(1 - s, t * 60.0f));
}

View File

@ -0,0 +1,153 @@
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <SDL3/SDL_rect.h>
#include <math.h>
/**
* @brief Point struct. (int)
*
* @param x X pos
* @param y Y pos
*/
typedef SDL_Point vector2d_t;
/**
* @brief Point struct. (float)
*
* @param x X pos
* @param y Y pos
*/
typedef SDL_FPoint fvector2d_t;
/**
* @brief Rectangle struct. (int)
*
* @param x X pos
* @param y Y pos
* @param w Width
* @param h Height
*/
typedef SDL_Rect rect_t;
/**
* @brief Rectangle struct. (float)
*
* @param x X pos
* @param y Y pos
* @param w Width
* @param h Height
*/
typedef SDL_FRect frect_t;
#define EPSILON 0.0f
#define f_is_zero(X) (fabsf(X) <= EPSILON)
#define f_is_not_zero(X) (fabsf(X) > EPSILON)
#define rect_to_frect(R, FR) SDL_RectToFRect(R, FR)
void frect_to_rect(const frect_t *frect, rect_t *rect);
/**
* @brief Verify if a point is in a rectangle. (int)
*
* @param P Vector2d
* @param R Rectangle
*
* @return True if the point is in the rectangle, else false.
*/
#define point_in_rect(P, R) SDL_PointInRect(P, R)
/**
* @brief Verify if a point is in a rectangle. (float)
*
* @param P Vector2d
* @param R Rectangle
*
* @return True if the point is in the rectangle, else false.
*/
#define fpoint_in_frect(P, R) SDL_PointInRectF(P, R)
/**
* @brief Verify if a rectangle is empty
*
* @param R Rectangle
*
* @return True if the rectangle is empty, else false.
*/
#define rect_empty(R) SDL_RectEmpty(R)
/**
* @brief Verify if a rectangle is empty
*
* @param R Rectangle
*
* @return True if the rectangle is empty, else false.
*/
#define frect_empty(R) SDL_FRectEmpty(R)
/**
* @brief Verify if there is a intersction between two rectangles. (int)
*
* @param A Rectangle A
* @param B Rectangle B
*
* @return True if there is an intersection, else false.
*/
#define has_intersection(A, B) SDL_HasRectIntersection(A, B)
/**
* @brief Verify if there is a intersction between two rectangles. (float)
*
* @param A Rectangle A
* @param B Rectangle B
*
* @return True if there is an intersection, else false.
*/
#define fhas_intersection(A, B) SDL_HasRectIntersectionFloat(A, B)
/**
* @brief Verify if there is an intersection between two rectangles and get the intersection rectangle. (int)
*
* @param A Rectangle A
* @param B Rectangle B
* @param result The intersection rectangle
*
* @return True if there is an intersection, else false.
*/
#define intersect_rect(A, B, result) SDL_GetRectIntersection(A, B, result)
/**
* @brief Verify if there is an intersection between two rectangles and get the intersection rectangle. (float)
*
* @param A Rectangle A
* @param B Rectangle B
* @param result The intersection rectangle
*
* @return True if there is an intersection, else false.
*/
#define intersect_frect(A, B, result) SDL_GetRectIntersectionFloat(A, B, result)
/**
* @brief Lerp a to b with smoothing factor s.
*
* @param a Current value
* @param b Target value
* @param s Smoothing factor
*/
float lerp(float a, float b, float s);
/**
* @brief Lerp a to b with smoothing factor s and time basis t.
* Smoothing factor is for 60 fps call time.
*
* @param a Current value
* @param b Target value
* @param s Smoothing factor
* @param t Delta time
*/
float lerp_time(float a, float b, float s, float t);
#endif // VECTOR2D_H

View File

@ -1,9 +1,6 @@
#include "linked_list.h"
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include "memory_alloc.h"
#include "linked_list.h"
#include "errors.h"
elem_t *create_elem(linked_list_t *linked_list, ...)
@ -68,7 +65,7 @@ inline int destroy_elem(linked_list_t *linked_list, elem_t *elem)
free(elem);
}
return SUCCESS;
return EXIT_SUCCESS;
}
void linked_list_init(linked_list_t *linked_list, size_t data_size, const allocator_t allocator, const deleter_t elem_deleter)
@ -117,7 +114,7 @@ int linked_list_exit(linked_list_t *linked_list)
free(tmp);
}
return SUCCESS;
return EXIT_SUCCESS;
}
int linked_list_clear(linked_list_t *linked_list)
@ -141,7 +138,7 @@ int linked_list_clear(linked_list_t *linked_list)
linked_list->size = 0;
return SUCCESS;
return EXIT_SUCCESS;
}
int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size)
@ -156,7 +153,7 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size)
if(!elem)
{
error_printf("Failed to allocate memory : %d", errno);
return FAILURE;
return EXIT_FAILURE;
}
elem->data = malloc(linked_list->data_size);
@ -164,14 +161,14 @@ int linked_list_reserve(linked_list_t *linked_list, size_t target_max_size)
{
free(elem);
error_printf("Failed to allocate memory : %d", errno);
return FAILURE;
return EXIT_FAILURE;
}
elem->next = linked_list->reserved;
linked_list->reserved = elem;
}
return SUCCESS;
return EXIT_SUCCESS;
}
void linked_list_push_back(linked_list_t *linked_list, elem_t *elem)
@ -292,12 +289,12 @@ int linked_list_insert(linked_list_t *linked_list, elem_t *elem, size_t index) /
if(index == 0)
{
linked_list_push_front(linked_list, elem);
return SUCCESS;
return EXIT_SUCCESS;
}
else if(index == linked_list->size)
{
linked_list_push_back(linked_list, elem);
return SUCCESS;
return EXIT_SUCCESS;
}
elem_t *next_insert_elem = linked_list_get(linked_list, index);
@ -312,7 +309,7 @@ int linked_list_insert(linked_list_t *linked_list, elem_t *elem, size_t index) /
linked_list->size++;
return SUCCESS;
return EXIT_SUCCESS;
}
int linked_list_remove(linked_list_t *linked_list, elem_t *elem)
@ -331,7 +328,7 @@ int linked_list_remove(linked_list_t *linked_list, elem_t *elem)
linked_list->size--;
return SUCCESS;
return EXIT_SUCCESS;
}
elem_t *linked_list_get_if(const linked_list_t *linked_list, const condition_t condition, void *arg)
@ -380,7 +377,7 @@ int linked_list_remove_if(linked_list_t *linked_list, const condition_t conditio
actual_elem = next_elem;
}
return SUCCESS;
return EXIT_SUCCESS;
}
int linked_list_for_each(const linked_list_t *linked_list, const action_t action, void *arg)
@ -400,7 +397,7 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
current_elem = current_elem->next;
}
return SUCCESS;
return EXIT_SUCCESS;
}
static inline void swap_elems_data(elem_t *elem_a, elem_t *elem_b)

View File

@ -1,5 +1,5 @@
#ifndef _LINKED_LIST_H
#define _LINKED_LIST_H
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <stdbool.h>
#include <stdarg.h>
@ -280,4 +280,4 @@ int linked_list_for_each(const linked_list_t *linked_list, const action_t action
*/
void linked_list_insertion_sort(linked_list_t *linked_list, const condition_t condition, void *arg);
#endif // _LINKED_LIST_H
#endif // LINKED_LIST_H

View File

@ -6,7 +6,6 @@
#include "components.h"
#include "vector2d.h"
#include "display.h"
#include "utils.h"
#include "memory_alloc.h"
#include "errors.h"
@ -14,11 +13,23 @@ map_t *load_map(void)
{
map_t *map = malloc(sizeof(map_t));
if(file_read(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(file_read(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(file_read(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
linked_list_init(&map->backgroundlayers, map->width * map->height * sizeof(uint16_t), NULL, NULL);
if(linked_list_reserve(&map->backgroundlayers, map->backgroundlayernb)) return_failure_ptr;
@ -29,18 +40,34 @@ map_t *load_map(void)
if(!backgroundlayer_elem) return_failure_ptr;
uint16_t *backgroundlayer = backgroundlayer_elem->data;
if(file_read(backgroundlayer, sizeof(*backgroundlayer), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(backgroundlayer, sizeof(*backgroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
linked_list_push_back(&map->backgroundlayers, backgroundlayer_elem);
}
map->foregroundlayer = malloc(map->width * map->height * sizeof(*map->foregroundlayer));
if(file_read(map->foregroundlayer, sizeof(*map->foregroundlayer), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(map->foregroundlayer, sizeof(*map->foregroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
map->hitboxes = malloc(map->width * map->height * sizeof(*map->hitboxes));
if(file_read(map->hitboxes, sizeof(*map->hitboxes), map->width * map->height, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(map->hitboxes, sizeof(*map->hitboxes), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(file_read(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
map->entities_defs = calloc(map->entities_nb, sizeof(map_entity_def_t));
@ -48,9 +75,17 @@ map_t *load_map(void)
{
map_entity_def_t *entity_def = &map->entities_defs[i];
if(file_read(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(file_read(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
entity_def->components_defs = calloc(entity_def->components_nb, sizeof(map_entity_component_def_t));
@ -58,7 +93,11 @@ map_t *load_map(void)
{
map_entity_component_def_t *component_def = &entity_def->components_defs[j];
if(file_read(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
switch(component_def->type)
{
@ -68,7 +107,11 @@ map_t *load_map(void)
case HITBOX_COMPONENT:
data_size = sizeof(fvector2d_t) * 2;
component_def->attributes = malloc(data_size);
if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
case SPRITE_COMPONENT:
@ -101,20 +144,41 @@ map_t *load_map(void)
case ANIMATION_SYSTEM:
data_size = sizeof(int) * 2 + sizeof(float) + sizeof(bool) * 3;
component_def->attributes = malloc(data_size);
if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
case INTERACT_COMPONENT:
data_size = sizeof(fvector2d_t) * 2 + sizeof(int) * 3;
component_def->attributes = malloc(data_size);
if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
case DOOR_COMPONENT:
data_size = sizeof(int) + sizeof(bool);
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
case LEVER_COMPONENT:
data_size = sizeof(int) + sizeof(bool);
component_def->attributes = malloc(data_size);
if(file_read(component_def->attributes, data_size, 1, game.asset_manager.asset_file)) return_failure_ptr;
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
default:
@ -158,7 +222,7 @@ int destroy_map(map_t *map)
free(map);
return SUCCESS;
return EXIT_SUCCESS;
}
int map_load_entities(map_t *map)
@ -302,12 +366,12 @@ int map_load_entities(map_t *map)
default:
error_printf("Component type not supported for loading : %s (%d)", component_type_to_name(component_def->type), component_def->type);
return FAILURE;
return EXIT_FAILURE;
}
}
}
return SUCCESS;
return EXIT_SUCCESS;
}
int map_remove_entities(map_t *map)
@ -349,13 +413,13 @@ int map_remove_entities(map_t *map)
}
}
return SUCCESS;
return EXIT_SUCCESS;
}
static inline int draw_tile(uint16_t tile_x, uint16_t tile_y, uint16_t tile_id)
{
if(!tile_id) return SUCCESS;
if(!tile_id) return EXIT_SUCCESS;
rect_t src_frect = {
.x = tile_id * TILE_SIZE % texture_get_width(game.map->tileset),
@ -373,7 +437,7 @@ static inline int draw_tile(uint16_t tile_x, uint16_t tile_y, uint16_t tile_id)
if(display_draw_texture(game.map->tileset, &src_frect, &dst_frect, false)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
int map_display_background(void)
@ -406,7 +470,7 @@ int map_display_background(void)
}
}
return SUCCESS;
return EXIT_SUCCESS;
}
int map_display_foreground(void)
@ -433,7 +497,7 @@ int map_display_foreground(void)
if(draw_tile(tile_x, tile_y, game.map->foregroundlayer[tile_y * game.map->width + tile_x])) return_failure_int;
}
return SUCCESS;
return EXIT_SUCCESS;
}
#if DEBUG > 1
@ -467,7 +531,7 @@ int map_display_hitboxes(void)
.h = TILE_SIZE
};
if(display_draw_frect(&draw_frect, COLOR_RED, 128)) return_failure_int;
if(display_draw_frect(&draw_frect, C_RED, 128)) return_failure_int;
}
}

View File

@ -1,5 +1,5 @@
#ifndef _MAP_H
#define _MAP_H
#ifndef MAP_H
#define MAP_H
#include <stdint.h>
#include "linked_list.h"
@ -45,4 +45,4 @@ int map_display_foreground(void);
int map_display_hitboxes(void);
#endif // DEBUG > 1
#endif // _MAP_H
#endif // MAP_H

View File

@ -25,13 +25,12 @@ endif
$(OUTPUT): $(OBJECTS) $(LIBCORE)
@echo $(ECHO_OPTION) "[100%] $(YELLOW)Linking $(notdir $(OUTPUT))$(RESET)"
$(Q)$(TARGET_HOST_CC) $(TARGET_HOST_CFLAGS) $(OBJECTS) -o $@ -L$(BUILD_DIR) -lcore $(TARGET_HOST_LDFLAGS)
$(Q)$(POST_BUILD_COMMAND)
$(BUILD_DIR)/$(notdir $(SOURCE_DIR))/%.o: $(SOURCE_DIR)/%
$(Q)mkdir -p $(dir $@)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE) + 1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE) * 100 / $(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building C object $@$(RESET)"
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(TARGET_HOST_CC) $(TARGET_HOST_CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@
$(LIBCORE): FORCE

View File

@ -15,14 +15,14 @@ int animation_system_init(component_t *system)
if(!system_data->sprite_component)
{
error_printf("Animation system require a sprite component.");
return FAILURE;
return EXIT_FAILURE;
}
linked_list_init(&system_data->frames, sizeof(rect_t), NULL, NULL);
system_data->frame_timer_ms = 0.0f;
return SUCCESS;
return EXIT_SUCCESS;
}
int animation_system_update(component_t *system)
@ -77,7 +77,7 @@ int animation_system_update(component_t *system)
sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data;
}
return SUCCESS;
return EXIT_SUCCESS;
}
inline int animation_system_destroy(component_t *system)
@ -88,7 +88,7 @@ inline int animation_system_destroy(component_t *system)
free(system_data);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int animation_system_apply_changes(component_t *system)
@ -119,10 +119,10 @@ inline int animation_system_apply_changes(component_t *system)
if(!system_data->current_frame)
{
error_printf("Current frame number out of range.");
return FAILURE;
return EXIT_FAILURE;
}
sprite_component_data->src_rect = *(rect_t *)system_data->current_frame->data;
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _ANIMATION_SYSTEM_H
#define _ANIMATION_SYSTEM_H
#ifndef ANIMATION_SYSTEM_H
#define ANIMATION_SYSTEM_H
#include <stdbool.h>
#include "ecs.h"
@ -27,4 +27,4 @@ int animation_system_destroy(component_t *system);
int animation_system_apply_changes(component_t *system);
#endif // _SPRITE_COMPONENT_H
#endif // SPRITE_COMPONENT_H

View File

@ -1,5 +1,5 @@
#ifndef _COMPONENTS_H
#define _COMPONENTS_H
#ifndef COMPONENTS_H
#define COMPONENTS_H
#include "transform_component.h"
#include "sprite_component.h"
@ -13,4 +13,4 @@
//#include "lua_component.h"
// Add new components header file here
#endif // _COMPONENTS_H
#endif // COMPONENTS_H

View File

@ -17,19 +17,19 @@ inline int door_component_init(component_t *component)
if(!component_data->animation_system)
{
error_printf("Door component require an animation system.");
return FAILURE;
return EXIT_FAILURE;
}
component_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT);
if(!component_data->hitbox_component)
{
error_printf("Door component require an hitbox component.");
return FAILURE;
return EXIT_FAILURE;
}
component_data->last_state = false;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int door_component_update(component_t *component)
@ -48,7 +48,7 @@ inline int door_component_update(component_t *component)
component_data->last_state = component_data->state;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int door_component_destroy(component_t *component)
@ -57,7 +57,7 @@ inline int door_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}
// Event arg : size_t state, Subscription data : entity_t *entity
@ -68,7 +68,7 @@ static inline int door_component_callback(void *event_arg, void *subscription_da
component_data->state = *(size_t *)event_arg;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int door_component_subscribe(component_t *component)
@ -77,5 +77,5 @@ inline int door_component_subscribe(component_t *component)
if(event_bus_subscribe(component_data->topic_id, door_component_callback, component)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _DOOR_COMPONENT_H
#define _DOOR_COMPONENT_H
#ifndef DOOR_COMPONENT_H
#define DOOR_COMPONENT_H
#include "ecs.h"
@ -22,4 +22,4 @@ int door_component_destroy(component_t *component);
int door_component_subscribe(component_t *component);
#endif // _DOOR_COMPONENT_H
#endif // DOOR_COMPONENT_H

View File

@ -14,7 +14,7 @@ inline int hitbox_component_init(component_t *component)
if(!component_data->transform_component)
{
error_printf("Hitbox component require a transform component.");
return FAILURE;
return EXIT_FAILURE;
}
component_data->position = (fvector2d_t) {
@ -29,7 +29,7 @@ inline int hitbox_component_init(component_t *component)
component_data->activated = true;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int hitbox_component_update(component_t *component)
@ -42,7 +42,7 @@ inline int hitbox_component_update(component_t *component)
component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x;
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
return SUCCESS;
return EXIT_SUCCESS;
}
#if DEBUG >= 2
@ -53,9 +53,9 @@ inline int hitbox_component_draw(const component_t *component)
const hitbox_component_data_t *component_data = component->data;
if(component_data->activated)
if(display_draw_frect(&component_data->bounds, COLOR_RED, 128)) return_failure_int;
if(display_draw_frect(&component_data->bounds, C_RED, 128)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
#endif // DEBUG >= 2
@ -65,5 +65,5 @@ inline int hitbox_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _HITBOX_COMPONENT_H
#define _HITBOX_COMPONENT_H
#ifndef HITBOX_COMPONENT_H
#define HITBOX_COMPONENT_H
#include <stdbool.h>
#include "ecs.h"
@ -23,4 +23,4 @@ int hitbox_component_draw(const component_t *component);
#endif
int hitbox_component_destroy(component_t *component);
#endif // _HITBOX_COMPONENT_H
#endif // HITBOX_COMPONENT_H

View File

@ -15,7 +15,7 @@ inline int interact_component_init(component_t *component)
if(!component_data->transform_component)
{
error_printf("Hitbox component require a transform component.");
return FAILURE;
return EXIT_FAILURE;
}
component_data->position = (fvector2d_t) {
@ -30,7 +30,7 @@ inline int interact_component_init(component_t *component)
component_data->topic_id = 0;
component_data->activated = true;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int interact_component_update(component_t *component)
@ -43,7 +43,7 @@ inline int interact_component_update(component_t *component)
component_data->bounds.w = transform_component_data->bounds.w * component_data->scale.x;
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
return SUCCESS;
return EXIT_SUCCESS;
}
#if DEBUG >= 2
@ -53,7 +53,7 @@ inline int interact_component_draw(const component_t *component)
{
const interact_component_data_t *component_data = component->data;
if(display_draw_frect(&component_data->bounds, COLOR_GREEN, 128)) return_failure_int;
if(display_draw_frect(&component_data->bounds, C_GREEN, 128)) return_failure_int;
return EXIT_SUCCESS;
}
@ -65,7 +65,7 @@ inline int interact_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}
inline int interact_component_interact(component_t *component)
@ -80,10 +80,10 @@ inline int interact_component_interact(component_t *component)
default:
error_printf("Interaction type not valid : %d", component_data->type);
return FAILURE;
return EXIT_FAILURE;
}
if(event_bus_publish(component_data->topic_id, &component_data->state)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _INTERACT_COMPONENT_H
#define _INTERACT_COMPONENT_H
#ifndef INTERACT_COMPONENT_H
#define INTERACT_COMPONENT_H
#include "ecs.h"
#include "vector2d.h"
@ -32,4 +32,4 @@ int interact_component_destroy(component_t *component);
int interact_component_interact(component_t *component);
#endif // _TRANSFORM_COMPONENT_H
#endif // TRANSFORM_COMPONENT_H

View File

@ -17,12 +17,12 @@ inline int lever_component_init(component_t *component)
if(!component_data->animation_system)
{
error_printf("Lever component require an animation system.");
return FAILURE;
return EXIT_FAILURE;
}
component_data->last_state = false;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int lever_component_update(component_t *component)
@ -38,7 +38,7 @@ inline int lever_component_update(component_t *component)
component_data->last_state = component_data->state;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int lever_component_destroy(component_t *component)
@ -47,7 +47,7 @@ inline int lever_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}
// Event arg : size_t state, Subscription data : component_t *component
@ -58,7 +58,7 @@ static inline int lever_component_callback(void *event_arg, void *subscription_d
component_data->state = *(size_t *)event_arg;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int lever_component_subscribe(component_t *component)
@ -67,5 +67,5 @@ inline int lever_component_subscribe(component_t *component)
if(event_bus_subscribe(component_data->topic_id, lever_component_callback, component)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _LEVER_COMPONENT_H
#define _LEVER_COMPONENT_H
#ifndef LEVER_COMPONENT_H
#define LEVER_COMPONENT_H
#include "ecs.h"
#include "vector2d.h"
@ -21,4 +21,4 @@ int lever_component_destroy(component_t *component);
int lever_component_subscribe(component_t *component);
#endif // _LEVER_COMPONENT_H
#endif // LEVER_COMPONENT_H

View File

@ -18,7 +18,7 @@ inline int lua_component_init(component_t *component)
luaL_dostring(component_data->lua_state, "function f (a) print(a) end");
return SUCCESS;
return EXIT_SUCCESS;
}
inline int lua_component_update(component_t *component __attribute__((unused)))
@ -27,7 +27,7 @@ inline int lua_component_update(component_t *component __attribute__((unused)))
luaL_dostring(component_data->lua_state, "f(\"prout\")");
return SUCCESS;
return EXIT_SUCCESS;
}
#if DEBUG >= 2
@ -47,5 +47,5 @@ inline int lua_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _LUA_COMPONENT_H
#define _LUA_COMPONENT_H
#ifndef LUA_COMPONENT_H
#define LUA_COMPONENT_H
#include <lua.h>
#include "ecs.h"
@ -16,4 +16,4 @@ int lua_component_draw(const component_t *component);
#endif // DEBUG >= 2
int lua_component_destroy(component_t *component);
#endif // _LUA_COMPONENT_H
#endif // LUA_COMPONENT_H

View File

@ -16,14 +16,14 @@ inline int physics_system_init(component_t *component)
if(!system_data->transform_component)
{
error_printf("Physics system require a transform component.");
return FAILURE;
return EXIT_FAILURE;
}
system_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT);
if(!system_data->hitbox_component)
{
error_printf("Physics system require an hitbox component.");
return FAILURE;
return EXIT_FAILURE;
}
system_data->target_velocity = (fvector2d_t) {
@ -40,7 +40,7 @@ inline int physics_system_init(component_t *component)
system_data->smoothing_factor = 1.0f;
return SUCCESS;
return EXIT_SUCCESS;
}
// Args : component_t *component
@ -58,7 +58,7 @@ int physics_system_update(component_t *component)
if(hitbox_component_data->activated)
{
// Resolve entities collisions.
if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return FAILURE;
if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return EXIT_FAILURE;
// Resolve map collisions
if(game.map)
@ -136,7 +136,7 @@ int physics_system_update(component_t *component)
transform_component_data->bounds.x += system_data->velocity.x * (float)game.delta_time_ms / 1000.0f;
transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int physics_system_destroy(component_t *component)
@ -145,7 +145,7 @@ inline int physics_system_destroy(component_t *component)
free(system_data);
return SUCCESS;
return EXIT_SUCCESS;
}
static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg)
@ -160,15 +160,15 @@ static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg
if(target_entity->id == physics_system->entity->id)
{
return SUCCESS;
return EXIT_SUCCESS;
}
const component_t *target_hitbox_component = entity_get_component(target_entity, HITBOX_COMPONENT);
if(!target_hitbox_component) return SUCCESS;
if(!target_hitbox_component) return EXIT_SUCCESS;
const hitbox_component_data_t *target_hitbox_component_data = target_hitbox_component->data;
if(!target_hitbox_component_data->activated) return SUCCESS;
if(!target_hitbox_component_data->activated) return EXIT_SUCCESS;
const frect_t *hitbox_bounds = &hitbox_component_data->bounds;
const frect_t *target_hitbox_bounds = &target_hitbox_component_data->bounds;
@ -210,5 +210,5 @@ static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg
}
}
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _PHYSICS_SYSTEM_H
#define _PHYSICS_SYSTEM_H
#ifndef PHYSICS_SYSTEM_H
#define PHYSICS_SYSTEM_H
#include "ecs.h"
#include "vector2d.h"
@ -18,4 +18,4 @@ int physics_system_init(component_t *component);
int physics_system_update(component_t *component);
int physics_system_destroy(component_t *component);
#endif // _PHYSICS_SYSTEM_H
#endif // PHYSICS_SYSTEM_H

View File

@ -18,26 +18,26 @@ inline int player_system_init(component_t *component)
if(!system_data->sprite_component)
{
error_printf("Player system require a sprite component.");
return FAILURE;
return EXIT_FAILURE;
}
system_data->animation_system = entity_get_component(component->entity, ANIMATION_SYSTEM);
if(!system_data->animation_system)
{
error_printf("Player system require an animation system.");
return FAILURE;
return EXIT_FAILURE;
}
system_data->physics_system = entity_get_component(component->entity, PHYSICS_SYSTEM);
if(!system_data->physics_system)
{
error_printf("Player system require a physics system.");
return FAILURE;
return EXIT_FAILURE;
}
system_data->state = PLAYER_MOVING;
return SUCCESS;
return EXIT_SUCCESS;
}
static void player_system_get_inputs(player_system_data_t *system_data);
@ -54,7 +54,7 @@ inline int player_system_update(component_t *component)
if(player_system_set_animation_and_speed(system_data)) return_failure_int;
if(player_system_process_interaction(system_data)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int player_system_destroy(component_t *component)
@ -63,7 +63,7 @@ inline int player_system_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}
static inline void player_system_get_inputs(player_system_data_t *system_data)
@ -169,7 +169,7 @@ static inline int player_system_set_animation_and_speed(player_system_data_t *sy
if(animation_system_apply_changes(system_data->animation_system)) return_failure_int;
}
return SUCCESS;
return EXIT_SUCCESS;
}
// Args : player_system_data_t *system_data
@ -181,13 +181,13 @@ static inline int action_detect_activate_interact(elem_t *elem, void *arg)
physics_system_data_t *physics_system_data = system_data->physics_system->data;
component_t *target_interact_component = entity_get_component(target_entity, INTERACT_COMPONENT);
if(!target_interact_component) return SUCCESS;
if(!target_interact_component) return EXIT_SUCCESS;
interact_component_data_t *target_interact_component_data = target_interact_component->data;
if(!target_interact_component_data->activated)
{
return SUCCESS;
return EXIT_SUCCESS;
}
const frect_t *hitbox_bounds = &((hitbox_component_data_t *)physics_system_data->hitbox_component->data)->bounds;
@ -198,7 +198,7 @@ static inline int action_detect_activate_interact(elem_t *elem, void *arg)
if(interact_component_interact(target_interact_component)) return_failure_int;
}
return SUCCESS;
return EXIT_SUCCESS;
}
static inline int player_system_process_interaction(player_system_data_t *system_data)
@ -212,5 +212,5 @@ static inline int player_system_process_interaction(player_system_data_t *system
last_state = game.inputs.keys[KEY_INTERACT];
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _PLAYER_SYSTEM_H
#define _PLAYER_SYSTEM_H
#ifndef PLAYER_SYSTEM_H
#define PLAYER_SYSTEM_H
#include "ecs.h"
@ -33,4 +33,4 @@ int player_system_init(component_t *component);
int player_system_update(component_t *component);
int player_system_destroy(component_t *component);
#endif // _PLAYER_SYSTEM_H
#endif // PLAYER_SYSTEM_H

View File

@ -15,7 +15,7 @@ inline int sprite_component_init(component_t *component)
if(!component_data->transform_component)
{
error_printf("Sprite component require a transform component.");
return FAILURE;
return EXIT_FAILURE;
}
transform_component_data_t *transform_component_data = component_data->transform_component->data;
@ -26,7 +26,7 @@ inline int sprite_component_init(component_t *component)
component_data->flip = false;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int sprite_component_update(component_t *component)
@ -37,7 +37,7 @@ inline int sprite_component_update(component_t *component)
frect_to_rect(&transform_component_data->bounds, &component_data->dst_rect);
component->entity->draw_priority = transform_component_data->bounds.y + transform_component_data->bounds.h;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int sprite_component_draw(const component_t *component)
@ -46,7 +46,7 @@ inline int sprite_component_draw(const component_t *component)
if(display_draw_texture(component_data->texture, &component_data->src_rect, &component_data->dst_rect, component_data->flip)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
inline int sprite_component_destroy(component_t *component)
@ -55,5 +55,5 @@ inline int sprite_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _SPRITE_COMPONENT_H
#define _SPRITE_COMPONENT_H
#ifndef SPRITE_COMPONENT_H
#define SPRITE_COMPONENT_H
#include "ecs.h"
#include "asset_manager.h"
@ -20,4 +20,4 @@ int sprite_component_update(component_t *component);
int sprite_component_draw(const component_t *component);
int sprite_component_destroy(component_t *component);
#endif // _SPRITE_COMPONENT_H
#endif // SPRITE_COMPONENT_H

View File

@ -16,12 +16,12 @@ inline int transform_component_init(component_t *component)
.h = 32.0f
};
return SUCCESS;
return EXIT_SUCCESS;
}
inline int transform_component_update(component_t *component __attribute__((unused)))
{
return SUCCESS;
return EXIT_SUCCESS;
}
#if DEBUG >= 2
@ -31,9 +31,9 @@ inline int transform_component_draw(const component_t *component)
{
const transform_component_data_t *component_data = component->data;
if(display_draw_frect(&component_data->bounds, COLOR_BLUE, 128)) return_failure_int;
if(display_draw_frect(&component_data->bounds, C_BLUE, 128)) return_failure_int;
return SUCCESS;
return EXIT_SUCCESS;
}
#endif // DEBUG >= 2
@ -43,5 +43,5 @@ inline int transform_component_destroy(component_t *component)
free(component_data);
return SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
#ifndef _TRANSFORM_COMPONENT_H
#define _TRANSFORM_COMPONENT_H
#ifndef TRANSFORM_COMPONENT_H
#define TRANSFORM_COMPONENT_H
#include "ecs.h"
#include "vector2d.h"
@ -16,4 +16,4 @@ int transform_component_draw(const component_t *component);
#endif // DEBUG >= 2
int transform_component_destroy(component_t *component);
#endif // _TRANSFORM_COMPONENT_H
#endif // TRANSFORM_COMPONENT_H

View File

@ -129,7 +129,7 @@ int game_init(void)
*/
game.is_running = true;
return SUCCESS;
return EXIT_SUCCESS;
}
void game_handle_event(void)
@ -158,7 +158,7 @@ int game_update(void)
camera_update();
return SUCCESS;
return EXIT_SUCCESS;
}
int game_render(void)
@ -200,7 +200,7 @@ int game_render(void)
return_failure_int;
}
return SUCCESS;
return EXIT_SUCCESS;
}
int game_exit(void)
@ -210,5 +210,5 @@ int game_exit(void)
if(event_bus_exit()) return_failure_int;
display_exit();
return SUCCESS;
return EXIT_SUCCESS;
}