Skip to content

Commit

Permalink
Merge pull request #39 from Tekkunsan/zig-casting-changes
Browse files Browse the repository at this point in the history
Use `zig fmt` to fix the casting changes in the latest zig version
  • Loading branch information
floooh authored Jul 1, 2023
2 parents 4616f66 + b3632dd commit 0c30d0a
Show file tree
Hide file tree
Showing 29 changed files with 716 additions and 763 deletions.
54 changes: 29 additions & 25 deletions src/examples/blend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// Test/demonstrate blend modes.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/blend.glsl.zig");
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/blend.glsl.zig");

const NUM_BLEND_FACTORS = 15;

Expand All @@ -34,13 +34,13 @@ export fn init() void {
state.pass_action.stencil.action = .DONTCARE;

state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32 {
// pos color
-1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.5,
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5,
-1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.5
})
.data = sg.asRange(&[_]f32{
// pos color
-1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 0.5,
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.5,
-1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.5,
1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.5,
}),
});

// pipeline object for rendering the background
Expand All @@ -56,7 +56,7 @@ export fn init() void {
pip_desc = .{
.shader = sg.makeShader(shd.quadShaderDesc(sg.queryBackend())),
.primitive_type = .TRIANGLE_STRIP,
.blend_color = .{ .r=1.0, .g=0.0, .b=0.0, .a=1.0 }
.blend_color = .{ .r = 1.0, .g = 0.0, .b = 0.0, .a = 1.0 },
};
pip_desc.layout.attrs[shd.ATTR_vs_quad_position].format = .FLOAT3;
pip_desc.layout.attrs[shd.ATTR_vs_quad_color0].format = .FLOAT4;
Expand All @@ -65,17 +65,19 @@ export fn init() void {
.src_factor_alpha = .ONE,
.dst_factor_alpha = .ZERO,
};
var src: usize = 0; while (src < NUM_BLEND_FACTORS): (src += 1) {
var dst: usize = 0; while (dst < NUM_BLEND_FACTORS): (dst += 1) {
pip_desc.colors[0].blend.src_factor_rgb = @intToEnum(sg.BlendFactor, src + 1);
pip_desc.colors[0].blend.dst_factor_rgb = @intToEnum(sg.BlendFactor, dst + 1);
var src: usize = 0;
while (src < NUM_BLEND_FACTORS) : (src += 1) {
var dst: usize = 0;
while (dst < NUM_BLEND_FACTORS) : (dst += 1) {
pip_desc.colors[0].blend.src_factor_rgb = @as(sg.BlendFactor, @enumFromInt(src + 1));
pip_desc.colors[0].blend.dst_factor_rgb = @as(sg.BlendFactor, @enumFromInt(dst + 1));
state.pip[src][dst] = sg.makePipeline(pip_desc);
}
}
}

export fn frame() void {
const time = @floatCast(f32, sapp.frameDuration()) * 60.0;
const time = @as(f32, @floatCast(sapp.frameDuration())) * 60.0;

sg.beginDefaultPass(state.pass_action, sapp.width(), sapp.height());

Expand All @@ -94,18 +96,20 @@ export fn frame() void {

state.r += 0.6 * time;
var r0 = state.r;
var src: usize = 0; while (src < NUM_BLEND_FACTORS): (src += 1) {
var dst: usize = 0; while (dst < NUM_BLEND_FACTORS): (dst += 1) {
var src: usize = 0;
while (src < NUM_BLEND_FACTORS) : (src += 1) {
var dst: usize = 0;
while (dst < NUM_BLEND_FACTORS) : (dst += 1) {
// compute model-view-proj matrix
const shift = NUM_BLEND_FACTORS / 2;
const t: vec3 = .{
.x = (@intToFloat(f32, dst) - shift) * 3.0,
.y = (@intToFloat(f32, src) - shift) * 2.2,
.z = 0.0
.x = (@as(f32, @floatFromInt(dst)) - shift) * 3.0,
.y = (@as(f32, @floatFromInt(src)) - shift) * 2.2,
.z = 0.0,
};
const model = mat4.mul(mat4.translate(t), mat4.rotate(r0, vec3.up()));
const quad_vs_params: shd.QuadVsParams = .{
.mvp = mat4.mul(view_proj, model)
.mvp = mat4.mul(view_proj, model),
};
sg.applyPipeline(state.pip[src][dst]);
sg.applyBindings(state.bind);
Expand Down
37 changes: 20 additions & 17 deletions src/examples/bufferoffsets.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
// buffer offsets.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const shd = @import("shaders/bufferoffsets.glsl.zig");
const shd = @import("shaders/bufferoffsets.glsl.zig");

const state = struct {
var pass_action: sg.PassAction = .{};
var pip: sg.Pipeline = .{};
var bind: sg.Bindings = .{};
};

// zig fmt: off
const Vertex = extern struct {
x: f32, y: f32,
r: f32, g: f32, b: f32
r: f32, g: f32, b: f32,
};
// zig fmt: on

export fn init() void {
sg.setup(.{
Expand All @@ -29,37 +31,38 @@ export fn init() void {
});

// clear to a blue-ish color
state.pass_action.colors[0] = .{ .action = .CLEAR, .value = .{ .r=0.5, .g=0.5, .b=1, .a=1 } };
state.pass_action.colors[0] = .{ .action = .CLEAR, .value = .{ .r = 0.5, .g = 0.5, .b = 1, .a = 1 } };

// a 2D triangle and quad in 1 vertex buffer and 1 index buffer
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]Vertex{
// triangle vertices
.{ .x= 0.0, .y= 0.55, .r=1.0, .g=0.0, .b=0.0 },
.{ .x= 0.25, .y= 0.05, .r=0.0, .g=1.0, .b=0.0 },
.{ .x=-0.25, .y= 0.05, .r=0.0, .g=0.0, .b=1.0 },
.{ .x = 0.0, .y = 0.55, .r = 1.0, .g = 0.0, .b = 0.0 },
.{ .x = 0.25, .y = 0.05, .r = 0.0, .g = 1.0, .b = 0.0 },
.{ .x = -0.25, .y = 0.05, .r = 0.0, .g = 0.0, .b = 1.0 },

// quad vertices
.{ .x=-0.25, .y=-0.05, .r=0.0, .g=0.0, .b=1.0 },
.{ .x= 0.25, .y=-0.05, .r=0.0, .g=1.0, .b=0.0 },
.{ .x= 0.25, .y=-0.55, .r=1.0, .g=0.0, .b=0.0 },
.{ .x=-0.25, .y=-0.55, .r=1.0, .g=1.0, .b=0.0 }
})
.{ .x = -0.25, .y = -0.05, .r = 0.0, .g = 0.0, .b = 1.0 },
.{ .x = 0.25, .y = -0.05, .r = 0.0, .g = 1.0, .b = 0.0 },
.{ .x = 0.25, .y = -0.55, .r = 1.0, .g = 0.0, .b = 0.0 },
.{ .x = -0.25, .y = -0.55, .r = 1.0, .g = 1.0, .b = 0.0 },
}),
});
state.bind.index_buffer = sg.makeBuffer(.{
.type = .INDEXBUFFER,
.data = sg.asRange(&[_]u16{
// triangle indices
0, 1, 2,
// quad indices
0, 1, 2, 0, 2, 3
})
0, 1, 2,
0, 2, 3,
}),
});

// a shader and pipeline object
var pip_desc: sg.PipelineDesc = .{
.shader = sg.makeShader(shd.bufferoffsetsShaderDesc(sg.queryBackend())),
.index_type = .UINT16
.index_type = .UINT16,
};
pip_desc.layout.attrs[shd.ATTR_vs_position].format = .FLOAT2;
pip_desc.layout.attrs[shd.ATTR_vs_color0].format = .FLOAT3;
Expand Down
14 changes: 8 additions & 6 deletions src/examples/clear.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// Just clear the framebuffer with an animated color.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const print = @import("std").debug.print;

Expand All @@ -15,10 +15,12 @@ var pass_action: sg.PassAction = .{};
export fn init() void {
sg.setup(.{
.context = sgapp.context(),
.logger = .{ .func = slog.func }
.logger = .{
.func = slog.func,
},
});
pass_action.colors[0] = .{ .action=.CLEAR, .value=.{ .r=1, .g=1, .b=0, .a=1 } };
print("Backend: {}\n", .{ sg.queryBackend()});
pass_action.colors[0] = .{ .action = .CLEAR, .value = .{ .r = 1, .g = 1, .b = 0, .a = 1 } };
print("Backend: {}\n", .{sg.queryBackend()});
}

export fn frame() void {
Expand Down
104 changes: 51 additions & 53 deletions src/examples/cube.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
// Shader with uniform data.
//------------------------------------------------------------------------------
const sokol = @import("sokol");
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const slog = sokol.log;
const sg = sokol.gfx;
const sapp = sokol.app;
const sgapp = sokol.app_gfx_glue;
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/cube.glsl.zig");
const vec3 = @import("math.zig").Vec3;
const mat4 = @import("math.zig").Mat4;
const shd = @import("shaders/cube.glsl.zig");

const state = struct {
var rx: f32 = 0.0;
var ry: f32 = 0.0;
var pip: sg.Pipeline = .{};
var bind: sg.Bindings = .{};
var pass_action: sg.PassAction = .{};
const view: mat4 = mat4.lookat(.{ .x=0.0, .y=1.5, .z=6.0 }, vec3.zero(), vec3.up());
const view: mat4 = mat4.lookat(.{ .x = 0.0, .y = 1.5, .z = 6.0 }, vec3.zero(), vec3.up());
};

export fn init() void {
Expand All @@ -29,51 +29,51 @@ export fn init() void {

// cube vertex buffer
state.bind.vertex_buffers[0] = sg.makeBuffer(.{
.data = sg.asRange(&[_]f32 {
.data = sg.asRange(&[_]f32{
// positions colors
-1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.0, 1.0,
-1.0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0,

-1.0, -1.0, -1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, 1.0, -1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.0, 1.0, 1.0,

1.0, -1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.5, 0.0, 1.0,
1.0, 1.0, 1.0, 1.0, 0.5, 0.0, 1.0,
1.0, -1.0, 1.0, 1.0, 0.5, 0.0, 1.0,

-1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,
-1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 1.0,
1.0, -1.0, -1.0, 0.0, 0.5, 1.0, 1.0,

-1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0,
-1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0,
}),
});

// cube index buffer
state.bind.index_buffer = sg.makeBuffer(.{
.type = .INDEXBUFFER,
.data = sg.asRange(&[_]u16{
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
})
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20,
}),
});

// shader and pipeline object
Expand All @@ -84,18 +84,18 @@ export fn init() void {
.compare = .LESS_EQUAL,
.write_enabled = true,
},
.cull_mode = .BACK
.cull_mode = .BACK,
};
pip_desc.layout.attrs[shd.ATTR_vs_position].format = .FLOAT3;
pip_desc.layout.attrs[shd.ATTR_vs_color0].format = .FLOAT4;
state.pip = sg.makePipeline(pip_desc);

// framebuffer clear color
state.pass_action.colors[0] = .{ .action=.CLEAR, .value = .{ .r=0.25, .g=0.5, .b=0.75, .a=1 } };
state.pass_action.colors[0] = .{ .action = .CLEAR, .value = .{ .r = 0.25, .g = 0.5, .b = 0.75, .a = 1 } };
}

export fn frame() void {
const dt = @floatCast(f32, sapp.frameDuration() * 60);
const dt = @as(f32, @floatCast(sapp.frameDuration() * 60));
state.rx += 1.0 * dt;
state.ry += 2.0 * dt;
const vs_params = computeVsParams(state.rx, state.ry);
Expand Down Expand Up @@ -128,12 +128,10 @@ pub fn main() void {
}

fn computeVsParams(rx: f32, ry: f32) shd.VsParams {
const rxm = mat4.rotate(rx, .{ .x=1.0, .y=0.0, .z=0.0 });
const rym = mat4.rotate(ry, .{ .x=0.0, .y=1.0, .z=0.0 });
const rxm = mat4.rotate(rx, .{ .x = 1.0, .y = 0.0, .z = 0.0 });
const rym = mat4.rotate(ry, .{ .x = 0.0, .y = 1.0, .z = 0.0 });
const model = mat4.mul(rxm, rym);
const aspect = sapp.widthf() / sapp.heightf();
const proj = mat4.persp(60.0, aspect, 0.01, 10.0);
return shd.VsParams {
.mvp = mat4.mul(mat4.mul(proj, state.view), model)
};
return shd.VsParams{ .mvp = mat4.mul(mat4.mul(proj, state.view), model) };
}
Loading

0 comments on commit 0c30d0a

Please sign in to comment.