Problem still GDI woes... why is this not working?

Moach

Crazy dude with a rocket
Addon Developer
Joined
Aug 6, 2008
Messages
1,581
Reaction score
62
Points
63
Location
Vancouver, BC
hi...

well i've managed to get it to compile now... so the last thread about this seems no longer fitting

yet, although it compiles alright, some functions seem to have no effect... particularly those that involve brushes and fonts...

functions such as "textOut" and my variant of it "textFltData" are working correctly, as i can see their output on the display....

"SelectObject" and some others, still refuse to yield proper results...


so i figure i might as well post up my code for you to see if you have any thoughts on why any of this wouldn't work

this is the struct i'm using to collect my GDI-related stuff, only the final three handles are being put to use so far...
Code:
///////////////////////////
//
//           things used by GDI functions for painting the displays...

static struct
{
	HBRUSH greenBrush;
	HPEN   greenPen;

	HBRUSH yellowBrush;
	HPEN   yellowPen;

	HBRUSH redBrush;
	HPEN   redPen;

	HBRUSH whiteBrush;
	HPEN   whitePen;
	
	HBRUSH bkgdBrush;
	HPEN   nullPen;

	HFONT  fontSmall;
	
} sysmfd_GDI;

this sits in a DVC-specific header file... no problems so far...

the following, is the last part of the clbkLoadVC function...
Code:
// 
	//
	
	
	SURFHANDLE sysmfd_t1;
	//
	sysmfd_t1 = oapiGetTextureHandle(mdl_dvc, 5);
	oapiVCRegisterArea(DVC_SYSMFD_1, _R(0,0,256,256), PANEL_REDRAW_ALWAYS, PANEL_MOUSE_IGNORE, PANEL_MAP_NONE, sysmfd_t1);
	
	
	
	
	// setup GDI thingz
	//
	
	sysmfd_GDI.bkgdBrush = CreateSolidBrush (RGB(0,0,0));
	sysmfd_GDI.nullPen = CreatePen (PS_NULL, 0, RGB(0,0,0));
	
	
	
	
	HDC hDC = GetDC (NULL);
	long fontH12 = -MulDiv(10, GetDeviceCaps (hDC, LOGPIXELSY), 72);
	ReleaseDC (NULL, hDC);
	
	
	
	sysmfd_GDI.fontSmall = CreateFont(fontH12, 0, 0, 0, FW_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0, TEXT("Courier New"));
	//
	if (!sysmfd_GDI.fontSmall)
	{
		cout << "SYSMFD font FAIL!" << endl;
	}

i'm not getting any errors from here either, nor any "fail" message shows in my debug console...


now, on to the final part, the actual drawing of the panel, in the clbkVCRedrawEvent function
Code:
bool DGa2::clbkVCRedrawEvent (int id, int ev, SURFHANDLE surf)
{
	HDC hDC = oapiGetDC(surf);
	
	
	switch (id)
	{
		//
		
	
		case DVC_SYSMFD_1:

		SelectObject (hDC, sysmfd_GDI.nullPen);
		SelectObject (hDC, sysmfd_GDI.bkgdBrush);
		Rectangle (hDC, 0,0, 256,256); // rectangle shows up all white...
		
		RECT r =  _R(10,10,25,25);
		FillRect (hDC, &r, sysmfd_GDI.bkgdBrush); // also white...
		
		SetTextColor (hDC, RGB(255,220,0));
		SetBkColor (hDC, RGB(0,0,0)); // these work, text changes color
		
		HFONT restoreFont = (HFONT)SelectObject (hDC, sysmfd_GDI.fontSmall);
		// this doesn't... still getting the same default font
		
		TextOut(hDC, 150,90,  TEXT("MAIN | INT"), 10);
		TextOut(hDC, 150,105, TEXT("RD OPEN"), 7);
		TextOut(hDC, 150,120, TEXT("FS OPEN"), 7); // these work as expected...
		
		
		SetTextColor (hDC, RGB(0,255,0));
		TextOut(hDC, 50, 15, TEXT("APU OffLine"), 11); 
		
		double mainfuel = GetFuelMass ();
		txtFltData (hDC, 60, 150, TEXT("Fuel Main:  "), mainfuel);
		//
		double fuelPct = (mainfuel / GetMaxFuelMass())*100;
		txtFltData (hDC, 60, 165, TEXT("Fuel Pct:   "), fuelPct);
		//
                // also working...

		
		SelectObject(hDC, restoreFont); // no effect apparently
		
		oapiReleaseDC (surf, hDC);
		return true;
	
	}
	
	
	//
	return false;	
}


it appears my fonts brushes and pens simply aren't working... they fail silently, leaving me to post here for any insight... this makes very little sense... why would the other functions work and not these?


thanks in advance for any advice :tiphat:

cheerz
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,218
Reaction score
1,566
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
Your best bet is to fire up the debugger and step through the code in question. That will let you examine the various resource handles as well as look at the return codes of each different call. :thumbup:
 

SiameseCat

Addon Developer
Addon Developer
Joined
Feb 9, 2008
Messages
1,699
Reaction score
2
Points
0
Location
Ontario
Your best bet is to step through the code.

A couple of possible problems:
You probably only need to create the HPEN/HBRUSH once, instead of in clbkLoadVC (which is called every time you enter the VC view or change position).
Also, when you call SelectObject for the first time, you should store the default object that's returned and call SelectObject with it before you call oapiReleaseDC.
 

Moach

Crazy dude with a rocket
Addon Developer
Joined
Aug 6, 2008
Messages
1,581
Reaction score
62
Points
63
Location
Vancouver, BC
I GOT IT!

it started working when i moved the creation code to the redraw function...

stepping through the code revealed that all things created in the setup function did not survive until redraw was called... although those are static vars, something seems to delete them as soon as the function in which they were created ends...

is there anyway to stop this from happening?
i don't see how it would be "ok" to re-create the brushes and fonts every frame... talk about a resource hog...

how can i make sure that when i create my GDI things, they STAY THERE?
:shrug:
 

Hielor

Defender of Truth
Donator
Beta Tester
Joined
May 30, 2008
Messages
5,580
Reaction score
2
Points
0
I GOT IT!

it started working when i moved the creation code to the redraw function...

stepping through the code revealed that all things created in the setup function did not survive until redraw was called... although those are static vars, something seems to delete them as soon as the function in which they were created ends...

is there anyway to stop this from happening?
i don't see how it would be "ok" to re-create the brushes and fonts every frame... talk about a resource hog...

how can i make sure that when i create my GDI things, they STAY THERE?
:shrug:
As long as you delete the brushes and fonts after using them (via DeleteObject), you won't be a "resource hog." If you don't delete them, that's not really called a "resource hog" either, that's called a "memory leak" :)

As for why it didn't work when you made it static: the sysmfd_GDI struct of yours, is it a class member of something or a global variable? If it's global, the "static" keyword doesn't do what you think it does, and can be omitted. Also, as SiameseCat pointed out, you should only create these once, instead of every time the VC is loaded.
 

Moach

Crazy dude with a rocket
Addon Developer
Joined
Aug 6, 2008
Messages
1,581
Reaction score
62
Points
63
Location
Vancouver, BC
As long as you delete the brushes and fonts after using them (via DeleteObject), you won't be a "resource hog." If you don't delete them, that's not really called a "resource hog" either, that's called a "memory leak" :)

i know... i meant "resource hog" in the sense that it it's an unnecessary thing to do every frame

As for why it didn't work when you made it static: the sysmfd_GDI struct of yours, is it a class member of something or a global variable? If it's global, the "static" keyword doesn't do what you think it does, and can be omitted. Also, as SiameseCat pointed out, you should only create these once, instead of every time the VC is loaded.

now... this makes sense! it totally escaped me that this would happen... i assumed that a static struct defined in a header would be evenly accessible for all modules which included it... but not at all!
in such a case, the struct is independently instanced for each module, which sorta makes sense, now that i think of it... since headers are included before the actual compiling takes place

guess i still have a couple of habits from ActionScript... "static" back there, really does "what we think it does"

i'll try changing that once i get home - shoud work now :tiphat:


thanks everyone... i think we cracked this one :thumbup:
 
Last edited:
Top