From d68d3b2782f5042f879b2449e5b2790dba48bf5c Mon Sep 17 00:00:00 2001 From: Ralph Caraveo Date: Sun, 3 Nov 2024 17:16:31 -0800 Subject: [PATCH] Prototype of scaled background, a dungeon twice as large. Additionally, working on background animation so the field doesn't fill like it's simply floating --- zsrc/audio.zig | 2 ++ zsrc/game.zig | 3 ++- zsrc/render.zig | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ zsrc/res.zig | 8 +++++-- zsrc/ui.zig | 2 +- 5 files changed, 68 insertions(+), 4 deletions(-) diff --git a/zsrc/audio.zig b/zsrc/audio.zig index eb26b96..f55379a 100644 --- a/zsrc/audio.zig +++ b/zsrc/audio.zig @@ -48,6 +48,8 @@ pub fn stopBgm() void { nowBgmId = null; } +/// randomBgm selects a random background music track from 1.. because track +/// 0 is reserved for the game menu. pub fn randomBgm() void { const r: usize = @intCast(hlp.randInt(1, res.bgmsPath.len - 1)); playBgm(r); diff --git a/zsrc/game.zig b/zsrc/game.zig index 7f04af8..fcbc708 100644 --- a/zsrc/game.zig +++ b/zsrc/game.zig @@ -1175,7 +1175,8 @@ fn initGame(localPlayers: c_int, remotePlayers: c_int, localFirst: bool) !void { } try ren.initInfo(); // create map - mp.initRandomMap(0.7, 7, GAME_TRAP_RATE); + //mp.initRandomMap(0.7, 7, GAME_TRAP_RATE); // Original + mp.initRandomMap(0.6, 3, GAME_TRAP_RATE); clearItemMap(); diff --git a/zsrc/render.zig b/zsrc/render.zig index 4efb5dc..a855e8e 100644 --- a/zsrc/render.zig +++ b/zsrc/render.zig @@ -34,6 +34,9 @@ const hlp = @import("helper.zig"); const gAllocator = @import("alloc.zig").gAllocator; pub const ANIMATION_LINK_LIST_NUM = 16; + +/// NOTE: these render list act as layers where MAP_ID is drawn first. +/// and UI_ID is drawn last (on top). pub const RENDER_LIST_MAP_ID = 0; pub const RENDER_LIST_MAP_SPECIAL_ID = 1; pub const RENDER_LIST_MAP_ITEMS_ID = 2; @@ -42,6 +45,7 @@ pub const RENDER_LIST_SPRITE_ID = 4; pub const RENDER_LIST_EFFECT_ID = 5; pub const RENDER_LIST_MAP_FOREWALL = 6; pub const RENDER_LIST_UI_ID = 7; + pub const RENDER_BUFFER_SIZE = 1 << 16; pub const RENDER_HP_BAR_HEIGHT = 3; pub const RENDER_HP_BAR_WIDTH = 20; @@ -664,10 +668,63 @@ fn renderFps() void { _ = renderCenteredText(&res.texts[res.textList.len + fpsUsize], 300, 10, 1); } +const star = struct { + speed: c_int, + scale: c_int, + frame: c_int, +}; + +const fieldSize = 1000; +var starSpeeds: [fieldSize]star = undefined; +var starPts: [fieldSize]c.SDL_Point = undefined; + +var starFieldInited: bool = false; + +fn renderStarField() void { + if (!starFieldInited) { + var initial_value: [fieldSize]star = undefined; + for (&initial_value, 0..) |*st, idx| { + st.* = star{ + .speed = hlp.randInt(1, 2), + .scale = hlp.randInt(2, 3), + .frame = hlp.randInt(0, 5), + }; + starPts[idx].x = hlp.randInt(0, res.SCREEN_WIDTH * res.SCREEN_FACTOR); + starPts[idx].y = hlp.randInt(0, res.SCREEN_WIDTH * res.SCREEN_FACTOR); + } + starSpeeds = initial_value; + starFieldInited = true; + } + + // Draw star field. + for (&starPts, 0..) |*pt, idx| { + const txt = res.textures[176]; // Shine + const frame = starSpeeds[idx].frame; + const src: c.SDL_Rect = txt.crops[@intCast(frame)]; + const dst: c.SDL_Rect = .{ .x = pt.x, .y = pt.y, .w = 32 * starSpeeds[idx].scale, .h = 32 * starSpeeds[idx].scale }; + + _ = c.SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + _ = c.SDL_RenderCopy(renderer, txt.origin, &src, &dst); + + if (pt.x > (res.SCREEN_WIDTH * res.SCREEN_FACTOR)) { + pt.x = 0; + } + + pt.x += starSpeeds[idx].speed; + if (hlp.randDouble() < 0.1) starSpeeds[idx].frame += 1; + + if (starSpeeds[idx].frame > 5) { + starSpeeds[idx].frame = 0; + } + } +} + pub fn render() !void { _ = c.SDL_SetRenderDrawColor(renderer, 25, 17, 23, 255); _ = c.SDL_RenderClear(renderer); + renderStarField(); + for (0..ANIMATION_LINK_LIST_NUM) |i| { updateAnimationLinkList(&animationsList[i]); if (i == RENDER_LIST_SPRITE_ID) { diff --git a/zsrc/res.zig b/zsrc/res.zig index 2a12800..0c2270b 100644 --- a/zsrc/res.zig +++ b/zsrc/res.zig @@ -220,6 +220,7 @@ pub const SPRITE_GREEN_HOOD_SKEL = 21; // Audio pub const AUDIO_BGM_SIZE = 16; pub const AUDIO_SOUND_SIZE = 256; + pub const AUDIO_WIN = 0; pub const AUDIO_LOSE = 1; pub const AUDIO_POWERLOSS = 2; @@ -260,8 +261,11 @@ pub const AUDIO_BOW_HIT = 35; // End Resource ID pub const UNIT = 32; -pub const SCREEN_WIDTH = 1440; -pub const SCREEN_HEIGHT = 960; + +// Suprisingly, the game works just as well when the SCREEN_WIDTH/HEIGHT are multiplied by 2. +pub const SCREEN_FACTOR = 2; +pub const SCREEN_WIDTH = 1440 * SCREEN_FACTOR; +pub const SCREEN_HEIGHT = 960 * SCREEN_FACTOR; pub const n = SCREEN_WIDTH / UNIT; pub const m = SCREEN_HEIGHT / UNIT; diff --git a/zsrc/ui.zig b/zsrc/ui.zig index 787629c..aedda4a 100644 --- a/zsrc/ui.zig +++ b/zsrc/ui.zig @@ -137,7 +137,7 @@ fn chooseOptions(optionsNum: c_int, options: []const *tps.Text) !c_int { // Wedge in Zig-Edition // by @deckarep text. - _ = ren.renderCenteredText(&res.texts[17], res.SCREEN_WIDTH / 2, 920, 1); + _ = ren.renderCenteredText(&res.texts[17], res.SCREEN_WIDTH / 2, 920 * res.SCREEN_FACTOR, 1); // Update Screen c.SDL_RenderPresent(ren.renderer);