Skip to content

Commit

Permalink
Move SDL handle out of renderer
Browse files Browse the repository at this point in the history
Only create/destroy it where needed.
  • Loading branch information
vkoskiv committed Nov 4, 2023
1 parent 1819abb commit 99407a3
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
4 changes: 0 additions & 4 deletions src/api/c-ray.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,9 @@ void cr_start_renderer(struct renderer *r) {
logr(warning, "You specified 0 local threads, and no network clients were found. Nothing to do.\n");
return;
}
//FIXME: Move actual window creation to separate call and init window elsewhere
struct camera cam = r->scene->cameras[r->prefs.selected_camera];
r->sdl = win_try_init(&r->prefs.window, cam.width, cam.height);
timer_start(&r->state.timer);
currentImage = renderFrame(r);
printDuration(timer_get_ms(r->state.timer));
win_destroy(r->sdl);
}

void cr_start_render_worker() {
Expand Down
7 changes: 5 additions & 2 deletions src/renderer/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ struct texture *renderFrame(struct renderer *r) {
if (thread_start(&r->state.workers[t].thread))
logr(error, "Failed to start worker %d\n", t);
}

struct sdl_window *sdl = win_try_init(&r->prefs.window, camera.width, camera.height);

//Start main thread loop to handle SDL and statistics computation
//FIXME: Statistics computation is a gigantic mess. It will also break in the case
//where a worker node disconnects during a render, so maybe fix that next.
while (r->state.rendering) {
getKeyboardInput(r);
win_check_keyboard(sdl, r);

if (g_aborted) {
r->state.saveImage = false;
Expand All @@ -140,7 +142,7 @@ struct texture *renderFrame(struct renderer *r) {

//Gather and maintain this average constantly.
if (!r->state.workers[0].paused) {
if (r->sdl) win_update(r->sdl, r, output);
if (sdl) win_update(sdl, r, output);
for (size_t t = 0; t < total_thread_count; ++t) {
avgSampleTime += r->state.workers[t].avgSampleTime;
}
Expand Down Expand Up @@ -188,6 +190,7 @@ struct texture *renderFrame(struct renderer *r) {
for (size_t t = 0; t < total_thread_count; ++t) {
thread_wait(&r->state.workers[t].thread);
}
win_destroy(sdl);
return output;
}

Expand Down
1 change: 0 additions & 1 deletion src/renderer/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ struct renderer {
struct state state; //Internal state
struct prefs prefs; //User prefs
char *sceneCache; //Packed scene data that can be passed to workers
struct sdl_window *sdl; //FIXME: Temporarily here
};

//Initialize a new renderer
Expand Down
6 changes: 3 additions & 3 deletions src/utils/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ void printDuration(uint64_t ms) {
logr(plain, " \n");
}

void getKeyboardInput(struct renderer *r) {
if (!r->sdl) return;
void win_check_keyboard(struct sdl_window *sdl, struct renderer *r) {
if (!sdl) return;
SDL_Event event;
while (r->sdl->sym->SDL_PollEvent(&event)) {
while (sdl->sym->SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
if (event.key.keysym.sym == SDLK_s) {
printf("\n");
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct sdl_prefs;
// Returns NULL if we couldn't load the SDL2 lib and/or needed SDL symbols.
struct sdl_window *win_try_init(struct sdl_prefs *prefs, int width, int height);
void win_update(struct sdl_window *w, struct renderer *r, struct texture *t);
void win_check_keyboard(struct sdl_window *w, struct renderer *r);
void win_destroy(struct sdl_window *);

void printDuration(uint64_t ms);
void getKeyboardInput(struct renderer *r);

0 comments on commit 99407a3

Please sign in to comment.