Game Development Community

Display Images from the Web

by Phillip O'Shea · 11/06/2009 (3:37 am) · 14 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

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 the latest offerings Violent Tulip.


#1
11/06/2009 (4:30 am)
Very useful for me. Gonna try this.
Thanks.
#2
11/06/2009 (8:16 am)
Simple idea yet simply brilliant as well. Thx Phil.
#3
11/06/2009 (9:37 am)
Thanks indeed.
#4
11/06/2009 (3:19 pm)
Awesome Phillip. Thanks for this resource ... definitely very useful. :)
#5
11/06/2009 (3:58 pm)
very useful, thanks, this resource use the same main thread right?, so if the web server dont respond you see lag?
#6
11/06/2009 (4:06 pm)
Thanks! Definitely have a use for this in our project!
#7
11/06/2009 (9:56 pm)
Hey very cool, I want to try this!
#8
11/07/2009 (2:05 am)
Quote:very useful, thanks, this resource use the same main thread right?, so if the web server dont respond you see lag?
It sends the query off but doesn't stop and wait for the response. Once it is sent everything will run as normal, but when it finishes receiving the data it will display the image.

@All: You're all welcome!
#9
11/08/2009 (5:53 pm)
Hi Phillip - Can you add a url link in this also?
#10
11/09/2009 (12:22 am)
How do you mean Julian? Click on the control and it opens up a URL?
#11
11/10/2009 (4:19 am)
Yes, that's what I meant.. sorry was in web developer mode :)

I guess you could add the URL in the area shown above?

1. void MyClass::MyResponseMethod( GFXTexHandle &pImageObject )
2. {
3. // Do Stuff.
4. }

I'd like to make it dynamic so that it's called from a php script, the image and the URL. This way I can change the image and the link.
#12
11/10/2009 (1:37 pm)
As far as the mouse click goes, all you would need to do is add a mouseclick callback which calls the function:
// C++
Platform::openWebBrowser( "http://www.google.com" );

// Torque Script
gotoWebPage( "http://www.google.com" );
Is that the kind of thing you were looking for?

Loading the image from a php script would be a little bit tricky when creating the texture as it uses the image URL's extension to examine what type of image it is. The solution to this problem would be to examine the response header to see what the document type is.
#13
11/10/2009 (10:18 pm)
Just the job! thanks Phillip.
#14
11/13/2009 (6:27 am)
Yes trying to get this to work, with TGE.
I don't have a AL folder nor a platformNet.h
Am I overlooking something, did I install this wrong.
Thanks