Skip to content

Instantly share code, notes, and snippets.

@vassvik
Last active September 8, 2018 12:58
Show Gist options
  • Save vassvik/04e55bbad0a6a8f8daad3360749901e0 to your computer and use it in GitHub Desktop.
Save vassvik/04e55bbad0a6a8f8daad3360749901e0 to your computer and use it in GitHub Desktop.
minimal glfw, C vs Odin
#include <stdio.h> // fprintf, stderr
#include <GLFW/glfw3.h>
// this function will be called internally by GLFW whenever an error occur.
void error_callback(int error, const char* description);
int main()
{
// tell GLFW to call error_callback if an internal error ever occur at some point inside GLFW functions.
glfwSetErrorCallback(error_callback);
// initialize all the internal state of GLFW
if (!glfwInit()) {
fprintf(stderr, "Error initializing GLFW\n");
return -1;
}
// create the window
double resx = 640, resy = 480;
GLFWwindow *window = glfwCreateWindow(resx, resy, "Checkpoint 1: Creating a window.", NULL, NULL);
// check if the opening of the window failed whatever reason and clean up
if (!window) {
glfwTerminate();
return -2;
}
// in principle we can have multiple windows,
// so we set the newly created on as "current"
glfwMakeContextCurrent(window);
// Enable v-sync for now, if possible
glfwSwapInterval(1);
// main loop
while (!glfwWindowShouldClose(window)) {
// listen for events (keyboard, mouse, etc.). ignored for now, but useful later
glfwPollEvents();
// swap buffers (replace the old image with a new one)
// this won't have any visible effect until we add actual drawing
glfwSwapBuffers(window);
}
// clean up
glfwTerminate();
return 0;
}
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s (%d)\n", description, error);
}
package main
import "core:fmt"
import "shared:odin-glfw"
main :: proc() {
// tell GLFW to call error_callback if an internal error ever occur at some point inside GLFW functions.
error_callback :: proc"c"(error: i32, desc: cstring) {
fmt.printf("Error code %d:\n %s\n", error, desc);
}
glfw.SetErrorCallback(error_callback);
// initialize all the internal state of GLFW
if glfw.Init() == 0 {
fmt.println("Error initializing GLFW");
return;
}
defer glfw.Terminate();
// create the window
window := glfw.CreateWindow(640, 480, "Checkpoint 1: Creating a window.", nil, nil);
// check if the opening of the window failed whatever reason and clean up
if (window == nil) do return;
// in principle we can have multiple windows,
// so we set the newly created on as "current"
glfw.MakeContextCurrent(window);
// Enable v-sync for now, if possible
glfw.SwapInterval(1);
// main loop
for !glfw.WindowShouldClose(window) {
// listen for events (keyboard, mouse, etc.). ignored for now, but useful later
glfw.PollEvents();
// swap buffers (replace the old image with a new one)
// this won't have any visible effect until we add actual drawing
glfw.SwapBuffers(window);
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment