Add ability to move bricks
This commit is contained in:
+73
-29
@@ -38,26 +38,45 @@ const State = struct {
|
|||||||
frame_count: i32,
|
frame_count: i32,
|
||||||
mouse_pos: V2,
|
mouse_pos: V2,
|
||||||
camera: Camera,
|
camera: Camera,
|
||||||
|
brick: Brick,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
fn pointInsideRect(p: V2, rect_start: V2, rect_size: V2) bool {
|
||||||
|
var result: bool = false;
|
||||||
|
|
||||||
|
const rect_end: V2 = rect_start.add(rect_size);
|
||||||
|
|
||||||
|
const inside_x = rect_start.x < p.x and p.x < rect_end.x;
|
||||||
|
const inside_y = rect_start.y < p.y and p.y < rect_end.y;
|
||||||
|
|
||||||
|
result = inside_x and inside_y;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
fn fillRect(
|
fn fillRect(
|
||||||
buffer: platform.OffscreenBuffer,
|
buffer: platform.OffscreenBuffer,
|
||||||
x: i32,
|
pos_x: i32,
|
||||||
y: i32,
|
pos_y: i32,
|
||||||
width: i32,
|
width: i32,
|
||||||
height: i32,
|
height: i32,
|
||||||
color: u32,
|
color: u32,
|
||||||
) void {
|
) void {
|
||||||
var pixels: []u32 = @ptrCast(@alignCast(buffer.data));
|
var pixels: []u32 = @ptrCast(@alignCast(buffer.data));
|
||||||
|
|
||||||
const start_x: usize = @intCast(x);
|
var start_x: i32 = pos_x;
|
||||||
const end_x: usize = @intCast(x + width);
|
var start_y: i32 = pos_y;
|
||||||
const start_y: usize = @intCast(y);
|
var end_x: i32 = pos_x + width;
|
||||||
const end_y: usize = @intCast(y + height);
|
var end_y: i32 = pos_y + height;
|
||||||
|
|
||||||
for (start_y..end_y) |i| {
|
if (start_x < 0) start_x = 0;
|
||||||
for (start_x..end_x) |j| {
|
if (start_y < 0) start_y = 0;
|
||||||
const idx: usize = i * @as(usize, @intCast(buffer.width)) + j;
|
if (buffer.width < end_x) end_x = buffer.width;
|
||||||
|
if (buffer.height < end_y) end_y = buffer.height;
|
||||||
|
|
||||||
|
for (@intCast(start_y) .. @intCast(end_y)) |y| {
|
||||||
|
for (@intCast(start_x) .. @intCast(end_x)) |x| {
|
||||||
|
const idx: usize = y * @as(usize, @intCast(buffer.width)) + x;
|
||||||
|
|
||||||
pixels[idx] = color;
|
pixels[idx] = color;
|
||||||
}
|
}
|
||||||
@@ -77,11 +96,19 @@ fn fillSquare(
|
|||||||
pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBuffer, input: platform.AppInput) void {
|
pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBuffer, input: platform.AppInput) void {
|
||||||
var state: *State = @ptrCast(@alignCast(memory.permanent_storage));
|
var state: *State = @ptrCast(@alignCast(memory.permanent_storage));
|
||||||
|
|
||||||
|
const mid = V2{ .x = @floatFromInt(@divTrunc(buffer.width, 2)), .y = @floatFromInt(@divTrunc(buffer.height, 2)), };
|
||||||
if (!memory.initialized) {
|
if (!memory.initialized) {
|
||||||
state.camera.offset.x = @floatFromInt(@divTrunc(buffer.width, 2));
|
state.camera.offset = mid;
|
||||||
state.camera.offset.y = @floatFromInt(@divTrunc(buffer.height, 2));
|
|
||||||
|
|
||||||
state.camera.pos = state.camera.pos.add(state.camera.offset);
|
state.brick = Brick{
|
||||||
|
.pos = V2{ .x = 50, .y = 50},
|
||||||
|
.tiles = [4][4]u32{
|
||||||
|
.{1, 1, 1, 1},
|
||||||
|
.{1, 0, 0, 0},
|
||||||
|
.{0, 0, 0, 0},
|
||||||
|
.{0, 0, 0, 0},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
memory.initialized = true;
|
memory.initialized = true;
|
||||||
}
|
}
|
||||||
@@ -89,27 +116,45 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
|
|||||||
// Update
|
// Update
|
||||||
const mouse_pos = V2{ .x = @floatCast(input.mouse_x), .y = @floatCast(input.mouse_y) };
|
const mouse_pos = V2{ .x = @floatCast(input.mouse_x), .y = @floatCast(input.mouse_y) };
|
||||||
|
|
||||||
if (input.mouse_left_down) {
|
const tile_size = V2{
|
||||||
|
.x = TILE_SIZE,
|
||||||
|
.y = TILE_SIZE,
|
||||||
|
};
|
||||||
|
|
||||||
|
var mouse_on_brick = false;
|
||||||
|
for (state.brick.tiles, 0..) |row, i| {
|
||||||
|
for (row, 0..) |tile, j| {
|
||||||
|
if (tile == 1) {
|
||||||
|
const tile_pos = V2{
|
||||||
|
.x = @floatFromInt(j * TILE_SIZE),
|
||||||
|
.y = @floatFromInt(i * TILE_SIZE),
|
||||||
|
};
|
||||||
|
const pos = tile_pos.add(state.brick.pos);
|
||||||
|
|
||||||
|
const camera_pos = pos.add(state.camera.pos);
|
||||||
|
if (pointInsideRect(state.mouse_pos, camera_pos, tile_size)) {
|
||||||
|
if (input.mouse_left_down) {
|
||||||
|
const diff = mouse_pos.sub(state.mouse_pos);
|
||||||
|
state.brick.pos = state.brick.pos.add(diff);
|
||||||
|
mouse_on_brick = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.mouse_left_down and !mouse_on_brick) {
|
||||||
const diff = mouse_pos.sub(state.mouse_pos);
|
const diff = mouse_pos.sub(state.mouse_pos);
|
||||||
state.camera.pos = state.camera.pos.add(diff);
|
state.camera.pos = state.camera.pos.add(diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
state.mouse_pos = mouse_pos;
|
|
||||||
|
|
||||||
const brick = Brick{
|
state.mouse_pos = mouse_pos;
|
||||||
.pos = V2{ .x = 50, .y = 50},
|
|
||||||
.tiles = [4][4]u32{
|
|
||||||
.{1, 1, 1, 1},
|
|
||||||
.{1, 0, 0, 0},
|
|
||||||
.{0, 0, 0, 0},
|
|
||||||
.{0, 0, 0, 0},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
fillRect(buffer, 0, 0, buffer.width, buffer.height, 0xFFFFFFFF);
|
fillRect(buffer, 0, 0, buffer.width, buffer.height, 0xFFFFFFFF);
|
||||||
|
|
||||||
const board_pos = V2{ .x = 64, .y = 64 };
|
const board_pos = mid;
|
||||||
for (0..7) |i| {
|
for (0..7) |i| {
|
||||||
for (0..7) |j| {
|
for (0..7) |j| {
|
||||||
if (i == 0 and j == 6) continue;
|
if (i == 0 and j == 6) continue;
|
||||||
@@ -128,17 +173,16 @@ pub fn updateAndRender(memory: *platform.AppMemory, buffer: platform.OffscreenBu
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (brick.tiles, 0..) |row, i| {
|
for (state.brick.tiles, 0..) |row, i| {
|
||||||
for (row, 0..) |tile, j| {
|
for (row, 0..) |tile, j| {
|
||||||
if (tile == 1) {
|
if (tile == 1) {
|
||||||
const pos = V2{
|
const pos = V2{
|
||||||
.x = @floatFromInt(j * TILE_SIZE + 8),
|
.x = @floatFromInt(j * TILE_SIZE),
|
||||||
.y = @floatFromInt(i * TILE_SIZE + 8),
|
.y = @floatFromInt(i * TILE_SIZE),
|
||||||
};
|
};
|
||||||
const camera_pos = brick.pos.add(pos).add(state.camera.pos);
|
const camera_pos = state.brick.pos.add(pos).add(state.camera.pos);
|
||||||
fillSquare(buffer, @intFromFloat(camera_pos.x), @intFromFloat(camera_pos.y), 30, 0xFFFF0000);
|
fillSquare(buffer, @intFromFloat(camera_pos.x), @intFromFloat(camera_pos.y), 30, 0xFFFF0000);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user