Подготовлены файлы фигуры

This commit is contained in:
2025-11-17 14:05:42 +03:00
parent c21df94757
commit de18bd8252
5 changed files with 77 additions and 28 deletions

View File

@@ -16,7 +16,14 @@
"editor.codeLens": false, "editor.codeLens": false,
"files.associations": { "files.associations": {
"flake.lock": "json", "flake.lock": "json",
"ios": "c" "ios": "c",
"cstdint": "c",
"array": "c",
"string": "c",
"string_view": "c",
"ranges": "c",
"span": "c",
"vector": "c"
}, },
} }
} }

21
wayland/include/figure.h Normal file
View File

@@ -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

View File

@@ -0,0 +1,9 @@
#ifndef GEOMETRY_H
#define GEOMETRY_H
struct vec2 {
float x;
float y;
};
#endif

View File

@@ -2,6 +2,15 @@
#define WAYLAND_WINDOW_H #define WAYLAND_WINDOW_H
#include <wayland-client.h> #include <wayland-client.h>
#include "figure.h"
struct window_draw_info {
uint8_t *data;
int32_t width;
int32_t height;
uint8_t color;
struct figure_info figure;
};
/* Данные одного Wayland-окна (одна поверхность) */ /* Данные одного Wayland-окна (одна поверхность) */
struct wayland_window { struct wayland_window {
@@ -12,13 +21,11 @@ struct wayland_window {
struct xdg_surface *xdg_surface; struct xdg_surface *xdg_surface;
struct xdg_toplevel *xdg_toplevel; struct xdg_toplevel *xdg_toplevel;
struct wl_event_queue *queue; /* очередь событий для окна */ struct wl_event_queue *queue; /* очередь событий для окна */
uint8_t *data; struct window_draw_info draw_info;
int32_t width;
int32_t height;
int need_close; int need_close;
uint8_t color;
}; };
/* Инициализация окна; структура предоставляется вызывающим */ /* Инициализация окна; структура предоставляется вызывающим */
int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wayland_window *win); int window_init(struct wl_display *display, struct wl_event_queue *queue, struct wayland_window *win);

View File

@@ -44,8 +44,9 @@ static void destroy_frame_callback(struct wayland_window *win)
static void resize_canvas(struct wayland_window *win) static void resize_canvas(struct wayland_window *win)
{ {
size_t stride = win->width * 4; struct window_draw_info* draw_info = &win->draw_info;
size_t size = stride * win->height; size_t stride = draw_info->width * 4;
size_t size = stride * draw_info->height;
int32_t fd = alloc_shm(size); int32_t fd = alloc_shm(size);
if (fd == -1) if (fd == -1)
return; return;
@@ -64,42 +65,46 @@ static void resize_canvas(struct wayland_window *win)
} }
struct wl_shm *shm = registry_get_shm(); struct wl_shm *shm = registry_get_shm();
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); 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); wl_shm_pool_destroy(pool);
close(fd); close(fd);
win->data = map; draw_info->data = map;
} }
static void resize_new(struct wayland_window *win, int32_t w, int32_t h) static void resize_new(struct wayland_window *win, int32_t w, int32_t h)
{ {
if (!w || !h) if (!w || !h)
return; 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); munmap(draw_info->data, draw_info->width * draw_info->height * 4);
win->data = NULL; draw_info->data = NULL;
} }
win->width = w; draw_info->width = w;
win->height = h; draw_info->height = h;
resize_canvas(win); resize_canvas(win);
} }
} }
static void draw(struct wayland_window *win) static void draw(struct wayland_window *win)
{ {
size_t stride = win->width * 4; struct window_draw_info* draw_info = &win->draw_info;
size_t size = stride * win->height;
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; 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_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); 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); xdg_surface_ack_configure(xdg_surface_local, serial);
struct wayland_window *win = data; struct wayland_window *win = data;
if (!win->data) if (!win->draw_info.data)
resize_canvas(win); resize_canvas(win);
draw(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); wl_surface_commit(win->wl_surface);
/* wm_base — глобальный объект, listener в registry */ struct window_draw_info* draw_info = &win->draw_info;
/* Инициализация состояния */ /* Инициализация состояния */
win->id = id++; win->id = id++;
win->width = 400; draw_info->width = 400;
win->height = 250; draw_info->height = 250;
win->data = NULL; draw_info->data = NULL;
win->buffer = NULL; win->buffer = NULL;
win->frame_callback = NULL; win->frame_callback = NULL;
win->need_close = 0; 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_app_id(win->xdg_toplevel, "my-wayland-window");
xdg_toplevel_set_title(win->xdg_toplevel, "Custom Title"); 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); xdg_surface_destroy(win->xdg_surface);
if (win->wl_surface) if (win->wl_surface)
wl_surface_destroy(win->wl_surface); wl_surface_destroy(win->wl_surface);
if (win->data) if (win->draw_info.data)
munmap(win->data, win->width * win->height * 4); munmap(win->draw_info.data, win->draw_info.width * win->draw_info.height * 4);
} }