Here are the images for the game objects.
I have changed the bonus graphics - since a watering-can with no handle, a waffle on a stick and a bag of poo just does not cut it in these modern times.
Download and save it in your graphics folder. Load it into paint or whatever and save it out as graphics.BMP.(256 colour)
Open a dos prompt in this folder and type :
bin2h graphics.bmp graphics.c
Move the file produced (graphics.c) to your project folder. Add it into your project (project->Add Files->(select the file)->Open).
Now to test if we can display an image from it. This involves changes to game.cpp - replace the contents with the following :
CODE
#include <SDL\SDL.h>
#include <SDL\SDL_rwops.h>
#include "game.h"
#include "buffers.h"
#include "ioBasics.h"
int game_loop(void) {
int status = 0;
// this is the ZX Spectrum style display area
SDL_Rect dstrect = { 32, 24,256,192};
// this the position of the Robbie graphic in the bitmap
SDL_Rect robbie_g = { 0,146, 20, 21};
// This is the screen position for Robbie
SDL_Rect robbie_d = {150, 90, 20, 21};
// initialise graphics
status = init_graphics();
// check everything OK
if(status != 0 ) {
// no graphics, no game - so exit
return status;
}
// copy the background to the screen
SDL_BlitSurface(backdrop, NULL, screen, &dstrect );
// put Robbie on the screen
SDL_BlitSurface(images, &robbie_g, screen, &robbie_d );
// make the screen visible
SDL_Flip(screen);
WaitForKey();
return status;
}
int init_graphics(void) {
char *buffer = (char *)background;
// Load the background image into a surface using RWops
SDL_RWops *rw = SDL_RWFromMem(buffer, background_size);
SDL_Surface *temp = SDL_LoadBMP_RW(rw, 1);
// Were we able to load the bitmap?
if (temp == NULL) {
// return 1 to signal failure
return 1;
}
//Convert the image to optimal display format
backdrop = SDL_DisplayFormat(temp);
// Now set up the game object's images
buffer = (char *)graphics;
// Load the graphic's image into a surface using RWops
rw = SDL_RWFromMem(buffer, graphics_size);
temp = SDL_LoadBMP_RW(rw, 1);
// Were we able to load the bitmap?
if (temp == NULL) {
// return 1 to signal failure
return 1;
}
//Convert the image to optimal display format
images = SDL_DisplayFormat(temp);
//Free the temporary surface
SDL_FreeSurface(temp);
// set transparency to background colour of graphics bmp (pink)
SDL_SetColorKey( images, SDL_SRCCOLORKEY, SDL_MapRGB(images->format, 255, 102, 255) );
// return zero to signal success
return 0;
}
The initialisation of the graphics is now in its own function and handling the new bitmap has been added. Notice the selection of a transparent colour - otherwise all the objects would have a fetching pink rectangle behind them. Just to check we can access the new image, a temporary addition is the displaying of the robot graphic.
We need a new file called game.h into which we place the following :
CODE
extern unsigned int background_size; // the size of the background image generated by bin2h.exe (background.c)
extern unsigned int graphics_size; // the size of the graphics image generated by bin2h.exe (graphics.c)
int game_loop(void); // game control loop (game.cpp)
int init_graphics(void); // initialise graphics function (game.cpp)
'extern' tells the compiler that whatever follows it exists somewhere. In this case the sizes of the graphics that bin2h.exe generated for us. We can now use these labels instead of plain numbers. That way we will know when we look at it next week what the hell the value represents.
Also there are the prototypes of the two functions we have in game.cpp so far. The compile would fail when it reached the call to init_graphics() in game_loop() even though it is in the same file. So we have to provide the compiler with a prototype so it knows the call is structured correctly.
We also need to add a place to record where are new graphics are: Add the following to buffers.cpp ...
CODE
SDL_Surface *images = NULL; // tells us where the object graphics are
... and in buffers.h replace the contents with :
CODE
extern SDL_Joystick *joystick; // reference to the joystick device
extern SDL_Surface *screen; // tells us where the screen is
extern SDL_Surface *backdrop; // tells us where the background image is
extern SDL_Surface *images; // tells us where the object graphics are
extern const unsigned char graphics[]; // this is the graphic data for the game objects (graphics.c)
extern const unsigned char background[]; // this is the graphic data for the background image (background.c)
Hopefully it will still build and Robbie will be displayed near the centre of the screen. Press any key to exit.