Skip to content

Commit a17fe6a

Browse files
committed
Update X11 examples
1 parent a12f144 commit a17fe6a

File tree

10 files changed

+252
-1178
lines changed

10 files changed

+252
-1178
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
.vs
66
**/core
77
examples/x11/x11-example
8+
examples/x11-custom-implementation/x11-custom-implementation

Diff for: examples/win32-custom-implementation/build.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cl /Zi /W3 /WX /D _UNICODE /D UNICODE /D SOGL_MAJOR_VERSION=4 /D SOGL_MINOR_VERSION=5 win32-custom-implementation.cpp user32.lib gdi32.lib opengl32.lib
File renamed without changes.

Diff for: tests/win32-app-def/win32-app-def-example.cpp renamed to examples/win32-custom-implementation/win32-custom-implementation.cpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
//////////////////////////////////////////////////////
2525
// Example of basic C++ usage of Simple OpenGL
26-
// Loader with Win32 and platform functions
27-
// defined by the application.
26+
// Loader with a custom Win32 implementation and
27+
// constants defined at the command line.
2828
//////////////////////////////////////////////////////
2929

3030
#define WIN32_LEAN_AND_MEAN
@@ -37,27 +37,27 @@
3737
#include <stdio.h>
3838

3939
typedef PROC (*wglGetProcAddressFP)(LPCSTR Arg1);
40-
static HMODULE libHandle = NULL;
40+
static HMODULE openGLLibHandle = NULL;
4141

4242
void *sogl_loadOpenGLFunction(const char *name) {
4343
static wglGetProcAddressFP wglGetProcAddress = NULL;
4444

45-
if (!libHandle) {
46-
libHandle = LoadLibraryA("opengl32.dll");
47-
wglGetProcAddress = (wglGetProcAddressFP) GetProcAddress(libHandle, "wglGetProcAddress");
45+
if (!openGLLibHandle) {
46+
openGLLibHandle = LoadLibraryA("opengl32.dll");
47+
wglGetProcAddress = (wglGetProcAddressFP) GetProcAddress(openGLLibHandle, "wglGetProcAddress");
4848
}
4949
void *fn = (void *)wglGetProcAddress(name);
5050
if(fn == 0 || (fn == (void *) 0x1) || (fn == (void *) 0x2) || (fn == (void*) 0x3) || (fn == (void *) -1)) {
51-
fn = (void *) GetProcAddress(libHandle, name);
51+
fn = (void *) GetProcAddress(openGLLibHandle, name);
5252
}
5353

5454
return fn;
5555
}
5656

5757
void sogl_cleanup() {
58-
if (libHandle) {
59-
FreeLibrary(libHandle);
60-
libHandle = NULL;
58+
if (openGLLibHandle) {
59+
FreeLibrary(openGLLibHandle);
60+
openGLLibHandle = NULL;
6161
}
6262
}
6363

@@ -175,7 +175,7 @@ int CALLBACK WinMain(HINSTANCE instance, HINSTANCE prevInstance, LPSTR cmdLine,
175175

176176
HWND window = CreateWindow(
177177
WIN_CLASS_NAME,
178-
L"Simple OpenGL Loader App-defined Platform Support Example ",
178+
L"Simple OpenGL Loader Win32 C++ Custom Implementation Example",
179179
WS_OVERLAPPEDWINDOW,
180180
CW_USEDEFAULT, CW_USEDEFAULT,
181181
800, 800,

Diff for: examples/x11-custom-implementation/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CFLAGS=-g -Wall -Werror -std=c++11 -DSOGL_MAJOR_VERSION=4 -DSOGL_MINOR_VERSION=5
2+
CC=g++
3+
LDLIBS=-lX11 -ldl -lGL
4+
5+
all:
6+
$(CC) $(CFLAGS) -o x11-custom-implementation x11-custom-implementation.cc $(LDLIBS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*********************************************************************************
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2020 Tarek Sherif
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
**********************************************************************************/
23+
//////////////////////////////////////////////////////
24+
// Example of basic C++ usage of Simple OpenGL
25+
// Loader with a custom X11 implementation and
26+
// constants defined at the command line.
27+
//////////////////////////////////////////////////////
28+
29+
#define SOGL_IMPLEMENTATION
30+
#include "../../simple-opengl-loader.h"
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
#include <X11/Xlib.h>
34+
#include <GL/glx.h>
35+
#include <dlfcn.h>
36+
37+
static void* openGLLibHandle = NULL;
38+
39+
void *sogl_loadOpenGLFunction(const char *name) {
40+
if (!openGLLibHandle) {
41+
openGLLibHandle = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL);
42+
if (!openGLLibHandle) {
43+
openGLLibHandle = dlopen("libGL.so", RTLD_LAZY | RTLD_LOCAL);
44+
}
45+
}
46+
47+
void *fn = dlsym(openGLLibHandle, name);
48+
49+
return fn;
50+
}
51+
52+
void sogl_cleanup() {
53+
if (openGLLibHandle) {
54+
dlclose(openGLLibHandle);
55+
openGLLibHandle = NULL;
56+
}
57+
}
58+
59+
typedef GLXContext (*glXCreateContextAttribsARBFUNC)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
60+
61+
int main(int argc, char const *argv[]) {
62+
Display* display;
63+
Window window;
64+
XEvent event;
65+
XWindowAttributes xWinAtt;
66+
67+
// X Windows stuff
68+
display = XOpenDisplay(NULL);
69+
70+
if (display == NULL) {
71+
printf("Unable to connect to X Server\n");
72+
return 1;
73+
}
74+
75+
window = XCreateSimpleWindow(display, DefaultRootWindow(display), 20, 20, 800, 800, 0, 0, 0);
76+
77+
78+
XSelectInput(display, window, ExposureMask | KeyPressMask | ButtonPressMask);
79+
XStoreName(display, window, "Simple OpenGL Loader X11 C++ Custom Implementation Example");
80+
XMapWindow(display, window);
81+
82+
int numFBC = 0;
83+
GLint visualAtt[] = {
84+
GLX_RENDER_TYPE, GLX_RGBA_BIT,
85+
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
86+
GLX_DOUBLEBUFFER, True,
87+
GLX_RED_SIZE, 1,
88+
GLX_GREEN_SIZE, 1,
89+
GLX_BLUE_SIZE, 1,
90+
GLX_DEPTH_SIZE, 1,
91+
GLX_STENCIL_SIZE, 1,
92+
None
93+
};
94+
95+
GLXFBConfig *fbc = glXChooseFBConfig(display, DefaultScreen(display), visualAtt, &numFBC);
96+
97+
if (!fbc) {
98+
fprintf(stderr, "Unable to get framebuffer\n");
99+
return -1;
100+
}
101+
102+
glXCreateContextAttribsARBFUNC glXCreateContextAttribsARB = (glXCreateContextAttribsARBFUNC) glXGetProcAddress((const GLubyte *) "glXCreateContextAttribsARB");
103+
104+
if (!glXCreateContextAttribsARB) {
105+
fprintf(stderr, "Unable to get proc glXCreateContextAttribsARB\n");
106+
XFree(fbc);
107+
return -1;
108+
}
109+
110+
static int contextAttribs[] = {
111+
GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
112+
GLX_CONTEXT_MINOR_VERSION_ARB, 5,
113+
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
114+
None
115+
};
116+
117+
GLXContext ctx = glXCreateContextAttribsARB(display, *fbc, NULL, True, contextAttribs);
118+
119+
XFree(fbc);
120+
121+
if (!ctx) {
122+
fprintf(stderr, "Unable to create OpenGL context\n");
123+
return -1;
124+
}
125+
126+
glXMakeCurrent(display, window, ctx);
127+
128+
if (!sogl_loadOpenGL()) {
129+
const char **failures = sogl_getFailures();
130+
while (*failures) {
131+
fprintf(stderr, "SOGL X11 EXAMPLE: Failed to load function %s\n", *failures);
132+
failures++;
133+
}
134+
}
135+
136+
glClearColor(0.0, 0.0, 0.0, 1.0);
137+
138+
GLuint vertexArray = 0;
139+
glGenVertexArrays(1, &vertexArray);
140+
glBindVertexArray(vertexArray);
141+
142+
GLfloat positionData[] = {
143+
-0.5, -0.5,
144+
0.5, -0.5,
145+
0.0, 0.5
146+
};
147+
148+
GLuint positions = 0;
149+
glGenBuffers(1, &positions);
150+
glBindBuffer(GL_ARRAY_BUFFER, positions);
151+
glBufferData(GL_ARRAY_BUFFER, 3 * 2 * sizeof(GLfloat), positionData, GL_STATIC_DRAW);
152+
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
153+
glEnableVertexAttribArray(0);
154+
155+
GLubyte colorData[] = {
156+
255, 0, 0,
157+
0, 255, 0,
158+
0, 0, 255
159+
};
160+
161+
GLuint colors = 0;
162+
glGenBuffers(1, &colors);
163+
glBindBuffer(GL_ARRAY_BUFFER, colors);
164+
glBufferData(GL_ARRAY_BUFFER, 3 * 3 * sizeof(GLubyte), colorData, GL_STATIC_DRAW);
165+
glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, NULL);
166+
glEnableVertexAttribArray(1);
167+
168+
const char* vsSource = R"GLSL(#version 450
169+
layout (location=0) in vec4 position;
170+
layout (location=1) in vec3 color;
171+
out vec3 vColor;
172+
void main() {
173+
vColor = color;
174+
gl_Position = position;
175+
};
176+
)GLSL";
177+
178+
const char* fsSource = R"GLSL(#version 450
179+
in vec3 vColor;
180+
out vec4 fragColor;
181+
void main() {
182+
fragColor = vec4(vColor, 1.0);
183+
}
184+
)GLSL";
185+
186+
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
187+
glShaderSource(vs, 1, &vsSource, NULL);
188+
glCompileShader(vs);
189+
190+
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
191+
glShaderSource(fs, 1, &fsSource, NULL);
192+
glCompileShader(fs);
193+
194+
GLuint program = glCreateProgram();
195+
glAttachShader(program, vs);
196+
glAttachShader(program, fs);
197+
glLinkProgram(program);
198+
199+
int params = -1;
200+
glGetProgramiv(program, GL_LINK_STATUS, &params);
201+
202+
if (params != GL_TRUE) {
203+
fprintf(stderr, "Program did not link!\n");
204+
}
205+
206+
glUseProgram(program);
207+
208+
Atom wmDeleteMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
209+
XSetWMProtocols(display, window, &wmDeleteMessage, 1);
210+
211+
// Animation loop
212+
while (1) {
213+
if (XCheckTypedWindowEvent(display, window, Expose, &event) == True) {
214+
XGetWindowAttributes(display, window, &xWinAtt);
215+
glViewport(0, 0, xWinAtt.width, xWinAtt.height);
216+
}
217+
218+
if (XCheckTypedWindowEvent(display, window, ClientMessage, &event) == True) {
219+
if (event.xclient.data.l[0] == (long) wmDeleteMessage) {
220+
break;
221+
}
222+
}
223+
224+
glClear(GL_COLOR_BUFFER_BIT);
225+
glDrawArrays(GL_TRIANGLES, 0, 3);
226+
227+
glXSwapBuffers(display, window);
228+
};
229+
230+
// Teardown
231+
XDestroyWindow(display, window);
232+
XCloseDisplay(display);
233+
}

Diff for: tests/win32-app-def/build.bat

-1
This file was deleted.

Diff for: tests/win32-cpp/build.bat

-1
This file was deleted.

0 commit comments

Comments
 (0)