Compare commits

..

5 Commits

Author SHA1 Message Date
c69a601a4f Can add blocks with mouse now 2023-06-17 14:44:28 +02:00
4723632792 added logic for adding boxes from mouse selection 2023-06-16 14:18:34 +02:00
IgLemp
933538a42f Broken some stuff, too lazy to branch 2023-06-15 23:18:13 +02:00
IgLemp
8fdca7bbb9 Made half implemented system to determine mouse position in WS coordinated 2023-06-15 15:26:24 +02:00
IgLemp
c003155edd Swapped allocators 2023-06-15 12:13:56 +02:00
6 changed files with 101 additions and 35 deletions

26
src/display.zig Normal file
View File

@@ -0,0 +1,26 @@
const rl = @import("raylib");
// functions for drawing shapes from WS coordinates
// 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); }
pub inline fn DrawRectangleVWS(position: rl.Vector2, size: rl.Vector2, color: rl.Color) void
{ rl.DrawRectangleV(.{ .x = position.x, .y = -(position.y + size.y) }, size, color); }
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 ===============
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); }

0
src/io.zig Normal file
View File

View File

@@ -3,19 +3,21 @@ const rlm = @import("raylib-math");
const std = @import("std");
const obj = @import("types.zig");
const phs = @import("physics.zig");
const utils = @import("utils.zig");
const disp = @import("display.zig");
const DEBUG = true;
// NOTE
// Coordinate system starts at top left corner, whitch means THE Y AXIS IS FLIPPED!!!
// Coordinate system starts at top left corner, whitch means THE Y AXIS IS FLIPPED!!! (for on screen coordinates)
// MAIN
pub fn main() anyerror!void
{
// MEMORY ALLOCATOR
//--------------------------------------------------------------------------------------
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
var arenaAlloc = arena.allocator();
var gp_alloc = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gp_alloc.allocator();
// Initialization
//--------------------------------------------------------------------------------------
@@ -26,20 +28,15 @@ 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,
.zoom = 1,
};
// var map: obj.Map = obj.Map { .tiles = undefined };
// var objects = [_]obj.Object{
// .{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null },
// .{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null }
// };
var objects = std.ArrayList(obj.Object).init(arenaAlloc);
var objects = std.ArrayList(obj.Object).init(allocator);
try objects.append(.{ .box = .{ .x = 0, .y = 0, .width = 300, .height = 20, }, .texture = null });
try objects.append(.{ .box = .{ .x = 100, .y = 0, .width = 20, .height = 200, }, .texture = null });
@@ -55,13 +52,15 @@ pub fn main() anyerror!void
player.detection_box = .{ .x = &player.box.x, .y = &player.box.y, .width = 22, .height = 22 };
var texture = rl.LoadTexture("./resources/log.png");
var playerTexture = rl.LoadTexture("./resources/cursor.png");
var player_texture = rl.LoadTexture("./resources/cursor.png");
// freefly
var fly = true;
rl.SetTargetFPS(60); // Set our game to run at 60 frames-per-second
var mouse_ws_position: rl.Vector2 = undefined;
var mouse_ws_end_position: rl.Vector2 = undefined;
// Main game loop ================================================================
while (!rl.WindowShouldClose()) // Detect window close button or ESC key
@@ -83,62 +82,86 @@ 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
// try stdout.print("player position: x = {d}, y = {d}\n", .{player.box.x, player.box.y});
// std.log.debug("player position: x = {d}, y = {d}, velocity: x = {d}, y = {d}", .{player.box.x, player.box.y, player.velocity.x, player.velocity.y});
// std.log.debug("{}", .{player.detection_box});
// std.debug.print("camera terget: x = {d}, y = {d}\n", .{camera.target.x, camera.target.y});
// MOUSE SELECTION LOGIC =======
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)};
if (mouse_ws_end_position.x < mouse_ws_position.x) { rec_fixed.x = mouse_ws_end_position.x; }
if (mouse_ws_end_position.y < mouse_ws_position.y) { rec_fixed.y = mouse_ws_end_position.y; }
if (rl.IsMouseButtonReleased(.MOUSE_BUTTON_LEFT)) { try map.tiles.append( .{ .box = .{ .x = rec_fixed.x, .y = rec_fixed.y, .width = rec_wh.x, .height = rec_wh.y }, .texture = null } ); }
{
// 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 rl.DrawCircleV( .{ .x = 0, .y = 0 } , 4, rl.BLUE);
defer disp.DrawCircleVWS( .{ .x = 0, .y = 0 }, 4, rl.BLUE);
// what have I done
rl.DrawTextureV(texture, .{ .x = -100, .y = -200}, rl.WHITE);
disp.DrawCircleVWS( mouse_ws_position, 10, rl.RED);
disp.DrawCircleVWS( mouse_ws_end_position, 10, rl.BLUE);
// draw player
var playerRenderBox = .{ .x = player.box.x, .y = -player.box.y - player.box.height, .width = player.box.width, .height = player.box.height };
_ = playerRenderBox;
// for drawing region selected with mouse
disp.DrawRectangleVWS( rec_fixed, rec_wh, rl.BLUE );
// draw player =====
// 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( playerTexture, .{ .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);
// tile drawing
for (map.tiles.allocatedSlice()) |tile| {
var dispTile = .{ .x = tile.box.x, .y = tile.box.y - tile.box.height, .width = tile.box.width, .height = tile.box.height };
rl.DrawRectangleRec(dispTile, rl.GRAY);
for (map.tiles.items) |tile| {
disp.DrawRectangleRecWS(tile.box, rl.GRAY);
// DEBUG
if (DEBUG) {
// draw block midpoints
var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = -(tile.box.y + (tile.box.height / 2)) };
rl.DrawCircleV( midpoint, 4, rl.GREEN);
var midpoint: rl.Vector2 = .{ .x = tile.box.x + (tile.box.width / 2), .y = tile.box.y + (tile.box.height / 2) };
disp.DrawCircleVWS( midpoint, 4, rl.GREEN);
// draw collision rectangles (with player)
var col_rec = rl.GetCollisionRec(player.box, tile.box);
rl.DrawRectangleRec(col_rec, rl.BLUE);
disp.DrawRectangleRecWS(col_rec, rl.BLUE);
// draw tile origin points
rl.DrawCircleV( .{ .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)
rl.DrawRectangleRec( rl.GetCollisionRec( .{ .x = player.detection_box.x.* - 1, .y = -player.detection_box.y.* - 1 - player.box.height, .width = player.detection_box.width, .height = player.detection_box.height}, dispTile ), rl.BLUE);
// 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);
}
if (!fly) {
@@ -148,9 +171,9 @@ pub fn main() anyerror!void
}
}
}
rl.EndDrawing();
}
}

View File

@@ -49,7 +49,7 @@ pub const movement = struct {
pub fn ApplyPlayerCollisions(player: *obj.Player, map: obj.Map) void {
// for every tile
for (map.tiles.allocatedSlice()) |tile| {
for (map.tiles.items) |tile| {
// check if any collision occured
if ( rl.CheckCollisionRecs(player.box, tile.box) ) {

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 {

17
src/utils.zig Normal file
View File

@@ -0,0 +1,17 @@
const rl = @import("raylib");
pub fn GetMousePositionWS(camera: rl.Camera2D, screenWidth: f32, screenHeight: f32) rl.Vector2 {
var mouse_position = rl.GetMousePosition();
// wold space position vectors for left-top and right-botton respectivly
var ws_position_vector_lt: rl.Vector2 = .{ .x = camera.target.x - (screenWidth / 2) * (1 / camera.zoom), .y = camera.target.y - (screenHeight / 2) * (1 / camera.zoom)};
var ws_position_vector_rb: rl.Vector2 = .{ .x = camera.target.x + (screenWidth / 2) * (1 / camera.zoom), .y = camera.target.y + (screenHeight / 2) * (1 / camera.zoom)};
// range vector of distances of screen height and width translated to worldspace coordinates
var ws_position_vector_range: rl.Vector2 = .{ .x = ws_position_vector_rb.x - ws_position_vector_lt.x, .y = ws_position_vector_rb.y - ws_position_vector_lt.y};
// mouse position vector but it's a range between 0 and 1
var scaled_mouse_position_vector: rl.Vector2 = .{ .x = mouse_position.x / screenWidth, .y = mouse_position.y / screenHeight};
return .{ .x = ws_position_vector_lt.x + scaled_mouse_position_vector.x * ws_position_vector_range.x, .y = -(ws_position_vector_lt.y + scaled_mouse_position_vector.y * ws_position_vector_range.y)};
}