marlersoft / zigwin32

Zig bindings for Win32 generated by https://github.com/marlersoft/zigwin32gen
MIT License
234 stars 30 forks source link

Do you know why SetDisplayMode returns an error? #35

Closed jiangbo closed 1 week ago

jiangbo commented 1 week ago

project: https://github.com/jiangbo/demo/tree/win32-panic, The whole code is only in main.zig. zig version: 0.13.0-dev.351+64ef45eb0

// clone the project and build
zig build run
fn gameInit() void {
    std.log.info("gameInit", .{});

    if (failed(draw.DirectDrawCreateEx(null, @ptrCast(&draw7), //
        draw.IID_IDirectDraw7, null))) win32Panic();

    // const style = draw.DDSCL_FULLSCREEN | draw.DDSCL_ALLOWMODEX |
    //     draw.DDSCL_EXCLUSIVE | draw.DDSCL_ALLOWREBOOT;

    const style = draw.DDSCL_NORMAL;

    if (failed(draw7.IDirectDraw7_SetCooperativeLevel( //
        hander, style))) win32Panic();

    if (failed(draw7.IDirectDraw7_SetDisplayMode( //
        WIDTH, HEIGHT, 8, 0, 0))) win32Panic();
}

the IDirectDraw7_SetDisplayMode panic, and return 5.

error info:

PS C:\workspace\demo> zig build run
info: wWinMain
info: WM_CREATE
info: gameInit
error: win32 painc code 5
thread 21380 panic: ERROR_ACCESS_DENIED
C:\workspace\demo\src\main.zig:180:12: 0x871c87 in win32Panic (demo.exe.obj)
    @panic(@tagName(err));
           ^
C:\workspace\demo\src\main.zig:109:45: 0x871f3c in gameInit (demo.exe.obj)
        WIDTH, HEIGHT, 8, 0, 0))) win32Panic();
                                            ^
C:\workspace\demo\src\main.zig:69:13: 0x8713d0 in wWinMain (demo.exe.obj)
    gameInit();
            ^
C:\software\zig\lib\std\start.zig:577:25: 0x8710f2 in call_wWinMain (demo.exe.obj)
    return root.wWinMain(hInstance, null, lpCmdLine, nCmdShow);
                        ^
C:\software\zig\lib\std\start.zig:374:53: 0x871013 in wWinMainCRTStartup (demo.exe.obj)
    const result: std.os.windows.INT = call_wWinMain();
                                                    ^
???:?:?: 0x7ffe338e257c in ??? (KERNEL32.DLL)
???:?:?: 0x7ffe350caf27 in ??? (ntdll.dll)
run
└─ run demo failure
error: the following command exited with error code 3:
C:\workspace\demo\zig-out\bin\demo.exe
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run demo failure
error: the following build command failed with exit code 1:
C:\workspace\demo\.zig-cache\o\37c7dd6492f569f3c1dae9a742a84c30\build.exe C:\software\zig\zig.exe C:\workspace\demo C:\workspace\demo\.zig-cache C:\Users\JiangBo\AppData\Local\zig --seed 0x2681f274 -Z07f7ba4b48d8300a run

update: the cpp code return S_OK, https://ziggit.dev/t/win32-setdisplaymode-api-return-a-error/4932/1

aqrit commented 1 week ago

SetDisplayMode requires DDSCL_EXCLUSIVE to change the bit-depth of the display.

AFAIK, you should only call SetDisplayMode from DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN mode, it is not normally used in window mode (aka. DDSCL_NORMAL). There may be other problems in your code, I haven't checked.

jiangbo commented 1 week ago

Should use 32-bit mode instead of 64-bit mode.