Skip to content

Commit

Permalink
Clean up examples code. Add namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
ropez committed Feb 7, 2010
1 parent 68c89c6 commit b8616d1
Show file tree
Hide file tree
Showing 38 changed files with 205 additions and 69 deletions.
5 changes: 5 additions & 0 deletions src/examples/pong/ball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "Pieces/Debug"

namespace pong
{

Ball::Ball(pcs::objectid_t objectId)
: GameObject(objectId)
, m_posX(0.0)
Expand Down Expand Up @@ -54,3 +57,5 @@ double Ball::getPositionZ() const
{
return m_posZ;
}

}
7 changes: 6 additions & 1 deletion src/examples/pong/ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "Pieces/GameObject"
#include "Pieces/GameObjectAction"

namespace pong
{

class Ball : public pcs::GameObject
{
public:
Expand All @@ -17,7 +20,7 @@ class Ball : public pcs::GameObject

void addRelativeX(double xOffset);
void addRelativeZ(double zOffset);

double getPositionZ() const;
double getPositionX() const;

Expand All @@ -32,4 +35,6 @@ class Ball : public pcs::GameObject
double m_speed;
};

}

#endif //BALL_H
19 changes: 12 additions & 7 deletions src/examples/pong/ball_host_callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "Pieces/Host"
#include "Pieces/Message"

namespace pong
{

BallUpdateCallback::BallUpdateCallback(pcs::Host* host, Ball* ball, GameState* gameState, PlayerList_t* playerList)
: pcs::GameObjectAction()
, m_lastTick(0)
Expand All @@ -31,19 +34,19 @@ void BallUpdateCallback::operator()(pcs::framenum_t /*frameNum*/)

double angle = m_ball->getAngle();
double velocity = m_ball->getVelocity();

// Update balls position w.r.t velocity and angle.
m_ball->addRelativeX(deltaTime * velocity * sin(angle));
m_ball->addRelativeZ(deltaTime * velocity * cos(angle));

// Check if ball hits walls

// Check right wall
if(m_ball->getPositionX() >= cfg::frameInsideRight - cfg::ballSizeHalf)
{
m_ball->setAngle(cfg::pi * 2 - m_ball->getAngle());
m_gameState->ballIsLostForPlayerRight = true;

pcs::Message msg(MSG_SCORE_UPDATED);
m_host->sendMessage(msg);
}
Expand Down Expand Up @@ -74,17 +77,17 @@ void BallUpdateCallback::operator()(pcs::framenum_t /*frameNum*/)
// Make angle in the interval [0, 2*PI) (If someone knows a better way to do this,
// please let me know, Joakim)
m_ball->setAngle(fmod(cfg::pi2 + fmod(m_ball->getAngle(), cfg::pi2), cfg::pi2));


// Check if ball hit a player
for(PlayerList_t::iterator it = m_playerList->begin(); it != m_playerList->end(); ++it)
{
pcs::ReferencePointer<Player> player = (*it);

double ballPotentialHitSide = 0.0;

const bool ballIsMovingTowardLeft = m_ball->getAngle() > cfg::pi ? true : false;

if(ballIsMovingTowardLeft)
{
// Ball is moving towards left
Expand Down Expand Up @@ -123,7 +126,7 @@ void BallUpdateCallback::operator()(pcs::framenum_t /*frameNum*/)
}

// This "break" is a tiny optimization, since we assume the ball cannot hit both players in the same frame.
break;
break;
}
}
}
Expand All @@ -133,3 +136,5 @@ void BallUpdateCallback::operator()(pcs::framenum_t /*frameNum*/)
m_ball->setAngle(fmod(cfg::pi2 + fmod(m_ball->getAngle(), cfg::pi2), cfg::pi2));

}

}
5 changes: 5 additions & 0 deletions src/examples/pong/ball_host_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

#include "timer.h"

namespace pong
{

class BallUpdateCallback : public pcs::GameObjectAction
{
public:
Expand All @@ -30,4 +33,6 @@ class BallUpdateCallback : public pcs::GameObjectAction
bool m_wallHitLeft;
};

}

#endif //BALLUPDATECALLBACK_H
12 changes: 8 additions & 4 deletions src/examples/pong/ballosg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include <iostream>

namespace pong
{

class BallOSGUpdateCallback : public osg::NodeCallback
{
public:
Expand All @@ -17,7 +20,7 @@ class BallOSGUpdateCallback : public osg::NodeCallback
void operator() (osg::Node* node, osg::NodeVisitor* nv)
{
BallOSG* ballOSG = dynamic_cast<BallOSG*>(node);

if(ballOSG)
{
ballOSG->setMatrix(osg::Matrix::translate(osg::Vec3(m_ball->getPositionX(), 0.0, m_ball->getPositionZ())));
Expand Down Expand Up @@ -48,15 +51,15 @@ osg::ref_ptr<osg::Geode> BallOSG::createGeode()

// create Geometry object to store all the vetices and lines primtive.
osg::ref_ptr<osg::Geometry> polyGeom = new osg::Geometry();

// note, anticlockwsie ordering.
const double halfBallSize = cfg::ballSize / 2;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();
vertices->push_back(osg::Vec3(-halfBallSize, 0.0, -halfBallSize));
vertices->push_back(osg::Vec3( halfBallSize, 0.0, -halfBallSize));
vertices->push_back(osg::Vec3( halfBallSize, 0.0, halfBallSize));
vertices->push_back(osg::Vec3(-halfBallSize, 0.0, halfBallSize));

// pass the created vertex array to the points geometry object.
polyGeom->setVertexArray(vertices.get());

Expand All @@ -76,7 +79,7 @@ osg::ref_ptr<osg::Geode> BallOSG::createGeode()
polyGeom->setNormalArray(normals.get());
polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);

// This time we simply use primitive, and hardwire the number of coords to use
// This time we simply use primitive, and hardwire the number of coords to use
// since we know up front,
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, vertices->size()));

Expand All @@ -87,3 +90,4 @@ osg::ref_ptr<osg::Geode> BallOSG::createGeode()

}

}
9 changes: 7 additions & 2 deletions src/examples/pong/ballosg.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "ball.h"
#include "Pieces/ReferencePointer"

namespace pong
{

class BallOSG : public osg::MatrixTransform
{
public:
Expand All @@ -15,9 +18,11 @@ class BallOSG : public osg::MatrixTransform

private:
osg::ref_ptr<osg::Geode> createGeode();

pcs::ReferencePointer<Ball> m_ball;

};

}

#endif // BALLOSG_H
5 changes: 5 additions & 0 deletions src/examples/pong/event_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include <iostream>

namespace pong
{

PongEventHandler::PongEventHandler(pcs::Peer* peer)
: osgGA::GUIEventHandler()
, m_peer(peer)
Expand Down Expand Up @@ -110,4 +113,6 @@ void PongEventHandler::operator()(osg::Node* node, osg::NodeVisitor* nv)
{


}

}
9 changes: 7 additions & 2 deletions src/examples/pong/event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "playerosg.h"
#include "ballosg.h"

namespace pong
{

class PongEventHandler : public osgGA::GUIEventHandler
{
public:
PongEventHandler(pcs::Peer* peer);
~PongEventHandler();

virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&);
private:
Expand All @@ -31,7 +34,9 @@ class PongEventHandler : public osgGA::GUIEventHandler

int m_scorePlayer1;
int m_scorePlayer2;

};

}

#endif // EVENT_HANDLER_H
11 changes: 8 additions & 3 deletions src/examples/pong/frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include <osg/Geode>
#include <osg/Geometry>

namespace pong
{

Frame::Frame()
: osg::MatrixTransform()
, _xPos(0.0)
Expand All @@ -21,8 +24,8 @@ osg::ref_ptr<osg::Geode> Frame::createGeode()

// create Geometry object to store all the vetices and lines primtive.
osg::ref_ptr<osg::Geometry> polyGeom = new osg::Geometry();


osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array();

// note, anticlockwsie ordering.
Expand Down Expand Up @@ -65,7 +68,7 @@ osg::ref_ptr<osg::Geode> Frame::createGeode()
polyGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);


// This time we simply use primitive, and hardwire the number of coords to use
// This time we simply use primitive, and hardwire the number of coords to use
// since we know up front,
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, vertices->size()));

Expand Down Expand Up @@ -97,3 +100,5 @@ void Frame::addRelativeZ(double zOffset)
_zPos += zOffset;
setPositionZ(_zPos);
}

}
7 changes: 6 additions & 1 deletion src/examples/pong/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <osg/MatrixTransform>

namespace pong
{

class Frame : public osg::MatrixTransform
{
public:
Expand All @@ -16,10 +19,12 @@ class Frame : public osg::MatrixTransform

private:
osg::ref_ptr<osg::Geode> createGeode();

double _xPos;
double _zPos;

};

}

#endif // FRAME_H
1 change: 1 addition & 0 deletions src/examples/pong/main_host.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Pieces/Application"
#include "pong_host.h"

using pong::PongHost;

int main(int argc, char** argv)
{
Expand Down
3 changes: 3 additions & 0 deletions src/examples/pong/main_peer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include "Pieces/Application"
#include "osg_thread.h"

using pong::PongPeer;
using pong::OSGThread;

int main(int argc, char** argv)
{
pcs::Application application(argc, argv);
Expand Down
39 changes: 5 additions & 34 deletions src/examples/pong/osg_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@

#include "Pieces/TimerEvent"

namespace pong
{

OSGThread::OSGThread(osg::ref_ptr<osg::Group> root, PongPeer* peer)
: OpenThreads::Thread()
, m_viewer()
, m_root(root)
{
m_viewer.setUpViewInWindow(100, 100, 800, 600);
// m_viewer.setUpViewer(
// osgProducer::Viewer::TRACKBALL_MANIPULATOR |
// osgProducer::Viewer::STATE_MANIPULATOR |
// osgProducer::Viewer::HEAD_LIGHT_SOURCE |
// osgProducer::Viewer::VIEWER_MANIPULATOR |
// osgProducer::Viewer::ESCAPE_SETS_DONE);

// m_viewer.getCameraConfig()->getRenderSurface(0)->setWindowName("Pong");

// m_viewer.setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));

m_root->addChild(setUpWorld().get());
m_root->addChild(new ScoreBoard(peer->getPlayerList()));

Expand All @@ -35,14 +27,10 @@ OSGThread::OSGThread(osg::ref_ptr<osg::Group> root, PongPeer* peer)

osg::Camera* camera = m_viewer.getCamera();
camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0));

// Set up view frustum
camera->setProjectionMatrixAsFrustum(-640, 640, -512, 512, 2048, 10000);
// osg::Camera::Lens* lens = new osg::Camera::Lens();
// lens->setFrustum(-640, 640, -512, 512, 2048, 10000);
// lens->setAutoAspect(true);
// camera->setLens(lens);


// Define the root node of the scene graph.
m_viewer.setSceneData(m_root.get());
}
Expand All @@ -65,23 +53,6 @@ osg::ref_ptr<osg::Group> OSGThread::setUpWorld()
void OSGThread::run()
{
m_viewer.run();

// m_viewer.realize();
//
// for(;;)
// {
// m_viewer.sync();
// m_viewer.update();
// m_viewer.frame();
// }
//
// // wait for all cull and draw threads to complete.
// m_viewer.sync();
//
// // run a clean up frame to delete all OpenGL objects.
// m_viewer.cleanup_frame();
//
// // wait for all the clean up frame to complete.
// m_viewer.sync();
}

}
Loading

0 comments on commit b8616d1

Please sign in to comment.