From 775ac56bc837ee40466e62792e45a425fc9d6fab Mon Sep 17 00:00:00 2001 From: Lain Date: Mon, 17 Mar 2025 21:52:34 -0700 Subject: [PATCH] libobs/graphics: Add gs_draw_quadf() Adds a function to draw a sprite or quad using floating point sizes rather than integer sizes. Also named it more appropriately. --- docs/sphinx/graphics.rst | 2 +- .../reference-libobs-graphics-graphics.rst | 24 +++++++++++++++++-- libobs/graphics/graphics.c | 13 ++++++---- libobs/graphics/graphics.h | 1 + 4 files changed, 33 insertions(+), 7 deletions(-) diff --git a/docs/sphinx/graphics.rst b/docs/sphinx/graphics.rst index 8848a4653..ec085ad23 100644 --- a/docs/sphinx/graphics.rst +++ b/docs/sphinx/graphics.rst @@ -85,7 +85,7 @@ effects: **ViewProj**, and **image**. The **ViewProj** parameter combination. The **image** parameter (which is a texture2d) is a commonly used parameter for the main texture; this parameter will be used with the functions :c:func:`obs_source_draw()`, -:c:func:`gs_draw_sprite()`, and +:c:func:`gs_draw_sprite()`, :c:func:`gs_draw_quadf()`, and :c:func:`obs_source_process_filter_end()`. Here is an example of effect parameters: diff --git a/docs/sphinx/reference-libobs-graphics-graphics.rst b/docs/sphinx/reference-libobs-graphics-graphics.rst index 407abab40..d65de6d84 100644 --- a/docs/sphinx/reference-libobs-graphics-graphics.rst +++ b/docs/sphinx/reference-libobs-graphics-graphics.rst @@ -420,13 +420,33 @@ Draw Functions .. function:: void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t height) Draws a 2D sprite. Sets the "image" parameter of the current effect - to the texture and renders a quad. + to the texture and renders a quad. Can be omitted if drawing without + a texture. If width or height is 0, the width or height of the texture will be used. The flip value specifies whether the texture should be flipped on the U or V axis with GS_FLIP_U and GS_FLIP_V. - :param tex: Texture to draw + :param tex: Texture to draw (can be NULL if drawing without a + texture) + :param flip: Can be 0 or a bitwise-OR combination of one of the + following values: + + - GS_FLIP_U - Flips the texture horizontally + - GS_FLIP_V - Flips the texture vertically + + :param width: Width + :param height: Height + +--------------------- + +.. function:: void gs_draw_quadf(gs_texture_t *tex, uint32_t flip, float width, float height) + + Same as :c:func:`gs_draw_sprite()`, but with floating point width/height + values instead of integer width/height values. + + :param tex: Texture to draw (can be NULL if drawing without a + texture) :param flip: Can be 0 or a bitwise-OR combination of one of the following values: diff --git a/libobs/graphics/graphics.c b/libobs/graphics/graphics.c index 70fccaf54..bf9848df7 100644 --- a/libobs/graphics/graphics.c +++ b/libobs/graphics/graphics.c @@ -1004,7 +1004,7 @@ static inline void build_sprite_rect(struct gs_vb_data *data, gs_texture_t *tex, build_sprite(data, fcx, fcy, start_u, end_u, start_v, end_v); } -void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t height) +void gs_draw_quadf(gs_texture_t *tex, uint32_t flip, float width, float height) { graphics_t *graphics = thread_graphics; float fcx, fcy; @@ -1016,15 +1016,15 @@ void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t h return; } } else { - if (!width || !height) { + if (width == 0.0f || height == 0.0f) { blog(LOG_ERROR, "A sprite cannot be drawn without " "a width/height"); return; } } - fcx = width ? (float)width : (float)gs_texture_get_width(tex); - fcy = height ? (float)height : (float)gs_texture_get_height(tex); + fcx = width != 0.0f ? width : (float)gs_texture_get_width(tex); + fcy = height != 0.0f ? height : (float)gs_texture_get_height(tex); gs_matrix_push(); gs_matrix_scale3f(fcx, fcy, 1.0f); @@ -1045,6 +1045,11 @@ void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t h gs_matrix_pop(); } +void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t height) +{ + gs_draw_quadf(tex, flip, (float)width, (float)height); +} + void gs_draw_sprite_subregion(gs_texture_t *tex, uint32_t flip, uint32_t sub_x, uint32_t sub_y, uint32_t sub_cx, uint32_t sub_cy) { diff --git a/libobs/graphics/graphics.h b/libobs/graphics/graphics.h index 1d290fb1b..099d02aa1 100644 --- a/libobs/graphics/graphics.h +++ b/libobs/graphics/graphics.h @@ -581,6 +581,7 @@ EXPORT uint8_t *gs_create_texture_file_data3(const char *file, enum gs_image_alp * axis with GS_FLIP_U and GS_FLIP_V. */ EXPORT void gs_draw_sprite(gs_texture_t *tex, uint32_t flip, uint32_t width, uint32_t height); +EXPORT void gs_draw_quadf(gs_texture_t *tex, uint32_t flip, float width, float height); EXPORT void gs_draw_sprite_subregion(gs_texture_t *tex, uint32_t flip, uint32_t x, uint32_t y, uint32_t cx, uint32_t cy);