Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why is the rectangle half the size of the window? #892

Open
historydev opened this issue Feb 1, 2025 · 1 comment
Open

Why is the rectangle half the size of the window? #892

historydev opened this issue Feb 1, 2025 · 1 comment

Comments

@historydev
Copy link

historydev commented Feb 1, 2025

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 historydev changed the title Why is the rectangle half the size of the window when using screen_width() and screen_height()? Why is the rectangle half the size of the window when using screen_width() and screen_height() or similar size in scalar values? Feb 1, 2025
@historydev historydev changed the title Why is the rectangle half the size of the window when using screen_width() and screen_height() or similar size in scalar values? Why is the rectangle half the size of the window? Feb 1, 2025
@FredTheDino
Copy link

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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants