diff --git a/build.zig b/build.zig index eb71b0b..7f7d925 100644 --- a/build.zig +++ b/build.zig @@ -28,8 +28,15 @@ pub fn build(b: *Build) !void { odin_exec.addArg("-define:TRACY_ENABLE=true"); odin_exec.addArg(b.fmt("-define:TRACY_CALLSTACK={}", .{tracy_callstack})); } - const dancer_static_lib = odin_exec.addPrefixedOutputFileArg("-out:", switch (target.result.abi) { - else => "game.a", + odin_exec.addArg(b.fmt("-target:{s}", .{switch (target.result.os.tag) { + .linux => "linux_amd64", + .windows => "windows_amd64", + else => return error.OsNotSupported, + }})); + const dancer_static_lib = odin_exec.addPrefixedOutputFileArg("-out:", switch (target.result.os.tag) { + .linux => "game.a", + .windows => "game.obj", + else => return error.OsNotSupported, }); odin_exec.addDirectoryArg(b.path(switch (target.result.cpu.arch) { .wasm32, .wasm64 => "src/main_web", diff --git a/src/game.odin b/src/game.odin index 550d726..fecc4b8 100644 --- a/src/game.odin +++ b/src/game.odin @@ -10,6 +10,7 @@ import "core:container/queue" import "core:container/topological_sort" import "core:fmt" import "core:log" +import "core:math/linalg" import "core:math/rand" import "core:mem" import "core:reflect" @@ -21,6 +22,7 @@ import "tracy" import ray "vendor:raylib" import "vendor:raylib/rlgl" +LOG_LEVEL :: log.Level.Debug MAJOR_COUNT :: 22 MINOR_COUNT :: 14 TAROT_COUNT :: MAJOR_COUNT + 4 * MINOR_COUNT @@ -47,6 +49,8 @@ when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 { } else { GLSL_VERSION :: "330" } +RESET_BUTTON_SIZE :: Vec2{CARD_SIZE.x, CARD_SIZE.x / 4} +UNDO_BUTTON_SIZE :: Vec2{CARD_SIZE.x, CARD_SIZE.x / 3} game: Game Game :: struct { @@ -57,10 +61,13 @@ Game :: struct { static_arena_data: [128 * mem.Kilobyte]byte, // Allocations here will never be freed static_allocator: runtime.Allocator, + rl_logger: log.Logger, delta: f32, entities: [Card_Handle(TAROT_COUNT)]Card_Entity, state: State, displayed_state: State, + should_redraw: bool, + mouse_position: Vec2, font: ray.Font, card_bottom_font: ray.Font, suit_textures: [Suit]ray.Texture, @@ -160,7 +167,18 @@ init :: proc() { tracy.Zone() game.run = true - ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT, .MSAA_4X_HINT}) + mem.arena_init(&game.static_arena, game.static_arena_data[:]) + game.static_allocator = mem.arena_allocator(&game.static_arena) + + context.logger = log.create_console_logger(LOG_LEVEL, allocator = game.static_allocator) + game.rl_logger = log.create_console_logger( + LOG_LEVEL, + ident = "raylib", + allocator = game.static_allocator, + ) + game.ctx = context + + ray.SetConfigFlags({.WINDOW_RESIZABLE, .VSYNC_HINT, .MSAA_4X_HINT, .WINDOW_MAXIMIZED}) ray.InitWindow(1600, 900, "Fortune's Foundation") // NOTE(lilith): Sorting statically included assets @@ -214,13 +232,13 @@ init :: proc() { ), ) ray.SetTextureFilter(game.font.texture, .BILINEAR) - game.ctx = context } update :: proc() { tracy.Zone() - ray.BeginDrawing(); defer ray.EndDrawing() + if ray.IsKeyPressed(.F11) {ray.ToggleFullscreen()} + ray.BeginDrawing(); defer tracy.FrameMark(); defer ray.EndDrawing() render_size := Vec2{f32(ray.GetRenderWidth()), f32(ray.GetRenderHeight())} zoom_v := render_size / BOARD_SIZE zoom := min(zoom_v.x, zoom_v.y) @@ -234,6 +252,17 @@ update :: proc() { } ray.BeginMode2D(game.camera); defer ray.EndMode2D() + + switch game.phase { + case .Searching: + draw_background() + game_update_searching() + case .Playing: + game_update() + } +} + +draw_background :: proc() { ray.ClearBackground({40, 65, 52, 255}) ray.DrawRectangleV(Vec2{}, Vec2{BOARD_SIZE.x, 10 * BOARD_SIZE.y}, {25, 40, 43, 255}) @@ -250,13 +279,6 @@ update :: proc() { ROUNDED_RECT_SEGMENTS, {40, 35, 50, 255}, ) - - switch game.phase { - case .Searching: - game_update_searching() - case .Playing: - game_update() - } } game_update_searching :: proc() { @@ -313,7 +335,10 @@ game_update :: proc() { free_all(context.temp_allocator) game.delta = f32(ray.GetFrameTime()) delta := game.delta + game.should_redraw = false + if ray.IsKeyPressed(.F11) {game.should_redraw = true} + if ray.IsWindowResized() {game.should_redraw = true} for &entity in game.entities {entity.is_draggable = false} for &column in game.state.columns { @@ -326,6 +351,7 @@ game_update :: proc() { } mouse_position := ray.GetScreenToWorld2D(ray.GetMousePosition(), game.camera) + game.mouse_position = mouse_position game.dragged_card_position = mouse_position game.hovered_draggable = nil @@ -338,6 +364,7 @@ game_update :: proc() { break } } + if game.hovered_draggable != nil {game.should_redraw = true} if ray.IsMouseButtonPressed(.LEFT) && game.hovered_draggable != nil { game.dragged_card = game.hovered_draggable.? @@ -356,6 +383,7 @@ game_update :: proc() { } if dragged_card, ok := game.dragged_card.?; ok { + game.should_redraw = true obj := &game_cards_zipped()[int(dragged_card)] if ray.IsMouseButtonReleased(.LEFT) { old_place := state_card_place(game.state, dragged_card) @@ -400,14 +428,12 @@ game_update :: proc() { } - // NOTE(lilith): Draw slots and set card target positions + // NOTE(lilith): Set card target positions for &entity in game.entities { entity.target_rotation = 0 } for major_pile, major_pile_kind in game.displayed_state.major { place := Place{.Major, i8(major_pile_kind)} - position := card_position(place) - slot_draw(position) for handle, height in major_pile { game.entities[int(handle)].target_position = card_position(place, height) } @@ -420,24 +446,12 @@ game_update :: proc() { for suit in Suit.Chalice ..= Suit.Wand { suit_index := i8(suit) - 1 suit_position := card_position({.Suit, suit_index}) - slot_draw(suit_position) - - icon_size :: Vec2{40, 40} - ray.DrawTexturePro( - game.suit_textures[suit], - source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE), - dest = rrect_from_pos_sz(suit_position - icon_size * 0.5, icon_size), - origin = {0, 0}, - rotation = 0, - tint = {255, 255, 255, 100}, - ) for handle in game.displayed_state.minor[suit_index] { game.entities[int(handle)].target_position = suit_position } } for column, column_index in game.displayed_state.columns { - slot_draw(card_position({.Column, i8(column_index)})) for handle, cc_index in column { position := card_position({.Column, i8(column_index)}, cc_index) game.entities[int(handle)].target_position = position @@ -449,12 +463,19 @@ game_update :: proc() { entity := &obj.entity target_position := obj.entity.state != .Dragged ? entity.target_position : game.dragged_card_position + starting_position := entity.position entity.position, entity.velocity = critical_spring_vec2( entity.position, entity.velocity, target_position, dt = delta, ) + if linalg.distance(entity.position, entity.target_position) < 0.05 { + entity.position = entity.target_position + } + if entity.position != starting_position { + game.should_redraw = true + } target_rotation := obj.entity.state != .Dragged ? entity.target_rotation : game.dragged_card_rotation entity.rotation, entity.angular_velocity = critical_spring_scalar( @@ -463,6 +484,14 @@ game_update :: proc() { target_rotation, dt = delta, ) + + if entity.is_neighbor_highlit || entity.is_hovered { + entity.highlight_amount += game.delta * 6 + } else { + entity.highlight_amount -= game.delta * 3 + } + if 0 <= entity.highlight_amount && entity.highlight_amount <= 1 {game.should_redraw = true} + entity.highlight_amount = clamp(entity.highlight_amount, 0, 1) } // NOTE(lilith): Neighbor card highlighting @@ -486,6 +515,7 @@ game_update :: proc() { } } if handle, ok := game.dragged_card.?; ok {game.hovered_any_card = handle} + if game.hovered_any_card != nil {game.should_redraw = true} if hover_handle, ok := game.hovered_any_card.?; ok { hover_card := card_from_handle(hover_handle) for &entity, handle in game.entities { @@ -501,6 +531,75 @@ game_update :: proc() { } } + // NOTE(lilith): Update suit scrim + if draggin_card_over_aside || len(game.displayed_state.aside) != 0 { + game.suits_overlay_opacity = min(160, game.suits_overlay_opacity + 500 * delta) + } else { + game.suits_overlay_opacity = max(0, game.suits_overlay_opacity - 500 * delta) + } + + { + target_highlight_card_handle := game.dragged_card + if target_highlight_card_handle == + nil {target_highlight_card_handle = game.hovered_draggable} + if handle, ok := target_highlight_card_handle.?; ok { + game.last_highlight_card = handle + if game.highlight_opacity < 1 {game.should_redraw = true} + game.highlight_opacity = min(1, game.highlight_opacity + delta * 2) + } else { + if game.highlight_opacity > 0 {game.should_redraw = true} + game.highlight_opacity = max(0, game.highlight_opacity - delta * 2) + } + } + + { + center_point := Vec2{BOARD_SIZE.x / 2, GAP + RESET_BUTTON_SIZE.y / 2} + reset_rect := rrect_from_pos_sz(center_point - RESET_BUTTON_SIZE / 2, RESET_BUTTON_SIZE) + if rect_contains(reset_rect, game.mouse_position) || + game.reset_button.fill > 0 {game.should_redraw = true} + } + { + center_point := Vec2{BOARD_SIZE.x / 2, GAP + CARD_SIZE.y - UNDO_BUTTON_SIZE.y / 2} + undo_rect := rrect_from_pos_sz(center_point - UNDO_BUTTON_SIZE / 2, UNDO_BUTTON_SIZE) + if rect_contains(undo_rect, game.mouse_position) {game.should_redraw = true} + } + if game.should_redraw { + game_draw() + } else { + ray.WaitTime(1.0 / 20.0) + } +} + +game_draw :: proc() { + draw_background() + + // NOTE(lilith): Draw slots + for major_pile, major_pile_kind in game.displayed_state.major { + place := Place{.Major, i8(major_pile_kind)} + position := card_position(place) + slot_draw(position) + } + + for suit in Suit.Chalice ..= Suit.Wand { + suit_index := i8(suit) - 1 + suit_position := card_position({.Suit, suit_index}) + slot_draw(suit_position) + + icon_size :: Vec2{40, 40} + ray.DrawTexturePro( + game.suit_textures[suit], + source = rrect_from_pos_sz({0, 0}, SUIT_TEX_SIZE), + dest = rrect_from_pos_sz(suit_position - icon_size * 0.5, icon_size), + origin = {0, 0}, + rotation = 0, + tint = {255, 255, 255, 100}, + ) + } + + for column, column_index in game.displayed_state.columns { + slot_draw(card_position({.Column, i8(column_index)})) + } + // NOTE(lilith): Draw cards for column, column_index in game.displayed_state.columns { for handle, cc_index in column { @@ -519,12 +618,6 @@ game_update :: proc() { } } - - if draggin_card_over_aside || len(game.displayed_state.aside) != 0 { - game.suits_overlay_opacity = min(160, game.suits_overlay_opacity + 500 * delta) - } else { - game.suits_overlay_opacity = max(0, game.suits_overlay_opacity - 500 * delta) - } // NOTE(lilith): Scrim over the minor foundations ray.DrawRectangleRounded( rrect_from_pos_sz( @@ -540,36 +633,25 @@ game_update :: proc() { card_draw(game.displayed_state.aside[0]) } - { - target_highlight_card_handle := game.dragged_card - if target_highlight_card_handle == - nil {target_highlight_card_handle = game.hovered_draggable} - if handle, ok := target_highlight_card_handle.?; ok { - game.last_highlight_card = handle - game.highlight_opacity = min(1, game.highlight_opacity + delta * 2) - } else { - game.highlight_opacity = max(0, game.highlight_opacity - delta * 2) + // NOTE(lilith): Draw highlights for placable positions + if handle, ok := game.last_highlight_card.?; ok { + place := state_card_place(game.state, handle) + for column, column_index in game.state.columns { + to := Place{.Column, i8(column_index)} + move := Move { + from = place, + to = to, + card = handle, + } + if !state_is_move_valid(game.state, move) {continue} + highlight_draw(card_position(to, len(column))) } - if handle, ok := game.last_highlight_card.?; ok { - place := state_card_place(game.state, handle) - for column, column_index in game.state.columns { - to := Place{.Column, i8(column_index)} - move := Move { - from = place, - to = to, - card = handle, - } - if !state_is_move_valid(game.state, move) {continue} - highlight_draw(card_position(to, len(column))) - } - if len(game.state.aside) == 0 { - highlight_draw(card_position({.Aside, 0}), rotated = true) - } + if len(game.state.aside) == 0 { + highlight_draw(card_position({.Aside, 0}), rotated = true) } } { // NOTE(lilith): Draw reset button - RESET_BUTTON_SIZE :: Vec2{CARD_SIZE.x, CARD_SIZE.x / 4} center_point := Vec2{BOARD_SIZE.x / 2, GAP + RESET_BUTTON_SIZE.y / 2} rect := rrect_from_pos_sz(center_point - RESET_BUTTON_SIZE / 2, RESET_BUTTON_SIZE) ray.DrawRectangleRounded( @@ -585,7 +667,7 @@ game_update :: proc() { border_color({.Wand, 1}), ) outline_color := border_color(CARDS[0]) - outline_color.a = rect_contains(rect, mouse_position) ? 255 : 170 + outline_color.a = rect_contains(rect, game.mouse_position) ? 255 : 170 ray.DrawRectangleRoundedLines(rect, CARD_ROUNDNESS, ROUNDED_RECT_SEGMENTS, outline_color) RESET_TEXT :: "Reset" RESET_FONT_SIZE :: 22 @@ -601,7 +683,7 @@ game_update :: proc() { ) } if ray.IsMouseButtonDown(.LEFT) && - rect_contains(rect, mouse_position) && + rect_contains(rect, game.mouse_position) && !game.reset_button.activated { rate := 1 - game.reset_button.fill game.reset_button.fill += game.delta * (rate * rate * 1.6 + 0.25) @@ -624,11 +706,10 @@ game_update :: proc() { } { // NOTE(lilith): Draw undo - UNDO_BUTTON_SIZE :: Vec2{CARD_SIZE.x, CARD_SIZE.x / 3} center_point := Vec2{BOARD_SIZE.x / 2, GAP + CARD_SIZE.y - UNDO_BUTTON_SIZE.y / 2} rect := rrect_from_pos_sz(center_point - UNDO_BUTTON_SIZE / 2, UNDO_BUTTON_SIZE) outline_color := border_color(CARDS[0]) - outline_color.a = rect_contains(rect, mouse_position) ? 255 : 220 + outline_color.a = rect_contains(rect, game.mouse_position) ? 255 : 220 if game.undo_state == nil {outline_color.a = 70} ray.DrawRectangleRoundedLines(rect, CARD_ROUNDNESS, ROUNDED_RECT_SEGMENTS, outline_color) UNDO_TEXT :: "Undo" @@ -645,7 +726,7 @@ game_update :: proc() { ) } if ray.IsMouseButtonPressed(.LEFT) && - rect_contains(rect, mouse_position) && + rect_contains(rect, game.mouse_position) && game.undo_state != nil { queue.clear(&game.auto_moves) game.state = game.undo_state.? @@ -654,6 +735,7 @@ game_update :: proc() { } } + // NOTE(lilith): Draw dragged card over everything if handle, ok := game.dragged_card.?; ok { card_draw(handle) } @@ -704,12 +786,6 @@ card_draw :: proc(handle: Card_Handle) { entity := &game.entities[handle] card := card_from_handle(handle) - if entity.is_neighbor_highlit || entity.is_hovered { - entity.highlight_amount += game.delta * 6 - } else { - entity.highlight_amount -= game.delta * 3 - } - entity.highlight_amount = clamp(entity.highlight_amount, 0, 1) highlight_t := math.smoothstep(f32(0), 1, entity.highlight_amount) rlgl.PushMatrix(); defer rlgl.PopMatrix() @@ -1726,39 +1802,3 @@ rect_contains :: proc(rect: ray.Rectangle, p: Vec2) -> bool { p.y <= rect.y + rect.height \ ) } -// raylib_set_os_api :: proc() { -// // Forward raylib's logging to Odin's logging. -// ray.SetTraceLogLevel(.INFO) -// ray.SetTraceLogCallback(raylib_log_proc) -// raylib_log_proc :: proc "c" (rl_level: ray.TraceLogLevel, text: cstring, args: ^libc.va_list) { -// context = game.ctx -// level: log.Level -// switch rl_level { -// case .NONE: -// level = .Fatal -// case .FATAL: -// level = .Fatal -// case .ERROR: -// level = .Error -// case .WARNING: -// level = .Warning -// case .INFO: -// level = .Info -// case .DEBUG: -// level = .Debug -// case .TRACE: -// level = .Debug -// case .ALL: -// level = .Debug -// } -// BUFFER_SIZE :: 1024 -// @(static) buffer: [BUFFER_SIZE]u8 -// result := libc.vsnprintf(raw_data(buffer[:]), BUFFER_SIZE, text, args) -// if result < 0 { -// log.warn("Failed to format a message from raylib. Format string was: \"%s\"", text) -// return -// } -// context.logger = game.rl_logger -// log.logf(level, "%s", buffer[:result], location = {}) -// } -// }