Following are the changes mentioned in bold in order to obtain the required result and stop
scrolling in the background.
#include \"SDL/SDL.h\"
#include
//The attributes of the screen can be defined as follows
const int SCN_WIDTH = 640;
const int SCN_HEIGHT = 480;
const int SCN_BPP = 32;
//BPP defines bits per pixel
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
int SpriteFrame = 0;
int FrameCounter = 0;
const int MaxSpriteFrame = 12;
const int FrameDelay = 2;
int BackgroundX = 0;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void FreeFiles();
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y);
void DrawImageFrame(SDL_Surface* image, SDL_Surface* destSurface, int x, int y, int width,
int height, int frame);
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(\"Image Animation\", NULL);
if(!LoadFiles())
{
printf(\"Failed to load all files!\ \");
FreeFiles();
SDL_Quit();
return 0;
}
while(ProgramIsRunning())
{
//Update\'s the sprites frame
FrameCounter++;
if(FrameCounter > FrameDelay)
{
FrameCounter = 0;
SpriteFrame++;
}
if(SpriteFrame > MaxSpriteFrame)
SpriteFrame = 0;
//Background scrolling can be removed from this position
//Render the scene
DrawImage(Background,Backbuffer, BackgroundX, 0);
DrawImage(Background,Backbuffer, BackgroundX+800, 0);
DrawImageFrame(SpriteImage, Backbuffer, 350,250, 150, 120, SpriteFrame);
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)
{
//Here we map the color key
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0xFF, 0xFF);
//Now, set all the pixels of color R 0,G 0*FF,B 0*FF to be transparent
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage(\"graphics/background.bmp\");
if(Background == NULL)
return false;
SpriteImage = LoadImage(\"graphics/bat.bmp\");
//The file should be preloaded and linked with the required libraries in SDL
if(SpriteImage == NULL)
return false;
else
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y)
//A temporary rectangle is used to hold the offsets
{
SDL_Rect destRect;
//Giving the offsets to the rectangle
destRect.x = x;
destRect.y = y;
//Blit the surface
SDL_BlitSurface(image, NULL, destSurface, &destRect);
}
//Here, we need to start the main function:
int main(int argc,char** args)
//Now, initialize all SDL subsystems
if .
The byproduct of sericulture in different industries.pptx
Following are the changes mentioned in bold in order to obtain the r.pdf
1. Following are the changes mentioned in bold in order to obtain the required result and stop
scrolling in the background.
#include "SDL/SDL.h"
#include
//The attributes of the screen can be defined as follows
const int SCN_WIDTH = 640;
const int SCN_HEIGHT = 480;
const int SCN_BPP = 32;
//BPP defines bits per pixel
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
int SpriteFrame = 0;
int FrameCounter = 0;
const int MaxSpriteFrame = 12;
const int FrameDelay = 2;
int BackgroundX = 0;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void FreeFiles();
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y);
void DrawImageFrame(SDL_Surface* image, SDL_Surface* destSurface, int x, int y, int width,
int height, int frame);
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("Image Animation", NULL);
if(!LoadFiles())
{
2. printf("Failed to load all files! ");
FreeFiles();
SDL_Quit();
return 0;
}
while(ProgramIsRunning())
{
//Update's the sprites frame
FrameCounter++;
if(FrameCounter > FrameDelay)
{
FrameCounter = 0;
SpriteFrame++;
}
if(SpriteFrame > MaxSpriteFrame)
SpriteFrame = 0;
//Background scrolling can be removed from this position
//Render the scene
DrawImage(Background,Backbuffer, BackgroundX, 0);
DrawImage(Background,Backbuffer, BackgroundX+800, 0);
DrawImageFrame(SpriteImage, Backbuffer, 350,250, 150, 120, SpriteFrame);
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);
3. SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
//Here we map the color key
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0xFF, 0xFF);
//Now, set all the pixels of color R 0,G 0*FF,B 0*FF to be transparent
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/bat.bmp");
//The file should be preloaded and linked with the required libraries in SDL
if(SpriteImage == NULL)
return false;
else
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y)
//A temporary rectangle is used to hold the offsets
{
SDL_Rect destRect;
//Giving the offsets to the rectangle
destRect.x = x;
destRect.y = y;
//Blit the surface
4. SDL_BlitSurface(image, NULL, destSurface, &destRect);
}
//Here, we need to start the main function:
int main(int argc,char** args)
//Now, initialize all SDL subsystems
if (SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
return 1;
}
//SDL sysystems are things like video,timers,audio etc which are the individual engine
components used to
make a game
void DrawImageFrame(SDL_Surface* image, SDL_Surface* destSurface, int x, int y, int width,
int height, int frame)
{
SDL_Rect destRect;
destRect.x = x;
destRect.y = y;
int columns = image->w/width;
SDL_Rect sourceRect;
sourceRect.y = (frame/columns)*height;
sourceRect.x = (frame%columns)*width;
sourceRect.w = width;
sourceRect.h = height;
SDL_BlitSurface(image, &sourceRect, destSurface, &destRect);
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
running = false;
}
5. return running;
}
Solution
Following are the changes mentioned in bold in order to obtain the required result and stop
scrolling in the background.
#include "SDL/SDL.h"
#include
//The attributes of the screen can be defined as follows
const int SCN_WIDTH = 640;
const int SCN_HEIGHT = 480;
const int SCN_BPP = 32;
//BPP defines bits per pixel
SDL_Surface* Background = NULL;
SDL_Surface* SpriteImage = NULL;
SDL_Surface* Backbuffer = NULL;
int SpriteFrame = 0;
int FrameCounter = 0;
const int MaxSpriteFrame = 12;
const int FrameDelay = 2;
int BackgroundX = 0;
SDL_Surface* LoadImage(char* fileName);
bool LoadFiles();
void FreeFiles();
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y);
void DrawImageFrame(SDL_Surface* image, SDL_Surface* destSurface, int x, int y, int width,
int height, int frame);
bool ProgramIsRunning();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL! ");
return 0;
}
6. Backbuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
SDL_WM_SetCaption("Image Animation", NULL);
if(!LoadFiles())
{
printf("Failed to load all files! ");
FreeFiles();
SDL_Quit();
return 0;
}
while(ProgramIsRunning())
{
//Update's the sprites frame
FrameCounter++;
if(FrameCounter > FrameDelay)
{
FrameCounter = 0;
SpriteFrame++;
}
if(SpriteFrame > MaxSpriteFrame)
SpriteFrame = 0;
//Background scrolling can be removed from this position
//Render the scene
DrawImage(Background,Backbuffer, BackgroundX, 0);
DrawImage(Background,Backbuffer, BackgroundX+800, 0);
DrawImageFrame(SpriteImage, Backbuffer, 350,250, 150, 120, SpriteFrame);
SDL_Delay(20);
SDL_Flip(Backbuffer);
}
FreeFiles();
SDL_Quit();
return 0;
}
SDL_Surface* LoadImage(char* fileName)
{
SDL_Surface* imageLoaded = NULL;
SDL_Surface* processedImage = NULL;
7. imageLoaded = SDL_LoadBMP(fileName);
if(imageLoaded != NULL)
{
processedImage = SDL_DisplayFormat(imageLoaded);
SDL_FreeSurface(imageLoaded);
if(processedImage != NULL)
{
//Here we map the color key
Uint32 colorKey = SDL_MapRGB(processedImage->format, 0, 0xFF, 0xFF);
//Now, set all the pixels of color R 0,G 0*FF,B 0*FF to be transparent
SDL_SetColorKey(processedImage, SDL_SRCCOLORKEY, colorKey);
}
}
return processedImage;
}
bool LoadFiles()
{
Background = LoadImage("graphics/background.bmp");
if(Background == NULL)
return false;
SpriteImage = LoadImage("graphics/bat.bmp");
//The file should be preloaded and linked with the required libraries in SDL
if(SpriteImage == NULL)
return false;
else
return true;
}
void FreeFiles()
{
SDL_FreeSurface(Background);
SDL_FreeSurface(SpriteImage);
}
void DrawImage(SDL_Surface* image, SDL_Surface* destSurface, int x, int y)
//A temporary rectangle is used to hold the offsets
{
SDL_Rect destRect;
8. //Giving the offsets to the rectangle
destRect.x = x;
destRect.y = y;
//Blit the surface
SDL_BlitSurface(image, NULL, destSurface, &destRect);
}
//Here, we need to start the main function:
int main(int argc,char** args)
//Now, initialize all SDL subsystems
if (SDL_Init( SDL_INIT_EVERYTHING ) == -1)
{
return 1;
}
//SDL sysystems are things like video,timers,audio etc which are the individual engine
components used to
make a game
void DrawImageFrame(SDL_Surface* image, SDL_Surface* destSurface, int x, int y, int width,
int height, int frame)
{
SDL_Rect destRect;
destRect.x = x;
destRect.y = y;
int columns = image->w/width;
SDL_Rect sourceRect;
sourceRect.y = (frame/columns)*height;
sourceRect.x = (frame%columns)*width;
sourceRect.w = width;
sourceRect.h = height;
SDL_BlitSurface(image, &sourceRect, destSurface, &destRect);
}
bool ProgramIsRunning()
{
SDL_Event event;
bool running = true;
while(SDL_PollEvent(&event))