DIRS += config/
DIRS += input/
DIRS += graphics/
+DIRS += locks/
SRCS := # := start
include $(addsuffix /files.mk,${DIRS})
using std::cout;
using std::endl;
-#include <SDL/SDL.h>
+#include "locks/Mutex.h"
+#include "locks/Autolock.h"
/// ***** Public Methods *****
-SDL_mutex* muDPF = NULL;
+Mutex muDPF;
void DPF(int level, const char* pstr)
{
- // lock
- SDL_mutexP( muDPF );
+ Autolock lock(muDPF);
cout << pstr << endl;
-
- //unlock
- SDL_mutexV( muDPF );
}
void debug::init()
{
- muDPF = SDL_CreateMutex();
+ muDPF.init();
}
void debug::clean()
{
- SDL_DestroyMutex( muDPF );
- muDPF = NULL;
+ muDPF.clean();
}
/// ***** Private Methods *****
ball = addBall(Vector2(150, 150), 20, cCyan, startBalls);
ball->mass = startMass;
- for( int i = 0; i<100; i++)
+ for( int i = 0; i<50; i++)
{
addBall(Vector2(200+i*2, 200+i*2), 10, cCyan);
}
#include <set>
#include <SDL/SDL.h>
+#include "locks/Mutex.h"
+#include "locks/Autolock.h"
+
#include "Entities/Entity.h"
#include "Entities/Particle.h"
#include "Entities/PhysicsEntity.h"
setPhys active_Physics;
setPhys physics_To_Remove;
-SDL_mutex* particleSetLock = NULL;
-SDL_mutex* physicsEntitySetLock = NULL;
+Mutex particleSetLock;
+Mutex physicsEntitySetLock;
/// ***** Initializers/Cleaners *****
void manager::init()
{
- particleSetLock = SDL_CreateMutex();
- physicsEntitySetLock = SDL_CreateMutex();
+ particleSetLock.init();
+ physicsEntitySetLock.init();
collision::init();
}
{
collision::clean();
- SDL_DestroyMutex(particleSetLock);
- particleSetLock = NULL;
-
- SDL_DestroyMutex(physicsEntitySetLock);
- physicsEntitySetLock = NULL;
+ physicsEntitySetLock.clean();
+ particleSetLock.clean();
}
/// ***** Public Methods *****
}
void manager::draw()
{
- SDL_mutexP( particleSetLock );
+ {
+ Autolock lock( particleSetLock );
+ DPF(0, "Particle Draw Start");
+
// update active Particle*s
for( setPart::iterator it = active_Particles.begin();
it != active_Particles.end();
{
(*it)->draw();
}
- SDL_mutexV( particleSetLock );
- SDL_mutexP( physicsEntitySetLock );
+ DPF(0, "Particle Draw End");
+ }
+
+ {
+ Autolock lock( physicsEntitySetLock );
+ DPF(0, "Physics Draw Start");
+
// update active PhysicsEntity*s
for( setPhys::iterator it = active_Physics.begin();
it != active_Physics.end();
{
(*it)->draw();
}
- SDL_mutexV( physicsEntitySetLock );
+
+ DPF(0, "Physics Draw End");
+ }
}
/// ***** Private Methods *****
void updateParticles(float time_step)
{
- SDL_mutexP( particleSetLock );
+ {
+ Autolock lock( particleSetLock );
+
// add new Particle*s to Active
for( setPart::iterator it = particles_To_Add.begin();
it != particles_To_Add.end();
it++ )
{
+ DPF(0, "Particle Insert");
active_Particles.insert(*it);
}
- particles_To_Add.clear();
// remove dead Particle*s from Active
for( setPart::iterator it = particles_To_Remove.begin();
it != particles_To_Remove.end();
it++ )
{
+ DPF(0, "Particle Delete");
active_Particles.erase(*it);
}
- particles_To_Remove.clear();
- SDL_mutexV( particleSetLock );
+ }
+
+ particles_To_Add.clear();
+ particles_To_Remove.clear();
// update active Particle*s
for( setPart::iterator it = active_Particles.begin();
}
void updatePhysics(float time_step)
{
- SDL_mutexP( physicsEntitySetLock );
+ {
+ Autolock lock( physicsEntitySetLock );
+
// add new PhysicsEntity*s to Active
for( setPhys::iterator it = physics_To_Add.begin();
it != physics_To_Add.end();
it++ )
{
+ DPF(0, "Physics Insert");
active_Physics.insert(*it);
}
- physics_To_Add.clear();
// remove dead PhysicsEntity*s from Active
for( setPhys::iterator it = physics_To_Remove.begin();
it != physics_To_Remove.end();
it++ )
{
+ DPF(0, "Physics Delete");
active_Physics.erase(*it);
}
- physics_To_Remove.clear();
- SDL_mutexV( physicsEntitySetLock );
+ }
+
+ physics_To_Remove.clear();
+ physics_To_Add.clear();
// apply collision math
collision::update(active_Physics);
--- /dev/null
+/*
+ * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ * 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 3 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "Autolock.h"
+
+#include "Mutex.h"
+
+/// ***** Constructors/Destructors *****
+Autolock::Autolock(Mutex& mu)
+ : m_mu(mu)
+{
+ Lock();
+}
+
+Autolock::~Autolock()
+{
+ Unlock();
+}
+
+void Autolock::Lock()
+{
+ m_mu.Lock();
+}
+
+void Autolock::Unlock()
+{
+ m_mu.Unlock();
+}
--- /dev/null
+/*
+ * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ * 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 3 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef AUTOLOCK_H
+#define AUTOLOCK_H
+
+class Mutex;
+
+/// ***** Header Class *****
+class Autolock
+{
+public:
+ Autolock(Mutex& mu);
+ ~Autolock();
+
+ void Lock();
+ void Unlock();
+
+// hide copying methods!
+private:
+ Autolock(const Autolock&);
+ const Autolock& operator ==(const Autolock&);
+
+private:
+ Mutex& m_mu;
+};
+
+/// ***** Header Methods *****
+
+#endif // AUTOLOCK_H
--- /dev/null
+/*
+ * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ * 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 3 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "Mutex.h"
+
+#include <SDL/SDL.h>
+
+Mutex::Mutex()
+ : m_pSDL_mutex(NULL)
+{
+
+}
+Mutex::~Mutex()
+{
+
+}
+
+void Mutex::init()
+{
+ m_pSDL_mutex = SDL_CreateMutex();
+}
+void Mutex::clean()
+{
+ SDL_DestroyMutex(m_pSDL_mutex);
+ m_pSDL_mutex = NULL;
+}
+
+bool Mutex::IsValid()
+{
+ return NULL != m_pSDL_mutex;
+}
+
+void Mutex::Lock()
+{
+ if(IsValid())
+ SDL_mutexP(m_pSDL_mutex);
+}
+void Mutex::Unlock()
+{
+ if(IsValid())
+ SDL_mutexV(m_pSDL_mutex);
+}
--- /dev/null
+/*
+ * Copyright (C) 2008 Patrik Gornicz, Gornicz_P (at) hotmail (dot) com.
+ *
+ * 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 3 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef MUTEX_H
+#define MUTEX_H
+
+/// ***** Header Class *****
+
+class SDL_mutex;
+
+class Mutex
+{
+public:
+ Mutex();
+ ~Mutex();
+
+ void init();
+ void clean();
+
+ bool IsValid();
+
+ void Lock();
+ void Unlock();
+
+// hide copying methods!
+private:
+ Mutex(const Mutex&);
+ const Mutex& operator ==(const Mutex&);
+
+private:
+ SDL_mutex* m_pSDL_mutex;
+};
+
+#endif // MUTEX_H
--- /dev/null
+CURDIR := locks/
+FILES := # insure blank
+
+FILES += Autolock.cpp
+FILES += Mutex.cpp
+
+FILES := $(addprefix ${CURDIR},${FILES})
+
+SRCS += ${FILES}