отдельный поток для физики и изменение цвета

This commit is contained in:
2025-11-17 14:38:03 +03:00
parent de18bd8252
commit 8dd2aa19ad
5 changed files with 99 additions and 22 deletions

View File

@@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <wayland-client.h>
#include <unistd.h>
#include "input.h"
#include "registry.h"
@@ -16,6 +17,7 @@
struct window_thread_slot
{
pthread_t thread;
pthread_t aux_thread;
int active;
struct wl_event_queue *queue;
struct wayland_window window;
@@ -38,6 +40,21 @@ static void signal_thread_exit(struct window_thread_slot *slot)
pthread_mutex_unlock(&g_thread_lock);
}
static void *window_aux_loop(void *arg)
{
struct window_thread_slot *slot = arg;
struct window_draw_info *draw_info = &slot->window.draw_info;
while (!atomic_load(&g_shutdown) && !window_should_close(&slot->window))
{
/* На время обновления позиции фигуры локаем мутекс */
pthread_mutex_lock(&draw_info->figure_mutex);
usleep(10 * 1000);
pthread_mutex_unlock(&draw_info->figure_mutex);
usleep(100 * 1000);
}
return NULL;
}
static void *window_thread_main(void *arg)
{
struct window_thread_slot *slot = arg;
@@ -50,7 +67,6 @@ static void *window_thread_main(void *arg)
return NULL;
}
if (window_init(g_display, slot->queue, &slot->window) < 0)
{
wl_event_queue_destroy(slot->queue);
@@ -59,6 +75,12 @@ static void *window_thread_main(void *arg)
return NULL;
}
/* Запуск вспомогательного потока, который пока просто крутится с паузой 100 ms.
* Поток завершится, как только окно будет помечено как закрыто. */
int aux_res = pthread_create(&slot->aux_thread, NULL, window_aux_loop, slot);
if (aux_res == 0)
pthread_detach(slot->aux_thread);
while (!atomic_load(&g_shutdown) && !window_should_close(&slot->window))
{
int dispatch = wl_display_dispatch_queue(g_display, slot->queue);
@@ -176,10 +198,10 @@ void destroy_wayland(void)
atomic_store(&g_shutdown, 0);
}
struct wayland_window* get_window_by_surface(struct wl_surface* surf)
struct wayland_window *get_window_by_surface(struct wl_surface *surf)
{
for(int i = 0; i < MAX_WINDOW_THREADS; i++)
if(g_slots[i].window.wl_surface == surf)
for (int i = 0; i < MAX_WINDOW_THREADS; i++)
if (g_slots[i].window.wl_surface == surf)
return &g_slots[i].window;
return NULL;
}