move to directories. add examples
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package shoal
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
WindowComponent :: struct {
|
||||
width: i32,
|
||||
height: i32,
|
||||
title: string,
|
||||
background: rl.Color,
|
||||
}
|
||||
Window: Storage(WindowComponent)
|
||||
|
||||
SpriteComponent :: struct {
|
||||
texture: rl.Texture2D,
|
||||
}
|
||||
Sprite: Storage(SpriteComponent)
|
||||
|
||||
TransformComponent :: struct {
|
||||
translation: rl.Vector2,
|
||||
rotation: f32,
|
||||
}
|
||||
Transform: Storage(TransformComponent)
|
||||
@@ -0,0 +1,51 @@
|
||||
package shoal
|
||||
|
||||
import "core:strings"
|
||||
import rl "vendor:raylib"
|
||||
|
||||
STARTUP := 0
|
||||
UPDATE := 1
|
||||
DRAW := 2
|
||||
|
||||
App :: struct {
|
||||
startup_systems: [dynamic]proc(),
|
||||
update_systems: [dynamic]proc(),
|
||||
draw_systems: [dynamic]proc(),
|
||||
plugins: [dynamic]proc(app: ^App),
|
||||
entities: [dynamic]Entity,
|
||||
}
|
||||
|
||||
add_system :: proc(app: ^App, schedule: int, system: proc()) {
|
||||
switch schedule {
|
||||
case STARTUP:
|
||||
append(&app.startup_systems, system)
|
||||
case UPDATE:
|
||||
append(&app.update_systems, system)
|
||||
case DRAW:
|
||||
append(&app.draw_systems, system)
|
||||
}
|
||||
}
|
||||
|
||||
add_plugin :: proc(app: ^App, plugin: proc(app: ^App)) {
|
||||
append(&app.plugins, plugin)
|
||||
}
|
||||
|
||||
run :: proc(app: ^App, entity_limit: int = 1000) {
|
||||
for p in app.plugins {
|
||||
p(app)
|
||||
}
|
||||
for i in app.startup_systems {i()}
|
||||
|
||||
_, window := single(&Window)
|
||||
rl.InitWindow(window.width, window.height, strings.clone_to_cstring(window.title))
|
||||
defer rl.CloseWindow()
|
||||
|
||||
rl.SetTargetFPS(60)
|
||||
for !rl.WindowShouldClose() {
|
||||
rl.ClearBackground(window.background)
|
||||
for i in app.update_systems {i()}
|
||||
rl.BeginDrawing()
|
||||
for i in app.draw_systems {i()}
|
||||
rl.EndDrawing()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package shoal
|
||||
|
||||
import "core:fmt"
|
||||
|
||||
Entity :: distinct u32
|
||||
|
||||
Storage :: struct($T: typeid) {
|
||||
data: map[Entity]T,
|
||||
}
|
||||
|
||||
spawn_entity :: proc(app: ^App) -> Entity {
|
||||
entity_num := len(&app.entities)
|
||||
entity: Entity = Entity(entity_num)
|
||||
append(&app.entities, entity)
|
||||
return entity
|
||||
}
|
||||
|
||||
add_component :: proc {
|
||||
add_fresh_component,
|
||||
add_component_with_data,
|
||||
}
|
||||
|
||||
add_fresh_component :: proc(entity: Entity, storage: ^Storage($T)) {
|
||||
storage.data[entity] = T{}
|
||||
}
|
||||
|
||||
add_component_with_data :: proc(entity: Entity, storage: ^Storage($T), component: T) {
|
||||
storage.data[entity] = component
|
||||
}
|
||||
|
||||
slice :: proc(storage: ^Storage($T)) -> map[Entity]T {
|
||||
return storage.data
|
||||
}
|
||||
|
||||
single :: proc(storage: ^Storage($T)) -> (Entity, ^T) {
|
||||
for i, &v in slice(storage) {
|
||||
return i, &v
|
||||
}
|
||||
return Entity{}, new(T)
|
||||
}
|
||||
|
||||
get_component :: proc(entity: Entity, storage: ^Storage($T)) -> ^T {
|
||||
if c, exists := &storage.data[entity]; exists {
|
||||
return c
|
||||
}
|
||||
return new(T)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package shoal
|
||||
|
||||
import rl "vendor:raylib"
|
||||
|
||||
DefaultPlugin :: proc(app: ^App) {
|
||||
add_system(app, DRAW, draw_sprite)
|
||||
|
||||
display := spawn_entity(app)
|
||||
add_component(
|
||||
display,
|
||||
&Window,
|
||||
WindowComponent{width = 800, height = 600, title = "shoal window"},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
draw_sprite :: proc() {
|
||||
for i, &v in slice(&Sprite) {
|
||||
sprite_transform := get_component(i, &Transform)
|
||||
rl.DrawTexture(
|
||||
v.texture,
|
||||
i32(sprite_transform.translation.x),
|
||||
i32(sprite_transform.translation.y),
|
||||
rl.WHITE,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user