TechDemo/main.cpp
2023-11-14 00:32:05 -08:00

214 lines
4.9 KiB
C++

#include <LWE/LWE.h>
#include <LWE/Str.h>
#include <LWE/Log.h>
#include <LWE/GarbageCollector.h>
#include <LWE/Array.h>
#include <LWE/IO/File.h>
#include <LWE/IO/Console.h>
#include <LWE/System/Thread.h>
#include <LWE/Json/Json.h>
#include <LWE/IO/Image/PNG.h>
#include <LWE/Gpu/GpuDevice.h>
#include <LWE/Gpu/GpuInterface.h>
#include <LWE/IO/RenderWindow.h>
#include <LWE/GameLoop.h>
#include <LWE/IO/HID/Wooting.h>
#include <LWE/IO/HID/GenericInputHandler.h>
#include <vulkan/vulkan.h>
#include "Levels/MainMenu.h"
#include "Levels/TestLevel.h"
bool running = true;
lwe::UInt_32 GcCb(void* params)
{
while (running)
{
lwe::GarbageCollector::Poll();
lwe::Thread::SleepFor(50);
}
lwe::GarbageCollector::Dump();
return 0;
}
void LogRaised(const lwe::Log& log)
{
lwe::Array<lwe::Str_8> tags = log.GetTags();
lwe::Str_8 result = "{";
if (log.HasTag("Info"))
{
result += "Info";
result += "}: " + log.GetMsg();
}
else
{
for (lwe::UInt_32 i = 0; i < tags.Size(); ++i)
{
result += tags[i];
if (i != tags.Size() - 1)
result += ", ";
}
result += "} (" + lwe::Str_8::FromNum(log.GetCode()) + "): " + log.GetMsg();
}
lwe::Console::Write_8(result);
if (log.HasTag("Error"))
{
lwe::Console::Read_8();
}
}
lwe::SInt_32 Main(lwe::Str_8* appName, lwe::Str_8* appVerId, lwe::Version* appVer)
{
*appName = "TechDemo";
*appVerId = "Release";
*appVer = {1, 0, 0};
lwe::Log::SetCallback(LogRaised);
lwe::File configFile("Config.json", lwe::Mode::READ_WRITE, lwe::Disposition::OPEN_PERSISTENT);
if (!configFile.Size())
{
lwe::JsonObj root;
lwe::JsonObj server;
server.AddVar({"identifier", ""});
server.AddVar({"address", ""});
server.AddVar({"port", 7840});
server.AddVar({"maxPlayers", 0});
root.AddVar({"server", server});
lwe::JsonObj client;
client.AddVar({"username", ""});
client.AddVar({"connectAddress", ""});
client.AddVar({"connectPort", 7840});
root.AddVar({"client", client});
lwe::JsonObj server_client;
server_client.AddVar({"timeout", 5});
server_client.AddVar({"resendRate", 0.5f});
root.AddVar({"server_client", server_client});
lwe::JsonObj engine;
engine.AddVar({"threadCount", 0});
engine.AddVar({"timeLocked", true});
engine.AddVar({"maxTPS", 60});
root.AddVar({"engine", engine});
lwe::Json config(root);
configFile.WriteStr_8(config.ToStr(false));
configFile.Release();
return 0;
}
lwe::Json config(configFile.ReadStr_8(configFile.Size()), 0);
configFile.Release();
lwe::Array<lwe::Str_8> args = lwe::Console::GetArgs_8();
if (args.Size() > 1 && lwe::Str_8::Cmp(args[1], "-server"))
{
lwe::Console::Attach();
lwe::Console::SetTitle_8("TechDemo");
LWE_LOG("Info", "TechDemo", 0, "Initializing server, standby.");
lwe::GameLoop gl((lwe::UInt_8)*(lwe::JsonNum*)config.RetrieveValue("engine.threadCount"), 0);
gl.EnableTimeLock(*(lwe::JsonBool*)config.RetrieveValue("engine.timeLocked"));
gl.SetMaxTPS((lwe::UInt_32)*(lwe::JsonNum*)config.RetrieveValue("engine.maxTPS"));
lwe::Frame mainFrame("Main");
gl.AddFrame(&mainFrame);
mainFrame.AddLevel(new MainMenu("MainMenu"));
lwe::GarbageCollector::SetStride(1000);
lwe::GarbageCollector::SetMax(10);
lwe::Thread gcThread;
gcThread.Start(GcCb, nullptr);
gl.Initialize();
gl.Start();
gl.UnInitialize();
running = false;
gcThread.Join();
}
else
{
lwe::Console::Attach();
#if defined(LWE_OS_WINDOWS)
lwe::GpuInstance::AddExtension(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
#elif defined(LWE_OS_LINUX)
lwe::GpuInstance::AddExtension(VK_KHR_XCB_SURFACE_EXTENSION_NAME);
#endif
lwe::GpuInstance::AddExtension(VK_KHR_SURFACE_EXTENSION_NAME);
lwe::GpuInstance::Initialize(false);
lwe::GpuDevice device = std::move(lwe::GpuDevice::GetBest());
lwe::GpuQueueFamily* family = device.GetQueueFamily(lwe::QueueType::GRAPHICS);
lwe::GpuInterface inf(&device);
inf.AddExtensions(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
lwe::GpuQueue primary(family, &inf, 1.0f);
inf.Initialize();
lwe::RenderWindow win(&inf, &primary);
win.Create_8(lwe::GetAppName_8(), {0, 0}, {1024, 768});
win.Show();
lwe::GameLoop gl(&win, (lwe::UInt_8)*(lwe::JsonNum*)config.RetrieveValue("engine.threadCount"), 0);
gl.EnableTimeLock(*(lwe::JsonBool*)config.RetrieveValue("engine.timeLocked"));
gl.SetMaxTPS((lwe::UInt_32)*(lwe::JsonNum*)config.RetrieveValue("engine.maxTPS"));
lwe::Input* input = gl.GetInput();
input->AddHandler(new lwe::GenericInputHandler());
lwe::Frame mainFrame("Main");
gl.AddFrame(&mainFrame);
mainFrame.AddLevel(new TestLevel("Test"));
lwe::GarbageCollector::SetStride(1000);
lwe::GarbageCollector::SetMax(10);
lwe::Thread gcThread;
gcThread.Start(GcCb, nullptr);
gl.Initialize();
gl.Start();
gl.UnInitialize();
win.Close();
running = false;
gcThread.Join();
inf.Release();
lwe::GpuInstance::Release();
//lwe::AudioEngine::UnInitialize();
}
lwe::Console::Free();
return 0;
}