initial commit
This commit is contained in:
@@ -18,6 +18,17 @@ void BeginPlay(veng::Engine& engine) {
|
||||
veng::Model* const player = engine.SpawnModel("player", "player");
|
||||
player->scale = glm::vec3(.02f);
|
||||
player->colision = true;
|
||||
player->OnColision = [](veng::Model* self, veng::Model* other) {
|
||||
if (other->owner == self) return;
|
||||
std::cout << self << " and " << other << " is Nearby." << std::endl;
|
||||
|
||||
std::cout << self << "'s owner: " << self->owner << std::endl;
|
||||
std::cout << other << "'s owner: " << other->owner << std::endl;
|
||||
|
||||
std::cout << "Colided." << std::endl;
|
||||
other->colision = false;
|
||||
other->visible = false;
|
||||
};
|
||||
veng::Model* const player_flame =
|
||||
engine.SpawnModel("player_flame", "player_flame");
|
||||
player_flame->scale = player->scale;
|
||||
|
||||
82
Client/src/utils/thread_pool.cpp
Normal file
82
Client/src/utils/thread_pool.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "utils/thread_pool.h"
|
||||
#include "precomp.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
ThreadPool::ThreadPool() : ThreadPool(0) {}
|
||||
|
||||
ThreadPool::ThreadPool(std::uint32_t numThreads) { init(numThreads); }
|
||||
|
||||
ThreadPool::~ThreadPool() { terminate(); }
|
||||
|
||||
void ThreadPool::init(std::uint32_t numThreads) {
|
||||
int numCPU = numThreads;
|
||||
if (numThreads == 0) {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
numCPU = sysinfo.dwNumberOfProcessors;
|
||||
#elif __linux__
|
||||
numCPU = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#endif
|
||||
spdlog::info("Auto-detected cpu count: {}", numCPU);
|
||||
if (numCPU == 1 || numCPU == 2) {
|
||||
numCPU = 4;
|
||||
spdlog::info(
|
||||
"Set ThreadPool Worker count to: {} due to program to oprate "
|
||||
"concurrently",
|
||||
numCPU);
|
||||
} else {
|
||||
spdlog::info("Set ThreadPool Worker count to: {}", numCPU);
|
||||
}
|
||||
}
|
||||
threadCount = numCPU;
|
||||
workers_.reserve(numCPU);
|
||||
|
||||
while (numCPU--) workers_.emplace_back([this]() { this->Worker(); });
|
||||
}
|
||||
|
||||
void ThreadPool::terminate() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
spdlog::debug("waiting for threads to end their jobs...");
|
||||
for (auto& t : workers_) t.join();
|
||||
}
|
||||
|
||||
void ThreadPool::respawnWorker(std::uint32_t numThreads) {
|
||||
terminate();
|
||||
terminate_ = false;
|
||||
init(numThreads);
|
||||
}
|
||||
|
||||
void* ThreadPool::Worker() {
|
||||
#ifdef _WIN32
|
||||
DWORD pid = GetCurrentThreadId();
|
||||
#elif __linux__
|
||||
pthread_t pid = pthread_self();
|
||||
#endif
|
||||
spdlog::trace("ThreadPool Worker : {} up", pid);
|
||||
while (!terminate_) {
|
||||
std::unique_lock<std::mutex> lock(jobQueueMutex);
|
||||
jobQueueCV_.wait(lock,
|
||||
[this]() { return !this->jobs_.empty() || terminate_; });
|
||||
if (this->jobs_.empty() || terminate_) {
|
||||
jobs_ = std::queue<std::packaged_task<void()>>();
|
||||
break;
|
||||
}
|
||||
if (this->jobs_.empty()) continue;
|
||||
|
||||
auto job = std::move(jobs_.front());
|
||||
jobs_.pop();
|
||||
lock.unlock();
|
||||
|
||||
spdlog::trace("ThreadPool Worker : {} Executing a job", pid);
|
||||
job();
|
||||
}
|
||||
spdlog::trace("ThreadPool Worker : {} down", pid);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace Chattr
|
||||
@@ -14,17 +14,6 @@ void Engine::init() {
|
||||
1000.f);
|
||||
vulkan_graphics->SetViewProjection(view, projection);
|
||||
|
||||
for (auto it = model_assets_.begin(); it != model_assets_.end();) {
|
||||
it->second.vertex_buffer =
|
||||
vulkan_graphics->CreateVertexBuffer(it->second.vertices);
|
||||
it->second.index_buffer =
|
||||
vulkan_graphics->CreateIndexBuffer(it->second.indices);
|
||||
it->second.material.texture_handle =
|
||||
vulkan_graphics->CreateTexture(it->second.material.texture_image);
|
||||
|
||||
++it;
|
||||
}
|
||||
|
||||
if (BeginPlay != nullptr) BeginPlay(*this);
|
||||
}
|
||||
|
||||
@@ -32,7 +21,11 @@ void Engine::LoadModelAsset(std::string path, std::string name) {
|
||||
veng::Model model(vulkan_graphics);
|
||||
asset_loader_.setPath(path);
|
||||
asset_loader_.loadModel(model);
|
||||
model.material.texture_image = asset_loader_.readTexture();
|
||||
|
||||
model.vertex_buffer = vulkan_graphics->CreateVertexBuffer(model.vertices);
|
||||
model.index_buffer = vulkan_graphics->CreateIndexBuffer(model.indices);
|
||||
model.material.texture_handle =
|
||||
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
|
||||
model_assets_[name] = std::move(model);
|
||||
}
|
||||
|
||||
@@ -175,7 +168,7 @@ void Engine::Update() {
|
||||
vulkan_graphics->RenderModel(it);
|
||||
}
|
||||
|
||||
physics_controller_.invokeOnColisionEvent({models.data(), models.size()});
|
||||
physics_controller_.invokeOnColisionEvent(&thread_pool_, {models.data(), models.size()});
|
||||
|
||||
vulkan_graphics->EndFrame();
|
||||
}
|
||||
|
||||
@@ -6,8 +6,9 @@
|
||||
#include "precomp.h"
|
||||
|
||||
namespace veng {
|
||||
void Physics::invokeOnColisionEvent(gsl::span<Model*> models) {
|
||||
const std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
|
||||
void Physics::invokeOnColisionEvent(
|
||||
gsl::not_null<utils::ThreadPool*> thread_pool, gsl::span<Model*> models) {
|
||||
constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
|
||||
|
||||
for (int first = 0; first < models.size(); first++) {
|
||||
if (!models[first]->colision) continue;
|
||||
@@ -21,9 +22,19 @@ void Physics::invokeOnColisionEvent(gsl::span<Model*> models) {
|
||||
models[second]->radius * models[second]->scale.x;
|
||||
if (distance <= model1_radius + model2_radius) {
|
||||
if (models[first]->OnColision != nullptr)
|
||||
models[first]->OnColision(models[first], models[second]);
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[first]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[first], models[second]);
|
||||
if (models[second]->OnColision != nullptr)
|
||||
models[second]->OnColision(models[second], models[first]);
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[second]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[second], models[first]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user