I have been trying to solve this problem but I have not been able to find the solution yet.The problem is the background is moving. It
is not supposed to.I am trying to stop the background from scrolling
and only have the bat move from right to left. When the bat reaches
the edge, it needs to go back to its original position. I tried
changing the background code, but then it messes up the bat. It stops
moving.
//Rectangles
SDL_Rect rectBackground; //Background rectangle
SDL_Rect rectDemonPlacement; //Where to place demon on game screen
SDL_Rect rectDemonSheet; //Rectangle for sheet of demons
SDL_Rect rectDemon; //Rectangle for one demon
int SpriteFrame = 0;
int FrameCounter = 0;
int BackgroundX = 0;
int width = 32; //width of one frame
int height = 32; //height of one frame
const int MaxSpriteFrame = 12; //number of frames
const int FrameDelay = 2;
//Function Prototypes
bool ProgramIsRunning();
void FillRect(SDL_Rect &rect, int x, int y, int width, int height);
SDL_Surface* loadImage(std::string path);
SDL_Texture* loadTexture(SDL_Surface* surface);
SDL_Surface* setTransparentColor (SDL_Surface* surface, Uint8 r, Uint8 g, Uint8 b);
void CloseShop();
int main(int argc, char* args[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
printf("Failed to initialize SDL!n");
return 0;
}
//Create the game window
gameWindow = SDL_CreateWindow(
"Animation with Frames Demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
//Create a renderer for the window
renderer = SDL_CreateRenderer(gameWindow, -1, 0);
//Set up Background
//Load image
tempSurface = loadImage("graphics/background.bmp");
//Create texture
textureBackground = loadTexture(tempSurface);
// Create a rectangle at position 0, 0 for Background
FillRect(rectBackground, 0, 0, 800, 600);
//Set up Demon
//create the surface - load the whole sheet
tempSurface = loadImage("graphics/demon.bmp");
//Set magenta as transparency color
tempSurface = setTransparentColor(tempSurface, 255, 255, 255);
//create the texture for the whole sheet
textureDemonSheet = loadTexture(tempSurface);
//Create the rectangle to hold one demon
//SDL_QueryTexture() method gets the width and height of the sheet of demons
SDL_QueryTexture(textureDemonSheet, NULL, NULL, &rectDemonSheet.w, &rectDemonSheet.h);
//get the width of one frame (one demon) by dividing the width of the sheet by the number of frames
width = rectDemonSheet.w/MaxSpriteFrame;
//get the height of one frame
height = rectDemonSheet.h; //height on one demon is same as height of sheet
//Initialize the rectangle for one demon
FillRect(rectDemon, 0, 0, width, height);
//Create the rectangle on game screen for demon placement
FillRect(rectDemonPlacement, 350, 250, width, height);
FrameCounter++;
if(FrameCounter > FrameDelay)
{
FrameCounter = 0;
SpriteFrame++;
}
if(SpriteFrame > MaxSpriteFrame - 1)
SpriteFrame = 0;
rectDemon.x = SpriteFrame * rectDemon.w;
BackgroundX-=5;
if(BackgroundX <= -800)
BackgroundX = 0;
rectBackground.x = BackgroundX;
SDL_RenderCopy(renderer, textureBackground, NULL, &rectBackground);
rectBackground.x = BackgroundX+800;
SDL_RenderCopy(renderer, textureBackground, NULL, &rectBackground);''
Source: Windows Questions C++