Added plugin support for drawing messages to screen.
- Either a single string will be drawn to screen, or nothing will be drawn. - Intelligently registers and unregisters drawing callback to minimize overhead when no message is being displayed. - Currently does not support newline characters (\r or \n). Newlines will cause text to be drawn overlapped.
This commit is contained in:
73
xpcPlugin/xpcDrawing.cpp
Normal file
73
xpcPlugin/xpcDrawing.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "xpcDrawing.h"
|
||||
#include "XPLMDisplay.h"
|
||||
#include "XPLMGraphics.h"
|
||||
|
||||
//Internal Memory
|
||||
static int msgEnabled = 0;
|
||||
static int msgX = -1;
|
||||
static int msgY = -1;
|
||||
static char* msgVal = NULL;
|
||||
static float rgb[4] = { 0.5F, 1.0F, 0.5F };
|
||||
|
||||
//Internal Functions
|
||||
static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon)
|
||||
{
|
||||
if (msgVal)
|
||||
{
|
||||
XPLMDrawString(rgb, msgX, msgY, msgVal, NULL, xplmFont_Basic);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
//Public Functions
|
||||
void XPCClearMessage()
|
||||
{
|
||||
if (msgVal != NULL)
|
||||
{
|
||||
free(msgVal);
|
||||
}
|
||||
msgVal = NULL;
|
||||
if (msgEnabled)
|
||||
{
|
||||
XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL);
|
||||
msgEnabled = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void XPCSetMessage(int x, int y, char* msg)
|
||||
{
|
||||
//Determine size of message and clear instead if the message string
|
||||
//is empty.
|
||||
size_t len = strlen(msg);
|
||||
if (len == 0)
|
||||
{
|
||||
XPCClearMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
//Try to size msgVal to fit the new message. If we fail for any
|
||||
//reason, clear the message and bail.
|
||||
if (msgVal != NULL)
|
||||
{
|
||||
msgVal = (char*)realloc(msgVal, len);
|
||||
}
|
||||
else
|
||||
{
|
||||
msgVal = (char*)malloc(len);
|
||||
}
|
||||
if (!msgVal)
|
||||
{
|
||||
XPCClearMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
//Set the message, location, and enable drawing if necessary.
|
||||
strcpy(msgVal, msg);
|
||||
msgX = x < 0 ? 10 : x;
|
||||
msgY = y < 0 ? 600 : y;
|
||||
if (!msgEnabled)
|
||||
{
|
||||
XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL);
|
||||
msgEnabled = 1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user