Initial commit - some engine bugs stopping compiling

This commit is contained in:
Will
2026-03-29 15:52:42 +01:00
commit 3d573a200e
361 changed files with 332759 additions and 0 deletions

View File

@@ -0,0 +1,89 @@
#include <iostream>
// GLAD
#include <glad/gl.h>
// GLFW (include after glad)
#include <GLFW/glfw3.h>
// This example is taken from http://learnopengl.com/
// http://learnopengl.com/code_viewer.php?code=getting-started/hellowindow2
// The code originally used GLEW, I replaced it with Glad
// Compile:
// g++ example/c++/hellowindow2.cpp -Ibuild/include build/src/gl.c -lglfw -ldl
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
// Load OpenGL functions, gladLoadGL returns the loaded version, 0 on error.
int version = gladLoadGL(glfwGetProcAddress);
if (version == 0)
{
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
// Successfully loaded OpenGL
std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl;
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminates GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

View File

@@ -0,0 +1,130 @@
#include <iostream>
// GLAD
#define GLAD_GL_IMPLEMENTATION
#include <glad/gl.h>
// GLFW
#include <GLFW/glfw3.h>
// This example is taken from http://learnopengl.com/
// http://learnopengl.com/code_viewer.php?code=getting-started/hellowindow2
// The code originally used GLEW, I replaced it with Glad
// Compile:
// g++ example/c++/hellowindow2.cpp -Ibuild/include build/src/glad.c -lglfw -ldl
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
#ifdef GLAD_OPTION_GL_DEBUG
// Define a custom callback for demonstration purposes
void pre_gl_call(const char *name, void *funcptr, int len_args, ...) {
#ifdef GLAD_OPTION_GL_MX
printf("Current GL Context: %p -> ", gladGetGLContext());
#endif
printf("Calling: %s at %p (%d arguments)\n", name, funcptr, len_args);
}
#endif
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
#ifdef GLAD_OPTION_GL_LOADER
printf("Using internal loader.\n");
#endif
#ifdef GLAD_OPTION_GL_MX
GladGLContext context = {};
#ifdef GLAD_OPTION_GL_LOADER
int version = gladLoaderLoadGLContext(&context);
#else
int version = gladLoadGLContext(&context, glfwGetProcAddress);
#endif
#else
#ifdef GLAD_OPTION_GL_LOADER
int version = gladLoaderLoadGL();
#else
int version = gladLoadGL(glfwGetProcAddress);
#endif
#endif
if (version == 0)
{
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl;
#ifdef GLAD_OPTION_GL_DEBUG
// before every opengl call call pre_gl_call
glad_set_gl_pre_callback(pre_gl_call);
// don't use the callbacks for glClear and glClearColor
#ifdef GLAD_OPTION_GL_MX_GLOBAL
glad_debug_glClear = gladGetGLContext()->Clear;
glad_debug_glClearColor = gladGetGLContext()->ClearColor;
#else
glad_debug_glClear = glad_glClear;
glad_debug_glClearColor = glad_glClearColor;
#endif
#endif
// Define the viewport dimensions
glViewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminates GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

View File

@@ -0,0 +1,92 @@
#include <iostream>
// GLAD
#include <glad/gl.h>
// GLFW
#include <GLFW/glfw3.h>
// This example is taken from http://learnopengl.com/
// http://learnopengl.com/code_viewer.php?code=getting-started/hellowindow2
// The code originally used GLEW, I replaced it with Glad
// Compile:
// g++ example/c++/hellowindow2.cpp -Ibuild/include build/src/glad.c -lglfw -ldl
// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
void draw(GLFWwindow *window, GladGLContext *context);
// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;
// The MAIN function, from here we start the application and run the game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
glfwMakeContextCurrent(window);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);
GladGLContext context = {};
int version = gladLoadGLContext(&context, glfwGetProcAddress);
if (version == 0)
{
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
draw(window, &context);
return 0;
}
void draw(GLFWwindow *window, GladGLContext *gl) {
// Define the viewport dimensions
gl->Viewport(0, 0, WIDTH, HEIGHT);
// Game loop
while (!glfwWindowShouldClose(window))
{
// Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
glfwPollEvents();
// Render
// Clear the colorbuffer
gl->ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
gl->Clear(GL_COLOR_BUFFER_BIT);
// Swap the screen buffers
glfwSwapBuffers(window);
}
// Terminates GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

View File

@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.1)
project(glad_examples_c_multiwin_mx C CXX)
find_package(glfw3 REQUIRED)
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/../../..")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
glad_add_library(glad_gl_core_mx_33 REPRODUCIBLE MX API gl:core=3.3)
add_executable(multiwin_mx
multiwin_mx.cpp
)
target_link_libraries(multiwin_mx
PUBLIC
glad_gl_core_mx_33
glfw
)

View File

@@ -0,0 +1,109 @@
#include <iostream>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
// Function prototypes
GLFWwindow* create_window(const char *name, int major, int minor);
GladGLContext* create_context(GLFWwindow *window);
void free_context(GladGLContext *context);
void draw(GLFWwindow *window, GladGLContext *context, float r, float g, float b);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
// Window dimensions
const GLuint WIDTH = 400, HEIGHT = 300;
int main()
{
glfwInit();
GLFWwindow *window1 = create_window("Window 1", 3, 3);
GLFWwindow *window2 = create_window("Window 2", 3, 2);
if (!window1 || !window2) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwSetKeyCallback(window1, key_callback);
glfwSetKeyCallback(window2, key_callback);
GladGLContext *context1 = create_context(window1);
GladGLContext *context2 = create_context(window2);
if (!context1 || !context2) {
std::cout << "Failed to initialize GL contexts" << std::endl;
free_context(context1);
free_context(context2);
}
glfwMakeContextCurrent(window1);
context1->Viewport(0, 0, WIDTH, HEIGHT);
glfwMakeContextCurrent(window2);
context2->Viewport(0, 0, WIDTH, HEIGHT);
while (!glfwWindowShouldClose(window1) && !glfwWindowShouldClose(window2))
{
glfwPollEvents();
draw(window1, context1, 0.5, 0.2, 0.6);
draw(window2, context2, 0.0, 0.1, 0.8);
}
free_context(context1);
free_context(context2);
glfwTerminate();
return 0;
}
GLFWwindow* create_window(const char *name, int major, int minor) {
std::cout << "Creating Window, OpenGL " << major << "." << minor << ": " << name << std::endl;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, name, NULL, NULL);
return window;
}
GladGLContext* create_context(GLFWwindow *window) {
glfwMakeContextCurrent(window);
GladGLContext* context = (GladGLContext*) calloc(1, sizeof(GladGLContext));
if (!context) return NULL;
int version = gladLoadGLContext(context, glfwGetProcAddress);
std::cout << "Loaded OpenGL " << GLAD_VERSION_MAJOR(version) << "." << GLAD_VERSION_MINOR(version) << std::endl;
return context;
}
void free_context(GladGLContext *context) {
free(context);
}
void draw(GLFWwindow *window, GladGLContext *gl, float r, float g, float b) {
glfwMakeContextCurrent(window);
gl->ClearColor(r, g, b, 1.0f);
gl->Clear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

View File

@@ -0,0 +1,76 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <glad/egl.h>
#include <glad/gles2.h>
#define GLFW_INCLUDE_NONE 1
#include <GLFW/glfw3.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#else
#define GLFW_EXPOSE_NATIVE_EGL 1
#include <GLFW/glfw3native.h>
#endif
const GLuint WIDTH = 800, HEIGHT = 600;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void render_frame(GLFWwindow *window) {
glfwPollEvents();
glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
int main(int argc, char **argv) {
glfwInit();
srand(time(NULL));
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] EGL with GLFW", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
#ifndef __EMSCRIPTEN__
/* Load EGL */
EGLDisplay display = glfwGetEGLDisplay();
int egl_version = gladLoaderLoadEGL(display);
printf("EGL %d.%d\n", GLAD_VERSION_MAJOR(egl_version), GLAD_VERSION_MINOR(egl_version));
#endif
/* Load GLES */
int gles_version = 0;
if (rand() % 100 < 50) {
printf("-> using GLFW to load GLES2\n");
gles_version = gladLoadGLES2(glfwGetProcAddress);
} else {
printf("-> using GLAD loader to load GLES2\n");
gles_version = gladLoaderLoadGLES2();
}
printf("GLES %d.%d\n", GLAD_VERSION_MAJOR(gles_version), GLAD_VERSION_MINOR(gles_version));
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg((em_arg_callback_func) render_frame, window, 60, 1);
#else
while (!glfwWindowShouldClose(window)) { render_frame(window); }
#endif
glfwTerminate();
return 0;
}

View File

@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.1)
project(glad_examples_c_egl_x11 C)
find_package(X11 REQUIRED)
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/../../..")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
glad_add_library(glad_egl_15_gles2_20 REPRODUCIBLE LOADER API egl=1.5 gles2=2.0)
add_executable(egl_x11
egl_x11.c
)
target_include_directories(egl_x11
PUBLIC
${X11_INCLUDE_DIR}
)
target_link_libraries(egl_x11
PUBLIC
glad_egl_15_gles2_20
${X11_LIBRARIES}
)

View File

@@ -0,0 +1,163 @@
// gcc example/c/egl_x11.c -Ibuild/include build/src/*.c -ldl -lX11
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <glad/egl.h>
#include <glad/gles2.h>
const int window_width = 800, window_height = 480;
int main(void) {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
printf("cannot connect to X server\n");
return 1;
}
int screen = DefaultScreen(display);
Window root = RootWindow(display, screen);
Visual *visual = DefaultVisual(display, screen);
Colormap colormap = XCreateColormap(display, root, visual, AllocNone);
XSetWindowAttributes attributes;
attributes.colormap = colormap;
attributes.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask;
Window window =
XCreateWindow(display, root, 0, 0, window_width, window_height, 0,
DefaultDepth(display, screen), InputOutput, visual,
CWColormap | CWEventMask, &attributes);
XFreeColormap(display, colormap);
XMapWindow(display, window);
XStoreName(display, window, "[glad] EGL with X11");
if (!window) {
printf("Unable to create window.\n");
return 1;
}
int egl_version = gladLoaderLoadEGL(NULL);
if (!egl_version) {
printf("Unable to load EGL.\n");
return 1;
}
printf("Loaded EGL %d.%d on first load.\n",
GLAD_VERSION_MAJOR(egl_version), GLAD_VERSION_MINOR(egl_version));
EGLDisplay egl_display = eglGetDisplay((EGLNativeDisplayType) display);
if (egl_display == EGL_NO_DISPLAY) {
printf("Got no EGL display.\n");
return 1;
}
if (!eglInitialize(egl_display, NULL, NULL)) {
printf("Unable to initialize EGL\n");
return 1;
}
egl_version = gladLoaderLoadEGL(egl_display);
if (!egl_version) {
printf("Unable to reload EGL.\n");
return 1;
}
printf("Loaded EGL %d.%d after reload.\n",
GLAD_VERSION_MAJOR(egl_version), GLAD_VERSION_MINOR(egl_version));
EGLint attr[] = {
EGL_BUFFER_SIZE, 16,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLConfig egl_config;
EGLint num_config;
if (!eglChooseConfig(egl_display, attr, &egl_config, 1, &num_config)) {
printf("Failed to choose config (eglError: %d)\n", eglGetError());
return 1;
}
if (num_config != 1) {
printf("Didn't get exactly one config, but %d\n", num_config);
return 1;
}
EGLSurface egl_surface =
eglCreateWindowSurface(egl_display, egl_config, window, NULL);
if (egl_surface == EGL_NO_SURFACE) {
printf("Unable to create EGL surface (eglError: %d)\n",
eglGetError());
return 1;
}
EGLint ctxattr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLContext egl_context =
eglCreateContext(egl_display, egl_config, EGL_NO_CONTEXT, ctxattr);
if (egl_context == EGL_NO_CONTEXT) {
printf("Unable to create EGL context (eglError: %d)\n",
eglGetError());
return 1;
}
// activate context before loading GL functions using glad
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
int gles_version = gladLoaderLoadGLES2();
if (!gles_version) {
printf("Unable to load GLES.\n");
return 1;
}
printf("Loaded GLES %d.%d.\n",
GLAD_VERSION_MAJOR(gles_version), GLAD_VERSION_MINOR(gles_version));
XWindowAttributes gwa;
XGetWindowAttributes(display, window, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
bool quit = false;
while (!quit) {
while (XPending(display)) {
XEvent xev;
XNextEvent(display, &xev);
if (xev.type == KeyPress) {
quit = true;
}
}
glClearColor(0.8, 0.6, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(egl_display, egl_surface);
usleep(1000 * 10);
}
gladLoaderUnloadGLES2();
eglDestroyContext(egl_display, egl_context);
eglDestroySurface(egl_display, egl_surface);
eglTerminate(egl_display);
gladLoaderUnloadEGL();
XDestroyWindow(display, window);
XCloseDisplay(display);
return 0;
}

44
third-party/glad/example/c/gl_glfw.c vendored Normal file
View File

@@ -0,0 +1,44 @@
#include <stdlib.h>
#include <stdio.h>
#define GLAD_GL_IMPLEMENTATION // Necessary for headeronly version.
#include <glad/gl.h>
#include <GLFW/glfw3.h>
const GLuint WIDTH = 800, HEIGHT = 600;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
int main(void) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GL with GLFW", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
int version = gladLoadGL(glfwGetProcAddress);
printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}

View File

@@ -0,0 +1,73 @@
// This example requires you to generate glad with the --on-demand option and optionally --loader and --debug.
// gcc -o gl_glfw_on_demand example/c/gl_glfw_on_demand.c build/src/gl.c -Ibuild/include -ldl -lglfw
#include <stdlib.h>
#include <stdio.h>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
const GLuint WIDTH = 800, HEIGHT = 600;
static void pre_call_gl_callback(const char *name, GLADapiproc apiproc, int len_args, ...) {
printf("about to call gl func: %s\n", name);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (action != GLFW_PRESS) {
return;
}
if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, GL_TRUE);
#ifdef GLAD_OPTION_GL_DEBUG
} else if (key == GLFW_KEY_H) {
printf("Installing glad debug function pointers\n");
gladInstallGLDebug();
} else if (key == GLFW_KEY_J) {
printf("Uninstalling glad debug function pointers\n");
gladUninstallGLDebug();
#endif
}
}
int main(void) {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GL with GLFW", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
// If glad is generated with the --loader and --on-demand option
// you don't have to call any glad function.
// It is recommended to use the loader provided by your context creation library
// instead of the glad loader.
#ifndef GLAD_GL_LOADER
gladSetGLOnDemandLoader(glfwGetProcAddress);
#endif
#ifdef GLAD_OPTION_GL_DEBUG
gladUninstallGLDebug();
gladSetGLPreCallback(pre_call_gl_callback);
#endif
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}

64
third-party/glad/example/c/gl_sdl2.c vendored Normal file
View File

@@ -0,0 +1,64 @@
// gcc example/c/gl_sdl2.c build/src/gl.c -Ibuild/include `sdl2-config --libs --cflags` -ldl
#include <stdlib.h>
#include <stdio.h>
#include <glad/gl.h>
#include <SDL.h>
#include <SDL_opengl.h>
const GLuint WIDTH = 800, HEIGHT = 600;
int main(void) {
// code without checking for errors
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_Window *window = SDL_CreateWindow(
"[glad] GL with SDL",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WIDTH, HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN
);
SDL_GLContext context = SDL_GL_CreateContext(window);
int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress);
printf("GL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
int exit = 0;
while(!exit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
exit = 1;
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE) {
exit = 1;
}
break;
default:
break;
}
}
glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
SDL_Delay(1);
}
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}

View File

@@ -0,0 +1,55 @@
#include <stdlib.h>
#include <stdio.h>
#include <glad/gles2.h>
#define GLFW_INCLUDE_NONE 1
#include <GLFW/glfw3.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
const GLuint WIDTH = 800, HEIGHT = 600;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void render_frame(GLFWwindow *window) {
glfwPollEvents();
glClearColor(0.7f, 0.9f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
int main(void) {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "[glad] GLES2 with GLFW", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, key_callback);
/* Load GLES */
int gles_version = gladLoadGLES2(glfwGetProcAddress);
printf("GLES %d.%d\n", GLAD_VERSION_MAJOR(gles_version), GLAD_VERSION_MINOR(gles_version));
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_arg((em_arg_callback_func) render_frame, window, 60, 1);
#else
while (!glfwWindowShouldClose(window)) { render_frame(window); }
#endif
return 0;
}

78
third-party/glad/example/c/glut.c vendored Normal file
View File

@@ -0,0 +1,78 @@
#include <stdio.h>
#include <stdlib.h>
#include <glad/gl.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// This file is a modified version of gl3w's test.c
// https://github.com/skaslev/gl3w/blob/master/src/test.c
// Compile:
// gcc example/c/simple.c -Ibuild/include build/src/glad.c -lglut -ldl
static int width = 600, height = 600;
static void display(void)
{
glClearColor(1.0f, 0.2f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
glutPostRedisplay();
}
static void reshape(int w, int h)
{
width = w > 1 ? w : 1;
height = h > 1 ? h : 1;
glViewport(0, 0, width, height);
glClearDepth(1.0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
}
static void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // Escape key
glutDestroyWindow(1);
return;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("cookie");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
// initialize glad after creating a context
int version = gladLoaderLoadGL();
if(version == 0) {
printf("Something went wrong!\n");
exit(-1);
}
printf("OpenGL %d.%d\n", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
if (!GLAD_GL_VERSION_2_0) {
printf("Your system doesn't support OpenGL >= 2!\n");
return -1;
}
printf("OpenGL %s, GLSL %s\n",
glGetString(GL_VERSION),
glGetString(GL_SHADING_LANGUAGE_VERSION));
glutMainLoop();
return 0;
}

98
third-party/glad/example/c/glx.c vendored Normal file
View File

@@ -0,0 +1,98 @@
// gcc example/c/glx.c -o build/glx -Ibuild/include build/src/*.c -ldl -lX11
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <glad/gl.h>
#include <glad/glx.h>
const int window_width = 800, window_height = 480;
int main(void) {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
printf("cannot connect to X server\n");
return 1;
}
int screen = DefaultScreen(display);
int glx_version = gladLoaderLoadGLX(display, screen);
if (!glx_version) {
printf("Unable to load GLX.\n");
return 1;
}
printf("Loaded GLX %d.%d\n", GLAD_VERSION_MAJOR(glx_version), GLAD_VERSION_MINOR(glx_version));
Window root = RootWindow(display, screen);
GLint visual_attributes[] = { GLX_RGBA, GLX_DOUBLEBUFFER, None };
XVisualInfo *visual_info = glXChooseVisual(display, screen, visual_attributes);
Colormap colormap = XCreateColormap(display, root, visual_info->visual, AllocNone);
XSetWindowAttributes attributes;
attributes.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask;
attributes.colormap = colormap;
Window window =
XCreateWindow(display, root, 0, 0, window_width, window_height, 0,
visual_info->depth, InputOutput, visual_info->visual,
CWColormap | CWEventMask, &attributes);
XMapWindow(display, window);
XStoreName(display, window, "[glad] GLX with X11");
if (!window) {
printf("Unable to create window.\n");
return 1;
}
GLXContext context = glXCreateContext(display, visual_info, NULL, GL_TRUE);
glXMakeCurrent(display, window, context);
int gl_version = gladLoaderLoadGL();
if (!gl_version) {
printf("Unable to load GL.\n");
return 1;
}
printf("Loaded GL %d.%d\n", GLAD_VERSION_MAJOR(gl_version), GLAD_VERSION_MINOR(gl_version));
XWindowAttributes gwa;
XGetWindowAttributes(display, window, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
bool quit = false;
while (!quit) {
while (XPending(display)) {
XEvent xev;
XNextEvent(display, &xev);
if (xev.type == KeyPress) {
quit = true;
}
}
glClearColor(0.8, 0.6, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(display, window);
usleep(1000 * 10);
}
glXMakeCurrent(display, 0, 0);
glXDestroyContext(display, context);
XDestroyWindow(display, window);
XFreeColormap(display, colormap);
XCloseDisplay(display);
gladLoaderUnloadGLX();
}

117
third-party/glad/example/c/glx_modern.c vendored Normal file
View File

@@ -0,0 +1,117 @@
// gcc example/c/glx.c -o build/glx -Ibuild/include build/src/*.c -ldl -lX11
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <glad/gl.h>
#include <glad/glx.h>
const int window_width = 800, window_height = 480;
int main(void) {
Display *display = XOpenDisplay(NULL);
if (display == NULL) {
printf("cannot connect to X server\n");
return 1;
}
int screen = DefaultScreen(display);
Window root = RootWindow(display, screen);
Visual *visual = DefaultVisual(display, screen);
Colormap colormap = XCreateColormap(display, root, visual, AllocNone);
XSetWindowAttributes attributes;
attributes.event_mask = ExposureMask | KeyPressMask | KeyReleaseMask;
attributes.colormap = colormap;
Window window =
XCreateWindow(display, root, 0, 0, window_width, window_height, 0,
DefaultDepth(display, screen), InputOutput, visual,
CWColormap | CWEventMask, &attributes);
XMapWindow(display, window);
XStoreName(display, window, "[glad] Modern GLX with X11");
if (!window) {
printf("Unable to create window.\n");
return 1;
}
int glx_version = gladLoaderLoadGLX(display, screen);
if (!glx_version) {
printf("Unable to load GLX.\n");
return 1;
}
printf("Loaded GLX %d.%d\n", GLAD_VERSION_MAJOR(glx_version), GLAD_VERSION_MINOR(glx_version));
GLint visual_attributes[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, 1,
None
};
int num_fbc = 0;
GLXFBConfig *fbc = glXChooseFBConfig(display, screen, visual_attributes, &num_fbc);
GLint context_attributes[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 3,
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
None
};
GLXContext context =
glXCreateContextAttribsARB(display, fbc[0], NULL, 1, context_attributes);
if (!context) {
printf("Unable to create OpenGL context.\n");
return 1;
}
glXMakeCurrent(display, window, context);
int gl_version = gladLoaderLoadGL();
if (!gl_version) {
printf("Unable to load GL.\n");
return 1;
}
printf("Loaded GL %d.%d\n", GLAD_VERSION_MAJOR(gl_version), GLAD_VERSION_MINOR(gl_version));
XWindowAttributes gwa;
XGetWindowAttributes(display, window, &gwa);
glViewport(0, 0, gwa.width, gwa.height);
bool quit = false;
while (!quit) {
while (XPending(display)) {
XEvent xev;
XNextEvent(display, &xev);
if (xev.type == KeyPress) {
quit = true;
}
}
glClearColor(0.8, 0.6, 0.7, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(display, window);
usleep(1000 * 10);
}
glXMakeCurrent(display, 0, 0);
glXDestroyContext(display, context);
XDestroyWindow(display, window);
XFreeColormap(display, colormap);
XCloseDisplay(display);
gladLoaderUnloadGLX();
}

View File

@@ -0,0 +1,17 @@
cmake_minimum_required(VERSION 3.1)
project(glad_examples_c_vulkan_tri_glfw C)
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/../../..")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
find_package(glfw3 REQUIRED)
glad_add_library(glad_vulkan_12 REPRODUCIBLE LOADER API vulkan=1.2)
add_executable(vulkan_tri_glfw
vulkan_tri_glfw.c
)
target_link_libraries(vulkan_tri_glfw
PUBLIC
glad_vulkan_12
glfw
)

File diff suppressed because it is too large Load Diff

172
third-party/glad/example/c/wgl.c vendored Normal file
View File

@@ -0,0 +1,172 @@
/**
* Thanks to Xeek for the code!
*
* Building and running under Linux:
* i686-w64-mingw32-gcc example/c/wgl.c build/src/wgl.c build/src/gl.c -Ibuild/include -lgdi32 -lopengl32
* wine a.exe
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdbool.h>
#include <glad/wgl.h>
#include <glad/gl.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
static const TCHAR window_classname[] = _T("SampleWndClass");
static const TCHAR window_title[] = _T("[glad] WGL");
static const POINT window_location = { CW_USEDEFAULT, 0 };
static const SIZE window_size = { 1024, 768 };
static const GLfloat clear_color[] = { 0.0f, 0.0f, 1.0f, 1.0f };
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
WNDCLASSEX wcex = { };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wcex.lpszClassName = window_classname;
ATOM wndclass = RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(MAKEINTATOM(wndclass), window_title,
WS_OVERLAPPEDWINDOW,
window_location.x, window_location.y,
window_size.cx, window_size.cy,
NULL, NULL, hInstance, NULL);
if (!hWnd) {
MessageBox(NULL, _T("Failed to create window!"), window_title, MB_ICONERROR);
return -1;
}
// Configure & Initialize OpenGL:
// Get a device context so I can set the pixel format later:
HDC hdc = GetDC(hWnd);
if (hdc == NULL) {
DestroyWindow(hWnd);
MessageBox(NULL, _T("Failed to get Window's device context!"), window_title, MB_ICONERROR);
return -1;
}
// Set the pixel format for the device context:
PIXELFORMATDESCRIPTOR pfd = { };
pfd.nSize = sizeof(pfd);
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Set the size of the PFD to the size of the class
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // Enable double buffering, opengl support and drawing to a window
pfd.iPixelType = PFD_TYPE_RGBA; // Set our application to use RGBA pixels
pfd.cColorBits = 32; // Give us 32 bits of color information (the higher, the more colors)
pfd.cDepthBits = 32; // Give us 32 bits of depth information (the higher, the more depth levels)
pfd.iLayerType = PFD_MAIN_PLANE; // Set the layer of the PFD
int format = ChoosePixelFormat(hdc, &pfd);
if (format == 0 || SetPixelFormat(hdc, format, &pfd) == FALSE) {
ReleaseDC(hWnd, hdc);
DestroyWindow(hWnd);
MessageBox(NULL, _T("Failed to set a compatible pixel format!"), window_title, MB_ICONERROR);
return -1;
}
// Create and enable a temporary (helper) opengl context:
HGLRC temp_context = NULL;
if (NULL == (temp_context = wglCreateContext(hdc))) {
ReleaseDC(hWnd, hdc);
DestroyWindow(hWnd);
MessageBox(NULL, _T("Failed to create the initial rendering context!"), window_title, MB_ICONERROR);
return -1;
}
wglMakeCurrent(hdc, temp_context);
// Load WGL Extensions:
gladLoaderLoadWGL(hdc);
// Set the desired OpenGL version:
int attributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // Set the MAJOR version of OpenGL to 3
WGL_CONTEXT_MINOR_VERSION_ARB, 2, // Set the MINOR version of OpenGL to 2
WGL_CONTEXT_FLAGS_ARB,
WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // Set our OpenGL context to be forward compatible
0
};
// Create the final opengl context and get rid of the temporary one:
HGLRC opengl_context = NULL;
if (NULL == (opengl_context = wglCreateContextAttribsARB(hdc, NULL, attributes))) {
wglDeleteContext(temp_context);
ReleaseDC(hWnd, hdc);
DestroyWindow(hWnd);
MessageBox(NULL, _T("Failed to create the final rendering context!"), window_title, MB_ICONERROR);
return -1;
}
wglMakeCurrent(NULL, NULL); // Remove the temporary context from being active
wglDeleteContext(temp_context); // Delete the temporary OpenGL context
wglMakeCurrent(hdc, opengl_context); // Make our OpenGL 3.2 context current
// Glad Loader!
if (!gladLoaderLoadGL()) {
wglMakeCurrent(NULL, NULL);
wglDeleteContext(opengl_context);
ReleaseDC(hWnd, hdc);
DestroyWindow(hWnd);
MessageBox(NULL, _T("Glad Loader failed!"), window_title, MB_ICONERROR);
return -1;
}
// Show & Update the main window:
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
// A typical native Windows game loop:
bool should_quit = false;
MSG msg = { };
while (!should_quit) {
// Generally you'll want to empty out the message queue before each rendering
// frame or messages will build up in the queue possibly causing input
// delay. Multiple messages and input events occur before each frame.
while (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT || (msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE))
should_quit = true;
}
glClearColor(clear_color[0], clear_color[1], clear_color[2], clear_color[3]);
glClear(GL_COLOR_BUFFER_BIT);
SwapBuffers(hdc);
}
// Clean-up:
if (opengl_context)
wglDeleteContext(opengl_context);
if (hdc)
ReleaseDC(hWnd, hdc);
if (hWnd)
DestroyWindow(hWnd);
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_QUIT:
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}

View File

@@ -0,0 +1,8 @@
[package]
name = "gl-glfw"
version = "0.1.0"
[dependencies]
glfw = "0.37.0"
glad-gl = { path = "./build/glad-gl" }

View File

@@ -0,0 +1,34 @@
Example: gl-glfw-mx
================
This is basic example showcasing `glad-gl` in combination with
[`glfw`](https://crates.io/crates/glfw). And multiple OpenGL contexts
in different windows.
To run the example use the following command:
```sh
./init.sh && cargo run
```
The `init.sh` script is just a small utility used to generate
the `glad-gl` crate into the `build/` directory. The `Cargo.toml`
references the dependency using:
```toml
[dependencies]
glad-gl = { path = "./build/glad-gl" }
```
This example is the basic example of the
[glfw crate](https://crates.io/crates/glfw) with some
OpenGL instructions added and just one additional line
to initialize `glad`:
```rust
gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
```
That's all that is needed to initialize and use OpenGL using `glad`!

View File

@@ -0,0 +1,9 @@
#!/bin/sh
BASE_PATH="$(dirname $(realpath $0))"
cd "${BASE_PATH}/../../../"
python -m glad --out-path "${BASE_PATH}/build" --extensions="" --api="gl:core=3.3" rust --mx

View File

@@ -0,0 +1,63 @@
extern crate glfw;
extern crate glad_gl;
use std::sync::mpsc::Receiver;
use glfw::{Action, Context, Key};
use glad_gl::gl;
struct Window {
source: glfw::Window,
events: Receiver<(f64, glfw::WindowEvent)>,
gl: gl::Gl
}
fn main() {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
let mut w1 = create_window(&mut glfw);
let mut w2 = create_window(&mut glfw);
while !w1.source.should_close() && !w2.source.should_close() {
glfw.poll_events();
draw(&mut w1);
draw(&mut w2);
}
}
fn create_window(glfw: &mut glfw::Glfw) -> Window {
let (mut window, events) = glfw
.create_window(300, 300, "[glad] Rust - OpenGL with GLFW", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window.");
window.set_key_polling(true);
window.make_current();
let gl = gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
Window {
source: window, events, gl
}
}
fn draw(window: &mut Window) {
for (_, event) in glfw::flush_messages(&window.events) {
handle_window_event(&mut window.source, event);
}
window.source.make_current();
unsafe {
window.gl.ClearColor(0.7, 0.9, 0.1, 1.0);
window.gl.Clear(gl::COLOR_BUFFER_BIT);
}
window.source.swap_buffers();
}
fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
match event {
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
window.set_should_close(true)
}
_ => {}
}
}

View File

@@ -0,0 +1,8 @@
[package]
name = "gl-glfw"
version = "0.1.0"
[dependencies]
glfw = "0.37.0"
glad-gl = { path = "./build/glad-gl" }

View File

@@ -0,0 +1,33 @@
Example: gl-glfw
================
This is basic example showcasing `glad-gl` in combination with
[`glfw`](https://crates.io/crates/glfw).
To run the example use the following command:
```sh
./init.sh && cargo run
```
The `init.sh` script is just a small utility used to generate
the `glad-gl` crate into the `build/` directory. The `Cargo.toml`
references the dependency using:
```toml
[dependencies]
glad-gl = { path = "./build/glad-gl" }
```
This example is the basic example of the
[glfw crate](https://crates.io/crates/glfw) with some
OpenGL instructions added and just one additional line
to initialize `glad`:
```rust
gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
```
That's all that is needed to initialize and use OpenGL using `glad`!

View File

@@ -0,0 +1,9 @@
#!/bin/sh
BASE_PATH="$(dirname $(realpath $0))"
cd "${BASE_PATH}/../../../"
python -m glad --out-path "${BASE_PATH}/build" --extensions="" --api="gl:core=3.3" rust

View File

@@ -0,0 +1,40 @@
extern crate glfw;
extern crate glad_gl;
use glfw::{Action, Context, Key};
use glad_gl::gl;
fn main() {
let mut glfw = glfw::init(glfw::FAIL_ON_ERRORS).unwrap();
let (mut window, events) = glfw.create_window(300, 300, "[glad] Rust - OpenGL with GLFW", glfw::WindowMode::Windowed)
.expect("Failed to create GLFW window.");
window.set_key_polling(true);
window.make_current();
gl::load(|e| glfw.get_proc_address_raw(e) as *const std::os::raw::c_void);
while !window.should_close() {
glfw.poll_events();
for (_, event) in glfw::flush_messages(&events) {
handle_window_event(&mut window, event);
}
unsafe {
gl::ClearColor(0.7, 0.9, 0.1, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
}
window.swap_buffers();
}
}
fn handle_window_event(window: &mut glfw::Window, event: glfw::WindowEvent) {
match event {
glfw::WindowEvent::Key(Key::Escape, _, Action::Press, _) => {
window.set_should_close(true)
}
_ => {}
}
}