Move rendering out of main
This commit is contained in:
parent
4883fc68e2
commit
d0f444fa13
|
@ -0,0 +1,97 @@
|
|||
const sdl = @import("sdl2");
|
||||
const gl = @import("zgl");
|
||||
|
||||
const Self = @This();
|
||||
|
||||
window: sdl.Window,
|
||||
context: sdl.gl.Context,
|
||||
color_loc: u32,
|
||||
buffer: gl.Buffer,
|
||||
|
||||
pub fn init() !Self {
|
||||
try sdl.init(.{ .video = true, .audio = true, .events = true });
|
||||
|
||||
// Set OpenGL version
|
||||
try sdl.gl.setAttribute(.{ .context_major_version = 3 });
|
||||
try sdl.gl.setAttribute(.{ .context_minor_version = 3 });
|
||||
|
||||
const window = try sdl.createWindow(
|
||||
"USG",
|
||||
.{ .centered = {} },
|
||||
.{ .centered = {} },
|
||||
1280,
|
||||
720,
|
||||
.{ .opengl = true, .shown = true }
|
||||
);
|
||||
|
||||
const ctx = try sdl.gl.createContext(window);
|
||||
|
||||
var color_loc: u32 = undefined;
|
||||
const program = gl.Program.create();
|
||||
{
|
||||
const vs = gl.Shader.create(.vertex);
|
||||
defer vs.delete();
|
||||
vs.source(1, &.{@embedFile("shaders/vector.vs")});
|
||||
vs.compile();
|
||||
const fs = gl.Shader.create(.fragment);
|
||||
defer fs.delete();
|
||||
fs.source(1, &.{@embedFile("shaders/fragment.fs")});
|
||||
fs.compile();
|
||||
program.attach(vs);
|
||||
defer program.detach(vs);
|
||||
program.attach(fs);
|
||||
defer program.detach(fs);
|
||||
program.link();
|
||||
color_loc = program.uniformLocation("color").?;
|
||||
}
|
||||
program.use();
|
||||
|
||||
var vertex_array = gl.VertexArray.gen();
|
||||
vertex_array.bind();
|
||||
|
||||
const vertex_buffer = [_]f32{
|
||||
-1.0, -1.0, 0.0,
|
||||
-1.0, 1.0, 0.0,
|
||||
0.0, 1.0, 0.0,
|
||||
1.0, 1.0, 0.0,
|
||||
0.0, -1.0, 0.0,
|
||||
1.0, -1.0, 0.0,
|
||||
};
|
||||
|
||||
const buf = gl.Buffer.gen();
|
||||
buf.bind(.array_buffer);
|
||||
buf.data(f32, &vertex_buffer, .static_draw);
|
||||
|
||||
gl.clearColor(0.91, 0.97, 1.00, 1.00);
|
||||
|
||||
return Self{
|
||||
.window = window,
|
||||
.context = ctx,
|
||||
.color_loc = color_loc,
|
||||
.buffer = buf,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn render(self: Self) void {
|
||||
gl.clear(.{ .color = true });
|
||||
|
||||
gl.enableVertexAttribArray(0);
|
||||
self.buffer.bind(.array_buffer);
|
||||
gl.vertexAttribPointer(0, 3, .float, false, 0, 0);
|
||||
|
||||
gl.uniform4f(self.color_loc, 0.45, 0.21, 0.70, 1);
|
||||
gl.drawArrays(.triangles, 0, 3);
|
||||
|
||||
gl.uniform4f(self.color_loc, 0.2, 0.5, 1.0, 1);
|
||||
gl.drawArrays(.triangles, 3, 3);
|
||||
|
||||
gl.disableVertexAttribArray(0);
|
||||
|
||||
sdl.gl.swapWindow(self.window);
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
sdl.quit();
|
||||
self.window.destroy();
|
||||
sdl.gl.deleteContext(self.context);
|
||||
}
|
79
src/main.zig
79
src/main.zig
|
@ -1,71 +1,15 @@
|
|||
const std = @import("std");
|
||||
const sdl = @import("sdl2");
|
||||
const gl = @import("zgl");
|
||||
|
||||
const Renderer = @import("Renderer.zig");
|
||||
//const Game = @import("Game.zig");
|
||||
//const MainMenu = @import("MainMenu.zig");
|
||||
|
||||
//const State = @import("flow.zig").State;
|
||||
|
||||
pub fn main() !void {
|
||||
try sdl.init(.{ .video = true, .audio = true, .events = true });
|
||||
defer sdl.quit();
|
||||
|
||||
// Set OpenGL version
|
||||
try sdl.gl.setAttribute(.{ .context_major_version = 3 });
|
||||
try sdl.gl.setAttribute(.{ .context_minor_version = 3 });
|
||||
|
||||
const window = try sdl.createWindow(
|
||||
"USG",
|
||||
.{ .centered = {} },
|
||||
.{ .centered = {} },
|
||||
1280,
|
||||
720,
|
||||
.{ .opengl = true, .shown = true }
|
||||
);
|
||||
defer window.destroy();
|
||||
|
||||
const ctx = try sdl.gl.createContext(window);
|
||||
defer sdl.gl.deleteContext(ctx);
|
||||
|
||||
var color_loc: u32 = undefined;
|
||||
const program = gl.Program.create();
|
||||
{
|
||||
const vs = gl.Shader.create(.vertex);
|
||||
defer vs.delete();
|
||||
vs.source(1, &.{@embedFile("shaders/vector.vs")});
|
||||
vs.compile();
|
||||
const fs = gl.Shader.create(.fragment);
|
||||
defer fs.delete();
|
||||
fs.source(1, &.{@embedFile("shaders/fragment.fs")});
|
||||
fs.compile();
|
||||
program.attach(vs);
|
||||
defer program.detach(vs);
|
||||
program.attach(fs);
|
||||
defer program.detach(fs);
|
||||
program.link();
|
||||
color_loc = program.uniformLocation("color").?;
|
||||
}
|
||||
program.use();
|
||||
|
||||
var vertex_array = gl.VertexArray.gen();
|
||||
vertex_array.bind();
|
||||
|
||||
const vertex_buffer = [_]f32{
|
||||
-1.0, -1.0, 0.0,
|
||||
-1.0, 1.0, 0.0,
|
||||
0.0, 1.0, 0.0,
|
||||
1.0, 1.0, 0.0,
|
||||
0.0, -1.0, 0.0,
|
||||
1.0, -1.0, 0.0,
|
||||
};
|
||||
|
||||
const buf = gl.Buffer.gen();
|
||||
buf.bind(.array_buffer);
|
||||
buf.data(f32, &vertex_buffer, .static_draw);
|
||||
|
||||
//var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return;
|
||||
//defer _ = SDL.SDL_DestroyRenderer(renderer);
|
||||
var renderer = try Renderer.init();
|
||||
defer renderer.deinit();
|
||||
|
||||
//var main_menu = MainMenu.init();
|
||||
//var game = Game.init(renderer);
|
||||
|
@ -74,8 +18,6 @@ pub fn main() !void {
|
|||
|
||||
//_ = SDL.IMG_Init(SDL.IMG_INIT_JPG);
|
||||
|
||||
gl.clearColor(0.91, 0.97, 1.00, 1.00);
|
||||
|
||||
mainLoop: while (true) {
|
||||
while (sdl.pollEvent()) |ev| {
|
||||
switch (ev) {
|
||||
|
@ -84,25 +26,12 @@ pub fn main() !void {
|
|||
}
|
||||
}
|
||||
|
||||
gl.clear(.{ .color = true });
|
||||
|
||||
gl.enableVertexAttribArray(0);
|
||||
buf.bind(.array_buffer);
|
||||
gl.vertexAttribPointer(0, 3, .float, false, 0, 0);
|
||||
|
||||
gl.uniform4f(color_loc, 0.45, 0.21, 0.70, 1);
|
||||
gl.drawArrays(.triangles, 0, 3);
|
||||
|
||||
gl.uniform4f(color_loc, 0.2, 0.5, 1.0, 1);
|
||||
gl.drawArrays(.triangles, 3, 3);
|
||||
|
||||
gl.disableVertexAttribArray(0);
|
||||
renderer.render();
|
||||
|
||||
//current_state = switch (current_state) {
|
||||
//.main_menu => main_menu.tick(),
|
||||
//.game => game.tick(),
|
||||
//};
|
||||
|
||||
sdl.gl.swapWindow(window);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue