Game Development Community

Display Images from the Web

by Phillip O'Shea · 11/05/2009 (7:37 pm) · 21 comments

I've been working on a small project which requires the use of images to be downloaded from the internet and displayed on a GUI control. Torque 3D didn't really deal with this problem in the way that I wanted it handled so I went ahead and created a few classes to download and render an image from the web.

The following files work in Torque 3D and would require minor changes to get working for TGE/A:

Download Files - TGEA (1.8.X)
Download Files - Torque 3D (1.0.X)
Download Files - Torque 3D (1.1.X)

Installation

Just add the files into your solution and compile. It doesn't really matter where you place them, as long as you know where to find them!

Known Issues

Torque determines what type of image it is by examining the file extension. This causes problems when someone used GIF encoding and incorrectly labels the file with a .JPG extension. Solution? Ensure that a GIF/JPG/PNG is a .GIF/.JPG/.PNG!

HttpImageController

This class deals with the actual downloading and creation process. Since we are dealing with the internet here, downloading an image is not instantanious so there is a delay from the time you send the query to the time the texture is actually created. Your game will run as normal, but the texture will not appear until the entire file has been received.

Here is how you use this class:
// Send the Query.
myController.LoadImage( "http://www.violent-tulip.com/include/images/PSK_thumb.jpg" );
// Bind the Response Delegate.
myController.GetResponseDelegate().bind( this, &MyClass::MyResponseMethod );
The first command is pretty simple, but the second command may not make much sense unless you are familiar with the Signal/Delegate system in Torque. Basically, the Response Delegate is the "callback" and the method that will be called when the callback is triggered is the method that you pass here. It is just a way of abstracting out the controller so that it can be used in a variety of ways.

The callback method must look like this:
void MyClass::MyResponseMethod( GFXTexHandle &pImageObject )
{
    // Do Stuff.
}
GuiHttpImageControl

This is a basic integration of the HttpImageController being used to render the image to the GUI. It does everything explained above and is used like this:
new GuiHttpImageControl()
{
    ImageUrl = "http://www.violent-tulip.com/include/images/Verve_thumb.jpg";
    isContainer = "0";
    Profile = "GuiDefaultProfile";
    HorizSizing = "right";
    VertSizing = "bottom";
    Position = "0 0";
    Extent = "256 360";
    MinExtent = "8 2";
    canSave = "1";
    Visible = "1";
    tooltipprofile = "GuiToolTipProfile";
    hovertime = "1000";
    canSaveDynamicFields = "0";
};
Thats it! All you need to do is have that control added to your GUI and it will download and display the image that you define in the property, "ImageUrl".

About the author

Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.

Page«First 1 2 Next»
#21
02/13/2010 (9:53 pm)
Do you know if images can be replaced with flash (swf) files? Not tried, but would be useful!
Page«First 1 2 Next»