feat: c++ classes

This commit is contained in:
2026-02-11 22:13:02 +01:00
parent a8b73927d8
commit 6cbb72f0ec
6 changed files with 400 additions and 304 deletions

View File

@@ -1,286 +1,12 @@
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client-core.h>
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2.h>
#include <wayland-egl-core.h>
#include <wayland-egl.h>
#include <fcntl.h> /* For O_* constants */
#include <sys/mman.h>
#include <sys/stat.h> /* For mode constants */
#include <unistd.h>
#include "shader.h"
#include "state.h"
#include "xdg-shell-client-protocol.h"
#include "window.hpp"
#define UNUSED(x) (void)(x)
#define WIDTH 780
#define HEIGHT 420
const char *vs_src = "attribute vec2 pos;\n"
"uniform float angle;\n"
"void main() {\n"
" float c = cos(angle);\n"
" float s = sin(angle);\n"
" mat2 rot = mat2(c, -s, s, c);\n"
" gl_Position = vec4(rot * pos, 0.0, 1.0);\n"
"}\n";
const char *fs_src = "precision mediump float;\n"
"void main() {\n"
" gl_FragColor = vec4(1.0, 0.3, 0.2, 1.0);\n"
"}\n";
void wm_base_handle_ping(void *data, struct xdg_wm_base *wm_base,
uint32_t serial) {
UNUSED(data);
printf("INFO: ping method fired! sending pong...\n");
xdg_wm_base_pong(wm_base, serial);
}
static struct xdg_wm_base_listener wm_base_listener = {
.ping = wm_base_handle_ping,
};
void registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface,
uint32_t version) {
State *app_state = reinterpret_cast<State *>(data);
printf("HANDLE_GLOBAL: new interface '%s', name = %d, version = %d\n",
interface, name, version);
if (strcmp(interface, "wl_compositor") == 0) {
app_state->compositor = reinterpret_cast<struct wl_compositor *>(
wl_registry_bind(registry, name, &wl_compositor_interface, version));
printf("%p compositor binding\n", app_state->compositor);
}
if (strcmp(interface, "xdg_wm_base") == 0) {
app_state->wm_base = reinterpret_cast<struct xdg_wm_base *>(
wl_registry_bind(registry, name, &xdg_wm_base_interface, version));
if (app_state->wm_base == NULL) {
fprintf(stderr, "ERROR: could not bind to xdg_wm_base\n");
}
int res = xdg_wm_base_add_listener(app_state->wm_base, &wm_base_listener,
app_state);
printf("bound to wm_base: %d\n", res);
}
if (strcmp(interface, "wl_shm") == 0) {
app_state->shm = reinterpret_cast<struct wl_shm *>(
wl_registry_bind(registry, name, &wl_shm_interface, version));
if (app_state->shm == NULL) {
fprintf(stderr, "ERROR: could not bind to wl_shm\n");
}
}
}
void registry_handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name) {
UNUSED(data);
UNUSED(registry);
UNUSED(name);
}
static struct wl_registry_listener listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
static struct wl_callback_listener frame_listener;
static void frame_done(void *data, struct wl_callback *cb, uint32_t time) {
UNUSED(time);
printf("INFO: frame callback\n");
State *state = reinterpret_cast<State *>(data);
printf("state ptr = %p\n", state->w_surface);
wl_callback_destroy(cb);
state->frame_callback = wl_surface_frame(state->w_surface);
wl_callback_add_listener(state->frame_callback, &frame_listener, state);
GLint angle_loc = glGetUniformLocation(state->program, "angle");
static float angle = 0.0f;
angle += 0.02f;
glClear(GL_COLOR_BUFFER_BIT);
glUniform1f(angle_loc, angle);
glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(state->egl_display, state->egl_surface);
}
static void xsurface_handle_configure(void *data, struct xdg_surface *surface,
uint32_t serial) {
State *app_state = reinterpret_cast<State *>(data);
xdg_surface_ack_configure(surface, serial);
app_state->frame_callback = wl_surface_frame(app_state->w_surface);
printf("frame cb: %p\n", app_state->frame_callback);
int cb_res = wl_callback_add_listener(app_state->frame_callback,
&frame_listener, app_state);
printf("mount callback listener: %d\n", cb_res);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(app_state->egl_display, app_state->egl_surface);
}
static const struct xdg_surface_listener xsurface_listener = {
.configure = xsurface_handle_configure,
};
int main(int argc, char **argv) {
UNUSED(argc);
UNUSED(argv);
State *app_state = new State;
frame_listener.done = frame_done;
app_state->display = wl_display_connect(NULL);
if (app_state->display == NULL) {
fprintf(stderr, "ERROR: could not connect to wl display\n");
return 1;
}
struct wl_registry *registry = wl_display_get_registry(app_state->display);
if (registry == NULL) {
fprintf(stderr, "ERROR: could not connect to wl registry\n");
return 1;
}
int handle = wl_registry_add_listener(registry, &listener, app_state);
printf("registry listener bound: %d\n", handle);
wl_display_roundtrip(app_state->display);
app_state->w_surface = wl_compositor_create_surface(app_state->compositor);
if (app_state->w_surface == NULL) {
fprintf(stderr, "ERROR: could not connect create a surface\n");
return 1;
}
PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay =
reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(
eglGetProcAddress("eglGetPlatformDisplayEXT"));
PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurface =
reinterpret_cast<PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC>(
eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"));
app_state->egl_display =
getPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, app_state->display, NULL);
if (app_state->egl_display == NULL) {
fprintf(stderr, "ERROR: could not get platform display\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
if (!eglInitialize(app_state->egl_display, NULL, NULL)) {
fprintf(stderr, "ERROR: could not initialize EGL\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
EGLint config_attribs[] = {EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_NONE};
EGLConfig config;
EGLint num_configs;
if (!eglChooseConfig(app_state->egl_display, config_attribs, &config, 1,
&num_configs)) {
fprintf(stderr, "ERROR: could not choose EGL config\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
EGLint ctx_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
EGLContext context = eglCreateContext(app_state->egl_display, config,
EGL_NO_CONTEXT, ctx_attribs);
if (context == NULL) {
fprintf(stderr, "ERROR: could not create EGL context\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
app_state->egl_window =
wl_egl_window_create(app_state->w_surface, WIDTH, HEIGHT);
if (app_state->egl_window == NULL) {
fprintf(stderr, "ERROR: could not create wl_egl_window\n");
exit(1);
}
app_state->egl_surface = createPlatformWindowSurface(
app_state->egl_display, config, app_state->egl_window, NULL);
if (app_state->egl_surface == NULL) {
fprintf(stderr, "ERROR: could not create EGL window surface\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
if (!eglMakeCurrent(app_state->egl_display, app_state->egl_surface,
app_state->egl_surface, context)) {
fprintf(stderr, "ERROR: could not change current EGL context\n");
fprintf(stderr, "EGL error: 0x%x\n", eglGetError());
exit(1);
}
app_state->program = create_program(vs_src, fs_src);
glUseProgram(app_state->program);
GLint pos_loc = glGetAttribLocation(app_state->program, "pos");
// GLint angle_loc = glGetUniformLocation(app_state->program, "angle");
float vertices[] = {0.0f, 0.6f, -0.6f, -0.6f, 0.6f, -0.6f};
glEnableVertexAttribArray(pos_loc);
glVertexAttribPointer(pos_loc, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glViewport(0, 0, WIDTH, HEIGHT);
glClearColor(0.2f, 0.4f, 0.8f, 1.0f);
app_state->x_surface =
xdg_wm_base_get_xdg_surface(app_state->wm_base, app_state->w_surface);
if (app_state->x_surface == NULL) {
fprintf(stderr, "ERROR: could not get an xdg surface\n");
return 1;
}
app_state->toplevel = xdg_surface_get_toplevel(app_state->x_surface);
if (app_state->toplevel == NULL) {
fprintf(stderr, "ERROR: could not get an xdg toplevel\n");
return 1;
}
xdg_surface_add_listener(app_state->x_surface, &xsurface_listener, app_state);
app_state->ready = true;
wl_surface_commit(app_state->w_surface);
while (wl_display_dispatch(app_state->display)) {
}
Window window(720, 480);
return 0;
}