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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
(** Rendering utilities. *)
open Tsdl
open Misc
include (val Log.create_src "stk.render")
[@@@landmark "auto"]
let set_draw_color rend color =
let (r,g,b,a) = Color.to_int8s color in
Sdl.set_render_draw_color rend r g b a
let with_color rend color f =
let> (r,g,b,a) = Sdl.get_render_draw_color rend in
let> () = set_draw_color rend color in
let x = f rend in
let> () = Sdl.set_render_draw_color rend r g b a in
x
let fill_rect rend rect color =
debug (fun m -> m "Render.fill_rect rect=%a color=%#lx"
(fun fmt -> function None -> () | Some r -> G.pp fmt r) rect
color);
with_color rend color
(fun r ->
let rect = Option.map G.to_rect rect in
let>() = Sdl.render_fill_rect r rect in ())
let draw_line_color rend x1 y1 x2 y2 color =
debug (fun m -> m "Render.draw_line_color x1=%d, y1=%d, x2=%d, y2=%d color=%#lx"
x1 y1 x2 y2 color);
with_color rend color
(fun r -> let>() = Sdl.render_draw_line r x1 y1 x2 y2 in ())
let draw_rect rend rect color =
debug (fun m -> m "Render.draw_rect rect=%a color=%#lx"
(fun fmt -> function None -> () | Some r -> G.pp fmt r) rect
color);
with_color rend color
(fun r ->
let rect = Option.map G.to_rect rect in
let>() = Sdl.render_draw_rect r rect in ())
let render_copy rend ~src ~dst tex =
debug (fun m -> m "Render.render_copy src=%a dst=%a" G.pp src G.pp dst);
let src = G.to_rect src in
let dst = G.to_rect dst in
Sdl.render_copy ~src ~dst rend tex
let render_borders rend ~offset:(x,y) ~g geom bw bc =
let g = G.translate ~x ~y g in
let geom = G.translate ~x ~y geom in
debug (fun m -> m "Render.render_borders offset=(%d,%d) g=%a geom=%a" x y G.pp g G.pp geom);
debug (fun m -> m "Render.render_borders width=%a color=%a"
Props.(pp_prop border_width) bw
Props.(pp_prop border_color) bc);
(let h = bw.Props.top in
if h > 0 then
match G.inter geom { g with h } with
| None -> ()
| rect -> fill_rect rend rect bc.Props.top
);
(let h = bw.Props.bottom in
if h > 0 then
match G.inter geom { g with y = g.y + g.h - h } with
| None -> ()
| rect -> fill_rect rend rect bc.Props.bottom
);
let rec render_v col op bw (g:G.t) =
if bw > 0 then
(
let g2 = { G.w = 1 ; x = op g.x (bw-1) ; y = g.y + (bw-1) ; h = g.h - (bw-1) * 2 } in
let () = match G.inter geom g2 with
| None -> ()
| rect -> fill_rect rend rect col
in
render_v col op (bw-1) g
)
in
render_v bc.left (+) bw.Props.left { g with x = g.x };
render_v bc.right (-) bw.Props.right {g with x = g.x + g.w - 1}
let draw_circle =
let draw_point rend x y =
let> () = Sdl.render_draw_point rend (truncate x) (truncate y) in
()
in
let f ~x ~y ~r rend =
let cx = float x -. 0.5 in
let cy = float y -. 0.5 in
let r = float r in
let error = ref (-. r) in
let x = ref (r -. 0.5) in
let y = ref 0.5 in
while (x >= y) do
draw_point rend (cx +. !x) (cy +. !y) ;
draw_point rend (cx +. !y) (cy +. !x) ;
if (!x <> 0.) then
(
draw_point rend (cx -. !x) (cy +. !y) ;
draw_point rend (cx +. !y) (cy -. !x) ;
);
if (!y <> 0.) then
(
draw_point rend (cx +. !x) (cy -. !y) ;
draw_point rend (cx -. !y) (cy +. !x) ;
);
if (!x <> 0. && !y <> 0.) then
(
draw_point rend (cx -. !x) (cy -. !y);
draw_point rend (cx -. !y) (cy -. !x);
);
error := !error +. !y ;
y := !y +. 1. ;
error := !error +. !y;
if (!error >= 0.) then
(
x := !x -. 1. ;
error := !error -. !x;
error := !error -. !x;
);
done;
in
fun rend ~x ~y ~r color -> with_color rend color (f ~x ~y ~r)
let draw_circle_in_rect rend r color =
draw_circle rend ~x:(r.G.x + r.w/2) ~y:(r.y+r.h/2) ~r:(r.w/2) color
let fill_circle =
let draw_line rend x1 y1 x2 y2 (red,green,blue,alpha) =
let x2 =
let x = floor x2 in
if x2 -. x <= 0.5 then x else ceil x2
in
let y = truncate y1 in
let x1 = truncate x1 in
let x2 = truncate x2 in
for x = x1 to x2 do
if x = x1 || x = x2 then
(let> () = Sdl.set_render_draw_color rend red green blue (truncate (float alpha *. 0.7)) in ());
let> () = Sdl.render_draw_point rend x y in
if x = x1 then
let> () = Sdl.set_render_draw_color rend red green blue alpha in ()
done
in
let f ~x ~y ~r rend =
let> color = Sdl.get_render_draw_color rend in
let cx = float x -. 0.5 in
let cy = float y -. 0.5 in
let r = float r in
let dy = ref 1. in
while !dy <= r do
let dx = floor(sqrt((2.0 *. r *. !dy) -. (!dy *. !dy))) in
let y1 = cy +. !dy -. r in
let y2 = cy -. !dy +. r in
let x1 = cx -. dx in
let x2 = if y1 = y2 then cx +. dx -. 1. else cx +. dx in
draw_line rend x1 y1 x2 y1 color;
if y1 <> y2 then draw_line rend x1 y2 x2 y2 color;
dy := !dy +. 1.
done
in
fun rend ~x ~y ~r color -> with_color rend color (f ~x ~y ~r)
let fill_circle_in_rect rend r color =
fill_circle rend ~x:(r.G.x + r.w/2) ~y:(r.y+r.h/2) ~r:(r.w/2) color
let with_clip rend clip f =
let old_clip =
if Sdl.render_is_clip_enabled rend then
Some (Sdl.render_get_clip_rect rend)
else
None
in
let> () = Sdl.render_set_clip_rect rend (Some clip) in
try
let res = f rend in
let> () = Sdl.render_set_clip_rect rend old_clip in
res
with e ->
let> () = Sdl.render_set_clip_rect rend old_clip in
raise e
let with_target rend f t =
let old = Sdl.get_render_target rend in
Sdl.set_render_target rend t ;
match f rend with
| exception e -> Sdl.set_render_target rend old; raise e
| v -> Sdl.set_render_target rend old; v