diff --git a/Makefile b/Makefile index 9b7ba0d..a2aec1c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -LIBS=sdl3 opengl wayland-client +LIBS=sdl3 opengl wayland-client egl wayland-egl CFLAGS+=-g -Wall -Wextra $(shell pkg-config --cflags $(LIBS)) LDLIBS+=$(shell pkg-config --libs $(LIBS)) diff --git a/glonkers.c b/glonkers.c index a68c9a5..913ea5a 100644 --- a/glonkers.c +++ b/glonkers.c @@ -11,22 +11,11 @@ #include "renderer.h" #include "shm.h" #include "xdg-shell-protocol.h" - -//int main() { -// struct wl_display *display = wl_display_connect(NULL); -// if (!display) { -// fprintf(stderr, "Failed to connect to Wayland display.\n"); -// return 1; -// } -// fprintf(stderr, "Connection established!\n"); -// -// while (wl_display_dispatch(display) != -1) { -// } -// -// wl_display_disconnect(display); -//} +#include +#include struct client_state { + int width, height; /* Globals */ struct wl_display *wl_display; struct wl_registry *wl_registry; @@ -37,6 +26,8 @@ struct client_state { struct wl_surface *wl_surface; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; + + struct wl_egl_window *egl_window; }; static void @@ -54,9 +45,8 @@ static const struct wl_buffer_listener wl_buffer_listener = { static struct wl_buffer * draw_frame(struct client_state *state) { - const int width = 640, height = 480; - int stride = width * 4; - int size = stride * height; + int stride = state->width * 4; + int size = stride * state->height; int fd = allocate_shm_file(size); if (fd == -1) { @@ -72,17 +62,17 @@ draw_frame(struct client_state *state) struct wl_shm_pool *pool = wl_shm_create_pool(state->wl_shm, fd, size); struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, - width, height, stride, WL_SHM_FORMAT_XRGB8888); + state->width, state->height, stride, WL_SHM_FORMAT_XRGB8888); wl_shm_pool_destroy(pool); close(fd); /* Draw checkerboxed background */ - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { + for (int y = 0; y < state->height; ++y) { + for (int x = 0; x < state->width; ++x) { if ((x + y / 8 * 8) % 16 < 8) - data[y * width + x] = 0xFF666666; + data[y * state->width + x] = 0xFF666666; else - data[y * width + x] = 0xFFEEEEEE; + data[y * state->width + x] = 0xFFEEEEEE; } } @@ -154,17 +144,61 @@ wl_registry_listener = { .global_remove = registry_handle_global_remove, }; +//static void egl_init(struct client_state *state) { +// EGLint major; +// EGLint minor; +// EGLint num_configs; +// EGLint attribs[] = { +// EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, +// EGL_NONE +// }; +// +// state->egl_window = wl_egl_window_create(state->wl_surface, state->width, +// state->height); +// +// state->egl_display = eglGetDisplay((EGLNativeDisplayType) state->display); +// if(state->display == EGL_NO_DISPLAY) { +// fprintf(stderr, "Couldn't get EGL display\n"); +// exit(EXIT_FAILURE); +// } +// +// if(eglInitialize(state->egl_display, &major, &minor) != EGL_TRUE) { +// fprintf(stderr, "Couldnt initialize EGL\n"); +// exit(EXIT_FAILURE); +// } +// +// if(eglChooseConfig(state->egl_display, attribs, &state->egl_config, 1, +// &num_configs) != EGL_TRUE) { +// fprintf(stderr, "CouldnÄt find matching EGL config\n"); +// exit(EXIT_FAILURE); +// } +// +// state->egl_surface = eglCreateWindowSurface(state->egl_display, +// state->egl_config, +// (EGLNativeWindowType) state->egl_window, NULL); +// if(state->egl_surface == EGL_NO_SURFACE) { +// fprintf(stderr, "Couldn't create EGL surface\n"); +// exit(EXIT_FAILURE); +// } +// +// state->egl_context = eglCreateContext(state->egl_display, state->egl_config, +// EGL_NO_CONTEXT, NULL); +// if(state->egl_context == EGL_NO_CONTEXT) { +// fprintf(stderr, "Couldn't create EGL context\n"); +// exit(EXIT_FAILURE); +// } +// +// if(!eglMakeCurrent(state->egl_display, state->egl_surface, +// state->egl_surface, state->egl_context)) { +// fprintf(stderr, "Couldn't make EGL context current\n"); +// exit(EXIT_FAILURE); +// } +//} + int main(int argc, char *argv[]) { - //struct our_state state = { 0 }; - //struct wl_display *display = wl_display_connect(NULL); - //struct wl_registry *registry = wl_display_get_registry(display); - //wl_registry_add_listener(registry, ®istry_listener, &state); - //wl_display_roundtrip(display); - //return 0; - - struct client_state state = { 0 }; + struct client_state state = { 700, 450, 0 }; state.wl_display = wl_display_connect(NULL); state.wl_registry = wl_display_get_registry(state.wl_display); wl_registry_add_listener(state.wl_registry, &wl_registry_listener, &state); @@ -175,7 +209,7 @@ main(int argc, char *argv[]) state.xdg_wm_base, state.wl_surface); xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state); state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface); - xdg_toplevel_set_title(state.xdg_toplevel, "Example client"); + xdg_toplevel_set_title(state.xdg_toplevel, "GLONKERS!"); wl_surface_commit(state.wl_surface); while (wl_display_dispatch(state.wl_display)) { @@ -184,102 +218,3 @@ main(int argc, char *argv[]) return 0; } - -//struct timespec program_start; -// -//void init_timer() { -// clock_gettime(CLOCK_MONOTONIC, &program_start); -//} -// -//double time_since_start() { -// struct timespec now; -// clock_gettime(CLOCK_MONOTONIC, &now); -// -// return (now.tv_sec - program_start.tv_sec) + -// (now.tv_nsec - program_start.tv_nsec) / 1e9; -//} -// -//_Atomic bool running = true; -// -//void intHandler(int dummy) { -// printf("exiting\n"); -// running = false; -// fclose(stdin); -//} -// -//bool reload_shader = false; -// -///// reads stdin on a separate thread. -//void *read_stdin(void *ptr) { -// char buff[10]; -// while (running) { -// fgets(buff, 10, stdin); -// if (strcmp(buff, "r\n") == 0) { -// printf("reloading shader\n"); -// reload_shader = true; -// } -// } -// return NULL; -//} -// -//int main(int argc, char *argv[]) { -// signal(SIGINT, intHandler); -// pthread_t stdin_thread; -// pthread_create( &stdin_thread, NULL, read_stdin, NULL); -// -// init_timer(); -// -// printf("Good Morning 🍵\n"); -// SDL_Init(SDL_INIT_VIDEO); -// SDL_SetHint(SDL_HINT_OPENGL_ES_DRIVER, "1"); -// SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, -// SDL_GL_CONTEXT_PROFILE_ES); -// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); -// SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); -// SDL_Window* window = SDL_CreateWindow("SDL3 OpenGL Cube", -// 800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); -// if (!window) { -// printf("CreateWindow Error: %s\n", SDL_GetError()); -// SDL_Quit(); -// return 1; -// } -// -// SDL_GLContext glContext = SDL_GL_CreateContext(window); -// -// if (!glContext) { -// printf("GL Context Error: %s\n", SDL_GetError()); -// SDL_DestroyWindow(window); -// SDL_Quit(); -// return 1; -// } -// -// Renderer renderer = new_renderer(); -// -// SDL_Event e; -// int w, h; -// while (running) { -// while (SDL_PollEvent(&e)) { -// if (e.type == SDL_EVENT_QUIT) running = false; -// if (e.type == SDL_EVENT_WINDOW_RESIZED) { -// SDL_GetWindowSize(window, &w, &h); -// } -// } -// -// char *shader_path = NULL; -// if (argc >= 2) { -// shader_path = argv[1]; -// } else { -// printf("need to supply path to shader\n"); -// exit(1); -// } -// -// double time = time_since_start(); -// render(&renderer, w, h, time, shader_path, reload_shader); -// if (reload_shader) reload_shader = false; -// -// SDL_GL_SwapWindow(window); -// } -// -// pthread_join(stdin_thread, NULL); -//} -// diff --git a/run.sh b/run.sh index 1ef4771..446f27e 100755 --- a/run.sh +++ b/run.sh @@ -1,3 +1,2 @@ -make -SDL_VIDEODRIVER="wayland,x11" ./glonkers "$@" +make && SDL_VIDEODRIVER="wayland,x11" ./glonkers "$@"