So I am currently creating an SDL clone of Galaga, and am trying to render the background to the screen. My main class looks like this:
//While the user hasn't quit
while( currentState != ExitState::get() )
{
//calculate delta time
dt = (SDL_GetTicks() - pastCount) / 1000.0f;
pastCount = SDL_GetTicks();
//Do state event handling
while( SDL_PollEvent( &e ) != 0 )
{
//Handle state events
currentState->handleEvent( e );
//Exit on quit
if( e.type == SDL_QUIT )
{
setNextState( ExitState::get() );
}
}
//Do state logic
b.update();
currentState->update();
//Change state if needed
changeState();
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
//Do state rendering
b.render();
currentState->render();
//Update screen
SDL_RenderPresent( gRenderer );
}
The code above is my main game loop. As you can see I am updating and rendering the background at all times, and rendering other things depending on what gamestate I am in. My background class looks like this.
class Background {
public:
//constructor
Background();
//deconstructor
~Background();
//update the position of the stars
void update();
//render the stars
void render();
private:
//black background
LTexture black;
Star s;
};
And my render function for the background class looks like this:
//render the stars
void Background::render() {
//Render objects
black.render( 0, redPos );
//Star temp = Star(0, 0, 'r');
//temp.render();
s.render();
}
This is where I am experiencing an issue, I can get the black background to render to my screen just fine, But I cannot get the star to render. I initialize the star within the background class and loaded the texture to it. The s.render() call just renders the star to the screen using internal x and y pos associated with the star. What is wierd is that if I create a star within the Background::render() function and render it the code works just fine. This leads me to believe this may be some sort of c++ problem or how I am handling the global renderer. Any direction on this would be much appreciated, below I included my Star header file and render function.
class Star {
public:
//constructor
Star(int x, int y, char color);
//default constructor
Star();
//deconstructor
~Star();
//update the position of the star
void update();
//render the star
void render();
LTexture starTexture;
int xPos, yPos;
private:
//int xPos, yPos;
//LTexture starTexture;
};
and the Star::render() function
void Star::render() {
starTexture.render(xPos, yPos);
}