Cleanup raylib renderer

This commit is contained in:
FourteenBrush 2024-08-31 20:59:11 +02:00
parent 732ae76ef7
commit d50f021f0c
No known key found for this signature in database
GPG Key ID: DF8312C105596025

View File

@ -2,6 +2,7 @@ package main
import clay "../../clay-odin" import clay "../../clay-odin"
import "core:math" import "core:math"
import "core:strings"
import "vendor:raylib" import "vendor:raylib"
RaylibFont :: struct { RaylibFont :: struct {
@ -22,16 +23,16 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) ->
maxTextWidth: f32 = 0 maxTextWidth: f32 = 0
lineTextWidth: f32 = 0 lineTextWidth: f32 = 0
textHeight: f32 = cast(f32)config.fontSize textHeight := cast(f32)config.fontSize
fontToUse := raylibFonts[config.fontId].font fontToUse := raylibFonts[config.fontId].font
for i := 0; i < cast(int)text.length; i += 1 { for i in 0..<int(text.length) {
if (text.chars[i] == '\n') { if (text.chars[i] == '\n') {
maxTextWidth = math.max(maxTextWidth, lineTextWidth) maxTextWidth = max(maxTextWidth, lineTextWidth)
lineTextWidth = 0 lineTextWidth = 0
continue continue
} }
index: i32 = cast(i32)text.chars[i] - 32 index := cast(i32)text.chars[i] - 32
if (fontToUse.glyphs[index].advanceX != 0) { if (fontToUse.glyphs[index].advanceX != 0) {
lineTextWidth += cast(f32)fontToUse.glyphs[index].advanceX lineTextWidth += cast(f32)fontToUse.glyphs[index].advanceX
} else { } else {
@ -39,7 +40,7 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) ->
} }
} }
maxTextWidth = math.max(maxTextWidth, lineTextWidth) maxTextWidth = max(maxTextWidth, lineTextWidth)
textSize.width = maxTextWidth / 2 textSize.width = maxTextWidth / 2
textSize.height = textHeight textSize.height = textHeight
@ -48,168 +49,154 @@ measureText :: proc "c" (text: ^clay.String, config: ^clay.TextElementConfig) ->
} }
clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) { clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) {
for i := 0; i < cast(int)renderCommands.length; i += 1 { for i in 0..<int(renderCommands.length) {
renderCommand := clay.RenderCommandArray_Get(renderCommands, cast(i32)i) renderCommand := clay.RenderCommandArray_Get(renderCommands, cast(i32)i)
boundingBox := renderCommand.boundingBox boundingBox := renderCommand.boundingBox
switch (renderCommand.commandType) switch (renderCommand.commandType) {
{
case clay.RenderCommandType.None: case clay.RenderCommandType.None:
{} {}
case clay.RenderCommandType.Text: case clay.RenderCommandType.Text:
{ // Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator
// Raylib uses standard C strings so isn't compatible with cheap slices, we need to clone the string to append null terminator text := renderCommand.text
text := renderCommand.text cloned := make([]u8, text.length + 1, context.temp_allocator)
cloned := make([]u8, text.length + 1, context.temp_allocator) copy(cloned[:text.length], text.chars[:text.length])
copy(cloned[0:text.length], text.chars[0:text.length]) cloned[text.length] = 0
cloned[text.length] = 0
fontToUse: raylib.Font = raylibFonts[renderCommand.config.textElementConfig.fontId].font fontToUse: raylib.Font = raylibFonts[renderCommand.config.textElementConfig.fontId].font
raylib.DrawTextEx( raylib.DrawTextEx(
fontToUse, fontToUse,
cstring(raw_data(cloned)), cstring(raw_data(cloned)),
raylib.Vector2{boundingBox.x, boundingBox.y}, raylib.Vector2{boundingBox.x, boundingBox.y},
cast(f32)renderCommand.config.textElementConfig.fontSize, cast(f32)renderCommand.config.textElementConfig.fontSize,
cast(f32)renderCommand.config.textElementConfig.letterSpacing, cast(f32)renderCommand.config.textElementConfig.letterSpacing,
clayColorToRaylibColor(renderCommand.config.textElementConfig.textColor), clayColorToRaylibColor(renderCommand.config.textElementConfig.textColor),
) )
}
case clay.RenderCommandType.Image: case clay.RenderCommandType.Image:
{ // TODO image handling
// TODO image handling imageTexture := cast(^raylib.Texture2D)renderCommand.config.imageElementConfig.imageData
imageTexture := cast(^raylib.Texture2D)renderCommand.config.imageElementConfig.imageData raylib.DrawTextureEx(imageTexture^, raylib.Vector2{boundingBox.x, boundingBox.y}, 0, boundingBox.width / cast(f32)imageTexture.width, raylib.WHITE)
raylib.DrawTextureEx(imageTexture^, raylib.Vector2{boundingBox.x, boundingBox.y}, 0, boundingBox.width / cast(f32)imageTexture.width, raylib.WHITE)
}
case clay.RenderCommandType.ScissorStart: case clay.RenderCommandType.ScissorStart:
{ raylib.BeginScissorMode(
raylib.BeginScissorMode( cast(i32)math.round(boundingBox.x),
cast(i32)math.round(boundingBox.x), cast(i32)math.round(boundingBox.y),
cast(i32)math.round(boundingBox.y), cast(i32)math.round(boundingBox.width),
cast(i32)math.round(boundingBox.width), cast(i32)math.round(boundingBox.height),
cast(i32)math.round(boundingBox.height), )
)
}
case clay.RenderCommandType.ScissorEnd: case clay.RenderCommandType.ScissorEnd:
{ raylib.EndScissorMode()
raylib.EndScissorMode()
}
case clay.RenderCommandType.Rectangle: case clay.RenderCommandType.Rectangle:
{ config: ^clay.RectangleElementConfig = renderCommand.config.rectangleElementConfig
config: ^clay.RectangleElementConfig = renderCommand.config.rectangleElementConfig if (config.cornerRadius.topLeft > 0) {
if (config.cornerRadius.topLeft > 0) { radius: f32 = (config.cornerRadius.topLeft * 2) / min(boundingBox.width, boundingBox.height)
radius: f32 = (config.cornerRadius.topLeft * 2) / (boundingBox.width > boundingBox.height ? boundingBox.height : boundingBox.width) raylib.DrawRectangleRounded(
raylib.DrawRectangleRounded( raylib.Rectangle{boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height},
raylib.Rectangle{boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height}, radius,
radius, 8,
8, clayColorToRaylibColor(config.color),
clayColorToRaylibColor(config.color), )
) } else {
} else { raylib.DrawRectangle(
raylib.DrawRectangle( cast(i32)boundingBox.x,
cast(i32)boundingBox.x, cast(i32)boundingBox.y,
cast(i32)boundingBox.y, cast(i32)boundingBox.width,
cast(i32)boundingBox.width, cast(i32)boundingBox.height,
cast(i32)boundingBox.height, clayColorToRaylibColor(config.color),
clayColorToRaylibColor(config.color), )
)
}
} }
case clay.RenderCommandType.Border: case clay.RenderCommandType.Border:
{ config := renderCommand.config.borderElementConfig
config := renderCommand.config.borderElementConfig // Left border
// Left border if (config.left.width > 0) {
if (config.left.width > 0) { raylib.DrawRectangle(
raylib.DrawRectangle( cast(i32)math.round(boundingBox.x),
cast(i32)math.round(boundingBox.x), cast(i32)math.round(boundingBox.y + config.cornerRadius.topLeft),
cast(i32)math.round(boundingBox.y + config.cornerRadius.topLeft), cast(i32)config.left.width,
cast(i32)config.left.width, cast(i32)math.round(boundingBox.height - config.cornerRadius.topLeft - config.cornerRadius.bottomLeft),
cast(i32)math.round(boundingBox.height - config.cornerRadius.topLeft - config.cornerRadius.bottomLeft), clayColorToRaylibColor(config.left.color),
clayColorToRaylibColor(config.left.color), )
) }
} // Right border
// Right border if (config.right.width > 0) {
if (config.right.width > 0) { raylib.DrawRectangle(
raylib.DrawRectangle( cast(i32)math.round(boundingBox.x + boundingBox.width - cast(f32)config.right.width),
cast(i32)math.round(boundingBox.x + boundingBox.width - cast(f32)config.right.width), cast(i32)math.round(boundingBox.y + config.cornerRadius.topRight),
cast(i32)math.round(boundingBox.y + config.cornerRadius.topRight), cast(i32)config.right.width,
cast(i32)config.right.width, cast(i32)math.round(boundingBox.height - config.cornerRadius.topRight - config.cornerRadius.bottomRight),
cast(i32)math.round(boundingBox.height - config.cornerRadius.topRight - config.cornerRadius.bottomRight), clayColorToRaylibColor(config.right.color),
clayColorToRaylibColor(config.right.color), )
) }
} // Top border
// Top border if (config.top.width > 0) {
if (config.top.width > 0) { raylib.DrawRectangle(
raylib.DrawRectangle( cast(i32)math.round(boundingBox.x + config.cornerRadius.topLeft),
cast(i32)math.round(boundingBox.x + config.cornerRadius.topLeft), cast(i32)math.round(boundingBox.y),
cast(i32)math.round(boundingBox.y), cast(i32)math.round(boundingBox.width - config.cornerRadius.topLeft - config.cornerRadius.topRight),
cast(i32)math.round(boundingBox.width - config.cornerRadius.topLeft - config.cornerRadius.topRight), cast(i32)config.top.width,
cast(i32)config.top.width, clayColorToRaylibColor(config.top.color),
clayColorToRaylibColor(config.top.color), )
) }
} // Bottom border
// Bottom border if (config.bottom.width > 0) {
if (config.bottom.width > 0) { raylib.DrawRectangle(
raylib.DrawRectangle( cast(i32)math.round(boundingBox.x + config.cornerRadius.bottomLeft),
cast(i32)math.round(boundingBox.x + config.cornerRadius.bottomLeft), cast(i32)math.round(boundingBox.y + boundingBox.height - cast(f32)config.bottom.width),
cast(i32)math.round(boundingBox.y + boundingBox.height - cast(f32)config.bottom.width), cast(i32)math.round(boundingBox.width - config.cornerRadius.bottomLeft - config.cornerRadius.bottomRight),
cast(i32)math.round(boundingBox.width - config.cornerRadius.bottomLeft - config.cornerRadius.bottomRight), cast(i32)config.bottom.width,
cast(i32)config.bottom.width, clayColorToRaylibColor(config.bottom.color),
clayColorToRaylibColor(config.bottom.color), )
) }
} if (config.cornerRadius.topLeft > 0) {
if (config.cornerRadius.topLeft > 0) { raylib.DrawRing(
raylib.DrawRing( raylib.Vector2{math.round(boundingBox.x + config.cornerRadius.topLeft), math.round(boundingBox.y + config.cornerRadius.topLeft)},
raylib.Vector2{math.round(boundingBox.x + config.cornerRadius.topLeft), math.round(boundingBox.y + config.cornerRadius.topLeft)}, math.round(config.cornerRadius.topLeft - cast(f32)config.top.width),
math.round(config.cornerRadius.topLeft - cast(f32)config.top.width), config.cornerRadius.topLeft,
config.cornerRadius.topLeft, 180,
180, 270,
270, 10,
10, clayColorToRaylibColor(config.top.color),
clayColorToRaylibColor(config.top.color), )
) }
} if (config.cornerRadius.topRight > 0) {
if (config.cornerRadius.topRight > 0) { raylib.DrawRing(
raylib.DrawRing( raylib.Vector2{math.round(boundingBox.x + boundingBox.width - config.cornerRadius.topRight), math.round(boundingBox.y + config.cornerRadius.topRight)},
raylib.Vector2{math.round(boundingBox.x + boundingBox.width - config.cornerRadius.topRight), math.round(boundingBox.y + config.cornerRadius.topRight)}, math.round(config.cornerRadius.topRight - cast(f32)config.top.width),
math.round(config.cornerRadius.topRight - cast(f32)config.top.width), config.cornerRadius.topRight,
config.cornerRadius.topRight, 270,
270, 360,
360, 10,
10, clayColorToRaylibColor(config.top.color),
clayColorToRaylibColor(config.top.color), )
) }
} if (config.cornerRadius.bottomLeft > 0) {
if (config.cornerRadius.bottomLeft > 0) { raylib.DrawRing(
raylib.DrawRing( raylib.Vector2 {
raylib.Vector2 { math.round(boundingBox.x + config.cornerRadius.bottomLeft),
math.round(boundingBox.x + config.cornerRadius.bottomLeft), math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomLeft),
math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomLeft), },
}, math.round(config.cornerRadius.bottomLeft - cast(f32)config.top.width),
math.round(config.cornerRadius.bottomLeft - cast(f32)config.top.width), config.cornerRadius.bottomLeft,
config.cornerRadius.bottomLeft, 90,
90, 180,
180, 10,
10, clayColorToRaylibColor(config.bottom.color),
clayColorToRaylibColor(config.bottom.color), )
) }
} if (config.cornerRadius.bottomRight > 0) {
if (config.cornerRadius.bottomRight > 0) { raylib.DrawRing(
raylib.DrawRing( raylib.Vector2 {
raylib.Vector2 { math.round(boundingBox.x + boundingBox.width - config.cornerRadius.bottomRight),
math.round(boundingBox.x + boundingBox.width - config.cornerRadius.bottomRight), math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomRight),
math.round(boundingBox.y + boundingBox.height - config.cornerRadius.bottomRight), },
}, math.round(config.cornerRadius.bottomRight - cast(f32)config.bottom.width),
math.round(config.cornerRadius.bottomRight - cast(f32)config.bottom.width), config.cornerRadius.bottomRight,
config.cornerRadius.bottomRight, 0.1,
0.1, 90,
90, 10,
10, clayColorToRaylibColor(config.bottom.color),
clayColorToRaylibColor(config.bottom.color), )
)
}
} }
case clay.RenderCommandType.Custom: case clay.RenderCommandType.Custom:
{ // Implement custom element rendering here
// Implement custom element rendering here
}
} }
} }
} }