Hello World (SDK 3)

#include "XPLMDisplay.h"
#include "XPLMGraphics.h"
#include <string.h>
#if IBM
	#include <windows.h>
#endif
#if LIN
	#include <GL/gl.h>
#elif __GNUC__
	#include <OpenGL/gl.h>
#else
	#include <GL/gl.h>
#endif

#ifndef XPLM300
	#error This is made to be compiled against the XPLM300 SDK
#endif

// An opaque handle to the window we will create
static XPLMWindowID	g_window;

// Callbacks we will register when we create our window
void				draw_hello_world(XPLMWindowID in_window_id, void * in_refcon);
int					dummy_mouse_handler(XPLMWindowID in_window_id, int x, int y, int is_down, void * in_refcon) { return 0; }
XPLMCursorStatus	dummy_cursor_status_handler(XPLMWindowID in_window_id, int x, int y, void * in_refcon) { return xplm_CursorDefault; }
int					dummy_wheel_handler(XPLMWindowID in_window_id, int x, int y, int wheel, int clicks, void * in_refcon) { return 0; }
void				dummy_key_handler(XPLMWindowID in_window_id, char key, XPLMKeyFlags flags, char virtual_key, void * in_refcon, int losing_focus) { }

PLUGIN_API int XPluginStart(
							char *		outName,
							char *		outSig,
							char *		outDesc)
{
	strcpy(outName, "HelloWorld3Plugin");
	strcpy(outSig, "xpsdk.examples.helloworld3plugin");
	strcpy(outDesc, "A Hello World plug-in for the XPLM300 SDK.");
	
	XPLMCreateWindow_t params;
	params.structSize = sizeof(params);
	params.visible = 1;
	params.drawWindowFunc = draw_hello_world;
	// Note on "dummy" handlers:
	// Even if we don't want to handle these events, we have to register a "do-nothing" callback for them
	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 = NULL;
	params.layer = xplm_WindowLayerFloatingWindows;
	// Opt-in to styling our window like an X-Plane 11 native window
	params.decorateAsFloatingWindow = 1;
	
	// Set the window's initial bounds
	// Note that we're not guaranteed that the main monitor's lower left is at (0, 0)...
	// We'll need to query for the global desktop bounds!
	int left, bottom, right, top;
	XPLMGetScreenBoundsGlobal(&left, &top, &right, &bottom);
	params.left = left + 50;
	params.bottom = bottom + 150;
	params.right = params.left + 200;
	params.top = params.bottom + 200;
	
	g_window = XPLMCreateWindowEx(&params);
	
	// Position the window as a "free" floating window, which the user can drag around
	XPLMSetWindowPositioningMode(g_window, xplm_WindowPositionFree, -1);
	// Limit resizing our window: maintain a minimum width/height of 100 boxels and a max width/height of 300 boxels
	XPLMSetWindowResizingLimits(g_window, 200, 200, 300, 300);
	XPLMSetWindowTitle(g_window, "Sample Window");
	
	return g_window != NULL;
}

PLUGIN_API void	XPluginStop(void)
{
	// Since we created the window, we'll be good citizens and clean it up
	XPLMDestroyWindow(g_window);
	g_window = NULL;
}

PLUGIN_API void XPluginDisable(void) { }
PLUGIN_API int  XPluginEnable(void)  { return 1; }
PLUGIN_API void XPluginReceiveMessage(XPLMPluginID inFrom, int inMsg, void * inParam) { }

void	draw_hello_world(XPLMWindowID in_window_id, void * in_refcon)
{
	// Mandatory: We *must* set the OpenGL state before drawing
	// (we can't make any assumptions about it)
	XPLMSetGraphicsState(
						 0 /* no fog */,
						 0 /* 0 texture units */,
						 0 /* no lighting */,
						 0 /* no alpha testing */,
						 1 /* do alpha blend */,
						 1 /* do depth testing */,
						 0 /* no depth writing */
						 );
	
	int l, t, r, b;
	XPLMGetWindowGeometry(in_window_id, &l, &t, &r, &b);
	
	float col_white[] = {1.0, 1.0, 1.0}; // red, green, blue
	
	XPLMDrawString(col_white, l + 10, t - 20, "Hello world!", NULL, xplmFont_Proportional);
}
  • Facebook
  • Reddit
  • Twitter
  • LinkedIn

About Tyler Young

Tyler is a software developer for X-Plane. Among other projects, he was in charge of the new X-Plane 11 user interface.
Bookmark the permalink.

6 Responses to Hello World (SDK 3)

  1. Antoine Becker says:

    Hi,

    I downloaded this Hello World project for Visual Studio 2010.
    I installed a fresh copy of Visual Studio 2010 with SP1
    The project is compiling without problem, and I copied the file in : "..../Resources/plugins/X-Plane-11-Window-API-Sample/64/win.xpl".

    I launch XPlane11, the plugin is not loaded and the log.txt file shows:
    "> : Error Code = 127 : The specified procedure could not be found."

    Can you help ?

    • Tyler Young says:

      I can't reproduce this. The error indicates that one of the five required callbacks is missing from the plugin. Since the sample code implements all 5, I suspect the error either comes from some another plugin, or it's caused by a modification you made to the code.

  2. Antoine Becker says:

    Thank you for the reply.

    I didn't touch any code in the sample, don’t have any other plugin installed, and the visual studio 2010 is a fresh install.

    I reproduced on another PC with all new fresh installs :
    - Visual Studio 2010 SP1 from MSDN
    - Latest Xplane11 version from Steam
    - An untouched Hello-World-SDK-3 project directly from your ZIP¨

    It compiles without any warning or errors and I deploy the generated (in release mode) win.xpl in the .../Resources/plugins/Hello-World-SDK-3/64/win.xpl
    and same result “Error Code = 127 : The specified procedure could not be found.” In the log.txt, so I assume it can be reproduced on many PCs.

    I suspect a linker or compiler option not set in your sample and that I didn’t notice in the documentation ?

  3. Antoine Becker says:

    Hi Philipp,
    The reason we used Visual Studio 2010 with your sample is because in the sample download title your mention "For Visual Studio 2010” and we wanted to use the sample the way you told us for a first test (It’s our first XPlane plugin, so we don’t know anything about your practices yet). We had to download Visual Studio 2010 from Microsoft MSDN because we don’t use it anymore for years

    To go on with our project, we downloaded the 64 bit version of the SDK (we had some difficulty to find the files, the links on your website seem dead).
    Now with Visual Studio 2017, our first plugin prototype is working well in Xplane11 linked to the 64 bits version of the 2.1.2 SDK.

    We will go on with this version of the SDK and come back when we are able to use the SDK 3.0 (may be more feedback from other users who reproduce the problem ?)

    In the meantime, thank you for your time.

  4. Atc Stager says:

    Works fine with VS community 2017

Leave a Reply

Your email address will not be published. Required fields are marked *

Please do not report bugs in the blog comments.
Only bugs reported via the X-Plane Bug Reporter are tracked.