From 292223f3e65a2a92050f3f8469362426602846dd Mon Sep 17 00:00:00 2001 From: Patrik Gornicz Date: Mon, 27 Apr 2009 15:58:17 -0400 Subject: [PATCH] added more files to library so it can compile --- Makefile | 4 +-- src/debug.cpp | 56 +++++++++++++++++++++++++++++++++++++++ src/debug.h | 40 ++++++++++++++++++++++++++++ src/dir.mk | 4 +++ src/locks/Autolock.cpp | 42 +++++++++++++++++++++++++++++ src/locks/Autolock.h | 45 ++++++++++++++++++++++++++++++++ src/locks/Mutex.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++ src/locks/Mutex.h | 50 +++++++++++++++++++++++++++++++++++ src/locks/dir.mk | 19 ++++++++++++++ src/mathw.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/mathw.h | 54 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 444 insertions(+), 2 deletions(-) create mode 100644 src/debug.cpp create mode 100644 src/debug.h create mode 100644 src/locks/Autolock.cpp create mode 100644 src/locks/Autolock.h create mode 100644 src/locks/Mutex.cpp create mode 100644 src/locks/Mutex.h create mode 100644 src/locks/dir.mk create mode 100644 src/mathw.cpp create mode 100644 src/mathw.h diff --git a/Makefile b/Makefile index 073ff04..00be4b2 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ include ${SRCSDIR}${DIRMK} WORKINGDIR := bind/ -TARGETNAME := pglib.so +TARGETNAME := libpg.so TARGETTMP := ${OBJSDIR}${TARGETNAME} TARGET := ${WORKINGDIR}${TARGETNAME} @@ -66,7 +66,7 @@ all: ${TARGET} # how to link the main target ${TARGETTMP}: ${OBJS} ${Q1}${PRNTFMT} "${CXX}" "$@" - ${Q2}${CXX} ${CXXFLAGS} -o $@ $^ ${LIBS} + ${Q2}${CXX} -shared ${CXXFLAGS} -o $@ $^ ${LIBS} # rule to copy tmp target to working directory ${TARGET}: ${TARGETTMP} | ${WORKINGDIR} diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..4e659d2 --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,56 @@ +/* + * 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 . + */ + +#include "debug.h" + +#include +using std::cerr; +using std::cout; +using std::endl; + +#include + +#include "locks/Mutex.h" +#include "locks/Autolock.h" + +/// ***** Public Methods ***** + +Mutex muDPF; + +void DPF(int level, const char* pstr) +{ + Autolock lock(muDPF); + + cout << pstr << endl; +} + +void debug::init() +{ + muDPF.init(); +} + +void debug::clean() +{ + muDPF.clean(); +} + +void DASSERT(bool fBreak) +{ + assert(fBreak); +} + +/// ***** Private Methods ***** diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..6085003 --- /dev/null +++ b/src/debug.h @@ -0,0 +1,40 @@ +/* + * 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 . + */ + +#ifndef DEBUG_H +#define DEBUG_H + +#include +using std::cout; +using std::cerr; +using std::endl; + +void DPF(int level, const char* pstr); + +namespace debug +{ + void init(); + void clean(); +} + +void DASSERT(bool fBreak); + +// comment out when not debugging +#define DEBUGGING + + +#endif // DEBUG_H diff --git a/src/dir.mk b/src/dir.mk index db28ab3..55ab192 100644 --- a/src/dir.mk +++ b/src/dir.mk @@ -1,9 +1,13 @@ NEWSRCS := # insure blank NEWSRCS += Vector2.cpp +NEWSRCS += mathw.cpp +NEWSRCS += debug.cpp NEWDIRS := # insure blank +NEWDIRS += locks/ + # Post dir setup CURDIR := diff --git a/src/locks/Autolock.cpp b/src/locks/Autolock.cpp new file mode 100644 index 0000000..3601bdf --- /dev/null +++ b/src/locks/Autolock.cpp @@ -0,0 +1,42 @@ +/* + * 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 . + */ + +#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(); +} diff --git a/src/locks/Autolock.h b/src/locks/Autolock.h new file mode 100644 index 0000000..de0107c --- /dev/null +++ b/src/locks/Autolock.h @@ -0,0 +1,45 @@ +/* + * 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 . + */ + + +#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 diff --git a/src/locks/Mutex.cpp b/src/locks/Mutex.cpp new file mode 100644 index 0000000..e4004c6 --- /dev/null +++ b/src/locks/Mutex.cpp @@ -0,0 +1,61 @@ +/* + * 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 . + */ + +#include "Mutex.h" +#include "debug.h" + +#include + +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() +{ + DASSERT(IsValid()); + + SDL_mutexP(m_pSDL_mutex); + m_uiThreadID = SDL_ThreadID(); +} +void Mutex::Unlock() +{ + DASSERT(IsValid()); + + DASSERT(m_uiThreadID == SDL_ThreadID()); + SDL_mutexV(m_pSDL_mutex); +} diff --git a/src/locks/Mutex.h b/src/locks/Mutex.h new file mode 100644 index 0000000..cac4d94 --- /dev/null +++ b/src/locks/Mutex.h @@ -0,0 +1,50 @@ +/* + * 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 . + */ + + +#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; + unsigned int m_uiThreadID; +}; + +#endif // MUTEX_H diff --git a/src/locks/dir.mk b/src/locks/dir.mk new file mode 100644 index 0000000..f7e8b9e --- /dev/null +++ b/src/locks/dir.mk @@ -0,0 +1,19 @@ +NEWSRCS := # insure blank + +NEWSRCS += Autolock.cpp +NEWSRCS += Mutex.cpp + + +# Post dir setup + +CURDIR := locks/ + +NEWSRCS := $(addprefix ${CURDIR},${NEWSRCS}) +NEWOBJS := ${NEWSRCS:.cpp=.o} +NEWDEPS := ${NEWSRCS:.cpp=.d} + +# Append to lists + +SRCS += ${NEWSRCS} +OBJS += $(addprefix ${OBJSDIR},${NEWOBJS}) +DEPS += $(addprefix ${DEPSDIR},${NEWDEPS}) diff --git a/src/mathw.cpp b/src/mathw.cpp new file mode 100644 index 0000000..2b71dec --- /dev/null +++ b/src/mathw.cpp @@ -0,0 +1,71 @@ +/* + * 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 . + */ + +#include "mathw.h" + + +/// ***** Public Methods ***** + +int mod(int x, int y) +{ + return x % y + (x < 0 ? y : 0); +} + +// Vector2 Math + +Vector2 vectorToLine +( + const Vector2& vec, + float x1, + float y1, + float x2, + float y2 +) +{ + float lineSize = (float) sqrt((x1 - x2) * (x1 - x2) + + (y1 - y2) * (y1 - y2)); + if (lineSize == 0) + return Vector2(x1 - vec.m_fX, y1 - vec.m_fY); + + float u = ((vec.m_fX - x1) * (x2 - x1) + + (vec.m_fY - y1) * (y2 - y1)) / (lineSize * lineSize); + + if (u < 0) + return Vector2(x1 - vec.m_fX, y1 - vec.m_fY); + else if (u > 1) + return Vector2(x2 - vec.m_fX, y2 - vec.m_fY); + else + { + float ix = x1 + u * (x2 - x1); + float iy = y1 + u * (y2 - y1); + return Vector2(ix - vec.m_fX, iy - vec.m_fY); + } +} + +Vector2 perp(const Vector2& vec) +{ + return Vector2(-vec.m_fY, vec.m_fX); +} + +float dot(const Vector2& vec1, const Vector2& vec2) +{ + return vec1.m_fX * vec2.m_fX + vec1.m_fY * vec2.m_fY; +} + +//TODO float Vector2::projectionCoeff(const Vector2* vec) const; +//TODO Vector2* Vector2::projection(const Vector2* vec) const; + diff --git a/src/mathw.h b/src/mathw.h new file mode 100644 index 0000000..26a92d8 --- /dev/null +++ b/src/mathw.h @@ -0,0 +1,54 @@ +/* + * 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 . + */ + +#ifndef MATHW_H +#define MATHW_H + +#include +#include "Vector2.h" + + +/// ***** Public Variables ***** + +static const float PI = 3.1415926535897; + +/// ***** Header Methods ***** + +int mod(int, int); + +/// Vector2 Math + +Vector2 vectorToLine +( + const Vector2& vec, + float x1, + float y1, + float x2, + float y2 +); + +//Vector2 lineIntersection(Vector2&, Vector2&, Vector2&, Vector2&) const; + +//void Rotate(float rads); + +float dot(const Vector2&, const Vector2&); +Vector2 perp(const Vector2&); + +//TODO float projectionCoeff(const Vector2&, const Vector2&) const; +//TODO void projection(const Vector2&, const Vector2&); + +#endif // MATHW_H -- 2.10.2