PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

PocketFrog as a subwindow

Any tips on how I can set up PF to run in a subwindow.

I'm working on a GUI for the H/PC with an 800x600 display and what I want is a regular windows program (NOT MFC) to act as the mainframe for the GUI, and then when selected from the menu, display and run a PF visualisation in a subwindow.

I've managed to arrange this behaviour, but it only works ONCE. After I've closed the PF window (by calling PostQuitMessage(0) in StylusDown()) I can't start it again. All it draws is the frame with nothing in it (and no callbacks, så I can't close it again neither).

Any hints on what I'm doing wrong here? The code is at http://www.nada.kth.se/~t98_hes/cas/gui/ if that would help. It's based on the "Hello World" WCE Application skeleton. I'm kinda new to windows programming so I'm hoping it's just something I've missed. The main window is not ATL based, could this be it. Changing it (from regular windows style) to ATL ain't that hard (I hope) but it would be nice if I didn't have to change any code since the rest of the stuff is working just fine and it's easy to expand.

I register the PF/ATL window with "_Module.Init( 0, hInstance );" after I've registered the main window. And I start PF by invoking PFClass.Run() in the WndProc Callback function (when selecting the proper menu item)

It seems like PF is not completely and correctly exited/destroyed and that's why it refuses to start up a second time (just as if you try to run two PF windows at the same time).

Also, how do you get the PF to display a "Close this window" icon in the upper right corner (as one gets with all the other windows and dialogs you create).
Kzinti · 15 Nov 2002, 19:47 UTC — PocketFrog as a subwindow
adde wrote:
Any tips on how I can set up PF to run in a subwindow.


There is currently no support for windowed mode in PocketFrog.

adde wrote:
(...) run a PF visualisation in a subwindow.

I've managed to arrange this behaviour, but it only works ONCE. After I've closed the PF window (by calling PostQuitMessage(0) in StylusDown()) I can't start it again.


I am really surprised to hear you managed to achieve this, beacause PocketFrog doesn't support it. Only fullscreen mode is supported and this is because of GAPI's limitations. Windowed mode on HPC (or PPC) would be possible using GDI, but again there is no support at the moment.


adde wrote:
The main window is not ATL based, could this be it.


No, ATL as nothing to do with it. You can use ATL, MFC or straight Win32 API calls. They all are the same for PF.

adde wrote:
I register the PF/ATL window with "_Module.Init( 0, hInstance );"


This is registering your module, not your window. Window class registration is done automatically for you by ATL's window implementation.

adde wrote:
And I start PF by invoking PFClass.Run() (...)
It seems like PF is not completely and correctly exited/destroyed and that's why it refuses to start up a second time (just as if you try to run two PF windows at the same time).


I though that this was already working (starting PF, closing it, restarting it). I will check this when I have the time.

adde wrote:
Also, how do you get the PF to display a "Close this window" icon in the upper
(...)


Since PF doesn't support windowed mode, there is no way to display a close box (or any caption bar).
adde · 16 Nov 2002, 00:22 UTC — small correction
Ok, thanks for the info Kzinti. I'll just take the PF code and modify it to serve my purposes then. I've prosponed to do this as long as possible, because it's always better to leave the original code as it is, and derive from it. Just in case there will be newer releases or bugfixes or stuff like that. I saw that you won't be releasing any new versions of PF though. Maybee if I get this working someone developing graphical apps on HPC will find it usefull.

Anyways, before I end I'd like to make a few clarifications:

Only fullscreen mode is supported and this is because of GAPI's limitations. Windowed mode on HPC (or PPC) would be possible using GDI, but again there is no support at the moment.


I've looked briefly at the PF code, and it seems like it wrapps the GDI if it can't find GAPI, and there is no GAPI on HPC so.... This would explain two things:
1. The poor performance on my HPC (framerates below 15 in a 320x240 window and even less when the window size goes up)
2. The fact that I managed to launch a PF window from a Win32 application.

I though that this was already working (starting PF, closing it, restarting it). I will check this when I have the time.


What I ment was: PF refuses to launch from my win32 app a second time. The same thing happens if you try to launch two PF windows from the win32 apps mainframe.
Indeed, PF uses a DIB when GAPI is not available. Because of this, it should be easy to add windowed support to PF.

I've already tried direct screen access on the Jornada 720 and it wasn't any faster then using GDI. The bandwidth is simply not there.

Let me know what you find on your device.
adde · 25 Nov 2002, 12:46 UTC — Success
I took the liberty of making my own window class and a new thread class. I'm now getting the behaviour I wanted. If someone wants to look at the code, I can upload it to a suitable place. Just ask.

First of all, there is a small "bug" in the PocketPC code. The global variables are never reset to zero after the window has been destroyed, so you can't create another one unless you shut down the entire program. This is not a problem for regular PF usage so it's not a real bug, but still.

What I did was to add a function like this in the pocketpc.cpp file (and declared it in pocketpc.h):
Code:
void PocketPC::InvalidateDrivers()
{
if (l_displayDriver)
l_displayDriver = 0;
if (l_inputDriver)
l_inputDriver = 0;
}

and call it in the Init() function just before we create the new Display object

And second of all, if you want to be able to create and run more than one window in parallell, you need to start a new thread with each window. Otherwise the second (and third and ...) window won't get any window messages. The main thread is locked up in the infinity loop in the first window.

What you do here, is write your own thread class, and then derive your new window class from it. Instead of the infinity loop in Run(), you create a new thread and tell it to run GameLoop as long as it's active. You also need to set a flag to make shure the thread doesn't end untill you want it to.

Now all the window messages are taken care of in the main window message loop in your control application and passed on to your new windows. Since they are running on their own threads, they can intercept and act on these messages.

For performance, you also need to add a Suspend() and Resume() function to your thread class that alters the window thread priority and thread time-quota. Otherwise, they will keep interupting each other.

There are a few more minor changes you need to make to your new threaded window class, like changing the window styles in Init() and removing calls to static function PocketPC::Suspend/Resume, but I won't describe them all here. If you're interested in the code, I will upload it to http://www.nada.kth.se/~t98_hes/pf/ as soon as I've removed all the unnessessary debug code. I've only changed the pocketpc.cpp/.h file and then added my own window class and the thread class to the PF project. So to compile it, you need to do the same.

And finally, just to make shure: This is only for GDI wrapping purposes. As Kzinti said earlier, the GAPI can't run in windowed mode and all though I havn't tried it myself (I don't have GAPI on my device unfortunately) I have to take his word for it. He's the master, I'm just an understudy.

Hope this can help some poor smuck sitting on a H/PC device without GAPI support.
dkelle4umbc · 02 Jul 2003, 18:05 UTC — Success -->Upload please
Hey Adde,

I would love to have a look at your code....could you upload it some place :roll:

adde wrote:
I took the liberty of making my own window class and a new thread class. I'm now getting the behaviour I wanted. If someone wants to look at the code, I can upload it to a suitable place. Just ask.
The next version of PocketFrog (using the soon-to-be PocketHAL) will have support for windowed mode. Stay tuned.
dkelle4umbc · 02 Jul 2003, 21:14 UTC — How long until next build?
I am currently running 2 projects using your wonderful, wonderful code...one ends August 27 and the other is on going. You should really consider the application of you software to the non-game-development industry. There is so much potential with PF. Keep up the good work! :wink:

Thierry wrote:
The next version of PocketFrog (using the soon-to-be PocketHAL) will have support for windowed mode. Stay tuned.
You should really consider the application of you software to the non-game-development industry. There is so much potential with PF. Keep up the good work!

I totally agree with dkelle4umbc (strange nick b.t.w.). PF is very powerfull and can easily (well, with a few minor alterations) be used for GUI applications (i.e. not games).

As I wrote in my previous post (a couple of months ago) you need to make the game class threadable in order to spawn multiple windows AND, as Thierry said, this is only valid on non GAPI platforms as GAPI doesn't allow windowed mode (or atleast not the way PF is written today).

Of course, one could modify PF further to always use GDI if you want to do windowed mode on GAPI enabled devices, but really; what's the point? On 240x320 devices (i.e. GAPI devices) only one window should be visable anyways.

Enough said. For those of you (and dkelle4umbc in perticular) who don't have the time to wait for PF2 (Thierry's rewrite of PF with a HAL layer) to write applications in windowed mode, I have uploaded a few selected classes of my GUI project.

On the following link:

http://www.nada.kth.se/~t98_hes/pf/

you'll find the following files:

ControlPanelWnd.cpp/h
- A basic ATL window that shows how I combined PF with regular ATL/WTL components.

CSLock.h
- CriticalSection lock for Thread syncronization.

GUI.cpp
- The base application. From here, I launch the GUI and this is also where I define the window message handeling loop (as oppose to in the Frog::Game object as PF does)

ManualDriveWnd.cpp/h
- Just an example of how to use my custom PFWindow class

PFWindow.cpp/h
- A rewrite of Frog::Game to work as a subwindow running its own thread to update its content.

pocketpc.cpp/h
- These two files must be added and recompiled into the PF source.

README.txt
- Some info about the PFWindow object.

Thread.cpp/h
- My thread class. All thread objects inherit from this class (and CSLock).

Well.. that's about it. From here you should be able to write your very own GUI application using PF as a GDI wrapper.

Oh, right.. the full documentation/reference of my GUI can be found on the following link if it's of any interest:

http://www.nada.kth.se/~t98_hes/cas/gui/html/

Cheers!
I could also upload my tiny C++ wrapper for the Expat XML parser if anyone is interested.
Thanks a lot for sharing this code adde. I sure will take a look tonight.

When I wrote the Game class, it was really a quick hack to put samples together. People have started using it for their game, putting me to shame. It's so much inflexible when you want to handle multiple view/windows.
No sweat! I would have posted the complete source for the GUI but it doesn't make much sence to run it without access to the robot it's suppose to manipulate.

The selected files should be enough to get a feeling of how one could use PF as a subwindow in an ATL/WTL framework.

It also provides a nice thread and CRITICAL_SECTION wrapper that one can use in other projects as well.
Could you not support multiple windows with only one thread? My understanding is that the message pump should be dispatching the messages to the appropriate windows.
Obviously you can, since not many regular GUI applications are multi threaded.

But the problem is that you want to call GameLoop() over and over. If you create two windows, you have two game loops to call. Without building some sort of global queue of window objects, calling each after another, the simplest way is to use a separate thread for each window.

As you said, the message pump (or whatever it's called) will take care of all the window messages and dispatch them to their respective window. BUT this is provided that the main thread (or GUI thread as I would like to call it) doesn't get stuck somewhere.

For instance, say you get an event from a menu or a button and you want to create a new object/window or call a function in a given object as a result. If that object (or function) has an infinite while loop (or for some other reason doesn't return to the event handler function) it will lock the GUI thread and you'll stop processing events.

So, this is why you need one thread for each window, to keep the GUI thread free.

Had the window been a standard ATL window which is only updated/redrawn when it recieves a WM_PAINT event, then you only need a single thread because it will never get stuck (unless you call an function that doesn't return to the event handler ofcourse)

Come to think of it, you should be able to rewrite PF using the WM_TIMER event to call GameLoop with a given interval, and then use Suspend() and Resume() to stop and start the timer. This will work with a single thread. Provided you only update the window that currently has the focus, this approach will behave exactly as the multiple threaded one if you set the timer correctly. If you want several windows to update its content simultaniously, then perhaps the WM_TIMER approach will work even better since each window will wait for the other to return from the WM_TIMER event handler function. The only way to get this with threads is to define them as uninteruptable (you can do this in WinCE but not on Win32) and hence you should avoid updating more than one window at a time (it will produce flicker or not fully updated windows)

Hmm.. seems the WM_TIMER approach would be better. I don't know why I didn't use it myself. Probably because I didn't knew it excisted and I knew I would use threads for some network stuff anyways. I guess the only hard part is to figure out a good timer interval. On WinCE the timer resolution is 1 ms/tick, but on 2000/XP it is 10 ms and on ME/98 it is 55 ms giving a maximum (theoretical) framerate of approx 18 which is way too low. Setting the interval to 1 (tick) will guarantee the fastest performance, and the windows will interleave in random order (but never twice in a row).

Anyways.. that's something to think about.
Looking at timers some more and I found out that on the PC there is something called multimedia timers and high-resolution timers which one can use instead of the standard timer to get a higher resolution. I recon a timer of 1 ms should suffice (generating a theoretical maximum framerate of 1000FPS for a single window).
dkelle4umbc · 03 Jul 2003, 16:33 UTC — Thanks for the code....
Adde thanks for the code...I'll take a look at it over this weekend. dkelle4umbc is my online identity, UMBC = "University of Maryland Baltimore County" and dkelle4 is my username.