Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Switching in between backgrounds  (Read 1375 times)

0 Members and 1 Guest are viewing this topic.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Switching in between backgrounds
« on: January 06, 2013, 03:11:25 pm »
Hi, I'm trying to make my program that I'm making for school change backgrounds every five seconds. My code compiles just fine, but the program crashes immediately. I've managed to debug it enough to where I know what's causing the problem, but I can't figure out how. Heres my code:

BackgroundManager.hpp:

#include <memory>
#include <iostream>
#include <sstream>

#include "Background.hpp"
#include "GameState.hpp"

class BackgroundManager //manages the initialization and switching of different backgrounds
{
public:
    BackgroundManager() : resourceFilepath("Resources/") {}; //sets resourcefilepath equal to Resources/
    ~BackgroundManager() {};

    void initBackgrounds(); //initializes backgrounds
    void switchBackgrounds(sf::RenderWindow& screen, int state); //function to switch between backgrounds
    static std::unique_ptr<Background> createBackground() { return std::unique_ptr<Background>(new Background); }

private:
    std::unique_ptr<Background> background;

    int randBackgroundNum;

    const std::string& resourceFilepath; //const reference to a string for the resource file path

    std::ostringstream backgroundNum; //a stream used to collect background numbers and initialize them

    sf::Clock clock; //clock for measuring team in between backgrounds
};

BackgroundManager.cpp:

#include "BackgroundManager.hpp"

void BackgroundManager::initBackgrounds()
{
    srand(time(NULL)); //seed random number

    try
    {
         background = createBackground();
         std::cout << "Successfully created a background." << std::endl;
    }

    catch (std::bad_alloc& ba)
    {
        std::cerr << ba.what() << " at background manager" << std::endl;
    }

    catch (...)
    {
        std::cout << "An unknown exception occured." << std::endl;
    }
}

void BackgroundManager::switchBackgrounds(sf::RenderWindow& screen, int state)
{
    switch (state)
    {
        case 0: //Case introstate
            randBackgroundNum = rand() % 5; //get a number 0 - 5
            break;
        case 1: //Case historystate
            randBackgroundNum = rand() % 10 + 6; //get a number 6 - 10
            break;
    }

    backgroundNum << randBackgroundNum; //place the randBackgroundNum in a stream for file usage

    background->init(resourceFilepath + backgroundNum.str() + ".jpg"); //initialize background with given random filepath >>>>>>PROBLEM IS HERE THAT CAUSES CRASH <<<<<<

    clock.restart(); //restart the clock

    while (clock.getElapsedTime().asSeconds() < 5.0f) //while 5 seconds haven't passed, keep drawing current background
        screen.draw(background->getSprite());
}
Current Projects:
Technoport

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
Re: Switching in between backgrounds
« Reply #1 on: January 06, 2013, 05:24:12 pm »
I see a problem with you using rand()
Use this method below..
int rand(int low, int high){return std::rand () % (high - low + 1) + low;}

case 0: //Case introstate
            randBackgroundNum = rand() % 5; //get a number 0 - 5
            break;
        case 1: //Case historystate
            randBackgroundNum = rand() % 10 + 6; //get a number 6 - 10
            break;

So above should be
randBackgroundNum=rand(0, 5);
randBackgroundNum=rand(6, 10);
« Last Edit: January 06, 2013, 05:39:22 pm by iride »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Switching in between backgrounds
« Reply #2 on: January 06, 2013, 06:36:03 pm »
Why are backgroundNum and randBackgroundNum members of BackgroundManager?

Why is createBackground static?

Why are you using an init function instead of initializing things in the constructor?

Why are you using a unique_pointer+dynamic memory instead of an actual Background object?

Why does switchBackground block the entire program for 5 seconds while it repeatedly draws the same sprite to the window (which is probably enough for the O/S to flag your program as unresponsive if you're on Windows?)