~alva/bee

43075de5ec7fa347db71d60031ea5e664dc7b10a — Alva 4 years ago e840451
Add toothy flower; some code updates
3 files changed, 84 insertions(+), 43 deletions(-)

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

M src/main.zig => src/main.zig +14 -11
@@ 29,14 29,16 @@ 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.player = Player{
        .mo = 0,
        .attribute = 0,
    };
//    _ = c.SDL_ShowCursor(0);
    stage.attributes[0] = Attributes{
        .enabled = true,
        .position = .{0.0, resolution[1] / 2 - 8},
        .size = Size.Sixteen.toInt(),
        .position = .{0.0, resolution[1] / 2.0 - Size.Sixteen.toFloat() / 2.0},
        .speed = .{0.0, 0.0},
        .direction = 0,
        .texture = 0,


@@ 45,7 47,8 @@ pub fn main() !void {

    stage.attributes[1] = Attributes{
        .enabled = true,
        .position = .{resolution[1] / 2 + 8, resolution[1] - 16},
        .size = Size.Sixteen.toInt(),
        .position = .{resolution[1] / 2.0 + 8.0, resolution[1] - Size.Sixteen.toFloat()},
        .speed = .{0.0, 0.0},
        .direction = 0,
        .texture = 1,


@@ 54,17 57,14 @@ pub fn main() !void {

    stage.attributes[2] = Attributes{
        .enabled = true,
        .position = .{resolution[1] / 2 + 32, resolution[1] - 16},
        .size = Size.Sixtyfour.toInt(),
        .position = .{resolution[1] / 2.0 + 32.0, resolution[1] - Size.Sixtyfour.toFloat()},
        .speed = .{0.0, 0.0},
        .direction = 0,
        .texture = 1,
        .texture = 2,
        .whatever = undefined,
    };

    const pf = @intToFloat(f64, c.SDL_GetPerformanceFrequency());

    stage.tick_now = @intToFloat(f64, c.SDL_GetPerformanceCounter());

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


@@ 76,6 76,9 @@ pub fn main() !void {
    _ = c.SDL_SetRenderDrawColor(renderer, 32, 64, 128, c.SDL_ALPHA_OPAQUE);
    _ = c.SDL_RenderFillRect(renderer, null);

    const pf = @intToFloat(f64, c.SDL_GetPerformanceFrequency());
    stage.tick_now = @intToFloat(f64, c.SDL_GetPerformanceCounter());

    var event: c.SDL_Event = undefined;

    loop: while (true) {


@@ 83,7 86,7 @@ pub fn main() !void {
        const delta = (stage.tick_now - stage.tick_last) / pf;

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



@@ 113,7 116,7 @@ pub fn main() !void {
}

fn initSdl() !void {
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
    if (c.SDL_Init(c.SDL_INIT_EVERYTHING) != 0) {
//        warn("Unable to initialize SDL: {s}", .{c.SDL_GetError()});
        return error.SdlInitFailed;
    }

M src/objects.zig => src/objects.zig +69 -32
@@ 16,8 16,31 @@ pub const AxisAlignedBoundingBox = struct {
    half_size: Vec2,
}; 

pub const Size = enum {
    Four,
    Eight,
    Sixteen,
    Thirtytwo,
    Sixtyfour,

    pub fn toInt(   self: @This()) u8 {
        return switch (self) {
            .Four => 4,
            .Eight => 8,
            .Sixteen => 16,
            .Thirtytwo => 32,
            .Sixtyfour => 64,
        };
    }

    pub fn toFloat(self: @This()) f64 {
        return @intToFloat(f64, self.toInt());
    }
};

pub const Attributes = struct {
    enabled: bool,
    size: u8,
    position: Vec2,
    speed: Vec2,
    direction: u8,


@@ 34,49 57,49 @@ const Direction = enum(u8) {
};

pub const Player = struct {
    mo: u8,
    attribute: u8,
    const Self = @This();
    const acc = 7.5;
    const dec = 2.5;
    const smax = 60.0;

    fn update(self: *Self, delta: f64) void {
        const mo = &stage.attributes[self.mo];
        const a = &stage.attributes[self.attribute];

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

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

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

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

        if (mo.direction &
        if (a.direction &
                (@enumToInt(Direction.Up) | @enumToInt(Direction.Down)) == 0) {
            if (mo.speed[1] < 0.0) {
                mo.speed[1] = min(mo.speed[1] + dec, 0.0);
            } else if (0.0 < mo.speed[1])
                mo.speed[1] = max(mo.speed[1] - dec, 0.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 (mo.direction &
        if (a.direction &
                (@enumToInt(Direction.Right) | @enumToInt(Direction.Left)) == 0) {
            if (mo.speed[0] < 0.0) {
                mo.speed[0] = min(mo.speed[0] + dec, 0.0);
            } else if (0.0 < mo.speed[0])
                mo.speed[0] = max(mo.speed[0] - dec, 0.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);
        }

//        warn("Speed {}\n", .{mo.speed});
        mo.whatever +%= 32;
        a.whatever +%= 32;
    }

    fn handleKeyUp(self: *Self, sym: c.SDL_Keycode) void {
        const a = &stage.attributes[self.mo];
        const a = &stage.attributes[self.attribute];
        a.direction &= ~@enumToInt(switch (sym) {
            c.SDLK_UP, c.SDLK_w => Direction.Up,
            c.SDLK_DOWN, c.SDLK_s => Direction.Down,


@@ 92,7 115,7 @@ pub const Player = struct {
            return;
        }

        const a = &stage.attributes[self.mo];
        const a = &stage.attributes[self.attribute];
        a.direction |= @enumToInt(switch (sym) {
            c.SDLK_UP, c.SDLK_w => Direction.Up,
            c.SDLK_DOWN, c.SDLK_s => Direction.Down,


@@ 103,17 126,17 @@ pub const Player = struct {
    }

    fn render(self: *Self, renderer: *c.SDL_Renderer) void {
        const mo = &stage.attributes[self.mo];
        _ = c.SDL_RenderCopy(renderer, stage.textures[mo.texture],
        const a = &stage.attributes[self.attribute];
        _ = c.SDL_RenderCopy(renderer, stage.textures[a.texture],
            &c.SDL_Rect{
                .x = mo.whatever >> 7 << 4,
                .x = a.whatever >> 7 << 4,
                .y = 0,
                .w = 16,
                .h = 16
            },
            &c.SDL_Rect{
                .x = @floatToInt(c_int, mo.position[0]),
                .y = @floatToInt(c_int, mo.position[1]),
                .x = @floatToInt(c_int, a.position[0]),
                .y = @floatToInt(c_int, a.position[1]),
                .w = 16,
                .h = 16
            });


@@ 141,6 164,7 @@ pub const Stage = struct {
            .attributes = [_]Attributes{
                Attributes{
                    .enabled = false,
                    .size = undefined,
                    .position = undefined,
                    .speed = undefined,
                    .direction = undefined,


@@ 173,20 197,33 @@ pub const Stage = struct {
        for (self.attributes[1..]) |*a| {
            if (!a.enabled)
                continue;
            if (boundingBoxesOverlap(&self.attributes[0], a))
                warn("Boom!\n", .{});
//            warn("txtidx: {}\n", .{a.texture});
            _ = c.SDL_RenderCopy(renderer, stage.textures[a.texture],
                &c.SDL_Rect{
                    .x = 0,
                    .y = 0,
                    .w = 16,
                    .h = 16
                    .w = a.size,
                    .h = a.size,
                },
                &c.SDL_Rect{
                    .x = @floatToInt(c_int, a.position[0]),
                    .y = @floatToInt(c_int, a.position[1]),
                    .w = 16,
                    .h = 16
                    .w = a.size,
                    .h = a.size,
                });
        }
    }
};

fn boundingBoxesOverlap(as1: *Attributes, as2: *Attributes) bool {
    return (as1.position[0] < as2.position[0] + @intToFloat(f64, as2.size) - 2.0
        and as2.position[0] < as1.position[0] + @intToFloat(f64, as1.size) - 2.0
        and as1.position[1] < as2.position[1] + @intToFloat(f64, as2.size) - 2.0
        and as2.position[1] < as1.position[1] + @intToFloat(f64, as1.size) - 2.0);
}

fn pixelsOverlap(as1: *Attributes, as2: *Attributes) bool {
    return false;
}