Better usage of SDL2 implementation and working Makefile structure

This commit is contained in:
Ulysse Cura 2026-04-12 16:20:33 +02:00
parent 7da6337f37
commit e792cb7bc5
10 changed files with 74 additions and 35 deletions

37
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,37 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/build/2D_Engine",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [
{
"name": "LD_LIBRARY_PATH",
"value": "${workspaceFolder}/build/"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

2
.vscode/tasks.json vendored
View File

@ -7,7 +7,7 @@
"type": "shell", "type": "shell",
"command": "make", "command": "make",
"args": [ "args": [
// "-j$(nproc)" "-j$(nproc)"
], ],
"group": "build", "group": "build",
"presentation": { "presentation": {

View File

@ -3,9 +3,6 @@ SHELL = /bin/bash
# Build folder # Build folder
BUILD_DIR = build BUILD_DIR = build
# Core output target name
CORE_OUTPUT := libCore.so
# Source output target name # Source output target name
SOURCE_OUTPUT := 2D_Engine SOURCE_OUTPUT := 2D_Engine
@ -24,22 +21,18 @@ YELLOW := \033[33m
RESET := \033[m RESET := \033[m
# Default targets # Default targets
#all: build_core build_src compress_assets end #all: build_core build_src compress_assets
all: build_core build_src all: build_core build_src
# Sub Makefiles # Sub Makefiles
SOURCE_DIR = src SOURCE_DIR = src
CORE_DIR = core CORE_DIR = core/src
ASSETS_DIR = assets ASSETS_DIR = assets
include $(CORE_DIR)/Makefile include $(CORE_DIR)/Makefile
include $(SOURCE_DIR)/Makefile include $(SOURCE_DIR)/Makefile
include $(ASSETS_DIR)/Makefile include $(ASSETS_DIR)/Makefile
# Print ending message
end:
@echo "Built target $(OUTPUT)"
# Clean # Clean
.PHONY: clean .PHONY: clean
clean: clean:
@ -49,5 +42,5 @@ clean:
# Run executable # Run executable
.PHONY: run .PHONY: run
run: run:
$(Q)./$(OUTPUT) $(Q)LD_LIBRARY_PATH=$(shell pwd)/build/ ./$(BUILD_DIR)/$(SOURCE_OUTPUT)

View File

@ -0,0 +1,9 @@
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
# 2D_Engine_C SDL2 Core
## Description
The core made on SDL2 for the multi-platform 2D_Engine_C.
## Licences
- This program is open-source : you can redistribute and/or modify it under the term of the **GNU GPLv3**.
Copyright (C) 2025 Ulysse Cura. See [LICENSE](LICENSE) or [gnu.org/licenses/gpl-3.0.html](https://www.gnu.org/licenses/gpl-3.0.html).

View File

@ -2,7 +2,6 @@
SOURCES := \ SOURCES := \
main.c \ main.c \
game.c \ game.c \
texture_manager.c \
ecs/ecs.c \ ecs/ecs.c \
ecs/components/transform_component.c \ ecs/components/transform_component.c \
ecs/components/sprite_component.c \ ecs/components/sprite_component.c \
@ -11,7 +10,7 @@ SOURCES := \
# Compiler and flags # Compiler and flags
SRC_CC = gcc SRC_CC = gcc
SRC_CFLAGS = -Wall -Wextra -std=c17 SRC_CFLAGS = -Wall -Wextra -std=c17 -g
SRC_INCLUDE_DIRS = $(SOURCE_DIR)/headers $(SOURCE_DIR)/ecs/headers $(SOURCE_DIR)/ecs/components/headers $(CORE_DIR)/headers SRC_INCLUDE_DIRS = $(SOURCE_DIR)/headers $(SOURCE_DIR)/ecs/headers $(SOURCE_DIR)/ecs/components/headers $(CORE_DIR)/headers
SRC_LDFLAGS = SRC_LDFLAGS =
@ -22,7 +21,7 @@ SOURCE_OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/$(SOURCE_DIR)/%.o)
CURRENT_SRC_FILE := 0 CURRENT_SRC_FILE := 0
# Build src target # Build src target
build_src: src_setup count_src_build $(BUILD_DIR)/$(SOURCE_OUTPUT) build_src: src_setup count_src_build $(BUILD_DIR)/$(SOURCE_OUTPUT) src_end
# Source build directories and # Source build directories and
src_setup: src_setup:
@ -34,9 +33,10 @@ count_src_build:
$(eval export TOTAL_SRC_FILES := $(shell echo $$(($$(make -n $(SOURCE_OBJECTS) 2>/dev/null | grep -c "Building") + 1)))) $(eval export TOTAL_SRC_FILES := $(shell echo $$(($$(make -n $(SOURCE_OBJECTS) 2>/dev/null | grep -c "Building") + 1))))
# Link game target # Link game target
$(BUILD_DIR)/$(SOURCE_OUTPUT): $(SOURCE_OBJECTS) # The ignore unresolved symbols in shared libs is ugly but works if the core changes.
$(BUILD_DIR)/$(SOURCE_OUTPUT): $(SOURCE_OBJECTS) $(BUILD_DIR)/libCore.so
@echo -e "[100%] $(YELLOW)Linking $(SOURCE_OUTPUT)$(RESET)" @echo -e "[100%] $(YELLOW)Linking $(SOURCE_OUTPUT)$(RESET)"
$(Q)$(SRC_CC) $(SRC_LDFLAGS) -o $@ $(SOURCE_OBJECTS) $(Q)$(SRC_CC) $(SRC_LDFLAGS) $(SOURCE_OBJECTS) -L./$(BUILD_DIR) -lCore -Wl,-unresolved-symbols=ignore-in-shared-libs -o $@
# Build .o files # Build .o files
$(BUILD_DIR)/$(SOURCE_DIR)/%.o: $(SOURCE_DIR)/%.c $(BUILD_DIR)/$(SOURCE_DIR)/%.o: $(SOURCE_DIR)/%.c
@ -45,5 +45,9 @@ $(BUILD_DIR)/$(SOURCE_DIR)/%.o: $(SOURCE_DIR)/%.c
@echo -e "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)" @echo -e "[$(PERCENTAGE)%] $(GREEN)Building C object $@$(RESET)"
$(Q)$(SRC_CC) $(SRC_CFLAGS) $(SRC_INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@ $(Q)$(SRC_CC) $(SRC_CFLAGS) $(SRC_INCLUDE_DIRS:%=-I%) -MMD -MP -c $< -o $@
# Print ending message
src_end: $(BUILD_DIR)/$(SOURCE_OUTPUT)
@echo "Built target $(SOURCE_OUTPUT)"
# Source files dependencies # Source files dependencies
-include $(SOURCE_OBJECTS:.o=.d) -include $(SOURCE_OBJECTS:.o=.d)

View File

@ -5,8 +5,8 @@
#include "vector2d.h" #include "vector2d.h"
typedef struct transform_component_data_t { typedef struct transform_component_data_t {
rect_t bounds; frect_t bounds;
vector2d_t velocity; fvector2d_t velocity;
float speed; float speed;
} transform_component_data_t; } transform_component_data_t;

View File

@ -9,7 +9,7 @@ void sprite_component_init(component_t *component, va_list args)
component_data->transform_component_data = get_component(component->entity, TRANSFORM_COMPONENT)->component_data.transform_component_data; component_data->transform_component_data = get_component(component->entity, TRANSFORM_COMPONENT)->component_data.transform_component_data;
component_data->src_rect = component_data->transform_component_data->bounds; frect_to_rect(&component_data->transform_component_data->bounds, &component_data->src_rect);
component_data->src_rect.x = 0; component_data->src_rect.x = 0;
component_data->src_rect.y = 0; component_data->src_rect.y = 0;
@ -21,7 +21,7 @@ void sprite_component_init(component_t *component, va_list args)
inline void sprite_component_update(component_t *component) inline void sprite_component_update(component_t *component)
{ {
sprite_component_data_t *component_data = component->component_data.sprite_component_data; sprite_component_data_t *component_data = component->component_data.sprite_component_data;
component_data->dst_rect = component_data->transform_component_data->bounds; frect_to_rect(&component_data->transform_component_data->bounds, &component_data->dst_rect);
} }
inline void sprite_component_draw(component_t *component) inline void sprite_component_draw(component_t *component)

View File

@ -2,20 +2,16 @@
#include "transform_component.h" #include "transform_component.h"
#include "game.h" #include "game.h"
// Args : rect_t bounds, double speed // Args : frect_t bounds, double speed
void transform_component_init(component_t *component, va_list args) void transform_component_init(component_t *component, va_list args)
{ {
transform_component_data_t *const component_data = component->component_data.transform_component_data; transform_component_data_t *const component_data = component->component_data.transform_component_data;
rect_t bounds = va_arg(args, rect_t); component_data->bounds = va_arg(args, frect_t);
float speed = (float)va_arg(args, double); component_data->speed = (float)va_arg(args, double);
component_data->bounds = bounds; component_data->velocity.x = 0.0f;
component_data->velocity.y = 0.0f;
component_data->velocity.x = 0;
component_data->velocity.y = 0;
component_data->speed = speed;
} }
void transform_component_update(component_t *component) void transform_component_update(component_t *component)

View File

@ -16,7 +16,7 @@ void game_init(void)
entity_manager_add_entity(&game.entity_manager, player); entity_manager_add_entity(&game.entity_manager, player);
rect_t player_bounds = {10.0f, 10.0f, 32.0f, 32.0f}; frect_t player_bounds = {10.0f, 10.0f, 32.0f, 32.0f};
add_component(player, create_component(TRANSFORM_COMPONENT), player_bounds, PLAYER_DEFAULT_SPEED); add_component(player, create_component(TRANSFORM_COMPONENT), player_bounds, PLAYER_DEFAULT_SPEED);
add_component(player, create_component(SPRITE_COMPONENT), "player_idle_sheet"); add_component(player, create_component(SPRITE_COMPONENT), "player_idle_sheet");
@ -32,12 +32,12 @@ void game_handle_event(void)
while(pollevent(&event)) while(pollevent(&event))
{ {
if(event.type == KEYEV_DOWN) if(event.type == EVENT_KEY_DOWN)
game.events.keys[event.key.keysym.scancode] = true; game.events.keys[event.key.keysym.scancode] = true;
else if(event.type == KEYEV_UP) else if(event.type == EVENT_KEY_UP)
game.events.keys[event.key.keysym.scancode] = false; game.events.keys[event.key.keysym.scancode] = false;
if(event.type == SDL_QUIT) if(event.type == EVENT_QUIT)
game.is_running = false; game.is_running = false;
} }
} }
@ -46,7 +46,7 @@ static inline void update_time(void)
{ {
clock_t last_clock_state = clock(); clock_t last_clock_state = clock();
clock_t start_clock = clock(); clock_t start_clock = clock();
game.delta_time_ms = (double)(start_clock - last_clock_state) * 1000.0f / CLOCKS_PER_SEC; game.delta_time_ms = (float)(start_clock - last_clock_state) * 1000.0f / CLOCKS_PER_SEC;
last_clock_state = start_clock; last_clock_state = start_clock;
} }

View File

@ -21,7 +21,7 @@ typedef struct game_t {
entity_manager_t entity_manager; entity_manager_t entity_manager;
bool is_running; bool is_running;
double delta_time_ms; float delta_time_ms;
} game_t; } game_t;
// Global game instance // Global game instance