Help a Noob! (re: building a new MFD)

bujin

New member
Joined
Oct 20, 2007
Messages
505
Reaction score
2
Points
0
Location
Wrexham, N. Wales, UK
I'm taking my first tentative steps into developing stuff for Orbiter - dunno how far I'll get with it, but I thought I'd give it a shot anyway! :p

Anyway, I have kinda fallen at the first hurdle. Not a great start! I have made a copy of the MFDTemplate, renamed it as TestMFD, given it the appropriate MFD title and compiled it, and it appears to work.

I've scoured the documentation, and I have looked through the CustomMFD, but I can't find the simple command to just output some text to the MFD!

If someone could just tell me how to output "Hello, World!" to the MFD (in time-honoured programming tradition! ;) ), I might be able to make a better go of it! :speakcool:


Ta!
 

agentgonzo

Grounded since '09
Addon Developer
Joined
Feb 8, 2008
Messages
1,649
Reaction score
4
Points
38
Location
Hampshire, UK
Website
orbiter.quorg.org
Presumably you have an update function in your MFD class. If not, then you need to overwrite it.

One of the parameters passed to the Update function is a window Handle (HWND). To draw anything to the HUD, you need to draw to this window. You can use the TextOut function to do this:
Code:
int x = 5, y = 10; // offset from top left of MFD as to where to draw text
char msg[] = "Hello World";
TextOut(hWnd, x, y, msg, strlen(msg));

Use the GDI functions (lookup GDI in the help or web - not GDI+ though) to draw to the HUD.
 

Enjo

Mostly harmless
Addon Developer
Tutorial Publisher
Donator
Joined
Nov 25, 2007
Messages
1,665
Reaction score
13
Points
38
Location
Germany
Website
www.enderspace.de
Preferred Pronouns
Can't you smell my T levels?
Having understood this, consider this example:


Code:
// globals
// allow you to control how much text fits in your MFD
const int MFD_LINE_COUNT = 20;
const int MFD_CHAR_WIDTH = 35;

const int BUFSIZE = 255;

TestMFD::TestMFD (DWORD w, DWORD h, VESSEL *vessel) : MFD(w, h, vessel)
{
    linespacing = h / MFD_LINE_COUNT;
    charwidth = w / MFD_CHAR_WIDTH;
    width = w;
}


TestMFD::Update(HDC hDC)
{
char buf[BUFSIZE];
int i = 1, nr = 5, len = 0;
memset(buf, 0, BUFSIZE); 
// or memset(buf, 0, sizeof(buf)); but don't pass the buf as a parameter then
strncpy(buf, " Hello World, number = ", BUFSIZE-1); // save 1 for NULL terminator
len = strlen(buf);
SetTextColor(hDC, 0x00FFBB22);    // Light blue
TextOut(hDC, 0, static_cast<int>(linespacing * i), buf, len)
SetTextColor(hDC, 0x0022FFAA);    // Lime Green
memset(buf, 0, BUFSIZE); 
sprintf(buf, "%d", nr);
TextOut(hDC, static_cast<int> (charwidth * len), static_cast<int>(linespacing * i), buf, strlen(buf)) // for a dynamic column
// or
//TextOut(hDC,  static_cast<int>(width/2), static_cast<int>(linespacing * i), buf, strlen(buf)); // for a fixed column
i ++; // for next line
memset(buf, 0, BUFSIZE); 
strncpy(buf, " Goodbye, cruel world", BUFSIZE-1); // save 1 for NULL terminator
TextOut(hDC, 0, static_cast<int>(linespacing * i), buf, strlen(buf))
}
you could simply do:
Code:
sprintf(buf," Hello World number %d",nr);
but the string and the variable would have the same colour.
 
Last edited:

bujin

New member
Joined
Oct 20, 2007
Messages
505
Reaction score
2
Points
0
Location
Wrexham, N. Wales, UK
Excellent. Thanks guys. :speakcool:

I've been programming in other languages for years, but I'm new to C++ so it'll take a bit of decoding, but I can handle that!

Thanks for your help. :)
 

V8Li

Member
Joined
Apr 9, 2008
Messages
200
Reaction score
0
Points
16
I've just released my first ever addon too (and my fully functional C++ script), I too had programing experience but none with C++ (I know some Java dough and are about the same concept wise). I've used the same approach, building an mfd template of my own, with pages and everything. Then I've built the HUDdataMFD, is not much but it was good practice plus it was something that I needed (and some others). Get the source code here: http://www.orbiter-forum.com/showthread.php?t=1617 . I hope it will help you.
 

agentgonzo

Grounded since '09
Addon Developer
Joined
Feb 8, 2008
Messages
1,649
Reaction score
4
Points
38
Location
Hampshire, UK
Website
orbiter.quorg.org
If you're coming from java to C++, then one of the main things to mention is that C++ doesn't have a garbage collector, so you need to explicitly delete all objects that you create on the heap. If you don't do this you will have MASSIVE memory leaks.
 

Urwumpe

Not funny anymore
Addon Developer
Donator
Joined
Feb 6, 2008
Messages
37,627
Reaction score
2,345
Points
203
Location
Wolfsburg
Preferred Pronouns
Sire
LOL, yeah. But on the other hand, your memory consumption will be far better as you can ever get with Java, even with using all tricks for telling the garbage collector that a object is no longer needed.
 

computerex

Addon Developer
Addon Developer
Joined
Oct 16, 2007
Messages
1,282
Reaction score
17
Points
0
Location
Florida
Presumably you have an update function in your MFD class. If not, then you need to overwrite it.

One of the parameters passed to the Update function is a window Handle (HWND). To draw anything to the HUD, you need to draw to this window. You can use the TextOut function to do this:
Code:
int x = 5, y = 10; // offset from top left of MFD as to where to draw text
char msg[] = "Hello World";
TextOut(hWnd, x, y, msg, strlen(msg));
Use the GDI functions (lookup GDI in the help or web - not GDI+ though) to draw to the HUD.

MFD::Update only has one parameter, the handle to the device context. TextOut's first parameter is not a windows handle, it's a HDC.
 
Top