package draw import "core:log" import rl "vendor:raylib" Sprite :: struct { image: rl.Texture2D, anim: Animation, active_anim: u32, frame_time: f32, current_frame: i32, width: i32, height: i32, pos: Vec2, offset: Vec2, rotation: f32, scale: Vec2, } init_sprite :: proc( sprite: ^Sprite, path: string, anim: Animation = {}, ) -> bool { byte_path := make([]u8, len(path) + 1, context.temp_allocator) copy(byte_path, path) byte_path[len(path)] = 0 sprite.image = rl.LoadTexture(transmute(cstring)&byte_path[0]) sprite.anim = anim sprite.width = i32(int(sprite.image.width) / len(sprite.anim.frames)) sprite.height = i32(sprite.image.height) sprite.scale = Vec2{1, 1} log.debugf( "loaded sprite '%v' %vx%v (%vx%v)", path, sprite.width, sprite.height, sprite.image.width, sprite.image.height, ) return true } destroy_sprite :: proc(sprite: Sprite) { rl.UnloadTexture(sprite.image) } set_sprite_active_tag :: proc(sprite: ^Sprite, tag_name: string) { sprite.active_anim = sprite.anim.frame_tags_indices[tag_name] } update_sprite :: proc(sprite: ^Sprite, dt: f32) { sprite.frame_time += dt tag := sprite.anim.frame_tags[sprite.active_anim] frame := sprite.anim.frames[sprite.current_frame] duration := f32(frame.duration) / 1000 if sprite.frame_time > duration { sprite.frame_time -= duration sprite.current_frame += 1 } if sprite.current_frame < tag.from { sprite.current_frame = tag.to } if sprite.current_frame > tag.to { sprite.current_frame = tag.from } } sprite :: proc( sprite: Sprite, ) { frame := sprite.anim.frames[sprite.current_frame] texture_quad( sprite.image, Rect { Vec2{f32(frame.rect.x), f32(frame.rect.y)}, Vec2{f32(frame.rect.w), f32(frame.rect.h)}, }, sprite.pos, sprite.offset, sprite.rotation, sprite.scale, ) }