mirror of
https://github.com/nicbarker/clay.git
synced 2025-04-20 05:08:04 +00:00
Cleanup raylib renderer
This commit is contained in:
parent
732ae76ef7
commit
d50f021f0c
@ -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,20 +49,19 @@ 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[0:text.length], text.chars[0:text.length])
|
copy(cloned[:text.length], text.chars[: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,
|
||||||
@ -71,31 +71,23 @@ clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) {
|
|||||||
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) / (boundingBox.width > boundingBox.height ? boundingBox.height : boundingBox.width)
|
radius: f32 = (config.cornerRadius.topLeft * 2) / min(boundingBox.width, boundingBox.height)
|
||||||
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,
|
||||||
@ -111,9 +103,7 @@ clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) {
|
|||||||
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) {
|
||||||
@ -205,11 +195,8 @@ clayRaylibRender :: proc(renderCommands: ^clay.ClayArray(clay.RenderCommand)) {
|
|||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user