feat(engine): 重构游戏引擎核心逻辑
- 重新设计了引擎的初始化和运行流程 - 引入了实体组件系统(ECS)和物理系统 - 优化了渲染系统和输入系统 - 移除了不必要的资源管理系统 - 调整了日志系统的实现
This commit is contained in:
66
game_engine/interface/timer/ge_fps.c
Normal file
66
game_engine/interface/timer/ge_fps.c
Normal file
@@ -0,0 +1,66 @@
|
||||
// ge_fps.c
|
||||
#include "ge_fps.h"
|
||||
|
||||
// 初始化FPS控制器
|
||||
void ge_fps_init(ge_fps_controller_t* fps_ctrl, ge_u32_t target_fps,
|
||||
ge_fps_get_us_func_t get_us, ge_fps_sleep_us_func_t sleep_us) {
|
||||
fps_ctrl->target_fps = target_fps;
|
||||
fps_ctrl->frame_duration = 1000 / target_fps;
|
||||
fps_ctrl->last_frame_time = get_us();
|
||||
fps_ctrl->frame_time = 0;
|
||||
fps_ctrl->sleep_time = 0;
|
||||
fps_ctrl->frame_count = 0;
|
||||
fps_ctrl->fps = 0;
|
||||
fps_ctrl->last_fps_time = fps_ctrl->last_frame_time;
|
||||
fps_ctrl->call_get_us = get_us;
|
||||
fps_ctrl->call_sleep_us = sleep_us;
|
||||
}
|
||||
|
||||
// 帧开始
|
||||
void ge_fps_begin_frame(ge_fps_controller_t* fps_ctrl) {
|
||||
fps_ctrl->last_frame_time = fps_ctrl->call_get_us();
|
||||
}
|
||||
|
||||
// 帧结束
|
||||
void ge_fps_end_frame(ge_fps_controller_t* fps_ctrl) {
|
||||
ge_u32_t current_time = fps_ctrl->call_get_us();
|
||||
fps_ctrl->frame_time = current_time - fps_ctrl->last_frame_time;
|
||||
|
||||
// 帧率自适应算法
|
||||
if (fps_ctrl->frame_time < fps_ctrl->frame_duration) {
|
||||
ge_u32_t sleep_time = fps_ctrl->frame_duration - fps_ctrl->frame_time;
|
||||
|
||||
// 动态调整休眠精度
|
||||
if (sleep_time > 2000) { // >2ms使用Sleep
|
||||
fps_ctrl->call_sleep_us(sleep_time - 1000); // 提前1ms唤醒
|
||||
current_time = fps_ctrl->call_get_us();
|
||||
|
||||
// 剩余时间忙等待
|
||||
while ((current_time - fps_ctrl->last_frame_time) < fps_ctrl->frame_duration) {
|
||||
current_time = fps_ctrl->call_get_us();
|
||||
}
|
||||
} else {
|
||||
// 短时间直接忙等待
|
||||
while ((fps_ctrl->call_get_us() - fps_ctrl->last_frame_time) < fps_ctrl->frame_duration);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新FPS计数(同原逻辑)
|
||||
fps_ctrl->frame_count++;
|
||||
if (current_time - fps_ctrl->last_fps_time >= 1000000) {
|
||||
fps_ctrl->fps = fps_ctrl->frame_count;
|
||||
fps_ctrl->frame_count = 0;
|
||||
fps_ctrl->last_fps_time = current_time;
|
||||
|
||||
// 动态调整目标帧时间(±5%容差)
|
||||
// ge_u32_t avg_frame_time = 1000000 / fps_ctrl->fps;
|
||||
// if (avg_frame_time > fps_ctrl->frame_duration * 1.05f) {
|
||||
// fps_ctrl->frame_duration = avg_frame_time;
|
||||
// } else if (avg_frame_time < fps_ctrl->frame_duration * 0.95f) {
|
||||
// fps_ctrl->frame_duration = GE_MAX(
|
||||
// fps_ctrl->target_fps * 1000,
|
||||
// avg_frame_time
|
||||
// );
|
||||
// }
|
||||
}
|
||||
}
|
||||
31
game_engine/interface/timer/ge_fps.h
Normal file
31
game_engine/interface/timer/ge_fps.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __GE_FPS_H__
|
||||
#define __GE_FPS_H__
|
||||
|
||||
#include <ge_common.h>
|
||||
struct ge_fps_controller;
|
||||
typedef struct ge_fps_controller ge_fps_controller_t;
|
||||
|
||||
typedef ge_u32_t (*ge_fps_get_us_func_t)(void);
|
||||
typedef void (*ge_fps_sleep_us_func_t)(ge_u32_t us);
|
||||
|
||||
void ge_fps_init(ge_fps_controller_t* fps_ctrl, ge_u32_t target_fps,
|
||||
ge_fps_get_us_func_t get_ms, ge_fps_sleep_us_func_t sleep_ms);
|
||||
void ge_fps_begin_frame(ge_fps_controller_t* fps_ctrl);
|
||||
void ge_fps_end_frame(ge_fps_controller_t* fps_ctrl);
|
||||
|
||||
// FPS控制器结构体
|
||||
struct ge_fps_controller {
|
||||
ge_u32_t target_fps; // 目标帧率
|
||||
ge_u32_t frame_duration; // 目标每帧时长(毫秒)
|
||||
ge_u32_t last_frame_time; // 上一帧开始时间
|
||||
ge_u32_t frame_time; // 当前帧耗时
|
||||
ge_u32_t sleep_time; // 需要休眠的时间
|
||||
ge_u32_t frame_count; // 帧计数器
|
||||
ge_u32_t fps; // 实际帧率
|
||||
ge_u32_t last_fps_time; // 上次计算FPS的时间
|
||||
|
||||
ge_fps_get_us_func_t call_get_us;
|
||||
ge_fps_sleep_us_func_t call_sleep_us;
|
||||
};
|
||||
|
||||
#endif // __GE_FPS_H__
|
||||
0
game_engine/interface/timer/ge_timer.c
Normal file
0
game_engine/interface/timer/ge_timer.c
Normal file
23
game_engine/interface/timer/ge_timer.h
Normal file
23
game_engine/interface/timer/ge_timer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef __GE_TIMER_H__
|
||||
#define __GE_TIMER_H__
|
||||
|
||||
#include <ge_config.h>
|
||||
#include "ge_fps.h"
|
||||
|
||||
typedef void (*ge_sleep_ms_func_t)(ge_u32_t);
|
||||
typedef ge_u32_t (*ge_get_ms_func_t)(void);
|
||||
|
||||
typedef void (*ge_sleep_us_func_t)(ge_u32_t);
|
||||
typedef ge_u32_t (*ge_get_us_func_t)(void);
|
||||
|
||||
typedef struct {
|
||||
ge_sleep_ms_func_t sleep_ms;
|
||||
ge_get_ms_func_t get_ms;
|
||||
|
||||
ge_sleep_us_func_t sleep_us;
|
||||
ge_get_us_func_t get_us;
|
||||
|
||||
ge_fps_controller_t fps_ctl;
|
||||
} ge_timer_t;
|
||||
|
||||
#endif // __GE_TIMER_H__
|
||||
Reference in New Issue
Block a user