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
+57 -31
View File
@@ -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)
}