#include
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
SDL_Rect SpritePos;
int xPos;
int yPos;
int width=100;
int height=90;
int xDir;
int yDir;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection);
void FreeFiles();
bool ProgramIsRunning();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL! ");
return 0;
}
Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Color Keying", NULL);
if(!LoadFiles())
{
printf("Failed to load files! ");
FreeFiles();
SDL_Quit();
return 0;
}
SpritePos.x = 0;
SpritePos.y = 0;
while(ProgramIsRunning())
{
xPos = SpritePos.x; //get the current x position
yPos = SpritePos.y; //get the current y position
moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap
SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int
width, int height, int& xDirection, int& yDirection)tion
SpritePos.y=yPos; //update y with new position
SDL_BlitSurface(Background, NULL, Backbuffer, NULL);
SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
SDL_Delay(20);
SDL_Flip(Backbuffer);
}
FreeFiles();
SDL_Quit();
return 0;
}
SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;
imageLoaded = SDL_LoadBMP(fileName);
if(imageLoaded != NULL)
{
processedImage = SDL_DisplayFormat(imageLoaded);
SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0);
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/aieplane.bmp");
if(SpriteImage == NULL)
return false;
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection)
{
int x = xPosition;
int y = yPosition;
int dirX = xDirection;
int dirY = yDirection;
x = x + dirX;
//if the bitmap has reached the right side
if(x > 800-width)
{
x = width - 100;
dirX = (-1);
}
if(x= 0)
{
x = 5;
dirX = 1;
}
y = y + dirY;
if(y > 600-height)
{
y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int&
xDirection, int& yDirection)
dirY = (-1);
}
if(y = 0)
y = 5;
dirY = 1;
xPosition = x;
yPosition = y;
xDirection = dirX;
yDirection = dirY;
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
running = false;
}
return running;
}
Solution
#include
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
SDL_Rect SpritePos;
int xPos;
int yPos;
int width=100;
int height=90;
int xDir;
int yDir;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection);
void FreeFiles();
bool ProgramIsRunning();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL! ");
return 0;
}
Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Color Keying", NULL);
if(!LoadFiles())
{
printf("Failed to load files! ");
FreeFiles();
SDL_Quit();
return 0;
}
SpritePos.x = 0;
SpritePos.y = 0;
while(ProgramIsRunning())
{
xPos = SpritePos.x; //get the current x position
yPos = SpritePos.y; //get the current y position
moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap
SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int
width, int height, int& xDirection, int& yDirection)tion
SpritePos.y=yPos; //update y with new position
SDL_BlitSurface(Background, NULL, Backbuffer, NULL);
SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
SDL_Delay(20);
SDL_Flip(Backbuffer);
}
FreeFiles();
SDL_Quit();
return 0;
}
SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;
imageLoaded = SDL_LoadBMP(fileName);
if(imageLoaded != NULL)
{
processedImage = SDL_DisplayFormat(imageLoaded);
SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0);
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/aieplane.bmp");
if(SpriteImage == NULL)
return false;
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int&
yDirection)
{
int x = xPosition;
int y = yPosition;
int dirX = xDirection;
int dirY = yDirection;
x = x + dirX;
//if the bitmap has reached the right side
if(x > 800-width)
{
x = width - 100;
dirX = (-1);
}
if(x= 0)
{
x = 5;
dirX = 1;
}
y = y + dirY;
if(y > 600-height)
{
y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int&
xDirection, int& yDirection)
dirY = (-1);
}
if(y = 0)
y = 5;
dirY = 1;
xPosition = x;
yPosition = y;
xDirection = dirX;
yDirection = dirY;
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
running = false;
}
return running;
}

#include SDLSDL.hSDL_Surface Background = NULL; SDL_Surface.pdf

  • 1.
    #include SDL_Surface* Background =NULL; SDL_Surface* SpriteImage = NULL; SDL_Surface* Backbuffer = NULL; SDL_Rect SpritePos; int xPos; int yPos; int width=100; int height=90; int xDir; int yDir; SDL_Surface* LoadImage(char* fileName); bool LoadFiles(); void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection); void FreeFiles(); bool ProgramIsRunning(); int main(int argc, char* args[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Failed to initialize SDL! "); return 0; } Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); SDL_WM_SetCaption("Color Keying", NULL); if(!LoadFiles()) { printf("Failed to load files! "); FreeFiles(); SDL_Quit(); return 0; } SpritePos.x = 0; SpritePos.y = 0;
  • 2.
    while(ProgramIsRunning()) { xPos = SpritePos.x;//get the current x position yPos = SpritePos.y; //get the current y position moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection)tion SpritePos.y=yPos; //update y with new position SDL_BlitSurface(Background, NULL, Backbuffer, NULL); SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos); SDL_Delay(20); SDL_Flip(Backbuffer); } FreeFiles(); SDL_Quit(); return 0; } SDL_Surface* LoadImage(char* fileName) { SDL_Surface* imageLoaded = NULL; SDL_Surface* processedImage = NULL; imageLoaded = SDL_LoadBMP(fileName); if(imageLoaded != NULL) { processedImage = SDL_DisplayFormat(imageLoaded); SDL_FreeSurface(imageLoaded); if(processedImage != NULL) { Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0); SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey); } } return processedImage; } bool LoadFiles()
  • 3.
    { Background = LoadImage("graphics/background.bmp"); if(Background== NULL) return false; SpriteImage = LoadImage("graphics/aieplane.bmp"); if(SpriteImage == NULL) return false; return true; } void FreeFiles() { SDL_FreeSurface(Background); SDL_FreeSurface(SpriteImage); } void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) { int x = xPosition; int y = yPosition; int dirX = xDirection; int dirY = yDirection; x = x + dirX; //if the bitmap has reached the right side if(x > 800-width) { x = width - 100; dirX = (-1); } if(x= 0) { x = 5; dirX = 1; } y = y + dirY; if(y > 600-height)
  • 4.
    { y = height- 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) dirY = (-1); } if(y = 0) y = 5; dirY = 1; xPosition = x; yPosition = y; xDirection = dirX; yDirection = dirY; } bool ProgramIsRunning() { SDL_Event event; bool running = true; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; } return running; } Solution #include SDL_Surface* Background = NULL; SDL_Surface* SpriteImage = NULL; SDL_Surface* Backbuffer = NULL; SDL_Rect SpritePos; int xPos; int yPos; int width=100; int height=90;
  • 5.
    int xDir; int yDir; SDL_Surface*LoadImage(char* fileName); bool LoadFiles(); void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection); void FreeFiles(); bool ProgramIsRunning(); int main(int argc, char* args[]) { if(SDL_Init(SDL_INIT_EVERYTHING) < 0) { printf("Failed to initialize SDL! "); return 0; } Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE); SDL_WM_SetCaption("Color Keying", NULL); if(!LoadFiles()) { printf("Failed to load files! "); FreeFiles(); SDL_Quit(); return 0; } SpritePos.x = 0; SpritePos.y = 0; while(ProgramIsRunning()) { xPos = SpritePos.x; //get the current x position yPos = SpritePos.y; //get the current y position moveBitMap( xPos, yPos, width, height, xDir, yDir); //move the bitmap SpritePos.x=xPos; //update x with new posivoid moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection)tion SpritePos.y=yPos; //update y with new position SDL_BlitSurface(Background, NULL, Backbuffer, NULL); SDL_BlitSurface(SpriteImage, NULL, Backbuffer, &SpritePos);
  • 6.
    SDL_Delay(20); SDL_Flip(Backbuffer); } FreeFiles(); SDL_Quit(); return 0; } SDL_Surface* LoadImage(char*fileName) { SDL_Surface* imageLoaded = NULL; SDL_Surface* processedImage = NULL; imageLoaded = SDL_LoadBMP(fileName); if(imageLoaded != NULL) { processedImage = SDL_DisplayFormat(imageLoaded); SDL_FreeSurface(imageLoaded); if(processedImage != NULL) { Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0, 0); SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey); } } return processedImage; } bool LoadFiles() { Background = LoadImage("graphics/background.bmp"); if(Background == NULL) return false; SpriteImage = LoadImage("graphics/aieplane.bmp"); if(SpriteImage == NULL) return false; return true; } void FreeFiles()
  • 7.
    { SDL_FreeSurface(Background); SDL_FreeSurface(SpriteImage); } void moveBitMap(int& xPosition,int& yPosition, int width, int height, int& xDirection, int& yDirection) { int x = xPosition; int y = yPosition; int dirX = xDirection; int dirY = yDirection; x = x + dirX; //if the bitmap has reached the right side if(x > 800-width) { x = width - 100; dirX = (-1); } if(x= 0) { x = 5; dirX = 1; } y = y + dirY; if(y > 600-height) { y = height - 90;//void moveBitMap(int& xPosition, int& yPosition, int width, int height, int& xDirection, int& yDirection) dirY = (-1); } if(y = 0) y = 5; dirY = 1; xPosition = x; yPosition = y;
  • 8.
    xDirection = dirX; yDirection= dirY; } bool ProgramIsRunning() { SDL_Event event; bool running = true; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) running = false; } return running; }