Better structure and size constants for pixel art resolution.

This commit is contained in:
2026-09-28 08:04:30 +02:00
parent 23301801e7
commit a185a1e777
2 changed files with 118 additions and 75 deletions
+61 -44
View File
@@ -27,11 +27,22 @@ push_scene :: proc(game: ^Game, scene: Scene) {
pop_scene :: proc(game: ^Game) {
if len(game.scene_stack) > 1 {
top_index := len(game.scene_stack) - 1
destroy_scene(&game.scene_stack[top_index])
pop(&game.scene_stack)
}
}
destroy_scene :: proc(scene: ^Scene) {
if level, ok := &scene.level.?; ok {
cleanup_level(level)
}
}
set_root_scene :: proc(game: ^Game, root_scene: Scene) {
for &scene in game.scene_stack {
destroy_scene(&scene)
}
clear(&game.scene_stack)
append(&game.scene_stack, root_scene)
}
@@ -90,6 +101,18 @@ scene_render :: proc(scene: ^Scene) {
}
}
// ------------------------------------------------------------------------------
// UI Layout & Styling Constants
// ------------------------------------------------------------------------------
UI_TITLE_FONT_SIZE :: 20
UI_LABEL_FONT_SIZE :: 10
UI_TITLE_PADDING :: 10.0
UI_ITEM_HEIGHT :: 20.0
UI_ITEM_SPACING :: 8.0
UI_ITEM_BORDER_THICK :: 2.0
UI_TOP_MARGIN_DEFAULT :: 60.0
UI_TOP_MARGIN_LIST :: 75.0
// ------------------------------------------------------------------------------
// UI Definitions
// ------------------------------------------------------------------------------
@@ -112,8 +135,8 @@ UI_Slider :: struct {
}
UI_Selector :: struct {
label: cstring,
value_label: cstring,
label: cstring,
value_label: cstring,
}
UI_Item :: union {
@@ -128,7 +151,7 @@ UI_Menu :: struct {
selected_index: int,
}
get_canvas_item_rec :: proc(canvas: Canvas, index: int, top_margin: f32 = 120) -> rl.Rectangle {
get_canvas_item_rec :: proc(canvas: Canvas, index: int, top_margin: f32 = UI_TOP_MARGIN_DEFAULT) -> rl.Rectangle {
y_pos := (canvas.bounds.y + top_margin) + (f32(index) * (canvas.item_height + canvas.item_spaceing))
width_value := (canvas.bounds.width * 0.5)
@@ -143,26 +166,26 @@ get_canvas_item_rec :: proc(canvas: Canvas, index: int, top_margin: f32 = 120) -
list_menu_items :: proc(canvas: Canvas, menu: UI_Menu) {
for i in 0 ..< menu.count {
item := menu.items[i]
item_rec := get_canvas_item_rec(canvas, i, top_margin = 150.0)
item_rec := get_canvas_item_rec(canvas, i, top_margin = UI_TOP_MARGIN_LIST)
is_selected := (i == menu.selected_index)
border_color := is_selected ? rl.ORANGE : rl.DARKGRAY
text_color := is_selected ? rl.ORANGE : rl.WHITE
rl.DrawRectangleLinesEx(item_rec, 3, border_color)
rl.DrawRectangleLinesEx(item_rec, UI_ITEM_BORDER_THICK, border_color)
#partial switch v in item {
case UI_Button:
text_width := rl.MeasureText(v.label, 20)
text_width := rl.MeasureText(v.label, UI_LABEL_FONT_SIZE)
text_x := i32(item_rec.x + item_rec.width * 0.5) - (text_width / 2)
text_y := i32(item_rec.y + (item_rec.height * 0.5)) - 10
rl.DrawText(v.label, text_x, text_y, 20, text_color)
text_y := i32(item_rec.y + (item_rec.height * 0.5)) - (UI_LABEL_FONT_SIZE / 2)
rl.DrawText(v.label, text_x, text_y, UI_LABEL_FONT_SIZE, text_color)
case UI_Slider:
slider_rec := item_rec
text_width := rl.MeasureText(v.label, 20)
slider_rec.x = slider_rec.x + f32(text_width)
slider_rec.width = (slider_rec.width - f32(text_width))
text_width := rl.MeasureText(v.label, UI_LABEL_FONT_SIZE)
slider_rec.x = slider_rec.x + f32(text_width * 2)
slider_rec.width = (slider_rec.width - f32(text_width * 2))
rl.GuiSlider(
bounds = slider_rec,
@@ -174,14 +197,13 @@ list_menu_items :: proc(canvas: Canvas, menu: UI_Menu) {
)
case UI_Selector:
// draw label left half
lbl_y := i32(item_rec.y + (item_rec.height * 0.5)) - 10
rl.DrawText(v.label, i32(item_rec.x + 15), lbl_y, 20, text_color)
lbl_y := i32(item_rec.y + (item_rec.height * 0.5)) - (UI_LABEL_FONT_SIZE / 2)
rl.DrawText(v.label, i32(item_rec.x + 8), lbl_y, UI_LABEL_FONT_SIZE, text_color)
display_str := fmt.ctprintf("< %s >", v.value_label)
str_w := rl.MeasureText(display_str, 20)
val_x := i32(item_rec.x + item_rec.width) - str_w - 15
rl.DrawText(display_str, val_x, lbl_y, 20, text_color)
str_w := rl.MeasureText(display_str, UI_LABEL_FONT_SIZE)
val_x := i32(item_rec.x + item_rec.width) - str_w - 8
rl.DrawText(display_str, val_x, lbl_y, UI_LABEL_FONT_SIZE, text_color)
}
}
}
@@ -200,9 +222,9 @@ make_main_menu :: proc() -> Scene {
type = .Main_Menu,
canvas = Canvas{
bounds = rl.Rectangle{0, 0, VIEWPORT_W, VIEWPORT_H},
padding = 20,
item_height = 40,
item_spaceing = 15,
padding = UI_TITLE_PADDING,
item_height = UI_ITEM_HEIGHT,
item_spaceing = UI_ITEM_SPACING,
menu = UI_Menu{
items = ui_items,
count = 3,
@@ -219,12 +241,10 @@ render_main_menu :: proc(scene: Scene) {
menu, menu_ok := canvas.menu.?
assert(menu_ok, "render_main_menu: Canvas is missing a UI_Menu component")
rl.DrawRectangleRec(canvas.bounds, rl.BLACK)
t_text: cstring = "PONG"
t_width := rl.MeasureText(t_text, 40)
t_width := rl.MeasureText(t_text, UI_TITLE_FONT_SIZE)
t_x := i32(canvas.bounds.width * 0.5) - (t_width / 2)
rl.DrawText(t_text, t_x, i32(canvas.padding), 40, rl.WHITE)
rl.DrawText(t_text, t_x, i32(canvas.padding), UI_TITLE_FONT_SIZE, rl.WHITE)
list_menu_items(canvas, menu)
}
@@ -271,9 +291,9 @@ make_pause_overlay :: proc() -> Scene {
type = .Pause_Menu,
canvas = Canvas{
bounds = rl.Rectangle{0, 0, VIEWPORT_W, VIEWPORT_H},
padding = 20,
item_height = 40,
item_spaceing = 15,
padding = UI_TITLE_PADDING,
item_height = UI_ITEM_HEIGHT,
item_spaceing = UI_ITEM_SPACING,
menu = UI_Menu{
items = ui_items,
count = 3,
@@ -294,9 +314,9 @@ render_pause_menu :: proc(scene: Scene) {
rl.DrawRectangleRec(canvas.bounds, rl.Color{0, 0, 0, 200})
t_text: cstring = "PAUSE MENU"
t_width := rl.MeasureText(t_text, 40)
t_width := rl.MeasureText(t_text, UI_TITLE_FONT_SIZE)
t_x := i32(canvas.bounds.width * 0.5) - (t_width / 2)
rl.DrawText(t_text, t_x, i32(canvas.padding), 40, rl.WHITE)
rl.DrawText(t_text, t_x, i32(canvas.padding), UI_TITLE_FONT_SIZE, rl.WHITE)
list_menu_items(canvas, menu)
}
@@ -344,12 +364,12 @@ make_settings_menu :: proc(audio: ^Audio_Manager, current_res_label: cstring) ->
value_label = current_res_label,
},
2 = UI_Slider{
label = "Music Volume",
label = "Music ",
value = &audio.user_music_vol,
step = 0.1,
},
3 = UI_Slider{
label = "SFX Volume",
label = "SFX ",
value = &audio.sfx_vol,
step = 0.1,
},
@@ -359,9 +379,9 @@ make_settings_menu :: proc(audio: ^Audio_Manager, current_res_label: cstring) ->
type = .Settings_Menu,
canvas = Canvas{
bounds = rl.Rectangle{0, 0, VIEWPORT_W, VIEWPORT_H},
padding = 20,
item_height = 40,
item_spaceing = 15,
padding = UI_TITLE_PADDING,
item_height = UI_ITEM_HEIGHT,
item_spaceing = UI_ITEM_SPACING,
menu = UI_Menu{
items = ui_items,
count = 4,
@@ -378,13 +398,10 @@ render_settings_menu :: proc(scene: Scene) {
menu, menu_ok := canvas.menu.?
assert(menu_ok, "render_settings_menu: Canvas is missing a UI_Menu component")
// Dark semi-transparent overlay
rl.DrawRectangleRec(canvas.bounds, rl.Color{0, 0, 0, 255})
t_text: cstring = "SETTINGS"
t_width := rl.MeasureText(t_text, 40)
t_width := rl.MeasureText(t_text, UI_TITLE_FONT_SIZE)
t_x := i32(canvas.bounds.width * 0.5) - (t_width / 2)
rl.DrawText(t_text, t_x, i32(canvas.padding), 40, rl.WHITE)
rl.DrawText(t_text, t_x, i32(canvas.padding), UI_TITLE_FONT_SIZE, rl.WHITE)
list_menu_items(canvas, menu)
}
@@ -451,9 +468,9 @@ make_game_over_overlay :: proc(winner_is_player: bool) -> Scene {
winner_is_player = winner_is_player,
canvas = Canvas{
bounds = rl.Rectangle{0, 0, VIEWPORT_W, VIEWPORT_H},
padding = 20,
item_height = 40,
item_spaceing = 15,
padding = UI_TITLE_PADDING,
item_height = UI_ITEM_HEIGHT,
item_spaceing = UI_ITEM_SPACING,
menu = UI_Menu{
items = ui_items,
count = 2,
@@ -473,10 +490,10 @@ render_game_over :: proc(scene: Scene) {
rl.DrawRectangleRec(canvas.bounds, rl.Color{0, 0, 0, 220})
t_text: cstring = scene.winner_is_player ? "YOU WIN!" : "GAME OVER"
t_width := rl.MeasureText(t_text, 40)
t_width := rl.MeasureText(t_text, UI_TITLE_FONT_SIZE)
t_x := i32(canvas.bounds.width * 0.5) - (t_width / 2)
text_color := scene.winner_is_player ? rl.GREEN : rl.RED
rl.DrawText(t_text, t_x, i32(canvas.padding), 40, text_color)
rl.DrawText(t_text, t_x, i32(canvas.padding), UI_TITLE_FONT_SIZE, text_color)
list_menu_items(canvas, menu)
}