diff --git a/src/client/main.zig b/src/client/main.zig index db44804..b5e8eee 100644 --- a/src/client/main.zig +++ b/src/client/main.zig @@ -368,6 +368,105 @@ +// const c = @cImport({ +// @cInclude("raylib.h"); +// }); + +// //------------------------------------------------------------------------------------ +// // Program main entry point +// //------------------------------------------------------------------------------------ +// pub fn main() !void { +// // Initialization +// //-------------------------------------------------------------------------------------- +// const screenWidth = 800; +// const screenHeight = 450; + +// c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_WINDOW_HIGHDPI); +// c.InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi testbed"); + +// var scaleDpi = c.GetWindowScaleDPI(); +// var mousePos = c.GetMousePosition(); +// var currentMonitor = c.GetCurrentMonitor(); +// var windowPos = c.GetWindowPosition(); + +// const gridSpacing = 40; // Grid spacing in pixels + +// c.SetTargetFPS(60); +// //-------------------------------------------------------------------------------------- + +// // Main game loop +// while (!c.WindowShouldClose()) // Detect window close button or ESC key +// { +// // Update +// //---------------------------------------------------------------------------------- +// mousePos = c.GetMousePosition(); +// currentMonitor = c.GetCurrentMonitor(); +// scaleDpi = c.GetWindowScaleDPI(); +// windowPos = c.GetWindowPosition(); + +// if (c.IsKeyPressed(c.KEY_SPACE)) c.ToggleBorderlessWindowed(); +// if (c.IsKeyPressed(c.KEY_F)) c.ToggleFullscreen(); +// //---------------------------------------------------------------------------------- + +// // Draw +// //---------------------------------------------------------------------------------- +// c.BeginDrawing(); + +// c.ClearBackground(c.RAYWHITE); + +// // Draw grid +// const screen_h : usize = @intCast(c.GetScreenHeight()); +// const spacing : usize = @intCast(gridSpacing); + +// var limit = screen_h / spacing + 1; +// for (0..limit) |h| { +// const y : i32 = @intCast(h * gridSpacing); +// c.DrawText(c.TextFormat("%02i", y), 4, y - 4, 10, c.GRAY); +// c.DrawLine(24, y, c.GetScreenWidth(), y, c.LIGHTGRAY); +// } +// const screen_v : usize = @intCast(c.GetScreenWidth()); + +// limit = screen_v / spacing + 1; +// for (0..limit) |v| { +// const x : i32 = @intCast(v * gridSpacing); +// c.DrawText(c.TextFormat("%02i", x), x - 10, 4, 10, c.GRAY); +// c.DrawLine(x, 20, x, c.GetScreenHeight(), c.LIGHTGRAY); +// } + +// // Draw UI info +// c.DrawText(c.TextFormat("CURRENT MONITOR: %i/%i (%ix%i)", currentMonitor + 1, c.GetMonitorCount(), +// c.GetMonitorWidth(currentMonitor), c.GetMonitorHeight(currentMonitor)), 50, 50, 20, c.DARKGRAY); +// c.DrawText(c.TextFormat("WINDOW POSITION: %ix%i", windowPos.x, windowPos.y), 50, 90, 20, c.DARKGRAY); +// c.DrawText(c.TextFormat("SCREEN SIZE: %ix%i", c.GetScreenWidth(), c.GetScreenHeight()), 50, 130, 20, c.DARKGRAY); +// c.DrawText(c.TextFormat("RENDER SIZE: %ix%i", c.GetRenderWidth(), c.GetRenderHeight()), 50, 170, 20, c.DARKGRAY); +// c.DrawText(c.TextFormat("SCALE FACTOR: %.1fx%.1f", scaleDpi.x, scaleDpi.y), 50, 210, 20, c.GRAY); + +// // Draw reference rectangles, top-left and bottom-right corners +// c.DrawRectangle(0, 0, 30, 60, c.RED); +// c.DrawRectangle(c.GetScreenWidth() - 30, c.GetScreenHeight() - 60, 30, 60, c.BLUE); + +// // Draw mouse position +// c.DrawCircleV(c.GetMousePosition(), 20, c.MAROON); +// c.DrawRectangle(@intFromFloat(mousePos.x - 25), @intFromFloat(mousePos.y), 50, 2, c.BLACK); +// c.DrawRectangle(@intFromFloat(mousePos.x), @intFromFloat(mousePos.y - 25), 2, 50, c.BLACK); +// c.DrawText(c.TextFormat("[%i,%i]", c.GetMouseX(), c.GetMouseY()), @intFromFloat(mousePos.x - 44), +// if (mousePos.y > @as(f32, @floatFromInt(c.GetScreenHeight())) - 60) @intFromFloat(mousePos.y - 46) else @intFromFloat(mousePos.y + 30), 20, c.BLACK); + +// c.EndDrawing(); +// //---------------------------------------------------------------------------------- +// } + +// // De-Initialization +// //-------------------------------------------------------------------------------------- + +// // TODO: Unload all loaded resources at this point + +// c.CloseWindow(); // Close window and OpenGL context +// //-------------------------------------------------------------------------------------- + +// return; +// } + const c = @cImport({ @cInclude("raylib.h"); }); @@ -375,23 +474,28 @@ const c = @cImport({ //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ -pub fn main() !void { +pub fn main() void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; const screenHeight = 450; - c.SetConfigFlags(c.FLAG_WINDOW_RESIZABLE | c.FLAG_WINDOW_HIGHDPI); - c.InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi testbed"); + c.SetConfigFlags(c.FLAG_WINDOW_HIGHDPI | c.FLAG_WINDOW_RESIZABLE); + c.InitWindow(screenWidth, screenHeight, "raylib [core] example - highdpi demo"); + c.SetWindowMinSize(450, 450); - var scaleDpi = c.GetWindowScaleDPI(); - var mousePos = c.GetMousePosition(); - var currentMonitor = c.GetCurrentMonitor(); - var windowPos = c.GetWindowPosition(); + const logicalGridDescY = 120; + const logicalGridLabelY = logicalGridDescY + 30; + const logicalGridTop = logicalGridLabelY + 30; + const logicalGridBottom = logicalGridTop + 80; + const pixelGridTop = logicalGridBottom - 20; + const pixelGridBottom = pixelGridTop + 80; + const pixelGridLabelY = pixelGridBottom + 30; + const pixelGridDescY = pixelGridLabelY + 30; + const cellSize = 50; + var cellSizePx : f32 = @floatFromInt(cellSize); - const gridSpacing = 40; // Grid spacing in pixels - - c.SetTargetFPS(60); + c.SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop @@ -399,13 +503,15 @@ pub fn main() !void { { // Update //---------------------------------------------------------------------------------- - mousePos = c.GetMousePosition(); - currentMonitor = c.GetCurrentMonitor(); - scaleDpi = c.GetWindowScaleDPI(); - windowPos = c.GetWindowPosition(); + const monitorCount = c.GetMonitorCount(); - if (c.IsKeyPressed(c.KEY_SPACE)) c.ToggleBorderlessWindowed(); - if (c.IsKeyPressed(c.KEY_F)) c.ToggleFullscreen(); + if ((monitorCount > 1) and c.IsKeyPressed(c.KEY_N)) { + c.SetWindowMonitor(@mod((c.GetCurrentMonitor() + 1), monitorCount)); + } + + const currentMonitor = c.GetCurrentMonitor(); + const dpiScale = c.GetWindowScaleDPI(); + cellSizePx = (@as(f32, @floatFromInt(cellSize)))/dpiScale.x; //---------------------------------------------------------------------------------- // Draw @@ -414,43 +520,50 @@ pub fn main() !void { c.ClearBackground(c.RAYWHITE); - // Draw grid - const screen_h : usize = @intCast(c.GetScreenHeight()); - const spacing : usize = @intCast(gridSpacing); + const windowCenter = @divFloor(c.GetScreenWidth(), 2); + DrawTextCenter(c.TextFormat("Dpi Scale: %f", dpiScale.x), windowCenter, 30, 40, c.DARKGRAY); + DrawTextCenter(c.TextFormat("Monitor: %d/%d ([N] next monitor)", currentMonitor+1, monitorCount), windowCenter, 70, 20, c.LIGHTGRAY); + DrawTextCenter(c.TextFormat("Window is %d \"logical points\" wide", c.GetScreenWidth()), windowCenter, logicalGridDescY, 20, c.ORANGE); - var limit = screen_h / spacing + 1; - for (0..limit) |h| { - const y : i32 = @intCast(h * gridSpacing); - c.DrawText(c.TextFormat("%02i", y), 4, y - 4, 10, c.GRAY); - c.DrawLine(24, y, c.GetScreenWidth(), y, c.LIGHTGRAY); - } - const screen_v : usize = @intCast(c.GetScreenWidth()); + var odd = true; + var i : i32 = cellSize; + while (i < c.GetScreenWidth()) { + // for (int i = cellSize; i < GetScreenWidth(); i += cellSize, odd = !odd) { + if (odd) c.DrawRectangle(i, logicalGridTop, cellSize, logicalGridBottom-logicalGridTop, c.ORANGE); - limit = screen_v / spacing + 1; - for (0..limit) |v| { - const x : i32 = @intCast(v * gridSpacing); - c.DrawText(c.TextFormat("%02i", x), x - 10, 4, 10, c.GRAY); - c.DrawLine(x, 20, x, c.GetScreenHeight(), c.LIGHTGRAY); + DrawTextCenter(c.TextFormat("%d", i), i, logicalGridLabelY, 10, c.LIGHTGRAY); + c.DrawLine(i, logicalGridLabelY + 10, i, logicalGridBottom, c.GRAY); + + i += cellSize; + odd = !odd; } - // Draw UI info - c.DrawText(c.TextFormat("CURRENT MONITOR: %i/%i (%ix%i)", currentMonitor + 1, c.GetMonitorCount(), - c.GetMonitorWidth(currentMonitor), c.GetMonitorHeight(currentMonitor)), 50, 50, 20, c.DARKGRAY); - c.DrawText(c.TextFormat("WINDOW POSITION: %ix%i", windowPos.x, windowPos.y), 50, 90, 20, c.DARKGRAY); - c.DrawText(c.TextFormat("SCREEN SIZE: %ix%i", c.GetScreenWidth(), c.GetScreenHeight()), 50, 130, 20, c.DARKGRAY); - c.DrawText(c.TextFormat("RENDER SIZE: %ix%i", c.GetRenderWidth(), c.GetRenderHeight()), 50, 170, 20, c.DARKGRAY); - c.DrawText(c.TextFormat("SCALE FACTOR: %.1fx%.1f", scaleDpi.x, scaleDpi.y), 50, 210, 20, c.GRAY); + odd = true; + const minTextSpace = 30; + var lastTextX : i32 = -minTextSpace; + i = cellSize; + while (i < c.GetRenderWidth()) { + // for (int i = cellSize; i < GetRenderWidth(); i += cellSize, odd = !odd) { + const x : i32 = @intFromFloat(@as(f32, @floatFromInt(i)) / dpiScale.x); + if (odd) c.DrawRectangle(x, pixelGridTop, @intFromFloat(cellSizePx), pixelGridBottom - pixelGridTop, c.Color{ .r = 0, .g = 121, .b = 241, .a = 100 }); - // Draw reference rectangles, top-left and bottom-right corners - c.DrawRectangle(0, 0, 30, 60, c.RED); - c.DrawRectangle(c.GetScreenWidth() - 30, c.GetScreenHeight() - 60, 30, 60, c.BLUE); + c.DrawLine(x, pixelGridTop, @intFromFloat(@as(f32, @floatFromInt(i)) / dpiScale.x), pixelGridLabelY - 10, c.GRAY); - // Draw mouse position - c.DrawCircleV(c.GetMousePosition(), 20, c.MAROON); - c.DrawRectangle(@intFromFloat(mousePos.x - 25), @intFromFloat(mousePos.y), 50, 2, c.BLACK); - c.DrawRectangle(@intFromFloat(mousePos.x), @intFromFloat(mousePos.y - 25), 2, 50, c.BLACK); - c.DrawText(c.TextFormat("[%i,%i]", c.GetMouseX(), c.GetMouseY()), @intFromFloat(mousePos.x - 44), - if (mousePos.y > @as(f32, @floatFromInt(c.GetScreenHeight())) - 60) @intFromFloat(mousePos.y - 46) else @intFromFloat(mousePos.y + 30), 20, c.BLACK); + if ((x - lastTextX) >= minTextSpace) + { + DrawTextCenter(c.TextFormat("%d", i), x, pixelGridLabelY, 10, c.LIGHTGRAY); + lastTextX = x; + } + i += cellSize; + odd = !odd; + } + + DrawTextCenter(c.TextFormat("Window is %d \"physical pixels\" wide", c.GetRenderWidth()), windowCenter, pixelGridDescY, 20, c.BLUE); + + const text = "Can you see this?"; + const size = c.MeasureTextEx(c.GetFontDefault(), text, 20, 3); + const pos = c.Vector2{ .x = @as(f32, @floatFromInt(c.GetScreenWidth())) - size.x - 5, .y = @as(f32, @floatFromInt(c.GetScreenHeight())) - size.y - 5 }; + c.DrawTextEx(c.GetFontDefault(), text, pos, 20, 3, c.LIGHTGRAY); c.EndDrawing(); //---------------------------------------------------------------------------------- @@ -458,11 +571,16 @@ pub fn main() !void { // De-Initialization //-------------------------------------------------------------------------------------- - - // TODO: Unload all loaded resources at this point - c.CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- - - return; +} + +//------------------------------------------------------------------------------------ +// Module Functions Definition +//------------------------------------------------------------------------------------ +fn DrawTextCenter(text : [*:0]const u8, x : i32, y: i32, fontSize : i32, color: c.Color) void { +// fn DrawTextCenter(const char *text, int x, int y, int fontSize, Color color) void { + const size = c.MeasureTextEx(c.GetFontDefault(), text, @floatFromInt(fontSize), 3); + const pos = c.Vector2{ .x = @as(f32, @floatFromInt(x)) - size.x/2, .y = @as(f32, @floatFromInt(y)) - size.y/2 }; + c.DrawTextEx(c.GetFontDefault(), text, pos, @as(f32, @floatFromInt(fontSize)), 3, color); }