scrolling kinda works if you squint!

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2022-07-02 13:04:58 -04:00
parent 243fbb0584
commit 3a86c4674d
8 changed files with 540 additions and 568 deletions

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.9" tiledversion="1.9.0" orientation="orthogonal" renderorder="right-down" width="40" height="40" tilewidth="8" tileheight="8" infinite="0" nextlayerid="6" nextobjectid="39">
<map version="1.4" tiledversion="1.4.3" orientation="orthogonal" renderorder="right-down" width="40" height="40" tilewidth="8" tileheight="8" infinite="0" nextlayerid="6" nextobjectid="39">
<editorsettings>
<export target="RPGmap.json" format="json"/>
</editorsettings>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 326 B

After

Width:  |  Height:  |  Size: 356 B

View File

@ -19,7 +19,6 @@ var mara_frame: bool = false;
var mara_box: Rect = Rect{ .base = map.start_point, .width = 16, .height = 16 };
var mara_speed: i16 = 0;
var old_speed: i16 = 0;
var screen = Rect{ .base = Point{ .x = 0, .y = 0 }, .width = 160, .height = 160 };
const screen_width = 20;
const screen_height = 20;
@ -35,7 +34,7 @@ var camera = Point{ .x = 0, .y = 0 };
var state: sh0rk.State = .Gameplay;
fn world_to_screen(p: Point) Point {
p.sub(camera);
return p.sub(camera);
}
fn screen_to_world(p: Point) Point {
@ -69,10 +68,10 @@ fn bonk() void {
},
);
sound_timer = 12;
sound_timer = 30;
}
fn drawMap() !void {
fn map_colors() void {
switch (map.tileset) {
.Dungeon => {
w4.m.colors.* = .{
@ -91,58 +90,44 @@ fn drawMap() !void {
};
},
}
if (!done) {
var buf = fmt.bufPrint(&textBuf, "camera: {},{}", .{ camera.x, camera.y }) catch unreachable;
w4.trace(buf);
}
var startCol: i32 = @divTrunc(camera.x, tile_size);
var endCol = startCol + screen_width;
var startRow: i32 = @divTrunc(camera.y, tile_size);
var endRow = startRow + screen_height;
fn drawMap() !void {
map_colors();
var offsetX = startCol * tile_size;
var offsetY = startRow * tile_size;
var startCol = @divTrunc(camera.x, tile_size);
var endCol = startCol + screen_width;
var startRow = @divTrunc(camera.y, tile_size);
var endRow = startRow + screen_height;
if (!done) {
var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ startCol, startRow, endCol, endRow }) catch unreachable;
w4.trace(buf);
}
if (!done) {
var buf = fmt.bufPrint(&textBuf, "offx: {}, offy: {}", .{ offsetX, offsetY }) catch unreachable;
w4.trace(buf);
}
var col: i32 = startCol;
var col: i16 = startCol;
while (col < endCol) {
var row: i32 = startRow;
var row: i16 = startRow;
defer col += 1;
while (row < endRow) {
defer row += 1;
var tile = map.get_tile(@intCast(u32, col), @intCast(u32, row));
var x = (row - startCol) * tile_size - offsetX;
var y = (col - startRow) * tile_size - offsetY;
if (x < 0) {
x = x * -1;
}
if (y < 0) {
y = y * -1;
}
if (col < map.width and row < map.height) {
var tile = map.get_tile(col, row);
var world = Point{ .x = col * 8, .y = row * 8 };
var scr = world_to_screen(world);
if(!done) {
var buf = fmt.bufPrint(&textBuf, "{},{}: {}: {},{}", .{ col, row, tile, x, y }) catch unreachable;
var buf = fmt.bufPrint(&textBuf, "{},{}: {},{}", .{ row, col, scr.x, scr.y }) catch unreachable;
w4.trace(buf);
}
draw_tile(tile, @intCast(u32, x), @intCast(u32, y));
draw_tile(tile, scr.y, scr.x);
}
}
}
}
fn draw_tile(tile: u32, x: u32, y: u32) void {
fn draw_tile(tile: u32, x: i32, y: i32) void {
if (tile == 0) {
w4.rect(x, y, 8, 8);
return;
@ -158,8 +143,8 @@ fn draw_tile(tile: u32, x: u32, y: u32) void {
y,
8,
8,
@intCast(u32, tileX) * 8,
@intCast(u32, tileY) * 8,
tileX * 8,
tileY * 8,
sprites.kenney_rpg_width,
w4.BlitFlags{ .two_bits = true },
),
@ -169,8 +154,8 @@ fn draw_tile(tile: u32, x: u32, y: u32) void {
y,
8,
8,
@intCast(u32, tileX) * 8,
@intCast(u32, tileY) * 8,
tileX * 8,
tileY * 8,
sprites.dungeon_width,
w4.BlitFlags{ .two_bits = true },
),
@ -197,16 +182,16 @@ fn move_mara(gamepad: w4.GamePad) void {
switch (mara_direction) {
.Up => {
mara_box.base.y -= mara_speed;
camera.x -= mara_speed;
},
.Down => {
mara_box.base.y += mara_speed;
camera.x += mara_speed;
},
.Left => {
mara_box.base.x -= mara_speed;
camera.y -= mara_speed;
},
.Right => {
mara_box.base.x += mara_speed;
camera.y += mara_speed;
},
}
}
@ -232,7 +217,7 @@ fn title() !void {
}
const story = "After defeating\nthe evil mage,\nMalto was at\npeace.\n\nOne day Mara was\nwalking along the\nbeach when she saw\nthe killing stone\nwas split in two.\nTamamo-no-Mae was\nfree to wreak havoc\nacross the land.\n\nHelp us again Mara!\nSave Kanar from\nTamamo-no-Mae!\n\nPress x.";
var story_idx: usize = 0;
var story_idx: u16 = 0;
var story_counter: u8 = 4;
fn storydump() !void {
@ -378,7 +363,7 @@ fn draw_mara() void {
flags.flip_x = true;
}
if (frame_count % 32 == 0 and mara_speed > 0) {
if (frame_count % 16 == 0 and mara_speed > 0) {
mara_frame = !mara_frame;
}
@ -390,12 +375,12 @@ fn draw_mara() void {
};
var step: u32 = if (mara_frame) 1 else 0;
w4.blitSub(&sprites.Mara, @intCast(u32, mara_box.base.x), @intCast(u32, mara_box.base.y), 16, 16, 16 * (frame + step), 0, 96, flags);
w4.blitSub(&sprites.Mara, mara_box.base.x, mara_box.base.y, 16, 16, 16 * (frame + step), 0, 96, flags);
}
pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace) noreturn {
_ = st;
w4.trace("!!! PANIC !!!");
w4.trace(msg);
while (true) {}
unreachable;
}

View File

@ -10,8 +10,14 @@ const Rect = sh0rk.Rect;
pub const width = 20;
pub const height = 20;
pub fn get_tile(x: u32, y: u32) u9 {
return data[x * height + y];
pub fn get_tile(x: i32, y: i32) u9 {
if (x < 0 or y < 0) {
return 0;
}
if (x >= width or y >= height) {
return 0;
}
return data[@intCast(u32, x) * height + @intCast(u32, y)];
}
pub const tileset: Tileset = .Dungeon;
pub const ts_width = 14;

View File

@ -10,8 +10,14 @@ const Rect = sh0rk.Rect;
pub const width = 40;
pub const height = 40;
pub fn get_tile(x: u32, y: u32) u9 {
return data[x * height + y];
pub fn get_tile(x: i32, y: i32) u9 {
if (x < 0 or y < 0) {
return 0;
}
if (x >= width or y >= height) {
return 0;
}
return data[@intCast(u32, x) * height + @intCast(u32, y)];
}
pub const tileset: Tileset = .Rpg;
pub const ts_width = 34;

View File

@ -24,8 +24,14 @@ out += `const Rect = sh0rk.Rect;\n\n`;
out += `pub const width = ${map.width};\n`;
out += `pub const height = ${map.height};\n\n`;
out += `pub fn get_tile(x: u32, y: u32) u9 {
return data[x * height + y];
out += `pub fn get_tile(x: i32, y: i32) u9 {
if (x < 0 or y < 0) {
return 0;
}
if (x >= width or y >= height) {
return 0;
}
return data[@intCast(u32, x) * height + @intCast(u32, y)];
}
`;

View File

@ -116,8 +116,8 @@ pub const SystemFlags = packed struct {
//
const raw_api = struct {
extern fn blit(sprite: [*]const u8, x: u32, y: u32, width: u32, height: u32, flags: u32) void;
extern fn blitSub(sprite: [*]const u8, x: u32, y: u32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: u32) void;
extern fn blit(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, flags: u32) void;
extern fn blitSub(sprite: [*]const u8, x: i32, y: i32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: u32) void;
extern fn tone(frequency: u32, duration: u32, volume: u32, flags: u32) void;
};
@ -137,7 +137,7 @@ pub const BlitFlags = packed struct {
};
/// Copies pixels to the framebuffer.
pub fn blit(sprite: []const u8, x: u32, y: u32, width: u32, height: u32, flags: BlitFlags) void {
pub fn blit(sprite: []const u8, x: i32, y: i32, width: u32, height: u32, flags: BlitFlags) void {
raw_api.blit(sprite.ptr, x, y, width, height, @bitCast(u32, flags));
}
@ -145,24 +145,24 @@ pub fn blit(sprite: []const u8, x: u32, y: u32, width: u32, height: u32, flags:
/// srcX: Source X position of the sprite region.
/// srcY: Source Y position of the sprite region.
/// stride: Total width of the overall sprite atlas. This is typically larger than width.
pub fn blitSub(sprite: []const u8, x: u32, y: u32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: BlitFlags) void {
pub fn blitSub(sprite: []const u8, x: i32, y: i32, width: u32, height: u32, src_x: u32, src_y: u32, stride: u32, flags: BlitFlags) void {
raw_api.blitSub(sprite.ptr, x, y, width, height, src_x, src_y, stride, @bitCast(u32, flags));
}
/// Draws a line between two points.
pub extern fn line(x1: u32, y1: u32, x2: u32, y2: u32) void;
pub extern fn line(x1: i32, y1: i32, x2: i32, y2: i32) void;
/// Draws an oval (or circle).
pub extern fn oval(x: u32, y: u32, width: u32, height: u32) void;
pub extern fn oval(x: i32, y: i32, width: u32, height: u32) void;
/// Draws a rectangle.
pub extern fn rect(x: u32, y: u32, width: u32, height: u32) void;
pub extern fn rect(x: i32, y: i32, width: u32, height: u32) void;
/// Draws text using the built-in system font.
pub fn text(str: []const u8, x: u32, y: u32) void {
pub fn text(str: []const u8, x: i32, y: i32) void {
textUtf8(str.ptr, str.len, x, y);
}
extern fn textUtf8(strPtr: [*]const u8, strLen: usize, x: u32, y: u32) void;
extern fn textUtf8(strPtr: [*]const u8, strLen: usize, x: i32, y: i32) void;
/// Draws a vertical line
pub extern fn vline(x: u32, y: u32, len: u32) void;