diff options
author | chai <chaifix@163.com> | 2021-10-31 14:27:26 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-10-31 14:27:26 +0800 |
commit | 601442f94fc0dcfdc5a117c5f87d90b156d53045 (patch) | |
tree | b006bcd6a28a965a900c64f4716007fcb45eee98 | |
parent | 26f05c6e3dcac9995345fb5a2b031be7e3ea79e9 (diff) |
+static initiator
29 files changed, 818 insertions, 67 deletions
diff --git a/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Shader.lua b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Shader.lua new file mode 100644 index 0000000..c64d526 --- /dev/null +++ b/Data/DefaultContent/Libraries/GameLab/Engine/Rendering/Shader.lua @@ -0,0 +1,11 @@ +local Shader = GameLab.Engine.Rendering.Shader or {} + +-- @Shader.CreateFromFile +-- 从文件ä¸åˆ›å»ºShader +-- @param path:string 路径 +-- @return shader 创建的shader +Shader.CreateFromFile = function( path ) + +end + +return Shader
\ No newline at end of file diff --git a/Data/Resources/Shaders/Editor-Text.glsl b/Data/Resources/Shaders/Editor-Text.glsl new file mode 100644 index 0000000..7a88fcf --- /dev/null +++ b/Data/Resources/Shaders/Editor-Text.glsl @@ -0,0 +1,34 @@ +#version 330 core + +uniform mat4 gamelab_mat_mvp; +uniform sampler2D uiTex; +uniform vec2 uiText_ST; + +VSH_BEGIN +layout (location = 0) in vec2 vPos; +layout (location = 1) in vec2 vUV; + +out vec2 uv; + +void main() +{ + vec4 clip = gamelab_mat_mvp * vec4(vPos, -1, 1.0); + gl_Position = clip; + uv = vUV; +} + +VSH_END + +FSH_BEGIN +in vec2 uv; + +out vec4 FragColor; + +void main() +{ + vec2 uv = vec2(uv.x, 1 - uv.y); + vec4 sampled = vec4(1,1,1,texture(uiTex, uv).r); + FragColor = sampled; +} + +FSH_END
\ No newline at end of file diff --git a/Data/Scripts/Editor/AssetBrowser.lua b/Data/Scripts/Editor/AssetBrowser.lua index 68f0483..5f00dc1 100644 --- a/Data/Scripts/Editor/AssetBrowser.lua +++ b/Data/Scripts/Editor/AssetBrowser.lua @@ -42,7 +42,8 @@ out vec4 FragColor; void main()
{
- vec4 sampled = vec4(texture(uiTex, uv).r, 0, 0, 1.0);
+ vec2 uv = vec2(uv.x, 1 - uv.y);
+ vec4 sampled = vec4(1,1,1,texture(uiTex, uv).r);
FragColor = sampled;
// FragColor = texture(uiTex, uv);
// FragColor.rgb = vec3(1,0,0);
diff --git a/Documents/图片/æ–‡å—æ¸²æŸ“/é¢ å€’.png b/Documents/图片/æ–‡å—æ¸²æŸ“/é¢ å€’.png Binary files differnew file mode 100644 index 0000000..6cbe043 --- /dev/null +++ b/Documents/图片/æ–‡å—æ¸²æŸ“/é¢ å€’.png diff --git a/Editor/EditorMain.cpp b/Editor/EditorMain.cpp index e31da28..289eed4 100644 --- a/Editor/EditorMain.cpp +++ b/Editor/EditorMain.cpp @@ -22,15 +22,13 @@ void TestFont() {
TextGeneratingSettings setting;
setting.margin = 5;
- setting.padding = 3;
- setting.atlasSize = Internal::Vector2(256, 256);
+ setting.padding = 5;
+ setting.atlasSize = Internal::Vector2(512, 512);
+
TextGenerator::Instance()->Setup(setting);
- TextGenerator::Instance()->RenderCharacter(L'ºÃ', 14);
- TextGenerator::Instance()->RenderCharacter(L'´ó', 14);
- TextGenerator::Instance()->RenderCharacter(L'¼Ò', 14);
- TextGenerator::Instance()->RenderCharacter(L'Íí', 14);
- TextGenerator::Instance()->RenderCharacter(L'ÉÏ', 14);
- TextGenerator::Instance()->RenderCharacter(L'¿ì', 14);
+ auto content = L"abcdfÓëµç×Ó¼ÆËã»úµÄÇø±ð Óëµç×Ó¼ÆËã»úµÄ×î´óÇø±ðÔÚÓÚ: Ö»ÊǼòµ¥µÄ¼ÆË㹤¾ß,ÓÐЩ»úÐ;߱¸º¯Êý¼ÆË㹦ÄÜ,ÓÐЩ»úÐ;߱¸Ò»¶¨µÄÖü´æ¹¦ÄÜ,µ«Ò»°ãÖ»ÄÜ´æ´¢¼¸×éÊý¾Ý¡£¼ÆËã»úÔò¾ß±¸¸´ÔÓ´æÖü¹¦ÄÜ¡¢¿ØÖƹ¦ÄÜ,¸ü¼Ó";
+ TextGenerator::Instance()->RenderCharacters((character::Codepoint*)content, wcslen(content), 14);
+
}
int BeforeMainLoop(lua_State* L)
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj b/Projects/VisualStudio/Editor/Editor.vcxproj index d845f72..d35cb10 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj +++ b/Projects/VisualStudio/Editor/Editor.vcxproj @@ -191,13 +191,11 @@ <ClCompile Include="..\..\..\Runtime\Graphics\OpenGL.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\Point.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\PolyLine.cpp" />
- <ClCompile Include="..\..\..\Runtime\Graphics\Quad.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\RenderTexture.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\Shader.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\ShaderCompiler.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\SharedVertexBuffer.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\Texture.cpp" />
- <ClCompile Include="..\..\..\Runtime\Graphics\UIQuad.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\VertexAttribute.cpp" />
<ClCompile Include="..\..\..\Runtime\Graphics\VertexBuffer.cpp" />
<ClCompile Include="..\..\..\Runtime\GUI\TextGenerator.cpp" />
@@ -217,6 +215,8 @@ <ClCompile Include="..\..\..\Runtime\Math\Vector3.cpp" />
<ClCompile Include="..\..\..\Runtime\Math\Vector4.cpp" />
<ClCompile Include="..\..\..\Runtime\Profiling\FrameStats.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Rendering\Quad.cpp" />
+ <ClCompile Include="..\..\..\Runtime\Rendering\UIQuad.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Common\Common.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Common\DataBuffer.bind.cpp" />
<ClCompile Include="..\..\..\Runtime\Scripting\Debug\Debug.bind.cpp" />
@@ -274,13 +274,11 @@ <ClInclude Include="..\..\..\Runtime\Graphics\Point.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\PolyLine.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Primitive.h" />
- <ClInclude Include="..\..\..\Runtime\Graphics\Quad.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\RenderTexture.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Shader.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\ShaderCompiler.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\SharedVertexBuffer.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\Texture.h" />
- <ClInclude Include="..\..\..\Runtime\Graphics\UIQuad.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\VertexAttribute.h" />
<ClInclude Include="..\..\..\Runtime\Graphics\VertexBuffer.h" />
<ClInclude Include="..\..\..\Runtime\GUI\freetype.h" />
@@ -315,6 +313,9 @@ <ClInclude Include="..\..\..\Runtime\Math\Vector4.h" />
<ClInclude Include="..\..\..\Runtime\Profiling\FrameStats.h" />
<ClInclude Include="..\..\..\Runtime\Profiling\Profiler.h" />
+ <ClInclude Include="..\..\..\Runtime\Rendering\DynamicMesh.h" />
+ <ClInclude Include="..\..\..\Runtime\Rendering\Quad.h" />
+ <ClInclude Include="..\..\..\Runtime\Rendering\UIQuad.h" />
<ClInclude Include="..\..\..\Runtime\Threading\Job.h" />
<ClInclude Include="..\..\..\Runtime\Threading\JobSystem.h" />
<ClInclude Include="..\..\..\Runtime\Threading\Mutex.h" />
@@ -328,6 +329,7 @@ <ClInclude Include="..\..\..\Runtime\Utilities\IncrementalTask.h" />
<ClInclude Include="..\..\..\Runtime\Utilities\NonCopyable.h" />
<ClInclude Include="..\..\..\Runtime\Utilities\Singleton.h" />
+ <ClInclude Include="..\..\..\Runtime\Utilities\StaticInitiator.h" />
<ClInclude Include="..\..\..\Runtime\Utilities\Type.h" />
<ClInclude Include="..\..\..\Runtime\Utilities\UIDGenerator.h" />
<ClInclude Include="..\..\..\Runtime\Utilities\Utf8.h" />
diff --git a/Projects/VisualStudio/Editor/Editor.vcxproj.filters b/Projects/VisualStudio/Editor/Editor.vcxproj.filters index e816c60..3f35125 100644 --- a/Projects/VisualStudio/Editor/Editor.vcxproj.filters +++ b/Projects/VisualStudio/Editor/Editor.vcxproj.filters @@ -112,6 +112,9 @@ <Filter Include="Runtime\GUI">
<UniqueIdentifier>{2e933d36-b374-4029-a210-1e38f81886b8}</UniqueIdentifier>
</Filter>
+ <Filter Include="Runtime\Rendering">
+ <UniqueIdentifier>{c87ed6d9-48d6-4c07-8b94-d33e8f5a5f9c}</UniqueIdentifier>
+ </Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\..\Editor\GUI\Dock.cpp">
@@ -270,9 +273,6 @@ <ClCompile Include="..\..\..\Runtime\Graphics\PolyLine.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\Graphics\Quad.cpp">
- <Filter>Runtime\Graphics</Filter>
- </ClCompile>
<ClCompile Include="..\..\..\Runtime\Graphics\RenderTexture.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
@@ -372,9 +372,6 @@ <ClCompile Include="..\..\..\Runtime\Graphics\CustomVertexLayout.cpp">
<Filter>Runtime\Graphics</Filter>
</ClCompile>
- <ClCompile Include="..\..\..\Runtime\Graphics\UIQuad.cpp">
- <Filter>Runtime\Graphics</Filter>
- </ClCompile>
<ClCompile Include="..\..\..\Runtime\Math\Matrix44.cpp">
<Filter>Runtime\Math</Filter>
</ClCompile>
@@ -387,6 +384,12 @@ <ClCompile Include="..\..\..\Runtime\GUI\TextGenerator.cpp">
<Filter>Runtime\GUI</Filter>
</ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Rendering\Quad.cpp">
+ <Filter>Runtime\Rendering</Filter>
+ </ClCompile>
+ <ClCompile Include="..\..\..\Runtime\Rendering\UIQuad.cpp">
+ <Filter>Runtime\Rendering</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\Editor\GUI\Dock.h">
@@ -563,9 +566,6 @@ <ClInclude Include="..\..\..\Runtime\Graphics\PolyLine.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
- <ClInclude Include="..\..\..\Runtime\Graphics\Quad.h">
- <Filter>Runtime\Graphics</Filter>
- </ClInclude>
<ClInclude Include="..\..\..\Runtime\Graphics\RenderTexture.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
@@ -638,9 +638,6 @@ <ClInclude Include="..\..\..\Runtime\Graphics\Primitive.h">
<Filter>Runtime\Graphics</Filter>
</ClInclude>
- <ClInclude Include="..\..\..\Runtime\Graphics\UIQuad.h">
- <Filter>Runtime\Graphics</Filter>
- </ClInclude>
<ClInclude Include="..\..\..\Runtime\Utilities\DynamicArray.h">
<Filter>Runtime\Utilities</Filter>
</ClInclude>
@@ -668,6 +665,18 @@ <ClInclude Include="..\..\..\Runtime\Math\Math.h">
<Filter>Runtime\Math</Filter>
</ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Rendering\Quad.h">
+ <Filter>Runtime\Rendering</Filter>
+ </ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Rendering\UIQuad.h">
+ <Filter>Runtime\Rendering</Filter>
+ </ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Rendering\DynamicMesh.h">
+ <Filter>Runtime\Rendering</Filter>
+ </ClInclude>
+ <ClInclude Include="..\..\..\Runtime\Utilities\StaticInitiator.h">
+ <Filter>Runtime\Utilities</Filter>
+ </ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\Runtime\Lua\LuaBind\LuaBindClass.inc">
diff --git a/Runtime/GUI/TextGenerator.cpp b/Runtime/GUI/TextGenerator.cpp index a518772..1b34d1b 100644 --- a/Runtime/GUI/TextGenerator.cpp +++ b/Runtime/GUI/TextGenerator.cpp @@ -38,7 +38,7 @@ character::Hash TextGenerator::GetHash(Codepoint codepoint, int pixelSize) return hash; } -Character TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelSize) +const Character* TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); auto iter = m_Characters.find(hash); @@ -48,14 +48,22 @@ Character TextGenerator::GetCharacter(character::Codepoint codepoint, int pixelS iter = m_Characters.find(hash); } Assert(iter != m_Characters.end()); - return iter->second; + return &iter->second; +} + +void TextGenerator::RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize) +{ + for (int i = 0; i < n; ++i) + { + RenderCharacter(codepoint[i], pixelSize); + } } void TextGenerator::RenderCharacter(character::Codepoint codepoint, int pixelSize) { character::Hash hash = GetHash(codepoint, pixelSize); -// if (m_Characters.count(hash) != 0) -// return; + if (m_Characters.count(hash) != 0) + return; FT_Set_Pixel_Sizes(m_FTFace, 0, pixelSize); if (FT_Load_Char(m_FTFace, codepoint, FT_LOAD_RENDER)) { @@ -88,12 +96,12 @@ void TextGenerator::RenderCharacter(character::Codepoint codepoint, int pixelSiz character.atlas = atlas->index; character.position = rect; character.bearing = Internal::Vector2(m_FTFace->glyph->bitmap_left, m_FTFace->glyph->bitmap_top); - character.advance = m_FTFace->glyph->advance.x; + character.advance = m_FTFace->glyph->advance.x * 1/64.f; m_Characters.insert(std::pair<character::Hash, Character>(hash, character)); } -GlyphAtals* TextGenerator::GetGlyphAtlas(int index) +const GlyphAtals* TextGenerator::GetGlyphAtlas(int index) { if (index >= m_Atlases.size()) return NULL; @@ -104,25 +112,25 @@ Internal::Rect TextGenerator::GetRenderChartAndMove(GlyphAtals* atlas, Internal: { Internal::Rect rect; Internal::Vector2 space; - space.x = atlas->width - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.x; - space.y = atlas->height - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.y; + space.x = atlas->width - atlas->cursor.x - m_AtlasMargin; + space.y = atlas->height - atlas->cursor.y - m_AtlasMargin; if (space.x > preferSize.x && space.y > preferSize.y) { rect.x = atlas->cursor.x; rect.y = atlas->cursor.y; rect.width = preferSize.x; rect.height = preferSize.y; - atlas->cursor.x += rect.width; + atlas->cursor.x += rect.width + m_GlyphPadding; atlas->rowHeight = max(atlas->rowHeight, preferSize.y); } - else if (space.y - atlas->rowHeight > preferSize.y) + else if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y) { rect.x = m_AtlasMargin; rect.y = atlas->cursor.y + m_GlyphPadding + atlas->rowHeight; rect.width = preferSize.x; rect.height = preferSize.y; atlas->cursor.x = m_AtlasMargin; - atlas->cursor.y += atlas->rowHeight; + atlas->cursor.y += atlas->rowHeight + m_GlyphPadding; atlas->rowHeight = rect.height; } else @@ -178,11 +186,11 @@ bool TextGenerator::HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSi if (atlas == NULL) return false; Internal::Vector2 space; - space.x = atlas->width - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.x; - space.y = atlas->height - m_AtlasMargin * 2 - m_GlyphPadding - atlas->cursor.y; + space.x = atlas->width - atlas->cursor.x - m_AtlasMargin; + space.y = atlas->height - atlas->cursor.y - m_AtlasMargin; if (space.x > preferSize.x && space.y > preferSize.y) return true; - if (space.y - atlas->rowHeight > preferSize.y) + if (space.y - atlas->rowHeight - m_GlyphPadding - m_AtlasMargin > preferSize.y) return true; return false; } diff --git a/Runtime/GUI/TextGenerator.h b/Runtime/GUI/TextGenerator.h index 1065acf..ef78eb6 100644 --- a/Runtime/GUI/TextGenerator.h +++ b/Runtime/GUI/TextGenerator.h @@ -51,6 +51,7 @@ struct GlyphAtals int index; Texture* altas; // Ìùͼ int width, height; // ³ß´ç + Internal::Vector2 cursor; // Óα꣬´Ó×óÉϽǣ¨0,0£©¿ªÊ¼ int rowHeight; // µ±Ç°ÐÐµÄ¸ß¶È }; @@ -67,9 +68,12 @@ class TextGenerator : public Singleton<TextGenerator> public: void Setup(TextGeneratingSettings settings); - Character GetCharacter(character::Codepoint codepoint, int pixelSize); + const Character* GetCharacter(character::Codepoint codepoint, int pixelSize); + const GlyphAtals* GetGlyphAtlas(int index); + + // pre-bake void RenderCharacter(character::Codepoint codepoint, int pixelSize); - GlyphAtals* GetGlyphAtlas(int index); + void RenderCharacters(character::Codepoint* codepoint, int n, int pixelSize); private: @@ -79,6 +83,8 @@ private: Internal::Rect GetRenderChartAndMove(GlyphAtals* atlas, Internal::Vector2 preferSize); bool HasEnoughSpace(GlyphAtals* atlas, Internal::Vector2 preferSize); + character::Hash GetHash(character::Codepoint Codepoint, int pixelSize); + std::unordered_map<character::Hash, Character> m_Characters; // äÖȾÍêµÄÎÄ×Ö std::vector<GlyphAtals> m_Atlases; // µ±Ç°ËùÓеÄatlas @@ -91,8 +97,6 @@ private: FT_Library m_FTLibrary; FT_Face m_FTFace; - character::Hash GetHash(character::Codepoint Codepoint, int pixelSize); - }; #define g_TextGenerator (*TextGenerator::Instance()) diff --git a/Runtime/Graphics/CustomVertexLayout.h b/Runtime/Graphics/CustomVertexLayout.h index 86dcf90..d72c71d 100644 --- a/Runtime/Graphics/CustomVertexLayout.h +++ b/Runtime/Graphics/CustomVertexLayout.h @@ -11,6 +11,15 @@ struct CustomVertexLayout { GLuint buffer; // ´´½¨Ê±Áô¿Õ std::vector<VertexAttributeDescriptor> attributes; + + // ÖØÖÃpointer£¨startOffset£©Îª0 + void ResetPointer() + { + for (int i = 0; i < attributes.size(); ++i) + { + attributes[i].pointer = 0; + } + } }; namespace VertexLayout @@ -19,4 +28,3 @@ namespace VertexLayout extern void SetupCustomVertexLayout(CustomVertexLayout& layout); } - diff --git a/Runtime/Graphics/SharedVertexBuffer.cpp b/Runtime/Graphics/SharedVertexBuffer.cpp index 464a919..e787630 100644 --- a/Runtime/Graphics/SharedVertexBuffer.cpp +++ b/Runtime/Graphics/SharedVertexBuffer.cpp @@ -173,6 +173,8 @@ void SharedVertexBuffer::DrawChunk(CustomVertexLayout& layout) VertexLayout::SetupCustomVertexLayout(layout); + layout.ResetPointer(); + const void* indexPtr = m_CurIB ? 0 : (m_CurIBData.empty() ? 0 : &m_CurIBData[0]); if (m_CurIB) diff --git a/Runtime/Graphics/Texture.cpp b/Runtime/Graphics/Texture.cpp index f92c094..eed0b90 100644 --- a/Runtime/Graphics/Texture.cpp +++ b/Runtime/Graphics/Texture.cpp @@ -158,12 +158,13 @@ Texture::Texture(TextureSetting setting, int w, int h) if (m_Format == ETextureFormat::R8)
{
internalFormat = GL_RED;
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
}
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, NULL);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glDeleteTextures(1, &m_GPUID);
glBindTexture(GL_TEXTURE_2D, 0);
@@ -185,6 +186,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo throw TextureException(error);
);
+ int alignment = 4;
+ if (m_Format == ETextureFormat::R8)
+ {
+ alignment = 1;
+ }
+
GLenum fmt = GL_RED;
switch (format)
{
@@ -205,8 +212,12 @@ void Texture::UpdateSubImage(Internal::Rect rect, int format, int type, const vo case EPixelElementType::PixelType_FLOAT: t = GL_FLOAT; break;
}
+ glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
+
glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.width, rect.height, fmt, t, data);
+ glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
+
CheckGLError(
glBindTexture(GL_TEXTURE_2D, 0);
throw TextureException(error);
diff --git a/Runtime/Graphics/VertexAttribute.h b/Runtime/Graphics/VertexAttribute.h index 4e50a1a..2757c70 100644 --- a/Runtime/Graphics/VertexAttribute.h +++ b/Runtime/Graphics/VertexAttribute.h @@ -21,6 +21,15 @@ enum VertexAttrFormat struct VertexAttributeDescriptor { + //union { + const void* pointer; // ÄÚ´æµØÖ·£¬¸Õ´´½¨Ê±Áô¿Õ + int startOffset; // ÏÔ´æÖÐÏà¶ÔVBOµÄÆ«ÒÆÖµ + //}; + uint componentNum; // ÏòÁ¿Î¬¶È1,2,3,4 + uint componentFormat; // ÿ¸ö·ÖÁ¿µÄÀàÐÍ + uint16 stride; // ¼ä¸ô + bool normalize; // ÊÇ·ñ¹éÒ»»¯£¬Ö»ÓÃÓÚCustomVertexLayout + // for default vertex layout VertexAttributeDescriptor() {} // for custom vertex layout @@ -33,14 +42,6 @@ struct VertexAttributeDescriptor normalize = normalized; } - union { - const void* pointer; // ÄÚ´æµØÖ·»òÏÔ´æÖÐÏà¶ÔVBOµÄÆ«ÒÆÖµ - int startOffset; - }; - uint componentNum; // ÏòÁ¿Î¬¶È1,2,3,4 - uint componentFormat; // ÿ¸ö·ÖÁ¿µÄÀàÐÍ - uint16 stride; // ¼ä¸ô - bool normalize; // ÊÇ·ñ¹éÒ»»¯£¬Ö»ÓÃÓÚCustomVertexLayout }; namespace VertexAttribute diff --git a/Runtime/Rendering/DynamicMesh.h b/Runtime/Rendering/DynamicMesh.h new file mode 100644 index 0000000..3cbb6f6 --- /dev/null +++ b/Runtime/Rendering/DynamicMesh.h @@ -0,0 +1,10 @@ +#pragma once +#include "Runtime/Utilities/StaticInitiator.h" + +// Ìî³äg_SharedVBOµÄ¶¯Ì¬mesh +class DynamicMesh +{ +public: + virtual void Draw() = 0; + +}; diff --git a/Runtime/Graphics/Quad.cpp b/Runtime/Rendering/Quad.cpp index d769dd4..b4f81ee 100644 --- a/Runtime/Graphics/Quad.cpp +++ b/Runtime/Rendering/Quad.cpp @@ -1,7 +1,7 @@ #include "../Math/Vector2.h" #include "../Math/Vector3.h" #include "Quad.h" -#include "GfxDevice.h" +#include "../Graphics/GfxDevice.h" struct QuadVBOLayout { @@ -9,6 +9,16 @@ struct QuadVBOLayout Internal::Vector2 uv; }; +static CustomVertexLayout layout; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 3, VertexAttrFormat_Float, sizeof(QuadVBOLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector3), 2, VertexAttrFormat_Float, sizeof(QuadVBOLayout)); + + layout.attributes.push_back(POSITION); + layout.attributes.push_back(UV); +}); + void Quad::Draw() { const int nVerts = 4; @@ -34,8 +44,7 @@ void Quad::Draw() uint8* vb; uint16* ib; - uint attrs = Mask(VertexAttr_Position) | Mask(VertexAttr_UV); - g_SharedVBO.GetChunk(attrs, 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); + g_SharedVBO.GetChunk(sizeof(QuadVBOLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib); QuadVBOLayout* dst = (QuadVBOLayout*)vb; for (int i = 0; i < nVerts; ++i) @@ -48,5 +57,5 @@ void Quad::Draw() ib[i] = indices[i]; g_SharedVBO.ReleaseChunk(4, 6); - g_SharedVBO.DrawChunk(); + g_SharedVBO.DrawChunk(layout); }
\ No newline at end of file diff --git a/Runtime/Graphics/Quad.h b/Runtime/Rendering/Quad.h index 0e38ec4..ee6c5be 100644 --- a/Runtime/Graphics/Quad.h +++ b/Runtime/Rendering/Quad.h @@ -2,8 +2,9 @@ #define QUAD_H #include "../Utilities/UtilMacros.h" +#include "DynamicMesh.h" -class Quad +class Quad : public DynamicMesh { public: Quad(float l, float r, float t, float b); @@ -15,7 +16,7 @@ public: GET_SET(float, Top, m_Top); GET_SET(float, Bottom, m_Bottom); - void Draw(); + void Draw() override; private: float m_Left, m_Right, m_Top, m_Bottom; diff --git a/Runtime/Graphics/UIQuad.cpp b/Runtime/Rendering/UIQuad.cpp index 73cf645..35dcf7e 100644 --- a/Runtime/Graphics/UIQuad.cpp +++ b/Runtime/Rendering/UIQuad.cpp @@ -1,5 +1,5 @@ #include "../Math/Vector2.h" -#include "GfxDevice.h" +#include "../Graphics/GfxDevice.h" #include "UIQuad.h" struct UIQuadLayout @@ -8,16 +8,18 @@ struct UIQuadLayout Internal::Vector2 uv; }; -void UIQuad::Draw() -{ - CustomVertexLayout layout; +static CustomVertexLayout layout; +InitializeStaticVariables([](){ VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector2), 2, VertexAttrFormat_Float, sizeof(UIQuadLayout)); layout.attributes.push_back(POSITION); layout.attributes.push_back(UV); +}); +void UIQuad::Draw() +{ const int nVerts = 4; const int nIndices = 6; diff --git a/Runtime/Graphics/UIQuad.h b/Runtime/Rendering/UIQuad.h index a0d77a5..f6d3a98 100644 --- a/Runtime/Graphics/UIQuad.h +++ b/Runtime/Rendering/UIQuad.h @@ -1,6 +1,8 @@ #pragma once +#include "DynamicMesh.h" +#include "../Utilities/StaticInitiator.h" -class UIQuad +class UIQuad : public DynamicMesh { public : UIQuad(float l, float r, float t, float b) @@ -10,7 +12,8 @@ public : m_Top = t; m_Bottom = b; } - void Draw(); + + void Draw() override; private: float m_Left, m_Right, m_Top, m_Bottom; diff --git a/Runtime/Scripting/Rendering/Rendering.bind.cpp b/Runtime/Scripting/Rendering/Rendering.bind.cpp index cb294a3..9fb9a48 100644 --- a/Runtime/Scripting/Rendering/Rendering.bind.cpp +++ b/Runtime/Scripting/Rendering/Rendering.bind.cpp @@ -1,8 +1,8 @@ #include "Runtime/Graphics/Shader.h" #include "Runtime/Graphics/Texture.h" #include "Runtime/Graphics/ImageData.h" -#include "Runtime/Graphics/UIQuad.h" #include "Runtime/Graphics/GfxDevice.h" +#include "Runtime/Rendering/UIQuad.h" // Rendering.DrawUIQuad({}) static int DrawUIQuad(lua_State* L) diff --git a/Runtime/Utilities/StaticInitiator.h b/Runtime/Utilities/StaticInitiator.h new file mode 100644 index 0000000..bbcdbee --- /dev/null +++ b/Runtime/Utilities/StaticInitiator.h @@ -0,0 +1,27 @@ +#pragma once + +// ¾²Ì¬¹¹Ô캯Êý +#include "ThirdParty/StaticConstructor/include/StaticConstructor.h" + +typedef void(*StaticFunc) (); + +// ×Ô¶¯³õʼ»¯¾²Ì¬Êý¾Ý +class StaticFuncInvoker +{ +public: + // Default Constructor: + StaticFuncInvoker(StaticFunc func) + { + if (func) + func(); + } +}; + +// µ÷ÓþßÃûµÄstaticº¯Êý +#define InvokeStaticFunc(func)\ + static StaticFuncInvoker staticInvokerOf_##func(func); + +// ÓÃÀ´³õʼ»¯µ±Ç°cppÀïµÄ¾²Ì¬±äÁ¿ +#define InitializeStaticVariables(lambda)\ + static StaticFuncInvoker staticInvoker(lambda); + diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.cpp b/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.cpp new file mode 100644 index 0000000..3ea12a3 --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.cpp @@ -0,0 +1,43 @@ +// MyClass.cpp +// +#include "stdafx.h" +#include "MyClass.h" + + +// Initialization of protected static data members: +void* MyClass::mpStaticMemory = NULL; +const double MyClass::mPI = 3.141592; +std::string MyClass::mStaticStr = "Init Value"; + + +// Invoke the StaticConstructor & StaticDestructor of the class: +// Make sure you put this AFTER the initialization of the static data members! +INVOKE_STATIC_CONSTRUCTOR(MyClass); + + +// Default Constructor: +MyClass::MyClass() +{ +// PI = 5.0; // Cannot be done, as PI is const + mStaticStr = "Modified by Default Constructor"; +} + +// Destructor: +MyClass::~MyClass() +{ + mStaticStr = "Modified by Default Destructor"; +} + +// Static Constructor: +void MyClass::StaticConstructor() +{ + mStaticStr = "Modified by Static Constructor"; + mpStaticMemory = new double[10]; +} + +// Static Destructor: +void MyClass::StaticDestructor() +{ + delete[] mpStaticMemory; +} + diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.h b/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.h new file mode 100644 index 0000000..5f86f22 --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/MyClass.h @@ -0,0 +1,36 @@ +// MyClass.h +// +#include <string> +#include <StaticConstructor.h> + + +class MyClass +{ +protected: + // Declaration of protected static data members: + // Cannot be initialized here (C++ language limitation) + static void* mpStaticMemory; + static const double mPI; + static std::string mStaticStr; + +public: + // Static function members to get the static data members (to unify the Tests): + static const double& PI() { return mPI; }; + static std::string& StaticStr() { return mStaticStr; }; + +public: + // Default Constructor: + MyClass(); + + // Destructor: + virtual ~MyClass(); + + // Static Constructor: + // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro in the CPP file) + STATIC_CONSTRUCTOR(); + + // Static Destructor: + // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro in the CPP file) + STATIC_DESTRUCTOR(); +}; + diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/MyTemplate.h b/ThirdParty/StaticConstructor/StaticConstructorSample/MyTemplate.h new file mode 100644 index 0000000..a0a147c --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/MyTemplate.h @@ -0,0 +1,48 @@ +// MyTemplate.h +// +#include <string> +#include <StaticConstructor.h> + + +template <typename Type> +class MyTemplate +{ +protected: + // Declaration and initialization of protected static Data-Function member: + STATIC_DF_MEMBER(Type*, pStaticMemory, NULL); + +public: + // Declaration and initialization of public static Data-Function members: + STATIC_DF_MEMBER(const double, PI, 3.141592); + STATIC_DF_MEMBER(std::string, StaticStr, "Init Value"); + +public: + // Default Constructor: + MyTemplate() + { + // PI() = 5.0; // Cannot be done, as PI is const + StaticStr() = "Modified by Default Constructor"; + } + + // Destructor: + virtual ~MyTemplate() + { + StaticStr() = "Modified by Default Destructor"; + } + + // Static Constructor: + // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro in one CPP file) + STATIC_CONSTRUCTOR() + { + StaticStr() = "Modified by Static Constructor"; + pStaticMemory() = new Type[10]; + } + + // Static Destructor: + // (Should be called by INVOKE_STATIC_CONSTRUCTOR macro in one CPP file) + STATIC_DESTRUCTOR() + { + delete[] pStaticMemory(); + } +}; + diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp new file mode 100644 index 0000000..26ff046 --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.cpp @@ -0,0 +1,76 @@ +// StaticConstructorSample.cpp : Defines the entry point for the console application. +// +#include "stdafx.h" +#include <iostream> +#include <string> + +// Sample Files: +#include "MyClass.h" +#include "MyTemplate.h" + + +// Sample code for StartUp +STATIC_STARTUP_CODE() +{ + std::cout << "Starting up..." << std::endl; +} + +// Sample code for FinishUp +STATIC_FINISHUP_CODE() +{ + std::cout << "Finishing up..." << std::endl; +} + + +// Template for testing each Type: +template <typename Type> +void TestType(const std::string& strType) +{ + using namespace std; + + // Static Constructor should have been already invoked: + cout << "Show static data members before creating any instance of " << strType << ":" << endl; + cout << "String: " << Type::StaticStr() << endl; + cout << "PI: " << Type::PI() << endl; + + // Temporary block: + // Destructor of local variable will be called at the end of this block. + { + Type myObj; + cout << "Show static data members after creating an instance of " << strType << ":" << endl; + cout << "String: " << myObj.StaticStr() << endl; + cout << "PI: " << myObj.PI() << endl; + } + + cout << "Show static data members after destroying the instance of " << strType << ":" << endl; + cout << "String: " << Type::StaticStr() << endl; + cout << "PI: " << Type::PI() << endl; + cout << endl; +} + + +// Declaration of alias (class names) for the template instaces: +typedef MyTemplate<int> MyTemplateInt; +typedef MyTemplate<double> MyTemplateDouble; + +// Invoke the StaticConstructor & StaticDestructor for each one of the template instances: +// Should be called with alias (class names) for the template instaces. +INVOKE_STATIC_CONSTRUCTOR(MyTemplateInt); +INVOKE_STATIC_CONSTRUCTOR(MyTemplateDouble); + + +// Main: +int _tmain(int argc, _TCHAR* argv[]) +{ + // Static Constructors have already been called before entering this function... + + // Run the same tests for each one of the classes: + TestType<MyClass>("MyClass"); + TestType<MyTemplateInt>("MyTemplateInt"); + TestType<MyTemplateDouble>("MyTemplateDouble"); + + // Static Destructors will be called after exiting this function... + return 0; +} + + diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.sln b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.sln new file mode 100644 index 0000000..265f06c --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual C++ Express 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "StaticConstructorSample", "StaticConstructorSample.vcproj", "{41329043-C008-486E-99A9-BC1A0B4DF9A5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {41329043-C008-486E-99A9-BC1A0B4DF9A5}.Debug|Win32.ActiveCfg = Debug|Win32 + {41329043-C008-486E-99A9-BC1A0B4DF9A5}.Debug|Win32.Build.0 = Debug|Win32 + {41329043-C008-486E-99A9-BC1A0B4DF9A5}.Release|Win32.ActiveCfg = Release|Win32 + {41329043-C008-486E-99A9-BC1A0B4DF9A5}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.vcproj b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.vcproj new file mode 100644 index 0000000..0f66ceb --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/StaticConstructorSample.vcproj @@ -0,0 +1,229 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="9,00" + Name="StaticConstructorSample" + ProjectGUID="{41329043-C008-486E-99A9-BC1A0B4DF9A5}" + RootNamespace="StaticConstructorSample" + Keyword="Win32Proj" + TargetFrameworkVersion="196613" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="$(SolutionDir)$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="1" + CharacterSet="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="..\include" + PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE" + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="3" + UsePrecompiledHeader="2" + WarningLevel="3" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + LinkIncremental="2" + GenerateDebugInformation="true" + SubSystem="1" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory="$(SolutionDir)$(ConfigurationName)" + IntermediateDirectory="$(ConfigurationName)" + ConfigurationType="1" + CharacterSet="1" + WholeProgramOptimization="1" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="2" + EnableIntrinsicFunctions="true" + AdditionalIncludeDirectories="..\include" + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + RuntimeLibrary="2" + EnableFunctionLevelLinking="true" + UsePrecompiledHeader="2" + WarningLevel="3" + DebugInformationFormat="3" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + LinkIncremental="1" + GenerateDebugInformation="true" + SubSystem="1" + OptimizeReferences="2" + EnableCOMDATFolding="2" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <Filter + Name="include" + > + <File + RelativePath="..\include\StaticConstructor.h" + > + </File> + </Filter> + <Filter + Name="Sample Files" + > + <File + RelativePath=".\MyClass.cpp" + > + </File> + <File + RelativePath=".\MyClass.h" + > + </File> + <File + RelativePath=".\MyTemplate.h" + > + </File> + </Filter> + <Filter + Name="Project Files" + > + <File + RelativePath=".\StaticConstructorSample.cpp" + > + </File> + <File + RelativePath=".\stdafx.cpp" + > + <FileConfiguration + Name="Debug|Win32" + > + <Tool + Name="VCCLCompilerTool" + UsePrecompiledHeader="1" + /> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32" + > + <Tool + Name="VCCLCompilerTool" + UsePrecompiledHeader="1" + /> + </FileConfiguration> + </File> + <File + RelativePath=".\stdafx.h" + > + </File> + </Filter> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.cpp b/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.cpp new file mode 100644 index 0000000..88d97ab --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// StaticConstructorSample.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.h b/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.h new file mode 100644 index 0000000..022bc3b --- /dev/null +++ b/ThirdParty/StaticConstructor/StaticConstructorSample/stdafx.h @@ -0,0 +1,24 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +// The following macros define the minimum required platform. The minimum required platform +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. +#endif + +// STL includes +#include <stdio.h> +#include <tchar.h> + + +// TODO: reference additional headers your program requires here diff --git a/ThirdParty/StaticConstructor/include/StaticConstructor.h b/ThirdParty/StaticConstructor/include/StaticConstructor.h new file mode 100644 index 0000000..22310bd --- /dev/null +++ b/ThirdParty/StaticConstructor/include/StaticConstructor.h @@ -0,0 +1,126 @@ +////////////////////////////////////////////////////////////////////////////// +// AUTHOR: Hugo González Castro +// TITLE: Static Constructor in C++ +// DESCRIPTION: An easy way to implement static constructors and +// static destructors in standard C++. +// VERSION: v1.2 - 2011/11/27 +// LICENSE: CPOL (Code Project Open License). +// Please, do not remove nor modify this header. +// URL: http://www.codeproject.com/KB/cpp/StaticConstructor.aspx +////////////////////////////////////////////////////////////////////////////// +#ifndef _STATIC_CONSTRUCTOR_H_ +#define _STATIC_CONSTRUCTOR_H_ + + + +////////////////////////////////////////////////////////////////////////////// +// DECLARATIONS (macros to use): +////////////////////////////////////////////////////////////////////////////// + + +// REQUIRED macro to invoke the static constructor/destructor of a class: +// place the call to this macro out of the definition of the class, in a .CPP file +// (avoid using it in .H files unless they are included only once). +// Make sure you put this AFTER the initialization of the static data members! +// For templates, should be called with alias (typedef) for the template instaces. +////////////////////////////////////////////////////////////////////////////// +#define INVOKE_STATIC_CONSTRUCTOR(ClassName) \ + INVOKE_STATIC_CONSTRUCTOR_EXPANDED(ClassName) + + +// OPTIONAL macros to help to declare the header of the static constructor/destructor: +// place the calls to these macros inside the definition of the class/template, in a .H file +////////////////////////////////////////////////////////////////////////////// +#define STATIC_CONSTRUCTOR() static void StaticConstructor() +#define STATIC_DESTRUCTOR() static void StaticDestructor() + + +// OPTIONAL macro to declare static Data-Function members with inline initialization: +// place the call to this macro inside the definition of the class/template. +////////////////////////////////////////////////////////////////////////////// +// STATIC DF MEMBER (static Data-Function member): +// "a static function member with a static local (data) variable declared inside". +// The TypeName can be or not a CONST type. +////////////////////////////////////////////////////////////////////////////// +#define STATIC_DF_MEMBER(TypeName, DFMemberName, InitValue) \ + STATIC_DF_MEMBER_EXPANDED(TypeName, DFMemberName, InitValue) + + +// OPTIONAL macros to run code at startup and finishup. Place outside classes. +// Place the static code inside {} after one of these macros. +////////////////////////////////////////////////////////////////////////////// +#define STATIC_STARTUP_CODE() STATIC_STARTUP_CODE_EXPANDED() +#define STATIC_FINISHUP_CODE() STATIC_FINISHUP_CODE_EXPANDED() + + + +////////////////////////////////////////////////////////////////////////////// +// IMPLEMENTATION (do not directly use these macros): +////////////////////////////////////////////////////////////////////////////// + + +// Definition of special class to invoke +// the static constructor/destructor from +// its own non-static constructor/destructor. +template<typename ClassName> +class StaticInvoker +{ +public: + // Default Constructor: + StaticInvoker() + { + // Call to the static constructor of ClassName: + ClassName::StaticConstructor(); + } + + // Destructor: + virtual ~StaticInvoker() + { + // Call to the static destructor of ClassName: + ClassName::StaticDestructor(); + } +}; + + +// Macro with expanded name: +#define INVOKE_STATIC_CONSTRUCTOR_EXPANDED(ClassName) \ + /* Single instance of this invoker class, so its constructor is called only once */ \ + StaticInvoker<ClassName> staticInvokerOf_##ClassName; + + +// STATIC DF MEMBER (static Data-Function member): +// "a static function member with a static local (data) variable declared inside". +// The TypeName can be or not a CONST type. +#define STATIC_DF_MEMBER_EXPANDED(TypeName, DFMemberName, InitValue) \ + static TypeName& DFMemberName() \ + { \ + static TypeName DFMemberName(InitValue); \ + return DFMemberName; \ + } + + +// Expanded macro to run code at startup. +#define STATIC_STARTUP_CODE_EXPANDED() \ + class StartUpCode \ + { \ + public: \ + StartUpCode(); \ + }; \ + StartUpCode myStartUpCode; \ + StartUpCode::StartUpCode() + + +// Expanded macro to run code at finishup. +#define STATIC_FINISHUP_CODE_EXPANDED() \ + class FinishUpCode \ + { \ + public: \ + FinishUpCode() {}; \ + virtual ~FinishUpCode(); \ + }; \ + FinishUpCode myFinishUpCode; \ + FinishUpCode::~FinishUpCode() + + + +#endif // _STATIC_CONSTRUCTOR_H_ |