From ebabdb02fd944c3182dc4f9d0a56d2abaa5b61f1 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 12 Aug 2022 10:31:25 +0200 Subject: [PATCH] Update for current hare-sdl2 (02e827d 2022-04-26) --- board.ha | 18 ++++---- config.ha | 4 +- draw.ha | 14 +++--- main.ha | 136 +++++++++++++++++++++++++++--------------------------- update.ha | 38 +++++++-------- 5 files changed, 106 insertions(+), 104 deletions(-) diff --git a/board.ha b/board.ha index 3291d2b..2062685 100644 --- a/board.ha +++ b/board.ha @@ -56,9 +56,9 @@ fn draw_board(state: *state, board: *board) (void | sdl2::error) = { continue; }; let color = &color_map[cell]; - sdl2::set_texture_alpha_mod(tex.tex, 255)?; - sdl2::set_texture_color_mod(tex.tex, color.0, color.1, color.2)?; - sdl2::render_copy(state.render, tex.tex, null, &sdl2::rect { + sdl2::SDL_SetTextureAlphaMod(tex.tex, 255)?; + sdl2::SDL_SetTextureColorMod(tex.tex, color.0, color.1, color.2)?; + sdl2::SDL_RenderCopy(state.render, tex.tex, null, &sdl2::SDL_Rect { x = offs.0 + x * BOARD_SCALE * tex.width, y = offs.1 + y * BOARD_SCALE * tex.height, w = tex.width * BOARD_SCALE, @@ -74,7 +74,7 @@ fn draw_board(state: *state, board: *board) (void | sdl2::error) = { draw_active(state, board.ghost_x, board.ghost_y, 128)?; - const now = sdl2::get_ticks(); + const now = sdl2::SDL_GetTicks(); switch (state.state) { case gamestate::INTRO => // XXX: This is hacky as fuck. @@ -89,8 +89,8 @@ fn draw_board(state: *state, board: *board) (void | sdl2::error) = { if (now >= state.fadein_start) { alpha = (workaround * 255.0): int: u8; }; - sdl2::set_render_draw_color(state.render, 0, 0, 0, alpha)?; - sdl2::render_fill_rect(state.render, null)?; + sdl2::SDL_SetRenderDrawColor(state.render, 0, 0, 0, alpha)?; + sdl2::SDL_RenderFillRect(state.render, null)?; case => void; }; @@ -116,9 +116,9 @@ fn draw_active( continue; }; let color = &color_map[cell]; - sdl2::set_texture_color_mod(tex.tex, color.0, color.1, color.2)?; - sdl2::set_texture_alpha_mod(tex.tex, alpha)?; - sdl2::render_copy(state.render, tex.tex, null, &sdl2::rect { + sdl2::SDL_SetTextureColorMod(tex.tex, color.0, color.1, color.2)?; + sdl2::SDL_SetTextureAlphaMod(tex.tex, alpha)?; + sdl2::SDL_RenderCopy(state.render, tex.tex, null, &sdl2::SDL_Rect { x = offs.0 + x * BOARD_SCALE * tex.width, y = offs.1 + y * BOARD_SCALE * tex.height, w = tex.width * BOARD_SCALE, diff --git a/config.ha b/config.ha index 0031ebc..f4a6e2e 100644 --- a/config.ha +++ b/config.ha @@ -5,7 +5,7 @@ use os; use sdl2; fn config( - keybindings: *[](sdl2::keycode, action), + keybindings: *[](sdl2::SDL_Keycode, action), cfg: io::handle, ) (void | ini::error | sdl2::error) = { let sc = ini::scan(cfg); @@ -32,7 +32,7 @@ fn config( case => continue; }; - let keycode = sdl2::get_key_from_name(e.2)?; + let keycode = sdl2::SDL_GetKeyFromName(e.2)?; for (let i = 0z; i < len(keybindings); i += 1) { if (keybindings[i].0 == keycode) { delete(keybindings[i]); diff --git a/draw.ha b/draw.ha index 090c36d..619fd59 100644 --- a/draw.ha +++ b/draw.ha @@ -1,9 +1,9 @@ use sdl2; fn draw(state: *state) (void | sdl2::error) = { - sdl2::set_render_draw_color(state.render, 0, 0, 0, 255)?; - sdl2::render_clear(state.render)?; - sdl2::render_copy(state.render, state.scene.tex, null, &sdl2::rect{ + sdl2::SDL_SetRenderDrawColor(state.render, 0, 0, 0, 255)?; + sdl2::SDL_RenderClear(state.render)?; + sdl2::SDL_RenderCopy(state.render, state.scene.tex, null, &sdl2::SDL_Rect{ x = 0, y = 0, w = 640, @@ -18,7 +18,7 @@ fn draw(state: *state) (void | sdl2::error) = { case => void; // No special drawing required }; - sdl2::render_present(state.render); + sdl2::SDL_RenderPresent(state.render); }; fn draw_clear(state: *state) (void | sdl2::error) = { @@ -31,7 +31,7 @@ fn draw_clear(state: *state) (void | sdl2::error) = { }; fn draw_clearline(state: *state, y: int) (void | sdl2::error) = { - const inrect = &sdl2::rect { + const inrect = &sdl2::SDL_Rect { x = state.clearframe * 4, y = 0, w = 4, @@ -41,12 +41,12 @@ fn draw_clearline(state: *state, y: int) (void | sdl2::error) = { const board = &state.board; const offs = (board.offs_x * SCENE_SCALE, board.offs_y * SCENE_SCALE); for (let x = 0; x < BOARD_WIDTH; x += 1) { - const outrect = &sdl2::rect { + const outrect = &sdl2::SDL_Rect { x = offs.0 + x * BOARD_SCALE * TETROMINO_WIDTH, y = offs.1 + y * BOARD_SCALE * TETROMINO_HEIGHT, w = TETROMINO_WIDTH * BOARD_SCALE, h = TETROMINO_HEIGHT * BOARD_SCALE, }; - sdl2::render_copy(state.render, tex.tex, inrect, outrect)?; + sdl2::SDL_RenderCopy(state.render, tex.tex, inrect, outrect)?; }; }; diff --git a/main.ha b/main.ha index 2f4e430..77416fe 100644 --- a/main.ha +++ b/main.ha @@ -8,13 +8,13 @@ use math::random; use os; use sdl2::image; use sdl2::mixer; -use sdl2::{renderer_flags, window_flags}; +use sdl2::{SDL_RendererFlags, SDL_WindowFlags}; use sdl2; use time; use types; type texture = struct { - tex: *sdl2::texture, + tex: *sdl2::SDL_Texture, width: int, height: int, }; @@ -38,14 +38,14 @@ type action = enum { type state = struct { run: bool, - window: *sdl2::window, - render: *sdl2::renderer, + window: *sdl2::SDL_Window, + render: *sdl2::SDL_Renderer, piece: texture, scene: texture, clear: texture, - music: *mixer::chunk, + music: *mixer::Mix_Chunk, input: input, board: board, @@ -63,7 +63,9 @@ type state = struct { fadein_start: u32, fadein_finish: u32, - keybindings: [](sdl2::keycode, action), + score: int, + + keybindings: [](sdl2::SDL_Keycode, action), }; export fn main() void = { @@ -79,25 +81,25 @@ export fn main() void = { }; fn run() (void | fs::error | sdl2::error | ini::error) = { - let keybindings: [](sdl2::keycode, action) = alloc([ - (sdl2::keycode::UP, action::ROTLEFT), - (sdl2::keycode::x, action::ROTLEFT), - (sdl2::keycode::SPACE, action::COMMIT), - (sdl2::keycode::LCTRL, action::ROTRIGHT), - (sdl2::keycode::RCTRL, action::ROTRIGHT), - (sdl2::keycode::z, action::ROTRIGHT), - (sdl2::keycode::LEFT, action::LEFT), - (sdl2::keycode::RIGHT, action::RIGHT), - (sdl2::keycode::DOWN, action::DOWN), - (sdl2::keycode::KP_8, action::COMMIT), - (sdl2::keycode::KP_4, action::LEFT), - (sdl2::keycode::KP_6, action::RIGHT), - (sdl2::keycode::KP_2, action::COMMIT), - (sdl2::keycode::KP_1, action::ROTLEFT), - (sdl2::keycode::KP_5, action::ROTLEFT), - (sdl2::keycode::KP_9, action::ROTLEFT), - (sdl2::keycode::KP_3, action::ROTRIGHT), - (sdl2::keycode::KP_7, action::ROTRIGHT), + let keybindings: [](sdl2::SDL_Keycode, action) = alloc([ + (sdl2::SDL_Keycode::UP, action::ROTLEFT), + (sdl2::SDL_Keycode::x, action::ROTLEFT), + (sdl2::SDL_Keycode::SPACE, action::COMMIT), + (sdl2::SDL_Keycode::LCTRL, action::ROTRIGHT), + (sdl2::SDL_Keycode::RCTRL, action::ROTRIGHT), + (sdl2::SDL_Keycode::z, action::ROTRIGHT), + (sdl2::SDL_Keycode::LEFT, action::LEFT), + (sdl2::SDL_Keycode::RIGHT, action::RIGHT), + (sdl2::SDL_Keycode::DOWN, action::DOWN), + (sdl2::SDL_Keycode::KP_8, action::COMMIT), + (sdl2::SDL_Keycode::KP_4, action::LEFT), + (sdl2::SDL_Keycode::KP_6, action::RIGHT), + (sdl2::SDL_Keycode::KP_2, action::COMMIT), + (sdl2::SDL_Keycode::KP_1, action::ROTLEFT), + (sdl2::SDL_Keycode::KP_5, action::ROTLEFT), + (sdl2::SDL_Keycode::KP_9, action::ROTLEFT), + (sdl2::SDL_Keycode::KP_3, action::ROTRIGHT), + (sdl2::SDL_Keycode::KP_7, action::ROTRIGHT), ]); defer free(keybindings); const cfg = dirs::configfs("tetrominoes"); @@ -108,29 +110,29 @@ fn run() (void | fs::error | sdl2::error | ini::error) = { case fs::error => void; }; - sdl2::init(sdl2::init_flags::VIDEO - | sdl2::init_flags::AUDIO - | sdl2::init_flags::GAMECONTROLLER)?; - defer sdl2::quit(); - image::init(image::init_flags::PNG | image::init_flags::JPG)?; - defer image::quit(); - mixer::init(mixer::init_flags::FLAC - | mixer::init_flags::MP3 - | mixer::init_flags::OGG - | mixer::init_flags::OPUS)?; // That should be plenty - defer mixer::quit(); - mixer::open_audio(mixer::DEFAULT_FREQUENCY, - mixer::DEFAULT_FORMAT, mixer::DEFAULT_CHANNELS, 1024)?; - defer mixer::close_audio(); - - const win = sdl2::create_window("Tetrominoes", - sdl2::WINDOWPOS_UNDEFINED, sdl2::WINDOWPOS_UNDEFINED, - 640, 480, window_flags::NONE)?; - defer sdl2::destroy_window(win); - - const render = sdl2::create_renderer(win, -1, - renderer_flags::ACCELERATED)?; - defer sdl2::destroy_renderer(render); + sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO + | sdl2::SDL_INIT_AUDIO + | sdl2::SDL_INIT_GAMECONTROLLER)?; + defer sdl2::SDL_Quit(); + image::IMG_Init(image::IMG_InitFlags::PNG | image::IMG_InitFlags::JPG)?; + defer image::IMG_Quit(); + mixer::Mix_Init(mixer::MIX_InitFlags::FLAC + | mixer::MIX_InitFlags::MP3 + | mixer::MIX_InitFlags::OGG + | mixer::MIX_InitFlags::OPUS)?; // That should be plenty + defer mixer::Mix_Quit(); + mixer::Mix_OpenAudio(mixer::MIX_DEFAULT_FREQUENCY, + mixer::MIX_DEFAULT_FORMAT, mixer::MIX_DEFAULT_CHANNELS, 1024)?; + defer mixer::Mix_CloseAudio(); + + const win = sdl2::SDL_CreateWindow("Tetrominoes", + sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, + 640, 480, SDL_WindowFlags::NONE)?; + defer sdl2::SDL_DestroyWindow(win); + + const render = sdl2::SDL_CreateRenderer(win, -1, + SDL_RendererFlags::ACCELERATED)?; + defer sdl2::SDL_DestroyRenderer(render); const seed = time::now(time::clock::MONOTONIC).sec: u64; let state = state { @@ -151,23 +153,23 @@ fn run() (void | fs::error | sdl2::error | ini::error) = { keybindings = keybindings, ... }; - defer sdl2::destroy_texture(state.piece.tex); - defer sdl2::destroy_texture(state.scene.tex); - defer sdl2::destroy_texture(state.clear.tex); - defer mixer::free_chunk(state.music); + defer sdl2::SDL_DestroyTexture(state.piece.tex); + defer sdl2::SDL_DestroyTexture(state.scene.tex); + defer sdl2::SDL_DestroyTexture(state.clear.tex); + defer mixer::Mix_FreeChunk(state.music); - mixer::play_channel(0, state.music, 0)?; + mixer::Mix_PlayChannelTimed(0, state.music, 0)?; - sdl2::set_texture_blend_mode(state.piece.tex, sdl2::blend_mode::BLEND)?; - sdl2::set_render_draw_blend_mode(state.render, sdl2::blend_mode::BLEND)?; + sdl2::SDL_SetTextureBlendMode(state.piece.tex, sdl2::SDL_BlendMode::BLEND)?; + sdl2::SDL_SetRenderDrawBlendMode(state.render, sdl2::SDL_BlendMode::BLEND)?; - let controller: nullable *sdl2::gamecontroller = null; - for (let i = 0; i < sdl2::numjoysticks()?; i += 1) { - if (!sdl2::is_game_controller(i)) { + let controller: nullable *sdl2::SDL_GameController = null; + for (let i = 0; i < sdl2::SDL_NumJoysticks()?; i += 1) { + if (!sdl2::SDL_IsGameController(i)) { continue; }; - match (sdl2::game_controller_open(i)) { - case let c: *sdl2::gamecontroller => + match (sdl2::SDL_GameControllerOpen(i)) { + case let c: *sdl2::SDL_GameController => controller = c; break; case sdl2::error => void; @@ -175,11 +177,11 @@ fn run() (void | fs::error | sdl2::error | ini::error) = { }; defer match (controller) { case null => void; - case let c: *sdl2::gamecontroller => - sdl2::game_controller_close(c); + case let c: *sdl2::SDL_GameController => + sdl2::SDL_GameControllerClose(c); }; - const now = sdl2::get_ticks(); + const now = sdl2::SDL_GetTicks(); shuffle_tetrominoes(&state); do_spawn(&state, now); @@ -192,14 +194,14 @@ fn run() (void | fs::error | sdl2::error | ini::error) = { for (state.run) { update(&state)?; draw(&state)?; - sdl2::delay(1000 / 60); + sdl2::SDL_Delay(1000 / 60); }; }; -fn load_texture(render: *sdl2::renderer, path: str) (texture | sdl2::error) = { - const tex = image::load_texture(render, path)?; +fn load_texture(render: *sdl2::SDL_Renderer, path: str) (texture | sdl2::error) = { + const tex = image::IMG_LoadTexture(render, path)?; let width = 0, height = 0; - sdl2::query_texture(tex, null, null, &width, &height)?; + sdl2::SDL_QueryTexture(tex, null, null, &width, &height)?; return texture { tex = tex, width = width, diff --git a/update.ha b/update.ha index 36623b1..6040068 100644 --- a/update.ha +++ b/update.ha @@ -1,9 +1,9 @@ use math; use math::random; -use sdl2::{event_type, controller_axis, controller_button}; +use sdl2::{SDL_EventType, SDL_GameControllerAxis, SDL_GameControllerButton}; use sdl2; -def THRESHOLD: i16 = sdl2::JOYSTICK_AXIS_MAX / 2; +def THRESHOLD: i16 = sdl2::SDL_JOYSTICK_AXIS_MAX / 2; def TETROMINO_WIDTH: int = 4; def TETROMINO_HEIGHT: int = 4; def TETROMINO_LEN: size = 4 * 4; @@ -13,19 +13,19 @@ type input = struct { }; fn update(state: *state) (void | sdl2::error) = { - const now = sdl2::get_ticks(); + const now = sdl2::SDL_GetTicks(); const input = &state.input; const idle = math::absi16(input.axis_x): i16 < THRESHOLD; let ev = sdl2::event { ... }; - for (sdl2::poll_event(&ev)? == 1) switch (ev.event_type) { - case event_type::QUIT => + for (sdl2::SDL_PollEvent(&ev)? == 1) switch (ev.event_type) { + case SDL_EventType::QUIT => state.run = false; break; - case event_type::CONTROLLERAXISMOTION => + case SDL_EventType::CONTROLLERAXISMOTION => // TODO: This is buggy as fuck - if (ev.caxis.axis == controller_axis::LEFTX - || ev.caxis.axis == controller_axis::RIGHTX) { + if (ev.caxis.axis == SDL_GameControllerAxis::LEFTX + || ev.caxis.axis == SDL_GameControllerAxis::RIGHTX) { if (idle && ev.caxis.value < -THRESHOLD) { move_left(state); }; @@ -34,33 +34,33 @@ fn update(state: *state) (void | sdl2::error) = { }; input.axis_x = ev.caxis.value; }; - case event_type::CONTROLLERBUTTONDOWN => + case SDL_EventType::CONTROLLERBUTTONDOWN => switch (ev.cbutton.button) { - case controller_button::DPAD_LEFT => + case SDL_GameControllerButton::DPAD_LEFT => move_left(state); - case controller_button::DPAD_RIGHT => + case SDL_GameControllerButton::DPAD_RIGHT => move_right(state); - case controller_button::A => + case SDL_GameControllerButton::A => rotate(state, 1); - case controller_button::B => + case SDL_GameControllerButton::B => rotate(state, -1); - case controller_button::DPAD_UP => + case SDL_GameControllerButton::DPAD_UP => if (state.state != gamestate::INTRO) { state.board.active_y = state.board.ghost_y; commit(state); state.next = now; }; - case controller_button::DPAD_DOWN => + case SDL_GameControllerButton::DPAD_DOWN => state.faster = true; case => void; }; - case event_type::CONTROLLERBUTTONUP => + case SDL_EventType::CONTROLLERBUTTONUP => switch (ev.cbutton.button) { - case controller_button::DPAD_DOWN => + case SDL_GameControllerButton::DPAD_DOWN => state.faster = false; case => void; }; - case event_type::KEYDOWN => + case SDL_EventType::KEYDOWN => for (let i = 0z; i < len(state.keybindings); i += 1) { if (state.keybindings[i].0 == ev.key.keysym.sym) { switch (state.keybindings[i].1) { @@ -84,7 +84,7 @@ fn update(state: *state) (void | sdl2::error) = { }; }; }; - case event_type::KEYUP => + case SDL_EventType::KEYUP => for (let i = 0z; i < len(state.keybindings); i += 1) { if (state.keybindings[i].0 == ev.key.keysym.sym) { if (state.keybindings[i].1 == action::DOWN) { -- 2.45.2