rustraight 0.5.2

A simple 2D game library for Rust, inspired by DXLib
// 初期化 / ウィンドウ管理系 API の動作確認用サンプル。アセット不要。
//
// 確認対象:
//   init(WindowConfig{..})   advance_frame   delta_time   elapsed_time
//   set_window_position/window_position   set_window_size/window_size
//   set_screen_size/screen_size   show_cursor   MAIN_SCREEN
//
// WindowConfig の transparent/topmost/decorations/resizable/vsync は
// 初期化時にしか切り替えられないため、下の定数を書き換えて再実行して確認する。

use rustraight::prelude::*;

const TRANSPARENT: bool = false;
const TOPMOST: bool = false;
const DECORATIONS: bool = true;
const RESIZABLE: bool = true;
const VSYNC: bool = true;

fn main() {
    init(WindowConfig {
        title: String::from("01_window"),
        width: 800,
        height: 600,
        screen_width: 400,
        screen_height: 300,
        transparent: TRANSPARENT,
        topmost: TOPMOST,
        decorations: DECORATIONS,
        resizable: RESIZABLE,
        vsync: VSYNC,
        ..Default::default()
    });

    log_info!(
        "初期化完了: transparent={TRANSPARENT} topmost={TOPMOST} decorations={DECORATIONS} resizable={RESIZABLE} vsync={VSYNC}"
    );

    let mut cursor_visible = true;

    while advance_frame() {
        draw_fill(MAIN_SCREEN, Color::rgb(20, 20, 30));

        let dt = delta_time();
        let et = elapsed_time();
        let (wx, wy) = window_position();
        let (ww, wh) = window_size();
        let (sw, sh) = screen_size();

        draw_text(MAIN_SCREEN, 10, 10, format!("delta_time: {dt:.4}s"), Color::WHITE);
        draw_text(MAIN_SCREEN, 10, 30, format!("elapsed_time: {et:.2}s"), Color::WHITE);
        draw_text(MAIN_SCREEN, 10, 50, format!("window_position: ({wx}, {wy})"), Color::WHITE);
        draw_text(MAIN_SCREEN, 10, 70, format!("window_size: ({ww}, {wh})"), Color::WHITE);
        draw_text(MAIN_SCREEN, 10, 90, format!("screen_size: ({sw}, {sh})"), Color::WHITE);
        draw_text(
            MAIN_SCREEN, 10, 120,
            "矢印: 移動  W/S: ウィンドウサイズ  A/D: 仮想解像度  C: カーソル切替",
            Color::YELLOW,
        );

        // 矢印キーでウィンドウ位置を移動 → set_window_position / window_position の確認
        let move_speed = 4;
        if is_pressed(KeyCode::ArrowLeft) { set_window_position(wx - move_speed, wy); }
        if is_pressed(KeyCode::ArrowRight) { set_window_position(wx + move_speed, wy); }
        if is_pressed(KeyCode::ArrowUp) { set_window_position(wx, wy - move_speed); }
        if is_pressed(KeyCode::ArrowDown) { set_window_position(wx, wy + move_speed); }

        // W/S でウィンドウサイズ変更 → set_window_size / window_size の確認
        if is_just_pressed(KeyCode::KeyW) { set_window_size(ww + 40, wh + 30); }
        if is_just_pressed(KeyCode::KeyS) { set_window_size((ww - 40).max(160), (wh - 30).max(120)); }

        // A/D で仮想解像度を変更 → set_screen_size / screen_size の確認(描画内容の拡縮で分かる)
        if is_just_pressed(KeyCode::KeyD) { set_screen_size(sw + 40, sh + 30); }
        if is_just_pressed(KeyCode::KeyA) { set_screen_size((sw - 40).max(80), (sh - 30).max(60)); }

        // C でカーソル表示切替 → show_cursor の確認
        if is_just_pressed(KeyCode::KeyC) {
            cursor_visible = !cursor_visible;
            show_cursor(cursor_visible);
        }

        // 仮想スクリーンの端が分かるように枠線を描く(set_screen_size の効果を視覚化)
        draw_rectangle(MAIN_SCREEN, 0, 0, sw, sh, Color::CYAN, false);
    }
}