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
// RECTANGLES
// RECTANGLES ===============
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); }
@ -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); }
// CIRCLES
// CIRCLES ===============
pub inline fn DrawCircleVWS(center: rl.Vector2, radius: f32, color: rl.Color) void
{ 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
//--------------------------------------------------------------------------------------
var gpAlloc = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpAlloc.allocator();
var gp_alloc = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gp_alloc.allocator();
// Initialization
//--------------------------------------------------------------------------------------
@ -28,7 +28,7 @@ pub fn main() anyerror!void
// rl.SetWindowState(rl.ConfigFlags.FLAG_WINDOW_RESIZABLE);
defer rl.CloseWindow(); // Close window and OpenGL context
var camera = rl.Camera2D {
var ws_camera = rl.Camera2D {
.target = rl.Vector2 { .x = 0, .y = 0 },
.offset = rl.Vector2 { .x = screenWidth / 2, .y = screenHeight / 2 },
.rotation = 0,
@ -82,9 +82,9 @@ pub fn main() anyerror!void
phs.ApplyPlayerCollisions(&player, map);
// 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
camera.zoom += rl.GetMouseWheelMove() * 0.05 * camera.zoom;
ws_camera.zoom += rl.GetMouseWheelMove() * 0.05 * ws_camera.zoom;
// DEBUG
@ -95,8 +95,8 @@ pub fn main() anyerror!void
// MOUSE SELECTION LOGIC =======
if (rl.IsMouseButtonPressed(.MOUSE_BUTTON_LEFT)) { mouse_ws_position = utils.GetMousePositionWS(camera, screenWidth, screenHeight); }
if (rl.IsMouseButtonDown(.MOUSE_BUTTON_LEFT)) { mouse_ws_end_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(ws_camera, screenWidth, screenHeight); }
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)};
@ -110,14 +110,17 @@ pub fn main() anyerror!void
{
// init drawing
rl.BeginDrawing();
defer rl.EndDrawing();
// clear screen with WHITE
defer rl.ClearBackground(rl.RAYWHITE);
rl.ClearBackground(rl.RAYWHITE);
{
// init camera
camera.Begin();
defer camera.End();
ws_camera.Begin();
defer ws_camera.End();
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 };
// _ = playerRenderBox;
// 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);
@ -157,7 +160,7 @@ pub fn main() anyerror!void
// draw tile origin points
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);
}
@ -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 {
box: rl.Rectangle,
texture: ?rl.Texture2D,
texture: ?*rl.Texture2D,
};
pub const Player = struct {