mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Minimal GL and GLFW bindings to render a triangle
This commit is contained in:
committed by
Christoffer Lerno
parent
a99e4b602a
commit
b071e24d7e
76
resources/examples/opengl/.gitignore
vendored
Normal file
76
resources/examples/opengl/.gitignore
vendored
Normal 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
|
||||
0
resources/examples/opengl/LICENSE
Normal file
0
resources/examples/opengl/LICENSE
Normal file
0
resources/examples/opengl/README.md
Normal file
0
resources/examples/opengl/README.md
Normal file
0
resources/examples/opengl/docs/.gitkeep
Normal file
0
resources/examples/opengl/docs/.gitkeep
Normal file
0
resources/examples/opengl/lib/.gitkeep
Normal file
0
resources/examples/opengl/lib/.gitkeep
Normal file
28
resources/examples/opengl/project.json
Normal file
28
resources/examples/opengl/project.json
Normal 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"
|
||||
}
|
||||
0
resources/examples/opengl/resources/.gitkeep
Normal file
0
resources/examples/opengl/resources/.gitkeep
Normal file
0
resources/examples/opengl/scripts/.gitkeep
Normal file
0
resources/examples/opengl/scripts/.gitkeep
Normal file
36
resources/examples/opengl/src/gl/gl.c3
Normal file
36
resources/examples/opengl/src/gl/gl.c3
Normal 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;
|
||||
64
resources/examples/opengl/src/glfw/glfw.c3
Normal file
64
resources/examples/opengl/src/glfw/glfw.c3
Normal 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;
|
||||
30
resources/examples/opengl/src/triangle.c3
Normal file
30
resources/examples/opengl/src/triangle.c3
Normal 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();
|
||||
}
|
||||
0
resources/examples/opengl/test/.gitkeep
Normal file
0
resources/examples/opengl/test/.gitkeep
Normal file
Reference in New Issue
Block a user