Game Development Community

dev|Pro Game Development Curriculum

Enable Window Resize Callback for TGEA on All Platforms

by Ben McIntosh · 07/29/2009 (1:00 am) · 2 comments

For a much larger resource I am working on, I need to have a callback for window resize events. This is the most straight forward way of doing that with only 3 extra lines of code. These changes should also be platform independent.

Patch notation to follow:

- means line was removed
+ means line was added
! means line was changed
... means there is code in between that is not displayed here

In engine/source/gui/core/guiCanvas.cpp
...
 bool GuiCanvas::onAdd()
 {
     ...
     // Now, we have to hook in our event callbacks so we'll get
     // appropriate events from the window.
+    mPlatformWindow->resizeEvent .notify(this, &GuiCanvas::handleResize);
     mPlatformWindow->appEvent    .notify(this, &GuiCanvas::handleAppEvent);
     mPlatformWindow->displayEvent.notify(this, &GuiCanvas::handlePaintEvent);
     mPlatformWindow->setInputController( dynamic_cast<IProcessInput*>(this) );
     ...
 }
 ...
 void GuiCanvas::handleResize( WindowId did, S32 width, S32 height )
 {
-    // Nothing to do here right now.
+    if(isMethod("onResize"))
+        Con::executef(this, "onResize", Con::getIntArg(width), Con::getIntArg(height));
 }
 ...
Just recompile and then test out the callback to make sure it works.

In game/common/gameScripts/canvas.cs at the very end, add:
function GuiCanvas::onResize(%this, %width, %height)
{
   echo("Window Resized:" SPC %width SPC %height);
}
Then run the game in windowed mode and start to resize the window. You should see the callback fire. My next resource coming in the next day or so will show how to use this to make a nice robust widescreen letterbox feature. I decided to use a different method, but here is the resource.

By the way, some other nice canvas callbacks you should be aware of that aren't obvious are:
GuiCanvas::onLoseFocus
GuiCanvas::onGainFocus
GuiCanvas::onWindowClose
With the focus ones, you can do some neat stuff like pausing the game when losing window focus.

About the author

Ben is the co-founder of Urban Brain Studios; a new independent developer of games and game development tools.


#1
07/29/2009 (12:57 pm)
Very cool Ben, thanks.
#2
07/29/2009 (6:43 pm)
I can already imagine the possibilities. Sometimes great things can come from a few lines of code. Thanks!