Compare commits

...

16 Commits

Author SHA1 Message Date
Ulysse Cura cec0f40bcf Updated scenario possibilities. 2026-11-28 11:34:56 +01:00
Ulysse Cura 09b6276d7f Fixed typo in display.c for code coherence. 2026-11-28 11:34:21 +01:00
Ulysse Cura 50a4dbdafa Updated TODO.md. 2026-11-28 11:33:38 +01:00
Ulysse Cura 8dc98183a4 Added all the configure tasks for all platforms. 2026-11-28 11:33:28 +01:00
Ulysse Cura e5e358a33f Added display scale per platform and camera mode. 2026-11-28 11:32:06 +01:00
Ulysse Cura b5185d103c Fixed display function in fxcg50. 2026-11-28 11:31:29 +01:00
Ulysse Cura a753f697a1 Removed strict overflow warning for fxcg50 compilation. 2026-11-28 11:30:56 +01:00
Ulysse Cura 529f1de038 Made the run command launch mq instead of transfering the file. 2026-11-28 11:30:29 +01:00
Ulysse Cura 3fed5d5545 Fixed some return values. 2026-11-28 11:29:58 +01:00
Ulysse Cura b8a542f601 Rewind function instead of fseek. 2026-11-02 22:34:57 +01:00
Ulysse Cura 30dc3adf13 Renamed macros for compatibility with diferent standar libraries. 2026-11-02 22:34:40 +01:00
Ulysse Cura c458cb72ce Naïve target host endianness implementation in asset builder. 2026-11-02 22:29:46 +01:00
Ulysse Cura 6cc9e6a5bb Starting to add support for the fxcg50 platform. 2026-11-02 22:27:33 +01:00
Ulysse Cura ea5d21e306 Changed naming convention for unique file includes from FILE_NAME_H to _FILE_NAME_H. Fixed some typos for some of these macros. 2026-11-02 20:56:13 +01:00
Ulysse Cura 820bf6d902 Added spaces around brackets for make percentage and custom POST BUILD and RUN commands in makefiles. 2026-11-02 20:50:38 +01:00
Ulysse Cura 1c1a9ad5c1 Merged windows and Linux core parts because SDL3 abstracts enough. 2026-11-02 20:28:59 +01:00
100 changed files with 795 additions and 1037 deletions

View File

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

56
.vscode/tasks.json vendored
View File

@ -3,7 +3,7 @@
"tasks": [ "tasks": [
{ {
"label": "Configure", "label": "Configure",
"detail": "Configure project", "detail": "Configure project for current host",
"type": "shell", "type": "shell",
"command": "${workspaceFolder}/configure", "command": "${workspaceFolder}/configure",
"args": [ "args": [
@ -18,6 +18,60 @@
"clear": true "clear": true
} }
}, },
{
"label": "Configure for Linux",
"detail": "Configure project for Linux host",
"type": "shell",
"command": "${workspaceFolder}/configure",
"args": [
"--target-host=linux"
],
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{
"label": "Configure for Windows",
"detail": "Configure project for Windows host",
"type": "shell",
"command": "${workspaceFolder}/configure",
"args": [
"--target-host=windows"
],
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{
"label": "Configure for Casio Graph 90+",
"detail": "Configure project for Casio Graph 90+ host",
"type": "shell",
"command": "${workspaceFolder}/configure",
"args": [
"--target-host=fxcg50"
],
"group": "build",
"presentation": {
"echo": true,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": true
}
},
{ {
"label": "Build", "label": "Build",
"detail": "Build project", "detail": "Build project",

View File

@ -30,14 +30,14 @@ ASSET_OUTPUT = game.data
ifneq ($(DEBUG),) ifneq ($(DEBUG),)
TARGET_HOST_CFLAGS += -O1 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \ TARGET_HOST_CFLAGS += -O1 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \
-Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \ -Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \
-Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \ -Wcast-align -Wpointer-arith -Wbad-function-cast \
-Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \ -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \
-Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \ -Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \
-Wredundant-decls -Wold-style-definition -Wredundant-decls -Wold-style-definition
ASSET_BUILDER_CFLAGS += -O1 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \ ASSET_BUILDER_CFLAGS += -O1 -g -DDEBUG=$(DEBUG) -Wextra -Wall --pedantic -Wno-missing-braces \
-Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \ -Wno-missing-field-initializers -Wformat=2 -Wswitch-default -Wswitch \
-Wcast-align -Wpointer-arith -Wbad-function-cast -Wstrict-overflow=5 \ -Wcast-align -Wpointer-arith -Wbad-function-cast \
-Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \ -Wstrict-prototypes -Winline -Wundef -Wnested-externs -Wcast-qual \
-Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \ -Wshadow -Wunreachable-code -Wlogical-op -Wfloat-equal -Wstrict-aliasing=2 \
-Wredundant-decls -Wold-style-definition -Wredundant-decls -Wold-style-definition
@ -89,7 +89,7 @@ clean-all:
$(Q)rm -rf $(dir $(BUILD_DIR)) $(Q)rm -rf $(dir $(BUILD_DIR))
run: all run: all
$(Q)cd $(BUILD_DIR) && ./$(notdir $(OUTPUT)) $(Q)$(RUN_COMMAND)
print-var-% : print-var-% :
@echo $($*) @echo $($*)

View File

@ -19,3 +19,11 @@ Changer entre réalité et esprit, voir la construction d'un esprit critique,
l'enfant absorbe tout au début, questionne rien, et avance en se posant des questions, en cherchant des réponses, en faisant ses propres opinions, etc... l'enfant absorbe tout au début, questionne rien, et avance en se posant des questions, en cherchant des réponses, en faisant ses propres opinions, etc...
Que se soit un peu énigmes peut être, des choses à résoudre dans son esprit pour qu'il puisse s'en sortir dans la vraie vie, provoquant des scénarios différents, des réactions différentes face à ce qu'il peut vivre. Que se soit un peu énigmes peut être, des choses à résoudre dans son esprit pour qu'il puisse s'en sortir dans la vraie vie, provoquant des scénarios différents, des réactions différentes face à ce qu'il peut vivre.
Ce qu'il vit ne changerait pas mais uniquement ce qu'il en retient en quelque sorte. Ce qu'il vit ne changerait pas mais uniquement ce qu'il en retient en quelque sorte.
## Moyenne
- Routes:
- Destruction de la technologie
- Mort
- Ignorer
-

View File

@ -13,7 +13,10 @@
- [ ] Make camera use acceleration - [ ] Make camera use acceleration
- [ ] Create sub entity (parent - child)
- [ ] Create lua component - [ ] Create lua component
-[ ] Create engine lua bindings
###### Bugs ###### Bugs
@ -22,8 +25,12 @@
- [ ] Fix door component and other components (?) loading from map - [ ] Fix door component and other components (?) loading from map
- [ ] Fix map tileset texture loading
### In progress ### In progress
- [ ] Add support for fxcg50
### Done ### Done
- [x] Make functionnal hitboxes between entities and map - [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 $@) $(Q)mkdir -p $(dir $@)
$(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1)))) $(eval CURRENT_FILE := $(shell echo $$(($(CURRENT_FILE)+1))))
$(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_BUILD_FILES))))) $(eval PERCENTAGE := $(shell echo $$(($(CURRENT_FILE)*100/$(TOTAL_BUILD_FILES)))))
@echo $(ECHO_OPTION) "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)" @echo $(ECHO_OPTION) "[$(shell printf "%3d" $(PERCENTAGE))%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(HOST_CC) $(ASSET_BUILDER_CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@ $(Q)$(HOST_CC) $(ASSET_BUILDER_CFLAGS) $(INCLUDES:%=-I%) -MMD -MP -c $< -o $@
-include $(ASSET_BUILDER_OBJECTS:.o=.d) -include $(ASSET_BUILDER_OBJECTS:.o=.d)

View File

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

View File

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

View File

@ -1,9 +1,9 @@
#ifndef IMAGE_H #ifndef _IMAGE_H
#define IMAGE_H #define _IMAGE_H
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_size); 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; uint32_t header_size = 0;
const int nb_input_object_files = argc - 2; const uint32_t nb_input_object_files = argc - 2;
if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file)) goto_failure; if(file_write(&target_host_32b(nb_input_object_files), sizeof(nb_input_object_files), 1, output_file)) goto_failure;
header_size += sizeof(nb_input_object_files); header_size += sizeof(nb_input_object_files);
FILE *input_object_files[nb_input_object_files]; FILE *input_object_files[nb_input_object_files];
@ -81,8 +81,10 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i])) if(fread(&input_object_files_sizes[i], sizeof(input_object_files_sizes[0]), 1, input_object_files[i]) != 1)
{ {
error_printf("Failed to read output file : %d", errno);
for(; i >= 0; i--) for(; i >= 0; i--)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); error_printf("Failed to close input object file : %d", errno);
@ -112,7 +114,7 @@ int main(int argc, char *argv[])
input_object_file_ext[0] = '\00'; 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; 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), input_object_file_base_name_length, output_file)) if(file_write(input_object_file_name, sizeof(input_object_file_name[0]), input_object_file_base_name_length, output_file))
{ {
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
@ -121,7 +123,7 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file)) if(file_write(&target_host_32b(input_object_files_offsets[i]), sizeof(input_object_files_offsets[0]), 1, output_file))
{ {
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
@ -131,12 +133,14 @@ int main(int argc, char *argv[])
} }
} }
for(int i = 0; i < nb_input_object_files; i++) for(uint32_t i = 0; i < nb_input_object_files; i++)
{ {
char buffer[input_object_files_sizes[i]]; char buffer[input_object_files_sizes[i]];
if(file_read(buffer, sizeof(*buffer), input_object_files_sizes[i], input_object_files[i])) if(fread(buffer, sizeof(buffer[0]), input_object_files_sizes[i], input_object_files[i]) != input_object_files_sizes[i])
{ {
error_printf("Failed to read input file : %d", errno);
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); error_printf("Failed to close input object file : %d", errno);
@ -144,8 +148,10 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file)) if(fwrite(buffer, sizeof(buffer[0]), input_object_files_sizes[i], output_file) != input_object_files_sizes[i])
{ {
error_printf("Failed to write output file : %d", errno);
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); 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; 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); uint16_t map_height = json_object_get_int(tmp_obj);
if(file_write(&map_width, sizeof(map_width), 1, output_file)) goto_failure; if(file_write(&target_host_16b(map_width), sizeof(map_width), 1, output_file)) goto_failure;
*asset_size += sizeof(map_width); *asset_size += sizeof(map_width);
if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure; if(file_write(&target_host_16b(map_height), sizeof(map_height), 1, output_file)) goto_failure;
*asset_size += sizeof(map_height); *asset_size += sizeof(map_height);
// BackgroundLayers // 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++) 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; if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj)) goto_failure;
layer_data[j] = json_object_get_int(tmp_obj); layer_data[j] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) });
} }
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data); *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++) 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; if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj)) goto_failure;
layer_data[i] = json_object_get_int(tmp_obj); layer_data[i] = target_host_16b((uint16_t){ json_object_get_int(tmp_obj) });
} }
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data); *asset_size += sizeof(layer_data);
} }
// Hitboxes // 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); hitboxes_layer_data[i] = json_object_get_int(tmp_obj);
} }
if(file_write(&hitboxes_layer_data, sizeof(*hitboxes_layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure; if(file_write(&hitboxes_layer_data, sizeof(hitboxes_layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(hitboxes_layer_data); *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; if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure;
uint16_t entities_nb = json_object_array_length(entities); uint16_t entities_nb = json_object_array_length(entities);
if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file)) goto_failure; if(file_write(&target_host_16b(entities_nb), sizeof(entities_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(entities_nb); *asset_size += sizeof(entities_nb);
for(uint32_t i = 0; i < entities_nb; i++) 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); uint32_t entity_id = json_object_get_int(tmp_obj);
if(file_write(&entity_id, sizeof(entity_id), 1, output_file)) goto_failure; if(file_write(&target_host_32b(entity_id), sizeof(entity_id), 1, output_file)) goto_failure;
*asset_size += sizeof(entity_id); *asset_size += sizeof(entity_id);
json_object *components; 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); uint16_t components_nb = json_object_array_length(components);
if(file_write(&components_nb, sizeof(components_nb), 1, output_file)) goto_failure; if(file_write(&target_host_16b(components_nb), sizeof(components_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(components_nb); *asset_size += sizeof(components_nb);
for(uint32_t j = 0; j < components_nb; j++) 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; if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure;
uint16_t component_type; uint16_t component_type = -1;
#define COMPONENT(NAME, ...) \ #define COMPONENT(NAME, ...) \
if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \ if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \
component_type = NAME; component_type = NAME;
#include "components.def" #include "components.def"
if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure; if(file_write(&target_host_16b(component_type), sizeof(component_type), 1, output_file)) goto_failure;
*asset_size += sizeof(component_type); *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: case json_type_int:
{ {
uint32_t value = json_object_get_int(attribute); int32_t value = json_object_get_int(attribute);
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value); *asset_size += sizeof(value);
break; break;
} }
@ -197,7 +197,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_double: case json_type_double:
{ {
float value = json_object_get_double(attribute); float value = json_object_get_double(attribute);
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value); *asset_size += sizeof(value);
break; break;
} }
@ -213,8 +213,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_string: case json_type_string:
{ {
const char *str = json_object_get_string(attribute); const char *str = json_object_get_string(attribute);
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure; if(file_write(str, sizeof(str[0]), strlen(str) + 1, output_file)) goto_failure;
*asset_size += (strlen(str) + 1) * sizeof(*str); *asset_size += (strlen(str) + 1) * sizeof(str[0]);
break; break;
} }

View File

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

View File

@ -38,11 +38,7 @@ int build_object(const char *input_file_path, FILE *output_file)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if(fseek(output_file, 0, SEEK_SET)) rewind(output_file);
{
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; if(file_write(&asset_size, sizeof(asset_size), 1, output_file)) return_failure_int;

View File

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

View File

@ -4,18 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "errors.h" #include "errors.h"
inline 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)
{
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) if(fwrite(ptr, size, nb, file) != nb)
{ {

View File

@ -1,10 +1,21 @@
#ifndef UTILS_H #ifndef _UTILS_H
#define UTILS_H #define _UTILS_H
#include <stdio.h> #include <stdio.h>
int file_read(void *ptr, size_t size, size_t nb, FILE *file); #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
int file_write(const void *ptr, size_t size, size_t nb, FILE *file); int file_write(const void *ptr, size_t size, size_t nb, FILE *file);
#endif // UTILS_H #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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

78
configure vendored
View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
#ifndef ASSET_MANAGER_H #ifndef _ASSET_MANAGER_H
#define ASSET_MANAGER_H #define _ASSET_MANAGER_H
#include <stdio.h> #include <stdio.h>
#include <stdint.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); int asset_manager_let_go_asset(char *asset_name);
asset_t *asset_manager_get_asset(char *asset_name); asset_t *asset_manager_get_asset(char *asset_name);
#endif // ASSET_MANAGER_H #endif // _ASSET_MANAGER_H

View File

@ -8,8 +8,8 @@ void camera_init(void)
game.camera.view_rect = (rect_t) { game.camera.view_rect = (rect_t) {
.x = 0, .x = 0,
.y = 0, .y = 0,
.w = DISPLAY_WIDTH / 2, .w = DISPLAY_WIDTH / DISPLAY_SCALE,
.h = DISPLAY_HEIGHT / 2 .h = DISPLAY_HEIGHT / DISPLAY_SCALE
}; };
game.camera.tracking_pos = NULL; game.camera.tracking_pos = NULL;
@ -17,17 +17,19 @@ void camera_init(void)
.x = 0, .x = 0,
.y = 0 .y = 0
}; };
game.camera.mode = CAMERA_TRACKING;
} }
void camera_update(void) void camera_update(void)
{ {
if(game.camera.tracking_pos) if(game.camera.mode & CAMERA_TRACKING)
{ {
game.camera.view_rect.x = (int)game.camera.tracking_pos->x + game.camera.tracking_center_offset.x - game.camera.view_rect.w / 2; game.camera.view_rect.x = (int)game.camera.tracking_pos->x + game.camera.tracking_center_offset.x - game.camera.view_rect.w / 2;
game.camera.view_rect.y = (int)game.camera.tracking_pos->y + game.camera.tracking_center_offset.y - game.camera.view_rect.h / 2; game.camera.view_rect.y = (int)game.camera.tracking_pos->y + game.camera.tracking_center_offset.y - game.camera.view_rect.h / 2;
} }
if(game.map) if(game.camera.mode & CAMERA_BOUND_MAP)
{ {
if(game.camera.view_rect.w > game.map->width * TILE_SIZE) if(game.camera.view_rect.w > game.map->width * TILE_SIZE)
{ {

View File

@ -1,16 +1,23 @@
#ifndef CAMERA_H #ifndef _CAMERA_H
#define CAMERA_H #define _CAMERA_H
#include "vector2d.h" #include "vector2d.h"
typedef enum camera_mode_t {
CAMERA_STATIC = 0x0000,
CAMERA_TRACKING = 0x0001,
CAMERA_BOUND_MAP = 0x0010,
} camera_mode_t;
typedef struct camera_t { typedef struct camera_t {
rect_t view_rect; rect_t view_rect;
fvector2d_t *tracking_pos; fvector2d_t *tracking_pos;
vector2d_t tracking_center_offset; vector2d_t tracking_center_offset;
camera_mode_t mode;
} camera_t; } camera_t;
void camera_init(void); void camera_init(void);
void camera_update(void); void camera_update(void);
#endif // CAMERA_H #endif // _CAMERA_H

View File

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

View File

@ -1,5 +1,5 @@
#ifndef ECS_H #ifndef _ECS_H
#define ECS_H #define _ECS_H
#include <stdarg.h> #include <stdarg.h>
#include <stddef.h> #include <stddef.h>
@ -68,4 +68,4 @@ int entity_manager_update(void);
int entity_manager_draw(void); int entity_manager_draw(void);
int entity_manager_remove_entity(size_t id); int entity_manager_remove_entity(size_t id);
#endif // ECS_H #endif // _ECS_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->callback = va_arg(args, callback_t);
subscription->data = va_arg(args, void *); subscription->data = va_arg(args, void *);
return EXIT_SUCCESS; return SUCCESS;
} }
// Arg : size_t id // 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); linked_list_init(&topic->subscriptions, sizeof(subscription_t), (allocator_t)create_subscription, NULL);
return EXIT_SUCCESS; return SUCCESS;
} }
inline int destroy_topic(topic_t *topic) inline int destroy_topic(topic_t *topic)
{ {
if(linked_list_exit(&topic->subscriptions)) return_failure_int; if(linked_list_exit(&topic->subscriptions)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
inline void event_bus_init(void) 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; if(linked_list_exit(&game.event_bus.topics)) return_failure_int;
debug_printf("Exited event bus."); debug_printf("Exited event bus.");
return EXIT_SUCCESS; return SUCCESS;
} }
// Arg : size_t id // 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) int event_bus_new_topic(size_t id)
{ {
if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return EXIT_SUCCESS; if(linked_list_get_if(&game.event_bus.topics, condition_is_topic_id, &id)) return SUCCESS;
elem_t *topic_elem = create_elem(&game.event_bus.topics, id); elem_t *topic_elem = create_elem(&game.event_bus.topics, id);
if(!topic_elem) return_failure_int; 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); debug_printf("Created new topic with id : %lu", id);
return EXIT_SUCCESS; return SUCCESS;
} }
inline int event_bus_remove_topic(size_t id) 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); debug_printf("Removed topic with id : %lu", id);
return EXIT_SUCCESS; return SUCCESS;
} }
// Args : va_list args // 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; if(subscription->callback(arg, subscription->data)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
int event_bus_publish(size_t id, void *arg) 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); debug_printf("Published event on topic with id : %lu", id);
return EXIT_SUCCESS; return SUCCESS;
} }
int event_bus_subscribe(size_t id, callback_t callback, void *data) 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) if(!topic_elem)
{ {
error_printf("Topic with id : %lu doesn't exists.", id); error_printf("Topic with id : %lu doesn't exists.", id);
return EXIT_FAILURE; return FAILURE;
} }
topic_t *topic = topic_elem->data; 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); debug_printf("Subscribed to topic with id : %lu", id);
return EXIT_SUCCESS; return SUCCESS;
} }
// Arg : callback_t callback // 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; if(linked_list_remove_if(&topic->subscriptions, condition_is_callback, &callback)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef EVENT_BUS_H #ifndef _EVENT_BUS_H
#define EVENT_BUS_H #define _EVENT_BUS_H
#include <stdarg.h> #include <stdarg.h>
#include "linked_list.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_subscribe(size_t id, callback_t callback, void *data);
int event_bus_unsubscribe(size_t id, callback_t callback); 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 #ifndef _GAME_H
#define GAME_H #define _GAME_H
#include <stdbool.h> #include <stdbool.h>
#include "display.h" #include "display.h"
@ -42,4 +42,4 @@ void game_handle_event(void);
int game_update(void); int game_update(void);
int game_render(void); int game_render(void);
#endif // GAME_H #endif // _GAME_H

View File

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

View File

@ -0,0 +1,61 @@
#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)
{
rect_t display_rect;
if(dst_rect) display_rect = (rect_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)
};
dsubimage_rgb16_effect(dst_rect ? display_rect.x : 0, dst_rect ? display_rect.y : 0, 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)
{
rect_t display_rect = {
.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)
};
drect_border(display_rect.x, display_rect.y, (display_rect.x + display_rect.w), (display_rect.y + display_rect.h), C_NONE, 1, a ? C_RGB(r, g, b) : C_NONE);
return SUCCESS;
}

View File

@ -1,15 +1,21 @@
#ifndef DISPLAY_H #ifndef _DISPLAY_H
#define DISPLAY_H #define _DISPLAY_H
#include <gint/display.h> #include <gint/display.h>
#include "texture.h" #include "texture.h"
#include "vector2d.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_WIDTH DWIDTH
#define DISPLAY_HEIGHT DHEIGHT #define DISPLAY_HEIGHT DHEIGHT
#define DISPLAY_SCALE 1
typedef struct display_t { typedef int display_t;
} display_t;
int display_init(void); int display_init(void);
void display_exit(void); void display_exit(void);
@ -20,4 +26,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_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); 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

80
core/host/fxcg50/errors.h Normal file
View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

14
core/host/fxcg50/utils.c Normal file
View File

@ -0,0 +1,14 @@
#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;
}

8
core/host/fxcg50/utils.h Normal file
View File

@ -0,0 +1,8 @@
#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 #ifndef _VECTOR2D_H
#define VECTOR2D_H #define _VECTOR2D_H
#include <stdbool.h> #include <stdbool.h>
#include <math.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); float lerp_time(float a, float b, float s, float t);
#endif // VECTOR2D_H #endif // _VECTOR2D_H

View File

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

View File

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

View File

@ -1,18 +1,19 @@
#ifndef DISPLAY_H #ifndef _DISPLAY_H
#define DISPLAY_H #define _DISPLAY_H
#include <SDL3/SDL_render.h> #include <SDL3/SDL_render.h>
#include "texture.h" #include "texture.h"
#include "vector2d.h" #include "vector2d.h"
#define C_BLACK 0, 0, 0 #define COLOR_BLACK 0, 0, 0
#define C_RED 255, 0, 0 #define COLOR_RED 255, 0, 0
#define C_GREEN 0, 255, 0 #define COLOR_GREEN 0, 255, 0
#define C_BLUE 0, 0, 255 #define COLOR_BLUE 0, 0, 255
#define C_WHITE 255, 255, 255 #define COLOR_WHITE 255, 255, 255
#define DISPLAY_WIDTH 720 #define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480 #define DISPLAY_HEIGHT 480
#define DISPLAY_SCALE 2
typedef struct display_t { typedef struct display_t {
SDL_Window *window; SDL_Window *window;
@ -28,4 +29,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_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); 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,9 +1,10 @@
#ifndef ERRORS_H #ifndef _ERRORS_H
#define ERRORS_H #define _ERRORS_H
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <assert.h> #define SUCCESS 0
#define FAILURE 1
#ifndef DEBUG #ifndef DEBUG
#define DEBUG 0 #define DEBUG 0
@ -40,12 +41,12 @@
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
return NULL; \ return NULL; \
} while (0); } while (0);
#define goto_failure \ #define goto_failure \
do { \ do { \
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \ fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
goto failure; \ goto failure; \
} while (0); } while (0);
#else #else
#define debug_printf(...) #define debug_printf(...)
@ -66,4 +67,4 @@
#define goto_failure goto failure; #define goto_failure goto failure;
#endif #endif
#endif // ERRORS_H #endif // _ERRORS_H

View File

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

View File

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

View File

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

View File

@ -3,23 +3,16 @@
#include "asset_manager.h" #include "asset_manager.h"
#include "game.h" #include "game.h"
#include "display.h" #include "display.h"
#include "utils.h"
#include "errors.h" #include "errors.h"
texture_t *load_texture(void) texture_t *load_texture(void)
{ {
uint16_t width, height; uint16_t width, height;
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1) if(file_read(&width, sizeof(width), 1, game.asset_manager.asset_file)) return_failure_ptr;
{
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) if(file_read(&height, sizeof(height), 1, game.asset_manager.asset_file)) return_failure_ptr;
{
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); SDL_Surface *tmp_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGB565);
if(!tmp_surface) if(!tmp_surface)
@ -34,11 +27,7 @@ texture_t *load_texture(void)
goto failure; goto failure;
} }
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height) if(file_read(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file)) goto_failure;
{
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)) if(!SDL_SetSurfaceColorKey(tmp_surface, true, TEXTURE_TRANSPARENT_COLOR))
{ {
@ -65,6 +54,7 @@ texture_t *load_texture(void)
failure: failure:
SDL_DestroySurface(tmp_surface); SDL_DestroySurface(tmp_surface);
return NULL; return NULL;
} }

View File

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

View File

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

View File

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

14
core/host/linux/utils.c Normal file
View File

@ -0,0 +1,14 @@
#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;
}

8
core/host/linux/utils.h Normal file
View File

@ -0,0 +1,8 @@
#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 #ifndef _VECTOR2D_H
#define VECTOR2D_H #define _VECTOR2D_H
#include <SDL3/SDL_rect.h> #include <SDL3/SDL_rect.h>
#include <math.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); float lerp_time(float a, float b, float s, float t);
#endif // VECTOR2D_H #endif // _VECTOR2D_H

1
core/host/windows Symbolic link
View File

@ -0,0 +1 @@
linux

View File

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

View File

@ -1,193 +0,0 @@
#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

@ -1,31 +0,0 @@
#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

@ -1,14 +0,0 @@
#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

@ -1,17 +0,0 @@
#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

@ -1,19 +0,0 @@
#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

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

View File

@ -1,67 +0,0 @@
#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

@ -1,16 +0,0 @@
#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

@ -1,13 +0,0 @@
#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

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

View File

@ -1,19 +0,0 @@
#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

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,5 @@
#ifndef ANIMATION_SYSTEM_H #ifndef _ANIMATION_SYSTEM_H
#define ANIMATION_SYSTEM_H #define _ANIMATION_SYSTEM_H
#include <stdbool.h> #include <stdbool.h>
#include "ecs.h" #include "ecs.h"
@ -27,4 +27,4 @@ int animation_system_destroy(component_t *system);
int animation_system_apply_changes(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 #ifndef _COMPONENTS_H
#define COMPONENTS_H #define _COMPONENTS_H
#include "transform_component.h" #include "transform_component.h"
#include "sprite_component.h" #include "sprite_component.h"
@ -13,4 +13,4 @@
//#include "lua_component.h" //#include "lua_component.h"
// Add new components header file here // 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) if(!component_data->animation_system)
{ {
error_printf("Door component require an animation system."); error_printf("Door component require an animation system.");
return EXIT_FAILURE; return FAILURE;
} }
component_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT); component_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT);
if(!component_data->hitbox_component) if(!component_data->hitbox_component)
{ {
error_printf("Door component require an hitbox component."); error_printf("Door component require an hitbox component.");
return EXIT_FAILURE; return FAILURE;
} }
component_data->last_state = false; component_data->last_state = false;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int door_component_update(component_t *component) 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; component_data->last_state = component_data->state;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int door_component_destroy(component_t *component) inline int door_component_destroy(component_t *component)
@ -57,7 +57,7 @@ inline int door_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }
// Event arg : size_t state, Subscription data : entity_t *entity // 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; component_data->state = *(size_t *)event_arg;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int door_component_subscribe(component_t *component) 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; if(event_bus_subscribe(component_data->topic_id, door_component_callback, component)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef DOOR_COMPONENT_H #ifndef _DOOR_COMPONENT_H
#define DOOR_COMPONENT_H #define _DOOR_COMPONENT_H
#include "ecs.h" #include "ecs.h"
@ -22,4 +22,4 @@ int door_component_destroy(component_t *component);
int door_component_subscribe(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) if(!component_data->transform_component)
{ {
error_printf("Hitbox component require a transform component."); error_printf("Hitbox component require a transform component.");
return EXIT_FAILURE; return FAILURE;
} }
component_data->position = (fvector2d_t) { component_data->position = (fvector2d_t) {
@ -29,7 +29,7 @@ inline int hitbox_component_init(component_t *component)
component_data->activated = true; component_data->activated = true;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int hitbox_component_update(component_t *component) 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.w = transform_component_data->bounds.w * component_data->scale.x;
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y; component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
return EXIT_SUCCESS; return SUCCESS;
} }
#if DEBUG >= 2 #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; const hitbox_component_data_t *component_data = component->data;
if(component_data->activated) if(component_data->activated)
if(display_draw_frect(&component_data->bounds, C_RED, 128)) return_failure_int; if(display_draw_frect(&component_data->bounds, COLOR_RED, 128)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
#endif // DEBUG >= 2 #endif // DEBUG >= 2
@ -65,5 +65,5 @@ inline int hitbox_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef HITBOX_COMPONENT_H #ifndef _HITBOX_COMPONENT_H
#define HITBOX_COMPONENT_H #define _HITBOX_COMPONENT_H
#include <stdbool.h> #include <stdbool.h>
#include "ecs.h" #include "ecs.h"
@ -23,4 +23,4 @@ int hitbox_component_draw(const component_t *component);
#endif #endif
int hitbox_component_destroy(component_t *component); 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) if(!component_data->transform_component)
{ {
error_printf("Hitbox component require a transform component."); error_printf("Hitbox component require a transform component.");
return EXIT_FAILURE; return FAILURE;
} }
component_data->position = (fvector2d_t) { component_data->position = (fvector2d_t) {
@ -30,7 +30,7 @@ inline int interact_component_init(component_t *component)
component_data->topic_id = 0; component_data->topic_id = 0;
component_data->activated = true; component_data->activated = true;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int interact_component_update(component_t *component) 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.w = transform_component_data->bounds.w * component_data->scale.x;
component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y; component_data->bounds.h = transform_component_data->bounds.h * component_data->scale.y;
return EXIT_SUCCESS; return SUCCESS;
} }
#if DEBUG >= 2 #if DEBUG >= 2
@ -53,9 +53,9 @@ inline int interact_component_draw(const component_t *component)
{ {
const interact_component_data_t *component_data = component->data; const interact_component_data_t *component_data = component->data;
if(display_draw_frect(&component_data->bounds, C_GREEN, 128)) return_failure_int; if(display_draw_frect(&component_data->bounds, COLOR_GREEN, 128)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
#endif // DEBUG >= 2 #endif // DEBUG >= 2
@ -65,7 +65,7 @@ inline int interact_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }
inline int interact_component_interact(component_t *component) inline int interact_component_interact(component_t *component)
@ -80,10 +80,10 @@ inline int interact_component_interact(component_t *component)
default: default:
error_printf("Interaction type not valid : %d", component_data->type); error_printf("Interaction type not valid : %d", component_data->type);
return EXIT_FAILURE; return FAILURE;
} }
if(event_bus_publish(component_data->topic_id, &component_data->state)) return_failure_int; if(event_bus_publish(component_data->topic_id, &component_data->state)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef INTERACT_COMPONENT_H #ifndef _INTERACT_COMPONENT_H
#define INTERACT_COMPONENT_H #define _INTERACT_COMPONENT_H
#include "ecs.h" #include "ecs.h"
#include "vector2d.h" #include "vector2d.h"
@ -32,4 +32,4 @@ int interact_component_destroy(component_t *component);
int interact_component_interact(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) if(!component_data->animation_system)
{ {
error_printf("Lever component require an animation system."); error_printf("Lever component require an animation system.");
return EXIT_FAILURE; return FAILURE;
} }
component_data->last_state = false; component_data->last_state = false;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int lever_component_update(component_t *component) 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; component_data->last_state = component_data->state;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int lever_component_destroy(component_t *component) inline int lever_component_destroy(component_t *component)
@ -47,7 +47,7 @@ inline int lever_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }
// Event arg : size_t state, Subscription data : component_t *component // 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; component_data->state = *(size_t *)event_arg;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int lever_component_subscribe(component_t *component) 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; if(event_bus_subscribe(component_data->topic_id, lever_component_callback, component)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef LEVER_COMPONENT_H #ifndef _LEVER_COMPONENT_H
#define LEVER_COMPONENT_H #define _LEVER_COMPONENT_H
#include "ecs.h" #include "ecs.h"
#include "vector2d.h" #include "vector2d.h"
@ -21,4 +21,4 @@ int lever_component_destroy(component_t *component);
int lever_component_subscribe(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"); luaL_dostring(component_data->lua_state, "function f (a) print(a) end");
return EXIT_SUCCESS; return SUCCESS;
} }
inline int lua_component_update(component_t *component __attribute__((unused))) 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\")"); luaL_dostring(component_data->lua_state, "f(\"prout\")");
return EXIT_SUCCESS; return SUCCESS;
} }
#if DEBUG >= 2 #if DEBUG >= 2
@ -47,5 +47,5 @@ inline int lua_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef LUA_COMPONENT_H #ifndef _LUA_COMPONENT_H
#define LUA_COMPONENT_H #define _LUA_COMPONENT_H
#include <lua.h> #include <lua.h>
#include "ecs.h" #include "ecs.h"
@ -16,4 +16,4 @@ int lua_component_draw(const component_t *component);
#endif // DEBUG >= 2 #endif // DEBUG >= 2
int lua_component_destroy(component_t *component); 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) if(!system_data->transform_component)
{ {
error_printf("Physics system require a transform component."); error_printf("Physics system require a transform component.");
return EXIT_FAILURE; return FAILURE;
} }
system_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT); system_data->hitbox_component = entity_get_component(component->entity, HITBOX_COMPONENT);
if(!system_data->hitbox_component) if(!system_data->hitbox_component)
{ {
error_printf("Physics system require an hitbox component."); error_printf("Physics system require an hitbox component.");
return EXIT_FAILURE; return FAILURE;
} }
system_data->target_velocity = (fvector2d_t) { system_data->target_velocity = (fvector2d_t) {
@ -40,7 +40,7 @@ inline int physics_system_init(component_t *component)
system_data->smoothing_factor = 1.0f; system_data->smoothing_factor = 1.0f;
return EXIT_SUCCESS; return SUCCESS;
} }
// Args : component_t *component // Args : component_t *component
@ -58,7 +58,7 @@ int physics_system_update(component_t *component)
if(hitbox_component_data->activated) if(hitbox_component_data->activated)
{ {
// Resolve entities collisions. // Resolve entities collisions.
if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return EXIT_FAILURE; if(linked_list_for_each(&game.entity_manager.entities, action_detect_solve_entities_collision, component)) return FAILURE;
// Resolve map collisions // Resolve map collisions
if(game.map) 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.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; transform_component_data->bounds.y += system_data->velocity.y * (float)game.delta_time_ms / 1000.0f;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int physics_system_destroy(component_t *component) inline int physics_system_destroy(component_t *component)
@ -145,7 +145,7 @@ inline int physics_system_destroy(component_t *component)
free(system_data); free(system_data);
return EXIT_SUCCESS; return SUCCESS;
} }
static inline int action_detect_solve_entities_collision(elem_t *elem, void *arg) 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) if(target_entity->id == physics_system->entity->id)
{ {
return EXIT_SUCCESS; return SUCCESS;
} }
const component_t *target_hitbox_component = entity_get_component(target_entity, HITBOX_COMPONENT); const component_t *target_hitbox_component = entity_get_component(target_entity, HITBOX_COMPONENT);
if(!target_hitbox_component) return EXIT_SUCCESS; if(!target_hitbox_component) return SUCCESS;
const hitbox_component_data_t *target_hitbox_component_data = target_hitbox_component->data; const hitbox_component_data_t *target_hitbox_component_data = target_hitbox_component->data;
if(!target_hitbox_component_data->activated) return EXIT_SUCCESS; if(!target_hitbox_component_data->activated) return SUCCESS;
const frect_t *hitbox_bounds = &hitbox_component_data->bounds; const frect_t *hitbox_bounds = &hitbox_component_data->bounds;
const frect_t *target_hitbox_bounds = &target_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 EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef PHYSICS_SYSTEM_H #ifndef _PHYSICS_SYSTEM_H
#define PHYSICS_SYSTEM_H #define _PHYSICS_SYSTEM_H
#include "ecs.h" #include "ecs.h"
#include "vector2d.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_update(component_t *component);
int physics_system_destroy(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) if(!system_data->sprite_component)
{ {
error_printf("Player system require a sprite component."); error_printf("Player system require a sprite component.");
return EXIT_FAILURE; return FAILURE;
} }
system_data->animation_system = entity_get_component(component->entity, ANIMATION_SYSTEM); system_data->animation_system = entity_get_component(component->entity, ANIMATION_SYSTEM);
if(!system_data->animation_system) if(!system_data->animation_system)
{ {
error_printf("Player system require an animation system."); error_printf("Player system require an animation system.");
return EXIT_FAILURE; return FAILURE;
} }
system_data->physics_system = entity_get_component(component->entity, PHYSICS_SYSTEM); system_data->physics_system = entity_get_component(component->entity, PHYSICS_SYSTEM);
if(!system_data->physics_system) if(!system_data->physics_system)
{ {
error_printf("Player system require a physics system."); error_printf("Player system require a physics system.");
return EXIT_FAILURE; return FAILURE;
} }
system_data->state = PLAYER_MOVING; system_data->state = PLAYER_MOVING;
return EXIT_SUCCESS; return SUCCESS;
} }
static void player_system_get_inputs(player_system_data_t *system_data); 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_set_animation_and_speed(system_data)) return_failure_int;
if(player_system_process_interaction(system_data)) return_failure_int; if(player_system_process_interaction(system_data)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int player_system_destroy(component_t *component) inline int player_system_destroy(component_t *component)
@ -63,7 +63,7 @@ inline int player_system_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }
static inline void player_system_get_inputs(player_system_data_t *system_data) 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; if(animation_system_apply_changes(system_data->animation_system)) return_failure_int;
} }
return EXIT_SUCCESS; return SUCCESS;
} }
// Args : player_system_data_t *system_data // 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; physics_system_data_t *physics_system_data = system_data->physics_system->data;
component_t *target_interact_component = entity_get_component(target_entity, INTERACT_COMPONENT); component_t *target_interact_component = entity_get_component(target_entity, INTERACT_COMPONENT);
if(!target_interact_component) return EXIT_SUCCESS; if(!target_interact_component) return SUCCESS;
interact_component_data_t *target_interact_component_data = target_interact_component->data; interact_component_data_t *target_interact_component_data = target_interact_component->data;
if(!target_interact_component_data->activated) if(!target_interact_component_data->activated)
{ {
return EXIT_SUCCESS; return SUCCESS;
} }
const frect_t *hitbox_bounds = &((hitbox_component_data_t *)physics_system_data->hitbox_component->data)->bounds; 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; if(interact_component_interact(target_interact_component)) return_failure_int;
} }
return EXIT_SUCCESS; return SUCCESS;
} }
static inline int player_system_process_interaction(player_system_data_t *system_data) 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]; last_state = game.inputs.keys[KEY_INTERACT];
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef PLAYER_SYSTEM_H #ifndef _PLAYER_SYSTEM_H
#define PLAYER_SYSTEM_H #define _PLAYER_SYSTEM_H
#include "ecs.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_update(component_t *component);
int player_system_destroy(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) if(!component_data->transform_component)
{ {
error_printf("Sprite component require a transform component."); error_printf("Sprite component require a transform component.");
return EXIT_FAILURE; return FAILURE;
} }
transform_component_data_t *transform_component_data = component_data->transform_component->data; 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; component_data->flip = false;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int sprite_component_update(component_t *component) 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); 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; component->entity->draw_priority = transform_component_data->bounds.y + transform_component_data->bounds.h;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int sprite_component_draw(const component_t *component) 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; if(display_draw_texture(component_data->texture, &component_data->src_rect, &component_data->dst_rect, component_data->flip)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
inline int sprite_component_destroy(component_t *component) inline int sprite_component_destroy(component_t *component)
@ -55,5 +55,5 @@ inline int sprite_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef SPRITE_COMPONENT_H #ifndef _SPRITE_COMPONENT_H
#define SPRITE_COMPONENT_H #define _SPRITE_COMPONENT_H
#include "ecs.h" #include "ecs.h"
#include "asset_manager.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_draw(const component_t *component);
int sprite_component_destroy(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 .h = 32.0f
}; };
return EXIT_SUCCESS; return SUCCESS;
} }
inline int transform_component_update(component_t *component __attribute__((unused))) inline int transform_component_update(component_t *component __attribute__((unused)))
{ {
return EXIT_SUCCESS; return SUCCESS;
} }
#if DEBUG >= 2 #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; const transform_component_data_t *component_data = component->data;
if(display_draw_frect(&component_data->bounds, C_BLUE, 128)) return_failure_int; if(display_draw_frect(&component_data->bounds, COLOR_BLUE, 128)) return_failure_int;
return EXIT_SUCCESS; return SUCCESS;
} }
#endif // DEBUG >= 2 #endif // DEBUG >= 2
@ -43,5 +43,5 @@ inline int transform_component_destroy(component_t *component)
free(component_data); free(component_data);
return EXIT_SUCCESS; return SUCCESS;
} }

View File

@ -1,5 +1,5 @@
#ifndef TRANSFORM_COMPONENT_H #ifndef _TRANSFORM_COMPONENT_H
#define TRANSFORM_COMPONENT_H #define _TRANSFORM_COMPONENT_H
#include "ecs.h" #include "ecs.h"
#include "vector2d.h" #include "vector2d.h"
@ -16,4 +16,4 @@ int transform_component_draw(const component_t *component);
#endif // DEBUG >= 2 #endif // DEBUG >= 2
int transform_component_destroy(component_t *component); 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; game.is_running = true;
return EXIT_SUCCESS; return SUCCESS;
} }
void game_handle_event(void) void game_handle_event(void)
@ -158,7 +158,7 @@ int game_update(void)
camera_update(); camera_update();
return EXIT_SUCCESS; return SUCCESS;
} }
int game_render(void) int game_render(void)
@ -200,7 +200,7 @@ int game_render(void)
return_failure_int; return_failure_int;
} }
return EXIT_SUCCESS; return SUCCESS;
} }
int game_exit(void) int game_exit(void)
@ -210,5 +210,5 @@ int game_exit(void)
if(event_bus_exit()) return_failure_int; if(event_bus_exit()) return_failure_int;
display_exit(); display_exit();
return EXIT_SUCCESS; return SUCCESS;
} }