feat: major engine overhaul - scene demo with lighting, sphere, ground

- Scene Demo: cube + sphere + ground plane with proper lighting
- Audio: XAudio2 dynamic loading + WAV playback
- Physics: expanded collision system
- Renderer: improved pipeline with materials
- UI: extended widget system
- VisualScript: enhanced serializer + block interactions
- ASM: optimized math/memory/profiling routines
- Window: mouse input + scroll support
This commit is contained in:
shuiyu1123 committed 2026-07-12 08:21:07 +08:00
1 parent 2653926d85
commit 0669d102ce
35 files changed
+1844 -314

No files matched your search

+80 -1
View File
@@ -1,6 +1,7 @@
#include "graphics_device.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
GraphicsDevice* graphics_device_create(void) {
return (GraphicsDevice*)calloc(1, sizeof(GraphicsDevice));
@@ -60,7 +61,7 @@ int graphics_device_init(GraphicsDevice* d, void* hwnd, uint32_t w, uint32_t h,
d->device->lpVtbl->CreateRenderTargetView(d->device, (ID3D11Resource*)back_buffer, NULL, &d->render_target);
if (back_buffer) back_buffer->lpVtbl->Release(back_buffer);
create_depth_stencil(d, w, h);
d->context->lpVtbl->OMSetRenderTargets(d->context, 1, &d->render_target, NULL);
d->context->lpVtbl->OMSetRenderTargets(d->context, 1, &d->render_target, d->depth_stencil);
D3D11_VIEWPORT vp = {0};
vp.Width = (float)w; vp.Height = (float)h;
vp.MaxDepth = 1.0f;
@@ -77,6 +78,19 @@ int graphics_device_init(GraphicsDevice* d, void* hwnd, uint32_t w, uint32_t h,
d->context->lpVtbl->OMSetDepthStencilState(d->context, default_dss, 0);
default_dss->lpVtbl->Release(default_dss);
}
// 设置默认光栅化状态:背面剔除 + 实体填充
D3D11_RASTERIZER_DESC rd = {0};
rd.FillMode = D3D11_FILL_SOLID;
rd.CullMode = D3D11_CULL_BACK;
rd.FrontCounterClockwise = FALSE;
rd.DepthClipEnable = TRUE;
ID3D11RasterizerState* default_rs = NULL;
d->device->lpVtbl->CreateRasterizerState(d->device, &rd, &default_rs);
if (default_rs) {
d->context->lpVtbl->RSSetState(d->context, default_rs);
default_rs->lpVtbl->Release(default_rs);
}
return 1;
}
@@ -108,3 +122,68 @@ void graphics_device_clear(GraphicsDevice* d, float r, float g, float b, float a
void graphics_device_present(GraphicsDevice* d) {
d->swap_chain->lpVtbl->Present(d->swap_chain, d->vsync ? 1 : 0, 0);
}
int graphics_device_save_screenshot(GraphicsDevice* d, const char* path) {
if (!d || !d->swap_chain || !d->device || !d->context) return 0;
ID3D11Texture2D* back = NULL;
HRESULT hr = d->swap_chain->lpVtbl->GetBuffer(d->swap_chain, 0, &IID_ID3D11Texture2D, (void**)&back);
if (FAILED(hr) || !back) return 0;
D3D11_TEXTURE2D_DESC desc = {0};
back->lpVtbl->GetDesc(back, &desc);
D3D11_TEXTURE2D_DESC sd = desc;
sd.BindFlags = 0;
sd.Usage = D3D11_USAGE_STAGING;
sd.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
sd.MiscFlags = 0;
ID3D11Texture2D* staging = NULL;
hr = d->device->lpVtbl->CreateTexture2D(d->device, &sd, NULL, &staging);
if (FAILED(hr) || !staging) { back->lpVtbl->Release(back); return 0; }
d->context->lpVtbl->CopyResource(d->context, (ID3D11Resource*)staging, (ID3D11Resource*)back);
D3D11_MAPPED_SUBRESOURCE map = {0};
hr = d->context->lpVtbl->Map(d->context, (ID3D11Resource*)staging, 0, D3D11_MAP_READ, 0, &map);
if (FAILED(hr)) { staging->lpVtbl->Release(staging); back->lpVtbl->Release(back); return 0; }
FILE* f = fopen(path, "wb");
if (!f) { d->context->lpVtbl->Unmap(d->context, (ID3D11Resource*)staging, 0); staging->lpVtbl->Release(staging); back->lpVtbl->Release(back); return 0; }
int w = (int)desc.Width;
int h = (int)desc.Height;
int row_size = ((w * 3 + 3) / 4) * 4;
int file_size = 54 + row_size * h;
unsigned char header[54] = {0};
header[0] = 'B'; header[1] = 'M';
memcpy(header + 2, &file_size, 4);
header[10] = 54;
header[14] = 40;
memcpy(header + 18, &w, 4);
memcpy(header + 22, &h, 4);
header[26] = 1;
header[28] = 24;
fwrite(header, 1, 54, f);
unsigned char* row = (unsigned char*)malloc(row_size);
for (int y = 0; y < h; y++) {
uint8_t* src = (uint8_t*)map.pData + (h - 1 - y) * map.RowPitch;
for (int x = 0; x < w; x++) {
// 后台缓冲区为 RGBA,BMP 24-bit 需要 BGR
row[x * 3 + 0] = src[x * 4 + 2];
row[x * 3 + 1] = src[x * 4 + 1];
row[x * 3 + 2] = src[x * 4 + 0];
}
fwrite(row, 1, row_size, f);
}
free(row);
fclose(f);
d->context->lpVtbl->Unmap(d->context, (ID3D11Resource*)staging, 0);
staging->lpVtbl->Release(staging);
back->lpVtbl->Release(back);
return 1;
}