diff --git a/Kiwano/2d/Action.h b/Kiwano/2d/Action.h index 06e1f9e4..c73cc1a8 100644 --- a/Kiwano/2d/Action.h +++ b/Kiwano/2d/Action.h @@ -102,7 +102,7 @@ namespace kiwano protected: virtual void Init(NodePtr const& target) {} - virtual void Update(NodePtr const& target, Duration dt) {} + virtual void Update(NodePtr const& target, Duration dt) { Complete(target); } void UpdateStep(NodePtr const& target, Duration dt); diff --git a/Kiwano/2d/ActionHelper.h b/Kiwano/2d/ActionHelper.h index 0f00caa3..37dd81a4 100644 --- a/Kiwano/2d/ActionHelper.h +++ b/Kiwano/2d/ActionHelper.h @@ -203,6 +203,12 @@ namespace kiwano return TweenHelper(new kiwano::Animation(0, frames)); } + static inline ActionHelper + Delay(Duration delay) + { + return ActionHelper(new kiwano::ActionDelay(delay)); + } + static inline ActionHelper Group(Array const& actions, bool sequence = true) { diff --git a/Kiwano/2d/ActionTween.cpp b/Kiwano/2d/ActionTween.cpp index 7dcbf7c7..f0da9825 100644 --- a/Kiwano/2d/ActionTween.cpp +++ b/Kiwano/2d/ActionTween.cpp @@ -477,4 +477,24 @@ namespace kiwano } } + + //------------------------------------------------------- + // ActionDelay + //------------------------------------------------------- + + ActionDelay::ActionDelay(Duration delay) + { + SetDelay(delay); + } + + ActionPtr ActionDelay::Clone() const + { + return new ActionDelay(delay_); + } + + ActionPtr ActionDelay::Reverse() const + { + return new ActionDelay(delay_); + } + } diff --git a/Kiwano/2d/ActionTween.h b/Kiwano/2d/ActionTween.h index 563c43f1..77af20f0 100644 --- a/Kiwano/2d/ActionTween.h +++ b/Kiwano/2d/ActionTween.h @@ -475,4 +475,21 @@ namespace kiwano Point start_pos_; GeometryPtr geo_; }; + + + // 延时动作 + class KGE_API ActionDelay + : public Action + { + public: + ActionDelay( + Duration delay /* 持续时长 */ + ); + + // 获取该动作的拷贝对象 + ActionPtr Clone() const override; + + // 获取该动作的倒转 + ActionPtr Reverse() const override; + }; } diff --git a/Kiwano/2d/GifImage.cpp b/Kiwano/2d/GifImage.cpp index 9cd96357..5abb1a3a 100644 --- a/Kiwano/2d/GifImage.cpp +++ b/Kiwano/2d/GifImage.cpp @@ -135,6 +135,7 @@ namespace kiwano if (frame_delay_ <= frame_elapsed_) { frame_delay_ -= frame_elapsed_; + frame_elapsed_ = 0; ComposeNextFrame(); } } diff --git a/Kiwano/Kiwano.vcxproj b/Kiwano/Kiwano.vcxproj index 24653fa3..4af801fb 100644 --- a/Kiwano/Kiwano.vcxproj +++ b/Kiwano/Kiwano.vcxproj @@ -14,6 +14,8 @@ + + @@ -137,6 +139,7 @@ + diff --git a/Kiwano/Kiwano.vcxproj.filters b/Kiwano/Kiwano.vcxproj.filters index 2adb6337..5bd79110 100644 --- a/Kiwano/Kiwano.vcxproj.filters +++ b/Kiwano/Kiwano.vcxproj.filters @@ -321,6 +321,12 @@ 2d + + imgui + + + imgui + @@ -491,5 +497,8 @@ 2d + + imgui + \ No newline at end of file diff --git a/Kiwano/imgui/ImGuiView.cpp b/Kiwano/imgui/ImGuiView.cpp index 5b3a354a..c7f1662b 100644 --- a/Kiwano/imgui/ImGuiView.cpp +++ b/Kiwano/imgui/ImGuiView.cpp @@ -1,7 +1,7 @@ // Copyright (C) 2019 Nomango #include "../kiwano-imgui.h" -#include "imgui_impl_dx11.h" +#include "imgui_impl.hpp" namespace kiwano { @@ -23,7 +23,7 @@ namespace kiwano void ImGuiView::DestroyComponent() { - ImGui_ImplDX11_Shutdown(); + ImGui_Impl_Shutdown(); ImGui::DestroyContext(); } @@ -53,15 +53,14 @@ namespace kiwano io.KeyMap[ImGuiKey_Y] = KeyCode::Y; io.KeyMap[ImGuiKey_Z] = KeyCode::Z; - ImGui_ImplDX11_Init( - Renderer::Instance().GetDeviceResources()->GetD3DDevice(), - Renderer::Instance().GetDeviceResources()->GetD3DDeviceContext() + ImGui_Impl_Init( + Renderer::Instance() ); } void ImGuiView::NewFrame() { - ImGui_ImplDX11_NewFrame(); + ImGui_Impl_NewFrame(); ImGuiIO& io = ImGui::GetIO(); KGE_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built!"); @@ -77,7 +76,7 @@ namespace kiwano { ImGui::Render(); - ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); + ImGui_Impl_RenderDrawData(ImGui::GetDrawData()); } ImGuiLayerPtr ImGuiView::CreateLayer(Scene* scene) diff --git a/Kiwano/imgui/imgui_impl.hpp b/Kiwano/imgui/imgui_impl.hpp new file mode 100644 index 00000000..e14c213d --- /dev/null +++ b/Kiwano/imgui/imgui_impl.hpp @@ -0,0 +1,29 @@ +// dear imgui: Renderer for Kiwano + +#pragma once + +#if !defined(KGE_USE_DIRECTX10) + +#include "imgui_impl_dx11.h" + +inline bool ImGui_Impl_Init(::kiwano::Renderer& renderer) { return ImGui_ImplDX11_Init(renderer.GetDeviceResources()->GetD3DDevice(), renderer.GetDeviceResources()->GetD3DDeviceContext()); } +inline void ImGui_Impl_Shutdown() { ImGui_ImplDX11_Shutdown(); } +inline void ImGui_Impl_NewFrame() { ImGui_ImplDX11_NewFrame(); } +inline void ImGui_Impl_RenderDrawData(ImDrawData* draw_data) { ImGui_ImplDX11_RenderDrawData(draw_data); } + +inline void ImGui_Impl_InvalidateDeviceObjects() { ImGui_ImplDX11_InvalidateDeviceObjects(); } +inline bool ImGui_Impl_CreateDeviceObjects() { return ImGui_ImplDX11_CreateDeviceObjects(); } + +#else + +#include "imgui_impl_dx10.h" + +inline bool ImGui_Impl_Init(::kiwano::Renderer& renderer) { return ImGui_ImplDX10_Init(renderer.GetDeviceResources()->GetD3DDeviceContext()); } +inline void ImGui_Impl_Shutdown() { ImGui_ImplDX10_Shutdown(); } +inline void ImGui_Impl_NewFrame() { ImGui_ImplDX10_NewFrame(); } +inline void ImGui_Impl_RenderDrawData(ImDrawData* draw_data) { ImGui_ImplDX10_RenderDrawData(draw_data); } + +inline void ImGui_Impl_InvalidateDeviceObjects() { ImGui_ImplDX10_InvalidateDeviceObjects(); } +inline bool ImGui_Impl_CreateDeviceObjects() { return ImGui_ImplDX10_CreateDeviceObjects(); } + +#endif diff --git a/Kiwano/imgui/imgui_impl_dx10.cpp b/Kiwano/imgui/imgui_impl_dx10.cpp new file mode 100644 index 00000000..0c5f000b --- /dev/null +++ b/Kiwano/imgui/imgui_impl_dx10.cpp @@ -0,0 +1,484 @@ +// dear imgui: Renderer for Kiwano (DirectX10) + +#include "../kiwano-imgui.h" +#include "imgui_impl_dx10.h" + +// DirectX +#include +#include +#include +#include + +#ifdef _MSC_VER +#pragma comment(lib, "d3dcompiler") // Automatically link with d3dcompiler.lib as we are using D3DCompile() below. +#endif + +// DirectX data +static ID3D10Device* g_pd3dDevice = NULL; +static IDXGIFactory* g_pFactory = NULL; +static ID3D10Buffer* g_pVB = NULL; +static ID3D10Buffer* g_pIB = NULL; +static ID3D10Blob* g_pVertexShaderBlob = NULL; +static ID3D10VertexShader* g_pVertexShader = NULL; +static ID3D10InputLayout* g_pInputLayout = NULL; +static ID3D10Buffer* g_pVertexConstantBuffer = NULL; +static ID3D10Blob* g_pPixelShaderBlob = NULL; +static ID3D10PixelShader* g_pPixelShader = NULL; +static ID3D10SamplerState* g_pFontSampler = NULL; +static ID3D10ShaderResourceView*g_pFontTextureView = NULL; +static ID3D10RasterizerState* g_pRasterizerState = NULL; +static ID3D10BlendState* g_pBlendState = NULL; +static ID3D10DepthStencilState* g_pDepthStencilState = NULL; +static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000; + +struct VERTEX_CONSTANT_BUFFER +{ + float mvp[4][4]; +}; + +// Render function +// (this used to be set in io.RenderDrawListsFn and called by ImGui::Render(), but you can now call this directly from your main loop) +void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data) +{ + ID3D10Device* ctx = g_pd3dDevice; + + // Create and grow vertex/index buffers if needed + if (!g_pVB || g_VertexBufferSize < draw_data->TotalVtxCount) + { + if (g_pVB) { g_pVB->Release(); g_pVB = NULL; } + g_VertexBufferSize = draw_data->TotalVtxCount + 5000; + D3D10_BUFFER_DESC desc; + memset(&desc, 0, sizeof(D3D10_BUFFER_DESC)); + desc.Usage = D3D10_USAGE_DYNAMIC; + desc.ByteWidth = g_VertexBufferSize * sizeof(ImDrawVert); + desc.BindFlags = D3D10_BIND_VERTEX_BUFFER; + desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + desc.MiscFlags = 0; + if (ctx->CreateBuffer(&desc, NULL, &g_pVB) < 0) + return; + } + + if (!g_pIB || g_IndexBufferSize < draw_data->TotalIdxCount) + { + if (g_pIB) { g_pIB->Release(); g_pIB = NULL; } + g_IndexBufferSize = draw_data->TotalIdxCount + 10000; + D3D10_BUFFER_DESC desc; + memset(&desc, 0, sizeof(D3D10_BUFFER_DESC)); + desc.Usage = D3D10_USAGE_DYNAMIC; + desc.ByteWidth = g_IndexBufferSize * sizeof(ImDrawIdx); + desc.BindFlags = D3D10_BIND_INDEX_BUFFER; + desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + if (ctx->CreateBuffer(&desc, NULL, &g_pIB) < 0) + return; + } + + // Copy and convert all vertices into a single contiguous buffer + ImDrawVert* vtx_dst = NULL; + ImDrawIdx* idx_dst = NULL; + g_pVB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&vtx_dst); + g_pIB->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&idx_dst); + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + memcpy(vtx_dst, cmd_list->VtxBuffer.Data, cmd_list->VtxBuffer.Size * sizeof(ImDrawVert)); + memcpy(idx_dst, cmd_list->IdxBuffer.Data, cmd_list->IdxBuffer.Size * sizeof(ImDrawIdx)); + vtx_dst += cmd_list->VtxBuffer.Size; + idx_dst += cmd_list->IdxBuffer.Size; + } + g_pVB->Unmap(); + g_pIB->Unmap(); + + // Setup orthographic projection matrix into our constant buffer + // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). + { + void* mapped_resource; + if (g_pVertexConstantBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mapped_resource) != S_OK) + return; + VERTEX_CONSTANT_BUFFER* constant_buffer = (VERTEX_CONSTANT_BUFFER*)mapped_resource; + float L = draw_data->DisplayPos.x; + float R = draw_data->DisplayPos.x + draw_data->DisplaySize.x; + float T = draw_data->DisplayPos.y; + float B = draw_data->DisplayPos.y + draw_data->DisplaySize.y; + float mvp[4][4] = + { + { 2.0f/(R-L), 0.0f, 0.0f, 0.0f }, + { 0.0f, 2.0f/(T-B), 0.0f, 0.0f }, + { 0.0f, 0.0f, 0.5f, 0.0f }, + { (R+L)/(L-R), (T+B)/(B-T), 0.5f, 1.0f }, + }; + memcpy(&constant_buffer->mvp, mvp, sizeof(mvp)); + g_pVertexConstantBuffer->Unmap(); + } + + // Backup DX state that will be modified to restore it afterwards (unfortunately this is very ugly looking and verbose. Close your eyes!) + struct BACKUP_DX10_STATE + { + UINT ScissorRectsCount, ViewportsCount; + D3D10_RECT ScissorRects[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; + D3D10_VIEWPORT Viewports[D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]; + ID3D10RasterizerState* RS; + ID3D10BlendState* BlendState; + FLOAT BlendFactor[4]; + UINT SampleMask; + UINT StencilRef; + ID3D10DepthStencilState* DepthStencilState; + ID3D10ShaderResourceView* PSShaderResource; + ID3D10SamplerState* PSSampler; + ID3D10PixelShader* PS; + ID3D10VertexShader* VS; + D3D10_PRIMITIVE_TOPOLOGY PrimitiveTopology; + ID3D10Buffer* IndexBuffer, *VertexBuffer, *VSConstantBuffer; + UINT IndexBufferOffset, VertexBufferStride, VertexBufferOffset; + DXGI_FORMAT IndexBufferFormat; + ID3D10InputLayout* InputLayout; + }; + BACKUP_DX10_STATE old; + old.ScissorRectsCount = old.ViewportsCount = D3D10_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE; + ctx->RSGetScissorRects(&old.ScissorRectsCount, old.ScissorRects); + ctx->RSGetViewports(&old.ViewportsCount, old.Viewports); + ctx->RSGetState(&old.RS); + ctx->OMGetBlendState(&old.BlendState, old.BlendFactor, &old.SampleMask); + ctx->OMGetDepthStencilState(&old.DepthStencilState, &old.StencilRef); + ctx->PSGetShaderResources(0, 1, &old.PSShaderResource); + ctx->PSGetSamplers(0, 1, &old.PSSampler); + ctx->PSGetShader(&old.PS); + ctx->VSGetShader(&old.VS); + ctx->VSGetConstantBuffers(0, 1, &old.VSConstantBuffer); + ctx->IAGetPrimitiveTopology(&old.PrimitiveTopology); + ctx->IAGetIndexBuffer(&old.IndexBuffer, &old.IndexBufferFormat, &old.IndexBufferOffset); + ctx->IAGetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); + ctx->IAGetInputLayout(&old.InputLayout); + + // Setup viewport + D3D10_VIEWPORT vp; + memset(&vp, 0, sizeof(D3D10_VIEWPORT)); + vp.Width = (UINT)draw_data->DisplaySize.x; + vp.Height = (UINT)draw_data->DisplaySize.y; + vp.MinDepth = 0.0f; + vp.MaxDepth = 1.0f; + vp.TopLeftX = vp.TopLeftY = 0; + ctx->RSSetViewports(1, &vp); + + // Bind shader and vertex buffers + unsigned int stride = sizeof(ImDrawVert); + unsigned int offset = 0; + ctx->IASetInputLayout(g_pInputLayout); + ctx->IASetVertexBuffers(0, 1, &g_pVB, &stride, &offset); + ctx->IASetIndexBuffer(g_pIB, sizeof(ImDrawIdx) == 2 ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_R32_UINT, 0); + ctx->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + ctx->VSSetShader(g_pVertexShader); + ctx->VSSetConstantBuffers(0, 1, &g_pVertexConstantBuffer); + ctx->PSSetShader(g_pPixelShader); + ctx->PSSetSamplers(0, 1, &g_pFontSampler); + + // Setup render state + const float blend_factor[4] = { 0.f, 0.f, 0.f, 0.f }; + ctx->OMSetBlendState(g_pBlendState, blend_factor, 0xffffffff); + ctx->OMSetDepthStencilState(g_pDepthStencilState, 0); + ctx->RSSetState(g_pRasterizerState); + + // Render command lists + int vtx_offset = 0; + int idx_offset = 0; + ImVec2 pos = draw_data->DisplayPos; + for (int n = 0; n < draw_data->CmdListsCount; n++) + { + const ImDrawList* cmd_list = draw_data->CmdLists[n]; + for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.Size; cmd_i++) + { + const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i]; + if (pcmd->UserCallback) + { + // User callback (registered via ImDrawList::AddCallback) + pcmd->UserCallback(cmd_list, pcmd); + } + else + { + // Apply scissor/clipping rectangle + const D3D10_RECT r = { (LONG)(pcmd->ClipRect.x - pos.x), (LONG)(pcmd->ClipRect.y - pos.y), (LONG)(pcmd->ClipRect.z - pos.x), (LONG)(pcmd->ClipRect.w - pos.y)}; + ctx->RSSetScissorRects(1, &r); + + // Bind texture, Draw + ID3D10ShaderResourceView* texture_srv = (ID3D10ShaderResourceView*)pcmd->TextureId; + ctx->PSSetShaderResources(0, 1, &texture_srv); + ctx->DrawIndexed(pcmd->ElemCount, idx_offset, vtx_offset); + } + idx_offset += pcmd->ElemCount; + } + vtx_offset += cmd_list->VtxBuffer.Size; + } + + // Restore modified DX state + ctx->RSSetScissorRects(old.ScissorRectsCount, old.ScissorRects); + ctx->RSSetViewports(old.ViewportsCount, old.Viewports); + ctx->RSSetState(old.RS); if (old.RS) old.RS->Release(); + ctx->OMSetBlendState(old.BlendState, old.BlendFactor, old.SampleMask); if (old.BlendState) old.BlendState->Release(); + ctx->OMSetDepthStencilState(old.DepthStencilState, old.StencilRef); if (old.DepthStencilState) old.DepthStencilState->Release(); + ctx->PSSetShaderResources(0, 1, &old.PSShaderResource); if (old.PSShaderResource) old.PSShaderResource->Release(); + ctx->PSSetSamplers(0, 1, &old.PSSampler); if (old.PSSampler) old.PSSampler->Release(); + ctx->PSSetShader(old.PS); if (old.PS) old.PS->Release(); + ctx->VSSetShader(old.VS); if (old.VS) old.VS->Release(); + ctx->VSSetConstantBuffers(0, 1, &old.VSConstantBuffer); if (old.VSConstantBuffer) old.VSConstantBuffer->Release(); + ctx->IASetPrimitiveTopology(old.PrimitiveTopology); + ctx->IASetIndexBuffer(old.IndexBuffer, old.IndexBufferFormat, old.IndexBufferOffset); if (old.IndexBuffer) old.IndexBuffer->Release(); + ctx->IASetVertexBuffers(0, 1, &old.VertexBuffer, &old.VertexBufferStride, &old.VertexBufferOffset); if (old.VertexBuffer) old.VertexBuffer->Release(); + ctx->IASetInputLayout(old.InputLayout); if (old.InputLayout) old.InputLayout->Release(); +} + +static void ImGui_ImplDX10_CreateFontsTexture() +{ + // Build texture atlas + ImGuiIO& io = ImGui::GetIO(); + unsigned char* pixels; + int width, height; + io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); + + // Upload texture to graphics system + { + D3D10_TEXTURE2D_DESC desc; + ZeroMemory(&desc, sizeof(desc)); + desc.Width = width; + desc.Height = height; + desc.MipLevels = 1; + desc.ArraySize = 1; + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + desc.SampleDesc.Count = 1; + desc.Usage = D3D10_USAGE_DEFAULT; + desc.BindFlags = D3D10_BIND_SHADER_RESOURCE; + desc.CPUAccessFlags = 0; + + ID3D10Texture2D *pTexture = NULL; + D3D10_SUBRESOURCE_DATA subResource; + subResource.pSysMem = pixels; + subResource.SysMemPitch = desc.Width * 4; + subResource.SysMemSlicePitch = 0; + g_pd3dDevice->CreateTexture2D(&desc, &subResource, &pTexture); + + // Create texture view + D3D10_SHADER_RESOURCE_VIEW_DESC srv_desc; + ZeroMemory(&srv_desc, sizeof(srv_desc)); + srv_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + srv_desc.ViewDimension = D3D10_SRV_DIMENSION_TEXTURE2D; + srv_desc.Texture2D.MipLevels = desc.MipLevels; + srv_desc.Texture2D.MostDetailedMip = 0; + g_pd3dDevice->CreateShaderResourceView(pTexture, &srv_desc, &g_pFontTextureView); + pTexture->Release(); + } + + // Store our identifier + io.Fonts->TexID = (ImTextureID)g_pFontTextureView; + + // Create texture sampler + { + D3D10_SAMPLER_DESC desc; + ZeroMemory(&desc, sizeof(desc)); + desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR; + desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP; + desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP; + desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP; + desc.MipLODBias = 0.f; + desc.ComparisonFunc = D3D10_COMPARISON_ALWAYS; + desc.MinLOD = 0.f; + desc.MaxLOD = 0.f; + g_pd3dDevice->CreateSamplerState(&desc, &g_pFontSampler); + } +} + +bool ImGui_ImplDX10_CreateDeviceObjects() +{ + if (!g_pd3dDevice) + return false; + if (g_pFontSampler) + ImGui_ImplDX10_InvalidateDeviceObjects(); + + // By using D3DCompile() from / d3dcompiler.lib, we introduce a dependency to a given version of d3dcompiler_XX.dll (see D3DCOMPILER_DLL_A) + // If you would like to use this DX10 sample code but remove this dependency you can: + // 1) compile once, save the compiled shader blobs into a file or source code and pass them to CreateVertexShader()/CreatePixelShader() [preferred solution] + // 2) use code to detect any version of the DLL and grab a pointer to D3DCompile from the DLL. + // See https://github.com/ocornut/imgui/pull/638 for sources and details. + + // Create the vertex shader + { + static const char* vertexShader = + "cbuffer vertexBuffer : register(b0) \ + {\ + float4x4 ProjectionMatrix; \ + };\ + struct VS_INPUT\ + {\ + float2 pos : POSITION;\ + float4 col : COLOR0;\ + float2 uv : TEXCOORD0;\ + };\ + \ + struct PS_INPUT\ + {\ + float4 pos : SV_POSITION;\ + float4 col : COLOR0;\ + float2 uv : TEXCOORD0;\ + };\ + \ + PS_INPUT main(VS_INPUT input)\ + {\ + PS_INPUT output;\ + output.pos = mul( ProjectionMatrix, float4(input.pos.xy, 0.f, 1.f));\ + output.col = input.col;\ + output.uv = input.uv;\ + return output;\ + }"; + + D3DCompile(vertexShader, strlen(vertexShader), NULL, NULL, NULL, "main", "vs_4_0", 0, 0, &g_pVertexShaderBlob, NULL); + if (g_pVertexShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! + return false; + if (g_pd3dDevice->CreateVertexShader((DWORD*)g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pVertexShader) != S_OK) + return false; + + // Create the input layout + D3D10_INPUT_ELEMENT_DESC local_layout[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->pos), D3D10_INPUT_PER_VERTEX_DATA, 0 }, + { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, (size_t)(&((ImDrawVert*)0)->uv), D3D10_INPUT_PER_VERTEX_DATA, 0 }, + { "COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, (size_t)(&((ImDrawVert*)0)->col), D3D10_INPUT_PER_VERTEX_DATA, 0 }, + }; + if (g_pd3dDevice->CreateInputLayout(local_layout, 3, g_pVertexShaderBlob->GetBufferPointer(), g_pVertexShaderBlob->GetBufferSize(), &g_pInputLayout) != S_OK) + return false; + + // Create the constant buffer + { + D3D10_BUFFER_DESC desc; + desc.ByteWidth = sizeof(VERTEX_CONSTANT_BUFFER); + desc.Usage = D3D10_USAGE_DYNAMIC; + desc.BindFlags = D3D10_BIND_CONSTANT_BUFFER; + desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE; + desc.MiscFlags = 0; + g_pd3dDevice->CreateBuffer(&desc, NULL, &g_pVertexConstantBuffer); + } + } + + // Create the pixel shader + { + static const char* pixelShader = + "struct PS_INPUT\ + {\ + float4 pos : SV_POSITION;\ + float4 col : COLOR0;\ + float2 uv : TEXCOORD0;\ + };\ + sampler sampler0;\ + Texture2D texture0;\ + \ + float4 main(PS_INPUT input) : SV_Target\ + {\ + float4 out_col = input.col * texture0.Sample(sampler0, input.uv); \ + return out_col; \ + }"; + + D3DCompile(pixelShader, strlen(pixelShader), NULL, NULL, NULL, "main", "ps_4_0", 0, 0, &g_pPixelShaderBlob, NULL); + if (g_pPixelShaderBlob == NULL) // NB: Pass ID3D10Blob* pErrorBlob to D3DCompile() to get error showing in (const char*)pErrorBlob->GetBufferPointer(). Make sure to Release() the blob! + return false; + if (g_pd3dDevice->CreatePixelShader((DWORD*)g_pPixelShaderBlob->GetBufferPointer(), g_pPixelShaderBlob->GetBufferSize(), &g_pPixelShader) != S_OK) + return false; + } + + // Create the blending setup + { + D3D10_BLEND_DESC desc; + ZeroMemory(&desc, sizeof(desc)); + desc.AlphaToCoverageEnable = false; + desc.BlendEnable[0] = true; + desc.SrcBlend = D3D10_BLEND_SRC_ALPHA; + desc.DestBlend = D3D10_BLEND_INV_SRC_ALPHA; + desc.BlendOp = D3D10_BLEND_OP_ADD; + desc.SrcBlendAlpha = D3D10_BLEND_INV_SRC_ALPHA; + desc.DestBlendAlpha = D3D10_BLEND_ZERO; + desc.BlendOpAlpha = D3D10_BLEND_OP_ADD; + desc.RenderTargetWriteMask[0] = D3D10_COLOR_WRITE_ENABLE_ALL; + g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState); + } + + // Create the rasterizer state + { + D3D10_RASTERIZER_DESC desc; + ZeroMemory(&desc, sizeof(desc)); + desc.FillMode = D3D10_FILL_SOLID; + desc.CullMode = D3D10_CULL_NONE; + desc.ScissorEnable = true; + desc.DepthClipEnable = true; + g_pd3dDevice->CreateRasterizerState(&desc, &g_pRasterizerState); + } + + // Create depth-stencil State + { + D3D10_DEPTH_STENCIL_DESC desc; + ZeroMemory(&desc, sizeof(desc)); + desc.DepthEnable = false; + desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL; + desc.DepthFunc = D3D10_COMPARISON_ALWAYS; + desc.StencilEnable = false; + desc.FrontFace.StencilFailOp = desc.FrontFace.StencilDepthFailOp = desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP; + desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS; + desc.BackFace = desc.FrontFace; + g_pd3dDevice->CreateDepthStencilState(&desc, &g_pDepthStencilState); + } + + ImGui_ImplDX10_CreateFontsTexture(); + + return true; +} + +void ImGui_ImplDX10_InvalidateDeviceObjects() +{ + if (!g_pd3dDevice) + return; + + if (g_pFontSampler) { g_pFontSampler->Release(); g_pFontSampler = NULL; } + if (g_pFontTextureView) { g_pFontTextureView->Release(); g_pFontTextureView = NULL; ImGui::GetIO().Fonts->TexID = NULL; } // We copied g_pFontTextureView to io.Fonts->TexID so let's clear that as well. + if (g_pIB) { g_pIB->Release(); g_pIB = NULL; } + if (g_pVB) { g_pVB->Release(); g_pVB = NULL; } + + if (g_pBlendState) { g_pBlendState->Release(); g_pBlendState = NULL; } + if (g_pDepthStencilState) { g_pDepthStencilState->Release(); g_pDepthStencilState = NULL; } + if (g_pRasterizerState) { g_pRasterizerState->Release(); g_pRasterizerState = NULL; } + if (g_pPixelShader) { g_pPixelShader->Release(); g_pPixelShader = NULL; } + if (g_pPixelShaderBlob) { g_pPixelShaderBlob->Release(); g_pPixelShaderBlob = NULL; } + if (g_pVertexConstantBuffer) { g_pVertexConstantBuffer->Release(); g_pVertexConstantBuffer = NULL; } + if (g_pInputLayout) { g_pInputLayout->Release(); g_pInputLayout = NULL; } + if (g_pVertexShader) { g_pVertexShader->Release(); g_pVertexShader = NULL; } + if (g_pVertexShaderBlob) { g_pVertexShaderBlob->Release(); g_pVertexShaderBlob = NULL; } +} + +bool ImGui_ImplDX10_Init(ID3D10Device* device) +{ + ImGuiIO& io = ImGui::GetIO(); + io.BackendRendererName = "imgui_impl_dx10"; + + // Get factory from device + IDXGIDevice* pDXGIDevice = NULL; + IDXGIAdapter* pDXGIAdapter = NULL; + IDXGIFactory* pFactory = NULL; + + if (device->QueryInterface(IID_PPV_ARGS(&pDXGIDevice)) == S_OK) + if (pDXGIDevice->GetParent(IID_PPV_ARGS(&pDXGIAdapter)) == S_OK) + if (pDXGIAdapter->GetParent(IID_PPV_ARGS(&pFactory)) == S_OK) + { + g_pd3dDevice = device; + g_pFactory = pFactory; + } + if (pDXGIDevice) pDXGIDevice->Release(); + if (pDXGIAdapter) pDXGIAdapter->Release(); + + return true; +} + +void ImGui_ImplDX10_Shutdown() +{ + ImGui_ImplDX10_InvalidateDeviceObjects(); + if (g_pFactory) { g_pFactory->Release(); g_pFactory = NULL; } + g_pd3dDevice = NULL; +} + +void ImGui_ImplDX10_NewFrame() +{ + if (!g_pFontSampler) + ImGui_ImplDX10_CreateDeviceObjects(); +} diff --git a/Kiwano/imgui/imgui_impl_dx10.h b/Kiwano/imgui/imgui_impl_dx10.h new file mode 100644 index 00000000..cf129d61 --- /dev/null +++ b/Kiwano/imgui/imgui_impl_dx10.h @@ -0,0 +1,14 @@ +// dear imgui: Renderer for Kiwano (DirectX10) + +#pragma once + +struct ID3D10Device; + +IMGUI_IMPL_API bool ImGui_ImplDX10_Init(ID3D10Device* device); +IMGUI_IMPL_API void ImGui_ImplDX10_Shutdown(); +IMGUI_IMPL_API void ImGui_ImplDX10_NewFrame(); +IMGUI_IMPL_API void ImGui_ImplDX10_RenderDrawData(ImDrawData* draw_data); + +// Use if you want to reset your rendering device without losing ImGui state. +IMGUI_IMPL_API void ImGui_ImplDX10_InvalidateDeviceObjects(); +IMGUI_IMPL_API bool ImGui_ImplDX10_CreateDeviceObjects(); diff --git a/Kiwano/renderer/D3D10DeviceResources.cpp b/Kiwano/renderer/D3D10DeviceResources.cpp index 05e9defd..11c967c4 100644 --- a/Kiwano/renderer/D3D10DeviceResources.cpp +++ b/Kiwano/renderer/D3D10DeviceResources.cpp @@ -342,7 +342,7 @@ namespace kiwano if (SUCCEEDED(hr)) { ID3D10RenderTargetView* main_view = d3d_rt_view_.Get(); - d3d_device_context_->OMSetRenderTargets(1, &main_view, d3d_ds_view_.Get()); + d3d_device_->OMSetRenderTargets(1, &main_view, d3d_ds_view_.Get()); } } @@ -402,19 +402,18 @@ namespace kiwano return hr; } - void D3D10DeviceResources::SetLogicalSize(Size logical_size) + HRESULT D3D10DeviceResources::SetLogicalSize(Size logical_size) { if (logical_size_ != logical_size) { logical_size_ = logical_size; - ThrowIfFailed( - CreateWindowSizeDependentResources() - ); + return CreateWindowSizeDependentResources(); } + return S_OK; } - void D3D10DeviceResources::SetDpi(float dpi) + HRESULT D3D10DeviceResources::SetDpi(float dpi) { if (dpi != dpi_) { @@ -428,10 +427,9 @@ namespace kiwano GetD2DDeviceContext()->SetDpi(dpi_, dpi_); - ThrowIfFailed( - CreateWindowSizeDependentResources() - ); + return CreateWindowSizeDependentResources(); } + return S_OK; } } diff --git a/Kiwano/renderer/D3D10DeviceResources.h b/Kiwano/renderer/D3D10DeviceResources.h index 17420232..e8540dde 100644 --- a/Kiwano/renderer/D3D10DeviceResources.h +++ b/Kiwano/renderer/D3D10DeviceResources.h @@ -20,9 +20,10 @@ #pragma once +#include "../macros.h" + #if defined(KGE_USE_DIRECTX10) -#include "helper.hpp" #include "D2DDeviceResources.h" #include @@ -39,17 +40,18 @@ namespace kiwano HRESULT HandleDeviceLost(); - void SetLogicalSize( + HRESULT SetLogicalSize( Size logical_size ); - void SetDpi( + HRESULT SetDpi( float dpi ); void DiscardResources(); inline ID3D10Device* GetD3DDevice() const { return d3d_device_.Get(); } + inline ID3D10Device* GetD3DDeviceContext() const { return d3d_device_.Get(); } inline ID3D10RenderTargetView* GetD3DRenderTargetView() const { return d3d_rt_view_.Get(); } inline ID3D10DepthStencilView* GetD3DDepthStencilView() const { return d3d_ds_view_.Get(); } inline IDXGIFactory* GetDXGIFactory() const { return dxgi_factory_.Get(); } diff --git a/Kiwano/renderer/D3D11DeviceResources.h b/Kiwano/renderer/D3D11DeviceResources.h index 2cec9854..1ffe41e9 100644 --- a/Kiwano/renderer/D3D11DeviceResources.h +++ b/Kiwano/renderer/D3D11DeviceResources.h @@ -20,9 +20,10 @@ #pragma once +#include "../macros.h" + #if !defined(KGE_USE_DIRECTX10) -#include "helper.hpp" #include "D2DDeviceResources.h" #include diff --git a/samples/Samples/Demo1.h b/samples/Samples/Demo1.h index 48266fc7..422ecbb5 100644 --- a/samples/Samples/Demo1.h +++ b/samples/Samples/Demo1.h @@ -14,25 +14,42 @@ public: Demo1() { - // 创建文本 - TextPtr text = new Text(L"Hello Kiwano!"); - // 设置节点大小为文字布局大小 - text->SetSize(text->GetLayoutSize()); - // 让文本显示在屏幕中央 - text->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); - text->SetAnchor(0.5, 0.5); - // 添加到场景 - this->AddChild(text); + // 创建人物图片 + ImagePtr man_image = new Image(L"res/man.png"); - // 执行动画 - text->AddAction( - Tween::Multiple({ // Action5: 同时执行 Action1 和 Action4, 并无限循环 - Tween::RotateBy(60).SetDuration(1000), // Action1: 1秒旋转 60 度 - Tween::Group({ // Action4: 顺序执行 Action2 和 Action3 - Tween::FadeOut(500), // Action2: 500毫秒淡出动画 - Tween::FadeIn(500) // Action3: 500毫秒淡入动画 + // 缓动方程 + auto ease_functions = { + Ease::Linear, // 线性变化 + Ease::EaseInOut, // 变化过程中有缓冲 + Ease::ExpoInOut, // 在开始和结束阶段非常慢,但过程非常快 + Ease::BounceInOut, // 在开始和结束阶段均赋予弹性 + Ease::BackInOut // 开始和结束阶段均有一个短暂的反方向运动 + }; + + float height = 100.f; + for (auto& func : ease_functions) + { + SpritePtr man = new Sprite(man_image); + man->SetPosition(100, height); + man->SetScale(0.5f, 0.3f); + this->AddChild(man); + + // 重置人物位置函数 + auto reset_pos = [ptr = man.Get()]() { ptr->Move(-350, 0); }; + + // 执行动画 + man->AddAction( + Tween::Group({ // Tween::Group 组合动画 + Tween::MoveBy(Point{ 350, 0 }) // Tween::MoveBy 横向位移 350 像素 + .SetDuration(4000) // 设置位移时间为 4 秒 + .SetEaseFunc(func), // 设置缓动函数 + Tween::Delay(1000) // Tween::Delay 延迟 1 秒 }) - }).SetLoops(-1) - ); + .SetLoops(-1) // 无限循环执行 + .SetLoopDoneCallback(reset_pos) // 设置每次循环结束都重置人物位置 + ); + + height += 60.f; + } } }; diff --git a/samples/Samples/Demo2.h b/samples/Samples/Demo2.h index 152d0444..ae591942 100644 --- a/samples/Samples/Demo2.h +++ b/samples/Samples/Demo2.h @@ -3,18 +3,18 @@ #pragma once #include "common.h" -// 怪物 -KGE_DECLARE_SMART_PTR(Monster); -class Monster - : public Sprite +// 角色 +KGE_DECLARE_SMART_PTR(Hero); +class Hero + : public GifImage { public: - Monster() + Hero() { // 加载图片 - Load(L"res/akushu.png"); - // 缩小图片 - SetScale(0.7f); + Load(L"res/Kusanagi.gif"); + // 设置 GIF 动图无限循环 + SetLoopCount(-1); } // 每帧渲染前执行 OnUpdate @@ -22,6 +22,7 @@ public: { // 获取输入设备 auto& input = Input::Instance(); + // 按下左右键 if (input.IsDown(KeyCode::Left)) { @@ -42,7 +43,7 @@ public: this->Move(0, 2); } - // 按下鼠标左键,顺时针旋转怪物 + // 按下鼠标左键,顺时针旋转角色 if (input.IsDown(MouseButton::Left)) { // 获取当前旋转角度 @@ -51,7 +52,7 @@ public: this->SetRotation(rotation + 2); } - // 点击鼠标右键,隐藏或显示怪物 + // 点击鼠标右键,隐藏或显示角色 if (input.WasPressed(MouseButton::Right)) { // 获取当前显示状态 @@ -73,11 +74,11 @@ public: Demo2() { - // 创建怪物 - MonsterPtr monster = new Monster; + // 创建角色 + HeroPtr hero = new Hero; // 在屏幕上居中显示 - monster->SetAnchor(0.5f, 0.5f); - monster->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); + hero->SetAnchor(0.5f, 0.5f); + hero->SetPosition(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2); // 创建说明文字 TextPtr text = new Text(L"按上下左右键移动\n按鼠标左键旋转\n点击鼠标右键隐藏"); @@ -89,7 +90,7 @@ public: text->SetAlignment(TextAlign::Center); // 添加到场景 - this->AddChild(monster); + this->AddChild(hero); this->AddChild(text); } }; diff --git a/samples/Samples/Demo4.h b/samples/Samples/Demo4.h index 482ad600..0d6dbb89 100644 --- a/samples/Samples/Demo4.h +++ b/samples/Samples/Demo4.h @@ -43,8 +43,8 @@ public: // 执行动画 AddAction( Tween::Animation(stand_frames) - .SetDuration(1000) - .SetLoops(-1) + .SetDuration(1000) + .SetLoops(-1) ); // 添加按键监听 @@ -94,8 +94,8 @@ public: // 执行跑步动画 AddAction( Tween::Animation(run_frames) - .SetDuration(500) - .SetLoops(-1) + .SetDuration(500) + .SetLoops(-1) ); } @@ -124,8 +124,8 @@ public: // 执行站立动画 AddAction( Tween::Animation(stand_frames) - .SetDuration(1000) - .SetLoops(-1) + .SetDuration(1000) + .SetLoops(-1) ); } } @@ -170,6 +170,10 @@ public: Demo4() { + // 创建背景 + SpritePtr bg = new Sprite(L"res/spring_forest.jpg"); + bg->SetSize(GetSize()); + // 创建老虎 TigerPtr tiger = new Tiger; // 在屏幕上居中显示 @@ -186,6 +190,7 @@ public: text->SetAlignment(TextAlign::Center); // 添加到场景 + this->AddChild(bg); this->AddChild(tiger); this->AddChild(text); } diff --git a/samples/Samples/res/Kusanagi.gif b/samples/Samples/res/Kusanagi.gif new file mode 100644 index 00000000..a412cf1a Binary files /dev/null and b/samples/Samples/res/Kusanagi.gif differ diff --git a/samples/Samples/res/man.png b/samples/Samples/res/man.png new file mode 100644 index 00000000..71ecf69c Binary files /dev/null and b/samples/Samples/res/man.png differ diff --git a/samples/Samples/res/spring_forest.jpg b/samples/Samples/res/spring_forest.jpg new file mode 100644 index 00000000..72268be5 Binary files /dev/null and b/samples/Samples/res/spring_forest.jpg differ