Skip to content

Commit

Permalink
Return bounding box in riv_draw_text
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed May 16, 2024
1 parent 654850f commit 344d0b5
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
3 changes: 1 addition & 2 deletions demos/hello-lua/hello.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ typedef enum riv_spritesheet_id {
typedef enum riv_align {
RIV_CENTER = 4,
} riv_align;
typedef struct riv_vec2i {int64_t x; int64_t y;} riv_vec2i;
bool riv_present();
void riv_clear(uint32_t col);
riv_vec2i riv_draw_text(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t size, int64_t col);
void riv_draw_text(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t size, int64_t col);
]]
local L = ffi.load("riv")

Expand Down
16 changes: 16 additions & 0 deletions demos/tools/text-anchor.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <riv.h>
#include <string.h>

int main() {
// main loop
riv->target_fps = 1;
do {
riv_clear(RIV_COLOR_BLACK);
riv_recti bbox = riv_draw_text("SOME TEXT",
RIV_SPRITESHEET_FONT_5X7,
riv->frame % 9, 128, 128, 2, RIV_COLOR_YELLOW);
riv_draw_rect_line(bbox.x-1, bbox.y-1, bbox.width+2, bbox.height+2, RIV_COLOR_LIGHTGREY);
riv_draw_rect_fill(128-1, 128-1, 3, 3, RIV_COLOR_LIGHTRED);
} while(riv_present());
return 0;
}
4 changes: 2 additions & 2 deletions demos/tools/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\
glyphs_draw_glyphs(56, 16, RIV_SPRITESHEET_FONT_5X7);
riv_draw_text("SMALL FONT - 3x5 pixels", RIV_SPRITESHEET_FONT_3X5, RIV_TOP, 128, 88, 1, RIV_COLOR_YELLOW);
glyphs_draw_glyphs(56, 98, RIV_SPRITESHEET_FONT_3X5);
riv_vec2i text_size = riv_draw_text(text, RIV_SPRITESHEET_FONT_5X7, RIV_TOPLEFT, 8, 168, 1, 1);
riv_draw_rect_line(6, 166, (text_size.x + 4), (text_size.y + 4), RIV_COLOR_RED);
riv_recti text_bbox = riv_draw_text(text, RIV_SPRITESHEET_FONT_5X7, RIV_TOPLEFT, 8, 168, 1, 1);
riv_draw_rect_line(6, 166, (text_bbox.width + 4), (text_bbox.height + 4), RIV_COLOR_RED);
} while(riv_present());
return 0;
}
4 changes: 2 additions & 2 deletions libriv/riv.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,9 @@ RIV_API void riv_draw_image_rect(uint64_t img_id, int64_t x0, int64_t y0, int64_
// Draw fill of a rectangle copied from a sprite sheet
RIV_API void riv_draw_sprite(uint32_t n, uint64_t sps_id, int64_t x0, int64_t y0, int64_t nw, int64_t nh, int64_t mw, int64_t mh);
// Draw text determined by its sprite sheet, anchor point, size and returns its size
RIV_API riv_vec2i riv_draw_text(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t size, int64_t col);
RIV_API riv_recti riv_draw_text(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t size, int64_t col);
// Draw text determined by its sprite sheet, anchor point, size, scale, spacing and returns its size
RIV_API riv_vec2i riv_draw_text_ex(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t mw, int64_t mh, int64_t sx, int64_t sy, int64_t col);
RIV_API riv_recti riv_draw_text_ex(const char* text, uint64_t sps_id, riv_align anchor, int64_t x, int64_t y, int64_t mw, int64_t mh, int64_t sx, int64_t sy, int64_t col);

////////////////////////////////////////
// Audio
Expand Down
4 changes: 2 additions & 2 deletions libriv/riv.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ global function riv_draw_triangle_fill(x0: int64, y0: int64, x1: int64, y1: int6
global function riv_draw_triangle_line(x0: int64, y0: int64, x1: int64, y1: int64, x2: int64, y2: int64, col: uint32): void <cimport,nodecl> end
global function riv_draw_image_rect(img_id: uint64, x0: int64, y0: int64, w: int64, h: int64, sx0: int64, sy0: int64, mw: int64, mh: int64): void <cimport,nodecl> end
global function riv_draw_sprite(n: uint32, sps_id: uint64, x0: int64, y0: int64, nw: int64, nh: int64, mw: int64, mh: int64): void <cimport,nodecl> end
global function riv_draw_text(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, size: int64, col: int64): riv_vec2i <cimport,nodecl> end
global function riv_draw_text_ex(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, mw: int64, mh: int64, sx: int64, sy: int64, col: int64): riv_vec2i <cimport,nodecl> end
global function riv_draw_text(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, size: int64, col: int64): riv_recti <cimport,nodecl> end
global function riv_draw_text_ex(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, mw: int64, mh: int64, sx: int64, sy: int64, col: int64): riv_recti <cimport,nodecl> end

-- Audio
global function riv_make_soundbuffer(desc: *riv_soundbuffer_desc): uint64 <cimport,nodecl> end
Expand Down
14 changes: 8 additions & 6 deletions libriv/riv_draw.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -638,29 +638,31 @@ local function riv_draw_text_topleft(text: cstring <const>, sps_id: uint64, x0:
return riv_vec2i{xmax - x0, ymax - y0}
end

global function riv_draw_text_ex(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, mw: int64, mh: int64, sx: int64, sy: int64, col: int64): riv_vec2i <cexport>
global function riv_draw_text_ex(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, mw: int64, mh: int64, sx: int64, sy: int64, col: int64): riv_recti <cexport>
if anchor == RIV_TOPLEFT or col < 0 then
return riv_draw_text_topleft(text, sps_id, x, y, mw, mh, sx, sy, col)
local bbox_size: riv_vec2i = riv_draw_text_topleft(text, sps_id, x, y, mw, mh, sx, sy, col)
return {x, y, bbox_size.x, bbox_size.y}
else
local bbox_size: riv_vec2i = riv_draw_text_topleft(text, sps_id, x, y, mw, mh, sx, sy, -1)
local halign: uint32 = (@uint32)(anchor) % 3
local valign: uint32 = (@uint32)(anchor) // 3
-- horizontal
if halign == 2 then -- right
x = x - bbox_size.x
x = x - bbox_size.x + 1
elseif halign == 1 then -- horizontal center
x = x - bbox_size.x /// 2
end
-- vertical
if valign == 2 then -- bottom
y = y - bbox_size.y
y = y - bbox_size.y + 1
elseif valign == 1 then -- vertical center
y = y - bbox_size.y /// 2
end
return riv_draw_text_topleft(text, sps_id, x, y, mw, mh, sx, sy, col)
bbox_size = riv_draw_text_topleft(text, sps_id, x, y, mw, mh, sx, sy, col)
return {x, y, bbox_size.x, bbox_size.y}
end
end

global function riv_draw_text(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, size: int64, col: int64): riv_vec2i <cexport>
global function riv_draw_text(text: cstring <const>, sps_id: uint64, anchor: riv_align, x: int64, y: int64, size: int64, col: int64): riv_recti <cexport>
return riv_draw_text_ex(text, sps_id, anchor, x, y, size, size, size, size, col)
end
1 change: 1 addition & 0 deletions rivemu-web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<option value="cartridges/palette.sqfs">TOOL - Palette</option>
<option value="cartridges/shapes.sqfs">TOOL - Shapes</option>
<option value="cartridges/text.sqfs">TOOL - Text</option>
<option value="cartridges/text-anchor.sqfs">TOOL - Text Anchor</option>
</select>
<div class="canvas-wrapper">
<canvas id="canvas" tabindex="-1" width="768" height="768" onclick="window.focus()" oncontextmenu="event.preventDefault()"></canvas>
Expand Down

0 comments on commit 344d0b5

Please sign in to comment.