From 9f6c367fce02ce8c52695b1f4f48ef9100d2cef5 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 13 Nov 2022 22:23:56 +0900 Subject: [PATCH] [console] Resurrect console scaling Currently only for gl/glsl/vulkan. However, rather than futzing with con_width and con_height (and trying to guess good values), con_scale (currently an integer) gives consistent pixel scaling regardless of window size. --- include/QF/GL/qf_draw.h | 1 + include/QF/GLSL/qf_draw.h | 1 + include/QF/draw.h | 3 ++ include/QF/plugin/console.h | 1 - include/QF/plugin/vid_render.h | 1 + include/vid_vulkan.h | 1 + libs/client/cl_screen.c | 9 ----- libs/console/client.c | 51 ++++++++++++++++++++---- libs/video/renderer/gl/gl_draw.c | 15 +++++-- libs/video/renderer/glsl/glsl_draw.c | 24 +++++++---- libs/video/renderer/r_draw.c | 14 +++++++ libs/video/renderer/vid_render_gl.c | 1 + libs/video/renderer/vid_render_glsl.c | 1 + libs/video/renderer/vid_render_vulkan.c | 12 +++++- libs/video/renderer/vulkan/vulkan_draw.c | 5 ++- libs/video/targets/vid_win_vulkan.c | 1 + libs/video/targets/vid_x11_vulkan.c | 1 + 17 files changed, 108 insertions(+), 34 deletions(-) diff --git a/include/QF/GL/qf_draw.h b/include/QF/GL/qf_draw.h index ff7b9a30a..ea051f356 100644 --- a/include/QF/GL/qf_draw.h +++ b/include/QF/GL/qf_draw.h @@ -35,6 +35,7 @@ struct draw_charbuffer_s; void gl_Draw_Init (void); void gl_Draw_Shutdown (void); void gl_Draw_CharBuffer (int x, int y, struct draw_charbuffer_s *buffer); +void gl_Draw_SetScale (int scale); void gl_Draw_Character (int x, int y, unsigned ch); void gl_Draw_String (int x, int y, const char *str); void gl_Draw_nString (int x, int y, const char *str, int count); diff --git a/include/QF/GLSL/qf_draw.h b/include/QF/GLSL/qf_draw.h index 6481cf7aa..10f18d6b9 100644 --- a/include/QF/GLSL/qf_draw.h +++ b/include/QF/GLSL/qf_draw.h @@ -35,6 +35,7 @@ struct draw_charbuffer_s; void glsl_Draw_Init (void); void glsl_Draw_Shutdown (void); void glsl_Draw_CharBuffer (int x, int y, struct draw_charbuffer_s *buffer); +void glsl_Draw_SetScale (int scale); void glsl_Draw_Character (int x, int y, unsigned ch); void glsl_Draw_String (int x, int y, const char *str); void glsl_Draw_nString (int x, int y, const char *str, int count); diff --git a/include/QF/draw.h b/include/QF/draw.h index 3c439f190..d31f877ab 100644 --- a/include/QF/draw.h +++ b/include/QF/draw.h @@ -61,6 +61,9 @@ void Draw_ScrollBuffer (draw_charbuffer_t *buffer, int lines); void Draw_CharBuffer (int x, int y, draw_charbuffer_t *buffer); int Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str); +void Draw_SetScale (int scale); +int Draw_MaxScale (void) __attribute__((pure)); + extern byte *draw_chars; /** Initialize the draw stuff. diff --git a/include/QF/plugin/console.h b/include/QF/plugin/console.h index 7307bb4f8..5d4393983 100644 --- a/include/QF/plugin/console.h +++ b/include/QF/plugin/console.h @@ -52,7 +52,6 @@ typedef struct console_data_s { void (*quit) (void); struct cbuf_s *cbuf; struct view_s *screen_view; - //struct view_s *view; struct view_s *status_view; float lines; int (*exec_line)(void *data, const char *line); diff --git a/include/QF/plugin/vid_render.h b/include/QF/plugin/vid_render.h index a3df2906a..64427fe57 100644 --- a/include/QF/plugin/vid_render.h +++ b/include/QF/plugin/vid_render.h @@ -88,6 +88,7 @@ typedef void (*capfunc_t) (struct tex_s *screencap, void *data); typedef struct vid_render_funcs_s { void (*init) (void); void (*Draw_CharBuffer) (int x, int y, struct draw_charbuffer_s *buffer); + void (*Draw_SetScale) (int scale); void (*Draw_Character) (int x, int y, unsigned ch); void (*Draw_String) (int x, int y, const char *str); void (*Draw_nString) (int x, int y, const char *str, int count); diff --git a/include/vid_vulkan.h b/include/vid_vulkan.h index 709755538..cd714d093 100644 --- a/include/vid_vulkan.h +++ b/include/vid_vulkan.h @@ -98,6 +98,7 @@ typedef struct vulkan_ctx_s { // size of window int window_width; int window_height; + int twod_scale; //FIXME this is for the parser qfv_output_t output; diff --git a/libs/client/cl_screen.c b/libs/client/cl_screen.c index 3afec5d2f..8f5af2de5 100644 --- a/libs/client/cl_screen.c +++ b/libs/client/cl_screen.c @@ -165,20 +165,11 @@ static SCR_Func *scr_funcs[] = { scr_funcs_intermission, }; -static void -cl_vidsize_listener (void *data, const viddef_t *vdef) -{ - View_SetLen (cl_screen_view, vdef->width, vdef->height); - View_UpdateHierarchy (cl_screen_view); -} - void CL_Init_Screen (void) { qpic_t *pic; - VID_OnVidResize_AddListener (cl_vidsize_listener, 0); - HUD_Init (); cl_screen_view = View_New (hud_registry, nullview); diff --git a/libs/console/client.c b/libs/console/client.c index 13fecf267..5e1433927 100644 --- a/libs/console/client.c +++ b/libs/console/client.c @@ -70,6 +70,16 @@ static con_buffer_t *con; static float con_cursorspeed = 4; +static int con_scale; +static cvar_t con_scale_cvar = { + .name = "con_scale", + .description = + "Pixel scale factor for the console and all 2D user interface " + "elements", + .default_value = "1", + .flags = CVAR_ARCHIVE, + .value = { .type = &cexpr_int, .value = &con_scale }, +}; static float con_notifytime; static cvar_t con_notifytime_cvar = { .name = "con_notifytime", @@ -807,6 +817,24 @@ exec_line (inputline_t *il) Con_ExecLine (il->line); } +static int win_xlen = -1; +static int win_ylen = -1; + +static void +con_set_size (void) +{ + int xlen = win_xlen / con_scale; + int ylen = win_ylen / con_scale; + if (xlen > 0 && ylen > 0) { + View_SetLen (screen_view, xlen, ylen); + View_UpdateHierarchy (screen_view); + if (con_data.screen_view) { + View_SetLen (*con_data.screen_view, xlen, ylen); + View_UpdateHierarchy (*con_data.screen_view); + } + } +} + // The console view is not a child of the client's screen view, so it won't // get resized automatically when the screen changes size. However, since the // console has to process input events anyway, handling ie_app_window is a @@ -814,15 +842,21 @@ exec_line (inputline_t *il) static void con_app_window (const IE_event_t *event) { - static int old_xlen = -1; - static int old_ylen = -1; - if (old_xlen != event->app_window.xlen - || old_ylen != event->app_window.ylen) { - old_xlen = event->app_window.xlen; - old_ylen = event->app_window.ylen; + if (win_xlen != event->app_window.xlen + || win_ylen != event->app_window.ylen) { + win_xlen = event->app_window.xlen; + win_ylen = event->app_window.ylen; + con_set_size (); + } +} - View_SetLen (screen_view, old_xlen, old_ylen); - View_UpdateHierarchy (screen_view); +static void +con_scale_f (void *data, const cvar_t *cvar) +{ + con_scale = bound (1, con_scale, Draw_MaxScale ()); + Draw_SetScale (con_scale); + if (con_initialized) { + con_set_size (); } } @@ -1001,6 +1035,7 @@ C_Init (void) Cvar_Register (&con_notifytime_cvar, 0, 0); + Cvar_Register (&con_scale_cvar, con_scale_f, 0); Cvar_Register (&con_alpha_cvar, 0, 0); Cvar_Register (&con_size_cvar, 0, 0); Cvar_Register (&con_speed_cvar, 0, 0); diff --git a/libs/video/renderer/gl/gl_draw.c b/libs/video/renderer/gl/gl_draw.c index 79115b819..bd7d3b38d 100644 --- a/libs/video/renderer/gl/gl_draw.c +++ b/libs/video/renderer/gl/gl_draw.c @@ -115,6 +115,7 @@ static int numcachepics; static byte menuplyr_pixels[4096]; +static int gl_2d_scale = 1; static void Draw_InitText (void) @@ -836,7 +837,7 @@ gl_Draw_ConsoleBackground (int lines, byte alpha) if (gl_constretch) { ofs = 0; } else - ofs = (vid.height - lines) / (float) vid.height; + ofs = (vid.height - gl_2d_scale * lines) / (float) vid.height; color_0_8[3] = alpha; qfglColor4ubv (color_0_8); @@ -851,9 +852,9 @@ gl_Draw_ConsoleBackground (int lines, byte alpha) qfglTexCoord2f (0, 0 + ofs); qfglVertex2f (0, 0); qfglTexCoord2f (1, 0 + ofs); - qfglVertex2f (vid.width, 0); + qfglVertex2f (vid.width / gl_2d_scale, 0); qfglTexCoord2f (1, 1); - qfglVertex2f (vid.width, lines); + qfglVertex2f (vid.width / gl_2d_scale, lines); qfglTexCoord2f (0, 1); qfglVertex2f (0, lines); qfglEnd (); @@ -995,7 +996,13 @@ GL_Set2D (void) void GL_Set2DScaled (void) { - set_2d (vid.width, vid.height); + set_2d (vid.width / gl_2d_scale, vid.height / gl_2d_scale); +} + +void +gl_Draw_SetScale (int scale) +{ + gl_2d_scale = scale; } void diff --git a/libs/video/renderer/glsl/glsl_draw.c b/libs/video/renderer/glsl/glsl_draw.c index 835b93948..ab4067746 100644 --- a/libs/video/renderer/glsl/glsl_draw.c +++ b/libs/video/renderer/glsl/glsl_draw.c @@ -112,6 +112,7 @@ static qpic_t *white_pic; static qpic_t *backtile_pic; static hashtab_t *pic_cache; static int glsl_conback_texnum; +static int glsl_2d_scale = 1; static cvar_t glsl_conback_texnum_cvar = { .name = "glsl_conback_texnum", .description = @@ -647,15 +648,16 @@ glsl_Draw_SubPic (int x, int y, qpic_t *pic, int srcx, int srcy, int width, void glsl_Draw_ConsoleBackground (int lines, byte alpha) { - float ofs = (vid.height - lines) / (float) vid.height; + int s = glsl_2d_scale; + float ofs = (vid.height - s * lines) / (float) vid.height; quat_t color = {1, 1, 1, bound (0, alpha, 255) / 255.0}; drawvert_t verts[] = { - {{ 0, 0, 0, ofs}}, - {{vid.width, 0, 1, ofs}}, - {{vid.width, lines, 1, 1}}, - {{ 0, 0, 0, ofs}}, - {{vid.width, lines, 1, 1}}, - {{ 0, lines, 0, 1}}, + {{ 0, 0, 0, ofs}}, + {{vid.width / s, 0, 1, ofs}}, + {{vid.width / s, lines, 1, 1}}, + {{ 0, 0, 0, ofs}}, + {{vid.width / s, lines, 1, 1}}, + {{ 0, lines, 0, 1}}, }; GLSL_FlushText (); // Flush text that should be rendered before the console @@ -828,7 +830,13 @@ GLSL_Set2D (void) void GLSL_Set2DScaled (void) { - set_2d (vid.width, vid.height); + set_2d (vid.width / glsl_2d_scale, vid.height / glsl_2d_scale); +} + +void +glsl_Draw_SetScale (int scale) +{ + glsl_2d_scale = scale; } void diff --git a/libs/video/renderer/r_draw.c b/libs/video/renderer/r_draw.c index 5a18b99b0..a86a61aa5 100644 --- a/libs/video/renderer/r_draw.c +++ b/libs/video/renderer/r_draw.c @@ -141,3 +141,17 @@ Draw_PrintBuffer (draw_charbuffer_t *buffer, const char *str) } return lines; } + +void +Draw_SetScale (int scale) +{ + if (r_funcs->Draw_SetScale) { + r_funcs->Draw_SetScale (scale); + } +} + +int +Draw_MaxScale (void) +{ + return r_funcs->Draw_SetScale ? 20 : 1; +} diff --git a/libs/video/renderer/vid_render_gl.c b/libs/video/renderer/vid_render_gl.c index 7ec796c66..6b4d50f12 100644 --- a/libs/video/renderer/vid_render_gl.c +++ b/libs/video/renderer/vid_render_gl.c @@ -502,6 +502,7 @@ vid_render_funcs_t gl_vid_render_funcs = { .init = gl_vid_render_init, .Draw_CharBuffer = gl_Draw_CharBuffer, + .Draw_SetScale = gl_Draw_SetScale, .Draw_Character = gl_Draw_Character, .Draw_String = gl_Draw_String, .Draw_nString = gl_Draw_nString, diff --git a/libs/video/renderer/vid_render_glsl.c b/libs/video/renderer/vid_render_glsl.c index 5c95998bd..87608ab40 100644 --- a/libs/video/renderer/vid_render_glsl.c +++ b/libs/video/renderer/vid_render_glsl.c @@ -446,6 +446,7 @@ vid_render_funcs_t glsl_vid_render_funcs = { .init = glsl_vid_render_init, .Draw_CharBuffer = glsl_Draw_CharBuffer, + .Draw_SetScale = glsl_Draw_SetScale, .Draw_Character = glsl_Draw_Character, .Draw_String = glsl_Draw_String, .Draw_nString = glsl_Draw_nString, diff --git a/libs/video/renderer/vid_render_vulkan.c b/libs/video/renderer/vid_render_vulkan.c index 733ada82d..2683a48e4 100644 --- a/libs/video/renderer/vid_render_vulkan.c +++ b/libs/video/renderer/vid_render_vulkan.c @@ -145,6 +145,12 @@ vulkan_Draw_CharBuffer (int x, int y, draw_charbuffer_t *buffer) Vulkan_Draw_CharBuffer (x, y, buffer, vulkan_ctx); } +static void +vulkan_Draw_SetScale (int scale) +{ + vulkan_ctx->twod_scale = max (1, scale); +} + static void vulkan_Draw_Character (int x, int y, unsigned ch) { @@ -360,11 +366,12 @@ vulkan_set_2d (int scaled) //FIXME this should not be done every frame __auto_type mctx = vulkan_ctx->matrix_context; __auto_type mat = &mctx->matrices; + int scale = vulkan_ctx->twod_scale; float left = -0.5; float top = -0.5; - float right = left + vid.width; //FIXME vid - float bottom = top + vid.height; + float right = left + vid.width / scale; + float bottom = top + vid.height / scale; QFV_Orthographic (mat->Projection2d, left, right, top, bottom, 0, 99999); mctx->dirty = mctx->frames.size; @@ -780,6 +787,7 @@ vid_render_funcs_t vulkan_vid_render_funcs = { .init = vulkan_vid_render_init, .Draw_CharBuffer = vulkan_Draw_CharBuffer, + .Draw_SetScale = vulkan_Draw_SetScale, .Draw_Character = vulkan_Draw_Character, .Draw_String = vulkan_Draw_String, .Draw_nString = vulkan_Draw_nString, diff --git a/libs/video/renderer/vulkan/vulkan_draw.c b/libs/video/renderer/vulkan/vulkan_draw.c index 6b65907f8..7eb2a9156 100644 --- a/libs/video/renderer/vulkan/vulkan_draw.c +++ b/libs/video/renderer/vulkan/vulkan_draw.c @@ -897,10 +897,11 @@ Vulkan_Draw_ConsoleBackground (int lines, byte alpha, vulkan_ctx_t *ctx) quat_t color = { a, a, a, a}; qpic_t *cpic; cpic = Vulkan_Draw_CachePic ("gfx/conback.lmp", false, ctx); - float frac = (vid.height - lines) / (float) vid.height; + int s = ctx->twod_scale; + float frac = (vid.height - s * lines) / (float) vid.height; int ofs = frac * cpic->height; subpic_t *subpic = *(subpic_t **) cpic->data; - draw_pic (0, 0, vid.width, lines, subpic, + draw_pic (0, 0, vid.width / s, lines, subpic, 0, ofs, cpic->width, cpic->height - ofs, color, &frame->quad_verts); } diff --git a/libs/video/targets/vid_win_vulkan.c b/libs/video/targets/vid_win_vulkan.c index 2a8618073..5ae2c4d61 100644 --- a/libs/video/targets/vid_win_vulkan.c +++ b/libs/video/targets/vid_win_vulkan.c @@ -185,6 +185,7 @@ Win_Vulkan_Context (void) ctx->create_surface = win_vulkan_create_surface; ctx->required_extensions = required_extensions; ctx->va_ctx = va_create_context (4); + ctx->twod_scale = 1; return ctx; } diff --git a/libs/video/targets/vid_x11_vulkan.c b/libs/video/targets/vid_x11_vulkan.c index c8aa494ea..66f15073a 100644 --- a/libs/video/targets/vid_x11_vulkan.c +++ b/libs/video/targets/vid_x11_vulkan.c @@ -224,6 +224,7 @@ X11_Vulkan_Context (void) ctx->create_surface = x11_vulkan_create_surface; ctx->required_extensions = required_extensions; ctx->va_ctx = va_create_context (32); + ctx->twod_scale = 1; return ctx; }