Skip to content

Commit b1332d0

Browse files
Jack-Jickrowland
authored andcommitted
Add ellipse/concave-poly methods to DrawList (zig-gamedev#37)
1 parent 33380df commit b1332d0

File tree

2 files changed

+159
-3
lines changed

2 files changed

+159
-3
lines changed

src/gui.zig

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,11 @@ fn zguiMemFree(maybe_ptr: ?*anyopaque, _: ?*anyopaque) callconv(.C) void {
126126
defer mem_mutex.unlock();
127127

128128
if (mem_allocations != null) {
129-
const size = mem_allocations.?.fetchRemove(@intFromPtr(ptr)).?.value;
130-
const mem = @as([*]align(mem_alignment) u8, @ptrCast(@alignCast(ptr)))[0..size];
131-
mem_allocator.?.free(mem);
129+
if (mem_allocations.?.fetchRemove(@intFromPtr(ptr))) |kv| {
130+
const size = kv.value;
131+
const mem = @as([*]align(mem_alignment) u8, @ptrCast(@alignCast(ptr)))[0..size];
132+
mem_allocator.?.free(mem);
133+
}
132134
}
133135
}
134136
}
@@ -4225,6 +4227,59 @@ pub const DrawList = *opaque {
42254227
num_segments: c_int,
42264228
) void;
42274229
//----------------------------------------------------------------------------------------------
4230+
pub fn addEllipse(draw_list: DrawList, args: struct {
4231+
p: [2]f32,
4232+
r: [2]f32,
4233+
col: u32,
4234+
rot: f32 = 0,
4235+
num_segments: i32 = 0,
4236+
thickness: f32 = 1.0,
4237+
}) void {
4238+
zguiDrawList_AddEllipse(
4239+
draw_list,
4240+
&args.p,
4241+
&args.r,
4242+
args.col,
4243+
args.rot,
4244+
args.num_segments,
4245+
args.thickness,
4246+
);
4247+
}
4248+
extern fn zguiDrawList_AddEllipse(
4249+
draw_list: DrawList,
4250+
center: *const [2]f32,
4251+
radius: *const [2]f32,
4252+
col: u32,
4253+
rot: f32,
4254+
num_segments: c_int,
4255+
thickness: f32,
4256+
) void;
4257+
//----------------------------------------------------------------------------------------------
4258+
pub fn addEllipseFilled(draw_list: DrawList, args: struct {
4259+
p: [2]f32,
4260+
r: [2]f32,
4261+
col: u32,
4262+
rot: f32 = 0,
4263+
num_segments: u16 = 0,
4264+
}) void {
4265+
zguiDrawList_AddEllipseFilled(
4266+
draw_list,
4267+
&args.p,
4268+
&args.r,
4269+
args.col,
4270+
args.rot,
4271+
args.num_segments,
4272+
);
4273+
}
4274+
extern fn zguiDrawList_AddEllipseFilled(
4275+
draw_list: DrawList,
4276+
center: *const [2]f32,
4277+
radius: *const [2]f32,
4278+
col: u32,
4279+
rot: f32,
4280+
num_segments: c_int,
4281+
) void;
4282+
//----------------------------------------------------------------------------------------------
42284283
pub fn addNgon(draw_list: DrawList, args: struct {
42294284
p: [2]f32,
42304285
r: f32,
@@ -4323,6 +4378,25 @@ pub const DrawList = *opaque {
43234378
col: u32,
43244379
) void;
43254380
//----------------------------------------------------------------------------------------------
4381+
pub fn addConcavePolyFilled(
4382+
draw_list: DrawList,
4383+
points: []const [2]f32,
4384+
col: u32,
4385+
) void {
4386+
zguiDrawList_AddConcavePolyFilled(
4387+
draw_list,
4388+
points.ptr,
4389+
@intCast(points.len),
4390+
col,
4391+
);
4392+
}
4393+
extern fn zguiDrawList_AddConcavePolyFilled(
4394+
draw_list: DrawList,
4395+
points: [*]const [2]f32,
4396+
num_points: c_int,
4397+
col: u32,
4398+
) void;
4399+
//----------------------------------------------------------------------------------------------
43264400
pub fn addBezierCubic(draw_list: DrawList, args: struct {
43274401
p1: [2]f32,
43284402
p2: [2]f32,
@@ -4499,6 +4573,11 @@ pub const DrawList = *opaque {
44994573
}
45004574
extern fn zguiDrawList_PathFillConvex(draw_list: DrawList, col: c_uint) void;
45014575
//----------------------------------------------------------------------------------------------
4576+
pub fn pathFillConcave(draw_list: DrawList, col: u32) void {
4577+
return zguiDrawList_PathFillConcave(draw_list, col);
4578+
}
4579+
extern fn zguiDrawList_PathFillConcave(draw_list: DrawList, col: c_uint) void;
4580+
//----------------------------------------------------------------------------------------------
45024581
pub fn pathStroke(draw_list: DrawList, args: struct {
45034582
col: u32,
45044583
flags: DrawFlags = .{},
@@ -4549,6 +4628,34 @@ pub const DrawList = *opaque {
45494628
a_max_of_12: c_int,
45504629
) void;
45514630
//----------------------------------------------------------------------------------------------
4631+
pub fn pathEllipticalArcTo(draw_list: DrawList, args: struct {
4632+
p: [2]f32,
4633+
r: [2]f32,
4634+
rot: f32,
4635+
amin: f32,
4636+
amax: f32,
4637+
num_segments: u16 = 0,
4638+
}) void {
4639+
zguiDrawList_PathEllipticalArcTo(
4640+
draw_list,
4641+
&args.p,
4642+
&args.r,
4643+
args.rot,
4644+
args.amin,
4645+
args.amax,
4646+
args.num_segments,
4647+
);
4648+
}
4649+
extern fn zguiDrawList_PathEllipticalArcTo(
4650+
draw_list: DrawList,
4651+
center: *const [2]f32,
4652+
radius: *const [2]f32,
4653+
rot: f32,
4654+
amin: f32,
4655+
amax: f32,
4656+
num_segments: c_int,
4657+
) void;
4658+
//----------------------------------------------------------------------------------------------
45524659
pub fn pathBezierCubicCurveTo(draw_list: DrawList, args: struct {
45534660
p2: [2]f32,
45544661
p3: [2]f32,

src/zgui.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,6 +2240,29 @@ extern "C"
22402240
draw_list->AddCircleFilled({center[0], center[1]}, radius, col, num_segments);
22412241
}
22422242

2243+
ZGUI_API void zguiDrawList_AddEllipse(
2244+
ImDrawList *draw_list,
2245+
const float center[2],
2246+
const float radius[2],
2247+
ImU32 col,
2248+
float rot,
2249+
int num_segments,
2250+
float thickness)
2251+
{
2252+
draw_list->AddEllipse({center[0], center[1]}, {radius[0], radius[1]}, col, rot, num_segments, thickness);
2253+
}
2254+
2255+
ZGUI_API void zguiDrawList_AddEllipseFilled(
2256+
ImDrawList *draw_list,
2257+
const float center[2],
2258+
const float radius[2],
2259+
ImU32 col,
2260+
float rot,
2261+
int num_segments)
2262+
{
2263+
draw_list->AddEllipseFilled({center[0], center[1]}, {radius[0], radius[1]}, col, rot, num_segments);
2264+
}
2265+
22432266
ZGUI_API void zguiDrawList_AddNgon(
22442267
ImDrawList *draw_list,
22452268
const float center[2],
@@ -2291,6 +2314,15 @@ extern "C"
22912314
draw_list->AddConvexPolyFilled((const ImVec2 *)&points[0][0], num_points, col);
22922315
}
22932316

2317+
ZGUI_API void zguiDrawList_AddConcavePolyFilled(
2318+
ImDrawList *draw_list,
2319+
const float points[][2],
2320+
int num_points,
2321+
ImU32 col)
2322+
{
2323+
draw_list->AddConcavePolyFilled((const ImVec2 *)&points[0][0], num_points, col);
2324+
}
2325+
22942326
ZGUI_API void zguiDrawList_AddBezierCubic(
22952327
ImDrawList *draw_list,
22962328
const float p1[2],
@@ -2404,6 +2436,11 @@ extern "C"
24042436
draw_list->PathFillConvex(col);
24052437
}
24062438

2439+
ZGUI_API void zguiDrawList_PathFillConcave(ImDrawList *draw_list, ImU32 col)
2440+
{
2441+
draw_list->PathFillConcave(col);
2442+
}
2443+
24072444
ZGUI_API void zguiDrawList_PathStroke(ImDrawList *draw_list, ImU32 col, ImDrawFlags flags, float thickness)
24082445
{
24092446
draw_list->PathStroke(col, flags, thickness);
@@ -2430,6 +2467,18 @@ extern "C"
24302467
draw_list->PathArcToFast({center[0], center[1]}, radius, a_min_of_12, a_max_of_12);
24312468
}
24322469

2470+
ZGUI_API void zguiDrawList_PathEllipticalArcTo(
2471+
ImDrawList *draw_list,
2472+
const float center[2],
2473+
const float radius[2],
2474+
float rot,
2475+
int a_min,
2476+
int a_max,
2477+
int num_segments)
2478+
{
2479+
draw_list->PathEllipticalArcTo({center[0], center[1]}, {radius[0], radius[1]}, rot, a_min, a_max, num_segments);
2480+
}
2481+
24332482
ZGUI_API void zguiDrawList_PathBezierCubicCurveTo(
24342483
ImDrawList *draw_list,
24352484
const float p2[2],

0 commit comments

Comments
 (0)