Added support for newlines in TEXT command.

This commit is contained in:
Jason Watkins
2015-04-08 09:04:33 -07:00
parent f8372f6f4d
commit c3e284fc02
5 changed files with 48 additions and 36 deletions

View File

@@ -507,19 +507,19 @@ int handleTEXT(char *buf, int len)
updateLog("[TEXT] ERROR: Length less than 14 bytes", 39);
return -1;
}
int msgLen = buf[13];
size_t msgLen = (unsigned char)buf[13];
if (msgLen == 0)
{
updateLog("[TEXT] Text cleared", 19);
XPCClearMessage();
updateLog("[TEXT] Text cleared", 19);
}
else
{
updateLog("[TEXT] Text set", 15);
int x = *((int*)(buf + 5));
int y = *((int*)(buf + 9));
strncpy(msg, buf + 14, msgLen);
XPCSetMessage(x, y, msg);
updateLog("[TEXT] Text set", 15);
}
return 0;
}

Binary file not shown.

Binary file not shown.

View File

@@ -5,18 +5,23 @@
#include <string.h>
//Internal Memory
static int msgEnabled = 0;
static bool msgEnabled = false;
static int msgX = -1;
static int msgY = -1;
static char* msgVal = NULL;
static float rgb[4] = { 0.5F, 1.0F, 0.5F };
static char msgVal[256] = { 0 };
static size_t newLineCount = 0;
static size_t newLines[64] = { 0 };
static float rgb[3] = { 0.25F, 1.0F, 0.25F };
//Internal Functions
static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon)
{
if (msgVal)
XPLMDrawString(rgb, msgX, msgY, msgVal, NULL, xplmFont_Basic);
int y = msgY - 16;
for (size_t i = 0; i < newLineCount; ++i)
{
XPLMDrawString(rgb, msgX, msgY, msgVal, NULL, xplmFont_Basic);
XPLMDrawString(rgb, msgX, y, msgVal + newLines[i], NULL, xplmFont_Basic);
y -= 16;
}
return 1;
}
@@ -24,52 +29,39 @@ static int MessageDrawCallback(XPLMDrawingPhase inPhase, int inIsBefore, void *
//Public Functions
void XPCClearMessage()
{
if (msgVal != NULL)
{
free(msgVal);
}
msgVal = NULL;
if (msgEnabled)
{
XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL);
msgEnabled = 0;
}
XPLMUnregisterDrawCallback(MessageDrawCallback, xplm_Phase_Window, 0, NULL);
msgEnabled = false;
}
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);
size_t len = strnlen(msg, 255);
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)
//Set the message, location, and mark new lines.
strncpy(msgVal, msg, len);
newLineCount = 0;
for (size_t i = 0; i < len && newLineCount < 64; ++i)
{
msgVal = (char*)realloc(msgVal, len);
if (msgVal[i] == '\n' || msgVal[i] == '\r')
{
msgVal[i] = 0;
newLines[newLineCount++] = i + 1;
}
}
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;
//Enable drawing if necessary
if (!msgEnabled)
{
XPLMRegisterDrawCallback(MessageDrawCallback, xplm_Phase_LastCockpit, 0, NULL);
msgEnabled = 1;
msgEnabled = true;
}
}