2023-02-03 23:33:37 +01:00
|
|
|
#include <nng/nng.h>
|
2023-02-20 22:50:17 +01:00
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
#include <imgui.h>
|
|
|
|
#include <backends/imgui_impl_sdl.h>
|
|
|
|
#include <backends/imgui_impl_sdlrenderer.h>
|
2023-02-22 20:29:22 +01:00
|
|
|
#include <SDL.h>
|
2023-02-12 03:32:02 +01:00
|
|
|
|
2023-02-14 23:41:47 +01:00
|
|
|
#include <algorithm>
|
2023-02-19 01:24:52 +01:00
|
|
|
#include <array>
|
2023-02-14 23:41:47 +01:00
|
|
|
#include <cerrno>
|
2023-02-12 15:48:15 +01:00
|
|
|
#include <cstdlib>
|
2023-02-19 23:52:54 +01:00
|
|
|
#include <cstring>
|
2023-02-18 22:08:12 +01:00
|
|
|
#include <thread>
|
2023-02-12 15:48:15 +01:00
|
|
|
#include <type_traits>
|
|
|
|
#include <vector>
|
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
#include <signal.h>
|
2023-02-14 23:41:47 +01:00
|
|
|
#include <spawn.h>
|
|
|
|
#include <sys/wait.h>
|
2023-02-19 16:23:29 +01:00
|
|
|
#include <unistd.h>
|
2023-02-14 23:41:47 +01:00
|
|
|
|
2023-02-08 06:39:23 +01:00
|
|
|
#include "dp_common.h"
|
2023-02-12 15:48:15 +01:00
|
|
|
#include "dp_util.hpp"
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
#if DoomBytesPerPixel == 3
|
|
|
|
static const u32 DoomSdlTexturePixelFormat = SDL_PIXELFORMAT_RGB888;
|
|
|
|
#elif DoomBytesPerPixel == 4
|
|
|
|
static const u32 DoomSdlTexturePixelFormat = SDL_PIXELFORMAT_ARGB8888;
|
|
|
|
#else
|
|
|
|
#error Unhandled DoomBytesPerPixel value. Which SDL_PIXELFORMAT to use?
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
void dp_sdl_fatal(const char *const msg)
|
|
|
|
{
|
2023-02-12 04:47:48 +01:00
|
|
|
log_fatal("%s: %s", msg, SDL_GetError());
|
2023-02-12 03:32:02 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
void dp_sdl_error(const char *const msg)
|
|
|
|
{
|
|
|
|
log_error("%s: %s", msg, SDL_GetError());
|
|
|
|
}
|
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
struct DoomState
|
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
// So easy to get this wrong and destroy the texture by accident. Cleanup is
|
|
|
|
// now done in check_on_dooms() before erasing the state. Enabling this code
|
|
|
|
// still works though.
|
2023-02-20 22:50:17 +01:00
|
|
|
#if 0
|
2023-02-19 23:52:54 +01:00
|
|
|
DoomState() = default;
|
|
|
|
~DoomState()
|
|
|
|
{
|
|
|
|
if (texture)
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
}
|
|
|
|
DoomState(const DoomState &) = delete;
|
|
|
|
DoomState &operator=(const DoomState &) = delete;
|
|
|
|
|
|
|
|
DoomState(DoomState &&o)
|
|
|
|
: DoomState()
|
|
|
|
{
|
|
|
|
*this = std::move(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
DoomState &operator=(DoomState &&o)
|
|
|
|
{
|
|
|
|
std::swap(id, o.id);
|
|
|
|
std::swap(state, o.state);
|
|
|
|
std::swap(tLastActive, o.tLastActive);
|
|
|
|
std::swap(texture, o.texture);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-02-19 01:24:52 +01:00
|
|
|
doomid_t id = 0;
|
2023-02-14 23:41:47 +01:00
|
|
|
DP_DoomState state = DP_DS_Unknown;
|
2023-02-19 23:52:54 +01:00
|
|
|
std::chrono::steady_clock::time_point tLastActive;
|
2023-02-19 01:24:52 +01:00
|
|
|
SDL_Texture *texture = nullptr;
|
2023-02-12 15:48:15 +01:00
|
|
|
};
|
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
struct ControllerContext
|
|
|
|
{
|
|
|
|
nng_socket pub;
|
|
|
|
nng_socket sub;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
2023-02-12 15:48:15 +01:00
|
|
|
std::vector<DoomState> dooms;
|
|
|
|
bool quit = false;
|
2023-02-12 22:21:06 +01:00
|
|
|
ExampleAppLog appLog;
|
2023-02-19 10:44:39 +01:00
|
|
|
int columns = 4;
|
2023-02-19 01:24:52 +01:00
|
|
|
std::array<u8, DoomScreenWidth * DoomScreenHeight * DoomBytesPerPixel> pixelBuffer;
|
2023-02-19 23:52:54 +01:00
|
|
|
float scaleFactor = 1.0;
|
|
|
|
s32 offsetX = 0;
|
|
|
|
s32 offsetY = 0;
|
|
|
|
bool isMousePanning = false;
|
2023-02-20 22:50:17 +01:00
|
|
|
bool isFullscreen = false;
|
|
|
|
bool uiVisible = true;
|
2023-02-12 13:40:41 +01:00
|
|
|
};
|
2023-02-03 23:33:37 +01:00
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
struct ControllerActions
|
|
|
|
{
|
|
|
|
int doomsToSpawn = 0;
|
2023-02-18 22:08:12 +01:00
|
|
|
bool endAllDooms = false;
|
2023-02-12 22:21:06 +01:00
|
|
|
};
|
|
|
|
|
2023-02-19 10:44:39 +01:00
|
|
|
#define DOOM_EXECUTABLE "dp_doom"
|
|
|
|
|
2023-02-19 16:23:29 +01:00
|
|
|
void spawn_doom_posix_spawn(ControllerContext &ctx)
|
2023-02-12 22:21:06 +01:00
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
(void) ctx;
|
2023-02-19 10:44:39 +01:00
|
|
|
const char *const argv[] = { DOOM_EXECUTABLE, nullptr };
|
2023-02-14 23:41:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
// TODO: Close stdin and stdout? Leave them open for now to see the logging
|
2023-02-14 23:41:47 +01:00
|
|
|
// output.
|
2023-02-19 23:52:54 +01:00
|
|
|
pid_t pid;
|
|
|
|
if (auto err = posix_spawn(&pid, DOOM_EXECUTABLE, nullptr, nullptr,
|
2023-02-21 23:03:39 +01:00
|
|
|
const_cast<char *const *>(argv), __environ))
|
2023-02-14 23:41:47 +01:00
|
|
|
{
|
2023-02-21 23:03:39 +01:00
|
|
|
log_error("Could not spawn %s: %s", DOOM_EXECUTABLE, strerror(err));
|
2023-02-14 23:41:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-02-12 22:21:06 +01:00
|
|
|
|
2023-02-21 23:03:39 +01:00
|
|
|
log_info("Spawned new %s, pid=%d", DOOM_EXECUTABLE, pid);
|
2023-02-12 22:21:06 +01:00
|
|
|
}
|
|
|
|
|
2023-02-19 16:23:29 +01:00
|
|
|
inline void spawn_doom(ControllerContext &ctx)
|
|
|
|
{
|
|
|
|
spawn_doom_posix_spawn(ctx);
|
|
|
|
}
|
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
void end_all_dooms(ControllerContext &ctx)
|
2023-02-18 11:26:47 +01:00
|
|
|
{
|
2023-02-18 22:08:12 +01:00
|
|
|
nng_msg *msg = nullptr;
|
|
|
|
int res = 0;
|
2023-02-18 11:26:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
|
|
|
|
dp_nng_fatal("ctrl/nng_msg_alloc", res);
|
2023-02-18 11:26:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
|
|
|
|
dpmsg->head.msgType = DP_MT_McstCommand;
|
2023-02-18 23:04:34 +01:00
|
|
|
dpmsg->cmd = DP_DC_Endoom;
|
2023-02-18 11:26:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
if ((res = nng_sendmsg(ctx.pub, msg, 0)))
|
|
|
|
dp_nng_fatal("ctrl/sendmsg", res);
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
void perform_actions(ControllerContext &ctx, const ControllerActions &actions)
|
|
|
|
{
|
|
|
|
if (actions.doomsToSpawn)
|
|
|
|
{
|
|
|
|
log_info("Spawning %d new dooms", actions.doomsToSpawn);
|
|
|
|
for (int i=0; i<actions.doomsToSpawn; ++i)
|
|
|
|
spawn_doom(ctx);
|
|
|
|
}
|
2023-02-18 11:26:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
if (actions.endAllDooms)
|
2023-02-18 11:26:47 +01:00
|
|
|
{
|
|
|
|
log_info("Telling all dooms to quit");
|
2023-02-18 22:08:12 +01:00
|
|
|
end_all_dooms(ctx);
|
2023-02-18 11:26:47 +01:00
|
|
|
}
|
2023-02-12 22:21:06 +01:00
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
inline auto dp_now() { return std::chrono::steady_clock::now(); }
|
|
|
|
inline auto dp_elapsed(const std::chrono::steady_clock::time_point &tStart) { return tStart - dp_now(); }
|
|
|
|
|
2023-02-14 23:41:47 +01:00
|
|
|
void check_on_dooms(ControllerContext &ctx)
|
|
|
|
{
|
|
|
|
pid_t pid = 0;
|
|
|
|
|
2023-02-21 23:05:53 +01:00
|
|
|
// Detect dooms that have terminated. This works for dooms forked from the
|
|
|
|
// controller, not for externally started dooms.
|
2023-02-14 23:41:47 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
int wstatus = 0;
|
|
|
|
|
|
|
|
if (pid = waitpid(0, &wstatus, WNOHANG); pid > 0)
|
|
|
|
{
|
2023-02-21 23:05:53 +01:00
|
|
|
if (auto ds = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });
|
|
|
|
ds != std::end(ctx.dooms))
|
2023-02-14 23:41:47 +01:00
|
|
|
{
|
|
|
|
if (WIFEXITED(wstatus))
|
2023-02-18 22:28:15 +01:00
|
|
|
log_info("doom(%d) exited with status %d", pid, WEXITSTATUS(wstatus));
|
2023-02-14 23:41:47 +01:00
|
|
|
else if (WIFSIGNALED(wstatus))
|
2023-02-18 22:28:15 +01:00
|
|
|
log_warn("doom#(%d) got killed by signal %d", pid, WTERMSIG(wstatus));
|
2023-02-14 23:41:47 +01:00
|
|
|
|
2023-02-21 23:05:53 +01:00
|
|
|
// Manually set Endoom state and let the code below clean it up.
|
|
|
|
ds->state = DP_DS_Endoom;
|
2023-02-14 23:41:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (pid > 0);
|
2023-02-21 23:05:53 +01:00
|
|
|
|
|
|
|
// Find dooms that are in Endoom state and remove them. This works for
|
|
|
|
// externally started dooms if we received their Endoom DoomState update.
|
2023-02-19 23:52:54 +01:00
|
|
|
{
|
2023-02-21 23:05:53 +01:00
|
|
|
const auto prevCount = ctx.dooms.size();
|
|
|
|
const auto removedBegin = std::remove_if(std::begin(ctx.dooms), std::end(ctx.dooms), [](const auto &ds)
|
|
|
|
{ return ds.state == DP_DS_Endoom; });
|
|
|
|
if (removedBegin != std::end(ctx.dooms))
|
|
|
|
{
|
|
|
|
auto count = std::distance(removedBegin, std::end(ctx.dooms));
|
|
|
|
std::for_each(removedBegin, std::end(ctx.dooms), [](auto &ds)
|
|
|
|
{ SDL_DestroyTexture(ds.texture); ds.texture = nullptr; });
|
|
|
|
ctx.dooms.erase(removedBegin, std::end(ctx.dooms));
|
|
|
|
const auto newCount = ctx.dooms.size();
|
|
|
|
log_info("Erased %zu dooms which were in Endoom state. Doomcount before=%zu, after=%zu", count, prevCount, newCount);
|
|
|
|
}
|
2023-02-19 23:52:54 +01:00
|
|
|
}
|
2023-02-21 23:05:53 +01:00
|
|
|
|
|
|
|
// FIXME: We can miss Endoom state updates when nng has to drop message due
|
|
|
|
// to queue size limits. If this happens for an externally started doom it
|
|
|
|
// will never be removed from ctx.dooms (waitpid() does not work because the
|
|
|
|
// doom is not our child). Use DoomState::tLastActive and a fixed timeout
|
|
|
|
// value to timeout dooms and erase them from ctx.dooms.
|
2023-02-14 23:41:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void do_networking(ControllerContext &ctx)
|
|
|
|
{
|
2023-02-19 12:46:17 +01:00
|
|
|
// Set to true if we receive at least on DP_DS_Ready DoomState update. Then
|
|
|
|
// a single DP_DC_RunDoom command is broadcast.
|
2023-02-19 10:44:39 +01:00
|
|
|
bool sendRunDoom = false;
|
2023-02-19 12:46:17 +01:00
|
|
|
|
|
|
|
// Limit the max time we spend doing network stuff.
|
|
|
|
static const auto MaxNetworkingTime = std::chrono::milliseconds(10);
|
2023-02-19 23:52:54 +01:00
|
|
|
auto tStart = dp_now();
|
2023-02-19 10:44:39 +01:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
if (auto elapsed = dp_elapsed(tStart); elapsed >= MaxNetworkingTime)
|
2023-02-19 10:44:39 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
nng_msg *msg = nullptr;
|
|
|
|
|
|
|
|
if (auto res = dp_recv_new_msg_nonblock(ctx.sub, &msg))
|
|
|
|
{
|
|
|
|
if (!dp_nng_is_timeout(res))
|
|
|
|
dp_nng_fatal("ctrl/recvmsg", res);
|
|
|
|
break; // timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
auto msgBase = DP_NNG_BODY_AS(msg, MessageBase);
|
|
|
|
|
|
|
|
if (msgBase->msgType == DP_MT_DoomState)
|
|
|
|
{
|
|
|
|
auto msgDoomState = DP_NNG_BODY_AS(msg, MsgDoomState);
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
// Check if we know this doom and register it if we don't.
|
2023-02-19 12:46:17 +01:00
|
|
|
auto pid = msgDoomState->doomId;
|
|
|
|
auto dit = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });
|
|
|
|
|
|
|
|
if (dit != std::end(ctx.dooms))
|
|
|
|
{
|
|
|
|
dit->state = msgDoomState->doomState;
|
2023-02-19 23:52:54 +01:00
|
|
|
dit->tLastActive = dp_now();
|
2023-02-19 12:46:17 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DoomState ds;
|
|
|
|
ds.id = pid;
|
|
|
|
ds.state = msgDoomState->doomState;
|
2023-02-19 23:52:54 +01:00
|
|
|
ds.tLastActive = dp_now();
|
|
|
|
ds.texture = SDL_CreateTexture(
|
|
|
|
ctx.renderer, DoomSdlTexturePixelFormat,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING, DoomScreenWidth, DoomScreenHeight);
|
2023-02-19 12:46:17 +01:00
|
|
|
|
|
|
|
if (!ds.texture)
|
|
|
|
dp_sdl_fatal("SDL_CreateTexture");
|
|
|
|
|
2023-02-20 00:01:29 +01:00
|
|
|
#if 0
|
2023-02-19 23:52:54 +01:00
|
|
|
{
|
|
|
|
u32 format = 0;
|
|
|
|
int access = 0;
|
|
|
|
int w = 0;
|
|
|
|
int h = 0;
|
|
|
|
if (SDL_QueryTexture(ds.texture, &format, &access, &w, &h))
|
|
|
|
dp_sdl_fatal("SDL_QueryTexture");
|
|
|
|
|
|
|
|
log_debug("texture info (wanted, got): format=(%u, %u), access=(%d, %d), w=(%d, %d), h=(%d, %d)",
|
|
|
|
DoomSdlTexturePixelFormat, format,
|
|
|
|
SDL_TEXTUREACCESS_STREAMING, access,
|
|
|
|
DoomScreenWidth, w,
|
|
|
|
DoomScreenHeight, h
|
|
|
|
);
|
|
|
|
|
|
|
|
assert(format == DoomSdlTexturePixelFormat);
|
|
|
|
assert(access == SDL_TEXTUREACCESS_STREAMING);
|
|
|
|
assert(w == DoomScreenWidth);
|
|
|
|
assert(h == DoomScreenHeight);
|
|
|
|
|
|
|
|
}
|
2023-02-20 00:01:29 +01:00
|
|
|
#endif
|
2023-02-19 23:52:54 +01:00
|
|
|
|
|
|
|
log_info("Registered new doom (pid=%d)", ds.id);
|
|
|
|
ctx.dooms.emplace_back(std::move(ds));
|
2023-02-19 12:46:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-19 10:44:39 +01:00
|
|
|
if (msgDoomState->doomState == DP_DS_Ready)
|
|
|
|
sendRunDoom = true;
|
|
|
|
}
|
|
|
|
else if (msgBase->msgType == DP_MT_DoomFrame)
|
|
|
|
{
|
|
|
|
auto msgDoomFrame = DP_NNG_BODY_AS(msg, MsgDoomFrame);
|
|
|
|
auto pid = msgDoomFrame->doomId;
|
|
|
|
auto dit = find_in_container(ctx.dooms, [pid] (const auto &ds) { return ds.id == pid; });
|
|
|
|
|
|
|
|
if (dit != std::end(ctx.dooms))
|
|
|
|
{
|
|
|
|
auto &ds = *dit;
|
2023-02-19 23:52:54 +01:00
|
|
|
ds.tLastActive = dp_now();
|
|
|
|
|
|
|
|
const u8 *sourcePixels = msgDoomFrame->frame;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// FIXME: buggy. black screen with tiny bar on top
|
|
|
|
//log_trace("Texture update for doom (pid=%d, texture=%p)", ds.id, ds.texture);
|
|
|
|
|
|
|
|
u8 *destPixels = nullptr;
|
|
|
|
int texturePitch = 0;
|
|
|
|
|
|
|
|
if (SDL_LockTexture(ds.texture, nullptr, reinterpret_cast<void **>(&destPixels), &texturePitch))
|
|
|
|
dp_sdl_fatal("SDL_LockTexture");
|
|
|
|
|
|
|
|
// When using 3 bytes per pixel (960 bytes per row), SDL yields
|
|
|
|
// a pitch of 1280 on my machine. This is likely done to get
|
|
|
|
// good alignment.
|
|
|
|
assert(DoomFramePitch <= texturePitch);
|
|
|
|
|
|
|
|
for (size_t row=0; row<DoomScreenHeight; ++row)
|
|
|
|
{
|
|
|
|
u8 *destRow = destPixels + row + texturePitch;
|
|
|
|
const u8 *sourceRow = sourcePixels + row * DoomFramePitch;
|
|
|
|
std::memcpy(destRow, sourceRow, DoomFramePitch);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_UnlockTexture(ds.texture);
|
|
|
|
#else
|
|
|
|
// FIXME: buggy. sometimes crashes with DoomBytesPerPixel=3, hasn't crashed with DoomBytesPerPixel=4 yet.
|
|
|
|
// In the latter case SDL texture pitch equals DoomFramePitch...
|
|
|
|
if (SDL_UpdateTexture(ds.texture, nullptr, sourcePixels, DoomFramePitch))
|
|
|
|
dp_sdl_fatal("SDL_UpdateTexture");
|
|
|
|
#endif
|
2023-02-19 10:44:39 +01:00
|
|
|
}
|
2023-02-19 12:46:17 +01:00
|
|
|
else
|
2023-02-19 23:52:54 +01:00
|
|
|
log_trace("Received DoomFrame from unregistered doom (pid=%d)", pid);
|
2023-02-19 10:44:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nng_msg_free(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sendRunDoom)
|
2023-02-14 23:41:47 +01:00
|
|
|
{
|
|
|
|
nng_msg *msg = nullptr;
|
|
|
|
int res = 0;
|
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
if ((res = nng_msg_alloc(&msg, sizeof(MsgMcstCommand))))
|
|
|
|
dp_nng_fatal("ctrl/nng_msg_alloc", res);
|
2023-02-14 23:41:47 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
auto dpmsg = DP_NNG_BODY_AS(msg, MsgMcstCommand);
|
|
|
|
dpmsg->head.msgType = DP_MT_McstCommand;
|
|
|
|
dpmsg->cmd = DP_DC_RunDoom;
|
2023-02-14 23:41:47 +01:00
|
|
|
|
|
|
|
if ((res = nng_sendmsg(ctx.pub, msg, 0)))
|
|
|
|
dp_nng_fatal("ctrl/sendmsg", res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
void final_cleanup(ControllerContext &ctx)
|
2023-02-14 23:41:47 +01:00
|
|
|
{
|
2023-02-18 22:08:12 +01:00
|
|
|
log_debug("final cleanup: ending all dooms");
|
|
|
|
end_all_dooms(ctx);
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
|
|
check_on_dooms(ctx);
|
|
|
|
|
|
|
|
if (!ctx.dooms.empty())
|
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
log_warn("final cleanup: %zu dooms remain", ctx.dooms.size());
|
2023-02-18 22:08:12 +01:00
|
|
|
}
|
2023-02-14 23:41:47 +01:00
|
|
|
}
|
|
|
|
|
2023-02-19 01:24:52 +01:00
|
|
|
inline s32 RoundFloatToInt(float value)
|
|
|
|
{
|
|
|
|
return static_cast<s32>(value + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct V4: public ImVec4
|
|
|
|
{
|
|
|
|
using ImVec4::ImVec4;
|
|
|
|
float &a = ImVec4::w;
|
|
|
|
float &r = ImVec4::x;
|
|
|
|
float &g = ImVec4::y;
|
|
|
|
float &b = ImVec4::z;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline u32 V4ToARGB(V4 c)
|
|
|
|
{
|
|
|
|
u32 result = ((RoundFloatToInt(c.a * 255.0) & 0xff) << 24
|
|
|
|
| (RoundFloatToInt(c.r * 255.0) & 0xff) << 16
|
|
|
|
| (RoundFloatToInt(c.g * 255.0) & 0xff) << 8
|
|
|
|
| (RoundFloatToInt(c.b * 255.0) & 0xff) << 0);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OffscreenBuffer
|
|
|
|
{
|
|
|
|
s32 width;
|
|
|
|
s32 height;
|
|
|
|
u8 *pixels;
|
|
|
|
s32 pitch;
|
|
|
|
const int BytesPerPixel;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void PutPixelUnchecked(OffscreenBuffer *buffer, s32 x, s32 y, V4 color)
|
|
|
|
{
|
|
|
|
u32 *pixel = reinterpret_cast<u32 *>(buffer->pixels
|
|
|
|
+ y * buffer->pitch
|
|
|
|
+ x * buffer->BytesPerPixel);
|
|
|
|
*pixel = V4ToARGB(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void PutPixel(OffscreenBuffer *buffer, s32 x, s32 y, V4 color)
|
|
|
|
{
|
|
|
|
if (x >= 0 && x < buffer->width && y >= 0 && y < buffer->height)
|
|
|
|
{
|
|
|
|
PutPixelUnchecked(buffer, x, y, color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawRectangle(OffscreenBuffer *buffer,
|
|
|
|
float realMinX, float realMinY,
|
|
|
|
float realMaxX, float realMaxY,
|
|
|
|
V4 color)
|
|
|
|
|
|
|
|
{
|
|
|
|
s32 minX = RoundFloatToInt(realMinX);
|
|
|
|
s32 minY = RoundFloatToInt(realMinY);
|
|
|
|
s32 maxX = RoundFloatToInt(realMaxX);
|
|
|
|
s32 maxY = RoundFloatToInt(realMaxY);
|
|
|
|
|
|
|
|
if (minX < 0)
|
|
|
|
minX = 0;
|
|
|
|
|
|
|
|
if (minY < 0)
|
|
|
|
minY = 0;
|
|
|
|
|
|
|
|
if (maxX > buffer->width)
|
|
|
|
maxX = buffer->width;
|
|
|
|
|
|
|
|
if (maxY > buffer->height)
|
|
|
|
maxY = buffer->height;
|
|
|
|
|
|
|
|
|
|
|
|
u8 *row = buffer->pixels + minY * buffer->pitch;
|
|
|
|
|
|
|
|
for (s32 y = minY; y < maxY; ++y)
|
|
|
|
{
|
|
|
|
u32 *dstPixel = reinterpret_cast<u32 *>(row + minX * buffer->BytesPerPixel);
|
|
|
|
|
|
|
|
for (s32 x = minX; x < maxX; ++x)
|
|
|
|
{
|
|
|
|
float a = color.a;
|
|
|
|
float srcR = color.r * 255.0;
|
|
|
|
float srcG = color.g * 255.0;
|
|
|
|
float srcB = color.b * 255.0;
|
|
|
|
|
|
|
|
float dstR = ((*dstPixel >> 16) & 0xff);
|
|
|
|
float dstG = ((*dstPixel >> 8) & 0xff);
|
|
|
|
float dstB = ((*dstPixel >> 0) & 0xff);
|
|
|
|
|
|
|
|
dstR = (1 - a) * dstR + a * srcR;
|
|
|
|
dstG = (1 - a) * dstG + a * srcG;
|
|
|
|
dstB = (1 - a) * dstB + a * srcB;
|
|
|
|
|
|
|
|
*dstPixel = (RoundFloatToInt(dstR) << 16 |
|
|
|
|
RoundFloatToInt(dstG) << 8 |
|
|
|
|
RoundFloatToInt(dstB) << 0);
|
|
|
|
|
|
|
|
++dstPixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
row += buffer->pitch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void render_dooms(ControllerContext &ctx)
|
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
#if 0
|
2023-02-19 01:24:52 +01:00
|
|
|
OffscreenBuffer buffer =
|
|
|
|
{
|
|
|
|
DoomScreenWidth,
|
|
|
|
DoomScreenHeight,
|
|
|
|
ctx.pixelBuffer.data(),
|
|
|
|
DoomScreenWidth * DoomBytesPerPixel,
|
|
|
|
DoomBytesPerPixel
|
|
|
|
};
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
DrawRectangle(&buffer, 0, 0, 10, 10, { 1, 0, 0, 1 });
|
2023-02-19 01:24:52 +01:00
|
|
|
DrawRectangle(&buffer, 0, buffer.height-10, 10, buffer.height, { 0, 1, 0, 1 });
|
|
|
|
DrawRectangle(&buffer, buffer.width-10, buffer.height-10, buffer.width, buffer.height, { 0, 0, 1, 1 });
|
|
|
|
DrawRectangle(&buffer, buffer.width-10, 0, buffer.width, 10, { 0.840, 0.0168, 0.717, 1 });
|
2023-02-19 23:52:54 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
SDL_Rect destRect = { ctx.offsetX, ctx.offsetY, DoomScreenWidth, DoomScreenHeight };
|
2023-02-19 01:24:52 +01:00
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
destRect.w *= ctx.scaleFactor;
|
|
|
|
destRect.h *= ctx.scaleFactor;
|
2023-02-19 01:24:52 +01:00
|
|
|
|
|
|
|
const size_t doomCount = ctx.dooms.size();
|
|
|
|
|
|
|
|
for (size_t i=0; i<doomCount; ++i)
|
|
|
|
{
|
|
|
|
if (i != 0 && i % ctx.columns == 0)
|
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
destRect.x = ctx.offsetX;
|
|
|
|
destRect.y += destRect.h;
|
2023-02-19 01:24:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto &ds = ctx.dooms.at(i);
|
|
|
|
auto texture = ds.texture;
|
|
|
|
SDL_RenderCopy(ctx.renderer, texture, nullptr, &destRect);
|
2023-02-19 23:52:54 +01:00
|
|
|
destRect.x += destRect.w;
|
2023-02-19 01:24:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
ControllerActions run_ui(ControllerContext &ctx)
|
2023-02-12 15:48:15 +01:00
|
|
|
{
|
|
|
|
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
|
|
|
|
const bool has_debug_tools = true;
|
|
|
|
#else
|
|
|
|
const bool has_debug_tools = false;
|
|
|
|
#endif
|
|
|
|
static bool show_app_metrics = false;
|
|
|
|
static bool show_app_debug_log = false;
|
2023-02-12 22:21:06 +01:00
|
|
|
static bool show_log_window = true;
|
2023-02-12 15:48:15 +01:00
|
|
|
|
|
|
|
if (show_app_metrics)
|
|
|
|
ImGui::ShowMetricsWindow(&show_app_metrics);
|
|
|
|
if (show_app_debug_log)
|
|
|
|
ImGui::ShowDebugLogWindow(&show_app_debug_log);
|
|
|
|
|
2023-02-12 16:12:01 +01:00
|
|
|
const ImGuiViewport* main_viewport = ImGui::GetMainViewport();
|
2023-02-12 22:21:06 +01:00
|
|
|
|
|
|
|
if (show_log_window)
|
|
|
|
{
|
|
|
|
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 666, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
|
|
|
|
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
|
|
|
|
ctx.appLog.Draw("log");
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:12:01 +01:00
|
|
|
ImGui::SetNextWindowPos(ImVec2(main_viewport->WorkPos.x + 20, main_viewport->WorkPos.y + 20), ImGuiCond_FirstUseEver);
|
2023-02-12 22:21:06 +01:00
|
|
|
ImGui::SetNextWindowSize(ImVec2(420, 340), ImGuiCond_FirstUseEver);
|
2023-02-12 16:12:01 +01:00
|
|
|
ImGuiWindowFlags window_flags = ImGuiWindowFlags_MenuBar;
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
static std::array<char, 1024> strbuf;
|
2023-02-12 16:12:01 +01:00
|
|
|
auto &io = ImGui::GetIO();
|
2023-02-12 22:21:06 +01:00
|
|
|
|
|
|
|
aprintf(strbuf, "doompanning - #dooms=%zu, %.2f ms/frame (%.1f fps)###doompanning",
|
|
|
|
ctx.dooms.size(), 1000.0f / io.Framerate, io.Framerate);
|
2023-02-12 16:12:01 +01:00
|
|
|
|
|
|
|
// Main body of the doompanning window starts here.
|
2023-02-12 22:21:06 +01:00
|
|
|
if (!ImGui::Begin(strbuf.data(), nullptr, window_flags))
|
2023-02-12 15:48:15 +01:00
|
|
|
{
|
|
|
|
// Early out if the window is collapsed, as an optimization.
|
|
|
|
ImGui::End();
|
2023-02-12 22:21:06 +01:00
|
|
|
return {};
|
2023-02-12 15:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Menu Bar
|
|
|
|
if (ImGui::BeginMenuBar())
|
|
|
|
{
|
|
|
|
if (ImGui::BeginMenu("Menu"))
|
|
|
|
{
|
2023-02-12 22:21:06 +01:00
|
|
|
ImGui::MenuItem("Log Window", nullptr, &show_log_window);
|
|
|
|
ImGui::MenuItem("Quit", "Ctrl+Q", &ctx.quit, true);
|
2023-02-12 15:48:15 +01:00
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::BeginMenu("Tools"))
|
|
|
|
{
|
|
|
|
ImGui::MenuItem("Dear ImGui Metrics/Debugger", NULL, &show_app_metrics, has_debug_tools);
|
|
|
|
ImGui::MenuItem("Dear ImGui Debug Log", NULL, &show_app_debug_log, has_debug_tools);
|
|
|
|
ImGui::EndMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
ImGui::EndMenuBar();
|
|
|
|
}
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
// Window contents
|
|
|
|
|
|
|
|
ControllerActions result = {};
|
2023-02-20 22:50:17 +01:00
|
|
|
static int doomsToSpawn = 4;
|
2023-02-12 22:21:06 +01:00
|
|
|
|
|
|
|
ImGui::PushItemWidth(ImGui::GetFontSize() * -16); // affects stuff like slider widths
|
|
|
|
|
|
|
|
ImGui::SliderInt("Layout columns##columns", &ctx.columns, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp);
|
2023-02-19 23:52:54 +01:00
|
|
|
ImGui::SliderFloat("Doom scale", &ctx.scaleFactor, 0.1, 10, "%.3f", ImGuiSliderFlags_AlwaysClamp);
|
2023-02-12 22:21:06 +01:00
|
|
|
|
2023-02-18 11:26:47 +01:00
|
|
|
ImGui::SliderInt("##dooms", &doomsToSpawn, 1, 256, "%d", ImGuiSliderFlags_AlwaysClamp | ImGuiSliderFlags_Logarithmic);
|
2023-02-12 22:21:06 +01:00
|
|
|
aprintf(strbuf, "Spawn %d more doom%s###spawnmore", doomsToSpawn, doomsToSpawn > 1 ? "s" : "");
|
|
|
|
if (ImGui::SameLine(); ImGui::Button(strbuf.data()))
|
|
|
|
result.doomsToSpawn = doomsToSpawn;
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
if (ImGui::Button("End all dooms"))
|
2023-02-18 22:08:12 +01:00
|
|
|
result.endAllDooms = true;
|
2023-02-18 11:26:47 +01:00
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
|
2023-02-12 15:48:15 +01:00
|
|
|
ImGui::PopItemWidth();
|
|
|
|
ImGui::End();
|
2023-02-12 22:21:06 +01:00
|
|
|
|
|
|
|
return result;
|
2023-02-12 15:48:15 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
int doom_controller_loop(ControllerContext &ctx)
|
2023-02-03 23:33:37 +01:00
|
|
|
{
|
2023-02-12 13:40:41 +01:00
|
|
|
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
2023-02-18 22:41:05 +01:00
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
ControllerActions actions = {};
|
2023-02-12 04:47:48 +01:00
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
while (!ctx.quit)
|
2023-02-12 03:32:02 +01:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
2023-02-19 23:52:54 +01:00
|
|
|
auto &io = ImGui::GetIO();
|
|
|
|
s32 mouseWheel = 0.0;
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
while (SDL_PollEvent(&event))
|
|
|
|
{
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
if (event.type == SDL_QUIT)
|
2023-02-12 22:21:06 +01:00
|
|
|
ctx.quit = true;
|
2023-02-12 13:40:41 +01:00
|
|
|
|
|
|
|
if (event.type == SDL_WINDOWEVENT
|
|
|
|
&& event.window.event == SDL_WINDOWEVENT_CLOSE
|
2023-02-12 22:21:06 +01:00
|
|
|
&& event.window.windowID == SDL_GetWindowID(ctx.window))
|
2023-02-12 13:40:41 +01:00
|
|
|
{
|
2023-02-12 22:21:06 +01:00
|
|
|
ctx.quit = true;
|
2023-02-12 13:40:41 +01:00
|
|
|
}
|
2023-02-19 23:52:54 +01:00
|
|
|
|
|
|
|
if (event.type == SDL_MOUSEWHEEL)
|
|
|
|
mouseWheel += event.wheel.y;
|
2023-02-12 03:32:02 +01:00
|
|
|
}
|
2023-02-12 04:47:48 +01:00
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
// Process input events not consumed by ImGui
|
2023-02-19 23:52:54 +01:00
|
|
|
if (!io.WantCaptureKeyboard)
|
2023-02-12 22:21:06 +01:00
|
|
|
{
|
2023-02-19 23:52:54 +01:00
|
|
|
// TODO: make scaling scale with the current scale factor
|
|
|
|
static const float ScaleStep = 0.01f;
|
|
|
|
s32 PanStep = io.KeyCtrl ? 10 : 2;
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
if (io.KeyCtrl && ImGui::IsKeyDown(ImGuiKey_Q))
|
|
|
|
ctx.quit = true;
|
2023-02-19 23:52:54 +01:00
|
|
|
|
2023-02-20 22:50:17 +01:00
|
|
|
// keyboard zoom with - and =, reset with 0
|
2023-02-19 23:52:54 +01:00
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_Equal))
|
|
|
|
ctx.scaleFactor += ScaleStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_Minus))
|
|
|
|
ctx.scaleFactor -= ScaleStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_0))
|
|
|
|
ctx.scaleFactor = 1.0;
|
|
|
|
|
2023-02-20 22:50:17 +01:00
|
|
|
// hjkl scrolling and reset with g
|
2023-02-19 23:52:54 +01:00
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_H))
|
|
|
|
ctx.offsetX -= PanStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_J))
|
|
|
|
ctx.offsetY += PanStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_K))
|
|
|
|
ctx.offsetY -= PanStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_L))
|
|
|
|
ctx.offsetX += PanStep;
|
|
|
|
if (ImGui::IsKeyDown(ImGuiKey_G))
|
|
|
|
ctx.offsetX = ctx.offsetY = 0;
|
2023-02-20 22:50:17 +01:00
|
|
|
|
|
|
|
// fullscreen toggle
|
|
|
|
if (io.KeyAlt && ImGui::IsKeyPressed(ImGuiKey_Enter, false))
|
|
|
|
{
|
|
|
|
u32 flag = (ctx.isFullscreen ? 0 : SDL_WINDOW_FULLSCREEN_DESKTOP);
|
|
|
|
SDL_SetWindowFullscreen(ctx.window, flag);
|
|
|
|
ctx.isFullscreen = !ctx.isFullscreen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ImGui::IsKeyPressed(ImGuiKey_F10, false))
|
|
|
|
ctx.uiVisible = !ctx.uiVisible;
|
2023-02-19 23:52:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static ImVec2 panStartPos;
|
|
|
|
|
|
|
|
if (!io.WantCaptureMouse)
|
|
|
|
{
|
|
|
|
ctx.scaleFactor += mouseWheel * 0.05;
|
|
|
|
|
|
|
|
if (ImGui::IsKeyPressed(ImGuiKey_MouseLeft, false))
|
|
|
|
{
|
|
|
|
panStartPos = io.MousePos;
|
|
|
|
ctx.isMousePanning = true;
|
2023-02-12 22:21:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
if (ImGui::IsKeyReleased(ImGuiKey_MouseLeft))
|
|
|
|
ctx.isMousePanning = false;
|
|
|
|
|
|
|
|
if (ctx.isMousePanning)
|
|
|
|
{
|
|
|
|
auto curPos = io.MousePos;
|
|
|
|
auto dx = curPos.x - panStartPos.x;
|
|
|
|
auto dy = curPos.y - panStartPos.y;
|
2023-02-20 22:50:17 +01:00
|
|
|
ctx.offsetX -= dx;
|
|
|
|
ctx.offsetY -= dy;
|
2023-02-19 23:52:54 +01:00
|
|
|
panStartPos = curPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.scaleFactor = std::clamp(ctx.scaleFactor, 0.1f, 10.0f);
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
perform_actions(ctx, actions);
|
2023-02-14 23:41:47 +01:00
|
|
|
check_on_dooms(ctx);
|
|
|
|
do_networking(ctx);
|
2023-02-12 22:21:06 +01:00
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
// Start the Dear ImGui frame
|
|
|
|
ImGui_ImplSDLRenderer_NewFrame();
|
|
|
|
ImGui_ImplSDL2_NewFrame();
|
|
|
|
ImGui::NewFrame();
|
|
|
|
|
2023-02-20 22:50:17 +01:00
|
|
|
actions = {};
|
|
|
|
if (ctx.uiVisible)
|
|
|
|
actions = run_ui(ctx);
|
2023-02-12 15:48:15 +01:00
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
// Rendering
|
2023-02-18 22:41:05 +01:00
|
|
|
const auto [r, g, b, a] = imvec4_to_rgba(clear_color);
|
2023-02-12 22:21:06 +01:00
|
|
|
SDL_SetRenderDrawColor(ctx.renderer, r, g, b, a);
|
|
|
|
SDL_RenderClear(ctx.renderer);
|
2023-02-18 22:41:05 +01:00
|
|
|
|
2023-02-19 01:24:52 +01:00
|
|
|
render_dooms(ctx);
|
2023-02-18 22:41:05 +01:00
|
|
|
|
|
|
|
ImGui::Render();
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
2023-02-12 22:21:06 +01:00
|
|
|
SDL_RenderPresent(ctx.renderer);
|
2023-02-12 03:32:02 +01:00
|
|
|
}
|
2023-02-11 23:52:20 +01:00
|
|
|
|
2023-02-18 22:08:12 +01:00
|
|
|
final_cleanup(ctx);
|
2023-02-14 23:41:47 +01:00
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-14 21:26:11 +01:00
|
|
|
void log_to_imgui(log_Event *ev)
|
2023-02-12 22:21:06 +01:00
|
|
|
{
|
|
|
|
auto ctx = reinterpret_cast<ControllerContext *>(ev->udata);
|
|
|
|
ctx->appLog.AddLog(ev->fmt, ev->ap);
|
|
|
|
}
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
log_set_level(LOG_TRACE);
|
|
|
|
#else
|
|
|
|
log_set_level(LOG_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2023-02-11 23:52:20 +01:00
|
|
|
log_info("doompanning ctrl starting");
|
|
|
|
|
2023-02-12 03:32:02 +01:00
|
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
|
|
|
dp_sdl_fatal("SDL_Init");
|
|
|
|
|
|
|
|
#ifdef SDL_HINT_IME_SHOW_UI
|
|
|
|
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
|
|
|
|
#endif
|
|
|
|
|
2023-02-12 22:21:06 +01:00
|
|
|
const auto windowFlags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_OPENGL;
|
2023-02-20 22:50:17 +01:00
|
|
|
auto window = SDL_CreateWindow("doompanning", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 960, windowFlags);
|
2023-02-12 03:32:02 +01:00
|
|
|
if (!window)
|
|
|
|
dp_sdl_fatal("SDL_CreateWindow");
|
|
|
|
|
|
|
|
auto renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
|
|
|
|
|
|
|
if (!renderer)
|
|
|
|
dp_sdl_fatal("SDL_CreateRenderer");
|
|
|
|
|
2023-02-12 04:47:48 +01:00
|
|
|
IMGUI_CHECKVERSION();
|
|
|
|
ImGui::CreateContext();
|
2023-02-12 15:48:15 +01:00
|
|
|
ImGui::GetIO().IniFilename = "doompanning_ui.ini";
|
2023-02-12 04:47:48 +01:00
|
|
|
ImGui::StyleColorsDark();
|
|
|
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
|
|
|
ImGui_ImplSDLRenderer_Init(renderer);
|
2023-02-12 03:32:02 +01:00
|
|
|
|
2023-02-19 23:52:54 +01:00
|
|
|
//dp_nng_init_limits(1, 1, 1); // int ncpu_max, int pool_thread_limit_max, int resolv_thread_limit
|
|
|
|
//nng_set_ncpu_max(ncpu_max);
|
|
|
|
//nng_set_pool_thread_limit_max(pool_thread_limit_max);
|
|
|
|
nng_set_resolve_thread_max(1);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-14 23:41:47 +01:00
|
|
|
ControllerContext ctx;
|
2023-02-22 19:17:06 +01:00
|
|
|
ctx.pub = make_ctrl_pub(CtrlUrlIpc);
|
|
|
|
ctx.sub = make_ctrl_sub(DoomUrlIpc);
|
2023-02-14 23:41:47 +01:00
|
|
|
ctx.window = window;
|
|
|
|
ctx.renderer = renderer;
|
2023-02-19 01:24:52 +01:00
|
|
|
ctx.pixelBuffer.fill(0u);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-22 21:15:03 +01:00
|
|
|
log_add_callback(log_to_imgui, &ctx, LOG_DEBUG);
|
2023-02-12 22:21:06 +01:00
|
|
|
|
|
|
|
int ret = doom_controller_loop(ctx);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-14 23:41:47 +01:00
|
|
|
nng_close(ctx.pub);
|
|
|
|
nng_close(ctx.sub);
|
2023-02-04 01:56:30 +01:00
|
|
|
|
2023-02-12 13:40:41 +01:00
|
|
|
return ret;
|
2023-02-18 22:08:12 +01:00
|
|
|
}
|