Minimal GL and GLFW bindings to render a triangle

This commit is contained in:
chopsticks-user
2024-09-26 13:05:07 -04:00
committed by Christoffer Lerno
parent a99e4b602a
commit b071e24d7e
12 changed files with 234 additions and 0 deletions

76
resources/examples/opengl/.gitignore vendored Normal file
View File

@@ -0,0 +1,76 @@
# Prerequisites
*.d
# Object files
*.o
*.ko
*.obj
*.elf
*.ll
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf
/build/
.idea/
/resources/grammar.tab.c
/resources/grammar.vcg
/resources/lex.yy.c
/resources/y.tab.c
/resources/y.tab.h
/bin/
#visual studio files
.vs/
.vscode/
out/
/cmake-build-debug/
/cmake-build-release/
# Emacs files
TAGS
# Clangd LSP files
/.cache/
/compile_commands.json

View File

View File

View File

View File

View File

@@ -0,0 +1,28 @@
{
"version": "0.1.0",
"authors": [
"John Doe <john.doe@example.com>"
],
"langrev": "1",
"warnings": [
"no-unused"
],
"sources": [
"./**"
],
"dependency-search-paths": [],
"dependencies": [],
"cc": "cc",
"c-sources": [],
"targets": {
"build/triangle": {
"type": "executable"
}
},
"linked-libraries": [
"glfw",
"GL"
],
"cpu": "generic",
"opt": "O0"
}

View File

@@ -0,0 +1,36 @@
module gl;
def BitField = int;
enum BufferBit : int(int value) {
COLOR = 0x00004000,
STENCIL = 0x00000400,
DEPTH = 0x00000100,
}
enum Primitive : int(int value) {
POINTS = 0,
LINES = 1,
LINE_LOOP = 2,
LINE_STRIP = 3,
TRIANGLES = 4,
TRIANGLE_STRIP = 5,
TRIANGLE_FAN = 6,
QUADS = 7,
QUAD_STRIP = 8,
POLYGON = 9,
}
extern fn void clear(BitField mask) @extern("glClear") @public;
extern fn void begin(BitField mask) @extern("glBegin") @public;
extern fn void end() @extern("glEnd") @public;
extern fn void flush() @extern("glFlush") @public;
extern fn void color3f(float r, float g, float b)
@extern("glColor3f") @public;
extern fn void vertex3f(float x, float y, float z)
@extern("glVertex3f") @public;

View File

@@ -0,0 +1,64 @@
module glfw;
import std::io;
distinct Window @public = _Window*;
distinct Monitor @public = _Monitor*;
fn void Window.create(
&self,
int width,
int height,
String title,
Monitor monitor = {},
Window share = {}
) @public {
*self = (Window)_glfwCreateWindow(
width,
height,
title,
(_Monitor*)monitor,
(_Window*)share
);
}
fn void Window.destroy(self) @public {
_glfwDestroyWindow((_Window*)self);
}
fn bool Window.shouldClose(self) @public {
return _glfwWindowShouldClose((_Window*)self);
}
fn void Window.swapBuffers(self) @public {
return _glfwSwapBuffers((_Window*)self);
}
fn void Window.makeContextCurrent(self) @public {
return _glfwMakeContextCurrent((_Window*)self);
}
extern fn void initialize() @extern("glfwInit") @public;
extern fn void terminate() @extern("glfwTerminate") @public;
extern fn void pollEvents() @extern("glfwPollEvents") @public;
distinct _Window @private = void;
distinct _Monitor @private = void;
extern fn _Window* _glfwCreateWindow(
int width, int height, char* title, _Monitor* monitor, _Window* share
) @extern("glfwCreateWindow") @private;
extern fn bool _glfwWindowShouldClose(_Window* window)
@extern("glfwWindowShouldClose") @private;
extern fn void _glfwDestroyWindow(_Window* window)
@extern("glfwDestroyWindow") @private;
extern fn void _glfwSwapBuffers(_Window* window)
@extern("glfwSwapBuffers") @private;
extern fn void _glfwMakeContextCurrent(_Window* window)
@extern("glfwMakeContextCurrent") @private;

View File

@@ -0,0 +1,30 @@
import std::io;
import glfw;
import gl;
fn void main() {
glfw::initialize();
Window window;
window.create(1280, 720, "Triangle example");
window.makeContextCurrent();
while(!window.shouldClose()) {
gl::clear(BufferBit.COLOR.value);
gl::begin(Primitive.TRIANGLES.value);
gl::color3f(1, 0, 0); gl::vertex3f(-0.6, -0.75, 0.5);
gl::color3f(0, 1, 0); gl::vertex3f(0.6, -0.75, 0);
gl::color3f(0, 0, 1); gl::vertex3f(0, 0.75, 0);
gl::end();
gl::flush();
window.swapBuffers();
glfw::pollEvents();
}
window.destroy();
glfw::terminate();
}

View File