demos: Introduce demo_window_set_expose_func().

This commit is contained in:
Henri Verbeet 2016-11-02 16:54:44 +01:00
parent 169810d20d
commit e467b02b29
3 changed files with 27 additions and 14 deletions

View File

@ -43,7 +43,7 @@ struct demo_window
HWND hwnd;
struct demo *demo;
void *user_data;
void (*draw_func)(void *user_data);
void (*expose_func)(struct demo_window *window, void *user_data);
void (*key_press_func)(struct demo_window *window, demo_key key, void *user_data);
};
@ -53,7 +53,7 @@ struct demo_swapchain
};
static inline struct demo_window *demo_window_create(struct demo *demo, const char *title,
unsigned int width, unsigned int height, void (*draw_func)(void *user_data), void *user_data)
unsigned int width, unsigned int height, void *user_data)
{
RECT rect = {0, 0, width, height};
struct demo_window *window;
@ -73,8 +73,8 @@ static inline struct demo_window *demo_window_create(struct demo *demo, const ch
MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w, title_size);
window->instance = GetModuleHandle(NULL);
window->draw_func = draw_func;
window->user_data = user_data;
window->expose_func = NULL;
window->key_press_func = NULL;
style = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE;
@ -117,8 +117,8 @@ static inline LRESULT CALLBACK demo_window_proc(HWND hwnd, UINT message, WPARAM
switch (message)
{
case WM_PAINT:
if (window && window->draw_func)
window->draw_func(window->user_data);
if (window && window->expose_func)
window->expose_func(window, window->user_data);
return 0;
case WM_KEYDOWN:
@ -142,6 +142,12 @@ static inline void demo_window_set_key_press_func(struct demo_window *window,
window->key_press_func = key_press_func;
}
static inline void demo_window_set_expose_func(struct demo_window *window,
void (*expose_func)(struct demo_window *window, void *user_data))
{
window->expose_func = expose_func;
}
static inline void demo_process_events(struct demo *demo)
{
MSG msg = {0};

View File

@ -49,7 +49,7 @@ struct demo_window
struct demo *demo;
void *user_data;
void (*draw_func)(void *user_data);
void (*expose_func)(struct demo_window *window, void *user_data);
void (*key_press_func)(struct demo_window *window, demo_key key, void *user_data);
};
@ -132,7 +132,7 @@ static inline struct demo_window *demo_find_window(struct demo *demo, xcb_window
}
static inline struct demo_window *demo_window_create(struct demo *demo, const char *title,
unsigned int width, unsigned int height, void (*draw_func)(void *user_data), void *user_data)
unsigned int width, unsigned int height, void *user_data)
{
static const uint32_t window_events = XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_KEY_PRESS;
@ -151,8 +151,8 @@ static inline struct demo_window *demo_window_create(struct demo *demo, const ch
window->window = xcb_generate_id(demo->connection);
window->demo = demo;
window->draw_func = draw_func;
window->user_data = user_data;
window->expose_func = NULL;
window->key_press_func = NULL;
screen = xcb_setup_roots_iterator(xcb_get_setup(demo->connection)).data;
xcb_create_window(demo->connection, XCB_COPY_FROM_PARENT, window->window, screen->root, 0, 0,
@ -191,6 +191,12 @@ static inline void demo_window_set_key_press_func(struct demo_window *window,
window->key_press_func = key_press_func;
}
static inline void demo_window_set_expose_func(struct demo_window *window,
void (*expose_func)(struct demo_window *window, void *user_data))
{
window->expose_func = expose_func;
}
static inline void demo_process_events(struct demo *demo)
{
const struct xcb_client_message_event_t *client_message;
@ -206,8 +212,9 @@ static inline void demo_process_events(struct demo *demo)
switch (XCB_EVENT_RESPONSE_TYPE(event))
{
case XCB_EXPOSE:
if ((window = demo_find_window(demo, ((struct xcb_expose_event_t *)event)->window)))
window->draw_func(window->user_data);
if ((window = demo_find_window(demo, ((struct xcb_expose_event_t *)event)->window))
&& window->expose_func)
window->expose_func(window, window->user_data);
break;
case XCB_KEY_PRESS:

View File

@ -150,7 +150,7 @@ static void cxt_wait_for_previous_frame(struct cx_triangle *cxt)
cxt->frame_idx = demo_swapchain_get_current_back_buffer_index(cxt->swapchain);
}
static void cxt_render_frame(void *user_data)
static void cxt_render_frame(struct demo_window *window, void *user_data)
{
struct cx_triangle *cxt = user_data;
@ -374,8 +374,8 @@ static int cxt_main(void)
if (!demo_init(&cxt.demo))
return EXIT_FAILURE;
cxt.window = demo_window_create(&cxt.demo, "Vkd3d Triangle",
width, height, cxt_render_frame, &cxt);
cxt.window = demo_window_create(&cxt.demo, "Vkd3d Triangle", width, height, &cxt);
demo_window_set_expose_func(cxt.window, cxt_render_frame);
demo_window_set_key_press_func(cxt.window, cxt_key_press);
cxt.width = width;