Can add blocks with mouse now

This commit is contained in:
Igor Łempicki 2023-06-17 14:44:28 +02:00
parent 4723632792
commit c69a601a4f
3 changed files with 29 additions and 18 deletions

View File

@ -2,7 +2,7 @@ const rl = @import("raylib");
// functions for drawing shapes from WS coordinates // functions for drawing shapes from WS coordinates
// RECTANGLES // RECTANGLES ===============
pub inline fn DrawRectangleWS(posX: c_int, posY: c_int, width: c_int, height: c_int, color: rl.Color) void pub inline fn DrawRectangleWS(posX: c_int, posY: c_int, width: c_int, height: c_int, color: rl.Color) void
{ rl.DrawRectangle(posX, -(posY + height), width, height, color); } { rl.DrawRectangle(posX, -(posY + height), width, height, color); }
@ -13,6 +13,14 @@ pub inline fn DrawRectangleRecWS(rec: rl.Rectangle, color: rl.Color) void
{ rl.DrawRectangleRec(.{ .x = rec.x, .y = -(rec.y + rec.height), .width = rec.width, .height = rec.height}, color); } { rl.DrawRectangleRec(.{ .x = rec.x, .y = -(rec.y + rec.height), .width = rec.width, .height = rec.height}, color); }
// CIRCLES // CIRCLES ===============
pub inline fn DrawCircleVWS(center: rl.Vector2, radius: f32, color: rl.Color) void pub inline fn DrawCircleVWS(center: rl.Vector2, radius: f32, color: rl.Color) void
{ rl.DrawCircleV( .{ .x = center.x, .y = -center.y }, radius, color); } { rl.DrawCircleV(.{ .x = center.x, .y = -center.y }, radius, color); }
// TEXTURES ===============
pub inline fn DrawTextureVWS(texture: rl.Texture2D, position: rl.Vector2, tint: rl.Color) void
{ rl.DrawTextureV(texture, .{ .x = position.x, .y = -position.y}, tint); }
pub inline fn DrawTextureRecWS(texture: rl.Texture2D, source: rl.Rectangle, position: rl.Vector2, tint: rl.Color) void
{ rl.DrawTextureRec(texture, .{ .x = source.x, .y = -source.y, .width = source.width, .height = source.height }, .{ .x = position.x, .y = -position.y }, tint); }

View File

@ -16,8 +16,8 @@ pub fn main() anyerror!void
{ {
// MEMORY ALLOCATOR // MEMORY ALLOCATOR
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
var gpAlloc = std.heap.GeneralPurposeAllocator(.{}){}; var gp_alloc = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpAlloc.allocator(); var allocator = gp_alloc.allocator();
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -28,7 +28,7 @@ pub fn main() anyerror!void
// rl.SetWindowState(rl.ConfigFlags.FLAG_WINDOW_RESIZABLE); // rl.SetWindowState(rl.ConfigFlags.FLAG_WINDOW_RESIZABLE);
defer rl.CloseWindow(); // Close window and OpenGL context defer rl.CloseWindow(); // Close window and OpenGL context
var camera = rl.Camera2D { var ws_camera = rl.Camera2D {
.target = rl.Vector2 { .x = 0, .y = 0 }, .target = rl.Vector2 { .x = 0, .y = 0 },
.offset = rl.Vector2 { .x = screenWidth / 2, .y = screenHeight / 2 }, .offset = rl.Vector2 { .x = screenWidth / 2, .y = screenHeight / 2 },
.rotation = 0, .rotation = 0,
@ -82,9 +82,9 @@ pub fn main() anyerror!void
phs.ApplyPlayerCollisions(&player, map); phs.ApplyPlayerCollisions(&player, map);
// camera setup // camera setup
camera.target = rl.Vector2 { .x = player.box.x + player.box.width / 2, .y = -(player.box.y + player.box.height / 2) }; ws_camera.target = rl.Vector2 { .x = player.box.x + player.box.width / 2, .y = -(player.box.y + player.box.height / 2) };
// input related to camera // input related to camera
camera.zoom += rl.GetMouseWheelMove() * 0.05 * camera.zoom; ws_camera.zoom += rl.GetMouseWheelMove() * 0.05 * ws_camera.zoom;
// DEBUG // DEBUG
@ -95,8 +95,8 @@ pub fn main() anyerror!void
// MOUSE SELECTION LOGIC ======= // MOUSE SELECTION LOGIC =======
if (rl.IsMouseButtonPressed(.MOUSE_BUTTON_LEFT)) { mouse_ws_position = utils.GetMousePositionWS(camera, screenWidth, screenHeight); } if (rl.IsMouseButtonPressed(.MOUSE_BUTTON_LEFT)) { mouse_ws_position = utils.GetMousePositionWS(ws_camera, screenWidth, screenHeight); }
if (rl.IsMouseButtonDown(.MOUSE_BUTTON_LEFT)) { mouse_ws_end_position = utils.GetMousePositionWS(camera, screenWidth, screenHeight); } if (rl.IsMouseButtonDown(.MOUSE_BUTTON_LEFT)) { mouse_ws_end_position = utils.GetMousePositionWS(ws_camera, screenWidth, screenHeight); }
var rec_fixed = .{ .x = mouse_ws_position.x, .y = mouse_ws_position.y }; var rec_fixed = .{ .x = mouse_ws_position.x, .y = mouse_ws_position.y };
var rec_wh = .{ .x = @fabs(mouse_ws_position.x - mouse_ws_end_position.x), .y = @fabs(mouse_ws_position.y - mouse_ws_end_position.y)}; var rec_wh = .{ .x = @fabs(mouse_ws_position.x - mouse_ws_end_position.x), .y = @fabs(mouse_ws_position.y - mouse_ws_end_position.y)};
@ -110,14 +110,17 @@ pub fn main() anyerror!void
{ {
// init drawing // init drawing
rl.BeginDrawing(); rl.BeginDrawing();
defer rl.EndDrawing();
// clear screen with WHITE // clear screen with WHITE
defer rl.ClearBackground(rl.RAYWHITE); rl.ClearBackground(rl.RAYWHITE);
{ {
// init camera // init camera
camera.Begin(); ws_camera.Begin();
defer camera.End(); defer ws_camera.End();
defer disp.DrawCircleVWS( .{ .x = 0, .y = 0 }, 4, rl.BLUE); defer disp.DrawCircleVWS( .{ .x = 0, .y = 0 }, 4, rl.BLUE);
@ -136,7 +139,7 @@ pub fn main() anyerror!void
// var playerRenderBox = .{ .x = player.box.x, .y = -player.box.y - player.box.height, .width = player.box.width, .height = player.box.height }; // var playerRenderBox = .{ .x = player.box.x, .y = -player.box.y - player.box.height, .width = player.box.width, .height = player.box.height };
// _ = playerRenderBox; // _ = playerRenderBox;
// defer rl.DrawRectangleRec( playerRenderBox, rl.RED); // defer rl.DrawRectangleRec( playerRenderBox, rl.RED);
defer rl.DrawTextureV( player_texture, .{ .x = player.box.x, .y = -player.box.y - player.box.height }, rl.WHITE); defer disp.DrawTextureVWS( player_texture, .{ .x = player.box.x, .y = player.box.y + player.box.height}, rl.WHITE);
// rl.DrawRectangleRec(.{ .x = player.detection_box.x.* - 5 , .y = player.detection_box.y.* - 5, .width = player.detection_box.width, .height = player.detection_box.height }, rl.RED); // rl.DrawRectangleRec(.{ .x = player.detection_box.x.* - 5 , .y = player.detection_box.y.* - 5, .width = player.detection_box.width, .height = player.detection_box.height }, rl.RED);
@ -157,7 +160,7 @@ pub fn main() anyerror!void
// draw tile origin points // draw tile origin points
disp.DrawCircleVWS( .{ .x = tile.box.x, .y = tile.box.y }, 4, rl.ORANGE ); disp.DrawCircleVWS( .{ .x = tile.box.x, .y = tile.box.y }, 4, rl.ORANGE );
// draw collision rectangle (with player collision box) // draw collision rectangle (with player detection box)
disp.DrawRectangleRecWS( rl.GetCollisionRec( .{ .x = player.detection_box.x.* - 1, .y = player.detection_box.y.* - 1, .width = player.detection_box.width, .height = player.detection_box.height}, tile.box ), rl.BLUE); disp.DrawRectangleRecWS( rl.GetCollisionRec( .{ .x = player.detection_box.x.* - 1, .y = player.detection_box.y.* - 1, .width = player.detection_box.width, .height = player.detection_box.height}, tile.box ), rl.BLUE);
} }
@ -168,9 +171,9 @@ pub fn main() anyerror!void
} }
} }
} }
rl.EndDrawing();
} }
} }

View File

@ -3,7 +3,7 @@ const std = @import("std");
pub const Object = struct { pub const Object = struct {
box: rl.Rectangle, box: rl.Rectangle,
texture: ?rl.Texture2D, texture: ?*rl.Texture2D,
}; };
pub const Player = struct { pub const Player = struct {