Better structure and size constants for pixel art resolution.
This commit is contained in:
+57
-31
@@ -4,11 +4,18 @@ import "core:fmt"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// LEVEL specifics
|
||||
// LEVEL Specifics
|
||||
// ------------------------------------------------------------------------------
|
||||
FIELD_BORDER :: 3
|
||||
FIELD_BORDER :: 2
|
||||
SERVE_WAIT_TIME :: 1.0
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// HUD Styling Constants
|
||||
// ------------------------------------------------------------------------------
|
||||
HUD_FONT_SIZE :: 10.0
|
||||
HUD_FONT_SPACING :: 1.0
|
||||
HUD_PADDING :: 4.0
|
||||
|
||||
Level :: struct {
|
||||
field_bounds: rl.Rectangle,
|
||||
player: Entity,
|
||||
@@ -19,19 +26,17 @@ Level :: struct {
|
||||
score_player: int,
|
||||
score_enemy: int,
|
||||
score_win: int,
|
||||
particles: [dynamic]Particle,
|
||||
}
|
||||
|
||||
init_level :: proc() -> Level {
|
||||
// positions work off viewport resolution,
|
||||
// independent of target display size
|
||||
// 10% padding around the field
|
||||
pad_x := f32(VIEWPORT_W) * 0.10
|
||||
pad_x := f32(VIEWPORT_W) * 0.10
|
||||
pad_y := f32(VIEWPORT_H) * 0.10
|
||||
|
||||
bounds := rl.Rectangle{
|
||||
x = pad_x,
|
||||
y = pad_y,
|
||||
width = f32(VIEWPORT_W) - (pad_x * 2),
|
||||
width = f32(VIEWPORT_W) - (pad_x * 2),
|
||||
height = f32(VIEWPORT_H) - (pad_y * 2),
|
||||
}
|
||||
|
||||
@@ -53,18 +58,28 @@ init_level :: proc() -> Level {
|
||||
score_win = 1,
|
||||
score_player = 0,
|
||||
score_enemy = 0,
|
||||
particles = make([dynamic]Particle),
|
||||
}
|
||||
}
|
||||
|
||||
// Free memory allocated by dynamic slice fields in the level struct
|
||||
cleanup_level :: proc(level: ^Level) {
|
||||
delete(level.particles)
|
||||
}
|
||||
|
||||
update_level :: proc(level: ^Level, input: Input_Event, dt: f32) {
|
||||
update_ball(&level.ball, level, dt)
|
||||
update_player(&level.player, input, level.field_bounds, dt)
|
||||
update_opponent(&level.opponent, level.ball, level.field_bounds, dt)
|
||||
update_particles(&level.particles, dt)
|
||||
}
|
||||
|
||||
render_level :: proc(level: Level) {
|
||||
rl.DrawRectangleRec(level.field_bounds, rl.DARKGRAY)
|
||||
rl.DrawRectangleLinesEx(level.field_bounds, FIELD_BORDER, rl.RAYWHITE)
|
||||
rl.BeginBlendMode(.ADDITIVE)
|
||||
// Draw outward field glow
|
||||
render_field_outward_glow(level.field_bounds, FIELD_BORDER, rl.WHITE)
|
||||
render_particles(level.particles)
|
||||
rl.EndBlendMode()
|
||||
|
||||
center_x := f32(VIEWPORT_W) / 2.0
|
||||
start_pos := rl.Vector2{center_x, level.field_bounds.y + FIELD_BORDER}
|
||||
@@ -76,31 +91,42 @@ render_level :: proc(level: Level) {
|
||||
render_ball(level.ball, level.field_bounds)
|
||||
}
|
||||
|
||||
render_field_outward_glow :: proc(bounds: rl.Rectangle, line_thickness: f32, glow_color: rl.Color) {
|
||||
rl.DrawRectangleLinesEx(bounds, line_thickness, glow_color)
|
||||
|
||||
glow_layers := 24
|
||||
glow_step := f32(1.0)
|
||||
|
||||
for i in 1 ..= glow_layers {
|
||||
offset := f32(i) * glow_step
|
||||
|
||||
outer_rec := rl.Rectangle{
|
||||
x = bounds.x - offset,
|
||||
y = bounds.y - offset,
|
||||
width = bounds.width + (offset * 2.0),
|
||||
height = bounds.height + (offset * 2.0),
|
||||
}
|
||||
|
||||
alpha := u8(155.0 / f32(i * i))
|
||||
layer_color := rl.Color{glow_color.r, glow_color.g, glow_color.b, alpha}
|
||||
|
||||
rl.DrawRectangleLinesEx(outer_rec, line_thickness, layer_color)
|
||||
}
|
||||
}
|
||||
|
||||
render_hud :: proc(level: Level) {
|
||||
// 2 spacing, 20 font size, 5 padding
|
||||
font := rl.GetFontDefault()
|
||||
|
||||
// player one id
|
||||
p1_size := rl.MeasureTextEx(font, "PLAYER 1", 20, 2)
|
||||
p1_box := rl.Rectangle{5, 5, p1_size.x, p1_size.y}
|
||||
p1_pos := rl.Vector2{p1_box.x, p1_box.y}
|
||||
rl.DrawTextEx(font, "PLAYER 1", p1_pos, 20, 2, rl.WHITE)
|
||||
// rl.DrawRectangleLinesEx(p1_box, 1.0, rl.RED)
|
||||
p1_size := rl.MeasureTextEx(font, "PLAYER 1", HUD_FONT_SIZE, HUD_FONT_SPACING)
|
||||
p1_pos := rl.Vector2{HUD_PADDING, HUD_PADDING}
|
||||
rl.DrawTextEx(font, "PLAYER 1", p1_pos, HUD_FONT_SIZE, HUD_FONT_SPACING, rl.WHITE)
|
||||
|
||||
// opponent id
|
||||
opp_size := rl.MeasureTextEx(font, "COMPUTER", 20, 2)
|
||||
opp_box := rl.Rectangle{(VIEWPORT_W - 5 - opp_size.x), 5, (opp_size.x), (opp_size.y)}
|
||||
opp_pos := rl.Vector2{opp_box.x, opp_box.y}
|
||||
rl.DrawTextEx(font, "COMPUTER", opp_pos, 20, 2, rl.WHITE)
|
||||
opp_size := rl.MeasureTextEx(font, "COMPUTER", HUD_FONT_SIZE, HUD_FONT_SPACING)
|
||||
opp_pos := rl.Vector2{f32(VIEWPORT_W) - HUD_PADDING - opp_size.x, HUD_PADDING}
|
||||
rl.DrawTextEx(font, "COMPUTER", opp_pos, HUD_FONT_SIZE, HUD_FONT_SPACING, rl.WHITE)
|
||||
|
||||
// score box
|
||||
// Format the score dynamically into a C-string allocated on the temp allocator
|
||||
score_text := fmt.ctprintf("%d : %d", level.score_player, level.score_enemy)
|
||||
score_size := rl.MeasureTextEx(font, score_text, 20, 2)
|
||||
score_box := rl.Rectangle{(VIEWPORT_W / 2 - score_size.x / 2), 5, (score_size.x), (score_size.y)}
|
||||
score_pos := rl.Vector2{score_box.x, score_box.y}
|
||||
rl.DrawTextEx(font, score_text, score_pos, 20, 2, rl.WHITE)
|
||||
|
||||
// game name
|
||||
// rl.DrawRectangleLines(VIEWPORT_W / 2 - 50, VIEWPORT_H - 50, 100, 50, rl.RED)
|
||||
}
|
||||
score_size := rl.MeasureTextEx(font, score_text, HUD_FONT_SIZE, HUD_FONT_SPACING)
|
||||
score_pos := rl.Vector2{(f32(VIEWPORT_W) * 0.5) - (score_size.x * 0.5), HUD_PADDING}
|
||||
rl.DrawTextEx(font, score_text, score_pos, HUD_FONT_SIZE, HUD_FONT_SPACING, rl.WHITE)
|
||||
}
|
||||
+61
-44
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user