Ok in this tutorial we will write a class that will draw objects to the screen, this will be useful when we want to start creating entities or GUI objects.
So lets get started...
First create 2 new files, Sprite.h and Sprite.cpp
Open up Sprite.h and lets get coding
#ifndef _SPRITE_H_
#define _SPRITE_H_
#include <SDL.h>
class Sprite
{
public:
Sprite();
static SDL_Surface* Load(char* pFile);
static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y);
static bool Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2,
int y2, int width, int height);
};
#endif
So thats the Sprite.h file, its quite basic for the moment but we will extend it and improve upon it over time, but for now this will do nicely. Lets write the functions themselves now, open up the Sprite.cpp
#include "Sprite.h"
// constructor
Sprite::Sprite()
{
}
here we will load the file needed to draw the surface, this will be the filename of a compatible BMP, we create 2 pointers to SDL surfaces and set them to NULL. These surfaces are used to store the data of the created surface, one is used to load the file onto, and the other is used to optimize the surface, we then return the optimized surface and free the unused temp surface
SDL_Surface* Sprite::Load(char* File)
{
SDL_Surface* temp = NULL;
SDL_Surface* optimized = NULL;
if((temp = SDL_LoadBMP(File)) == NULL)
{
return NULL;
}
optimized = SDL_DisplayFormatAlpha(temp);
SDL_FreeSurface(temp);
return optimized;
}
now we need to have a function that blits the BMP to an SDL surface, we pass in the screen and the name of the surface we loaded our BMP onto, we also pass in where we want to draw it. We use the x and y values to create an SDL_Rect, we then blit to this using SDL_Blitsurface();
bool Sprite::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y)
{
if(dest == NULL || src == NULL)
{
return false;
}
SDL_Rect destR;
destR.x = x;
destR.y = y;
SDL_BlitSurface(src, NULL, dest, &destR);
return true;
}
this next function only draws part of the bitmap, this is especially useful when using sprite sheets as you will find out in later tutorials, we pass in the values for an SDL_Rect again like last time, but we now will create another rectangle this one will be used to tell which part of the image to draw, so if we where to pass in x2 = 0, y2 = 0, w = 50, h = 50 , it would draw the top corner of the image, a 50 x 50 square.
bool Sprite::Draw(SDL_Surface* dest, SDL_Surface* src, int x, int y, int x2, int y2, int width, int height) {
if(dest == NULL || src == NULL) {
return false;
}
SDL_Rect destR;
destR.x = x;
destR.y = x;
SDL_Rect srcR;
srcR.x = x2;
srcR.y = y2;
srcR.w = width;
srcR.h = height;
SDL_BlitSurface(src, &srcR, dest, &destR);
return true;
}
Ok so thats our sprite class, we can use this to draw any image to the screen and also draw only the parts of the image that we want. lets test these functions out inside our game loop.
Open up the Game.h file and add this code
// we need to include the "Sprite.h" file that we just created #include "Sprite.h" private: // add a surface to test our functions. SDL_Surface* testSprite;
now open up the Game.cpp file and add some more code
// inside the Init function add this code
testSprite = NULL;
testSprite = Sprite::Load("test.bmp");
// now inside the Draw function add this code
Sprite::Draw(m_pScreen, testSprite, 0, 0);
Ok now build and run, you should see the image in the top left corner of the window, pretty easy huh
ok now we need to test our other function, we can use the same image we loaded last time so in Game.cpp file
// inside our Draw function Sprite::Draw(m_pScreen, testSprite, 300, 300, 0, 0, 50, 50);
Now build and run, you should see a small 50x50 square of our original image.
So to recap we created a sprite class, I call it a sprite class but if it makes more sense you could call it a drawing class or whatever your prefer. This class blits images to the screen, we also created a function to only draw specific parts of an image.
One thing we need to do after all that is to free our test sprite surface so as to avoid memory leaks, we can use our Clean function in Game.cpp to add this code
SDL_FreeSurface(testSprite);
Ok now we are done, next time we will incorporate some transparency and also an external library to use images other than BMP's.
Heres the image I used
test.bmp (144.05K)
Number of downloads: 975
Happy Coding.





MultiQuote








|