~alva/bee

d10f31513ba055f061ebd4c7959fe1d79f28907c — Alva 4 years ago d6be878
There is a blue bird now
5 files changed, 108 insertions(+), 93 deletions(-)

A assets/bg1.png
A assets/bluebird32x32bobbin.png
M src/assets.zig
M src/main.zig
M src/objects.zig
A assets/bg1.png => assets/bg1.png +0 -0
A assets/bluebird32x32bobbin.png => assets/bluebird32x32bobbin.png +0 -0
M src/assets.zig => src/assets.zig +3 -0
@@ 2,3 2,6 @@ pub const bee = @embedFile("../assets/bee-sheet.png");
pub const bullet = @embedFile("../assets/bullet.png");
pub const flower = @embedFile("../assets/flower-sheet.png");
pub const tooth_flower = @embedFile("../assets/tandblomma64x64prototype.png");
pub const blue_bird = @embedFile("../assets/bluebird32x32bobbin.png");

pub const bg1 = @embedFile("../assets/bg1.png");

M src/main.zig => src/main.zig +52 -18
@@ 6,9 6,9 @@ usingnamespace @import("objects.zig");
const assert = std.debug.assert;
const warn = std.debug.warn;

const resolution = Vec2{256.0, 224.0};
const scale = 3.0;
const fps = 60.0;
const resolution = [2]u16{256, 224};
const scale = 3;
const fps = 60;

pub fn main() !void {
    try initSdl();


@@ 30,6 30,7 @@ pub fn main() !void {
    stage.textures[0] = try createSdlTexture(renderer, assets.bee);
    stage.textures[1] = try createSdlTexture(renderer, assets.flower);
    stage.textures[2] = try createSdlTexture(renderer, assets.tooth_flower);
    stage.textures[3] = try createSdlTexture(renderer, assets.blue_bird);

//    _ = c.SDL_ShowCursor(0);
    stage.sprites[0] = Sprite{


@@ 59,6 60,15 @@ pub fn main() !void {
        .frame_offset = 0,
    };

    stage.sprites[3] = Sprite{
        .attributes = 3,
        .texture = 3,
        .frames = 4,
        .frame_counter = 0,
        .animation_speed = 6,
        .frame_offset = 0,
    };

//    var dmode: c.SDL_DisplayMode = undefined;
//    if (c.SDL_GetCurrentDisplayMode(0, &dmode) != 0)
//        return error.SdlInitFailed;


@@ 67,31 77,48 @@ pub fn main() !void {

    _ = c.SDL_SetRenderDrawColor(renderer, 0, 0, 0, c.SDL_ALPHA_OPAQUE);
    _ = c.SDL_RenderClear(renderer);
    _ = c.SDL_SetRenderDrawColor(renderer, 32, 64, 128, c.SDL_ALPHA_OPAQUE);
    _ = c.SDL_SetRenderDrawColor(renderer, 32, 32, 32, c.SDL_ALPHA_OPAQUE);
    _ = c.SDL_RenderFillRect(renderer, null);

    const pf = @intToFloat(f64, c.SDL_GetPerformanceFrequency());
    stage.tick_last = @intToFloat(f64, c.SDL_GetPerformanceCounter());
    const pf = c.SDL_GetPerformanceFrequency();
    warn("pf {}\n", .{pf});
    stage.tick_last = c.SDL_GetPerformanceCounter();

    stage.attributes[0] = Attributes.init(Size.Sixteen);
    stage.attributes[0].position = .{0.0, resolution[1] / 2.0 - Size.Sixteen.toFloat() / 2.0};
    stage.attributes[0] = Attributes.init(16);
    stage.attributes[0].position = .{.x = 0.0, .y = resolution[1] / 2 - 8};

    stage.attributes[1] = Attributes.init(Size.Sixteen);
    stage.attributes[1].position = .{resolution[1] / 2.0 + 8.0, resolution[1] - Size.Sixteen.toFloat()};
    stage.attributes[1] = Attributes.init(16);
    stage.attributes[1].position = .{.x = resolution[1] / 2 + 8, .y = resolution[1] - 16 - 16};
    stage.attributes[1].texture = 1;

    stage.attributes[2] = Attributes.init(Size.Sixtyfour);
    stage.attributes[2].position = .{resolution[1] - 32.0, resolution[1] - Size.Sixtyfour.toFloat() + 2};
    stage.attributes[2] = Attributes.init(64);
    stage.attributes[2].position = .{.x = resolution[1] - 32, .y = resolution[1] - 64 - 16 + 2};
    stage.attributes[2].texture = 2;
    stage.attributes[2].speed = .{-4.0, 0.0};
    stage.attributes[2].speed = .{.x = -1, .y = 0};

    stage.attributes[3] = Attributes.init(32);
    stage.attributes[3].position = .{.x = resolution[1] / 2 + 16, .y = resolution[1] / 2 - 16};
    stage.attributes[3].texture = 3;
    stage.attributes[3].speed = .{.x = -5.0, .y = 0};

    var event: c.SDL_Event = undefined;

    const bg1t = try createSdlTexture(renderer, assets.bg1);

    _ = c.SDL_RenderCopy(renderer, bg1t,
        null,
        &c.SDL_Rect{
            .x = 0,
            .y = 0,
            .w = resolution[0],
            .h = resolution[1],
        });
    
    loop: while (true) {
        stage.tick_now = @intToFloat(f64, c.SDL_GetPerformanceCounter());
        const delta = (stage.tick_now - stage.tick_last) / pf;
        stage.tick_now = c.SDL_GetPerformanceCounter();
        const delta = stage.tick_now - stage.tick_last;

        if (delta < 1.0 / fps) {
        if (delta < pf / 60 + 1) {
//            std.time.sleep(@floatToInt(u64, (1.0 / fps - delta) * 1000000000.0));
            continue;
        }


@@ 115,8 142,15 @@ pub fn main() !void {
            }
        }

        _ = c.SDL_RenderFillRect(renderer, null);
        stage.update(delta);
        _ = c.SDL_RenderCopy(renderer, bg1t,
            null,
            &c.SDL_Rect{
                .x = 0,
                .y = 0,
                .w = resolution[0],
                .h = resolution[1],
            });
        stage.update(@intToFloat(f32, delta) / @intToFloat(f32, pf));
        stage.render(renderer);
        c.SDL_RenderPresent(renderer);
    }

M src/objects.zig => src/objects.zig +53 -75
@@ 5,75 5,52 @@ const max = math.max;
const min = math.min;
const warn = std.debug.warn;

pub const Vec2 = @Vector(2, f64);
pub const Vec3 = @Vector(3, f64);
pub const Vec4 = @Vector(4, f64);

pub var stage: Stage = undefined;

pub const AxisAlignedBoundingBox = struct {
    center: Vec2,
    half_size: Vec2,
}; 

pub const Size = enum {
    Four,
    Eight,
    Sixteen,
    Thirtytwo,
    Sixtyfour,
    const Self = @This();

    pub fn toInt(self: Self) u8 {
        return switch (self) {
            .Four => 4,
            .Eight => 8,
            .Sixteen => 16,
            .Thirtytwo => 32,
            .Sixtyfour => 64,
        };
    }
pub fn Point(comptime T: type) type {
    return struct {
        x: T,
        y: T,
    };
}

pub const Rect = struct {
    width: u8,
    height: u8,
};

    pub fn toFloat(self: Self) f64 {
        return @intToFloat(f64, self.toInt());
    }
pub const HitBox = struct {
    offset: Point(u8),
    rect: Rect,
};

pub var stage: Stage = undefined;

pub const Attributes = struct {
    enabled: bool,
    size: u8,
    position: Vec2,
    speed: Vec2,
    position: Point(f32),
    speed: Point(f32),
    direction: u8,
    texture: u8,
    hit_box: HitBox,
    const Self = @This();

    pub fn init(size: Size) Self {
    pub fn init(size: u8) Self {
        return Self{
            .enabled = true,
            .size = size.toInt(),
            .position = .{0.0, 0.0},
            .speed = .{0.0, 0.0},
            .size = size,
            .position = .{.x = 0, .y = 0},
            .speed = .{.x = 0, .y = 0},
            .direction = 0,
            .texture = 0,
            .hit_box = undefined,
        };
    }

    pub fn overlaps(self: *Self, other: *Self) bool {
        return if (self.overlapsCrude(other))
            self.overlapsPrecise(other)
            else false;
    }

    fn overlapsCrude(self: *Self, other: *Self) bool {
        return (self.position[0] < other.position[0] + @intToFloat(f64, other.size)
            and other.position[0] < self.position[0] + @intToFloat(f64, self.size)
            and self.position[1] < other.position[1] + @intToFloat(f64, other.size)
            and other.position[1] < self.position[1] + @intToFloat(f64, self.size));
    }

    fn overlapsPrecise(self: *Self, other: *Attributes) bool {
        return true;
        return (self.position.x < other.position.x + @intToFloat(f32, other.size)
            and other.position.x < self.position.x + @intToFloat(f32, self.size)
            and self.position.y < other.position.y + @intToFloat(f32, other.size)
            and other.position.y < self.position.y + @intToFloat(f32, self.size));
    }
};



@@ 86,12 63,12 @@ pub const Sprite = struct {
    frame_offset: u16,
    const Self = @This();

    fn update(self: *Self, delta: f64) void {
    fn update(self: *Self, delta: f32) void {
        const a = &stage.attributes[self.attributes];
//        warn("Speed {}\n", .{mo.speed});
        self.updateAnimation();
        a.position[0] += a.speed[0] * delta;
        a.position[1] += a.speed[1] * delta;
        a.position.x += a.speed.x * delta;
        a.position.y += a.speed.y * delta;
    }

    fn updateAnimation(self: *Self) void {


@@ 143,8 120,8 @@ pub const Direction = enum(u8) {
};

pub const Stage = struct {
    tick_now: f64,
    tick_last: f64,
    tick_now: u64,
    tick_last: u64,
    attributes: [64]Attributes,
    textures: [32]?*c.SDL_Texture,
    sprites: [32]Sprite,


@@ 163,6 140,7 @@ pub const Stage = struct {
                    .speed = undefined,
                    .direction = undefined,
                    .texture = undefined,
                    .hit_box = undefined,
                }
            } ** 64,
            .textures = [_]?*c.SDL_Texture{null} ** 32,


@@ 186,7 164,7 @@ pub const Stage = struct {
        }
    }

    pub fn update(self: *Self, delta: f64) void {
    pub fn update(self: *Self, delta: f32) void {
        self.updatePlayer(delta);
        for (self.sprites) |*s| {
            const a = &self.attributes[s.attributes];


@@ 196,38 174,38 @@ pub const Stage = struct {
        }
    }

    fn updatePlayer(self: *Self, delta: f64) void {
        const acc = 7.5;
    fn updatePlayer(self: *Self, delta: f32) void {
        const acc = 6.5;
        const dec = 2.5;
        const smax = 60.0;
        const smax = 60;
        const a = &self.attributes[0];

        if (a.direction & @enumToInt(Direction.Up) != 0)
            a.speed[1] = max(a.speed[1] - acc, -smax);
            a.speed.y = max(a.speed.y - acc, -smax);

        if (a.direction & @enumToInt(Direction.Down) != 0)
            a.speed[1] = min(a.speed[1] + acc, smax);
            a.speed.y = min(a.speed.y + acc, smax);

        if (a.direction & @enumToInt(Direction.Left) != 0)
            a.speed[0] = max(a.speed[0] - acc, -smax);
            a.speed.x = max(a.speed.x - acc, -smax);

        if (a.direction & @enumToInt(Direction.Right) != 0)
            a.speed[0] = min(a.speed[0] + acc, smax);
            a.speed.x = min(a.speed.x + acc, smax);

        if (a.direction &
                (@enumToInt(Direction.Up) | @enumToInt(Direction.Down)) == 0) {
            if (a.speed[1] < 0.0) {
                a.speed[1] = min(a.speed[1] + dec, 0.0);
            } else if (0.0 < a.speed[1])
                a.speed[1] = max(a.speed[1] - dec, 0.0);
            if (a.speed.y < 0) {
                a.speed.y = min(a.speed.y + dec, 0);
            } else if (0 < a.speed.y)
                a.speed.y = max(a.speed.y - dec, 0);
        }

        if (a.direction &
                (@enumToInt(Direction.Right) | @enumToInt(Direction.Left)) == 0) {
            if (a.speed[0] < 0.0) {
                a.speed[0] = min(a.speed[0] + dec, 0.0);
            } else if (0.0 < a.speed[0])
                a.speed[0] = max(a.speed[0] - dec, 0.0);
            if (a.speed.x < 0) {
                a.speed.x = min(a.speed.x + dec, 0);
            } else if (0 < a.speed.x)
                a.speed.x = max(a.speed.x - dec, 0);
        }
    }



@@ 241,14 219,14 @@ pub const Stage = struct {
//            warn("txtidx: {}\n", .{a.texture});
            _ = c.SDL_RenderCopy(renderer, stage.textures[a.texture],
                &c.SDL_Rect{
                    .x = s.frame_offset * a.size,
                    .x = @intCast(c_int, s.frame_offset * a.size),
                    .y = 0,
                    .w = a.size,
                    .h = a.size,
                },
                &c.SDL_Rect{
                    .x = @floatToInt(c_int, a.position[0]),
                    .y = @floatToInt(c_int, a.position[1]),
                    .x = @floatToInt(c_int, a.position.x),
                    .y = @floatToInt(c_int, a.position.y),
                    .w = a.size,
                    .h = a.size,
                });