Skip to content

Instantly share code, notes, and snippets.

@silverweed
Last active September 24, 2017 14:27
Show Gist options
  • Save silverweed/343cbe57bd63e9c3edfc7b8749d26ef4 to your computer and use it in GitHub Desktop.
Save silverweed/343cbe57bd63e9c3edfc7b8749d26ef4 to your computer and use it in GitHub Desktop.
D + SFML + OpenGL
import std.stdio;
import derelict.sfml2.system;
import derelict.sfml2.window;
import derelict.opengl3.gl3;
bool running = true;
void main() {
// Load shared C libraries
DerelictGL3.load();
DerelictSFML2System.load("/usr/lib/x86_64-linux-gnu/libcsfml-system.so.2.4");
DerelictSFML2Window.load("/usr/lib/x86_64-linux-gnu/libcsfml-window.so.2.4");
// Create openGL context
sfContextSettings ctxSettings;
ctxSettings.majorVersion = 3;
ctxSettings.minorVersion = 3;
ctxSettings.attributeFlags = sfContextCore;
auto window = sfWindow_create(sfVideoMode(800, 600), "Test DSFML + DGL", sfResize|sfClose, &ctxSettings);
sfWindow_setActive(window, true);
DerelictGL3.reload();
//const shaderProgram = createShader();
// For unknown reasons, using a function fucks up the displayed result, so we use a mixin instead.
mixin(createShader!());
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
// D NOTE: these must be static arrays in order for `sizeof` to work properly!
immutable float[12] vertices = [
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
];
immutable uint[6] indices = [
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
];
uint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
scope (exit) {
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
}
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.sizeof, &vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.sizeof, &indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * float.sizeof, cast(void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
while (running) {
processInput(window);
glClearColor(0.2, 0.3, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, cast(void*)0);
sfWindow_display(window);
}
}
void processInput(sfWindow* window) {
sfEvent event;
while (sfWindow_pollEvent(window, &event)) {
switch (event.type) {
case sfEvtResized:
glViewport(0, 0, event.size.width, event.size.height);
break;
case sfEvtClosed:
running = false;
break;
case sfEvtKeyPressed:
switch (event.key.code) {
case sfKeyQ:
running = false;
break;
default:
break;
}
break;
default:
break;
}
}
}
template createShader() {
const char[] createShader = `
// build and compile our shader program
// ------------------------------------
static immutable char* vertexShaderSource = "
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}";
static immutable char* fragmentShaderSource = "
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}";
// vertex shader
auto vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, null);
glCompileShader(vertexShader);
// check for shader compile errors
int success;
char[512] infoLog;
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(vertexShader, 512, cast(int*)0, infoLog.ptr);
writeln("ERROR vertex shader compilation failed: \n", infoLog);
}
// fragment shader
auto fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, null);
glCompileShader(fragmentShader);
// check for shader compile errors
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if (!success) {
glGetShaderInfoLog(fragmentShader, 512, cast(int*)0, infoLog.ptr);
writeln("ERROR fragment shader compilation failed: \n", infoLog);
}
// link shaders
auto shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
// check for linking errors
if (!success) {
glGetShaderInfoLog(shaderProgram, 512, cast(int*)0, infoLog.ptr);
writeln("ERROR shader program linking failed: \n", infoLog);
}
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
`;
}
{
"name": "test",
"authors": [
"silverweed"
],
"description": "A minimal D application.",
"copyright": "Copyright © 2017, silverweed",
"license": "WTFPL",
"dependencies": {
"derelict-gl3": "*",
"derelict-sfml2": "*"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment