Skip to content

Instantly share code, notes, and snippets.

@matjam
Created June 11, 2020 07:11
Show Gist options
  • Save matjam/e75eb701aadc566129e03fc6932152c2 to your computer and use it in GitHub Desktop.
Save matjam/e75eb701aadc566129e03fc6932152c2 to your computer and use it in GitHub Desktop.
const std = @import("std");
const c = @import("c.zig");
const vertexShaderSource =
\\#version 330 core
\\layout (location = 0) in vec3 aPos;
\\void main()
\\{
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
\\}
;
const fragmentShaderSource =
\\#version 330 core
\\out vec4 FragColor;
\\void main()
\\{
\\ FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
\\}
;
pub fn main() anyerror!void {
std.debug.warn("starting zixel\n", .{});
if (c.glfwInit() == c.GLFW_FALSE) {
std.debug.warn("failed to initialize glfw\n", .{});
return;
}
defer c.glfwTerminate();
c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MAJOR, 3);
c.glfwWindowHint(c.GLFW_CONTEXT_VERSION_MINOR, 3);
c.glfwWindowHint(c.GLFW_OPENGL_PROFILE, c.GLFW_OPENGL_CORE_PROFILE);
c.glfwWindowHint(c.GLFW_OPENGL_FORWARD_COMPAT, c.GL_TRUE);
var window = c.glfwCreateWindow(800, 600, "Hello zig", null, null);
if (window == null) {
std.debug.warn("failed to create glfw window\n", .{});
return;
}
// Make the window's context current
c.glfwMakeContextCurrent(window);
// Now, glad can be initialized.
if (c.gladLoadGLLoader(@ptrCast(c.GLADloadproc, c.glfwGetProcAddress)) == 0) {
std.debug.warn("unable to init glad\n", .{});
return;
}
c.glViewport(0, 0, 800, 600);
_ = c.glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
// Loop until the user closes the window
while (c.glfwWindowShouldClose(window) == 0) {
processInput(window);
// Clear then render
c.glClearColor(0.2, 0.3, 0.3, 1.0);
c.glClear(c.GL_COLOR_BUFFER_BIT);
// swap the buffers
c.glfwSwapBuffers(window);
// Poll for and process events
c.glfwPollEvents();
}
}
fn framebufferSizeCallback(window: ?*c.GLFWwindow, width: c_int, height: c_int) callconv(.C) void {
c.glViewport(0, 0, width, height);
}
fn processInput(window: ?*c.GLFWwindow) callconv(.C) void {
if (c.glfwGetKey(window, c.GLFW_KEY_ESCAPE) == c.GLFW_PRESS)
c.glfwSetWindowShouldClose(window, c.GL_TRUE);
}
fn compileShader(source: var, type: u64) !c.GLuint {
c.GLuint shader = c.glCreateShader(type);
c.glShaderSource(shader, 1, &source, NULL);
c.glCompileShader(shader);
// check for shader compile errors
var success = 0;
var infoLog: [512]u8 = undefined;
c.glGetShaderiv(shader, c.GL_COMPILE_STATUS, &success);
if (!success) {
c.glGetShaderInfoLog(shader, 512, null, infoLog);
std.debug.warn("error compiling shader: {}\n", .{infoLog});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment