Since there are no more lab sessions, your best options for asking questions are, in my order of preference:
1) Ask me in the Friday afternoon lecture
2) Ask me after one of the other lectures
4) Email me. Please put G52CPP in the subject so that my filter lets me find it easily to make sure that I respond.
If I get emailed questions which are important and of general use I may add them to this page as well, so that they are available in future years. If so then I will anonymise them first.
The only ones which were important to everybody I actually sent out to everyone as emails.
Will I lose marks if I use somebody else's images rather than drawing my own?
No. I'm interested in the C++ code, whether you can understand and modify existing code and whether you can write you own. I am not interested in whether you can draw. However, please ensure that you have permission to use images and that there are no copyright issues.
My old image doesn't get deleted when my object moves.
If you copied this code from the bouncing ball sample:
im.RenderImageWithMask( this->GetBackground(), 0, 0, i*100, j*100 + 300, im.GetWidth(), im.GetHeight() );
Then you need to change the GetBackground() to GetForeground() to draw to the foreground (where moving objects are drawn) rather than the background.
Note: You only need to change this if drawing a moving object. Background images really should go on the background.
Can I use the tile manager to draw images?
Yes. You will probably need to do the following:
1) Sub-class the tilemanager class to create your own tile manager. See Demo2TileManager for an example.
2) Add members for each image you want to use - or an array of image pointers maybe?
3) Change the constructor to load the images, so that they are loaded once, at the start, rather than every time.
4) Create a DrawTileAt function to draw your images, rather than, for example, the rectangles that are drawn in Demo2TileManager. Note that there is an example of code to draw an image in the BouncingBall sample (see the question above).
5) Add a member of your tile manager class to the main class, so there is an instance to use. See demos 2, 3 or 4.
6) Remember to free/delete all resources (objects and images) before the program ends.
Can I use a different font for drawing different pieces of text?
You can use code such as:
Font* pMyFont = m_oFontManager.GetFont( szFontName, iFontSize );
to load a font. It will look for the font name and create a font of the specified size.
If it loaded the font in the past and has the same size it will use the same object.
That is how the main font is loaded (see BaseEngine::Initialise() ).
Note that m_oFontManager is protected, so is available to sub-classes of the BaseEngine class - i.e. your main class.
You can store the pointer which is returned from GetFont to avoid having to call GetFont multiple times. If you call it multiple times it has to search its font list each time to see if you loaded it previously, so it is more efficient to keep your own copy of the pointer.
You do not need to free the fonts - you didn't use new so you should not use delete. Just let the font manager delete them for you at the end. In the meantime it will keep the objects in case you ask for them again.
When you call DrawString you give it the font object to use. I.e. the one returned from GetFont().
If you don't specify a font then it uses NULL, and just uses the default font, which was initially loaded.
I need to format a number (e.g. score) as a string. How?
See the following example of an implementation of the DrawStrings() function:
// Build the string to print
char buf[128];
sprintf( buf, "Changing text %6d %6d", rand(), rand() );
// Clear the top of the screen, since we about to draw text on it.
CopyBackgroundPixels( 0, 0, GetScreenWidth(), 35 );
// Then draw the strings
DrawScreenString( 150, 10, buf, 0xffffff, NULL );
// And mark that area of the screen as having been changed, so it gets redrawn
SetNextUpdateRect( 0/*X*/, 0/*Y*/, GetScreenWidth(), 35/*Height*/ );
Note the use of sprintf to do a printf into a char array rather than output it.
Alternatively you could use the stringstream class (see lecture 17) if you wanted to do it using C++ classes rather than C-style functions. For coursework marking purposes it makes no
difference which you use.