Linux之wayland+OpenGL+EGL绘制(十九)
Linux 绘制 OpenGL 十九
2023-09-14 09:16:12 时间
// gcc -o wayland-egl wayland-egl.c -lwayland-client -lwayland-egl -lEGL -lGL
# emacs wayland-egl.c
#include <wayland-client.h>
#include <wayland-egl.h>
#include <EGL/egl.h>
#include <GL/gl.h>
#include <string.h>
#define WIDTH 256
#define HEIGHT 256
static struct wl_display *display;
static struct wl_compositor *compositor = NULL;
static struct wl_shell *shell = NULL;
static EGLDisplay egl_display;
static char running = 1;
struct window {
EGLContext egl_context;
struct wl_surface *surface;
struct wl_shell_surface *shell_surface;
struct wl_egl_window *egl_window;
EGLSurface egl_surface;
};
// listeners
static void registry_add_object (void *data, struct wl_registry *registry, uint32_t name, const char *interface, uint32_t version) {
if (!strcmp(interface,"wl_compositor")) {
compositor = wl_registry_bind (registry, name, &wl_compositor_interface, 1);
}
else if (!strcmp(interface,"wl_shell")) {
shell = wl_registry_bind (registry, name, &wl_shell_interface, 1);
}
}
static void registry_remove_object (void *data, struct wl_registry *registry, uint32_t name) {
}
static struct wl_registry_listener registry_listener = {®istry_add_object, ®istry_remove_object};
static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial) {
wl_shell_surface_pong (shell_surface, serial);
}
static void shell_surface_configure (void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {
struct window *window = data;
wl_egl_window_resize (window->egl_window, width, height, 0, 0);
}
static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface) {
}
static struct wl_shell_surface_listener shell_surface_listener = {&shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done};
static void create_window (struct window *window, int32_t width, int32_t height) {
eglBindAPI (EGL_OPENGL_API);
EGLint attributes[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_NONE};
EGLConfig config;
EGLint num_config;
eglChooseConfig (egl_display, attributes, &config, 1, &num_config);
window->egl_context = eglCreateContext (egl_display, config, EGL_NO_CONTEXT, NULL);
window->surface = wl_compositor_create_surface (compositor);
window->shell_surface = wl_shell_get_shell_surface (shell, window->surface);
wl_shell_surface_add_listener (window->shell_surface, &shell_surface_listener, window);
wl_shell_surface_set_toplevel (window->shell_surface);
window->egl_window = wl_egl_window_create (window->surface, width, height);
window->egl_surface = eglCreateWindowSurface (egl_display, config, window->egl_window, NULL);
eglMakeCurrent (egl_display, window->egl_surface, window->egl_surface, window->egl_context);
}
static void delete_window (struct window *window) {
eglDestroySurface (egl_display, window->egl_surface);
wl_egl_window_destroy (window->egl_window);
wl_shell_surface_destroy (window->shell_surface);
wl_surface_destroy (window->surface);
eglDestroyContext (egl_display, window->egl_context);
}
static void draw_window (struct window *window) {
glClearColor (0.0, 1.0, 0.0, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
eglSwapBuffers (egl_display, window->egl_surface);
}
int main () {
display = wl_display_connect (NULL);
struct wl_registry *registry = wl_display_get_registry (display);
wl_registry_add_listener (registry, ®istry_listener, NULL);
wl_display_roundtrip (display);
egl_display = eglGetDisplay (display);
eglInitialize (egl_display, NULL, NULL);
struct window window;
create_window (&window, WIDTH, HEIGHT);
while (running) {
wl_display_dispatch_pending (display);
draw_window (&window);
}
delete_window (&window);
eglTerminate (egl_display);
wl_display_disconnect (display);
return 0;
}
相关文章
- 【嵌入式Linux应用开发】SquareLine Studio与LVGL模拟器
- Linux系统操作快速入门指南(linux系统操作指南)
- 极大程度的提高生产力:Linux系统的好处(linux系统有什么好处)
- Linux内核较多:你选择哪一个?(Linux内核太多)
- Linux命令行: 号的功能(linux作用)
- Linux下快速启动Git使用(linuxgit启动)
- 地址Linux系统配置IP地址指南(linux系统设置ip)
- Linux开放端口的简单操作(linux怎么开放端口)
- Linux:如何配置子网掩码(linux子网掩码配置)
- 学习Linux,改变你的世界(怎么自学linux)
- 轻松访问Linux系统:远程桌面Win连接LINUX(win远程linux)
- 初探Linux:自学路上的基础指南(怎么自学linux)
- 5 个免费的 Linux 分区管理器
- Linux下getcwd函数的用法和作用(linuxgetcwd)
- Linux下的矢量图编辑和绘制(linux矢量图)
- Linux流程图软件:易用执行快速流程图绘制(linux流程图软件)
- 内核移除 Linux 内核:一步接一步(移除linux)
- 开启Linux之旅:学习LINUX系统登录(linux系统登录)
- 如何在Linux上搭建CA服务器(linux搭建ca服务器)
- 学习Linux:掌握程序编辑技能(linux 程序编辑)
- Linux掌握文件修改时间的技巧(linux查看文件修改时间)
- Linux触摸板:让你的生活更加便捷(linux 触摸板)