feat(game): 添加基础游戏引擎和渲染模块

- 新增游戏引擎核心模块,包括初始化和运行逻辑
- 实现基本的渲染功能,支持控制台输出
- 添加物理引擎基础,包括碰撞检测
- 集成日志系统,用于调试和信息输出
- 创建窗口和输入管理模块
This commit is contained in:
ZZY
2025-06-24 02:16:01 +08:00
commit 5ce660e3a6
30 changed files with 2056 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
// 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_ms_func_t get_ms, ge_fps_sleep_ms_func_t sleep_ms) {
fps_ctrl->target_fps = target_fps;
fps_ctrl->frame_duration = 1000 / target_fps;
fps_ctrl->last_frame_time = get_ms();
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_ms = get_ms;
fps_ctrl->call_sleep_ms = sleep_ms;
}
// 帧开始
void ge_fps_begin_frame(ge_fps_controller_t* fps_ctrl) {
fps_ctrl->last_frame_time = fps_ctrl->call_get_ms();
}
// 帧结束
void ge_fps_end_frame(ge_fps_controller_t* fps_ctrl) {
// 计算当前帧耗时
ge_u32_t current_time = fps_ctrl->call_get_ms();
fps_ctrl->frame_time = current_time - fps_ctrl->last_frame_time;
// 计算需要休眠的时间
if (fps_ctrl->frame_time < fps_ctrl->frame_duration) {
fps_ctrl->sleep_time = fps_ctrl->frame_duration - fps_ctrl->frame_time;
// 高精度休眠
fps_ctrl->call_sleep_ms(fps_ctrl->sleep_time);
// 更新实际休眠后时间
current_time = fps_ctrl->call_get_ms();
fps_ctrl->sleep_time = current_time - fps_ctrl->last_frame_time - fps_ctrl->frame_time;
} else {
fps_ctrl->sleep_time = 0;
}
// 更新FPS计数
fps_ctrl->frame_count++;
// 每秒计算一次实际FPS
if (current_time - fps_ctrl->last_fps_time >= 1000) {
fps_ctrl->fps = fps_ctrl->frame_count;
fps_ctrl->frame_count = 0;
fps_ctrl->last_fps_time = current_time;
}
}

View 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_ms_func_t)(void);
typedef void (*ge_fps_sleep_ms_func_t)(ge_u32_t ms);
void ge_fps_init(ge_fps_controller_t* fps_ctrl, ge_u32_t target_fps,
ge_fps_get_ms_func_t get_ms, ge_fps_sleep_ms_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_ms_func_t call_get_ms;
ge_fps_sleep_ms_func_t call_sleep_ms;
};
#endif // __GE_FPS_H__

View File

View File

@@ -0,0 +1,16 @@
#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 struct {
ge_sleep_ms_func_t sleep_ms;
ge_get_ms_func_t get_ms;
ge_fps_controller_t fps_ctl;
} ge_timer_t;
#endif // __GE_TIMER_H__