From de18bd82520ba154704397875db2489746f83cf6 Mon Sep 17 00:00:00 2001 From: Roman Pytkov Date: Mon, 17 Nov 2025 14:05:42 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B4=D0=B3=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= =?UTF-8?q?=20=D1=84=D0=B8=D0=B3=D1=83=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nasm.code-workspace | 9 ++++++- wayland/include/figure.h | 21 ++++++++++++++++ wayland/include/geomerty.h | 9 +++++++ wayland/include/window.h | 15 ++++++++--- wayland/src/window.c | 51 +++++++++++++++++++++----------------- 5 files changed, 77 insertions(+), 28 deletions(-) create mode 100644 wayland/include/figure.h create mode 100644 wayland/include/geomerty.h diff --git a/nasm.code-workspace b/nasm.code-workspace index e4325b9..4d2ea5a 100644 --- a/nasm.code-workspace +++ b/nasm.code-workspace @@ -16,7 +16,14 @@ "editor.codeLens": false, "files.associations": { "flake.lock": "json", - "ios": "c" + "ios": "c", + "cstdint": "c", + "array": "c", + "string": "c", + "string_view": "c", + "ranges": "c", + "span": "c", + "vector": "c" }, } } diff --git a/wayland/include/figure.h b/wayland/include/figure.h new file mode 100644 index 0000000..c25c1cc --- /dev/null +++ b/wayland/include/figure.h @@ -0,0 +1,21 @@ +#ifndef FIGURE_H +#define FIGURE_H + +#include "geomerty.h" + +enum figure_type +{ + FIGURE_CIRCLE = 0, + FIGURE_TRIANGLE = 1, + FIGURE_SQUARE = 2 +}; + +struct figure_info { + enum figure_type type; + struct vec2 position; + struct vec2 velocity; + float rotation; + float rotation_speed; +}; + +#endif diff --git a/wayland/include/geomerty.h b/wayland/include/geomerty.h new file mode 100644 index 0000000..82cb72e --- /dev/null +++ b/wayland/include/geomerty.h @@ -0,0 +1,9 @@ +#ifndef GEOMETRY_H +#define GEOMETRY_H + +struct vec2 { + float x; + float y; +}; + +#endif diff --git a/wayland/include/window.h b/wayland/include/window.h index 4accfa4..ed3dda6 100644 --- a/wayland/include/window.h +++ b/wayland/include/window.h @@ -2,6 +2,15 @@ #define WAYLAND_WINDOW_H #include +#include "figure.h" + +struct window_draw_info { + uint8_t *data; + int32_t width; + int32_t height; + uint8_t color; + struct figure_info figure; +}; /* Данные одного Wayland-окна (одна поверхность) */ struct wayland_window { @@ -12,13 +21,11 @@ struct wayland_window { struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; struct wl_event_queue *queue; /* очередь событий для окна */ - uint8_t *data; - int32_t width; - int32_t height; + struct window_draw_info draw_info; int need_close; - uint8_t color; }; + /* Инициализация окна; структура предоставляется вызывающим */ int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wayland_window *win); diff --git a/wayland/src/window.c b/wayland/src/window.c index 53607b1..7f5f274 100644 --- a/wayland/src/window.c +++ b/wayland/src/window.c @@ -44,8 +44,9 @@ static void destroy_frame_callback(struct wayland_window *win) static void resize_canvas(struct wayland_window *win) { - size_t stride = win->width * 4; - size_t size = stride * win->height; + struct window_draw_info* draw_info = &win->draw_info; + size_t stride = draw_info->width * 4; + size_t size = stride * draw_info->height; int32_t fd = alloc_shm(size); if (fd == -1) return; @@ -64,42 +65,46 @@ static void resize_canvas(struct wayland_window *win) } struct wl_shm *shm = registry_get_shm(); struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); - win->buffer = wl_shm_pool_create_buffer(pool, 0, win->width, win->height, stride, WL_SHM_FORMAT_ARGB8888); + win->buffer = wl_shm_pool_create_buffer(pool, 0, draw_info->width, draw_info->height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); - win->data = map; + draw_info->data = map; } static void resize_new(struct wayland_window *win, int32_t w, int32_t h) { if (!w || !h) return; - if (win->width != w || win->height != h) + struct window_draw_info* draw_info = &win->draw_info; + + if (draw_info->width != w || draw_info->height != h) { - if (win->data) + if (draw_info->data) { - munmap(win->data, win->width * win->height * 4); - win->data = NULL; + munmap(draw_info->data, draw_info->width * draw_info->height * 4); + draw_info->data = NULL; } - win->width = w; - win->height = h; + draw_info->width = w; + draw_info->height = h; resize_canvas(win); } } static void draw(struct wayland_window *win) { - size_t stride = win->width * 4; - size_t size = stride * win->height; + struct window_draw_info* draw_info = &win->draw_info; - if (!win->data || !win->buffer) + size_t stride = draw_info->width * 4; + size_t size = stride * draw_info->height; + + if (!draw_info->data || !win->buffer) return; - memset(win->data, win->color++, size); + memset(draw_info->data, draw_info->color++, size); wl_surface_attach(win->wl_surface, win->buffer, 0, 0); - wl_surface_damage_buffer(win->wl_surface, 0, 0, win->width, win->height); + wl_surface_damage_buffer(win->wl_surface, 0, 0, draw_info->width, draw_info->height); wl_surface_commit(win->wl_surface); } @@ -107,7 +112,7 @@ static void xdg_surface_conf(void *data, struct xdg_surface *xdg_surface_local, { xdg_surface_ack_configure(xdg_surface_local, serial); struct wayland_window *win = data; - if (!win->data) + if (!win->draw_info.data) resize_canvas(win); draw(win); @@ -202,17 +207,17 @@ int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wl_surface_commit(win->wl_surface); - /* wm_base — глобальный объект, listener в registry */ + struct window_draw_info* draw_info = &win->draw_info; /* Инициализация состояния */ win->id = id++; - win->width = 400; - win->height = 250; - win->data = NULL; + draw_info->width = 400; + draw_info->height = 250; + draw_info->data = NULL; win->buffer = NULL; win->frame_callback = NULL; win->need_close = 0; - win->color = 0; + draw_info->color = 0; xdg_toplevel_set_app_id(win->xdg_toplevel, "my-wayland-window"); xdg_toplevel_set_title(win->xdg_toplevel, "Custom Title"); @@ -236,6 +241,6 @@ void window_destroy(struct wayland_window *win) xdg_surface_destroy(win->xdg_surface); if (win->wl_surface) wl_surface_destroy(win->wl_surface); - if (win->data) - munmap(win->data, win->width * win->height * 4); + if (win->draw_info.data) + munmap(win->draw_info.data, win->draw_info.width * win->draw_info.height * 4); }