File: ParticleGroupManager.cpp

package info (click to toggle)
criticalmass 1%3A1.0.0-1.5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 18,740 kB
  • sloc: ansic: 47,628; cpp: 25,167; sh: 12,025; xml: 3,532; perl: 3,271; makefile: 662; python: 66; awk: 40; lisp: 33
file content (156 lines) | stat: -rw-r--r-- 3,873 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Description:
//   Particle group manager.
//
// Copyright (C) 2001 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation;  either version 2 of the License,  or (at your option) any  later
// version.
//
// This program is distributed in the hope that it will be useful,  but  WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include <Trace.hpp>

#include <ParticleGroup.hpp>
#include <ParticleGroupManager.hpp>
#include <FindHash.hpp>

ParticleGroupManager::ParticleGroupManager( void)
{
    XTRACE();
}

ParticleGroupManager::~ParticleGroupManager()
{
    XTRACE();

    list<LinkedParticleGroup*>::iterator li;
    for( li=_linkedParticleGroupList.begin(); 
         li!=_linkedParticleGroupList.end(); li++)
    {
        delete (*li);
    }
    _linkedParticleGroupList.clear();

    list<ParticleGroup*>::iterator i;
    for( i=_particleGroupList.begin(); i!=_particleGroupList.end(); i++)
    {
	delete (*i);
    }

    ParticleGroup::destroyParticleTypes();

    _particleGroupMap.clear();
    _particleGroupList.clear();
}

bool ParticleGroupManager::init( void)
{
    XTRACE();
    return true;
}

void ParticleGroupManager::reset( void)
{
    XTRACE();

    list<ParticleGroup*>::iterator i;
    for( i=_particleGroupList.begin(); i!=_particleGroupList.end(); i++)
    {
	(*i)->reset();
    }
}

void ParticleGroupManager::draw( void)
{
    XTRACE();

    list<ParticleGroup*>::iterator i;
    for( i=_particleGroupList.begin(); i!=_particleGroupList.end(); i++)
    {
	(*i)->draw();
    }
}

bool ParticleGroupManager::update( void)
{
    XTRACE();

    list<ParticleGroup*>::iterator i;
    for( i=_particleGroupList.begin(); i!=_particleGroupList.end(); i++)
    {
        (*i)->update();
    }

    list<LinkedParticleGroup*>::iterator li;
    for( li=_linkedParticleGroupList.begin(); 
         li!=_linkedParticleGroupList.end(); li++)
    {
	LinkedParticleGroup *lpg = *li;
        //collision detect based on links...
        lpg->group1->detectCollisions( lpg->group2);
    }

    return true;
}

void ParticleGroupManager::addGroup( const string &groupName, int groupSize)
{
    XTRACE();
    ParticleGroup *newGroup = new ParticleGroup( groupName, groupSize);
    newGroup->init();
    _particleGroupMap[ groupName] = newGroup;

    //We keep a list in addition to the hash map in order to have a bit
    //more control over drawing order.
    _particleGroupList.insert( _particleGroupList.end(), newGroup);

    LOG_INFO << "Adding Particle Group [" << groupName << "]" << endl;
}

void ParticleGroupManager::addLink( const string &group1, const string &group2)
{
    XTRACE();

    ParticleGroup *g1 = getParticleGroup( group1);
    ParticleGroup *g2 = getParticleGroup( group2);

    if( !g1 || !g2) return;

    LOG_INFO << "Collision detection between [" 
             << group1 << "] and [" 
             << group2 << "] enabled." 
             << endl;

    LinkedParticleGroup *lpg = new LinkedParticleGroup;
    lpg->group1 = g1;
    lpg->group2 = g2;

    _linkedParticleGroupList.insert( _linkedParticleGroupList.end(), lpg);
}

ParticleGroup *ParticleGroupManager::getParticleGroup( const string &groupName)
{
    XTRACE();
    ParticleGroup * pg = findHash( groupName, _particleGroupMap);
    if( !pg)
    {
        LOG_ERROR << "Unable to find particle group " << groupName << endl;
    }
    return pg;
}

int ParticleGroupManager::getAliveCount( void)
{
    XTRACE();
    int total = 0;
    list<ParticleGroup*>::iterator i;
    for( i=_particleGroupList.begin(); i!=_particleGroupList.end(); i++)
    {
        total += (*i)->getAliveCount();
    }
    return total;
}