signal handeling and properly close swapchain
This commit is contained in:
parent
0aa6e65ea6
commit
3c25e6a318
2 changed files with 28 additions and 8 deletions
11
src/main.c
11
src/main.c
|
@ -3,6 +3,13 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
static bool close = false;
|
||||||
|
|
||||||
|
static void signal_handler() {
|
||||||
|
close = true;
|
||||||
|
}
|
||||||
|
|
||||||
int main (void) {
|
int main (void) {
|
||||||
Screen screen;
|
Screen screen;
|
||||||
|
@ -10,8 +17,10 @@ int main (void) {
|
||||||
|
|
||||||
Camera camera;
|
Camera camera;
|
||||||
init_camera(&camera);
|
init_camera(&camera);
|
||||||
|
|
||||||
|
signal(SIGINT, signal_handler);
|
||||||
|
|
||||||
while (poll_screen(&screen)) {
|
while (poll_screen(&screen) && !close) {
|
||||||
render(&screen, &camera);
|
render(&screen, &camera);
|
||||||
update_camera(&camera, &screen);
|
update_camera(&camera, &screen);
|
||||||
|
|
||||||
|
|
25
src/screen.c
25
src/screen.c
|
@ -1,5 +1,4 @@
|
||||||
#include "screen.h"
|
#include "screen.h"
|
||||||
#include <sched.h>
|
|
||||||
#undef Screen
|
#undef Screen
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
@ -7,9 +6,9 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/select.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
#define XLIB_ILLEGAL_ACCESS
|
#define XLIB_ILLEGAL_ACCESS
|
||||||
#include <X11/X.h>
|
#include <X11/X.h>
|
||||||
|
@ -123,16 +122,20 @@ static Swapchain create_swapchain(int width, int height, int frames, int bit_dep
|
||||||
return swapchain;
|
return swapchain;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void free_swapchain(Swapchain swapchain, WindowState* state) {
|
static void free_swapchain(Swapchain* swapchain, WindowState* state) {
|
||||||
for (uint8_t i = 0; i < swapchain.image_count; i++) {
|
pthread_mutex_lock(&state->recreate_lock);
|
||||||
|
for (uint8_t i = 0; i < swapchain->image_count; i++) {
|
||||||
XDestroyImage(state->images[i]); // also frees pixel buffer :3
|
XDestroyImage(state->images[i]); // also frees pixel buffer :3
|
||||||
}
|
}
|
||||||
|
|
||||||
free(swapchain.images);
|
free(swapchain->images);
|
||||||
free(state->images);
|
free(state->images);
|
||||||
|
swapchain->images = NULL;
|
||||||
|
|
||||||
XFreePixmap(dpy, state->pixel_map);
|
XFreePixmap(dpy, state->pixel_map);
|
||||||
XFreeGC(dpy, state->graphics_ctx);
|
XFreeGC(dpy, state->graphics_ctx);
|
||||||
|
|
||||||
|
pthread_mutex_unlock(&state->recreate_lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void recreate_swapchain(int width, int height, int bit_depth, Swapchain* swapchain, WindowState* state) {
|
static void recreate_swapchain(int width, int height, int bit_depth, Swapchain* swapchain, WindowState* state) {
|
||||||
|
@ -186,8 +189,13 @@ static void* swapchain_thread(void* arg) {
|
||||||
WindowState* state = (WindowState*) screen->internal;
|
WindowState* state = (WindowState*) screen->internal;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
pthread_mutex_lock(&state->recreate_lock);
|
|
||||||
|
if (screen->swapchain.images == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
pthread_mutex_lock(&state->recreate_lock);
|
||||||
|
|
||||||
pthread_mutex_lock(&screen->swapchain.lock);
|
pthread_mutex_lock(&screen->swapchain.lock);
|
||||||
int index = screen->swapchain.image_recent;
|
int index = screen->swapchain.image_recent;
|
||||||
screen->swapchain.image_front = index;
|
screen->swapchain.image_front = index;
|
||||||
|
@ -294,9 +302,12 @@ bool poll_screen(struct Screen* screen) {
|
||||||
void free_screen(struct Screen* screen) {
|
void free_screen(struct Screen* screen) {
|
||||||
WindowState* state = (WindowState*) screen->internal;
|
WindowState* state = (WindowState*) screen->internal;
|
||||||
|
|
||||||
|
free_swapchain(&screen->swapchain, state);
|
||||||
pthread_exit(&state->update_thread);
|
pthread_exit(&state->update_thread);
|
||||||
|
pthread_mutex_destroy(&screen->swapchain.lock);
|
||||||
|
pthread_mutex_destroy(&state->recreate_lock);
|
||||||
|
|
||||||
XUnmapWindow(dpy, state->window);
|
XUnmapWindow(dpy, state->window);
|
||||||
free_swapchain(screen->swapchain, state);
|
|
||||||
XDestroyWindow(dpy, state->window);
|
XDestroyWindow(dpy, state->window);
|
||||||
free(screen->internal);
|
free(screen->internal);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue