feat(engine): 重构游戏引擎核心逻辑
- 重新设计了引擎的初始化和运行流程 - 引入了实体组件系统(ECS)和物理系统 - 优化了渲染系统和输入系统 - 移除了不必要的资源管理系统 - 调整了日志系统的实现
This commit is contained in:
64
game_engine/ecs/ge_entity.h
Normal file
64
game_engine/ecs/ge_entity.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef __GE_ENTIRY_H__
|
||||
#define __GE_ENTIRY_H__
|
||||
|
||||
#include <ge_core.h>
|
||||
#include <utils/ge_vector2i.h>
|
||||
#include "ge_render_component.h"
|
||||
#include "ge_physics_component.h"
|
||||
|
||||
#include <stdint.h>
|
||||
typedef uint16_t ge_ecs_id_t; // 支持65536个实体
|
||||
#define GE_ECS_MAX 128
|
||||
|
||||
typedef enum {
|
||||
GE_COMPONENT_ACVIVE = 1 << 0,
|
||||
GE_COMPONENT_POSITION = 1 << 1,
|
||||
GE_COMPONENT_TRANSFORM = 1 << 2, // TODO not implimented
|
||||
GE_COMPONENT_RENDERABLE = 1 << 3,
|
||||
GE_COMPONENT_PHYSICS_BODY = 1 << 4, // TODO not implimented
|
||||
GE_COMPONENT_COLLIDER = 1 << 5, // TODO not implimented
|
||||
GE_COMPONENT_TIMED_LIFE = 1 << 6, // TODO not implimented
|
||||
} ge_ecs_mask_t;
|
||||
|
||||
#define GE_RENDERABLE_MASK \
|
||||
(GE_COMPONENT_POSITION | GE_COMPONENT_RENDERABLE)
|
||||
#define GE_PHYSICS_MASK \
|
||||
(GE_COMPONENT_POSITION | GE_COMPONENT_PHYSICS_BODY)
|
||||
|
||||
typedef struct ge_entity {
|
||||
ge_ecs_id_t id;
|
||||
ge_ecs_mask_t component_mask;
|
||||
ge_vector2i_t position;
|
||||
ge_render_component_t renderable;
|
||||
ge_physics_component_t physics_body;
|
||||
} ge_entity_t;
|
||||
|
||||
typedef struct {
|
||||
// TODO static storage to dynamic storage
|
||||
ge_entity_t entities[GE_ECS_MAX];
|
||||
ge_ecs_id_t count;
|
||||
} ge_ecs_storage_t;
|
||||
|
||||
typedef struct ge_ecs {
|
||||
ge_ecs_storage_t storage;
|
||||
} ge_ecs_t;
|
||||
|
||||
|
||||
static inline ge_ecs_id_t ge_ecs_add_entity(ge_ecs_t* ecs, ge_entity_t** entity) {
|
||||
ge_ecs_id_t id = ++ecs->storage.count;
|
||||
if (id >= GE_ECS_MAX) {
|
||||
return 0;
|
||||
}
|
||||
// TODO squeze the data
|
||||
if (entity != NULL) *entity = &ecs->storage.entities[id];
|
||||
return id;
|
||||
}
|
||||
|
||||
static inline ge_entity_t* ge_ecs_get_entity(ge_ecs_t* ecs, ge_ecs_id_t id) {
|
||||
if(id >= ecs->storage.count) {
|
||||
return NULL;
|
||||
}
|
||||
return &ecs->storage.entities[id];
|
||||
}
|
||||
|
||||
#endif // __GE_ENTIRY_H__
|
||||
21
game_engine/ecs/ge_physics_component.h
Normal file
21
game_engine/ecs/ge_physics_component.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef __GE_PHYSICS_COMPONENT_H__
|
||||
#define __GE_PHYSICS_COMPONENT_H__
|
||||
|
||||
#include <utils/ge_vector2i.h>
|
||||
|
||||
#define GE_PHYSICS_VELOCITY_BIT 3
|
||||
#define GE_PHYSICS_ACCELERATION_BIT 3
|
||||
|
||||
typedef enum {
|
||||
GE_PHYSICS_COMPONENT_TYPE_NONE = 1 << 0,
|
||||
GE_PHYSICS_COMPONENT_TYPE_VELOCITY = 1 << 1,
|
||||
GE_PHYSICS_COMPONENT_TYPE_ACCELERATION = 1 << 2,
|
||||
} ge_physics_component_type_t;
|
||||
|
||||
typedef struct ge_physics_component {
|
||||
ge_physics_component_type_t type;
|
||||
ge_vector2i_t velocity;
|
||||
ge_vector2i_t acceleration;
|
||||
} ge_physics_component_t;
|
||||
|
||||
#endif // __GE_PHYSICS_COMPONENT_H__
|
||||
39
game_engine/ecs/ge_physics_system.h
Normal file
39
game_engine/ecs/ge_physics_system.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#ifndef __GE_PHYSICS_SYSTEM_H__
|
||||
#define __GE_PHYSICS_SYSTEM_H__
|
||||
|
||||
#include "ge_entity.h"
|
||||
#include "ge_physics_component.h"
|
||||
#include <pynic_log/pynic_log.h>
|
||||
|
||||
typedef struct ge_physics_system {
|
||||
ge_ecs_storage_t* ecs;
|
||||
} ge_physics_system_t;
|
||||
|
||||
#define _GE_PHYSICS_MASK (GE_PHYSICS_MASK | GE_COMPONENT_ACVIVE)
|
||||
|
||||
static inline void
|
||||
ge_physics_system_run_all(ge_physics_system_t* ctx) {
|
||||
Assert(ctx != NULL);
|
||||
ge_ecs_storage_t* ecs = ctx->ecs;
|
||||
Assert(ecs != NULL);
|
||||
for (int i = 1; i <= ecs->count && i < GE_ECS_MAX; ++i) {
|
||||
ge_entity_t* entity = &ecs->entities[i];
|
||||
ge_ecs_mask_t mask = entity->component_mask;
|
||||
if ((mask & _GE_PHYSICS_MASK) != _GE_PHYSICS_MASK) continue;
|
||||
|
||||
ge_physics_component_t* comp = &entity->physics_body;
|
||||
Assert(comp != NULL);
|
||||
ge_physics_component_type_t type= comp->type;
|
||||
|
||||
if (type & GE_PHYSICS_COMPONENT_TYPE_ACCELERATION) {
|
||||
comp->velocity.x += comp->acceleration.x >> GE_PHYSICS_ACCELERATION_BIT;
|
||||
comp->velocity.y += comp->acceleration.y >> GE_PHYSICS_ACCELERATION_BIT;
|
||||
}
|
||||
if (type & GE_PHYSICS_COMPONENT_TYPE_VELOCITY) {
|
||||
entity->position.x += comp->velocity.x >> GE_PHYSICS_VELOCITY_BIT;
|
||||
entity->position.y += comp->velocity.y >> GE_PHYSICS_VELOCITY_BIT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __GE_PHYSICS_SYSTEM_H__
|
||||
27
game_engine/ecs/ge_render_component.h
Normal file
27
game_engine/ecs/ge_render_component.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef __GE_RENDER_COMPONENT_H__
|
||||
#define __GE_RENDER_COMPONENT_H__
|
||||
|
||||
#include <interface/ge_render.h>
|
||||
|
||||
typedef enum {
|
||||
GE_RENDER_COMPONENT_TYPE_NONE,
|
||||
GE_RENDER_COMPONENT_TYPE_POINT,
|
||||
GE_RENDER_COMPONENT_TYPE_TEXT,
|
||||
GE_RENDER_COMPONENT_TYPE_RECT,
|
||||
GE_RENDER_COMPONENT_TYPE_RECOURCE,
|
||||
} ge_render_component_type_t;
|
||||
|
||||
typedef struct ge_render_component {
|
||||
ge_render_component_type_t type;
|
||||
union {
|
||||
struct {
|
||||
ge_render_color_t color;
|
||||
} point;
|
||||
struct {
|
||||
ge_render_pos2_t size;
|
||||
ge_render_color_t color;
|
||||
} rect;
|
||||
} data;
|
||||
} ge_render_component_t;
|
||||
|
||||
#endif // __GE_RENDER_COMPONENT_H__
|
||||
69
game_engine/ecs/ge_render_system.h
Normal file
69
game_engine/ecs/ge_render_system.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#ifndef __GE_RENDER_SYSTEM_H__
|
||||
#define __GE_RENDER_SYSTEM_H__
|
||||
|
||||
#include "ge_entity.h"
|
||||
#include "ge_render_component.h"
|
||||
#include <pynic_log/pynic_log.h>
|
||||
|
||||
typedef struct ge_render_system {
|
||||
ge_render_t* render;
|
||||
ge_ecs_storage_t* ecs;
|
||||
} ge_render_system_t;
|
||||
|
||||
static inline void
|
||||
ge_render_system_init(ge_render_system_t* ctx, ge_render_t* render) {
|
||||
Assert(ctx != NULL && render != NULL);
|
||||
ctx->render = render;
|
||||
}
|
||||
|
||||
#define _GE_RENDERABLE_MASK (GE_RENDERABLE_MASK | GE_COMPONENT_ACVIVE)
|
||||
|
||||
static inline void
|
||||
ge_render_system_draw_all(ge_render_system_t* ctx) {
|
||||
Assert(ctx != NULL);
|
||||
ge_ecs_storage_t* ecs = ctx->ecs;
|
||||
Assert(ecs != NULL);
|
||||
for (int i = 1; i <= ecs->count && i < GE_ECS_MAX; ++i) {
|
||||
ge_entity_t* entity = &ecs->entities[i];
|
||||
ge_ecs_mask_t mask = entity->component_mask;
|
||||
if ((mask & _GE_RENDERABLE_MASK) != _GE_RENDERABLE_MASK) continue;
|
||||
|
||||
ge_render_pos2_t pos = {
|
||||
entity->position.x,
|
||||
entity->position.y,
|
||||
};
|
||||
ge_render_component_t* comp = &entity->renderable;
|
||||
Assert(comp != NULL);
|
||||
|
||||
switch (comp->type) {
|
||||
case GE_RENDER_COMPONENT_TYPE_POINT:
|
||||
ctx->render->func_draw_point(
|
||||
ctx->render,
|
||||
&pos,
|
||||
comp->data.point.color
|
||||
);
|
||||
break;
|
||||
case GE_RENDER_COMPONENT_TYPE_RECT:
|
||||
ctx->render->func_draw_rect(
|
||||
ctx->render,
|
||||
&(ge_render_rect_t) {
|
||||
pos,
|
||||
comp->data.rect.size,
|
||||
},
|
||||
comp->data.rect.color
|
||||
);
|
||||
break;
|
||||
case GE_RENDER_COMPONENT_TYPE_TEXT:
|
||||
TODO();
|
||||
break;
|
||||
case GE_RENDER_COMPONENT_TYPE_RECOURCE:
|
||||
TODO();
|
||||
break;
|
||||
default:
|
||||
LOG_WARN("render component not set Avaliable type %d, id %d", comp->type, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // __GE_RENDER_SYSTEM_H__
|
||||
Reference in New Issue
Block a user