RT
libui.h
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* libui.h :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: nihuynh <nihuynh@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2018/11/28 03:32:43 by nihuynh #+# #+# */
9 /* Updated: 2019/07/25 13:43:18 by nihuynh ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #ifndef LIBUI_H
14 # define LIBUI_H
15 
16 # include <SDL.h>
17 # include <stdbool.h>
18 
19 # define THR_C 10
20 # define BATCH_SIZE 16
21 # define C_MASK 0xFF000000
22 # define P_TIME_LEN 80
23 # define GUI_FPS 60
24 
25 typedef struct s_pxl
26 {
27  int x;
28  int y;
29 } t_pxl;
30 
31 typedef struct s_data_thr t_data_thr;
32 typedef struct s_thr_pool t_thr_pool;
33 
34 typedef struct s_img
35 {
36  uint32_t *pixels;
37  int width;
38  int height;
39 } t_img;
40 
41 typedef struct s_sdl t_sdl;
42 
43 struct s_sdl
44 {
50  bool fullscreen;
51  bool layout;
52  int width_vp;
53  int height_vp;
54  int thr_len;
56  SDL_Window *win;
57  void (*key_map)(int*, SDL_Keycode, void*, bool state);
58  void (*mouse_map)(SDL_Event*, void *);
59  void (*click_map)(SDL_Event *, void *);
60  void (*update)(void *arg);
61  void (*render_gui)(void *arg);
62  void (*prep_render)(t_sdl *sdl);
63  bool sepia;
64  bool grayscale;
65  bool cartoon;
66  bool negative;
67  bool sub_s;
69  float gui_time[GUI_FPS];
70 };
71 
72 struct s_data_thr
73 {
74  uint32_t *pixels;
75  int idx;
77  int (*do_pxl) (int, int, void*);
78  void *prg_data;
79 };
80 
81 struct s_thr_pool
82 {
84  short thr_count;
85  pthread_mutex_t wait_lock;
86  pthread_cond_t wait_sig;
87  int pxl_idx;
88  int limit;
89  pthread_mutex_t idx_lock;
90  pthread_mutex_t idle_lock;
91  short idle_count;
92  pthread_cond_t render_done;
94  int (*do_pxl) (int, int, void*);
95  void *prg_data;
96  pthread_t *thrs;
97 };
98 
99 /*
100 ** SDL :
101 */
102 
103 void error_sdl(t_sdl *sdl);
104 t_sdl *init_sdl(int width, int height);
105 void exit_sdl(t_sdl *sdl);
106 void render_sdl(t_sdl *sdl, int (*f) (int, int, void*), void *d);
107 void loop_sdl(t_sdl *sdl, void *arg);
108 void save_screenshot(t_sdl *sdl, char *arg);
109 void init_mthr_sdl(t_sdl *sdl, int (*do_pxl)(int, int, void*),
110  void *data, void (*prep_render)(t_sdl*));
111 void render_mthr_sdl(t_sdl *sdl);
112 void apply_simple_filter(t_sdl *sdl, uint32_t (*fun) (uint32_t));
113 
114 void push_render_time(t_sdl *sdl, float time_frame_ms);
115 void push_gui_time(t_sdl *sdl, long new_frame);
116 
117 void mthr_task(t_data_thr *data_thr, void *(*func)(void *));
118 /*
119 ** Pool Render :
120 */
121 
122 int init_pool(t_sdl *sdl, int (*do_pxl) (int, int, void*),
123  void *prg_d, uint16_t thr_count);
124 int pool_render(t_thr_pool *pool);
125 int destroy_pool(t_thr_pool *pool);
126 
127 #endif
s_sdl
Definition: libui.h:43
t_pxl
struct s_pxl t_pxl
s_sdl::data_thr
t_data_thr * data_thr
Definition: libui.h:55
s_data_thr::sdl
t_sdl * sdl
Definition: libui.h:76
s_sdl::height_vp
int height_vp
Definition: libui.h:53
s_sdl::render_gui
void(* render_gui)(void *arg)
Definition: libui.h:61
s_sdl::gui_time
float gui_time[GUI_FPS]
Definition: libui.h:69
s_data_thr::idx
int idx
Definition: libui.h:75
push_render_time
void push_render_time(t_sdl *sdl, float time_frame_ms)
Definition: render_time.c:15
s_thr_pool::prg_data
void * prg_data
Definition: libui.h:95
s_thr_pool::idle_count
short idle_count
Definition: libui.h:91
s_sdl::sepia
bool sepia
Definition: libui.h:63
s_img::pixels
uint32_t * pixels
Definition: libui.h:36
s_thr_pool::thrs
pthread_t * thrs
Definition: libui.h:96
s_thr_pool::thr_count
short thr_count
Definition: libui.h:84
s_data_thr
Definition: libui.h:72
s_data_thr::do_pxl
int(* do_pxl)(int, int, void *)
Definition: libui.h:77
s_sdl::img
t_img img
Definition: libui.h:45
destroy_pool
int destroy_pool(t_thr_pool *pool)
Definition: render_pool.c:123
s_sdl::render_time
float render_time[P_TIME_LEN]
Definition: libui.h:68
s_thr_pool::is_stopped
int is_stopped
Definition: libui.h:83
init_sdl
t_sdl * init_sdl(int width, int height)
Definition: init_sdl.c:19
s_thr_pool::idle_lock
pthread_mutex_t idle_lock
Definition: libui.h:90
s_sdl::click_map
void(* click_map)(SDL_Event *, void *)
Definition: libui.h:59
s_img::width
int width
Definition: libui.h:37
s_img::height
int height
Definition: libui.h:38
s_thr_pool::do_pxl
int(* do_pxl)(int, int, void *)
Definition: libui.h:94
render_mthr_sdl
void render_mthr_sdl(t_sdl *sdl)
Definition: render_mthr_sdl.c:90
s_thr_pool::pxl_idx
int pxl_idx
Definition: libui.h:87
s_sdl::sub_s
bool sub_s
Definition: libui.h:67
s_sdl::width_vp
int width_vp
Definition: libui.h:52
pool_render
int pool_render(t_thr_pool *pool)
Definition: render_pool.c:101
s_sdl::layout
bool layout
Definition: libui.h:51
s_thr_pool::limit
int limit
Definition: libui.h:88
push_gui_time
void push_gui_time(t_sdl *sdl, long new_frame)
Definition: render_time.c:25
P_TIME_LEN
#define P_TIME_LEN
Definition: libui.h:22
s_sdl::update
void(* update)(void *arg)
Definition: libui.h:60
s_thr_pool::sdl
t_sdl * sdl
Definition: libui.h:93
apply_simple_filter
void apply_simple_filter(t_sdl *sdl, uint32_t(*fun)(uint32_t))
render_sdl
void render_sdl(t_sdl *sdl, int(*f)(int, int, void *), void *d)
Definition: render_sdl.c:31
s_sdl::cartoon
bool cartoon
Definition: libui.h:65
init_pool
int init_pool(t_sdl *sdl, int(*do_pxl)(int, int, void *), void *prg_d, uint16_t thr_count)
Definition: render_pool.c:70
exit_sdl
void exit_sdl(t_sdl *sdl)
Definition: exit_sdl.c:16
s_sdl::fullscreen
bool fullscreen
Definition: libui.h:50
s_data_thr::prg_data
void * prg_data
Definition: libui.h:78
s_sdl::key_map
void(* key_map)(int *, SDL_Keycode, void *, bool state)
Definition: libui.h:57
error_sdl
void error_sdl(t_sdl *sdl)
Definition: error_sdl.c:17
save_screenshot
void save_screenshot(t_sdl *sdl, char *arg)
Definition: save_screenshot.c:66
s_thr_pool
Definition: libui.h:81
s_thr_pool::wait_lock
pthread_mutex_t wait_lock
Definition: libui.h:85
s_sdl::needs_render
bool needs_render
Definition: libui.h:48
s_sdl::sub_sample
int sub_sample
Definition: libui.h:47
s_thr_pool::render_done
pthread_cond_t render_done
Definition: libui.h:92
s_sdl::thr_len
int thr_len
Definition: libui.h:54
prep_render
void prep_render(t_sdl *sdl)
Definition: pre_render.c:52
s_sdl::progress_sub_sample
float progress_sub_sample
Definition: libui.h:46
s_sdl::grayscale
bool grayscale
Definition: libui.h:64
s_sdl::negative
bool negative
Definition: libui.h:66
s_data_thr::pixels
uint32_t * pixels
Definition: libui.h:74
t_img
struct s_img t_img
GUI_FPS
#define GUI_FPS
Definition: libui.h:23
s_sdl::partial_render
bool partial_render
Definition: libui.h:49
s_pxl
Definition: libui.h:25
s_sdl::mouse_map
void(* mouse_map)(SDL_Event *, void *)
Definition: libui.h:58
s_pxl::x
int x
Definition: libui.h:27
s_img
Definition: libui.h:34
init_mthr_sdl
void init_mthr_sdl(t_sdl *sdl, int(*do_pxl)(int, int, void *), void *data, void(*prep_render)(t_sdl *))
Definition: init_mthr_sdl.c:18
s_sdl::prep_render
void(* prep_render)(t_sdl *sdl)
Definition: libui.h:62
s_sdl::win
SDL_Window * win
Definition: libui.h:56
mthr_task
void mthr_task(t_data_thr *data_thr, void *(*func)(void *))
Definition: init_mthr_sdl.c:41
s_thr_pool::idx_lock
pthread_mutex_t idx_lock
Definition: libui.h:89
s_thr_pool::wait_sig
pthread_cond_t wait_sig
Definition: libui.h:86
s_pxl::y
int y
Definition: libui.h:28
loop_sdl
void loop_sdl(t_sdl *sdl, void *arg)
Loop waiting for events to happen.
Definition: loop_sdl.c:61