-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_dwm.c
More file actions
198 lines (165 loc) · 4.43 KB
/
R_dwm.c
File metadata and controls
198 lines (165 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "R_dwm.h"
#include "RR.h"
#include "RR_context.h"
#include "RR_renderer.h"
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include "Conscreen/List/Heap.h"
typedef struct{
float x,y;
} f32vec2;
enum DWM_window_point_type{
DWM_ABSOLUTE,
DWM_RELATIVE
};
typedef struct{
enum DWM_window_point_type type;
union{
RR_point absolute;
f32vec2 relative;
};
} DWM_point;
typedef struct _DWM_window {
DWM_point pos;
DWM_point size;
int z_depth;
RR_context render_chain;
} Window;
static int16_t min(int16_t a, int16_t b)
{
if(a<b) return a;
return b;
}
struct Rect {
RR_point pos, size;
};
static struct Rect DWM_window_rect(RR_context ctx, DWM_window w)
{
struct Rect rect;
RR_point parrent_size = RR_size_get(ctx);
f32vec2 pos;
RR_point size;
switch (w->pos.type)
{
case DWM_ABSOLUTE:
pos.x = w->pos.absolute.x;
pos.y = w->pos.absolute.y;
break;
case DWM_RELATIVE: {
pos.x = parrent_size.x * w->pos.relative.x;
pos.y = parrent_size.y * w->pos.relative.y;
break;
}
}
rect.pos.x = ceilf(pos.x);
rect.pos.y = ceilf(pos.y);
switch (w->size.type)
{
case DWM_ABSOLUTE:
size.x = w->size.absolute.x;
size.y = w->size.absolute.y;
break;
case DWM_RELATIVE:
size.x = ceilf((w->size.relative.x*parrent_size.x)-pos.x+rect.pos.x);
size.y = ceilf((w->size.relative.y*parrent_size.y)-pos.y+rect.pos.y);
break;
}
rect.size.x = min(size.x, parrent_size.x-pos.x);
rect.size.y = min(size.y, parrent_size.y-pos.y);
return rect;
}
struct DWM_render_info {
RR_context origin;
RR_point pos;
};
static void dwm_forward_set(RR_context ctx, int16_t x, int16_t y, RR_pixel p)
{
struct DWM_render_info *info = RR_data_get(ctx);
RR_set(info->origin, x+info->pos.x, y+info->pos.y, p);
}
static RR_pixel dwm_forward_get(RR_context ctx, int16_t x, int16_t y)
{
struct DWM_render_info *info = RR_data_get(ctx);
return RR_get(info->origin, x+info->pos.x, y+info->pos.y);
}
static void DWM_internal_window_render(RR_context ctx, DWM_window w)
{
if(!w->render_chain) return;
struct Rect rect = DWM_window_rect(ctx, w);
struct DWM_render_info info = {ctx, rect.pos};
/* RR_render(ctx, RR_size_get(ctx), RR_get_default, RR_set_default, NULL); */
RR_render(w->render_chain,
rect.size,
dwm_forward_get,
dwm_forward_set,
&info);
}
static void RR_renderer_dynamicWM(RR_context context, void *data){
HEAP_LOOP(DWM_window, data, ptr){
DWM_internal_window_render(context, *ptr);
}
}
static int DWM_internal_window_depth_cmp(void* a, void* b)
{
DWM_window _a=*(DWM_window*)a, _b=*(DWM_window*)b;
return _a->z_depth < _b->z_depth;
}
RR_renderer R_dwm(){
RR_renderer renderer = RR_renderer_create(RR_renderer_dynamicWM);
RR_renderer_data_set(renderer,
Heap_create(sizeof(DWM_window), DWM_internal_window_depth_cmp));
return renderer;
}
void R_dwm_free(RR_renderer r)
{
Heap_free(RR_renderer_data_get(r));
RR_renderer_free(r);
}
void DWM_register(RR_renderer dwm, DWM_window w, int z_pos) {
w->z_depth = z_pos;
Heap_add(RR_renderer_data_get(dwm), &w);
}
void DWM_unregister(RR_renderer dwm, DWM_window w) {
Heap_rme(RR_renderer_data_get(dwm), &w);
}
DWM_window DWM_window_create() {
DWM_window w = malloc(sizeof(struct _DWM_window));
w->render_chain=NULL;
w->pos = (DWM_point) {
.type = DWM_RELATIVE,
.relative = {0,0}};
w->size = (DWM_point) {
.type = DWM_RELATIVE,
.relative = {1,1}};
w->z_depth=0;
return w;
}
// TODO refactor DWM window -> managed by DWM
void DWM_window_free(DWM_window win)
{
free(win);
}
void DWM_window_pos_set_abs(DWM_window w, int16_t x, int16_t y) {
w->pos.type = DWM_ABSOLUTE;
w->pos.absolute.x = x;
w->pos.absolute.y = y;
}
void DWM_window_pos_set_rel(DWM_window w, float x, float y) {
w->pos.type = DWM_RELATIVE;
w->pos.relative.x = x;
w->pos.relative.y = y;
}
void DWM_window_size_set_abs(DWM_window w, int16_t x, int16_t y) {
w->size.type = DWM_ABSOLUTE;
w->size.absolute.x = x;
w->size.absolute.y = y;
}
void DWM_window_size_set_rel(DWM_window w, float x, float y) {
w->size.type = DWM_RELATIVE;
w->size.relative.x = x;
w->size.relative.y = y;
}
void DWM_window_renderer_set(DWM_window w, RR_context ctx) {
w->render_chain = ctx;
}