• Which the release of FS2020 we see an explosition of activity on the forun and of course we are very happy to see this. But having all questions about FS2020 in one forum becomes a bit messy. So therefore we would like to ask you all to use the following guidelines when posting your questions:

    • Tag FS2020 specific questions with the MSFS2020 tag.
    • Questions about making 3D assets can be posted in the 3D asset design forum. Either post them in the subforum of the modelling tool you use or in the general forum if they are general.
    • Questions about aircraft design can be posted in the Aircraft design forum
    • Questions about airport design can be posted in the FS2020 airport design forum. Once airport development tools have been updated for FS2020 you can post tool speciifc questions in the subforums of those tools as well of course.
    • Questions about terrain design can be posted in the FS2020 terrain design forum.
    • Questions about SimConnect can be posted in the SimConnect forum.

    Any other question that is not specific to an aspect of development or tool can be posted in the General chat forum.

    By following these guidelines we make sure that the forums remain easy to read for everybody and also that the right people can find your post to answer it.

FSXA [C++] strings for gauges

Vitus

Resource contributor
Messages
1,480
Country
newzealand
Hi all,

I am confused about the use of strings in C++ gauges. Strings are one of the things I hated about C++ on sight when I started with it! There are so many different ways and each comes with new functions and restrictions. Also, if you try to find information on that topic on the web it is very likely to find code snippets that were written down in the Bronze Age and are outdated by now.

Hence the questions:
- what is the state-of-the-art type for text in gauge projects?
- what functions comes with the method(s)? Means: how do the strings have to be declared, initialized, destroyed? How can a value be assigned to it and what functions are used to convert int-string-int or float-string-float?


All this is way easier in Delphi (my favorite programming language!)....

Regards
Vitus
 
You have two choices. For non-GDI+ gauge strings, use the MAKE_STRING macro from the gauges.h file.

For GDI+ gauges, you can use any of the supported swprintf variations.

If you need only a simple digital display, stick with the MAKE_STRING macro because it's far, far faster to develop and maintain. ;)

floating point, use: "%02.0f" or "%3.2f", etc.
integer, use: "%d" or "%0d", etc.

Code:
FLOAT64 FSAPI CLK_callback5hi( PELEMENT_STRING pelement)
{
	FLOAT64 rwert;
	if ( clockmode == 0 ) rwert=CLOCK_SECONDvar.var_value.n;
	if ( clockmode == 1 ) rwert=CLOCK_SECONDvar.var_value.n;
	if ( clockmode == 2 ) rwert=FlightSeconds;
	if ( clockmode == 3 ) rwert=ElapsedSeconds;
	if ( panelleft > 50 ) 
		{ SHOW_IMAGE(pelement) ;  LIGHT_IMAGE(pelement) ; } 
	else { DARKEN_IMAGE(pelement) ; HIDE_IMAGE(pelement) ; }  
	sprintf(pelement->string,"%02.0f",rwert);
	return 0;
}


MAKE_STRING(CLK_String5hi,NULL,NULL,IMAGE_USE_ERASE | IMAGE_USE_TRANSPARENCY | BIT7,0,129,70,73,40,1000000,
			MODULE_VAR_NONE,MODULE_VAR_NONE,MODULE_VAR_NONE,RGB(255,178,0),RGB(0,0,0),RGB(0,0,0),
			GAUGE_FONT_DEFAULT6,GAUGE_WEIGHT_DEFAULT6,GAUGE_CHARSET6,0,0,NULL,CLK_callback5hi)
PELEMENT_HEADER CLK_ElementList2hi[] = {
&CLK_String5hi.header,
NULL
};
 
Hi Bill,

sorry, I didn't point out clear enough what I mean. It is not about the visuals! It's about the internal handling of strings. How do you store your string variables in C++? Do you use char arrays, or LPSTR, or something completely different?

I noticed in your example that you are using the "sprintf" function, which I also used for formating and copying strings. But it seems to be an outdated function - it gives me the warning:
'sprintf': name was marked as #pragma deprecated

And also seems to be the reason for my gauge to crash the sim. Probably because my string wasn't initialized corretly....

Vitus
 
The reason that sprintf is marked as deprecated is because the strsafe.h file has an updated version of it (snprintf I think) that accepts the size of the output string buffer as one of the parameters, allowing for buffer overrun checking before it happens :->.

An array of chars and a LPSTR are technically the same thing (an LPSTR is just a Long Pointer to STRing, which means its a pointer to an array of chars). If you are allocating actual space for a string, then you would use array of chars, like this:

char strOutput[1024];

if you are receiving a string pointer from another function, then you would declare it like this:

LPSTR pString;

Part of the problem with string functions in C++/Windows is that both were originally designed to handle ASCII strings, and both have been extended to support UNICODE strings, which causes some odd confusions on which string functions to use (although the gauge system is using ASCII strings, so stick with those, avoid the functions with a w (for wide string) in them (ie swprintf)). There are also some Win32 functions that deal with strings and are analogous to their C runtime counterparts(things like StringCchCopy or StringCbCopy - Cch has count of characters, Cb has count of bytes) - these deal with the same types of strings as the C Run Time versions, but since they are part of the Win32 API, are callable from all languages that target the Win32 API, which is why this parallel set exists.

Pretty much any string api that accepts/outputs a standard array of ASCII chars, or a pointer to an array of ASCII chars, should work to generate the strings you need for gauges.
 
Dear Vitus:

FLOAT64 FSAPI dme_string_cb( PELEMENT_STRING pelement )
{
SINT32 val=pelement->source_var[0].var_value.n;

if(val<0) val=0;

wsprintf(pelement->string, "%03d", (UINT32)val);

return val;
}


MAKE_STRING
(
obi_string,
&hsi_icon_dme_list,
hsi_fail,
IMAGE_USE_ERASE|IMAGE_USE_TRANSPARENCY|IMAGE_USE_LUMINOUS,
0,
308, 49,
60, 29,
3,
VOR1_OBI,
MODULE_VAR_NONE,
MODULE_VAR_NONE,
RGB(206, 206, 206),
RGB(0, 0, 0),
RGB(0, 0, 0),
GAUGE_FONT_DEFAULT,
GAUGE_WEIGHT_DEFAULT,
GAUGE_CHARSET,
0,
DT_CENTER | DT_VCENTER | DT_SINGLELINE,
NULL,
obs_string_cb
)


This is the format:
"%03d",

03 = Fill spaces with zeros if the number has less than 3 digits
d = Digits.

Is you look for sprintf formats you will find detailed explanation about that.

Declare strings:

char text[256]; //[StringSize +1 byte for null character (end of string indicator!!!!!!)]

memset(text, '\0', sizeof(text)) //Initialize the content with zero (null characters)

Now you can assign any text or use this variable with functions like memcpy or memset or sprintf.

The variable "text" is also a pointer containing the first address of the char array.

If you need more info just ask.

Alfredo Mendiola Loyola
Lima, Perú
 
Last edited:
Back
Top