118 lines
2.8 KiB
Odin
118 lines
2.8 KiB
Odin
package leveleditor
|
|
|
|
import rl "vendor:raylib"
|
|
import "core:math"
|
|
import "core:math/rand"
|
|
import "core:fmt"
|
|
import ui "vendor:microui"
|
|
import "rlmui"
|
|
|
|
Vec2 :: [2]f32
|
|
Rect :: rl.Rectangle
|
|
Img :: rl.Image
|
|
Color :: rl.Color
|
|
Tex :: rl.Texture
|
|
Key :: rl.KeyboardKey
|
|
|
|
SCREEN_WIDTH : i32 = 1400
|
|
SCREEN_HEIGHT : i32 = 1000
|
|
|
|
|
|
AppState :: struct {
|
|
dt: f32,
|
|
ctx: ^ui.Context,
|
|
keymap: map[string]Key,
|
|
mouse_drag_start: Maybe(Vec2),
|
|
tileset: Tex,
|
|
}
|
|
|
|
init_state :: proc() -> ^AppState {
|
|
tileset := rl.LoadImage("./Assets/Tileset/tileset.png")
|
|
defer rl.UnloadImage(tileset)
|
|
|
|
keymap := make(map[string]Key)
|
|
keymap["Left"] = .A
|
|
keymap["Right"] = .D
|
|
keymap["Up"] = .W
|
|
keymap["Down"] = .S
|
|
|
|
ctx := rlmui.InitUIScope()
|
|
state := AppState {
|
|
keymap = keymap,
|
|
tileset = rl.LoadTextureFromImage(tileset),
|
|
}
|
|
|
|
return new_clone(state)
|
|
}
|
|
|
|
user_input :: proc(s: ^AppState) {
|
|
if rl.IsMouseButtonPressed(rl.MouseButton.LEFT) {
|
|
s.mouse_drag_start = rl.GetMousePosition()
|
|
}
|
|
if rl.IsMouseButtonReleased(rl.MouseButton.LEFT) {
|
|
s.mouse_drag_start = nil
|
|
}
|
|
}
|
|
|
|
update :: proc(s: ^AppState) {
|
|
}
|
|
|
|
draw :: proc(s: ^AppState) {
|
|
w, h := f32(s.tileset.width), f32(s.tileset.height)
|
|
src := Rect{0, 0, w, h}
|
|
ratio := h / w
|
|
scale :f32 = 2.6
|
|
dst := Rect{5, 5, w * scale, h * scale}
|
|
rl.DrawTexturePro(s.tileset, src, dst, {0,0}, 0, rl.WHITE)
|
|
pixels :f32 = 8
|
|
color_lines := Color{255, 255, 255, 127}
|
|
padding :f32 = 5
|
|
for row in 0..=h / pixels {
|
|
start := Vec2{0, row * pixels * scale}
|
|
end := Vec2{w * scale, row * pixels * scale}
|
|
rl.DrawLineEx(start + padding, end + padding, 1, color_lines)
|
|
}
|
|
for col in 0..=w / pixels {
|
|
start := Vec2{col * pixels * scale, 0}
|
|
end := Vec2{col * pixels * scale, h * scale}
|
|
rl.DrawLineEx(start + padding, end + padding, 1, color_lines)
|
|
}
|
|
// Rectangular Mouse Selection
|
|
if mpos_start, ok := s.mouse_drag_start.(Vec2); ok {
|
|
mpos := rl.GetMousePosition()
|
|
start_x := min(mpos.x, mpos_start.x)
|
|
start_y := min(mpos.y, mpos_start.y)
|
|
w := abs(mpos.x - mpos_start.x)
|
|
h := abs(mpos.y - mpos_start.y)
|
|
rl.DrawRectangleLinesEx(Rect{start_x, start_y, w, h }, 4, rl.BLUE)
|
|
}
|
|
}
|
|
|
|
main :: proc() {
|
|
rl.SetTraceLogLevel(.ERROR)
|
|
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Level Editor")
|
|
rl.SetTargetFPS(60)
|
|
|
|
state := init_state()
|
|
defer free(state)
|
|
|
|
for !rl.WindowShouldClose() {
|
|
state.dt = rl.GetFrameTime()
|
|
|
|
user_input(state)
|
|
|
|
update(state)
|
|
|
|
rl.BeginDrawing()
|
|
rl.ClearBackground(rl.GRAY)
|
|
|
|
draw(state)
|
|
|
|
free_all(context.temp_allocator)
|
|
rl.EndDrawing()
|
|
}
|
|
|
|
rl.UnloadTexture(state.tileset)
|
|
rl.CloseWindow()
|
|
}
|