Better usage of SDL2 implementation and working Makefile structure
This commit is contained in:
parent
7da6337f37
commit
e792cb7bc5
|
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
"type": "shell",
|
||||
"command": "make",
|
||||
"args": [
|
||||
// "-j$(nproc)"
|
||||
"-j$(nproc)"
|
||||
],
|
||||
"group": "build",
|
||||
"presentation": {
|
||||
|
|
|
|||
13
Makefile
13
Makefile
|
|
@ -3,9 +3,6 @@ SHELL = /bin/bash
|
|||
# Build folder
|
||||
BUILD_DIR = build
|
||||
|
||||
# Core output target name
|
||||
CORE_OUTPUT := libCore.so
|
||||
|
||||
# Source output target name
|
||||
SOURCE_OUTPUT := 2D_Engine
|
||||
|
||||
|
|
@ -24,22 +21,18 @@ YELLOW := \033[33m
|
|||
RESET := \033[m
|
||||
|
||||
# Default targets
|
||||
#all: build_core build_src compress_assets end
|
||||
#all: build_core build_src compress_assets
|
||||
all: build_core build_src
|
||||
|
||||
# Sub Makefiles
|
||||
SOURCE_DIR = src
|
||||
CORE_DIR = core
|
||||
CORE_DIR = core/src
|
||||
ASSETS_DIR = assets
|
||||
|
||||
include $(CORE_DIR)/Makefile
|
||||
include $(SOURCE_DIR)/Makefile
|
||||
include $(ASSETS_DIR)/Makefile
|
||||
|
||||
# Print ending message
|
||||
end:
|
||||
@echo "Built target $(OUTPUT)"
|
||||
|
||||
# Clean
|
||||
.PHONY: clean
|
||||
clean:
|
||||
|
|
@ -49,5 +42,5 @@ clean:
|
|||
# Run executable
|
||||
.PHONY: run
|
||||
run:
|
||||
$(Q)./$(OUTPUT)
|
||||
$(Q)LD_LIBRARY_PATH=$(shell pwd)/build/ ./$(BUILD_DIR)/$(SOURCE_OUTPUT)
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
[](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).
|
||||
14
src/Makefile
14
src/Makefile
|
|
@ -2,7 +2,6 @@
|
|||
SOURCES := \
|
||||
main.c \
|
||||
game.c \
|
||||
texture_manager.c \
|
||||
ecs/ecs.c \
|
||||
ecs/components/transform_component.c \
|
||||
ecs/components/sprite_component.c \
|
||||
|
|
@ -11,7 +10,7 @@ SOURCES := \
|
|||
|
||||
# Compiler and flags
|
||||
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_LDFLAGS =
|
||||
|
||||
|
|
@ -22,7 +21,7 @@ SOURCE_OBJECTS = $(SOURCES:%.c=$(BUILD_DIR)/$(SOURCE_DIR)/%.o)
|
|||
CURRENT_SRC_FILE := 0
|
||||
|
||||
# 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
|
||||
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))))
|
||||
|
||||
# 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)"
|
||||
$(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_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)"
|
||||
$(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
|
||||
-include $(SOURCE_OBJECTS:.o=.d)
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@
|
|||
#include "vector2d.h"
|
||||
|
||||
typedef struct transform_component_data_t {
|
||||
rect_t bounds;
|
||||
vector2d_t velocity;
|
||||
frect_t bounds;
|
||||
fvector2d_t velocity;
|
||||
float speed;
|
||||
} transform_component_data_t;
|
||||
|
||||
|
|
|
|||
|
|
@ -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->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.y = 0;
|
||||
|
||||
|
|
@ -21,7 +21,7 @@ void sprite_component_init(component_t *component, va_list args)
|
|||
inline void sprite_component_update(component_t *component)
|
||||
{
|
||||
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)
|
||||
|
|
|
|||
|
|
@ -2,20 +2,16 @@
|
|||
#include "transform_component.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)
|
||||
{
|
||||
transform_component_data_t *const component_data = component->component_data.transform_component_data;
|
||||
|
||||
rect_t bounds = va_arg(args, rect_t);
|
||||
float speed = (float)va_arg(args, double);
|
||||
component_data->bounds = va_arg(args, frect_t);
|
||||
component_data->speed = (float)va_arg(args, double);
|
||||
|
||||
component_data->bounds = bounds;
|
||||
|
||||
component_data->velocity.x = 0;
|
||||
component_data->velocity.y = 0;
|
||||
|
||||
component_data->speed = speed;
|
||||
component_data->velocity.x = 0.0f;
|
||||
component_data->velocity.y = 0.0f;
|
||||
}
|
||||
|
||||
void transform_component_update(component_t *component)
|
||||
|
|
|
|||
10
src/game.c
10
src/game.c
|
|
@ -16,7 +16,7 @@ void game_init(void)
|
|||
|
||||
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(SPRITE_COMPONENT), "player_idle_sheet");
|
||||
|
|
@ -32,12 +32,12 @@ void game_handle_event(void)
|
|||
|
||||
while(pollevent(&event))
|
||||
{
|
||||
if(event.type == KEYEV_DOWN)
|
||||
if(event.type == EVENT_KEY_DOWN)
|
||||
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;
|
||||
|
||||
if(event.type == SDL_QUIT)
|
||||
if(event.type == EVENT_QUIT)
|
||||
game.is_running = false;
|
||||
}
|
||||
}
|
||||
|
|
@ -46,7 +46,7 @@ static inline void update_time(void)
|
|||
{
|
||||
clock_t last_clock_state = 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ typedef struct game_t {
|
|||
entity_manager_t entity_manager;
|
||||
|
||||
bool is_running;
|
||||
double delta_time_ms;
|
||||
float delta_time_ms;
|
||||
} game_t;
|
||||
|
||||
// Global game instance
|
||||
|
|
|
|||
Loading…
Reference in New Issue