Alt-Tab Problem
 
 
mtsmox  
Global user
(4/4/00 10:13:28 am)
Reply ALT-TAB problem
--------------------------------------------------------------------------------
I have a problem with ALT-TAB: If my game is running and the user presses ALT-TAB my program crashes. That is because my program hasn't got the focus anymore, so I can't blit to it. Also my DI devices are unaqcuired, because of the focus loss.
How can I tell if my program lost focus, are do you know another solution for my problem?

I was curious: how does alphablitting work? Ofcourse I could use the vbDABL dll, but I would like to know how it is done...

thanks
Joren



 
MetalWarrior  
Global user
(4/4/00 4:39:59 pm)
Reply Re: ALT-TAB problem
--------------------------------------------------------------------------------
The easiest way to tell if you lose focus is by using the Form_LostFocus event. Or you could use Subclassing, which is exampled in the DI mouse tutorial in the SDK.

Alphablitting is basically finding a color that is a given percentage difference between the source color and destination color for each pixel. That's what gives it that "see-through" look.



 
black eyez  
Global user
(4/4/00 4:51:06 pm)
Reply Alphablending
--------------------------------------------------------------------------------
Yeah, basically alphablending (blitting) is taking the source and destination colors and combining them to create an in-between color like this:

finalcolor = (sourcecolor * alphavalue) + (destinationcolor * (1 - alphavalue))

where alphavalue is a number between 0 and 1. This is the most basic form of alphablending (and the most common) called 'additive blending'. There are others like subtractitive, multiplicative, etc etc. I could get into much more detail if you want about simplifying the formula to make it faster etc if you want, but I won't for now  
 
 

david goodlad
  - aka black eyez
    webmaster: the black hole



 
Lucky
Unregistered User
(4/4/00 6:51:22 pm)
Reply ALT-TAB solution
--------------------------------------------------------------------------------
These are the (somewhat cheesy) functions that I use to detect and handle loss of exclusivity:
 

Public Function LostSurfaces() As Boolean

'This function will tell if we should reload our bitmaps or not
LostSurfaces = False
Do Until ExclusiveMode
DoEvents
LostSurfaces = True
Loop

'If we did lose our bitmaps, restore the surfaces and return 'true'
DoEvents
If LostSurfaces Then
mdd.RestoreAllSurfaces
End If

End Function

Private Function ExclusiveMode() As Boolean

Dim TestExMode As Long

'This function tests if we're still in exclusive mode
TestExMode = mdd.TestCooperativeLevel

If (TestExMode = DD_OK) Then
ExclusiveMode = True
Else
ExclusiveMode = False
End If

End Function
 

Call the "LostSurfaces" function.. if it returns "True" then you need to reload your bitmaps onto their surfaces. If it returns "False" then just carry on your merry way. Also note, this function repeatedly checks for a return to exclusivity (by calling the "ExclusiveMode" function) and won't return its value until we have the focus again. This prevents any potential errors caused by attempted blits of lost surfaces.
 

Lucky



 
mtsmox  
Global user
(4/6/00 10:07:40 am)
Reply Re: ALT-TAB solution
--------------------------------------------------------------------------------
Thanks for your replies, but I still had a few problems:

The Form_LostFocus event isn't called if the form loses focus and the Form_GotFocus event is only called if there are no objects on the form and only at startup.
I use Subclassing (DXEvent_Callback if that's what you mean) and that is called if a DirectInput device loses focus (a unacquire event) but I can't find out when the form regains focus... If I'm using Fullscreen mode, I can test it with DD.TestCooperativeLevel, but I also want to know if my form has got the focus in windowed mode. (if it isn't focused it has to pause, but if I can't tell if it is, I can't pause it  )

Thanks for your help,
Joren
 



 
Daniel Netz
Unregistered User
(4/6/00 2:39:27 pm)
Reply _
--------------------------------------------------------------------------------
Subclassing is a way of getting into the Windows Procedures that's hidden in VB. If you program Win32 for C++ you have to take care of the windows messages yourself while VB does that for you, that is, if you're not using subclassing.

I use the Form_Resize function to check what state the window is in. If you put a Select procedure there that checks what WindowState the form is in, you can easily keep track of when to restore your surfaces and when to pause the rendering.



 
MetalWarrior  
Global user
(4/6/00 7:14:05 pm)
Reply Re: _
--------------------------------------------------------------------------------
Hey, that's a good idea. Now why didn't I think of that? Don't answer that. ;-)



 
Daniel Netz
Global user
(4/7/00 12:39:17 am)
Reply Re: _
--------------------------------------------------------------------------------
I think I'll do that anyway, if you don't mind  

I would never have thought about it either, if I didn't learn that the Form_Resize event is triggered not just when the window resizes, but also when the WindowState has changed.
Daniel Netz, Sentinel Design



 
mtsmox  
Global user
(4/7/00 10:44:35 am)
Reply Re: _
--------------------------------------------------------------------------------
But Form_Resize isn't triggered if a form loses focus. So If you bring another form to the foreground, the programme happely continues. I tried Screen.ActiveForm but that only returns forms you use yourself, so if another programme is on top, it still returns a form you own... so you can't check if your application is running on top or not  

This obviously isn't that important, but those little things really annoy me  
If you know the answer, I will be very grateful...

Joren



 
MetalWarrior  
Global user
(4/7/00 1:53:26 pm)
Reply Re: _
--------------------------------------------------------------------------------
If you're running in FullScreen mode, Form_Resize is all you need.

If, however, you're running in Windowed mode, then the Form_LostFocus event should be triggered. Are you sure that doesn't work?



 
mtsmox  
Global user
(4/9/00 5:48:08 am)
Reply Re: _
--------------------------------------------------------------------------------
Form_LostFocus is only called if you switch from one form to another within your own application, not if you switch between apps.
And if I test Form_Resize by putting a stop command in it and make a fullscreen form and switch between apps with ALT-TAB, Form_Resize isn't called... but if you minimize or maximize it is called. But I'm testing it without DX, perhaps if you have setup DX and then use ALT-TAB the WindowState IS changed.

Getting back to my first post: I would like to know how to use alpha blitting in a FAST way... I've seen the tutorial on this page but that was for DX6. Isn't setpixel and getpixel in DX7 fast enough for alpha blitting, so you don't have to use CopyMemory?
Could anybody tell me how to do it fast?

thanks
Joren



 
Voodoo VB  
Administrator
(4/9/00 6:51:31 am)
Reply Well
--------------------------------------------------------------------------------
Well, no, it isn't fast enough really. If you would like to see, dl the BiohazrdDD library from the source code page. It uses setpixel/getpixel for alpha-blending.

Peter



 
black eyez  
Global user
(4/9/00 10:50:34 am)
Reply Re: Well
--------------------------------------------------------------------------------
Yeah, doing it in native vb code is.. err... shall we say really slow? :P

Even with copymemory it can be quite slow. *shameless plug* so if ya wanna do it fast, use some assembly like my dll 'vbDABL' hehehe

It's at my website: blackhole.thenexus.bc.ca/ under the downloads section.

David

david goodlad
  - aka black eyez
    webmaster: the black hole



 
CoryP
Global user
(4/9/00 1:59:03 pm)
Reply Re: ALT-TAB
--------------------------------------------------------------------------------
You could try to use the GetActiveWindow API function which returns a handle to the active window. The declaration is 

Public Declare Function GetActiveWindow Lib "user32" () As Long

Edited by CoryP at: 4/9/00 1:59:03 pm