1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-05 04:37:50 +02:00

#15 PSC Wayland EGL context initialization

This commit is contained in:
XProger
2019-03-13 10:13:04 +03:00
parent bccba2b42f
commit b0f2209d29

View File

@@ -13,10 +13,16 @@
#include <libudev.h> #include <libudev.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include "game.h" #include "game.h"
#define WND_TITLE "OpenLara" #define WND_TITLE "OpenLara"
#define WND_WIDTH 1280
#define WND_HEIGHT 720
// timing // timing
unsigned int startTime; unsigned int startTime;
@@ -122,28 +128,62 @@ void sndFree() {
} }
// Window // Window
struct FrameBuffer { wl_display *wlDisplay;
unsigned short width; wl_compositor *wlCompositor = NULL;
unsigned short height; wl_shell *wlShell = NULL;
} fb; wl_surface *wlSurface;
wl_shell_surface *wlShellSurface;
wl_egl_window *wlEGLWindow;
EGLDisplay display; EGLDisplay display;
EGLSurface surface; EGLSurface surface;
EGLContext context; EGLContext context;
// Wayland Listeners
void wlEventObjectAdd(void* data, wl_registry* registry, uint32_t name, const char* interface, uint32_t version) {
if (!strcmp(interface, "wl_compositor")) {
wlCompositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
} else if (!strcmp(interface, "wl_shell")) {
wlShell = wl_registry_bind(registry, name, &wl_shell_interface, 1);
}
}
void wlEventObjectRemove(void* data, wl_registry* registry, uint32_t name) {
//
}
wl_registry_listener wlRegistryListener = {
&wlEventObjectAdd,
&wlEventObjectRemove
};
void wlEventSurfacePing(void* data, wl_shell_surface* shell_surface, uint32_t serial) {
wl_shell_surface_pong(shell_surface, serial);
}
void wlEventSurfaceConfig(void* data, wl_shell_surface* shell_surface, uint32_t edges, int32_t width, int32_t height) {
wl_egl_window_resize(wlEGLWindow, width, height, 0, 0);
Core::width = width;
Core::height = height;
}
void wlEnentSurfacePopup(void* data, wl_shell_surface* shell_surface) {
//
}
wl_shell_surface_listener wlSurfaceListener = {
&wlEventSurfacePing,
&wlEventSurfaceConfig,
&wlEnentSurfacePopup
};
bool eglInit() { bool eglInit() {
LOG("EGL init context...\n"); LOG("EGL init context...\n");
fb_var_screeninfo vinfo;
int fd = open("/dev/fb0", O_RDWR);
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
LOG("! can't get framebuffer size\n");
return false;
}
close(fd);
fb.width = vinfo.xres; wlDisplay = wl_display_connect(NULL);
fb.height = vinfo.yres; wl_registry* registry = wl_display_get_registry(wlDisplay);
wl_registry_add_listener(registry, &wlRegistryListener, NULL);
wl_display_roundtrip(wlDisplay);
static const EGLint eglAttr[] = { static const EGLint eglAttr[] = {
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
@@ -160,7 +200,7 @@ bool eglInit() {
EGL_NONE EGL_NONE
}; };
display = eglGetDisplay(EGL_DEFAULT_DISPLAY); display = eglGetDisplay(wlDisplay);
if (display == EGL_NO_DISPLAY) { if (display == EGL_NO_DISPLAY) {
LOG("eglGetDisplay = EGL_NO_DISPLAY\n"); LOG("eglGetDisplay = EGL_NO_DISPLAY\n");
return false; return false;
@@ -171,6 +211,8 @@ bool eglInit() {
return false; return false;
} }
eglBindAPI(EGL_OPENGL_API);
EGLConfig config; EGLConfig config;
EGLint configCount; EGLint configCount;
@@ -179,18 +221,25 @@ bool eglInit() {
return false; return false;
} }
surface = eglCreateWindowSurface(display, config, &fb, NULL);
if (surface == EGL_NO_SURFACE) {
LOG("eglCreateWindowSurface = EGL_NO_SURFACE\n");
return false;
}
context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctxAttr); context = eglCreateContext(display, config, EGL_NO_CONTEXT, ctxAttr);
if (context == EGL_NO_CONTEXT) { if (context == EGL_NO_CONTEXT) {
LOG("eglCreateContext = EGL_NO_CONTEXT\n"); LOG("eglCreateContext = EGL_NO_CONTEXT\n");
return false; return false;
} }
wlSurface = wl_compositor_create_surface(wlCompositor);
wlShellSurface = wl_shell_get_shell_surface(wlShell, wlSurface);
wl_shell_surface_add_listener(wlShellSurface, &wlSurfaceListener, NULL);
wl_shell_surface_set_toplevel(wlShellSurface);
wlEGLWindow = wl_egl_window_create(wlSurface, WND_WIDTH, WND_HEIGHT);
surface = eglCreateWindowSurface(display, config, wlEGLWindow, NULL);
if (surface == EGL_NO_SURFACE) {
LOG("eglCreateWindowSurface = EGL_NO_SURFACE\n");
return false;
}
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOG("eglMakeCurrent = EGL_FALSE\n"); LOG("eglMakeCurrent = EGL_FALSE\n");
return false; return false;
@@ -203,8 +252,12 @@ void eglFree() {
LOG("EGL release context\n"); LOG("EGL release context\n");
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroySurface(display, surface); eglDestroySurface(display, surface);
wl_egl_window_destroy(wlEGLWindow);
wl_shell_surface_destroy(wlShellSurface);
wl_surface_destroy(wlSurface)
eglDestroyContext(display, context); eglDestroyContext(display, context);
eglTerminate(display); eglTerminate(display);
wl_display_disconnect(wlDisplay);
} }
// Input // Input
@@ -550,7 +603,7 @@ int main(int argc, char **argv) {
strcpy(cacheDir, contentDir); strcpy(cacheDir, contentDir);
strcat(cacheDir, "cache/"); strcat(cacheDir, "cache/");
struct stat st = {0}; stat st = {0};
if (stat(cacheDir, &st) == -1 && mkdir(cacheDir, 0777) == -1) if (stat(cacheDir, &st) == -1 && mkdir(cacheDir, 0777) == -1)
cacheDir[0] = 0; cacheDir[0] = 0;