Hello All,
I am having trouble trying to get my background to scroll. I want it to scroll it along the y axis. As you can see in my code, the background should scroll in a downward motion, but it does nothing, but sit still.
Code:
class Game
{
public:
Game();
~Game();
private:
SDL_Surface* Screen;
SDL_Event event;
SDL_Rect Camera;
SDL_Surface* TitleImage;
SDL_Rect TitleRect;
float title_x,title_y,title_w,title_h;
SDL_Surface* GameButtons[2];
SDL_Rect GameButtonsRect[2];
float gamebutton_x[2],gamebutton_y[2],gamebutton_w[2],gamebutton_h[2];
SDL_Surface* LogoImage;
SDL_Rect LogoRect;
float logo_x,logo_y,logo_w,logo_h;
TTF_Font* Font;
bool Game_Running;
bool GameScreens[3];
float cam_x,cam_y,cam_w,cam_h;
SDL_Surface* Backdrop;
SDL_Rect Backdrop_Rect;
float bg_x,bg_y,bg_w,bg_h;
long lastTick;
int iticks;
int itick_counter;
int ifps;
int ifps_counter;
int icurrentfps;
int freq;
int channels;
int samp;
int mouse_x;
int mouse_y;
SDL_Surface* PointImage;
SDL_Rect PointRect;
SDL_Surface* FPSImage;
SDL_Rect FPSRect;
SDL_Surface* LifeImage;
SDL_Rect LifeRect;
float point_x,point_y,point_w,point_h;
float fps_x,fps_y,fps_w,fps_h;
float life_x,life_y,life_w,life_h;
stringstream szPoints;
stringstream szFPS;
stringstream szLives;
};
Code:
#include “main.h”
#include “game.h”
#include “player.h”
#include “powerup.h”
const int MAX_WIDTH = 640;
const int MAX_HEIGHT = 480;
const int MAX_BITS = 24;
const char* szTitle = “4000 A.D. - DarkScar Games”;
SDLKey keyPressed;
SDLKey keyReleased;
Uint8* keyState = SDL_GetKeyState(NULL);
bool keysHeld[323] = {false};
SDL_Color textColor = {255,255,255};
Game::Game()
{
Game_Running = true;
GameScreens[0] = true;
GameScreens[1] = false;
GameScreens[2] = false;
freq = 22050;
channels = 2;
samp = 4096;
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
exit(1);
}
Screen = SDL_SetVideoMode(MAX_WIDTH,MAX_HEIGHT,MAX_BITS,
SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption(szTitle,NULL);
LogoImage = IMG_Load("Logo.png");
logo_x = 0;
logo_y = 0;
logo_w = 640;
logo_h = 480;
LogoRect.x = (int)logo_x;
LogoRect.y = (int)logo_y;
LogoRect.w = (int)logo_w;
LogoRect.h = (int)logo_h;
TitleImage = IMG_Load("Title.png");
title_x = 0;
title_y = 0;
title_w = 640;
title_h = 480;
TitleRect.x = (int)title_x;
TitleRect.y = (int)title_y;
TitleRect.w = (int)title_w;
TitleRect.h = (int)title_h;
Backdrop = IMG_Load("Backdrop.png");
bg_x = 0;
bg_y = 0;
bg_w = 640;
bg_h = 480;
Backdrop_Rect.x = (int)bg_x;
Backdrop_Rect.y = (int)bg_y;
Backdrop_Rect.w = (int)bg_w;
Backdrop_Rect.h = (int)bg_h;
GameButtons[0] = IMG_Load("newgame.png");
gamebutton_x[0] = 250.f;
gamebutton_y[0] = 200.f;
gamebutton_w[0] = 150.f;
gamebutton_h[0] = 20.f;
GameButtonsRect[0].x = (int)gamebutton_x[0];
GameButtonsRect[0].y = (int)gamebutton_y[0];
GameButtonsRect[0].w = (int)gamebutton_w[0];
GameButtonsRect[0].h = (int)gamebutton_h[0];
GameButtons[1] = IMG_Load("exit.png");
gamebutton_x[1] = 250.f;
gamebutton_y[1] = 250.f;
gamebutton_w[1] = 150.f;
gamebutton_h[1] = 20.f;
GameButtonsRect[1].x = (int)gamebutton_x[1];
GameButtonsRect[1].y = (int)gamebutton_y[1];
GameButtonsRect[1].w = (int)gamebutton_w[1];
GameButtonsRect[1].h = (int)gamebutton_h[1];
SDL_SetColorKey(GameButtons[0],SDL_SRCCOLORKEY,SDL_MapRGB(GameButtons[0]->format,255,255,255));
if(TTF_Init() == -1)
{
exit(1);
}
Font = TTF_OpenFont("Arial.ttf",20);
if(Font == NULL)
{
exit(1);
}
if(Mix_OpenAudio(freq,MIX_DEFAULT_FORMAT,channels,samp) == -1)
{
exit(1);
}
Player* gPlayer = new Player();
Powerup* gPowerup = new Powerup();
while(Game_Running)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
Game_Running = false;
}
if(event.type == SDL_KEYDOWN)
{
keyPressed = event.key.keysym.sym;
if(keyPressed == SDLK_ESCAPE)
{
Game_Running = false;
}
if(keyPressed == SDLK_RETURN && GameScreens[0] == true)
{
GameScreens[0] = false;
GameScreens[1] = true;
}
}
if(event.type == SDL_KEYUP)
{
keyReleased = event.key.keysym.sym;
}
if(event.type == SDL_MOUSEMOTION)
{
mouse_x = event.motion.x;
mouse_y = event.motion.y;
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
mouse_x = event.button.x;
mouse_y = event.button.y;
if(mouse_x > GameButtonsRect[0].x && mouse_x < GameButtonsRect[0].x + GameButtonsRect[0].w && mouse_y > GameButtonsRect[0].y && mouse_y < GameButtonsRect[0].y + GameButtonsRect[0].h)
{
GameScreens[1] = false;
GameScreens[2] = true;
}
else if(mouse_x > GameButtonsRect[1].x && mouse_x < GameButtonsRect[1].x + GameButtonsRect[1].w && mouse_y > GameButtonsRect[1].y && mouse_y < GameButtonsRect[1].y + GameButtonsRect[1].h)
{
Game_Running = false;
}
}
}
}
if(SDL_Flip(Screen) == -1)
{
exit(1);
}
//Do Sprite Movement here
if(GameScreens[0])
{
SDL_BlitSurface(LogoImage,NULL,Screen,&LogoRect);
}
if(GameScreens[1])
{
SDL_BlitSurface(TitleImage,NULL,Screen,&TitleRect);
SDL_BlitSurface(GameButtons[0],NULL,Screen,&GameButtonsRect[0]);
SDL_BlitSurface(GameButtons[1],NULL,Screen,&GameButtonsRect[1]);
}
if(GameScreens[2])
{
//here is where the screen is supposed to scroll
bg_y += 1;
SDL_BlitSurface(Backdrop,NULL,Screen,&Backdrop_Rect);
}
}
}
Game::~Game()
{
TTF_CloseFont(Font);
TTF_Quit();
Mix_CloseAudio();
SDL_Quit();
}