Skip to content

Commit

Permalink
chore: update zigwin32
Browse files Browse the repository at this point in the history
  • Loading branch information
alanoliveira committed Jul 25, 2024
1 parent f723937 commit 6739627
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
// .path = "foo",
//},
.zigwin32 = .{
.url = "https://github.com/marlersoft/zigwin32/archive/6777f1db221d0cb50322842f558f03e3c3a4099f.tar.gz",
.hash = "1220f8d5028adceac0c6fc8ef554d82efef49ca48913393e9c2f5d73a66b58c8aaad",
.url = "https://github.com/marlersoft/zigwin32/archive/407a4c7b869ee3d10db520fdfae8b9faf9b2adb5.tar.gz",
.hash = "1220cc9c9028e20f4ec2ece1155ae3479acc1cc509f9ab93acb74e8f5bbf8eefd2a9",
},
},

Expand Down
24 changes: 12 additions & 12 deletions src/font8x8.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sprite: ?*win.ID3DXSprite,

pub fn initialize(device: *win.IDirect3DDevice9) !Self {
var texture: ?*win.IDirect3DTexture9 = null;
if (device.IDirect3DDevice9_CreateTexture(
if (device.CreateTexture(
8,
8 * (128 + SYMBOLS_BMP.len),
1,
Expand All @@ -24,7 +24,7 @@ pub fn initialize(device: *win.IDirect3DDevice9) !Self {
&texture,
null,
) != win.S_OK) return error.CreateTextureError;
errdefer _ = texture.?.IUnknown_Release();
errdefer _ = texture.?.IUnknown.Release();

var surface: ?*win.IDirect3DSurface9 = null;
if (texture.?.vtable.GetSurfaceLevel(texture.?, 0, &surface) != win.S_OK) {
Expand Down Expand Up @@ -88,18 +88,18 @@ pub fn drawText(self: *const Self, text: []const u8, x: f32, y: f32, color: u32)
var magfilter: u32 = 0;
var minfilter: u32 = 0;
var mipfilter: u32 = 0;
_ = device.IDirect3DDevice9_GetSamplerState(0, .MAGFILTER, @ptrCast(&magfilter));
_ = device.IDirect3DDevice9_GetSamplerState(0, .MINFILTER, @ptrCast(&minfilter));
_ = device.IDirect3DDevice9_GetSamplerState(0, .MIPFILTER, @ptrCast(&mipfilter));
_ = device.GetSamplerState(0, .MAGFILTER, @ptrCast(&magfilter));
_ = device.GetSamplerState(0, .MINFILTER, @ptrCast(&minfilter));
_ = device.GetSamplerState(0, .MIPFILTER, @ptrCast(&mipfilter));

if (sprite.lpVtbl.*.Begin.?(sprite, win.D3DXSPRITE_DONOTSAVESTATE) != win.S_OK) {
std.debug.print("Error on sprite.Begin\n", .{});
}

// do not smooth the pixels
_ = device.IDirect3DDevice9_SetSamplerState(0, .MAGFILTER, @intFromEnum(win.D3DTEXF_NONE));
_ = device.IDirect3DDevice9_SetSamplerState(0, .MINFILTER, @intFromEnum(win.D3DTEXF_NONE));
_ = device.IDirect3DDevice9_SetSamplerState(0, .MIPFILTER, @intFromEnum(win.D3DTEXF_NONE));
_ = device.SetSamplerState(0, .MAGFILTER, @intFromEnum(win.D3DTEXF_NONE));
_ = device.SetSamplerState(0, .MINFILTER, @intFromEnum(win.D3DTEXF_NONE));
_ = device.SetSamplerState(0, .MIPFILTER, @intFromEnum(win.D3DTEXF_NONE));

var pos_x = x;
var pos_y = y;
Expand Down Expand Up @@ -130,14 +130,14 @@ pub fn drawText(self: *const Self, text: []const u8, x: f32, y: f32, color: u32)
std.debug.print("Error on sprite.End\n", .{});
}

_ = device.IDirect3DDevice9_SetSamplerState(0, .MAGFILTER, magfilter);
_ = device.IDirect3DDevice9_SetSamplerState(0, .MINFILTER, minfilter);
_ = device.IDirect3DDevice9_SetSamplerState(0, .MIPFILTER, mipfilter);
_ = device.SetSamplerState(0, .MAGFILTER, magfilter);
_ = device.SetSamplerState(0, .MINFILTER, minfilter);
_ = device.SetSamplerState(0, .MIPFILTER, mipfilter);
}

pub fn deinitialize(self: *Self) void {
if (self.sprite) |s| _ = s.lpVtbl.*.Release.?(s);
if (self.texture) |t| _ = t.IUnknown_Release();
if (self.texture) |t| _ = t.IUnknown.Release();
}

const SYMBOLS_BMP = [_][8]u8{
Expand Down
8 changes: 4 additions & 4 deletions src/injector/injector.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub fn inject(dll_path: []const u8, proc_name: []const u8) InjectorError!void {
const pid = getPidByProcName(proc_name) orelse return InjectorError.ProcNotFound;

const proc_handle = win.OpenProcess(
.ALL_ACCESS,
win.PROCESS_ALL_ACCESS,
win.FALSE,
pid,
) orelse return InjectorError.OpenProcError;
Expand All @@ -42,7 +42,7 @@ pub fn inject(dll_path: []const u8, proc_name: []const u8) InjectorError!void {

fn getPidByProcName(proc_name: []const u8) ?u32 {
const snapshot = win.CreateToolhelp32Snapshot(
.SNAPPROCESS,
win.TH32CS_SNAPPROCESS,
0,
) orelse return null;
defer _ = win.CloseHandle(snapshot);
Expand All @@ -63,8 +63,8 @@ fn load_dll(proc_handle: win.HANDLE, dll_path: []const u8) bool {
proc_handle,
null,
dll_path.len,
win.VIRTUAL_ALLOCATION_TYPE.initFlags(.{ .RESERVE = 1, .COMMIT = 1 }),
.PAGE_READWRITE,
win.VIRTUAL_ALLOCATION_TYPE{ .RESERVE = 1, .COMMIT = 1 },
win.PAGE_READWRITE,
) orelse return false;
defer _ = win.VirtualFreeEx(proc_handle, buff, 0, .RELEASE);

Expand Down
76 changes: 38 additions & 38 deletions src/renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,33 @@ const D3D9Settings = struct {

fn extract(device: *const win.IDirect3DDevice9) !D3D9Settings {
var cur: D3D9Settings = undefined;
if (device.IDirect3DDevice9_GetRenderState(.ALPHABLENDENABLE, &cur.alphaBlendEnable) != win.S_OK or
device.IDirect3DDevice9_GetRenderState(.DESTBLEND, &cur.destBlend) != win.S_OK or
device.IDirect3DDevice9_GetRenderState(.SRCBLEND, &cur.srcBlend) != win.S_OK or
device.IDirect3DDevice9_GetRenderState(.DESTBLENDALPHA, &cur.destBlendAlpha) != win.S_OK or
device.IDirect3DDevice9_GetRenderState(.SRCBLENDALPHA, &cur.srcBlendAlpha) != win.S_OK or
device.IDirect3DDevice9_GetSamplerState(0, .MAGFILTER, &cur.magfilter) != win.S_OK or
device.IDirect3DDevice9_GetSamplerState(0, .MINFILTER, &cur.minfilter) != win.S_OK or
device.IDirect3DDevice9_GetSamplerState(0, .MIPFILTER, &cur.mipfilter) != win.S_OK or
device.IDirect3DDevice9_GetFVF(&cur.fvf) != win.S_OK or
device.IDirect3DDevice9_GetPixelShader(&cur.pixelShader) != win.S_OK or
device.IDirect3DDevice9_GetTexture(0, &cur.texture) != win.S_OK) return error.ExtractSettingsError;
if (device.GetRenderState(.ALPHABLENDENABLE, &cur.alphaBlendEnable) != win.S_OK or
device.GetRenderState(.DESTBLEND, &cur.destBlend) != win.S_OK or
device.GetRenderState(.SRCBLEND, &cur.srcBlend) != win.S_OK or
device.GetRenderState(.DESTBLENDALPHA, &cur.destBlendAlpha) != win.S_OK or
device.GetRenderState(.SRCBLENDALPHA, &cur.srcBlendAlpha) != win.S_OK or
device.GetSamplerState(0, .MAGFILTER, &cur.magfilter) != win.S_OK or
device.GetSamplerState(0, .MINFILTER, &cur.minfilter) != win.S_OK or
device.GetSamplerState(0, .MIPFILTER, &cur.mipfilter) != win.S_OK or
device.GetFVF(&cur.fvf) != win.S_OK or
device.GetPixelShader(&cur.pixelShader) != win.S_OK or
device.GetTexture(0, &cur.texture) != win.S_OK) return error.ExtractSettingsError;

return cur;
}

fn apply(self: *const D3D9Settings, device: *const win.IDirect3DDevice9) !void {
if (device.IDirect3DDevice9_SetRenderState(.ALPHABLENDENABLE, self.alphaBlendEnable) != win.S_OK or
device.IDirect3DDevice9_SetRenderState(.DESTBLEND, self.destBlend) != win.S_OK or
device.IDirect3DDevice9_SetRenderState(.SRCBLEND, self.srcBlend) != win.S_OK or
device.IDirect3DDevice9_SetRenderState(.DESTBLENDALPHA, self.destBlendAlpha) != win.S_OK or
device.IDirect3DDevice9_SetRenderState(.SRCBLENDALPHA, self.srcBlendAlpha) != win.S_OK or
device.IDirect3DDevice9_SetSamplerState(0, .MAGFILTER, self.magfilter) != win.S_OK or
device.IDirect3DDevice9_SetSamplerState(0, .MINFILTER, self.minfilter) != win.S_OK or
device.IDirect3DDevice9_SetSamplerState(0, .MIPFILTER, self.mipfilter) != win.S_OK or
device.IDirect3DDevice9_SetPixelShader(self.pixelShader) != win.S_OK or
device.IDirect3DDevice9_SetFVF(self.fvf) != win.S_OK or
device.IDirect3DDevice9_SetTexture(0, self.texture) != win.S_OK) return error.ApplySettingsError;
if (device.SetRenderState(.ALPHABLENDENABLE, self.alphaBlendEnable) != win.S_OK or
device.SetRenderState(.DESTBLEND, self.destBlend) != win.S_OK or
device.SetRenderState(.SRCBLEND, self.srcBlend) != win.S_OK or
device.SetRenderState(.DESTBLENDALPHA, self.destBlendAlpha) != win.S_OK or
device.SetRenderState(.SRCBLENDALPHA, self.srcBlendAlpha) != win.S_OK or
device.SetSamplerState(0, .MAGFILTER, self.magfilter) != win.S_OK or
device.SetSamplerState(0, .MINFILTER, self.minfilter) != win.S_OK or
device.SetSamplerState(0, .MIPFILTER, self.mipfilter) != win.S_OK or
device.SetPixelShader(self.pixelShader) != win.S_OK or
device.SetFVF(self.fvf) != win.S_OK or
device.SetTexture(0, self.texture) != win.S_OK) return error.ApplySettingsError;
}

const CUSTOM_SETTINGS: D3D9Settings = D3D9Settings{
Expand Down Expand Up @@ -78,8 +78,8 @@ pub fn initialize(self: *Self, device: *win.IDirect3DDevice9) !void {
self.device = device;

std.debug.print("Creating render target texture\n", .{});
if (self.texture) |texture| _ = texture.IUnknown_Release();
if (!win.SUCCEEDED(device.IDirect3DDevice9_CreateTexture(
if (self.texture) |texture| _ = texture.IUnknown.Release();
if (!win.SUCCEEDED(device.CreateTexture(
@intFromFloat(SCREEN_WIDTH * SCALE),
@intFromFloat(SCREEN_HEIGHT * SCALE),
1,
Expand All @@ -102,48 +102,48 @@ pub fn initialize(self: *Self, device: *win.IDirect3DDevice9) !void {
self.original_settings = try D3D9Settings.extract(self.device);
try D3D9Settings.CUSTOM_SETTINGS.apply(self.device);

if (!win.SUCCEEDED(device.IDirect3DDevice9_GetRenderTarget(0, &self.original_render_target))) {
if (!win.SUCCEEDED(device.GetRenderTarget(0, &self.original_render_target))) {
std.debug.print("Error on get original render target\n", .{});
return error.RenderTargetGetError;
}

var render_target: ?*win.IDirect3DSurface9 = null;
if (!win.SUCCEEDED(self.texture.?.IDirect3DTexture9_GetSurfaceLevel(0, &render_target))) {
if (!win.SUCCEEDED(self.texture.?.GetSurfaceLevel(0, &render_target))) {
std.debug.print("Error on get render target surface\n", .{});
return error.RenderTargetGetError;
}

if (!win.SUCCEEDED(self.device.IDirect3DDevice9_SetRenderTarget(0, render_target))) {
if (!win.SUCCEEDED(self.device.SetRenderTarget(0, render_target))) {
std.debug.print("Error on set render target\n", .{});
return error.RenderTargetSetError;
}
errdefer _ = self.device.IDirect3DDevice9_SetRenderTarget(0, self.original_render_target);
errdefer _ = self.device.SetRenderTarget(0, self.original_render_target);

if (!win.SUCCEEDED(self.device.IDirect3DDevice9_SetTexture(0, null))) {
if (!win.SUCCEEDED(self.device.SetTexture(0, null))) {
std.debug.print("Error on set texture\n", .{});
return error.TextureSetError;
}
_ = self.device.IDirect3DDevice9_Clear(0, null, win.D3DCLEAR_TARGET, 0, 1.0, 0);
_ = self.device.Clear(0, null, win.D3DCLEAR_TARGET, 0, 1.0, 0);
}

pub fn deinitialize(self: *Self) !void {
if (!win.SUCCEEDED(self.device.IDirect3DDevice9_SetRenderTarget(0, self.original_render_target))) {
if (!win.SUCCEEDED(self.device.SetRenderTarget(0, self.original_render_target))) {
std.debug.print("Error on set original render target\n", .{});
return error.RenderTargetSetError;
}

var viewPort: win.D3DVIEWPORT9 = undefined;
if (!win.SUCCEEDED(self.device.IDirect3DDevice9_GetViewport(&viewPort))) {
if (!win.SUCCEEDED(self.device.GetViewport(&viewPort))) {
std.debug.print("Error on get viewport\n", .{});
return error.ViewPortGetError;
}

if (!win.SUCCEEDED(self.device.IDirect3DDevice9_SetTexture(0, @ptrCast(self.texture)))) {
if (!win.SUCCEEDED(self.device.SetTexture(0, @ptrCast(self.texture)))) {
std.debug.print("Error on set texture\n", .{});
return error.TextureSetError;
}

if (!win.SUCCEEDED(self.device.IDirect3DDevice9_SetFVF(win.D3DFVF_XYZRHW | win.D3DFVF_TEX1))) {
if (!win.SUCCEEDED(self.device.SetFVF(win.D3DFVF_XYZRHW | win.D3DFVF_TEX1))) {
std.debug.print("Error on set FVF\n", .{});
return error.FVFSetError;
}
Expand All @@ -164,7 +164,7 @@ pub fn deinitialize(self: *Self) !void {
.{ .x = width, .y = height, .z = 0.0, .rhw = 1.0, .u = 1.0, .v = 1.0 },
};

if (self.device.IDirect3DDevice9_DrawPrimitiveUP(
if (self.device.DrawPrimitiveUP(
win.D3DPRIMITIVETYPE.TRIANGLESTRIP,
2,
&vertices,
Expand All @@ -189,7 +189,7 @@ pub fn drawLine(self: *const Self, x1: i32, y1: i32, x2: i32, y2: i32, color: u3
Vertex{ .x = screen_x2, .y = screen_y2, .z = z, .rhw = rhw, .color = color },
};

if (self.device.IDirect3DDevice9_DrawPrimitiveUP(
if (self.device.DrawPrimitiveUP(
win.D3DPRIMITIVETYPE.LINELIST,
1,
&vertices,
Expand All @@ -214,7 +214,7 @@ pub fn drawRectFill(self: *const Self, x1: i32, y1: i32, x2: i32, y2: i32, color
Vertex{ .x = screen_x2, .y = screen_y2, .z = z, .rhw = rhw, .color = color },
};

if (self.device.IDirect3DDevice9_DrawPrimitiveUP(
if (self.device.DrawPrimitiveUP(
win.D3DPRIMITIVETYPE.TRIANGLESTRIP,
2,
&fill_vertices,
Expand All @@ -240,7 +240,7 @@ pub fn drawRectOutline(self: *const Self, x1: i32, y1: i32, x2: i32, y2: i32, co
Vertex{ .x = screen_x1, .y = screen_y1, .z = z, .rhw = rhw, .color = color },
};

if (self.device.IDirect3DDevice9_DrawPrimitiveUP(
if (self.device.DrawPrimitiveUP(
win.D3DPRIMITIVETYPE.LINESTRIP,
4,
&outline_vertices,
Expand Down
8 changes: 4 additions & 4 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub fn shutdown() void {
0,
@ptrCast(&win.FreeLibrary),
@import("root").SELF_HANDLE,
.THREAD_CREATE_RUN_IMMEDIATELY,
win.THREAD_CREATE_RUN_IMMEDIATELY,
null,
);
if (handle == null) {
Expand All @@ -133,22 +133,22 @@ pub fn shutdown() void {

fn getD3d9EndScenePtr() ?std.meta.FieldType(win.IDirect3DDevice9.VTable, .EndScene) {
const d3d9 = win.Direct3DCreate9(win.D3D_SDK_VERSION) orelse return null;
defer _ = d3d9.IUnknown_Release();
defer _ = d3d9.IUnknown.Release();

var present_params: win.D3DPRESENT_PARAMETERS = std.mem.zeroes(win.D3DPRESENT_PARAMETERS);
present_params.Windowed = win.TRUE;
present_params.SwapEffect = win.D3DSWAPEFFECT_DISCARD;

var device: ?*win.IDirect3DDevice9 = null;
if (d3d9.IDirect3D9_CreateDevice(
if (d3d9.CreateDevice(
win.D3DADAPTER_DEFAULT,
win.D3DDEVTYPE_HAL,
win.GetDesktopWindow(),
win.D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&present_params,
&device,
) != win.S_OK) return null;
defer _ = device.?.IUnknown_Release();
defer _ = device.?.IUnknown.Release();

return @ptrCast(device.?.vtable.EndScene);
}

0 comments on commit 6739627

Please sign in to comment.