From 4f6198b965357ca63daf24d7767be715987fe58b Mon Sep 17 00:00:00 2001 From: untir_l <87096069+untir-l@users.noreply.github.com> Date: Sat, 11 Jun 2022 21:34:29 +0530 Subject: Web: clean up code, fix memory leak --- web/hitomezashi_web.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'web/hitomezashi_web.c') diff --git a/web/hitomezashi_web.c b/web/hitomezashi_web.c index 413f727..994eaa5 100644 --- a/web/hitomezashi_web.c +++ b/web/hitomezashi_web.c @@ -40,24 +40,25 @@ void EMSCRIPTEN_KEEPALIVE main_loop(void) { const Uint32 fg_colour = hitomezashi_web_get_fg_colour(); const Uint32 bg_colour = hitomezashi_web_get_bg_colour(); - // If any of these are 0, input was invalid - if (!(x_pattern_len && y_pattern_len && gap && line_thickness)) - return; - - struct Hitomezashi_State state; - hitomezashi_state_init(&state, x_pattern_len, y_pattern_len, x_pattern, - y_pattern, gap, line_thickness, fg_colour, bg_colour); - SDL_SetWindowSize(window, state.output_width, state.output_height); - hitomezashi_draw(&state); - - SDL_BlitSurface(state.surface, NULL, SDL_GetWindowSurface(window), NULL); - SDL_UpdateWindowSurface(window); + // If input was valid (if any of these are 0 it was invalid) + if (x_pattern_len && y_pattern_len && gap && line_thickness) { + struct Hitomezashi_State state; + hitomezashi_state_init(&state, x_pattern_len, y_pattern_len, x_pattern, + y_pattern, gap, line_thickness, fg_colour, + bg_colour); + SDL_SetWindowSize(window, state.output_width, state.output_height); + hitomezashi_draw(&state); + + SDL_BlitSurface(state.surface, NULL, SDL_GetWindowSurface(window), NULL); + SDL_UpdateWindowSurface(window); + + SDL_FreeSurface(state.surface); + } free((char *)x_pattern_raw); free((char *)y_pattern_raw); free((char *)x_pattern); free((char *)y_pattern); - SDL_FreeSurface(state.surface); } EM_JS(char *, hitomezashi_web_get_x_pattern, (void), { -- cgit v1.2.3-57-g22cb