This commit is contained in:
2026-08-05 16:57:44 +03:00
parent 2ac0413d78
commit da416fe5e2
27 changed files with 92 additions and 1293 deletions
+61 -28
View File
@@ -1,6 +1,5 @@
package main
import "core:fmt"
import rl "vendor:raylib"
Graphics :: union {
@@ -44,11 +43,11 @@ GfxTexture :: struct {}
GfxAnimatedSprite :: struct {
pos: rl.Vector2,
rect: rl.Rectangle,
tex: ^rl.Texture2D,
frame: u32,
atlas: ^Atlas,
frame: i32,
speed: f32,
current_time: f32,
update: proc(g: ^GfxAnimatedSprite, dt: f32),
}
PlayerTexFiles :: enum {
@@ -60,33 +59,65 @@ PlayerTexFiles :: enum {
ATTACK,
}
GraphicsAssetStore :: struct {
playerTex: map[PlayerTexFiles]rl.Texture2D,
Atlas :: struct {
tex: rl.Texture2D,
frame_size: rl.Vector2,
cols: i32,
rows: i32,
frames: i32,
get_offset: proc(a: ^Atlas, f: i32) -> rl.Vector2,
}
gfx_get :: proc(g: ^GraphicsAssetStore, f: PlayerTexFiles) -> ^rl.Texture2D {
GraphicsAssetStore :: struct {
playerTex: map[PlayerTexFiles]Atlas,
}
player_atlas_get :: proc(g: ^GraphicsAssetStore, f: PlayerTexFiles) -> ^Atlas {
return &g.playerTex[f]
}
gfx_init :: proc() -> GraphicsAssetStore {
st := GraphicsAssetStore{}
st.playerTex = make(map[PlayerTexFiles]rl.Texture2D)
st.playerTex[.IDLE] = rl.LoadTexture("assets/3rdparty/player_idle.png")
st.playerTex = make(map[PlayerTexFiles]Atlas)
st.playerTex[.IDLE] = init_atlas(
texture_path = "assets/3rdparty/player_idle.png",
size = rl.Vector2{64, 64},
)
return st
}
init_atlas :: proc(texture_path: cstring, size: rl.Vector2) -> Atlas {
atlas := Atlas {
tex = rl.LoadTexture(texture_path),
frame_size = size,
get_offset = get_offset,
}
atlas.rows = atlas.tex.height / (i32(size.y))
atlas.cols = atlas.tex.width / i32(size.x)
atlas.frames = atlas.cols * atlas.rows
return atlas
}
get_offset :: proc(a: ^Atlas, f: i32) -> rl.Vector2 {
col := f % a.cols
row := f / a.cols
return rl.Vector2{f32(col) * a.frame_size.x, f32(row) * a.frame_size.y}
}
gfx_free :: proc(g: GraphicsAssetStore) {
for _, tex in g.playerTex {
rl.UnloadTexture(tex)
rl.UnloadTexture(tex.tex)
}
delete(g.playerTex)
}
gfx_draw :: proc(pos: rl.Vector2, g: ^Graphics) {
#partial switch &v in g {
#partial switch v in g {
case GfxCircle:
rl.DrawCircleV(pos + v.pos, v.radius, v.color)
case GfxRect:
@@ -96,29 +127,31 @@ gfx_draw :: proc(pos: rl.Vector2, g: ^Graphics) {
case GfxCircleLines:
rl.DrawCircleLinesV(pos + v.pos, v.radius, v.color)
case GfxAnimatedSprite:
fmt.println(pos.x, pos.y)
//TODO: Count from L to R row-wize
v.rect.x = v.rect.width * f32(v.frame)
v.rect.y = v.rect.height * f32(v.frame)
frame_offset := v.atlas->get_offset(v.frame)
rl.DrawTexturePro(
v.tex^,
v.rect,
v.atlas^.tex,
rl.Rectangle {
pos.x - v.rect.width * 3 / 2,
pos.y - v.rect.height * 3 / 2,
v.rect.width * 3,
v.rect.height * 3,
frame_offset.x,
frame_offset.y,
v.atlas.frame_size.x,
v.atlas.frame_size.y,
},
rl.Rectangle {
pos.x - v.atlas.frame_size.x * 3 / 2,
pos.y - v.atlas.frame_size.y * 3 / 2,
v.atlas.frame_size.x * 3,
v.atlas.frame_size.y * 3,
},
rl.Vector2{0, 0},
0,
rl.WHITE,
)
v.current_time += rl.GetFrameTime()
if v.current_time > v.speed {
v.current_time = v.current_time - v.speed
v.frame += 1
}
// v.current_time += rl.GetFrameTime()
// if v.current_time > v.speed {
// v.current_time = v.current_time - v.speed
// v.frame += 1
// }
// rl.DrawTextureRec(v.tex, v.rect, rl.Vector2{pos.x, pos.y}, rl.Color{})
}
}