Skip to content

Commit

Permalink
Multithread the journal... even more?
Browse files Browse the repository at this point in the history
  • Loading branch information
Speak2Erase committed May 26, 2024
1 parent 6907228 commit fabbad7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 35 deletions.
72 changes: 47 additions & 25 deletions journal/journal.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <cstdint>
#include <zmq.hpp>

#define STB_IMAGE_IMPLEMENTATION
Expand Down Expand Up @@ -102,7 +103,6 @@ struct Ctx {
zmq::socket_t sub_socket;

SDL_Mutex *mutex;
SDL_Thread *thread;

SDL_Renderer *renderer;
SDL_Window *window;
Expand Down Expand Up @@ -207,6 +207,46 @@ int server_thread(void *data) {
return 0;
}

int render_thread(void* data) {
Ctx *ctx = (Ctx *)data;

// I would move this into one line but apparently that leaves things uninitialized???
// C++ what the fuck
int old_window_x = INT32_MAX;
int old_window_y = INT32_MAX;
int window_x = 0;
int window_y = 0;

while (ctx->running) {
SDL_LockMutex(ctx->mutex);

SDL_GetWindowPosition(ctx->window, &window_x, &window_y);
if (old_window_x != window_x || old_window_y != window_y) {
old_window_x = window_x;
old_window_y = window_y;

Message message;
message.tag = Message::WindowPosition;
message.val.pos = { window_x, window_y };
zmq::message_t zmq_message(&message, sizeof(Message));
ctx->pub_socket.send(zmq_message, zmq::send_flags::dontwait);
}

SDL_RenderClear(ctx->renderer);
SDL_RenderTexture(ctx->renderer, ctx->texture, NULL, NULL);
SDL_RenderPresent(ctx->renderer);

SDL_ShowWindow(ctx->window);

SDL_UnlockMutex(ctx->mutex);

// calculates to 60 fps
SDL_Delay(1000 / 60);
}

return 0;
}

void journal_handling(const stbi_uc* initial_image_buf, size_t initial_image_buf_len) {
// FIXME error handling
SDL_Init(SDL_INIT_VIDEO);
Expand Down Expand Up @@ -242,39 +282,21 @@ void journal_handling(const stbi_uc* initial_image_buf, size_t initial_image_buf

SDL_SetWindowHitTest(ctx.window, hit_test_fun, ctx.surface);

ctx.mutex = SDL_CreateMutex();
ctx.thread = SDL_CreateThread(server_thread, "server thread", &ctx);

ctx.running = true;

ctx.mutex = SDL_CreateMutex();
SDL_CreateThread(server_thread, "server thread", &ctx);
SDL_CreateThread(render_thread, "render thread", &ctx);

SDL_Event event;
while (ctx.running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
while (SDL_WaitEventTimeout(&event, 16)) {
switch (event.type) {
case SDL_EVENT_QUIT:
ctx.running = false;
break;
case SDL_EVENT_WINDOW_MOVED:
message.tag = Message::WindowPosition;
message.val.pos = { event.window.data1, event.window.data2 };
zmq_message.rebuild(&message, sizeof(Message));
ctx.pub_socket.send(zmq_message, zmq::send_flags::dontwait);
break;
}
}

SDL_LockMutex(ctx.mutex);

SDL_RenderClear(ctx.renderer);
SDL_RenderTexture(ctx.renderer, ctx.texture, NULL, NULL);
SDL_RenderPresent(ctx.renderer);

SDL_ShowWindow(ctx.window);

SDL_UnlockMutex(ctx.mutex);

// calculates to 60 fps
SDL_Delay(1000 / 60);
}

// tell oneshot we have closed
Expand Down
2 changes: 1 addition & 1 deletion journal/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ if host_system == 'windows'
endif

executable(
meson.project_name() + '-journal',
'_______',
sources: sources,
dependencies: [sdl3, cppzmq],
link_args: link_args,
Expand Down
14 changes: 6 additions & 8 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,9 @@ else
endif
endif

exe_name = meson.project_name()

if host_system == 'linux' and get_option('appimage') == false
exe_name += '.' + host_machine.cpu()
endif
exe_name = 'oneshot'

if steamworks == true
exe_name = 'steam_' + exe_name
la = ''
if build_static == true
if host_system == 'windows'
Expand Down Expand Up @@ -204,14 +199,17 @@ executable(exe_name,
# Shim for Windows
if host_system == 'windows'
executable(
meson.project_name() + '-shim',
exe_name + '-shim',
sources: [
'windows/shim.c',
windows_resources
],
link_args: [

],
win_subsystem: 'windows'
win_subsystem: 'windows',
c_args: [
'-DGAME_LAUNCH_NAME=L"' + exe_name + '.exe"'
],
)
endif
2 changes: 1 addition & 1 deletion windows/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
argv[0] = ARGV0;
}

_wexecv(L"lib\\oneshot.exe", (const wchar_t *const *)argv);
_wexecv(GAME_LAUNCH_NAME, (const wchar_t *const *)argv);

MessageBoxW(NULL,
L"Cannot start ModShot for some reason.\nPlease check your "
Expand Down

0 comments on commit fabbad7

Please sign in to comment.