macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Why is the rectangle half the size of the window?

Open historydev opened this issue 10 months ago • 1 comments

System: win11

screen_dpi_scale() // returns 1.0 If multiply screen size on 2.0 all works like need, but why?

use macroquad::prelude::{
    clear_background, draw_rectangle, draw_rectangle_lines, next_frame, screen_height,
    screen_width, set_camera, vec2, Camera2D, Vec2, BLACK, BLUE,
};

#[macroquad::main("Playground")]
async fn main() {

    let mut camera = Camera2D {
        target: Vec2::ZERO,
        ..Default::default()
    };

    let mut zoom = 1.0;

    loop {
        clear_background(BLACK);

        let (w, h) = (screen_width(), screen_height());  // multiply on 2.0 - all ok

        camera.zoom = vec2(zoom / screen_width(), zoom / screen_height());

        set_camera(&camera);

        draw_rectangle_lines(-w / 2., -h / 2., w, h, 5., BLUE);

        next_frame().await;
    }
}

Image

historydev avatar Feb 01 '25 22:02 historydev

Macro-quads uses OpenGL for rendering - which is why this might be unexpected behavior. OpenGL uses something called "normalized coordinates" which ranges them from -1 to 1. So the left part of the screen is -1 and the right part is 1. This makes it easy to express the center of 0. Ths distance between -1 and 1 is what is scaled by your "zoom" factor - which is why you see exactly double the size of the screen.

Internally macroquad seems to use GLAM which uses the established glOrtho. My matrix math is a bit rusty (pun very much intended) but I believe the 2 in the formula you see is the cause of your confusion.

TL;DR: Because OpenGL used to do it like this. :)

FredTheDino avatar Feb 02 '25 09:02 FredTheDino