MiMoEngine ASM: x86-64 MASM汇编+C混合架构游戏引擎
核心热路径(数学/内存/字符串/性能分析)用MASM汇编(SSE/AVX2) 外围系统(窗口/渲染/ECS/音频/UI/脚本)用C语言 含自研编辑器、VisualScript积木编程、中文渲染(glyph_atlas)
This commit is contained in:
commit
2d56cb3bc9
91 files changed
+9988
No files matched your search
@@ -0,0 +1,100 @@
|
||||
#include "world.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
World* world_create(void) {
|
||||
World* w = (World*)calloc(1, sizeof(World));
|
||||
w->next_entity = 1;
|
||||
return w;
|
||||
}
|
||||
|
||||
void world_destroy(World* world) {
|
||||
if (!world) return;
|
||||
for (uint32_t i = 0; i < world->entity_count; i++) {
|
||||
Entity e = world->entities[i];
|
||||
for (uint32_t c = 0; c < world->component_count; c++) {
|
||||
if (world->components[c][e] && world->component_tables[c].destroy) {
|
||||
world->component_tables[c].destroy(world->components[c][e]);
|
||||
}
|
||||
free(world->components[c][e]);
|
||||
}
|
||||
}
|
||||
free(world);
|
||||
}
|
||||
|
||||
Entity world_create_entity(World* world) {
|
||||
if (world->entity_count >= MAX_ENTITIES) return 0;
|
||||
Entity e = world->next_entity++;
|
||||
world->entities[world->entity_count++] = e;
|
||||
world->entity_masks[e] = 0;
|
||||
return e;
|
||||
}
|
||||
|
||||
void world_destroy_entity(World* world, Entity entity) {
|
||||
for (uint32_t c = 0; c < world->component_count; c++) {
|
||||
if (world->components[c][entity] && world->component_tables[c].destroy) {
|
||||
world->component_tables[c].destroy(world->components[c][entity]);
|
||||
}
|
||||
free(world->components[c][entity]);
|
||||
world->components[c][entity] = NULL;
|
||||
}
|
||||
world->entity_masks[entity] = 0;
|
||||
|
||||
for (uint32_t i = 0; i < world->entity_count; i++) {
|
||||
if (world->entities[i] == entity) {
|
||||
world->entities[i] = world->entities[--world->entity_count];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int world_entity_alive(World* world, Entity entity) {
|
||||
for (uint32_t i = 0; i < world->entity_count; i++) {
|
||||
if (world->entities[i] == entity) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
ComponentType world_register_component(World* world, uint32_t size, ComponentVTable vtable) {
|
||||
ComponentType type = world->component_count++;
|
||||
world->component_sizes[type] = size;
|
||||
world->component_tables[type] = vtable;
|
||||
return type;
|
||||
}
|
||||
|
||||
void* world_add_component(World* world, Entity entity, ComponentType type) {
|
||||
if (world->components[type][entity]) return world->components[type][entity];
|
||||
void* comp = calloc(1, world->component_sizes[type]);
|
||||
world->components[type][entity] = comp;
|
||||
world->entity_masks[entity] |= (1ULL << type);
|
||||
return comp;
|
||||
}
|
||||
|
||||
void* world_get_component(World* world, Entity entity, ComponentType type) {
|
||||
return world->components[type][entity];
|
||||
}
|
||||
|
||||
void world_remove_component(World* world, Entity entity, ComponentType type) {
|
||||
if (world->components[type][entity]) {
|
||||
if (world->component_tables[type].destroy) {
|
||||
world->component_tables[type].destroy(world->components[type][entity]);
|
||||
}
|
||||
free(world->components[type][entity]);
|
||||
world->components[type][entity] = NULL;
|
||||
world->entity_masks[entity] &= ~(1ULL << type);
|
||||
}
|
||||
}
|
||||
|
||||
int world_has_component(World* world, Entity entity, ComponentType type) {
|
||||
return (world->entity_masks[entity] & (1ULL << type)) != 0;
|
||||
}
|
||||
|
||||
void world_each(World* world, uint64_t mask, SystemFunc func, void* data) {
|
||||
for (uint32_t i = 0; i < world->entity_count; i++) {
|
||||
Entity e = world->entities[i];
|
||||
if ((world->entity_masks[e] & mask) == mask) {
|
||||
func(world, e, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
// ============================================================================
|
||||
// world.h - 实体组件系统 (ECS)
|
||||
// ============================================================================
|
||||
|
||||
#include <stdint.h>
|
||||
#include "../include/engine_api.h"
|
||||
|
||||
#define MAX_ENTITIES 4096
|
||||
#define MAX_COMPONENTS 32
|
||||
|
||||
typedef uint32_t Entity;
|
||||
typedef uint32_t ComponentType;
|
||||
|
||||
// 组件接口
|
||||
typedef struct {
|
||||
void (*destroy)(void* component);
|
||||
} ComponentVTable;
|
||||
|
||||
// 世界
|
||||
typedef struct {
|
||||
Entity entities[MAX_ENTITIES];
|
||||
uint32_t entity_count;
|
||||
uint32_t next_entity;
|
||||
|
||||
void* components[MAX_COMPONENTS][MAX_ENTITIES];
|
||||
ComponentVTable component_tables[MAX_COMPONENTS];
|
||||
uint32_t component_sizes[MAX_COMPONENTS];
|
||||
uint32_t component_count;
|
||||
|
||||
uint64_t entity_masks[MAX_ENTITIES];
|
||||
} World;
|
||||
|
||||
// 创建/销毁
|
||||
World* world_create(void);
|
||||
void world_destroy(World* world);
|
||||
|
||||
// 实体
|
||||
Entity world_create_entity(World* world);
|
||||
void world_destroy_entity(World* world, Entity entity);
|
||||
int world_entity_alive(World* world, Entity entity);
|
||||
|
||||
// 组件
|
||||
ComponentType world_register_component(World* world, uint32_t size, ComponentVTable vtable);
|
||||
void* world_add_component(World* world, Entity entity, ComponentType type);
|
||||
void* world_get_component(World* world, Entity entity, ComponentType type);
|
||||
void world_remove_component(World* world, Entity entity, ComponentType type);
|
||||
int world_has_component(World* world, Entity entity, ComponentType type);
|
||||
|
||||
// 系统遍历
|
||||
typedef void (*SystemFunc)(World* world, Entity entity, void* data);
|
||||
void world_each(World* world, uint64_t mask, SystemFunc func, void* data);
|
||||
Reference in new issue
Block a user