Hi! hope youre doing well
been porting my c program into zig
im using marelsoft win32zig bindings and a c library for loading opengl function pointers called glad
glad needs device context handler to load function pointers and i cant pass the device context handler from zig binding struct
// HDC i have from zig bindgen
pub const HDC = *opaque {};
// HDC needed inside translate c function
pub const HDC = [*c]struct_HDC__;
pub const struct_HDC__ = extern struct {
unused: c_int = 0,
pub const Arc = __root.Arc;
pub const BitBlt = __root.BitBlt;
pub const CancelDC = __root.CancelDC;
pub const Chord = __root.Chord;
pub const ChoosePixelFormat = __root.ChoosePixelFormat;
pub const CloseMetaFile = __root.CloseMetaFile;
pub const CreateCompatibleBitmap = __root.CreateCompatibleBitmap;
pub const CreateDiscardableBitmap = __root.CreateDiscardableBitmap;
pub const CreateCompatibleDC = __root.CreateCompatibleDC;
pub const CreateDIBitmap = __root.CreateDIBitmap;
pub const DeleteDC = __root.DeleteDC;
...
}
// translate c function that needs HDC struct as arg
pub export fn gladLoaderLoadWGL(arg_hdc: HDC) c_int {
var hdc = arg_hdc;
_ = &hdc;
return gladLoadWGLUserPtr(hdc, glad_wgl_get_proc, null);
}
you have pointers of different types, so you would need to @ptrCast them
tried glad.gladLoaderLoadWGL(@ptrCast(device_context_handle)) and got these errors
run
└─ run exe namad
└─ compile exe namad Debug native 2 errors
.zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63353:210: error: type 'usize' cannot represent integer value '-1'
if ((@as(?*anyopaque, @ptrCast(@alignCast(@constCast(glad_wglGetExtensionsStringARB)))) == @as(?*anyopaque, null)) or (@as(HANDLE, @ptrCast(@alignCast(hdc))) == @as(HANDLE, @ptrFromInt(@as(usize, @intCast(@as(LONG_PTR, -@as(c_int, 1)))))))) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
glad_wgl_find_extensions_wgl: .zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63380:57
gladLoadWGLUserPtr: .zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63291:39
8 reference(s) hidden; use '-freference-trace=10' to see all references
src\nile\nile.zig:217:40: error: @ptrCast increases pointer alignment
_ = glad.gladLoaderLoadWGL(@ptrCast(device_context_handle));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\nile\nile.zig:217:49: note: '?*win32.graphics.gdi.HDC__opaque_3290' has alignment '1'
_ = glad.gladLoaderLoadWGL(@ptrCast(device_context_handle));
^~~~~~~~~~~~~~~~~~~~~
src\nile\nile.zig:217:40: note: '[*c]glad-win32-wgl.struct_HDC__' has alignment '4'
src\nile\nile.zig:217:40: note: use @alignCast to assert pointer alignment
error: 2 compilation errors
tried these two changes bellow but still get some strange errors
gladLoaderLoadWGL(@as(glad.HDC, @alignCast(device_context_handle.?)));
gladLoaderLoadWGL(@ptrCast(@alignCast(device_context_handle.?)));
run
└─ run exe namad
└─ compile exe namad Debug native 1 errors
.zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63353:210: error: type 'usize' cannot represent integer value '-1'
if ((@as(?*anyopaque, @ptrCast(@alignCast(@constCast(glad_wglGetExtensionsStringARB)))) == @as(?*anyopaque, null)) or (@as(HANDLE, @ptrCast(@alignCast(hdc))) == @as(HANDLE, @ptrFromInt(@as(usize, @intCast(@as(LONG_PTR, -@as(c_int, 1)))))))) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
referenced by:
glad_wgl_find_extensions_wgl: .zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63380:57
gladLoadWGLUserPtr: .zig-cache\o\f66a2503ece2aa7ef812a93594ca27df\glad-win32-wgl.zig:63291:39
7 reference(s) hidden; use '-freference-trace=9' to see all references
error: 1 compilation errors
to turn a minus 1 into a usize would require a @bitCast, assuming you mean to fill that memory with all 1s. for basics of type conversion and Zig’s type system like this you’d really be better served by reading the official documentation.
2 Likes
That would be all well and good if he was the one that made this mistake, but the attempt to turn -1 into a usize is actually happening inside a C-translated function.
The original C code was using -1 as a null value for a pointer (for God knows what reason), and the translate-c backend translated that code fairly literally and it resulted in a compile error.
Since this is just GLAD, my recommendation for OP is to seek out the glad_wgl_has_extension function in src/wgl.c, and edit it as follows:
if(wglGetExtensionsStringARB == NULL || hdc == INVALID_HANDLE_VALUE)
→
if(wglGetExtensionsStringARB == NULL || hdc == NULL)
Or maybe
if(wglGetExtensionsStringARB == NULL || hdc == (uint)INVALID_HANDLE_VALUE)
would work?
3 Likes
I see! Well, my advice stands: if you don’t know how to write Zig, it’s definitely tougher to fix autotranslation errors.
1 Like
yup that works perfectly
i was using a python script to remove the minus from ptr value in every time the zig-cache changed
your solution seems so much better