Static Libraries and ZERO Warnings

引言

也许是时候停下脚步,整理一下我们在引擎开发中遗留的各种警告与问题了。我们将引擎构建为静态库,并且清除所有警告!

由于目前的进度和各种警告问题,我们需要将引擎改为静态库,而不是像 DOOM 一样引擎作为 dll,游戏再链接到该 dll 上,在未来我们会尝试采取此模式。

预生成文件

在 VSCode 中打开所有预生成文件。

Infinite

workspace "Infinite"
architecture "x64"
startproject "Sandbox"

configurations
{
"Debug",
"Release",
"Dist"
}

outputdir = "%{cfg.buildcfg}-%{cfg.system}-%{cfg.architecture}"

-- Include directories relative to root folder (solution directory)
IncludeDir = {}
IncludeDir["GLFW"] = "Infinite/vendor/GLFW/include"
IncludeDir["GLAD"] = "Infinite/vendor/GLAD/include"
IncludeDir["ImGui"] = "Infinite/vendor/imgui"
IncludeDir["glm"] = "Infinite/vendor/glm"

include "Infinite/vendor/GLFW"
include "Infinite/vendor/GLAD"
include "Infinite/vendor/imgui"

project "Infinite"
location "Infinite"
kind "StaticLib"
language "C++"
+ staticruntime "on"
+ cppdialect "C++17"

targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

pchheader "ifnpch.h"
pchsource "Infinite/src/ifnpch.cpp"

files
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp",
"%{prj.name}/vendor/glm/glm/**.hpp",
"%{prj.name}/vendor/glm/glm/**.inl"
}

+ defines
+ {
+ "_CRT_SECURE_NO_WARNINGS"
+ }


includedirs
{
"%{prj.name}/src",
"%{prj.name}/vendor/spdlog/include",
"%{IncludeDir.GLFW}",
"%{IncludeDir.GLAD}",
"%{IncludeDir.ImGui}",
"%{IncludeDir.glm}"
}

links
{
"GLFW",
"GLAD",
"ImGui",
"opengl32.lib"
}

filter "system:windows"
systemversion "latest"

defines
{
"IFN_PLATFORM_WINDOWS",
"IFN_BUILD_DLL",
"GLFW_INCLUDE_NONE"
}

filter "configurations:Debug"
defines "IFN_DEBUG"
runtime "Debug"
+ symbols "on"

filter "configurations:Release"
defines "IFN_RELEASE"
runtime "Release"
+ optimize "on"

filter "configurations:Dist"
defines "IFN_DIST"
runtime "Release"
+ optimize "on"



project "Sandbox"
location "Sandbox"
kind "ConsoleApp"
language "C++"
+ cppdialect "C++17"
+ staticruntime "on"



targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

files
{
"%{prj.name}/src/**.h",
"%{prj.name}/src/**.cpp"
}

includedirs
{
"Infinite/vendor/spdlog/include",
"Infinite/src/Infinite",
"Infinite/src/Events",
"Infinite/src/**.h",
"Infinite/src/**.cpp",
"Infinite/vendor/imgui",
"%{IncludeDir.glm}"
}

links
{
"Infinite"
}

filter "system:windows"
systemversion "latest"

defines
{
"IFN_PLATFORM_WINDOWS"
}

filter "configurations:Debug"
defines "IFN_DEBUG"
runtime "Debug"
+ symbols "on"

filter "configurations:Release"
defines "IFN_RELEASE"
runtime "Release"
+ optimize "on"

filter "configurations:Dist"
defines "IFN_DIST"
runtime "Release"
+ optimize "on"

将引擎的预生成文件转化为 .lib 静态库。

ImGUI

之前出现的错误是由于我擅自修改了 imgui 的预生成文件:

project "ImGui"
kind "StaticLib"
language "C++"
staticruntime "on"

targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

files
{
"imconfig.h",
"imgui.h",
"imgui.cpp",
"imgui_draw.cpp",
"imgui_internal.h",
"imgui_tables.cpp",
"imgui_widgets.cpp",
"imstb_rectpack.h",
"imstb_textedit.h",
"imstb_truetype.h",
"imgui_demo.cpp"
}

filter "system:windows"
systemversion "latest"
cppdialect "C++17"
staticruntime "On"

filter { "system:windows", "configurations:Release" }
buildoptions "/MT"

GLAD

project "GLAD"
kind "StaticLib"
language "C"
staticruntime "on"

targetdir ("bin/" .. outputdir .. "/%{prj.name}")
objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

files
{
"include/glad/glad.h",
"include/KHR/khrplatform.h",
"src/glad.c"
}

includedirs
{
"include"
}

filter "system:windows"
systemversion "latest"

filter "configurations:Debug"
runtime "Debug"
symbols "on"

filter "configurations:Release"
runtime "Release"
optimize "on"

类型转换

ImGuiLayer.cpp 还有一个类型转换的问题:

	void ImGuiLayer::End()
{
ImGuiIO& io = ImGui::GetIO();

Application& app = Application::Get();
+ io.DisplaySize = ImVec2((float)app.GetWindow().GetWidth(), (float)app.GetWindow().GetHeight());

// Rendering
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
GLFWwindow* backup_current_context = glfwGetCurrentContext();
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
glfwMakeContextCurrent(backup_current_context);
}
}

调试

零报错零警告,引擎正常运行!