Created
September 30, 2021 20:05
-
-
Save Airbus5717/056ccfcac37a28e16ed017dce94ad069 to your computer and use it in GitHub Desktop.
Sample XPlane Hello World plugin in Zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const xplm = @import("xplm"); | |
var g_window: xplm.XPLMWindowID = std.mem.zeroes(xplm.XPLMWindowID); | |
pub export fn draw_hello_world(in_window_id: xplm.XPLMWindowID, in_refcon: ?*c_void) callconv(.C) void { | |
_ = in_refcon; | |
_ = in_window_id; | |
} | |
pub export fn dummy_mouse_handler(in_window_id: xplm.XPLMWindowID, x: c_int, y: c_int, is_down: c_int, in_refcon: ?*c_void) callconv(.C) c_int { | |
_ = in_window_id; | |
_ = x; | |
_ = y; | |
_ = is_down; | |
_ = in_refcon; | |
return 0; | |
} | |
pub export fn dummy_cursor_status_handler(in_window_id: xplm.XPLMWindowID, x: c_int, y: c_int, in_refcon: ?*c_void) callconv(.C) xplm.XPLMCursorStatus { | |
_ = in_window_id; | |
_ = x; | |
_ = y; | |
_ = in_refcon; | |
return xplm.xplm_CursorDefault; | |
} | |
pub export fn dummy_wheel_handler(in_window_id: xplm.XPLMWindowID, x: c_int, y: c_int, wheel: c_int, clicks: c_int, in_refcon: ?*c_void) callconv(.C) c_int { | |
_ = in_window_id; | |
_ = x; | |
_ = y; | |
_ = wheel; | |
_ = clicks; | |
_ = in_refcon; | |
return 0; | |
} | |
pub export fn dummy_key_handler(in_window_id: xplm.XPLMWindowID, key: u8, flags: xplm.XPLMKeyFlags, virtual_key: u8, in_refcon: ?*c_void, losing_focus: c_int) callconv(.C) void { | |
_ = in_window_id; | |
_ = key; | |
_ = flags; | |
_ = virtual_key; | |
_ = in_refcon; | |
_ = losing_focus; | |
} | |
pub export fn XPluginStart(outName: [*c]u8, outSig: [*c]u8, outDesc: [*c]u8) callconv(.C) c_int { | |
_ = xplm.strcpy(outName, "HelloWorld3Plugin"); | |
_ = xplm.strcpy(outSig, "xpsdk.examples.helloworld3plugin"); | |
_ = xplm.strcpy(outDesc, "A Hello World plug-in for the XPLM300 SDK."); | |
var params: xplm.XPLMCreateWindow_t = undefined; | |
params.structSize = @bitCast(c_int, @truncate(c_uint, @sizeOf(xplm.XPLMCreateWindow_t))); | |
params.visible = 1; | |
params.drawWindowFunc = draw_hello_world; | |
params.handleMouseClickFunc = dummy_mouse_handler; | |
params.handleRightClickFunc = dummy_mouse_handler; | |
params.handleMouseWheelFunc = dummy_wheel_handler; | |
params.handleKeyFunc = dummy_key_handler; | |
params.handleCursorFunc = dummy_cursor_status_handler; | |
params.refcon = @intToPtr(?*c_void, @as(c_int, 0)); | |
params.layer = xplm.xplm_WindowLayerFloatingWindows; | |
params.decorateAsFloatingWindow = xplm.xplm_WindowDecorationRoundRectangle; | |
var left: c_int = undefined; | |
var bottom: c_int = undefined; | |
var right: c_int = undefined; | |
var top: c_int = undefined; | |
xplm.XPLMGetScreenBoundsGlobal(&left, &top, &right, &bottom); | |
params.left = left + @as(c_int, 50); | |
params.bottom = bottom + @as(c_int, 150); | |
params.right = params.left + @as(c_int, 200); | |
params.top = params.bottom + @as(c_int, 200); | |
g_window = xplm.XPLMCreateWindowEx(¶ms); | |
xplm.XPLMSetWindowPositioningMode(g_window, xplm.xplm_WindowPositionFree, -@as(c_int, 1)); | |
xplm.XPLMSetWindowResizingLimits(g_window, @as(c_int, 200), @as(c_int, 200), @as(c_int, 300), @as(c_int, 300)); | |
xplm.XPLMSetWindowTitle(g_window, "Sample Window"); | |
return @boolToInt(g_window != @intToPtr(?*c_void, @as(c_int, 0))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment