0

I am creating a program that reads in data about fireworks from an XML file (such as type, colour, time in milliseconds it launches and how long it lasts for etc). This then displays the fireworks display using openGL in a loop.

My approach to this is to have a manager class, that takes in a vector of the xml read fireworks and moves them between 3 different vectors: inactiveFireworks (those that are yet to fire), activeFireworks (those that need to be animated) and depletedFireworks (those that will be pushed back to inactiveFireworks when the display is run).

To check to see if a firework needs to be fired, I need to work out the difference between the time the firework manager was called and the current time of the program and multiply that by 1000 to get milliseconds.

eg: if a firework is due to fire at 3000, it will be called 3 seconds in to the existence of the fire work manager.

Unfortunately, I get an unhandled exception error at this point and I'm not sure why...

timeDifference = difftime(time(&currentTime),initTime) * 1000;

here is the header file:

#ifndef FWORKMANAGER_H
#define FWORKMANAGER_H

#include <Time.h>
#include <vector>
#include "firework.h"

class fworkManager
{
private:
    time_t initTime;
    time_t currentTime;
    double timeDifference;

    std::vector<firework*> inactiveFireworks; 
    std::vector<firework*> activeFireworks; 
    std::vector<firework*> depletedFireworks; 

public:
    fworkManager(std::vector<firework*> fListIn);
    void drawAllFireworks();

    void evolve();
    void fireInactiveFireworks();
    void moveActiveFireworks();
    void moveDepletedFireworks();
    void reset();   
};

#endif

And here is the CPP.

#include <vector>
#include "LUtil.h"
#include "fworkManager.h"

fworkManager :: fworkManager(std::vector<firework*> fListIn){
    inactiveFireworks = fListIn;
    time (&initTime);
}

//animates fireworks
void fworkManager::evolve(){
    //check time against inactiveFireworks

    fireInactiveFireworks();
    moveActiveFireworks();
    moveDepletedFireworks();
    reset();
}

//draws fireworks as they come up
void fworkManager::drawAllFireworks()
{
    std::vector<firework*>::iterator i;

    for(i=activeFireworks.begin(); i != activeFireworks.end(); i ++)
    {
        (*i) -> draw();
    }
}

//if fireworks are ready to fire, push them to active list
void fworkManager::fireInactiveFireworks()
{
    timeDifference = difftime(time(&currentTime),initTime) * 1000;
    std::vector<firework*>::iterator i;

    for(i = inactiveFireworks.begin(); i != inactiveFireworks.end();)
    {
        if((*i) -> getBegin() <= timeDifference)
        {
            activeFireworks.push_back(*i);
            (*i) -> explode();
            i = inactiveFireworks.erase(i);
        }else{
            ++i;
        }
    }
}

//animate each firework in the active list
void fworkManager::moveActiveFireworks()
{
    std::vector<firework*>::iterator i;
        for(i = activeFireworks.begin(); i != activeFireworks.end(); i++)
    {
        (*i) -> evolve();
    }
}

//move fireworks that have met their duration requirement to the depleted list.
void fworkManager::moveDepletedFireworks()
{
        std::vector<firework*>::iterator i;
    for(i = activeFireworks.begin(); i != activeFireworks.end();)
    {
        if((*i) -> getLifeSpan() >= (*i) -> getDuration()  )
        {
            depletedFireworks.push_back(*i);

            i = activeFireworks.erase(i);
        }else{
            ++i;
        }
    }
}

//repopulates the inactive firework list and resets the time difference. Allows animation to loop.
void fworkManager::reset()
{
    if(inactiveFireworks.empty() && activeFireworks.empty())
    {
        time (&initTime);
        std::vector<firework*>::iterator i;
        for(i=depletedFireworks.begin(); i != depletedFireworks.end();)
        {
            (*i) -> reset();
            inactiveFireworks.push_back(*i);
            i = depletedFireworks.erase(i);

        }

    }
}

Many Thanks for any insight offered.

Jonas
  • 6,915
  • 8
  • 35
  • 53
  • "I get an unhandled exception error at this point" Any information about the exception? Name, error message...? "and I'm not sure why" => You could start by using a debugger... Splitting the expression into temporary variables may help: `time_t timeRes = time(&currentTime); double difftimeRes = difftime(timeRes,initTime); timeDifference = difftimeRes * 1000;` (with a line break after each `;`) then step each line inside the debugger and inpect the variables (and hopefully find the exact operation that causes the exception)... – gx_ Jun 20 '13 at 10:21
  • Many thanks for your swift reply. I have tried your suggestion and the error occurs during double difftimeRes = difftime(timeRes,iniTime); The program breaks down using time64.c at the line if(timeptr) *timeptr =tim. The exception error is: Unhandled exception at 0x5320F511 (msvcr110d.dll) in OGL.exe: 0xC0000005: Access violation writing location 0x00000008. – Ryan R Dizzle Graham Jun 20 '13 at 10:29
  • I suggest you inspect the value of `this` inside the debugger, it's likely to be a null pointer – gx_ Jun 20 '13 at 10:33
  • (can't edit) A "null `this`" can happen for example with `fworkManager* fm = NULL; fm->fireInactiveFireworks();` (not always that obvious...) – gx_ Jun 20 '13 at 10:41
  • Thank you for your help! It allowed me to track down the problem (I hadn't initialised my fworkManager properly. Consider it answered :) – Ryan R Dizzle Graham Jun 27 '13 at 08:21

0 Answers0