116 if (!allocateContainerMemory(container, containerInformation, (
void*)memoryPos))
118 logManager.
log((std::string(
"Couldn't allocate memory for constructing container: #")
131 SDL_Log(
"Failed to retrieve window: %s", SDL_GetError());
136 SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 400, 400);
143 loadedDll.bindAllocatorDllRealm(&container.
allocator);
146 if (!loadedDll.constructRuntimeContainer(container, containerInformation.
containerName.c_str()))
148 loadedDll.resetAllocatorDllRealm();
150 logManager.
log((std::string(
"Couldn't construct container: #") + std::to_string(
id)).c_str(),
samurai::logError);
152 freeContainerStuff(container);
162 loadedDll.resetAllocatorDllRealm();
165#pragma region setup requested container info
179 logManager.
log(std::string(std::string(
"Couldn't pass cmd argument because it is too big ")
184 cmdArgs = cmd.c_str();
187 loadedDll.bindAllocatorDllRealm(&container.
allocator);
189 loadedDll.resetAllocatorDllRealm();
191 runningContainers[id] = container;
195 logManager.
log((std::string(
"Couldn't create container because it returned 0")
197 destroyContainer(
id, loadedDll, logManager);
216 SAMURAI_DEVELOPMENT_ONLY_ASSERT(loadedDll.dllHand != 0,
"dll not loaded when trying to update containers");
218#pragma region reload dll
223 if (loadedDll.shouldReloadDll())
225 reloadDll(loadedDll, window, logs);
236#pragma region running containers
237 for (
auto &c : runningContainers)
242 SAMURAI_DEVELOPMENT_ONLY_ASSERT(
243 (c.second.requestedContainerInfo.requestedFBO.fbo == 0 &&
244 c.second.imguiWindowId == 0) ||
246 c.second.requestedContainerInfo.requestedFBO.fbo != 0 &&
247 c.second.imguiWindowId != 0),
"we have a fbo but no imguiwindow id"
250 auto windowInput = window.
input;
256 auto t1 = std::chrono::high_resolution_clock::now();
258 loadedDll.bindAllocatorDllRealm(&c.second.allocator);
259 bool rez = c.second.pointer->update(windowInput, windowState, c.second.requestedContainerInfo);
260 loadedDll.resetAllocatorDllRealm();
262 auto t2 = std::chrono::high_resolution_clock::now();
264 auto milliseconds = (std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1)).count()/1000.f;
266 c.second.frameTimer += milliseconds;
267 c.second.frameCounter++;
268 if (c.second.frameCounter >= 100)
270 c.second.currentMs = c.second.frameTimer/100.f;
272 c.second.frameTimer = 0;
273 c.second.frameCounter = 0;
282 if (c.second.imguiWindowId)
285 #pragma region imguiwindow
286 ImGui::PushID(c.second.imguiWindowId);
289 ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.f, 0.f, 0.f, 1.0f));
290 ImGui::SetNextWindowSize({200,200}, ImGuiCond_Once);
291 ImGui::Begin( (std::string(
"gameplay window id: ") + std::to_string(c.first)).c_str(),
292 &isOpen, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
297 auto windowPos = ImGui::GetWindowPos();
299 ImVec2 globalMousePos = {};
301 ImGuiContext *g = ImGui::GetCurrentContext();
302 globalMousePos = g->IO.MousePos;
305 windowInput.mouseX = globalMousePos.x;
306 windowInput.mouseY = globalMousePos.y;
308 ImVec2 vMin = ImGui::GetWindowContentRegionMin();
309 windowInput.mouseX -= windowPos.x + vMin.x;
310 windowInput.mouseY -= windowPos.y + vMin.y;
314 ImGuiViewport *viewPort = ImGui::GetWindowViewport();
315 auto io = ImGui::GetIO();
317 if (viewPort->PlatformUserData)
319 windowInput.hasFocus = ImGui::IsWindowFocused()
320 && ImGui::GetPlatformIO().Platform_GetWindowFocus(viewPort) && !io.AppFocusLost;
327 auto s = ImGui::GetContentRegionMax();
329 ImGui::Image((
void *)c.second.requestedContainerInfo.requestedFBO.texture, s, {0, 1}, {1, 0},
330 {1,1,1,1}, {0,0,0,1});
332 ImGui::Image((
void*)c.second.requestedContainerInfo.requestedFBO.texture, s, { 0, 1 }, { 1, 0 },
333 { 1,1,1,1 }, { 0,0,0,1 });
337 ImGui::PopStyleColor();
348 c.second.requestedContainerInfo.requestedFBO.resizeFramebuffer(windowState.w, windowState.h);
350 glBindFramebuffer(GL_FRAMEBUFFER, c.second.requestedContainerInfo.requestedFBO.fbo);
352 rez = callUpdate(windowState);
354 glBindFramebuffer(GL_FRAMEBUFFER, 0);
356 if (c.second.requestedContainerInfo.requestedFBO.width != windowState.w ||
357 c.second.requestedContainerInfo.requestedFBO.height != windowState.h)
359 c.second.requestedContainerInfo.requestedFBO.width = windowState.w;
360 c.second.requestedContainerInfo.requestedFBO.height = windowState.h;
361 c.second.requestedContainerInfo.requestedFBO.texture = SDL_CreateTexture(c.second.requestedContainerInfo.renderer,
362 SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, windowState.w, windowState.h);
364 SDL_SetRenderTarget(c.second.requestedContainerInfo.renderer, c.second.requestedContainerInfo.requestedFBO.texture);
365 rez = callUpdate(windowState);
367 SDL_SetRenderTarget(c.second.requestedContainerInfo.renderer,
nullptr);
373 destroyContainer(c.first, loadedDll, logs);
384 logs.
log((
"Terminated container because it returned 0: " + std::string(c.second.baseContainerName)
385 +
" #" + std::to_string(c.first)).c_str());
386 destroyContainer(c.first, loadedDll, logs);
399 std::this_thread::sleep_for(std::chrono::milliseconds(200));
402 auto oldContainerInfo = loadedDll.containerInfo;
404 if (!loadedDll.tryToloadDllUntillPossible(loadedDll.id, logs, std::chrono::seconds(5)))
411 std::unordered_map<std::string, samurai::ContainerInformation> containerNames;
412 for (
auto &c : loadedDll.containerInfo)
414 containerNames[c.containerName] = c;
417 std::unordered_map<std::string, samurai::ContainerInformation> oldContainerNames;
418 for (
auto &c : oldContainerInfo)
420 oldContainerNames[c.containerName] = c;
427 std::vector<samurai::containerId_t> containersToClean;
428 for (
auto &i : runningContainers)
430 if (containerNames.find(i.second.baseContainerName) ==
431 containerNames.end())
433 std::string l =
"Killed container because it does not exist anymore in dll: " +
434 std::string(i.second.baseContainerName)
435 +
" #" + std::to_string(i.first);
438 containersToClean.push_back(i.first);
442 for (
auto i : containersToClean)
444 forceTerminateContainer(i, loadedDll, logs);
451 std::vector<samurai::containerId_t> containersToClean;
452 for (
auto &i : runningContainers)
455 auto &newContainer = containerNames[i.second.baseContainerName];
456 auto &oldContainer = oldContainerNames[i.second.baseContainerName];
458 if (newContainer != oldContainer)
460 std::string l =
"Killed container because its static container info\nhas changed: "
461 + std::string(i.second.baseContainerName)
462 +
" #" + std::to_string(i.first);
465 containersToClean.push_back(i.first);
470 for (
auto i : containersToClean)
472 forceTerminateContainer(i, loadedDll, logs);
479 std::unordered_map<std::string, size_t> vtable;
481 for (
auto& i : runningContainers)
483 auto pos = vtable.find(i.second.baseContainerName);
484 if (pos == vtable.end())
491 for (
auto& l : loadedDll.containerInfo)
493 if (l.containerName == i.second.baseContainerName)
499 if (!allocateContainerMemory(container, info, 0))
505 loadedDll.bindAllocatorDllRealm(&container.
allocator);
510 loadedDll.resetAllocatorDllRealm();
512 freeContainerStuff(container);
513 loadedDll.resetAllocatorDllRealm();
518 size_t id = *(
size_t*)container.
pointer;
520 freeContainerStuff(container);
521 loadedDll.resetAllocatorDllRealm();
523 vtable[i.second.baseContainerName] = id;
530 pos = vtable.find(i.second.baseContainerName);
531 if (pos != vtable.end())
533 memcpy(i.second.pointer, (
void*)&pos->second,
sizeof(
size_t));
540 loadedDll.gameplayReload_(window.
context);
544 logs.
log(
"Reloaded dll");