mini refactoring and cleanup
This commit is contained in:
parent
9700ada079
commit
07033c9d13
2 changed files with 53 additions and 95 deletions
|
@ -7,7 +7,6 @@
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
#include "dp_common.h"
|
#include "dp_common.h"
|
||||||
#include "log.h"
|
|
||||||
|
|
||||||
void dp_sdl_fatal(const char *const msg)
|
void dp_sdl_fatal(const char *const msg)
|
||||||
{
|
{
|
||||||
|
@ -15,13 +14,17 @@ void dp_sdl_fatal(const char *const msg)
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
static const size_t NumDooms = 3;
|
struct ControllerContext
|
||||||
|
|
||||||
int doom_controller_loop(nng_socket pubSock, nng_socket subSock, SDL_Window *window, SDL_Renderer *renderer)
|
|
||||||
{
|
{
|
||||||
bool show_demo_window = true;
|
nng_socket pub;
|
||||||
bool show_another_window = false;
|
nng_socket sub;
|
||||||
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
SDL_Window *window;
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
};
|
||||||
|
|
||||||
|
int doom_controller_loop(ControllerContext *ctx)
|
||||||
|
{
|
||||||
|
static constexpr ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
||||||
|
|
||||||
bool done = false;
|
bool done = false;
|
||||||
|
|
||||||
|
@ -34,8 +37,13 @@ int doom_controller_loop(nng_socket pubSock, nng_socket subSock, SDL_Window *win
|
||||||
|
|
||||||
if (event.type == SDL_QUIT)
|
if (event.type == SDL_QUIT)
|
||||||
done = true;
|
done = true;
|
||||||
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
|
|
||||||
|
if (event.type == SDL_WINDOWEVENT
|
||||||
|
&& event.window.event == SDL_WINDOWEVENT_CLOSE
|
||||||
|
&& event.window.windowID == SDL_GetWindowID(ctx->window))
|
||||||
|
{
|
||||||
done = true;
|
done = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the Dear ImGui frame
|
// Start the Dear ImGui frame
|
||||||
|
@ -43,15 +51,13 @@ int doom_controller_loop(nng_socket pubSock, nng_socket subSock, SDL_Window *win
|
||||||
ImGui_ImplSDL2_NewFrame();
|
ImGui_ImplSDL2_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
if (show_demo_window)
|
|
||||||
ImGui::ShowDemoWindow(&show_demo_window);
|
|
||||||
|
|
||||||
// Rendering
|
// Rendering
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
SDL_SetRenderDrawColor(renderer, (Uint8)(clear_color.x * 255), (Uint8)(clear_color.y * 255), (Uint8)(clear_color.z * 255), (Uint8)(clear_color.w * 255));
|
SDL_SetRenderDrawColor(ctx->renderer,
|
||||||
SDL_RenderClear(renderer);
|
(u8)(clear_color.x * 255), (u8)(clear_color.y * 255), (u8)(clear_color.z * 255), (u8)(clear_color.w * 255));
|
||||||
|
SDL_RenderClear(ctx->renderer);
|
||||||
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplSDLRenderer_RenderDrawData(ImGui::GetDrawData());
|
||||||
SDL_RenderPresent(renderer);
|
SDL_RenderPresent(ctx->renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -64,11 +70,6 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
log_info("doompanning ctrl starting");
|
log_info("doompanning ctrl starting");
|
||||||
|
|
||||||
dp_nng_init_limits(1, 1, 1);
|
|
||||||
|
|
||||||
auto pubSock = make_ctrl_pub(CtrlUrl);
|
|
||||||
auto subSock = make_ctrl_sub(DoomUrl);
|
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER))
|
||||||
dp_sdl_fatal("SDL_Init");
|
dp_sdl_fatal("SDL_Init");
|
||||||
|
|
||||||
|
@ -92,65 +93,23 @@ int main(int argc, char *argv[])
|
||||||
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
|
||||||
ImGui_ImplSDLRenderer_Init(renderer);
|
ImGui_ImplSDLRenderer_Init(renderer);
|
||||||
|
|
||||||
int ret = doom_controller_loop(pubSock, subSock, window, renderer);
|
dp_nng_init_limits(1, 1, 1);
|
||||||
|
|
||||||
|
auto pubSock = make_ctrl_pub(CtrlUrl);
|
||||||
|
auto subSock = make_ctrl_sub(DoomUrl);
|
||||||
|
|
||||||
|
ControllerContext ctx
|
||||||
|
{
|
||||||
|
pubSock,
|
||||||
|
subSock,
|
||||||
|
window,
|
||||||
|
renderer
|
||||||
|
};
|
||||||
|
|
||||||
|
int ret = doom_controller_loop(&ctx);
|
||||||
|
|
||||||
nng_close(pubSock);
|
nng_close(pubSock);
|
||||||
nng_close(subSock);
|
nng_close(subSock);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// doomctrl test code
|
|
||||||
#if 0
|
|
||||||
size_t readyCount = 0;
|
|
||||||
|
|
||||||
log_debug("ctrl waiting for hello from dooms");
|
|
||||||
while (readyCount < numDooms)
|
|
||||||
{
|
|
||||||
char *buf = nullptr;
|
|
||||||
size_t sz = 0;
|
|
||||||
|
|
||||||
if (auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC))
|
|
||||||
dp_nng_fatal("run_ctrl/sub/go", res);
|
|
||||||
|
|
||||||
log_debug("ctrl received %zu bytes from a doom", sz);
|
|
||||||
nng_free(buf, sz);
|
|
||||||
++readyCount;
|
|
||||||
}
|
|
||||||
|
|
||||||
log_debug("ctrl is unleashing dooms");
|
|
||||||
|
|
||||||
if (auto res = nng_send(pubSock, (void *)&readyCount, sizeof(readyCount), 0))
|
|
||||||
dp_nng_fatal("run_ctrl/pub/unleash", res);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// doomsim test code
|
|
||||||
#if 0
|
|
||||||
while (true) // FIXME: ctrl code does not work this way
|
|
||||||
{
|
|
||||||
log_debug("doom%zu sending hello", id);
|
|
||||||
if (auto res = nng_send(pubSock, (void *)&id, sizeof(id), 0))
|
|
||||||
dp_nng_fatal("run_doom/pub/hello", res);
|
|
||||||
|
|
||||||
{
|
|
||||||
char *buf = nullptr;
|
|
||||||
size_t sz = 0;
|
|
||||||
|
|
||||||
log_debug("doom%zu waiting for unleash", id);
|
|
||||||
auto res = nng_recv(subSock, &buf, &sz, NNG_FLAG_ALLOC | NNG_FLAG_NONBLOCK);
|
|
||||||
|
|
||||||
if (res == NNG_EAGAIN)
|
|
||||||
{
|
|
||||||
usleep(10 * 1000);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (res)
|
|
||||||
dp_nng_fatal("run_doom/sub/go", res);
|
|
||||||
|
|
||||||
log_debug("doom%zu unleashed by %zu bytes!", id, sz);
|
|
||||||
nng_free(buf, sz);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
|
@ -6,7 +6,6 @@
|
||||||
#include <nng/protocol/pubsub0/pub.h>
|
#include <nng/protocol/pubsub0/pub.h>
|
||||||
#include <nng/protocol/pubsub0/sub.h>
|
#include <nng/protocol/pubsub0/sub.h>
|
||||||
#include "dp_common.h"
|
#include "dp_common.h"
|
||||||
#include "log.h"
|
|
||||||
|
|
||||||
enum DP_DoomState
|
enum DP_DoomState
|
||||||
{
|
{
|
||||||
|
@ -25,7 +24,7 @@ static const char *const DP_DoomState_Strings[DP_DS_COUNT] =
|
||||||
|
|
||||||
#define doomstate_str(s) DP_DoomState_Strings[s]
|
#define doomstate_str(s) DP_DoomState_Strings[s]
|
||||||
|
|
||||||
struct DoomContext;
|
typedef struct DoomContext DoomContext;
|
||||||
|
|
||||||
#define DEF_DOOM_STATE_FUNC(fn) int fn(DoomContext *ctx)
|
#define DEF_DOOM_STATE_FUNC(fn) int fn(DoomContext *ctx)
|
||||||
|
|
||||||
|
@ -113,29 +112,20 @@ DEF_DOOM_STATE_FUNC(do_doom_running)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int doom_loop(nng_socket pubSock, nng_socket subSock, doomid_t doomId)
|
int doom_loop(DoomContext *ctx)
|
||||||
{
|
{
|
||||||
log_debug("doom#%zu started", doomId);
|
log_debug("doom#%zu started", ctx->id);
|
||||||
|
|
||||||
DoomContext ctx
|
|
||||||
{
|
|
||||||
pubSock,
|
|
||||||
subSock,
|
|
||||||
doomId,
|
|
||||||
DP_DS_Ready,
|
|
||||||
do_doom_ready,
|
|
||||||
};
|
|
||||||
|
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
while (ctx.state != DP_DS_Quit && res == 0)
|
while (ctx->state != DP_DS_Quit && res == 0)
|
||||||
{
|
{
|
||||||
DP_DoomState prevState = ctx.state;
|
DP_DoomState prevState = ctx->state;
|
||||||
res = ctx.f(&ctx);
|
res = ctx->f(ctx);
|
||||||
if (prevState != ctx.state)
|
if (prevState != ctx->state)
|
||||||
{
|
{
|
||||||
log_info("transition %s -> %s",
|
log_info("transition %s -> %s",
|
||||||
doomstate_str(prevState), doomstate_str(ctx.state));
|
doomstate_str(prevState), doomstate_str(ctx->state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -167,7 +157,16 @@ int main(int argc, char *argv[])
|
||||||
auto pubSock = make_doom_pub(DoomUrl);
|
auto pubSock = make_doom_pub(DoomUrl);
|
||||||
auto subSock = make_doom_sub(CtrlUrl);
|
auto subSock = make_doom_sub(CtrlUrl);
|
||||||
|
|
||||||
int ret = doom_loop(pubSock, subSock, *doomId);
|
DoomContext ctx
|
||||||
|
{
|
||||||
|
pubSock,
|
||||||
|
subSock,
|
||||||
|
*doomId,
|
||||||
|
DP_DS_Ready,
|
||||||
|
do_doom_ready,
|
||||||
|
};
|
||||||
|
|
||||||
|
int ret = doom_loop(&ctx);
|
||||||
|
|
||||||
nng_close(pubSock);
|
nng_close(pubSock);
|
||||||
nng_close(subSock);
|
nng_close(subSock);
|
||||||
|
|
Loading…
Reference in a new issue