Skip to content

Commit

Permalink
Prototype of scaled background, a dungeon twice as large. Additionall…
Browse files Browse the repository at this point in the history
…y, working on background animation so the field doesn't fill like it's simply floating
  • Loading branch information
deckarep committed Nov 4, 2024
1 parent be3d903 commit d68d3b2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
2 changes: 2 additions & 0 deletions zsrc/audio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion zsrc/game.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
57 changes: 57 additions & 0 deletions zsrc/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions zsrc/res.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion zsrc/ui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d68d3b2

Please sign in to comment.