commit 767000fa0154a3e8cd2afef911f3aabca9e9ae42 Author: karutoh Date: Tue Nov 14 00:32:05 2023 -0800 Fixed project files. diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4a9af87 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,44 @@ +cmake_minimum_required(VERSION 3.18.4) +project(TechDemo C CXX) + +if (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows") + set(IS_OS_WINDOWS TRUE) + set(USER_HOME_DIRECTORY $ENV{USERPROFILE}) + message("Building for the Windows operating system.") +elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux") + set(IS_OS_LINUX TRUE) + set(USER_HOME_DIRECTORY $ENV{HOME}) + add_compile_options(-Wno-stringop-overflow) + message("Building for the Linux operating system.") +elseif (${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin") + set(IS_OS_MAC TRUE) + set(USER_HOME_DIRECTORY $ENV{HOME}) + message("Building for the Mac operating system.") +endif () + +if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "AMD64" OR ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "x86_64") + message("Building for AMD64 architecture.") + set(IS_ARCH_AMD64 TRUE) +elseif ("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "ARM64" OR "${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "aarch64") + message("Building for ARM64 architecture.") + set(IS_ARCH_ARM64 TRUE) +endif () + +set(CMAKE_CXX_STANDARD 20) + +add_executable(TechDemo main.cpp Levels/TestLevel.cpp Levels/TestLevel.h Levels/MainMenu.cpp Levels/MainMenu.h + Gui/HealthBarGui.cpp + Gui/HealthBarGui.h) + +if (IS_OS_WINDOWS) + add_compile_definitions(VK_USE_PLATFORM_WIN32_KHR) +elseif (IS_OS_LINUX) + add_compile_definitions(VK_USE_PLATFORM_XCB_KHR) +endif() + +target_link_directories(TechDemo PRIVATE "${USER_HOME_DIRECTORY}/Libraries/LWE/lib") +target_include_directories(TechDemo PRIVATE "${USER_HOME_DIRECTORY}/Libraries/LWE/include") + +find_package(Vulkan REQUIRED) + +target_link_libraries(TechDemo PRIVATE Vulkan::Headers Vulkan::Vulkan xcb xcb-xfixes xcb-xinput wooting_analog_wrapper LWE z asound) \ No newline at end of file diff --git a/Gui/HealthBarGui.cpp b/Gui/HealthBarGui.cpp new file mode 100644 index 0000000..085991c --- /dev/null +++ b/Gui/HealthBarGui.cpp @@ -0,0 +1,5 @@ +// +// Created by karutoh on 11/9/23. +// + +#include "HealthBarGui.h" diff --git a/Gui/HealthBarGui.h b/Gui/HealthBarGui.h new file mode 100644 index 0000000..4170b03 --- /dev/null +++ b/Gui/HealthBarGui.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +class HealthBarGui +{ + +}; \ No newline at end of file diff --git a/Levels/MainMenu.cpp b/Levels/MainMenu.cpp new file mode 100644 index 0000000..d5ee77b --- /dev/null +++ b/Levels/MainMenu.cpp @@ -0,0 +1,68 @@ +#include "MainMenu.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "TestLevel.h" + +MainMenu::MainMenu(const lwe::Str_8& id) + : Level(id) +{ +} + +void MainMenu::SetupResources() +{ + Level::SetupResources(); + +} + +void MainMenu::Setup() +{ + Level::Setup(); + + AddSystem(new lwe::CameraSystem()); + AddSystem(new lwe::RigidBodySystem()); + AddSystem(new lwe::GuiSystem()); + + lwe::Entity* cam = new lwe::Entity("Camera"); + + lwe::Camera* com = new lwe::Camera("Main"); + cam->AddComponent(com); + + AddEntity(cam); +} + +void MainMenu::PostInitialize(lwe::GpuCmdBuffer* cmdBuffer) +{ + Level::PostInitialize(cmdBuffer); +} + +void MainMenu::OnUpdate(lwe::Input* input, const float delta) +{ + Level::OnUpdate(input, delta); + + lwe::GameLoop* gl = (lwe::GameLoop*)GetParent("GameLoop"); + if (!gl) + return; + + lwe::RenderWindow* win = gl->GetWindow(); + if (!win) + return; + + lwe::Vec2_f client = win->GetScale(); + + lwe::Entity* sp = GetEntity("SinglePlayer"); + if (sp) + sp->SetPos({client.x * 0.5f - sp->GetScale().x * 0.5f, sp->GetPos().y, 0.0f}); + + lwe::Entity* mp = GetEntity("MultiPlayer"); + if (mp) + mp->SetPos({client.x * 0.5f - mp->GetScale().x * 0.5f, mp->GetPos().y, 0.0f}); +} diff --git a/Levels/MainMenu.h b/Levels/MainMenu.h new file mode 100644 index 0000000..5689696 --- /dev/null +++ b/Levels/MainMenu.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include +#include + +class MainMenu : public lwe::Level +{ +public: + MainMenu() = default; + + explicit MainMenu(const lwe::Str_8& id); + + MainMenu(const MainMenu& level) = default; + + MainMenu& operator=(const MainMenu& level) = default; + + void SetupResources() override; + + void Setup() override; + + void PostInitialize(lwe::GpuCmdBuffer* cmdBuffer) override; + + void OnUpdate(lwe::Input* input, const float delta) override; +}; \ No newline at end of file diff --git a/Levels/TestLevel.cpp b/Levels/TestLevel.cpp new file mode 100644 index 0000000..5f5d720 --- /dev/null +++ b/Levels/TestLevel.cpp @@ -0,0 +1,538 @@ +#include "TestLevel.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "LWE/Timer.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +TestLevel::TestLevel() + : count(0), cooldown(0.0f) +{ +} + +TestLevel::TestLevel(const lwe::Str_8& id) + : Level(id), count(0), cooldown(0.0f) +{ +} + +TestLevel::TestLevel(const TestLevel& level) + : Level(level), count(0), cooldown(0.0f) +{ +} + +TestLevel& TestLevel::operator=(const TestLevel& level) +{ + if (this == &level) + return *this; + + Level::operator=(level); + + count = 0; + cooldown = 0.0f; + + return *this; +} + +void TestLevel::SetupResources() +{ + lwe::FontAtlas* arial_24 = new lwe::FontAtlas("Resources/Fonts/Arial_24.ehf"); + AddResource(arial_24); + + lwe::Model* vampire = new lwe::Model("Resources/Models/Vampire.ehm"); + vampire->Calculate(); + AddResource(vampire); + AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Diffuse.png", lwe::IMG_ASPECT_COLOR)); + AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Normal.png", lwe::IMG_ASPECT_COLOR)); + AddResource(lwe::Img::FromFile_Heap("Resources/Textures/Character_Specular.png", lwe::IMG_ASPECT_COLOR)); + + lwe::Mesh* portrait = new lwe::Mesh("Portrait", lwe::portraitVerts, lwe::portraitIndices); + //portrait->Calculate(); + AddResource(portrait); + + lwe::Mesh* portraitGUI = new lwe::Mesh("PortraitGUI", lwe::portraitGuiVerts, lwe::portraitGuiIndices); + AddResource(portraitGUI); + + AddResource(new lwe::Model("Resources/Models/Cube.ehm")); + AddResource(new lwe::Model("Resources/Models/Sphere.ehm")); + AddResource(new lwe::Model("Resources/Models/PointLight.ehm")); + + lwe::CubeMap* cm = new lwe::CubeMap("Resources/Textures/Skybox", "Skybox"); + AddResource(cm); + + AddResource(lwe::Audio::FromFile_Heap("Resources/Audio/sample.wav", lwe::DataType::FLOAT)); +} + +void TestLevel::Setup() +{ + lwe::GameLoop* gl = (lwe::GameLoop*)GetParent(); + if (!gl) + return; + + /* + lwe::File configFile("Config.json", lwe::Mode::READ, lwe::Disposition::OPEN); + configFile.Initialize(); + + lwe::Json config(configFile.ReadStr(configFile.Size()), 0); + + configFile.UnInitialize(); + + if (gl->GetWindow()) + { + lwe::NetSystem* ns = new lwe::NetSystem(ehc::Disposition::ENDPOINT, *(lwe::JsonStr*)config.RetrieveValue("client.username"), 0); + ns->GetSocket()->SetAddressType(lwe::AddrType::IPV6); + ns->GetSocket()->EnableDropPackets(true); + ns->GetSocket()->SetMaxTimeout(*(lwe::JsonNum*)config.RetrieveValue("server_client.timeout")); + ns->GetSocket()->SetResendRate(*(lwe::JsonNum*)config.RetrieveValue("server_client.resendRate")); + + AddSystem(ns); + } + else + { + lwe::NetSystem* ns = new lwe::NetSystem(ehc::Disposition::SERVICE, *(lwe::JsonStr*)config.RetrieveValue("server.identifier"), (lwe::UInt_32)*(lwe::JsonNum*)config.RetrieveValue("server.maxPlayers")); + ns->GetSocket()->SetAddressType(lwe::AddrType::IPV6); + ns->GetSocket()->EnableDropPackets(true); + ns->GetSocket()->SetMaxTimeout(*(lwe::JsonNum*)config.RetrieveValue("server_client.timeout")); + ns->GetSocket()->SetResendRate(*(lwe::JsonNum*)config.RetrieveValue("server_client.resendRate")); + + ns->GetSocket()->SetActiveCb([](ehc::Socket* sock, ehc::Endpoint* end){ + lwe::Level* lvl = (lwe::Level*)sock->GetParent("Level"); + if (!lvl) + return; + + lwe::Entity* ply = lvl->CreateEntity("Puppet", "Ply-" + end->GetId()); + lwe::Networked* net = (lwe::Networked*)ply->GetComponent("Networked", ""); + net->SetOwner(end->GetHashId()); + + lwe::Serializer puppetPayload(lwe::Endianness::LE); + puppetPayload.Write(lwe::Str_8::Hash_64("Puppet")); + puppetPayload.WriteStr(ply->GetId()); + puppetPayload.Write(end->GetHashId()); + puppetPayload.WriteVec3(ply->GetPos()); + puppetPayload.WriteVec3(ply->GetRot()); + puppetPayload.WriteVec3(ply->GetScale()); + + lwe::Serializer playerPayload(lwe::Endianness::LE); + playerPayload.Write(lwe::Str_8::Hash_64("Player")); + playerPayload.WriteStr(ply->GetId()); + playerPayload.Write(end->GetHashId()); + playerPayload.WriteVec3(ply->GetPos()); + playerPayload.WriteVec3(ply->GetRot()); + playerPayload.WriteVec3(ply->GetScale()); + + lwe::Array endpoints = sock->GetEndpoints(ehc::Disposition::ENDPOINT, ehc::Status::ACTIVE); + for (lwe::UInt_64 i = 0; i < endpoints.Size(); ++i) + { + if (endpoints[i] == end) + endpoints[i]->Send(false, true, true, "LWE", "CreateEnt", playerPayload); + else + { + lwe::Serializer otherPuppet(lwe::Endianness::LE); + otherPuppet.Write(lwe::Str_8::Hash_64("Puppet")); + otherPuppet.WriteStr("Ply-" + endpoints[i]->GetId()); + playerPayload.Write(endpoints[i]->GetHashId()); + otherPuppet.WriteVec3(ply->GetPos()); + otherPuppet.WriteVec3(ply->GetRot()); + otherPuppet.WriteVec3(ply->GetScale()); + end->Send(false, true, true, "LWE", "CreateEnt", otherPuppet); + + endpoints[i]->Send(false, true, true, "LWE", "CreateEnt", puppetPayload); + } + } + }); + + ns->GetSocket()->SetDisconnectedCb([](ehc::Socket* sock, ehc::Endpoint* end){ + Level* lvl = (Level*)sock->GetParent("Level"); + if (!lvl) + return; + + lwe::Entity* ply = lvl->GetEntity("Ply-" + end->GetId()); + if (!ply) + return; + + lwe::Serializer payload(lwe::Endianness::LE); + payload.Write(ply->GetHashId()); + + lwe::Array endpoints = sock->GetEndpoints(ehc::Disposition::ENDPOINT, ehc::Status::ACTIVE); + for (lwe::UInt_64 i = 0; i < endpoints.Size(); ++i) + { + if (endpoints[i] == end) + continue; + + endpoints[i]->Send(false, true, true, "LWE", "DeleteEnt", payload); + } + + ply->Delete(); + }); + + AddSystem(ns); + } + */ + + AddSystem(new lwe::DynamicsSystem()); + AddSystem(new lwe::RigidBodySystem()); + AddSystem(new lwe::CameraSystem()); + AddSystem(new lwe::SkyboxSystem()); + AddSystem(new lwe::PhongSystem()); + AddSystem(new lwe::SolidColorSystem()); + AddSystem(new lwe::BillboardSystem()); + + lwe::LightSystem* ls = new lwe::LightSystem(true); + ls->SetAmbient({0.25f, 0.25f, 0.25f}); + AddSystem(ls); + + //AddSystem(new lwe::AudioSystem()); + + lwe::TimerSystem* timerSys = new lwe::TimerSystem(); + AddSystem(timerSys); + + lwe::GuiSystem* guiSys = new lwe::GuiSystem(); + AddSystem(guiSys); + + lwe::CollectionGui* stats = new lwe::CollectionGui("Stats"); + stats->SetScale({250.0f, 127.0f}); + + stats->AddResource(new lwe::FontAtlas("Resources/Fonts/Arial_24.ehf")); + + lwe::SolidBoxGui* bckgrd = new lwe::SolidBoxGui("Background"); + bckgrd->SetScale({250.0f, 127.0f}); + bckgrd->SetColor({0.0f, 0.0f, 0.0f, 0.5f}); + stats->AddChild(bckgrd); + + lwe::LabelGui* fps = new lwe::LabelGui("FPS", "Arial_24", "FPS: 0"); + fps->SetColor({1.0f}); + stats->AddChild(fps); + + lwe::LabelGui* deltaTime = new lwe::LabelGui("DeltaTime", "Arial_24", "Delta Time: 0"); + deltaTime->SetColor({1.0f}); + deltaTime->SetPosition({0.0f, 24.0f}); + stats->AddChild(deltaTime); + + lwe::LabelGui* gbg = new lwe::LabelGui("Garbage", "Arial_24", "Garbage: 0"); + gbg->SetColor({1.0f}); + gbg->SetPosition({0.0f, 48.0f}); + stats->AddChild(gbg); + + lwe::LabelGui* playback = new lwe::LabelGui("Playback", "Arial_24", "Playback: 0:0 / 0:0"); + playback->SetColor({1.0f}); + playback->SetPosition({0.0f, 72.0f}); + stats->AddChild(playback); + + lwe::LabelGui* volume = new lwe::LabelGui("Volume", "Arial_24", "Volume: 0"); + volume->SetColor({1.0f}); + volume->SetPosition({0.0f, 96.0f}); + stats->AddChild(volume); + + guiSys->AddGui(stats); + + lwe::Serializer args(lwe::Endianness::LE); + args.Write(stats); + args.SetOffset(0); + + timerSys->Add({0, 1.0f, args, [](lwe::Timer* timer, lwe::Serializer& args) + { + lwe::GameLoop* gl = (lwe::GameLoop*)timer->GetParent("GameLoop"); + + lwe::CollectionGui* stats = args.Read(); + args.SetOffset(0); + + lwe::LabelGui* deltaTime = (lwe::LabelGui*)stats->GetChild("DeltaTime"); + deltaTime->SetText("Delta Time: " + lwe::Str_8::FromNum(gl->GetRawDeltaTime())); + + lwe::LabelGui* gbg = (lwe::LabelGui*)stats->GetChild("Garbage"); + gbg->SetText("Garbage: " + lwe::Str_8::FromNum(lwe::GarbageCollector::Size())); + }}); + + lwe::ButtonGui* testBttn = new lwe::ButtonGui("Test", "Test"); + testBttn->SetScale({200.0f, 50.0f}); + testBttn->SetPosition({400.0f, 400.0f}); + guiSys->AddGui(testBttn); + + lwe::Entity* player = new lwe::Entity("Player"); + + //player->AddComponent(new lwe::Networked(true, false, lwe::EndpointOwner::ENDPOINT)); + player->AddComponent(new lwe::Point3D("Head")); + player->AddComponent(new lwe::PlyController("Head")); + //player->AddComponent(new lwe::Health(100, 100)); + + lwe::SolidColor* plyMdl = new lwe::SolidColor("Cube", {0.0f, 0.0f, 1.0f}, "Cube", ""); + plyMdl->SetDiffused(true); + plyMdl->SetScale({0.5f}); + plyMdl->SetPos({0.0f, 0.0f, -0.5f}); + //player->AddComponent(plyMdl); + + AddTemplate(player); + + lwe::Entity* plyPuppet = new lwe::Entity("Puppet"); + plyPuppet->AddComponent(new lwe::OBB3D("Head")); + plyPuppet->AddComponent(new lwe::Camera("Head")); + plyPuppet->AddComponent(new lwe::Health(100, 100)); + + lwe::SolidColor* puppetMdl = new lwe::SolidColor("Cube", {0.0f, 0.0f, 1.0f}, "Cube", ""); + puppetMdl->SetDiffused(true); + puppetMdl->SetScale({0.5f}); + puppetMdl->SetPos({0.0f, 0.0f, -0.5f}); + //plyPuppet->AddComponent(puppetMdl); + + AddTemplate(plyPuppet); + + CreateEntity(player->GetHashId(), "Player"); + + lwe::Entity* skyboxEnt = new lwe::Entity("Skybox"); + + lwe::Skybox* skybox = new lwe::Skybox("Skybox", "Skybox"); + skyboxEnt->AddComponent(skybox); + + AddEntity(skyboxEnt); + + lwe::Entity* ent = new lwe::Entity("Main"); + ent->SetScale({0.25f, 0.25f, 0.25f}); + ent->SetPos({0.0f, -1.0f, 5.0f}); + //ent->SetRot({-90.0f, 180.0f, 0.0f}); + + //ent->AddComponent(new lwe::Networked(false, false, lwe::EndpointOwner::SERVICE)); + + lwe::AudioSource3D* as = new lwe::AudioSource3D("Song", "sample", 20.0f); + as->Pause(); + as->EnableLoop(true); + //as->EnablePanning(false); + //as->EnableAttenuation(false); + ent->AddComponent(as); + + lwe::Sphere* sphere = new lwe::Sphere("Sphere", 5.0f); + ent->AddComponent(sphere); + + /* + lwe::AudioSource* as2 = new lwe::AudioSource("Song2", "sample"); + as2->Play(); + ent->AddComponent(as2); + */ + + lwe::Phong* ph = new lwe::Phong("Cube", "Vampire", "", "Character_Diffuse", "Character_Specular", "Character_Normal"); + ph->SetAnimation("Test"); + ent->AddComponent(ph); + + AddEntity(ent); + + lwe::Entity* sun = new lwe::Entity("Sun"); + sun->SetPos({-5.0f, 0.0f, 5.0f}); + + lwe::AmbientPointLight* sunLight = new lwe::AmbientPointLight("Light"); + sunLight->SetDiffuse({1.0f, 0.77254901960784313725490196078431f, 0.56078431372549019607843137254902f}); + sun->AddComponent(sunLight); + + AddEntity(sun); + + lwe::Entity* emergency = new lwe::Entity("Emergency"); + emergency->SetPos({5.0f, 0.0f, 5.0f}); + + lwe::PointLight* pl = new lwe::PointLight("Light"); + pl->SetColor({1.0f, 0.0f, 0.0f}); + emergency->AddComponent(pl); + + AddEntity(emergency); + + lwe::Entity* testEnt = new lwe::Entity("Test"); + + lwe::Billboard* testBb = new lwe::Billboard("TestBB", "Character_Diffuse"); + testEnt->AddComponent(testBb); + + AddEntity(testEnt); + + lwe::Entity* bullet = new lwe::Entity("Bullet"); + bullet->SetScale({0.1f, 0.1f, 0.1f}); + + bullet->AddComponent(new lwe::Projectile(25)); + bullet->AddComponent(new lwe::Dynamics(50.0f)); + bullet->AddComponent(new lwe::Point3D("Tip")); + + lwe::SolidColor* bulletMdl = new lwe::SolidColor("Mdl", {1.0f, 1.0f, 0.0f, 1.0f}, "Cube", ""); + bullet->AddComponent(bulletMdl); + + AddTemplate(bullet); +} + +void TestLevel::PostInitialize(lwe::GpuCmdBuffer* cmdBuffer) +{ + lwe::GameLoop* gl = (lwe::GameLoop*)GetParent(); + if (!gl) + return; + + /* + lwe::NetSystem* ns = (lwe::NetSystem*)GetSystem("NetSystem"); + if (!ns) + return; + + lwe::File configFile("Config.json", lwe::Mode::READ, lwe::Disposition::OPEN); + configFile.Initialize(); + + lwe::Json config(configFile.ReadStr(configFile.Size()), 0); + + configFile.UnInitialize(); + + if (gl->GetWindow()) + { + lwe::Str_8 address = lwe::DNS::Resolve(lwe::AddrType::IPV6, *(lwe::JsonStr*)config.RetrieveValue("client.connectAddress")); + lwe::UInt_16 port = (lwe::UInt_16)*(lwe::JsonNum*) config.RetrieveValue("client.connectPort"); + ns->GetSocket()->Connect(address, port); + + EHS_LOG("Info", "TechDemo", 0, "Connecting to server, standby."); + } + else + { + lwe::Str_8 addr = ((lwe::JsonStr*)config.RetrieveValue("server.address"))->value; + if (addr.Size()) + addr = lwe::DNS::Resolve(lwe::AddrType::IPV6, addr); + + lwe::UInt_16 port = (lwe::UInt_16) *(lwe::JsonNum*) config.RetrieveValue("server.port"); + ns->GetSocket()->Bind(addr, port); + + EHS_LOG("Info", "TechDemo", 0, "Server successfully initialized with the id, \"" + ns->GetSocket()->GetId() + "\"."); + } + */ +} + +void TestLevel::OnUpdate(lwe::Input* input, const float delta) +{ + lwe::GameLoop* gl = (lwe::GameLoop*)GetParent("GameLoop"); + if (!gl) + return; + + lwe::GenericInputHandler* handler = (lwe::GenericInputHandler*)input->GetHandler("GenericInputHandler"); + if (!handler) + return; + + const lwe::Mouse* mouse = handler->GetPrimaryMouse(); + const lwe::Keyboard* keyboard = handler->GetPrimaryKeyboard(); + + lwe::Entity* ent = GetEntity("Main"); + + lwe::Entity* ply = GetEntity("Player"); + if (ply) + { + lwe::PlyController* cam = (lwe::PlyController*)ply->GetComponent("PlyController", "Head"); + if (cam) + { + lwe::CameraSystem* camSys = (lwe::CameraSystem*)GetSystem("CameraSystem"); + if (camSys) + camSys->SetPrimary(ply->GetHashId(), cam->GetHashId()); + + if (cooldown <= 0.0f && mouse->IsDown(lwe::Mouse::LMB)) // Left Mouse Button + { + lwe::AudioSource* gunshot = new lwe::AudioSource("Gunshot_" + lwe::Str_8::FromNum(count), "Gunshot"); + gunshot->SetVolume(1.0f); + ply->AddComponent(gunshot); + + lwe::Entity* bullet = CreateEntity("Bullet", "Bullet_" + lwe::Str_8::FromNum(count++)); + bullet->SetPos(ply->GetPos() + cam->GetTransform().GetForward() * 2.0f); + bullet->SetScale({0.1f, 0.1f, 0.1f}); + bullet->SetRot(ply->GetRot()); + + lwe::Dynamics* dynamics = (lwe::Dynamics*)bullet->GetComponent("Dynamics"); + dynamics->SetVelocity(cam->GetTransform().GetForward() * 100.0f); + + cooldown = 0.15f; + } + } + } + + if (cooldown > 0.0f) + { + if (cooldown - delta < 0.0f) + cooldown = 0.0f; + else + cooldown -= delta; + } + + lwe::GuiSystem* guiSys = (lwe::GuiSystem*)GetSystem("GuiSystem"); + if (!guiSys) + return; + + lwe::CollectionGui* stats = (lwe::CollectionGui*)guiSys->GetGui("Stats"); + if (!stats) + return; + + lwe::LabelGui* fps = (lwe::LabelGui*)stats->GetChild("FPS"); + fps->SetText("FPS: " + lwe::Str_8::FromNum(gl->GetTPS())); + + lwe::AudioSource* source = (lwe::AudioSource*)ent->GetComponent("AudioSource3D", "Song"); + if (!source) + return; + + lwe::Audio* audio = (lwe::Audio*)GetResource("Audio", source->GetAudioHashId()); + if (!audio) + return; + + lwe::LabelGui* playback = (lwe::LabelGui*)stats->GetChild("Playback"); + + lwe::UInt_64 elapsed = source->GetFrameOffset() / audio->GetSampleRate(); + lwe::UInt_64 duration = audio->GetFrameCount() / audio->GetSampleRate(); + + playback->SetText("Playback: " + lwe::Str_8::FromNum(elapsed / 60) + ":" + lwe::Str_8::FromNum(elapsed % 60) + + " / " + lwe::Str_8::FromNum(duration / 60) + ":" + lwe::Str_8::FromNum(duration % 60)); + + if (keyboard->IsJustReleased(lwe::Keyboard::Left)) + source->SetVolume(source->GetVolume() - 0.25f); + + if (keyboard->IsJustReleased(lwe::Keyboard::Right)) + source->SetVolume(source->GetVolume() + 0.25f); + + lwe::LabelGui* volume = (lwe::LabelGui*)stats->GetChild("Volume"); + volume->SetText("Volume: " + lwe::Str_8::FromNum(source->GetVolume())); + + if (keyboard->IsJustReleased(lwe::Keyboard::P)) + { + if (source->IsPlaying()) + source->Pause(); + else + source->Play(); + } + + if (keyboard->IsJustReleased(lwe::Keyboard::Backspace)) + source->Reset(); +} diff --git a/Levels/TestLevel.h b/Levels/TestLevel.h new file mode 100644 index 0000000..de8728b --- /dev/null +++ b/Levels/TestLevel.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include +#include + +class TestLevel : public lwe::Level +{ +private: + lwe::UInt_32 count; + float cooldown; + +public: + TestLevel(); + + explicit TestLevel(const lwe::Str_8& id); + + TestLevel(const TestLevel& level); + + TestLevel& operator=(const TestLevel& level); + + void SetupResources() override; + + void Setup() override; + + void PostInitialize(lwe::GpuCmdBuffer* cmdBuffer) override; + + void OnUpdate(lwe::Input* input, const float delta) override; +}; \ No newline at end of file diff --git a/Resources.rc b/Resources.rc new file mode 100644 index 0000000..49ea5f5 --- /dev/null +++ b/Resources.rc @@ -0,0 +1,40 @@ +#include + +1 VERSIONINFO +FILEVERSION 1, 0, 0, 0 +PRODUCTVERSION 1, 0, 0, 0 +FILEFLAGSMASK 0x17L + +#ifdef _DEBUG + FILEFLAGS VS_FF_DEBUG +#else + FILEFLAGS 0x0L +#endif + +FILEOS VOS_NT_WINDOWS32 +FILETYPE VFT_APP +FILESUBTYPE VFT2_UNKNOWN + +BEGIN + BLOCK "StringFileInfo" + BEGIN + BLOCK "040904E4" + BEGIN + VALUE "Comments", "\0" + VALUE "CompanyName", "Event Horizon Studio\0" + VALUE "FileDescription", "Tech Demo\0" + VALUE "FileVersion", "1.0.0.0\0" + VALUE "InternalName", "TechDemo\0" + VALUE "LegalCopyright", "Event Horizon Studio\0" + VALUE "LegalTrademarks", "Event Horizon Studio\0" + VALUE "OriginalFilename", "TechDemo.exe\0" + VALUE "ProductName", "TechDemo\0" + VALUE "ProductVersion", "1.0.0.0\0" + END + END + + BLOCK "VarFileInfo" + BEGIN + VALUE "Translation", 0x409, 1252 + END +END \ No newline at end of file diff --git a/appicon.rc b/appicon.rc new file mode 100644 index 0000000..15cdd19 --- /dev/null +++ b/appicon.rc @@ -0,0 +1 @@ +IDI_ICON1 ICON DISCARDABLE "TechDemo.ico" \ No newline at end of file diff --git a/gitignore b/gitignore new file mode 100644 index 0000000..259148f --- /dev/null +++ b/gitignore @@ -0,0 +1,32 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..e9d5d1f --- /dev/null +++ b/main.cpp @@ -0,0 +1,214 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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 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 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; +} \ No newline at end of file