From b5927ba9a22dd256603b81d452fdb3cfaf0dc2ba Mon Sep 17 00:00:00 2001 From: kdx Date: Wed, 29 Nov 2023 11:24:04 +0100 Subject: interlace effect --- headers/TZR.h | 1 + headers/TZR_globals.h | 1 + headers/TZR_types.h | 1 + sources/TZR_DrawBegin.c | 5 +++++ sources/TZR_DrawEnd.c | 18 ++++++++++++++++++ sources/TZR_Init.c | 8 ++++++++ sources/TZR_Quit.c | 4 ++++ sources/globals.c | 1 + 8 files changed, 39 insertions(+) diff --git a/headers/TZR.h b/headers/TZR.h index 7d55471..17129c8 100644 --- a/headers/TZR.h +++ b/headers/TZR.h @@ -17,6 +17,7 @@ .target_fps=60, \ .ratio=1.0f, \ .pixel_perfect=true, \ + .interlace=false, \ .hd_render=false, \ .scale_linear=false, \ .show_cursor=false, \ diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index 208ad0b..6c4bd04 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -12,6 +12,7 @@ extern size_t ___tzr_resources_size; extern SDL_Window *___tzr_window; extern SDL_Renderer *___tzr_renderer; extern SDL_Texture *___tzr_target; +extern SDL_Texture *___tzr_target_pre; extern unsigned long ___tzr_tick; extern unsigned long ___tzr_next_time; extern unsigned long ___tzr_min_dt; diff --git a/headers/TZR_types.h b/headers/TZR_types.h index cd0baad..e7d13fc 100644 --- a/headers/TZR_types.h +++ b/headers/TZR_types.h @@ -59,6 +59,7 @@ struct TZR_Config { int target_fps; float ratio; bool pixel_perfect; + bool interlace; bool hd_render; bool scale_linear; bool show_cursor; diff --git a/sources/TZR_DrawBegin.c b/sources/TZR_DrawBegin.c index 59ac06a..9776f9f 100644 --- a/sources/TZR_DrawBegin.c +++ b/sources/TZR_DrawBegin.c @@ -10,6 +10,11 @@ TZR_DrawBegin(void) return sdl_error(-1); if (___tzr_config.hd_render) return 0; + if (___tzr_config.interlace) { + SDL_Texture *const tmp = ___tzr_target; + ___tzr_target = ___tzr_target_pre; + ___tzr_target_pre = tmp; + } if (SDL_SetRenderTarget(___tzr_renderer, ___tzr_target) < 0) return sdl_error(-1); return 0; diff --git a/sources/TZR_DrawEnd.c b/sources/TZR_DrawEnd.c index a1d09da..070accd 100644 --- a/sources/TZR_DrawEnd.c +++ b/sources/TZR_DrawEnd.c @@ -8,6 +8,24 @@ static int pixel_draw_end(void) { + if (___tzr_config.interlace) { + static int odd = 0; + odd ^= 1; + for (int i = odd; i < ___tzr_config.height; i += 2) { + const SDL_Rect rect = { + 0, + i, + ___tzr_config.width, + 1 + }; + if (SDL_RenderCopy(___tzr_renderer, + ___tzr_target_pre, + &rect, + &rect) < 0) + return sdl_error(-1); + } + } + if (SDL_SetRenderTarget(___tzr_renderer, NULL) < 0) return sdl_error(-1); if (SDL_SetRenderDrawColor(___tzr_renderer, 0, 0, 0, 255) < 0) diff --git a/sources/TZR_Init.c b/sources/TZR_Init.c index 1fd23ee..5af39f4 100644 --- a/sources/TZR_Init.c +++ b/sources/TZR_Init.c @@ -75,6 +75,14 @@ _TZR_Init(const TZR_Config *config) ___tzr_config.height); if (___tzr_target == NULL) return _sdl_error(); + if (___tzr_config.interlace) { + ___tzr_target_pre = SDL_CreateTexture( + ___tzr_renderer, + SDL_PIXELFORMAT_RGB888, + SDL_TEXTUREACCESS_TARGET, + ___tzr_config.width, + ___tzr_config.height); + } } SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, diff --git a/sources/TZR_Quit.c b/sources/TZR_Quit.c index 7d819a3..62be0b0 100644 --- a/sources/TZR_Quit.c +++ b/sources/TZR_Quit.c @@ -23,6 +23,10 @@ TZR_Quit(void) ___tzr_joysticks = NULL; ___tzr_joysticks_size = 0; ___tzr_joysticks_capacity = 0; + if (___tzr_target_pre != NULL) { + SDL_DestroyTexture(___tzr_target_pre); + ___tzr_target = NULL; + } if (___tzr_target != NULL) { SDL_DestroyTexture(___tzr_target); ___tzr_target = NULL; diff --git a/sources/globals.c b/sources/globals.c index a67ebc0..3286499 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -12,6 +12,7 @@ size_t ___tzr_resources_size = 0; SDL_Window *___tzr_window = NULL; SDL_Renderer *___tzr_renderer = NULL; SDL_Texture *___tzr_target = NULL; +SDL_Texture *___tzr_target_pre = NULL; unsigned long ___tzr_tick = 0; unsigned long ___tzr_next_time = 0; unsigned long ___tzr_min_dt = 0; -- cgit v1.2.3