Permalink
Browse files

random stuff

  • Loading branch information...
1 parent 6cfc131 commit e7ec6ae33072b2dbefd42602276e67d302f2d010 @Detegr committed Mar 12, 2013
Showing with 187 additions and 86 deletions.
  1. +4 −2 Makefile
  2. +45 −8 client.cpp
  3. +6 −1 entity.cpp
  4. +2 −1 entity.h
  5. +0 −26 gfxtimer.cpp
  6. +0 −22 gfxtimer.h
  7. +24 −5 packetparser.cpp
  8. +36 −5 physicalentity.cpp
  9. +8 −1 physicalentity.h
  10. +23 −0 physicsmanager.cpp
  11. +3 −0 physicsmanager.h
  12. +7 −7 server.cpp
  13. +12 −5 singleton.cpp
  14. +6 −3 singleton.h
  15. +10 −0 timer.cpp
  16. +1 −0 timer.h
View
@@ -12,9 +12,11 @@ SOURCES=singleton.cpp \
modelmanager.cpp \
entity.cpp \
model.cpp \
- gfxtimer.cpp \
timer.cpp \
- packetparser.cpp
+ packetparser.cpp \
+ physicsmanager.cpp \
+ physicalentity.cpp \
+ bullet.cpp
MAIN=client.cpp
SERVERMAIN=server.cpp
View
@@ -5,6 +5,8 @@
#include "packetparser.h"
#include "input.h"
#include "timer.h"
+#include "physicsmanager.h"
+#include "physicalentity.h"
using namespace dtglib;
@@ -75,6 +77,7 @@ int main()
C_Singleton::M_InputHandler();
C_Renderer* r = C_Singleton::M_Renderer();
C_ShaderManager* s = C_Singleton::M_ShaderManager();
+ C_PhysicsManager* pm = C_Singleton::M_PhysicsManager();
s->M_Load("minimal");
s->M_Load("green");
@@ -84,16 +87,20 @@ int main()
p.M_Clear();
unsigned char keyvec=0;
- unsigned char prevkeyvec=0;
- C_Timer idt;
- idt.M_Reset();
+ //unsigned char prevkeyvec=0;
+ C_Timer timer;
+ timer.M_Reset();
while(running)
{
- if(sock.M_Receive(p, 0, NULL, NULL))
+ double newt=timer.M_Now();
+ double currt=
+ idt.M_Reset();
+ keyvec=getkeys();
+ /*
+ if(sock.M_Receive(p, 1, NULL, NULL))
{
- idt.M_Reset();
+ //idt.M_Reset();
C_Packet keys;
- keyvec=getkeys();
if(prevkeyvec != 0xF0)
{
keys << keyvec;
@@ -102,8 +109,36 @@ int main()
prevkeyvec=keyvec;
while(p.M_Size()) C_PacketParser::M_Parse(p);
}
- else
+ */
+ //else
{
+ const std::vector<C_GfxEntity*>& entities = r->M_Entities();
+ for(std::vector<C_GfxEntity*>::const_iterator it=entities.begin(); it!=entities.end(); ++it)
+ {
+ C_Entity* e=pm->GetEntity((*it)->M_Id());
+ if((*it)->IsPlayer())
+ {
+ b2Body* b=e->M_Body();
+ if(keyvec & 0x1)
+ {
+ b->SetAngularVelocity(-3.0f);
+ }
+ else if(keyvec & 0x2)
+ {
+ b->SetAngularVelocity(3.0f);
+ }
+ else b->SetAngularVelocity(0.0f);
+ if(keyvec & 0x8)
+ {
+ float32 a = b->GetAngle();
+ b2Vec2 force = b2Vec2(-sin(a), cos(a));
+ force *= 0.06f;
+ b->ApplyForceToCenter(force);
+ }
+ }
+ (*it)->SetPosition(e->GetPosition().x, e->GetPosition().y);
+ (*it)->M_SetRotation(e->GetRotation());
+ }
/*
const std::vector<C_GfxEntity*>& entities = r->M_Entities();
for(std::vector<C_GfxEntity*>::const_iterator it=entities.begin(); it!=entities.end(); ++it)
@@ -113,7 +148,9 @@ int main()
}
*/
}
- g_Sleep(1);
+ //g_Sleep(1);
+ g_Sleep(30-((int)idt.M_Get()*1000));
+ pm->M_Simulate();
p.M_Clear();
r->M_Draw();
View
@@ -27,13 +27,18 @@ void C_GfxEntity::M_Translate(float amount, unsigned char axis)
m_TranslationMatrix = glm::translate(m_TranslationMatrix, glm::vec3(xamount,yamount,0.0f));
}
-void C_GfxEntity::M_SetPosition(float x, float y)
+void C_GfxEntity::SetPosition(float x, float y)
{
m_Ex.M_Add(x);
m_Ey.M_Add(y);
m_TranslationMatrix = glm::translate(glm::mat4(1.0), glm::vec3(x/m_Scale,y/m_Scale,0.0f));
}
+void C_GfxEntity::SetPosition(const C_Vec2& v)
+{
+ m_TranslationMatrix = glm::translate(glm::mat4(1.0), glm::vec3(v.x/m_Scale, v.y/m_Scale, 0.0f));
+}
+
const C_Vec2 C_GfxEntity::GetPosition() const
{
return C_Vec2(m_TranslationMatrix[3][0]*m_Scale, m_TranslationMatrix[3][1]*m_Scale);
View
@@ -30,7 +30,8 @@ class C_GfxEntity
public:
static C_GfxEntity* M_Create(unsigned short id, const C_Model& m, float scale=1.0f);
void M_Translate(float amount, unsigned char axis);
- void M_SetPosition(float x, float y);
+ void SetPosition(float x, float y);
+ void SetPosition(const C_Vec2& v);
const C_Vec2 GetPosition() const;
void M_ExtrapolatePosition(double dt);
void M_Scale(float amount);
View
@@ -1,26 +0,0 @@
-#include "gfxtimer.h"
-
-C_GfxTimer::C_GfxTimer() : m_Frames(0), m_Time(glfwGetTime())
-{
- m_PrevTime=0;
- m_Dt=0;
- memset(title, 0, 64);
-}
-
-void C_GfxTimer::M_SetTime()
-{
- m_Frames++;
- double t=glfwGetTime();
- m_Dt=t-m_PrevTime;
- m_PrevTime=t;
- if(1.0f + m_Time <= t)
- {
- m_MSPF=(t-m_Time)*1000 / m_Frames;
- sprintf(title, "ms per frame: %f", m_MSPF);
- glfwSetWindowTitle(title);
- m_Time=t;
- m_Frames=0;
- }
-}
-double C_GfxTimer::M_DT() const { return m_Dt; }
-double C_GfxTimer::M_MSPF() const { return m_MSPF; }
View
@@ -1,22 +0,0 @@
-#pragma once
-#include <GL3/gl3w.h>
-#include <GL/glfw.h>
-#include <GL3/gl3.h>
-#include <cstdio>
-#include <cstring>
-
-class C_GfxTimer
-{
- private:
- unsigned int m_Frames;
- double m_Time;
- double m_PrevTime;
- double m_Dt;
- float m_MSPF;
- char title[64];
- public:
- C_GfxTimer();
- void M_SetTime();
- double M_DT() const;
- double M_MSPF() const;
-};
View
@@ -59,21 +59,40 @@ void C_PacketParser::M_GfxEntity(C_Packet& p, bool full)
float scale,x,y,angle;
scale=x=y=angle=0.0f;
unsigned short id=65535;
- if(full) p >> id >> name >> scale >> x >> y >> angle;
+ unsigned char type;
+
+ if(full) p >> id >> type >> name >> scale >> x >> y >> angle;
else p >> id >> x >> y >> angle;
+ //C_PhysicsManager* pm=C_Singleton::M_PhysicsManager();
C_Renderer* r=C_Singleton::M_Renderer();
C_GfxEntity* e=r->M_GetEntity(id);
+ //C_Entity* pe=pm->GetEntity(id);
if(e)
{
- e->M_SetPosition(x,y);
- e->M_SetRotation(angle);
+ //e->SetPosition(pe->GetPosition());
+ //pe->SetPosition(x,y);
+ //e->M_SetRotation(angle);
}
else if(full)
{
std::cout << "Creating entity with id: " << id << std::endl;
- C_GfxEntity* e=C_GfxEntity::M_Create(id, C_Singleton::M_ModelManager()->M_Get(name), scale);
- e->M_SetPosition(x,y);
+ C_PhysicsManager* p=C_Singleton::M_PhysicsManager();
+ C_ModelManager* m=C_Singleton::M_ModelManager();
+ const C_Model& model=m->M_Get(name);
+ C_GfxEntity* e=C_GfxEntity::M_Create(id, model, scale);
+ C_Entity* pe;
+ if(type == C_Entity::Fixed)
+ {
+ pe=p->M_CreateStaticEntity(id, model, scale);
+ }
+ else
+ {
+ pe=p->M_CreateDynamicEntity(id, model, scale);
+ }
+ e->SetPosition(x,y);
+ pe->SetPosition(x,y);
+ pe->SetRotation(angle);
e->M_SetRotation(angle);
}
}
View
@@ -3,12 +3,24 @@
#define TOWORLD 10.0f
-C_Entity::C_Entity(b2World& w, const C_Model& m, float s, bool dynamic, Type t) :
- m_Id(0), m_Model(m), m_Scale(s), m_Dynamic(dynamic), m_Body(NULL), m_Data()
+C_Entity::C_Entity(b2World& w, const C_Model& m, float s, bool dynamic, Type t)
{
- static unsigned int id=0;
+ static unsigned short id=0;
id++;
+ M_Initialize(id,w,m,s,dynamic,t);
+}
+
+C_Entity::C_Entity(unsigned short id, b2World& w, const C_Model& m, float s, bool dynamic, Type t)
+{
+ M_Initialize(id,w,m,s,dynamic,t);
+}
+
+void C_Entity::M_Initialize(unsigned short id, b2World& w, const C_Model& m, float s, bool dynamic, Type t)
+{
m_Id=id;
+ m_Model=m;
+ m_Scale=s;
+ m_Body=NULL;
memset(&m_Data, 0, 2);
m_Data[0]=t;
b2BodyDef bodydef;
@@ -35,10 +47,14 @@ C_Entity::~C_Entity()
m_Body=NULL;
}
-void C_Entity::M_SetPosition(float x, float y)
+void C_Entity::SetPosition(float x, float y)
{
m_Body->SetTransform(b2Vec2(x*TOWORLD, y*TOWORLD), 0.0f);
}
+void C_Entity::SetPosition(const C_Vec2& v)
+{
+ m_Body->SetTransform(b2Vec2(v.x*TOWORLD, v.y*TOWORLD), 0.0f);
+}
void C_Entity::operator>>(dtglib::C_Packet& p)
{
@@ -51,7 +67,22 @@ void C_Entity::M_DumpFullInstance(dtglib::C_Packet& p)
{
b2Vec2 pos=m_Body->GetPosition();
float angle=m_Body->GetAngle();
- p << (unsigned char)NET::FullEntityBegin << m_Id << m_Model.M_Name() << m_Scale << pos.x/TOWORLD << pos.y/TOWORLD << angle;
+ unsigned char* t=(unsigned char*)m_Body->GetUserData();
+ p << (unsigned char)NET::FullEntityBegin << m_Id << *t << m_Model.M_Name() << m_Scale << pos.x/TOWORLD << pos.y/TOWORLD << angle;
+}
+C_Vec2 C_Entity::GetPosition() const
+{
+ b2Vec2 pos=m_Body->GetPosition();
+ return C_Vec2(pos.x/TOWORLD, pos.y/TOWORLD);
+}
+float C_Entity::GetRotation() const
+{
+ return m_Body->GetAngle();
+}
+
+void C_Entity::SetRotation(float r)
+{
+ m_Body->SetTransform(m_Body->GetPosition(), r);
}
/*
View
@@ -11,6 +11,7 @@ class C_Entity
enum Type
{
Default=0,
+ Fixed,
Bullet
};
protected:
@@ -26,10 +27,16 @@ class C_Entity
int m_Data[2];
C_Entity(b2World& w, const C_Model& m, float scale=1.0f, bool dynamic=true, Type t=Default);
+ C_Entity(unsigned short id, b2World& w, const C_Model& m, float s, bool dynamic, Type t=Default);
+ void M_Initialize(unsigned short id, b2World& w, const C_Model& m, float s, bool dynamic, Type t);
public:
static const int BULLET_HITS=2;
b2Body* M_Body() { return m_Body; }
- void M_SetPosition(float x, float y);
+ C_Vec2 GetPosition() const;
+ void SetPosition(float x, float y);
+ void SetPosition(const C_Vec2& v);
+ float GetRotation() const;
+ void SetRotation(float r);
void operator>>(dtglib::C_Packet& p);
void M_DumpFullInstance(dtglib::C_Packet& p);
float M_Scale() const {return m_Scale;}
View
@@ -7,22 +7,31 @@ float32 C_PhysicsManager::m_TimeStep;
C_PhysicsManager::C_PhysicsManager() :
m_World(b2Vec2(0.0f, 0.0f))
{
+ std::cout << "Initializing physicsmanager...";
C_PhysicsManager::m_TimeStep = 1.0f/30.0f;
m_World.SetAllowSleeping(true);
+ std::cout << "OK!" << std::endl;
}
C_PhysicsManager::~C_PhysicsManager()
{
+ std::cout << "Destroying physicsmanager...";
for(std::vector<C_Entity*>::iterator it=m_Bodies.begin(); it!=m_Bodies.end(); ++it)
{
delete *it;
}
delete m_ContactListener;
+ std::cout << "OK!" << std::endl;
}
C_Entity* C_PhysicsManager::M_CreateDynamicEntity(const C_Model& m, float scale)
{
m_Bodies.push_back(new C_Entity(m_World,m,scale,true));
return m_Bodies.back();
}
+C_Entity* C_PhysicsManager::M_CreateDynamicEntity(unsigned short id, const C_Model& m, float scale)
+{
+ m_Bodies.push_back(new C_Entity(id, m_World,m,scale,true));
+ return m_Bodies.back();
+}
C_Bullet* C_PhysicsManager::M_CreateBullet(const C_Model& m, float scale)
{
C_Bullet* b=new C_Bullet(m_World,m,scale,true);
@@ -34,6 +43,11 @@ C_Entity* C_PhysicsManager::M_CreateStaticEntity(const C_Model& m, float scale)
m_Bodies.push_back(new C_Entity(m_World,m,scale,false));
return m_Bodies.back();
}
+C_Entity* C_PhysicsManager::M_CreateStaticEntity(unsigned short id, const C_Model& m, float scale)
+{
+ m_Bodies.push_back(new C_Entity(id, m_World,m,scale,false));
+ return m_Bodies.back();
+}
void C_PhysicsManager::M_Simulate()
{
@@ -61,3 +75,12 @@ void C_PhysicsManager::M_SetContactListener(b2ContactListener* cl)
m_ContactListener=cl;
m_World.SetContactListener(m_ContactListener);
}
+
+C_Entity* C_PhysicsManager::GetEntity(unsigned short id) const
+{
+ for(std::vector<C_Entity*>::const_iterator it=m_Bodies.begin(); it!=m_Bodies.end(); ++it)
+ {
+ if((*it)->m_Id == id) return *it;
+ }
+ return NULL;
+}
View
@@ -26,8 +26,11 @@ class C_PhysicsManager
public:
void M_Simulate();
C_Entity* M_CreateDynamicEntity(const C_Model& m, float s=1.0f);
+ C_Entity* M_CreateDynamicEntity(unsigned short id, const C_Model& m, float s=1.0f);
C_Entity* M_CreateStaticEntity(const C_Model& m, float s=1.0f);
+ C_Entity* M_CreateStaticEntity(unsigned short id, const C_Model& m, float s=1.0f);
C_Bullet* M_CreateBullet(const C_Model& m, float s=1.0f);
void M_DestroyEntity(C_Entity* e);
void M_SetContactListener(b2ContactListener* cl);
+ C_Entity* GetEntity(unsigned short id) const;
};
Oops, something went wrong.

0 comments on commit e7ec6ae

Please sign in to comment.