added `windowDraw\' and renamed window functions

This commit is contained in:
Maciej Samborski 2025-06-08 11:42:38 +02:00
parent fe12584aae
commit c28c07dad4
3 changed files with 32 additions and 92 deletions

12
main.c
View File

@ -52,25 +52,27 @@ int main(void)
{ {
Window w = openWindow("Window", 800, 600); Window w = openWindow("Window", 800, 600);
setWindowHandler(&w, xevent); windowSetEventHandler(&w, xevent);
while (!w.close) { while (!w.close) {
handleWindowEvents(&w); windowHandleEvents(&w);
glClearColor(1, 1, 1, 1); glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5, -0.5); glVertex2f(-0.5, -0.5);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5, -0.5); glVertex2f(0.5, -0.5);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.0, 0.5); glVertex2f(0.0, 0.5);
glEnd(); glEnd();
glFlush(); glFlush();
glXSwapBuffers(w.x.display, w.x.window); windowDraw(w);
} }
destroyWindow(w); closeWindow(w);
return 0; return 0;
} }

View File

@ -1,6 +1,15 @@
#include "openwindow.h" #include "openwindow.h"
#include <stdio.h> #include <stdio.h>
int initOpenGL() {
if (getOpenGLProcs() == 0) return -1;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
return 0;
}
#ifndef _WIN32 #ifndef _WIN32
#define GLLOADER glXGetProcAddress #define GLLOADER glXGetProcAddress
@ -76,6 +85,7 @@ int initGLX(Window * window)
glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0; glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc)
glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" ); glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
int context_attribs[] = { int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 3, GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
GLX_CONTEXT_MINOR_VERSION_ARB, 0, GLX_CONTEXT_MINOR_VERSION_ARB, 0,
@ -147,14 +157,6 @@ int init(Window * window, const char * name, size_t width, size_t height) {
return 0; return 0;
} }
int initOpenGL() {
if (getOpenGLProcs() == 0) return -1;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
return 0;
}
Window openWindow(const char * name, size_t width, size_t height) { Window openWindow(const char * name, size_t width, size_t height) {
Window w = {0}; Window w = {0};
@ -171,13 +173,17 @@ Window openWindow(const char * name, size_t width, size_t height) {
return w; return w;
} }
void setWindowHandler(Window * window, WindowEventHandler handler) { void windowDraw(Window window) {
glXSwapBuffers(window.x.display, window.x.window);
}
void windowSetEventHandler(Window * window, WindowEventHandler handler) {
window->eventHandler = handler; window->eventHandler = handler;
} }
void handleWindowEvents(Window * window) { void windowHandleEvents(Window * window) {
if (window->eventHandler == NULL) { if (window->eventHandler == NULL) {
fprintf(stderr, "Window event handler is not defined. Use `setWindowHandler' to set it!\n"); fprintf(stderr, "Window event handler is not defined. Use `setEventHandler' to set it!\n");
exit(1); exit(1);
} }
@ -188,7 +194,7 @@ void handleWindowEvents(Window * window) {
window->eventHandler(window, xev); window->eventHandler(window, xev);
} }
void destroyWindow(Window window) { void closeWindow(Window window) {
glXMakeCurrent(window.x.display, None, None); glXMakeCurrent(window.x.display, None, None);
glXDestroyContext(window.x.display, window.glx.context); glXDestroyContext(window.x.display, window.glx.context);
@ -218,7 +224,7 @@ LRESULT CALLBACK wndProc(HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lPar
return (DefWindowProc(hwnd, msg, wParam, lParam)); return (DefWindowProc(hwnd, msg, wParam, lParam));
} }
int initWWM(Window * window, const char * title, size_t width, size_t height) { int initWGL(Window * window, const char * title, size_t width, size_t height) {
HINSTANCE hInstance = GetModuleHandle(NULL); HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASSEX wcex; WNDCLASSEX wcex;
@ -249,79 +255,10 @@ int initWWM(Window * window, const char * title, size_t width, size_t height) {
return 0; return 0;
} }
int initEgl(Window * window)
{
EGLint attr[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
EGLint num_config;
EGLint major, minor;
EGLint ctxattr[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
window->w.display = GetDC(window->w.window);
window->egl.display = eglGetDisplay((EGLNativeDisplayType) window->w.display);
if (window->egl.display == EGL_NO_DISPLAY) {
fprintf(stderr, "Error getting EGL display\n");
return -1;
}
if (!eglInitialize(window->egl.display, &major, &minor )) {
fprintf(stderr, "Error initializing EGL\n");
return -1;
}
/* create EGL rendering context */
int res = eglChooseConfig(window->egl.display, attr, &window->egl.config, 1, &num_config);
if (res != EGL_TRUE) {
fprintf(stderr, "Failed to choose config (eglError: %x)\n", res);
return -1;
}
if (num_config != 1) {
fprintf(stderr, "Couldn't choose config!\n");
return -1;
}
window->egl.surface = eglCreateWindowSurface(window->egl.display, window->egl.config, (EGLNativeWindowType) window->w.window, NULL);
if (window->egl.surface == EGL_NO_SURFACE) {
fprintf(stderr, "CreateWindowSurface, EGL eglError: %x\n", eglGetError());
return -1;
}
window->egl.context = eglCreateContext(window->egl.display, window->egl.config, EGL_NO_CONTEXT, ctxattr);
if (window->egl.context == EGL_NO_CONTEXT) {
fprintf(stderr, "CreateContext, EGL eglError: %d\n", eglGetError());
return -1;
}
return 0;
}
void initOpenGL() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
Window openWindow(const char * name, size_t width, size_t height) { Window openWindow(const char * name, size_t width, size_t height) {
Window w = {0}; Window w = {0};
if (initWWM(&w, name, width, height) < 0) { if (initWGL(&w, name, width, height) < 0) {
fprintf(stderr, "Failed to initialize egl!\n");
exit(1);
}
if (initEgl(&w) < 0) {
fprintf(stderr, "Failed to initialize egl!\n"); fprintf(stderr, "Failed to initialize egl!\n");
exit(1); exit(1);
} }
@ -333,7 +270,7 @@ Window openWindow(const char * name, size_t width, size_t height) {
return w; return w;
} }
void destroyWindow(Window window) { void closeWindow(Window window) {
DestroyWindow(window.w.window); DestroyWindow(window.w.window);
} }

View File

@ -63,9 +63,10 @@ typedef struct Window {
int getOpenGLProcs(void); int getOpenGLProcs(void);
Window openWindow(const char * name, size_t width, size_t height); Window openWindow(const char * name, size_t width, size_t height);
void setWindowHandler(Window * window, WindowEventHandler handler); void windowDraw(Window window);
void handleWindowEvents(Window * window); void windowSetEventHandler(Window * window, WindowEventHandler handler);
void destroyWindow(Window w); void windowHandleEvents(Window * window);
void closeWindow(Window w);
#endif // _OPEN_WINDOW_H_ #endif // _OPEN_WINDOW_H_