highdpi testbed
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [core] example - highdpi testbed
//!
//! Zig-API translation of examples/core/core_highdpi_testbed.c (raylib 6.2).
//! Example contributed by Ramon Santamaria (@raysan5) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 Ramon Santamaria (@raysan5)
const rl = @import("raylib");
pub fn main() void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.setConfigFlags(.{ .window_resizable = true, .window_highdpi = true });
rl.initWindow(screen_width, screen_height, "raylib [core] example - highdpi testbed");
defer rl.closeWindow(); // Close window and OpenGL context
const grid_spacing = 40; // Grid spacing in pixels
rl.setTargetFPS(60);
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
const mouse_pos = rl.getMousePosition();
const current_monitor = rl.getCurrentMonitor();
const scale_dpi = rl.getWindowScaleDPI();
const window_pos = rl.getWindowPosition();
if (rl.isKeyPressed(.space)) rl.toggleBorderlessWindowed();
if (rl.isKeyPressed(.f)) rl.toggleFullscreen();
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
// Draw grid
const rows: usize = @intCast(@divTrunc(rl.getScreenHeight(), grid_spacing) + 1);
for (0..rows) |h| {
const y: i32 = @intCast(h * grid_spacing);
rl.drawText(rl.textFormat("{d:0>2}", .{h * grid_spacing}), 4, y - 4, 10, .gray);
rl.drawLine(24, y, rl.getScreenWidth(), y, .light_gray);
}
const columns: usize = @intCast(@divTrunc(rl.getScreenWidth(), grid_spacing) + 1);
for (0..columns) |v| {
const x: i32 = @intCast(v * grid_spacing);
rl.drawText(rl.textFormat("{d:0>2}", .{v * grid_spacing}), x - 10, 4, 10, .gray);
rl.drawLine(x, 20, x, rl.getScreenHeight(), .light_gray);
}
// Draw UI info
rl.drawText(rl.textFormat("CURRENT MONITOR: {d}/{d} ({d}x{d})", .{
current_monitor + 1,
rl.getMonitorCount(),
rl.getMonitorWidth(current_monitor),
rl.getMonitorHeight(current_monitor),
}), 50, 50, 20, .dark_gray);
rl.drawText(rl.textFormat("WINDOW POSITION: {d}x{d}", .{
@as(i32, @intFromFloat(window_pos.x)),
@as(i32, @intFromFloat(window_pos.y)),
}), 50, 90, 20, .dark_gray);
rl.drawText(rl.textFormat("SCREEN SIZE: {d}x{d}", .{ rl.getScreenWidth(), rl.getScreenHeight() }), 50, 130, 20, .dark_gray);
rl.drawText(rl.textFormat("RENDER SIZE: {d}x{d}", .{ rl.getRenderWidth(), rl.getRenderHeight() }), 50, 170, 20, .dark_gray);
rl.drawText(rl.textFormat("SCALE FACTOR: {d:.2}x{d:.2}", .{ scale_dpi.x, scale_dpi.y }), 50, 210, 20, .gray);
// Draw reference rectangles, top-left and bottom-right corners
rl.drawRectangle(0, 0, 30, 60, .red);
rl.drawRectangle(rl.getScreenWidth() - 30, rl.getScreenHeight() - 60, 30, 60, .blue);
// Draw mouse position
rl.drawCircleV(rl.getMousePosition(), 20, .maroon);
rl.drawRectangleRec(.{ .x = mouse_pos.x - 25, .y = mouse_pos.y, .width = 50, .height = 2 }, .black);
rl.drawRectangleRec(.{ .x = mouse_pos.x, .y = mouse_pos.y - 25, .width = 2, .height = 50 }, .black);
const mouse_x: i32 = @intFromFloat(mouse_pos.x);
const mouse_y: i32 = @intFromFloat(mouse_pos.y);
const near_bottom = mouse_pos.y > @as(f32, @floatFromInt(rl.getScreenHeight() - 60));
rl.drawText(
rl.textFormat("[{d},{d}]", .{ rl.getMouseX(), rl.getMouseY() }),
mouse_x - 44,
if (near_bottom) mouse_y - 46 else mouse_y + 30,
20,
.black,
);
}
}/*******************************************************************************************
*
* raylib [core] example - highdpi testbed
*
* Example complexity rating: [★☆☆☆] 1/4
*
* Example originally created with raylib 6.0, last time updated with raylib 6.0
*
* Example contributed by Ramon Santamaria (@raysan5) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI);
InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi testbed");
Vector2 scaleDpi = GetWindowScaleDPI();
Vector2 mousePos = GetMousePosition();
int currentMonitor = GetCurrentMonitor();
Vector2 windowPos = GetWindowPosition();
int gridSpacing = 40; // Grid spacing in pixels
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
mousePos = GetMousePosition();
currentMonitor = GetCurrentMonitor();
scaleDpi = GetWindowScaleDPI();
windowPos = GetWindowPosition();
if (IsKeyPressed(KEY_SPACE)) ToggleBorderlessWindowed();
if (IsKeyPressed(KEY_F)) ToggleFullscreen();
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw grid
for (int h = 0; h < GetScreenHeight()/gridSpacing + 1; h++)
{
DrawText(TextFormat("%02i", h*gridSpacing), 4, h*gridSpacing - 4, 10, GRAY);
DrawLine(24, h*gridSpacing, GetScreenWidth(), h*gridSpacing, LIGHTGRAY);
}
for (int v = 0; v < GetScreenWidth()/gridSpacing + 1; v++)
{
DrawText(TextFormat("%02i", v*gridSpacing), v*gridSpacing - 10, 4, 10, GRAY);
DrawLine(v*gridSpacing, 20, v*gridSpacing, GetScreenHeight(), LIGHTGRAY);
}
// Draw UI info
DrawText(TextFormat("CURRENT MONITOR: %i/%i (%ix%i)", currentMonitor + 1, GetMonitorCount(),
GetMonitorWidth(currentMonitor), GetMonitorHeight(currentMonitor)), 50, 50, 20, DARKGRAY);
DrawText(TextFormat("WINDOW POSITION: %ix%i", (int)windowPos.x, (int)windowPos.y), 50, 90, 20, DARKGRAY);
DrawText(TextFormat("SCREEN SIZE: %ix%i", GetScreenWidth(), GetScreenHeight()), 50, 130, 20, DARKGRAY);
DrawText(TextFormat("RENDER SIZE: %ix%i", GetRenderWidth(), GetRenderHeight()), 50, 170, 20, DARKGRAY);
DrawText(TextFormat("SCALE FACTOR: %.2fx%.2f", scaleDpi.x, scaleDpi.y), 50, 210, 20, GRAY);
// Draw reference rectangles, top-left and bottom-right corners
DrawRectangle(0, 0, 30, 60, RED);
DrawRectangle(GetScreenWidth() - 30, GetScreenHeight() - 60, 30, 60, BLUE);
// Draw mouse position
DrawCircleV(GetMousePosition(), 20, MAROON);
DrawRectangleRec((Rectangle) { mousePos.x - 25, mousePos.y, 50, 2 }, BLACK);
DrawRectangleRec((Rectangle) { mousePos.x, mousePos.y - 25, 2, 50 }, BLACK);
DrawText(TextFormat("[%i,%i]", GetMouseX(), GetMouseY()), (int)mousePos.x - 44,
(mousePos.y > GetScreenHeight() - 60)? (int)mousePos.y - 46 : (int)mousePos.y + 30, 20, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.