Added rect rendering to Lua API

This commit is contained in:
MysterD 2022-02-15 22:15:49 -08:00
parent 669e17bc18
commit 1ea28b9c5a
7 changed files with 197 additions and 19 deletions

129
docs/lua/examples/hud.lua Normal file
View file

@ -0,0 +1,129 @@
-- This file is an example of how to render to the screen
rainbow = 0
function test_text()
-- set text and scale
local text = "This is example text in the bottom right."
local scale = 1
-- render to native screen space, with the MENU font
djui_hud_set_resolution(RESOLUTION_DJUI);
djui_hud_set_font(FONT_MENU);
-- get width of screen and text
local screenWidth = djui_hud_get_screen_width()
local width = djui_hud_measure_text(text) * scale
-- get height of screen and text
local screenHeight = djui_hud_get_screen_height()
local height = 64 * scale
-- set location
local x = screenWidth - width
local y = screenHeight - height
-- set color and render
djui_hud_set_color(255, 0, 255, 255);
djui_hud_print_text(text, x, y, scale);
end
function test_texture()
-- render to N64's screen space
djui_hud_set_resolution(RESOLUTION_N64)
-- get player's character texture
local tex = gMarioStates[0].character.hudHeadTexture
-- set scale
local wscale = 1
local hscale = 1
-- set position
local x = 256
local y = 64
-- set color and render
djui_hud_set_color(255, 0, 255, 255)
djui_hud_render_texture(tex, x, y, wscale, hscale)
end
function test_rect()
-- render to native screen space
djui_hud_set_resolution(RESOLUTION_DJUI)
-- set location
local x = 512
local y = 512
-- set width/height
local w = 256
local h = 256
-- set color and render first rectangle
djui_hud_set_color(255, 0, 0, 128)
djui_hud_render_rect(x, y, w, h)
-- adjust location and size
x = x + 16
y = y + 16
w = w - 32
h = h - 32
-- set color and render second rectangle
djui_hud_set_color(0, 255, 0, 128)
djui_hud_render_rect(x, y, w, h)
-- adjust location and size
x = x + 16
y = y + 16
w = w - 32
h = h - 32
-- set color and render third rectangle
djui_hud_set_color(0, 0, 255, 128)
djui_hud_render_rect(x, y, w, h)
end
function test_rainbow_text()
-- this function is incredibly silly
-- don't do anything like this
local res = RESOLUTION_DJUI
local text = "HELLO WORLD"
local scale = 3
djui_hud_set_resolution(res);
djui_hud_set_font(FONT_NORMAL);
for i=0,255 do
j = rainbow / 50
local r = math.sin(0.00 + i / 15 + j) * 127 + 127
local g = math.sin(0.33 + i / 33 + j) * 127 + 127
local b = math.sin(0.66 + i / 77 + j) * 127 + 127
local x = 64 + i
local y = 64 + i + math.sin(i / 40 + j) * 64
if i == 255 then
r = 0
g = 0
b = 0
end
djui_hud_set_color(r, g, b, i);
djui_hud_print_text(text, x, y, scale);
end
rainbow = rainbow + 1
end
function on_hud_render()
test_text()
test_rect()
test_texture()
test_rainbow_text()
end
function on_mario_update(m)
if m.action == ACT_JUMP then
displaying = true
end
end
hook_event(HOOK_ON_HUD_RENDER, on_hud_render)

View file

@ -31,6 +31,7 @@
- [djui_hud_get_screen_width](#djui_hud_get_screen_width)
- [djui_hud_measure_text](#djui_hud_measure_text)
- [djui_hud_print_text](#djui_hud_print_text)
- [djui_hud_render_rect](#djui_hud_render_rect)
- [djui_hud_render_texture](#djui_hud_render_texture)
- [djui_hud_set_color](#djui_hud_set_color)
- [djui_hud_set_font](#djui_hud_set_font)
@ -708,6 +709,29 @@
<br />
## [djui_hud_render_rect](#djui_hud_render_rect)
### Lua Example
`djui_hud_render_rect(x, y, width, height)`
### Parameters
| Field | Type |
| ----- | ---- |
| x | number |
| y | number |
| width | number |
| height | number |
### Returns
- None
### C Prototype
`void djui_hud_render_rect(f32 x, f32 y, f32 width, f32 height);`
[:arrow_up_small:](#)
<br />
## [djui_hud_render_texture](#djui_hud_render_texture)
### Lua Example

View file

@ -32,3 +32,4 @@ Lua scripts you make can be placed either the `mods` folder in the base director
- [Low Gravity](../../mods/low-gravity.lua)
- [Faster Swimming](../../mods/faster-swimming.lua)
- [Hide and Seek](../../mods/hide-and-seek.lua)
- [HUD Rendering](examples/hud.lua)

View file

@ -16,7 +16,7 @@ static const Gfx djui_font_normal_text_begin[] = {
gsDPPipeSync(),
gsSPClearGeometryMode(G_LIGHTING),
gsDPSetCombineMode(G_CC_FADEA, G_CC_FADEA),
gsDPSetEnvColor(255, 255, 255, 255),
//gsDPSetEnvColor(255, 255, 255, 255),
gsDPSetRenderMode(G_RM_XLU_SURF, G_RM_XLU_SURF2),
gsDPSetTextureFilter(G_TF_POINT),
gsSPTexture(0xFFFF, 0xFFFF, 0, G_TX_RENDERTILE, G_ON),
@ -39,6 +39,7 @@ static void djui_font_normal_render_char(char c) {
extern const u8* const font_normal_chars[];
// replace undisplayable characters
if (c < ' ' || (u8)c > ('~' + 1)) { c = '?'; }
if (c == ' ') { return; }
void* fontChar = (void*)font_normal_chars[c - '!'];
if (fontChar == NULL) { fontChar = (void*)font_normal_chars[94]; }
@ -72,6 +73,7 @@ static void djui_font_title_render_char(char c) {
extern const u8* const font_title_chars[];
// replace undisplayable characters
if (c < ' ' || (u8)c > ('~' + 1)) { c = '?'; }
if (c == ' ') { return; }
djui_gfx_render_texture(font_title_chars[c - '!'], 64, 64, 32);
}
@ -96,24 +98,6 @@ static const struct DjuiFont sDjuiFontTitle = {
// font 3 (hud font) //
///////////////////////
/*
texture_hud_char_0, texture_hud_char_1, texture_hud_char_2, texture_hud_char_3,
texture_hud_char_4, texture_hud_char_5, texture_hud_char_6, texture_hud_char_7,
texture_hud_char_8, texture_hud_char_9, texture_hud_char_A, texture_hud_char_B,
texture_hud_char_C, texture_hud_char_D, texture_hud_char_E, texture_hud_char_F,
texture_hud_char_G, texture_hud_char_H, texture_hud_char_I, texture_hud_char_J,
texture_hud_char_K, texture_hud_char_L, texture_hud_char_M, texture_hud_char_N,
texture_hud_char_O, texture_hud_char_P, 0x0, texture_hud_char_R,
texture_hud_char_S, texture_hud_char_T, texture_hud_char_U, 0x0,
texture_hud_char_W, 0x0, texture_hud_char_Y, texture_hud_char_waluigi_head,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0, 0x0, texture_hud_char_multiply, texture_hud_char_coin,
texture_hud_char_mario_head, texture_hud_char_star, texture_hud_char_luigi_head, texture_hud_char_toad_head,
texture_hud_char_apostrophe, texture_hud_char_double_quote,
*/
static u8 djui_font_hud_index(char c) {
if (c == 'q' || c == 'Q') { return 50; }
if (c == 'v' || c == 'V') { return 50; }
@ -138,6 +122,7 @@ static u8 djui_font_hud_index(char c) {
}
static void djui_font_hud_render_char(char c) {
if (c == ' ') { return; }
u8 index = djui_font_hud_index(c);
djui_gfx_render_texture(main_hud_lut[index], 16, 16, 16);
}

View file

@ -160,3 +160,23 @@ void djui_hud_render_texture(struct TextureInfo* texInfo, f32 x, f32 y, f32 scal
djui_hud_render_texture_raw(texInfo->texture, texInfo->bitSize, texInfo->width, texInfo->width, x, y, scaleW, scaleH);
}
void djui_hud_render_rect(f32 x, f32 y, f32 width, f32 height) {
// translate position
f32 translatedX = x;
f32 translatedY = y;
djui_hud_position_translate(&translatedX, &translatedY);
create_dl_translation_matrix(DJUI_MTX_PUSH, translatedX, translatedY, 0);
// translate scale
f32 translatedW = width;
f32 translatedH = height;
djui_hud_size_translate(&translatedW);
djui_hud_size_translate(&translatedH);
create_dl_scale_matrix(DJUI_MTX_NOPUSH, translatedW, translatedH, 1.0f);
// render
gSPDisplayList(gDisplayListHead++, dl_djui_simple_rect);
// pop
gSPPopMatrix(gDisplayListHead++, G_MTX_MODELVIEW);
}

View file

@ -36,5 +36,6 @@ u32 djui_hud_get_screen_height(void);
f32 djui_hud_measure_text(const char* message);
void djui_hud_print_text(const char* message, float x, float y, float scale);
void djui_hud_render_texture(struct TextureInfo* texInfo, f32 x, f32 y, f32 scaleW, f32 scaleH);
void djui_hud_render_rect(f32 x, f32 y, f32 width, f32 height);
#endif

View file

@ -252,6 +252,23 @@ int smlua_func_djui_hud_print_text(lua_State* L) {
return 1;
}
int smlua_func_djui_hud_render_rect(lua_State* L) {
if(!smlua_functions_valid_param_count(L, 4)) { return 0; }
f32 x = smlua_to_number(L, 1);
if (!gSmLuaConvertSuccess) { return 0; }
f32 y = smlua_to_number(L, 2);
if (!gSmLuaConvertSuccess) { return 0; }
f32 width = smlua_to_number(L, 3);
if (!gSmLuaConvertSuccess) { return 0; }
f32 height = smlua_to_number(L, 4);
if (!gSmLuaConvertSuccess) { return 0; }
djui_hud_render_rect(x, y, width, height);
return 1;
}
int smlua_func_djui_hud_render_texture(lua_State* L) {
if(!smlua_functions_valid_param_count(L, 5)) { return 0; }
@ -3484,6 +3501,7 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "djui_hud_get_screen_width", smlua_func_djui_hud_get_screen_width);
smlua_bind_function(L, "djui_hud_measure_text", smlua_func_djui_hud_measure_text);
smlua_bind_function(L, "djui_hud_print_text", smlua_func_djui_hud_print_text);
smlua_bind_function(L, "djui_hud_render_rect", smlua_func_djui_hud_render_rect);
smlua_bind_function(L, "djui_hud_render_texture", smlua_func_djui_hud_render_texture);
smlua_bind_function(L, "djui_hud_set_color", smlua_func_djui_hud_set_color);
smlua_bind_function(L, "djui_hud_set_font", smlua_func_djui_hud_set_font);