From: Patrik Gornicz Date: Thu, 1 May 2008 03:04:55 +0000 (-0400) Subject: Creation of physics project X-Git-Tag: physics-premerge~209 X-Git-Url: http://gitweb.pgornicz.com/gitweb.cgi?a=commitdiff_plain;h=ad9f1fb6bdfc51df61a7fb52d607ca0c0bceca4c;p=libbear.git Creation of physics project --- ad9f1fb6bdfc51df61a7fb52d607ca0c0bceca4c diff --git a/images/grass.bmp b/images/grass.bmp new file mode 100644 index 0000000..7c80c9d Binary files /dev/null and b/images/grass.bmp differ diff --git a/images/grass.xcf b/images/grass.xcf new file mode 100644 index 0000000..a679eb9 Binary files /dev/null and b/images/grass.xcf differ diff --git a/images/hills.bmp b/images/hills.bmp new file mode 100644 index 0000000..88e6ae1 Binary files /dev/null and b/images/hills.bmp differ diff --git a/images/hills.xcf b/images/hills.xcf new file mode 100644 index 0000000..0400385 Binary files /dev/null and b/images/hills.xcf differ diff --git a/images/sand.bmp b/images/sand.bmp new file mode 100755 index 0000000..f178435 Binary files /dev/null and b/images/sand.bmp differ diff --git a/images/sand.xcf b/images/sand.xcf new file mode 100755 index 0000000..aba33fb Binary files /dev/null and b/images/sand.xcf differ diff --git a/images/ship.bmp b/images/ship.bmp new file mode 100644 index 0000000..9b87ec9 Binary files /dev/null and b/images/ship.bmp differ diff --git a/images/ship.xcf b/images/ship.xcf new file mode 100644 index 0000000..6988628 Binary files /dev/null and b/images/ship.xcf differ diff --git a/images/ship1.bmp b/images/ship1.bmp new file mode 100644 index 0000000..812e764 Binary files /dev/null and b/images/ship1.bmp differ diff --git a/images/ship1.xcf b/images/ship1.xcf new file mode 100644 index 0000000..463cb4d Binary files /dev/null and b/images/ship1.xcf differ diff --git a/images/texture.bmp b/images/texture.bmp new file mode 100644 index 0000000..cc37504 Binary files /dev/null and b/images/texture.bmp differ diff --git a/images/texture.jpg b/images/texture.jpg new file mode 100644 index 0000000..f7eaf81 Binary files /dev/null and b/images/texture.jpg differ diff --git a/src/Entities/Ball.cpp b/src/Entities/Ball.cpp new file mode 100644 index 0000000..7c31364 --- /dev/null +++ b/src/Entities/Ball.cpp @@ -0,0 +1,18 @@ +#include "Ball.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +Ball::Ball(Vector2 pos, float radius) + : PhysicsEntity(pos), radius(radius) +{ + +} +Ball::~Ball() +{ + +} + +void Ball::draw() const +{ + // TODO +} diff --git a/src/Entities/Ball.h b/src/Entities/Ball.h new file mode 100644 index 0000000..8a1b1c8 --- /dev/null +++ b/src/Entities/Ball.h @@ -0,0 +1,21 @@ +#ifndef BALL_H +#define BALL_H + +#include "PhysicsEntity.h" +#include "../Vector2.h" + +/// ***** Header Class ***** +class Ball: public PhysicsEntity +{ + public: + Ball(Vector2, float); + virtual ~Ball(); + + virtual void draw() const; + + protected: + float radius; + // color; +}; + +#endif // BALL_H diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp new file mode 100644 index 0000000..d652e2d --- /dev/null +++ b/src/Entities/Entity.cpp @@ -0,0 +1,16 @@ +#include "Entity.h" +#include "../Vector2.h" + +#include + +/// ***** Public Class Methods ***** + +Entity::Entity(Vector2 pos) + : position(pos), velocity(0,0) +{ + +} +Entity::~Entity() +{ + +} diff --git a/src/Entities/Entity.h b/src/Entities/Entity.h new file mode 100644 index 0000000..d697803 --- /dev/null +++ b/src/Entities/Entity.h @@ -0,0 +1,23 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include "../Vector2.h" + +/// ***** Header Class ***** +class Entity +{ + protected: + Entity(Vector2); + + public: + virtual ~Entity(); + + virtual void update(float) = 0; + virtual void draw() const = 0; + + protected: + Vector2 position; + Vector2 velocity; +}; + +#endif // ENTITY_H diff --git a/src/Entities/Line.cpp b/src/Entities/Line.cpp new file mode 100644 index 0000000..14f1526 --- /dev/null +++ b/src/Entities/Line.cpp @@ -0,0 +1,23 @@ +#include "Line.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +Line::Line(Vector2 org, Vector2 dest, bool canDie) + : Particle(org, canDie), dest(dest) +{ + +} +Line::Line(Vector2 org, Vector2 dest, float life_time) + : Particle(org, life_time), dest(dest) +{ + +} +Line::~Line() +{ + +} + +void Line::draw() +{ + // TODO +} diff --git a/src/Entities/Line.h b/src/Entities/Line.h new file mode 100644 index 0000000..19ad61d --- /dev/null +++ b/src/Entities/Line.h @@ -0,0 +1,29 @@ +#ifndef LINE_H +#define LINE_H + +#include "Particle.h" +#include "../Vector2.h" + +/// NOTE to SELF +// a Particle should be drawn for at least the next frame +// ie. creating a particle using ParticleE(Vector2), +// should draw the particle once THEN kill it + +/// ***** Header Class ***** +class Line: public Particle +{ + public: + Line(Vector2, Vector2, bool=true); + Line(Vector2, Vector2, float); + virtual ~Line(); + + virtual void draw(); + + protected: + Vector2 dest; + + // triangle stuffs + // color +}; + +#endif // LINE_H diff --git a/src/Entities/Makefile b/src/Entities/Makefile new file mode 100644 index 0000000..a315853 --- /dev/null +++ b/src/Entities/Makefile @@ -0,0 +1,45 @@ + +LIBALLG = `allegro-config --libs release` +LIBGL = -lGL -lGLU +LIBSDL = `sdl-config --libs` +LIBS = ${LIBSDL} ${LIBGL} + +CXX = g++ +CXXFLAGS = -ggdb -Wall -pedantic + +LDFLAGS = -lc + +SRCS = Ball.cpp Entity.cpp Line.cpp Particle.cpp PhysicsEntity.cpp \ + Point.cpp Polygon.cpp WindParticle.cpp +OBJS = ${SRCS:.cpp=.o} + +TARGETS = Entity.ld +DEPEND = depend.mk + +# set suffixes to look for ... +.SUFFIXES: .cpp .o + +# set default action for a *.cc to create a *.o +.cpp.o: + g++ -c $< ${CXXFLAGS} + +all: ${TARGETS} + +depend: + ${CXX} -MM ${SRCS} > ${DEPEND} + +tags: + ctags ${SRCS} + +clean: + rm -f ${OBJS} ${TARGETS} *~ + +distclean: clean + rm -f tags depend.mk + touch depend.mk + +Entity.ld: ${OBJS} +# ld -o Entity.ld ${LDFLAGS} ${OBJS} + +include ${DEPEND} + diff --git a/src/Entities/Particle.cpp b/src/Entities/Particle.cpp new file mode 100644 index 0000000..9322314 --- /dev/null +++ b/src/Entities/Particle.cpp @@ -0,0 +1,31 @@ +#include "Particle.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +Particle::Particle(Vector2 pos, bool canDie) + : Entity(pos), canDie(canDie) +{ + +} +Particle::Particle(Vector2 pos, float lifeTime) + : Entity(pos), lifeTime(lifeTime) +{ + +} +Particle::~Particle() +{ + +} + +void Particle::update(float time_step) +{ + if(isDead) + return; + + if (canDie) + { + lifeTime -= time_step; + isDead = lifeTime <= 0; + } + position += velocity * time_step; +} diff --git a/src/Entities/Particle.h b/src/Entities/Particle.h new file mode 100644 index 0000000..de5176a --- /dev/null +++ b/src/Entities/Particle.h @@ -0,0 +1,31 @@ +#ifndef PARTICLE_H +#define PARTICLE_H + +#include "Entity.h" +#include "../Vector2.h" + +/// NOTE to SELF +// a Particle should be drawn for at least the next frame +// ie. creating a particle using ParticleE(Vector2), +// should draw the particle once THEN kill it + +/// ***** Header Class ***** +class Particle: public Entity +{ + protected: + Particle(Vector2, bool=true); + Particle(Vector2, float); + + public: + virtual ~Particle(); + + virtual void update(float); + + protected: + float lifeTime; + + bool isDead; + bool canDie; +}; + +#endif // PARTICLE_H diff --git a/src/Entities/PhysicsEntity.cpp b/src/Entities/PhysicsEntity.cpp new file mode 100644 index 0000000..2eadc34 --- /dev/null +++ b/src/Entities/PhysicsEntity.cpp @@ -0,0 +1,41 @@ +#include "PhysicsEntity.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +PhysicsEntity::PhysicsEntity(Vector2 pos) + : Entity(pos) +{ + +} +PhysicsEntity::~PhysicsEntity() +{ + +} + +void PhysicsEntity::update(float time_step) +{ + position = positionAt(time_step); + velocity = velocityAt(time_step); + + force *= 0; +} + +Vector2 PhysicsEntity::positionAt(float time_step) const +{ + return force/mass / 2 * time_step * time_step + velocity * time_step + position; +} + +Vector2 PhysicsEntity::velocityAt(float time_step) const +{ + return force/mass / 2 * time_step + velocity; +} + +void PhysicsEntity::applyForce(const Vector2& force) +{ + this->force += force; +} + +void PhysicsEntity::applyImpulse(const Vector2& impluse) +{ + velocity += impluse; +} diff --git a/src/Entities/PhysicsEntity.h b/src/Entities/PhysicsEntity.h new file mode 100644 index 0000000..49849ab --- /dev/null +++ b/src/Entities/PhysicsEntity.h @@ -0,0 +1,33 @@ +#ifndef PHYSICS_H +#define PHYSICS_H + +#include "Entity.h" +#include "../Vector2.h" + +/// ***** Header Class ***** +class PhysicsEntity: public Entity +{ + protected: + PhysicsEntity(Vector2); + + public: + virtual ~PhysicsEntity(); + + virtual void update(float); + + virtual Vector2 positionAt(float) const; + virtual Vector2 velocityAt(float) const; + + virtual void applyForce(const Vector2&); + virtual void applyImpulse(const Vector2&); + + protected: + Vector2 force; + + float mass; + + // Coefficient of Restitution + float CoR; +}; + +#endif // PHYSICS_H diff --git a/src/Entities/Point.cpp b/src/Entities/Point.cpp new file mode 100644 index 0000000..1c4692f --- /dev/null +++ b/src/Entities/Point.cpp @@ -0,0 +1,23 @@ +#include "Point.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +Point::Point(Vector2 org, bool canDie) + : Particle(org, canDie), radius(2) +{ + +} +Point::Point(Vector2 org, float life_time) + : Particle(org, life_time), radius(2) +{ + +} +Point::~Point() +{ + +} + +void Point::draw() +{ + // TODO +} diff --git a/src/Entities/Point.h b/src/Entities/Point.h new file mode 100644 index 0000000..65173b1 --- /dev/null +++ b/src/Entities/Point.h @@ -0,0 +1,27 @@ +#ifndef POINT_H +#define POINT_H + +#include "Particle.h" +#include "../Vector2.h" + +/// NOTE to SELF +// a Particle should be drawn for at least the next frame +// ie. creating a particle using ParticleE(Vector2), +// should draw the particle once THEN kill it + +/// ***** Header Class ***** +class Point: public Particle +{ + public: + Point(Vector2, bool=true); + Point(Vector2, float); + virtual ~Point(); + + virtual void draw(); + + protected: + float radius; + // color +}; + +#endif // POINT_H diff --git a/src/Entities/Polygon.cpp b/src/Entities/Polygon.cpp new file mode 100644 index 0000000..6b272bd --- /dev/null +++ b/src/Entities/Polygon.cpp @@ -0,0 +1,24 @@ +#include "Polygon.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +Polygon::Polygon(Vector2 pos, vector points) + : PhysicsEntity(pos), points(points) +{ + CreateBindingBox(); +} +Polygon::~Polygon() +{ + +} + +void Polygon::draw() const +{ + // TODO +} + +/// ***** Private Class Methods ***** +void CreateBindingBox() +{ + // TODO +} diff --git a/src/Entities/Polygon.h b/src/Entities/Polygon.h new file mode 100644 index 0000000..c6714fa --- /dev/null +++ b/src/Entities/Polygon.h @@ -0,0 +1,30 @@ +#ifndef POLYGON_H +#define POLYGON_H + +#include "PhysicsEntity.h" +#include "../Vector2.h" + +#include +using std::vector; + +/// ***** Header Class ***** +class Polygon: public PhysicsEntity +{ + public: + Polygon(Vector2, vector); + virtual ~Polygon(); + + virtual void draw() const; + + protected: + Vector2 maxP; // stores the max bounding box point + Vector2 minP; // stores the min bounding box point + + vector points; + + // color; + private: + void CreateBindingBox(); +}; + +#endif // POLYGON_H diff --git a/src/Entities/WindParticle.cpp b/src/Entities/WindParticle.cpp new file mode 100644 index 0000000..25b48ed --- /dev/null +++ b/src/Entities/WindParticle.cpp @@ -0,0 +1,19 @@ +#include "WindParticle.h" +#include "../Vector2.h" + +/// ***** Public Class Methods ***** +WindParticle::WindParticle(Vector2 org, float life_time) + : Point(org, life_time) +{ + +} +WindParticle::~WindParticle() +{ + +} + +void WindParticle::update(float time_step) +{ + Point::update(time_step); + // TODO, insure particle is in the screen by moding +} diff --git a/src/Entities/WindParticle.h b/src/Entities/WindParticle.h new file mode 100644 index 0000000..5440bb4 --- /dev/null +++ b/src/Entities/WindParticle.h @@ -0,0 +1,21 @@ +#ifndef WINDPARTICLE_H +#define WINDPARTICLE_H + +#include "Point.h" +#include "../Vector2.h" + +/// ***** Header Class ***** +class WindParticle: public Point +{ + public: + WindParticle(Vector2, float); + virtual ~WindParticle(); + + virtual void update(float); + + protected: + float radius; + // color +}; + +#endif // POINT_H diff --git a/src/Entities/depend.mk b/src/Entities/depend.mk new file mode 100644 index 0000000..e69de29 diff --git a/src/GameStates/CreatingPolygon.cpp b/src/GameStates/CreatingPolygon.cpp new file mode 100644 index 0000000..fec57dd --- /dev/null +++ b/src/GameStates/CreatingPolygon.cpp @@ -0,0 +1,25 @@ +#include "CreatingPolygon.h" + +/// ***** Constructors/Destructors ***** +CreatingPolygon::CreatingPolygon() +{ + +} +CreatingPolygon::~CreatingPolygon() +{ + +} + +/// ***** Public Class Methods ***** +void CreatingPolygon::handleInput(bool on_top) const +{ + // TODO +} +void CreatingPolygon::update(float time_step, bool on_top) const +{ + +} +void CreatingPolygon::draw(bool on_top) const +{ + // TODO +} diff --git a/src/GameStates/CreatingPolygon.h b/src/GameStates/CreatingPolygon.h new file mode 100644 index 0000000..f27f721 --- /dev/null +++ b/src/GameStates/CreatingPolygon.h @@ -0,0 +1,18 @@ +#ifndef CREATINGPOLYGON_H +#define CREATINGPOLYGON_H + +#include "GameState.h" + +/// ***** Header Class ***** +class CreatingPolygon : public GameState +{ + public: + CreatingPolygon(); + virtual ~CreatingPolygon(); + + virtual void handleInput(bool=false) const; + virtual void update(float, bool=false) const; + virtual void draw(bool=false) const; +}; + +#endif // CREATINGPOLYGON_H diff --git a/src/GameStates/GameState.cpp b/src/GameStates/GameState.cpp new file mode 100644 index 0000000..5bf1226 --- /dev/null +++ b/src/GameStates/GameState.cpp @@ -0,0 +1,11 @@ +#include "GameState.h" + +/// ***** Constructors/Destructors ***** +GameState::GameState() +{ + +} +GameState::~GameState() +{ + +} diff --git a/src/GameStates/GameState.h b/src/GameStates/GameState.h new file mode 100644 index 0000000..13e0d78 --- /dev/null +++ b/src/GameStates/GameState.h @@ -0,0 +1,18 @@ +#ifndef GAMESTATE_H +#define GAMESTATE_H + +/// ***** Header Class ***** +class GameState +{ + protected: + GameState(); + + public: + virtual ~GameState(); + + virtual void handleInput(bool=false) const = 0; + virtual void update(float, bool=false) const = 0; + virtual void draw(bool=false) const = 0; +}; + +#endif // GAMESTATE_H diff --git a/src/GameStates/Makefile b/src/GameStates/Makefile new file mode 100644 index 0000000..02c0cfd --- /dev/null +++ b/src/GameStates/Makefile @@ -0,0 +1,44 @@ + +LIBALLG = `allegro-config --libs release` +LIBGL = -lGL -lGLU +LIBSDL = `sdl-config --libs` +LIBS = ${LIBSDL} ${LIBGL} + +CXX = g++ +CXXFLAGS = -ggdb -Wall -pedantic + +LDFLAGS = -lc + +SRCS = GameState.cpp +OBJS = ${SRCS:.cpp=.o} + +TARGETS = GameStates.ld +DEPEND = depend.mk + +# set suffixes to look for ... +.SUFFIXES: .cpp .o + +# set default action for a *.cc to create a *.o +.cpp.o: + g++ -c $< ${CXXFLAGS} + +all: ${TARGETS} + +depend: + ${CXX} -MM ${SRCS} > ${DEPEND} + +tags: + ctags ${SRCS} + +clean: + rm -f ${OBJS} ${TARGETS} *~ + +distclean: clean + rm -f tags depend.mk + touch depend.mk + +GameStates.ld: ${OBJS} +# ld -o Entity.ld ${LDFLAGS} ${OBJS} + +include ${DEPEND} + diff --git a/src/GameStates/Paused.cpp b/src/GameStates/Paused.cpp new file mode 100644 index 0000000..c3c2e2d --- /dev/null +++ b/src/GameStates/Paused.cpp @@ -0,0 +1,25 @@ +#include "Paused.h" + +/// ***** Constructors/Destructors ***** +Paused::Paused() +{ + +} +Paused::~Paused() +{ + +} + +/// ***** Public Class Methods ***** +void Paused::handleInput(bool on_top) const +{ + // TODO +} +void Paused::update(float time_step, bool on_top) const +{ + +} +void Paused::draw(bool on_top) const +{ + // TODO +} diff --git a/src/GameStates/Paused.h b/src/GameStates/Paused.h new file mode 100644 index 0000000..536da72 --- /dev/null +++ b/src/GameStates/Paused.h @@ -0,0 +1,18 @@ +#ifndef PAUSED_H +#define PAUSED_H + +#include "GameState.h" + +/// ***** Header Class ***** +class Paused : public GameState +{ + public: + Paused(); + virtual ~Paused(); + + virtual void handleInput(bool=false) const; + virtual void update(float, bool=false) const; + virtual void draw(bool=false) const; +}; + +#endif // PAUSED_H diff --git a/src/GameStates/Running.cpp b/src/GameStates/Running.cpp new file mode 100644 index 0000000..10026da --- /dev/null +++ b/src/GameStates/Running.cpp @@ -0,0 +1,32 @@ +#include "Running.h" +#include "../entityManager.h" + +/// ***** Constructors/Destructors ***** +Running::Running() +{ + entityManager::init(); +} +Running::~Running() +{ + entityManager::clean(); +} + +/// ***** Public Class Methods ***** +void Running::handleInput(bool on_top) const +{ + if( on_top ) + { + entityManager::handleInput(); + } +} +void Running::update(float time_step, bool on_top) const +{ + if( on_top ) + { + entityManager::update(time_step); + } +} +void Running::draw(bool on_top) const +{ + entityManager::draw(); +} diff --git a/src/GameStates/Running.h b/src/GameStates/Running.h new file mode 100644 index 0000000..259b216 --- /dev/null +++ b/src/GameStates/Running.h @@ -0,0 +1,18 @@ +#ifndef RUNNING_H +#define RUNNING_H + +#include "GameState.h" + +/// ***** Header Class ***** +class Running : public GameState +{ + public: + Running(); + virtual ~Running(); + + virtual void handleInput(bool=false) const; + virtual void update(float, bool=false) const; + virtual void draw(bool=false) const; +}; + +#endif // RUNNING_H diff --git a/src/GameStates/depend.mk b/src/GameStates/depend.mk new file mode 100644 index 0000000..e69de29 diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..c25ab1b --- /dev/null +++ b/src/Makefile @@ -0,0 +1,53 @@ + +LIBALLG = `allegro-config --libs release` +LIBGL = -lGL -lGLU +LIBSDL = `sdl-config --libs` +LIBS = ${LIBSDL} ${LIBGL} + +CXX = g++ +CXXFLAGS = -ggdb -Wall -pedantic + +SRCS = Vector2.cpp ticks.cpp main.cpp game.cpp entityManager.cpp gldraw.cpp graphics.cpp +OBJS = ${SRCS:.cpp=.o} + +TARGETS = ../run_physics +DEPEND = depend.mk + +# set suffixes to look for ... +.SUFFIXES: .cpp .o + +# set default action for a *.cc to create a *.o +.cpp.o: + g++ -c $< ${CXXFLAGS} + +all: ${TARGETS} + +depend: + ${CXX} -MM ${SRCS} > ${DEPEND} + +tags: + ctags ${SRCS} + +clean: + rm -f ${OBJS} ${TARGETS} *~ + +distclean: clean + rm -f tags depend.mk + touch depend.mk + +# i need to find a nice way of ijnoring .svn folders for the below +tar: clean + cd ..; tar -cjf bluestar.tar.bz2 images/ source/ + + +run: ../run_physics + cd ..; ./run_physics + +../run_physics: ${OBJS} + ${CXX} ${CXXFLAGS} -o ../run_physics ${OBJS} ${LIBS} + +Entities.d: + cd Entities; make + +include ${DEPEND} + diff --git a/src/TODO.txt b/src/TODO.txt new file mode 100644 index 0000000..5956f98 --- /dev/null +++ b/src/TODO.txt @@ -0,0 +1,73 @@ +This file contains a TODO list for project Blue Star in C++ with allegro. +a - entry is something to do, +a ~ entry has been started, +a + entry is something that was done, +and a * entry is something to remember when working in this area of the project. +******************************************************************************* + +- make the tickCount file independ of platform. UNIX/Windows + - do a preprocessor if that changes the file based on OS + ++ investigate the make system, get it to build my project + * http://www.eng.hawaii.edu/Tutor/Make/ + * http://www.opussoftware.com/tutorial/TutMakefile.htm + * http://www.sethi.org/classes/cet375/lab_notes/lab_04_makefile_and_compilation.html + +- Ship class + - isFriendly + - hp + + position + + velocity + + force + + mass + + BITMAP + + drawing + + rotating + ++ Vector2 class + + take skeleton of my C# Vector2 class + +- map system + - should have a notion of speed and direction + - ex. side scrolling should be possible + - diagonal scrolling should also be possible + - rotational defense / final offensive scrolling + - scrolling map background + - static pieces are drawn into the background on loading + - dynamic map pieces are stored and only displayed when on the screen + + - game console needs to + - bind keys + - load / save game + - free play mode commands + - switch levels + - invincible + - free money + +- collision system + - boxes can be out-boxes as well as in-boxes + - boxes can be relative to + - map background + - map objects + - player + - enemies + - collision types include + - object pickup + - repulsion + - character effects + - slow down, map scrolling + - speed up, map scrolling + - gain health + - lose health + + +- random list of allegro things to read + - create_video_bitmap() + - int show_video_bitmap(BITMAP *bitmap); for my double buffering? + - int request_video_bitmap(BITMAP *bitmap); for possible triple buffering + - GFX_CAN_TRIPLE_BUFFER + - int enable_triple_buffer(); + + +- LInKS !! +http://www.gimp.org/tutorials/ \ No newline at end of file diff --git a/src/Vector2.cpp b/src/Vector2.cpp new file mode 100644 index 0000000..7985ef8 --- /dev/null +++ b/src/Vector2.cpp @@ -0,0 +1,118 @@ +#include "Vector2.h" +#include "mathw.h" + +Vector2::Vector2() + : x(0), y(0) +{ +} +Vector2::Vector2(float x, float y) + : x(x), y(y) +{ +} +Vector2::Vector2(const Vector2& vec) + : x(vec.x), y(vec.y) +{ +} + +void Vector2::zero() +{ + x = 0; + y = 0; +} +void Vector2::unit() +{ + float len = length(); + + x /= len; + y /= len; +} + +float Vector2::angle() const +{ + return atan2A(y,x); +} +float Vector2::length() const +{ + return sqrt(x*x + y*y); +} + + +string Vector2::toString() const +{ + char* strX = new char[50]; // long just to be safe + char* strY = new char[50]; // long just to be safe + sprintf(strX, "%f", x); + sprintf(strY, "%f", y); + + string val = (string)"Vector2 x: " + strX + ", y: " + strY; + delete []strX; // deletes the memory allocated, not just what is used by sprintf + delete []strY; // deletes the memory allocated, not just what is used by sprintf + + return val; +} +void Vector2::print() const +{ + printf("%s\n",toString().c_str()); +} + + +Vector2 Vector2::add(const Vector2& vec) const +{ + return Vector2(x+vec.x, y+vec.y); +} +Vector2 Vector2::subtract(const Vector2& vec) const +{ + return Vector2(x-vec.x, y-vec.y); +} +Vector2 Vector2::multiply(float c) const +{ + return Vector2(x*c, y*c); +} +Vector2 Vector2::divide(float c) const +{ + return Vector2(x/c, y/c); +} + + +Vector2 operator+(const Vector2& vec1, const Vector2& vec2) +{ + return vec1.add(vec2); +} +Vector2 operator-(const Vector2& vec1, const Vector2& vec2) +{ + return vec1.subtract(vec2); +} +Vector2 operator*(float c, const Vector2& vec) +{ + return vec.multiply(c); +} +Vector2 operator*(const Vector2& vec, float c) +{ + return vec.multiply(c); +} +Vector2 operator/(const Vector2& vec, float c) +{ + return vec.divide(c); +} + + +void operator+=(Vector2& vec1, const Vector2& vec2) +{ + vec1.x += vec2.x; + vec1.y += vec2.y; +} +void operator-=(Vector2& vec1, const Vector2& vec2) +{ + vec1.x -= vec2.x; + vec1.y -= vec2.y; +} +void operator*=(Vector2& vec, float c) +{ + vec.x *= c; + vec.y *= c; +} +void operator/=(Vector2& vec, float c) +{ + vec.x /= c; + vec.y /= c; +} diff --git a/src/Vector2.h b/src/Vector2.h new file mode 100644 index 0000000..d2bc2a0 --- /dev/null +++ b/src/Vector2.h @@ -0,0 +1,48 @@ +#ifndef VECTOR2_H +#define VECTOR2_H + +#include + +using std::string; + +/// ***** Header Class ***** +class Vector2 +{ + public: + float x; + float y; + + Vector2(); + Vector2(float, float); + Vector2(const Vector2&); + + void zero(); + void unit(); + + float angle() const; + float length() const; + + Vector2 add(const Vector2&) const; + Vector2 subtract(const Vector2&) const; + Vector2 divide(float) const; + Vector2 multiply(float) const; + + string toString() const; + void print() const; +}; + +/// ***** Header Methods ***** +// definitions of the operators are external +// because (float, Vector2) would fail inside class +Vector2 operator+(const Vector2&, const Vector2&); +Vector2 operator-(const Vector2&, const Vector2&); +Vector2 operator*(float, const Vector2&); +Vector2 operator*(const Vector2&, float); +Vector2 operator/(const Vector2&, float); + +void operator+=(Vector2&, const Vector2&); +void operator-=(Vector2&, const Vector2&); +void operator*=(Vector2&, float); +void operator/=(Vector2&, float); + +#endif // VECTOR2_H diff --git a/src/codingstyle.txt b/src/codingstyle.txt new file mode 100644 index 0000000..f1da776 --- /dev/null +++ b/src/codingstyle.txt @@ -0,0 +1,37 @@ +These are a few style guidelines of my own that I try to adhere to. +This is in a very short form as it's mostly for myself. + +- FileNameWithClass +- ClassName + - Public_Class_Variable + - protected_Class_Variable + - private_Class_Variable + +- fileNameWithMethods + - variable_Local_To_File +- methodName, class or file + - method_parameter + - method_variable + +- prefix consts with c, camel -> cName, under -> c_Name + + +- sections of a file should be commented with the following where appropriate + - they should appear in this order + - they should have two newlines before star lines, excluding the first + +/// ***** Private Method Headers ***** +/// ***** Private Variables ***** + +/// ***** Constructors/Destructors ***** + +/// ***** Public Methods ***** +/// ***** Private Methods ***** + +/// ***** Public Class Methods ***** +/// ***** Protected Class Methods ***** +/// ***** Private Class Methods ***** + + +/// ***** Header Class ***** +/// ***** Header Methods ***** diff --git a/src/depend.mk b/src/depend.mk new file mode 100644 index 0000000..9eb1e16 --- /dev/null +++ b/src/depend.mk @@ -0,0 +1,9 @@ +Vector2.o: Vector2.cpp Vector2.h mathw.h +ticks.o: ticks.cpp ticks.h +main.o: main.cpp game.h ticks.h graphics.h +game.o: game.cpp game.h +entityManager.o: entityManager.cpp entityManager.h Entities/Entity.h \ + Entities/../Vector2.h Entities/Particle.h Entities/Entity.h \ + Entities/PhysicsEntity.h +gldraw.o: gldraw.cpp +graphics.o: graphics.cpp diff --git a/src/entityManager.cpp b/src/entityManager.cpp new file mode 100644 index 0000000..f189c7d --- /dev/null +++ b/src/entityManager.cpp @@ -0,0 +1,174 @@ +#include "entityManager.h" +#include "Entities/Entity.h" +#include "Entities/Particle.h" +#include "Entities/PhysicsEntity.h" + +#include +using std::set; + +#include + +/// ***** Private Method Headers ***** +void updateParticles(float); +void updatePhysics(float); + +/// ***** Private Variables ***** +typedef set setPart; +setPart particles_To_Add; +setPart active_Particles; +setPart particles_To_Remove; +bool clearParticles; + +typedef set setPhys; +setPhys physics_To_Add; +setPhys active_Physics; +setPhys physics_To_Remove; +bool clearPhysics; + +/// ***** Public Methods ***** + +void entityMInit() +{ +} +void entityMClean() +{ +} + +void addEntity(Entity* e) +{ + { + Particle* p = dynamic_cast(e); + if( p != 0 ) + { + particles_To_Add.insert(p); + return; + } + } + + { + PhysicsEntity* p = dynamic_cast(e); + if( p != 0 ) + { + physics_To_Add.insert(p); + return; + } + } + + std::cerr << "ENTITY TYPE NOT SUPPORTED BY addEntity()!!"; + std::cerr << std::endl; +} +void deleteEntity(Entity* e) +{ + { + Particle* p = dynamic_cast(e); + if( p != 0 ) + { + particles_To_Remove.insert(p); + return; + } + } + + { + PhysicsEntity* p = dynamic_cast(e); + if( p != 0 ) + { + physics_To_Remove.insert(p); + return; + } + } + + std::cerr << "ENTITY TYPE NOT SUPPORTED BY deleteEntity()!!"; + std::cerr << std::endl; +} + +void clear() +{ + clearParticles = true; + clearPhysics = true; +} + +void handleInput() +{ + // TODO +} +void updateEntities(float time_step) +{ + updateParticles(time_step); + updatePhysics(time_step); +} +void drawEntities() +{ + // update active Particle*s + for( setPart::iterator it = active_Particles.begin(); + it != active_Particles.end(); + it++ ) + { + (*it)->draw(); + } + + // update active PhysicsEntity*s + for( setPhys::iterator it = active_Physics.begin(); + it != active_Physics.end(); + it++ ) + { + (*it)->draw(); + } +} + +/// ***** Private Methods ***** +void updateParticles(float time_step) +{ + // add new Particle*s to Active + for( setPart::iterator it = particles_To_Add.begin(); + it != particles_To_Add.end(); + it++ ) + { + 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++ ) + { + active_Particles.erase(*it); + } + particles_To_Remove.clear(); + + // update active Particle*s + for( setPart::iterator it = active_Particles.begin(); + it != active_Particles.end(); + it++ ) + { + (*it)->update(time_step); + } +} +void updatePhysics(float time_step) +{ + // add new PhysicsEntity*s to Active + for( setPhys::iterator it = physics_To_Add.begin(); + it != physics_To_Add.end(); + it++ ) + { + 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++ ) + { + active_Physics.erase(*it); + } + physics_To_Remove.clear(); + + // update active PhysicsEntity*s + for( setPhys::iterator it = active_Physics.begin(); + it != active_Physics.end(); + it++ ) + { + (*it)->update(time_step); + } +} diff --git a/src/entityManager.h b/src/entityManager.h new file mode 100644 index 0000000..a9d8e36 --- /dev/null +++ b/src/entityManager.h @@ -0,0 +1,21 @@ +#ifndef ENTITYMANAGER_H +#define ENTITYMANAGER_H + +#include "Entities/Entity.h" + +/// ***** Header Methods ***** +namespace entityManager +{ + void init(); + void clean(); + + // does not new or delete Entities + void add(Entity*); + void remove(Entity*); + void clear(); + + void update(float); + void draw(); + void handleInput(); +} +#endif // ENTITYMANAGER_H diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..8800e06 --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,80 @@ +#include "game.h" + +#include "GameStates/GameState.h" +#include "GameStates/Running.h" +#include "GameStates/Paused.h" +#include "GameStates/CreatingPolygon.h" + +#include +using std::vector; + + +/// ***** Private Variables ***** + +// The stack of active game states +vector active_States; + +// Pointers to each possible game state +// inserted and removed from the active_States +Running* running; +Paused* paused; +CreatingPolygon* creating_Polygon; + + +/// ***** Public Methods ***** +void gameInit() +{ + running = new Running(); + paused = new Paused(); + creating_Polygon = new CreatingPolygon(); +} + +void gameClean() +{ + delete creating_Polygon; + delete paused; + delete running; +} + +void gameInput() +{ + int size = active_States.size(); + for( int i = 0; + i < size; + i++ ) + { + if( i-1 == size ) + active_States[i]->handleInput(true); + else + active_States[i]->handleInput(false); + } + +} + +void gameUpdate(float time_step) +{ + int size = active_States.size(); + for( int i = 0; + i < size; + i++ ) + { + if( i-1 == size ) + active_States[i]->update(time_step, true); + else + active_States[i]->update(time_step, false); + } +} + +void gameDraw() +{ + int size = active_States.size(); + for( int i = 0; + i < size; + i++ ) + { + if( i-1 == size ) + active_States[i]->draw(true); + else + active_States[i]->draw(false); + } +} diff --git a/src/game.h b/src/game.h new file mode 100644 index 0000000..bf86897 --- /dev/null +++ b/src/game.h @@ -0,0 +1,12 @@ +#ifndef GAME_H +#define GAME_H + +/// ***** Header Methods ***** +void gameInit(); +void gameClean(); + +void gameInput(); +void gameUpdate(float); +void gameDraw(); + +#endif // GAME_H diff --git a/src/graphics.cpp b/src/graphics.cpp new file mode 100644 index 0000000..42c2e6d --- /dev/null +++ b/src/graphics.cpp @@ -0,0 +1,83 @@ +#include "graphics.h" + +#include +#include +#include +#include + +#include +using std::cerr; +using std::cout; +using std::endl; + + +static const float PI = 3.1415926535897; + +/// ***** Private Method Headers ***** +void sdlInit(); +void glInit(); + +/// ***** Public Methods ***** + +void graphicsInit() +{ + sdlInit(); + glInit(); +} + +void graphicsCleanUp() +{ + +} + +void glDrawCircle() +{ + int num = 32; + + glBegin(GL_POLYGON); + for(int n = 0; n < num; n++) + { + float angle = 2 * PI * n / num; + float x = cos(angle); + float y = sin(angle); + + glVertex3f(x, y, -1); + } + glEnd(); +} + +/// ***** Private Methods ***** +void sdlInit() +{ + if(SDL_Init(SDL_INIT_VIDEO) < 0) + { + cerr << "SDL_Init failed: " << SDL_GetError() << endl; + exit(1); + } + + // In order to use SDL_OPENGLBLIT we have to + // set GL attributes first + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + + if(SDL_SetVideoMode(800, 600, 16, SDL_OPENGL) < 0) + { + cerr << "SDL_SetVideoMode failed: " << SDL_GetError() << endl; + exit(1); + } +} + +void glInit() +{ + glClearColor(0.0, 0.0, 0.0, 0.0); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glOrtho(-20.0, 20.0, -15.0, 15.0, -0.01, 1.01); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + glEnable(GL_DEPTH_TEST); +} diff --git a/src/graphics.h b/src/graphics.h new file mode 100644 index 0000000..c3d5276 --- /dev/null +++ b/src/graphics.h @@ -0,0 +1,10 @@ +#ifndef GRAPHICS_H +#define GRAPHICS_H + +/// ***** Header Methods ***** +void graphicsInit(); +void graphicsCleanUp(); + +void glDrawCircle(); + +#endif // GRAPHICS_H diff --git a/src/init.h b/src/init.h new file mode 100644 index 0000000..824f8b7 --- /dev/null +++ b/src/init.h @@ -0,0 +1,13 @@ +#ifndef INIT_H +#define INIT_H + +/// ***** Header Methods ***** +void initGraphics(); +int initMouse(); +void initKeyboard(); + +void cleanGraphics(); +void cleanMouse(); +void cleanKeyboard(); + +#endif // INIT_H diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..df9f490 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,172 @@ +#include +#include +#include + +#include +using std::cerr; +using std::cout; +using std::endl; + +#include +using std::vector; + +#include "game.h" +#include "ticks.h" +#include "graphics.h" + + +/// ***** Private Method Headers ***** +void init(); + +void run(); +void cleanUp(); + +void blockUpdate(); +void updateFPSCounters(); + +void input(); +void update(float); +void draw(); + + +/// ***** MAIN Method ***** +int main() +{ + init(); + run(); + cleanUp(); + return 0; +} + + +/// ***** Private Variables ***** + +// variable used to determine if it is time to shutdown +bool is_Running; + +/* Values for the main game loop + * target_UPS := the amount of updates that is wanted in one second + * last_Update := stores the time of the last update + * update_Sum := used to determine the updates needs for this run + + * ups := updates per second for the last second + * fps := frames per second for the last second + * update_Count := counts this seconds updates + * draw_Count := counts this seconds draws + * last_Second := stores the time of the last second, used for ups and fps + */ +int target_UPS = 250; +long int last_Block_Update; +float update_Sum = 0; + +int ups, fps; +int update_Count, draw_Count; +long int last_Second; + + +/// ***** Private Methods ***** +void init() +{ + graphicsInit(); + + gameInit(); + + // TODO + // add a game state + + cout << "Initialization Complete" << endl; + + // create starting entities + + cout << "World Created" << endl; +} + +void run() +{ + is_Running = true; + last_Block_Update = tickCountMicro(); + last_Second = tickCountMicro(); + + while(is_Running) + { + blockUpdate(); + updateFPSCounters(); + draw(); + } +} + +void cleanUp() +{ + gameClean(); +} + +void blockUpdate() +{ + long int start = tickCountMicro(); + + // Calculate the updates that should be run for the next draw + update_Sum += (start - last_Block_Update) / (1000000 / (float)target_UPS); + + // insures the float to int cast is done once. + int iupdate_sum = (int)update_Sum; + + // TODO the main run loop needs to be tested and pruned + if (iupdate_sum > 0) + { + // Calculate a time step that spreads the updates out as much as possible + // used because really quick updates are nearly wasted + float time_step = ((float)(start - last_Block_Update)) / iupdate_sum; + + // run the updates + for (int i = 1; i <= iupdate_sum; i++) + { + input(); + update(time_step*i / 1000); + } + // remove the updates that where run from the sum + update_Sum -= iupdate_sum; + last_Block_Update = tickCountMicro(); + } +} + +void updateFPSCounters() +{ + // Check if a second has passed to recalculate UPS and FPS + if (tickCountMicro() - last_Second >= 1000000) + { + ups = update_Count; + fps = draw_Count; + update_Count = 0; + draw_Count = 0; + + last_Second = tickCountMicro(); + } +} + +void input() +{ + gameInput(); + + /* + if(key[KEY_ESC]) + is_Running = false; + */ +} + +void update(float time_step) +{ + update_Count++; + + gameUpdate(time_step); +} + +void draw() +{ + draw_Count++; + + gameDraw(); + + SDL_GL_SwapBuffers(); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +} diff --git a/src/mathw.cpp b/src/mathw.cpp new file mode 100644 index 0000000..c25df98 --- /dev/null +++ b/src/mathw.cpp @@ -0,0 +1,45 @@ +#include "mathw.h" + + +/// ***** Private Varibles ***** + +const float PI = 3.141592653; + + +/// ***** Public Methods ***** + +int mod(int x, int y) +{ + return x % y + (x < 0 ? y : 0); +} + +float radsToA(float rads) +{ + return ftofix(rads * 128/PI); +} + +float atanA(float c) +{ + return radsToA(atan(c)) + itofix(64); +} + +float atan2A(float y, float x) +{ + return radsToA(atan2(y,x)) + itofix(64); +} + +/// Vector2 Math + +Vector2 perp(const Vector2& vec) +{ + return Vector2(-vec.y, vec.x); +} + +float dot(const Vector2& vec1, const Vector2& vec2) +{ + return vec1.x * vec2.x + vec1.y * vec2.y; +} + +//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..0d9e3ab --- /dev/null +++ b/src/mathw.h @@ -0,0 +1,29 @@ +#ifndef MATHW_H +#define MATHW_H + +#include +#include "Vector2.h" + + +/// ***** Header Methods ***** + +int mod(int,int); + +float radsToA(float); +float atanA(float); +float atan2A(float, float); + +/// Vector2 Math + +//Vector2 vectorToLine(float, float, float, float) const; +//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 diff --git a/src/ticks.cpp b/src/ticks.cpp new file mode 100644 index 0000000..69a8cbf --- /dev/null +++ b/src/ticks.cpp @@ -0,0 +1,38 @@ +#include +#include "ticks.h" + +/* this file is specific to a UNIX system */ + +/// ***** Public Methods ***** + +// returns the current microseconds from unix time +long int tickCountMicro() +{ + struct timeval tv; + gettimeofday(&tv, 0); + + return (long int)tv.tv_sec * 1000000 + tv.tv_usec; +} + +// returns the current milliseconds from unix time +long int tickCountMilli() +{ + return tickCountMicro() / 1000; +} + +// returns the current seconds from unix time +long int tickCountSec() +{ + return tickCountMicro() / 1000000; +} + +// return after num milliseconds +void wait(int num) +{ + long int start; + int numMicro = num * 1000; + + start = tickCountMicro(); + while(tickCountMicro() - start < numMicro); +} + diff --git a/src/ticks.h b/src/ticks.h new file mode 100644 index 0000000..239ef0c --- /dev/null +++ b/src/ticks.h @@ -0,0 +1,17 @@ +#ifndef TICKS_H +#define TICKS_H + +/// ***** Public Methods ***** + +// returns the current seconds from program start +long int tickCountSec(); + +// returns the current milliseconds from program start +long int tickCountMilli(); + +// returns the current microseconds from program start +long int tickCountMicro(); + +void wait(int); + +#endif // TICKS_H