Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

random stuff

  • Loading branch information...
commit e7ec6ae33072b2dbefd42602276e67d302f2d010 1 parent 6cfc131
@Detegr authored
View
6 Makefile
@@ -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
53 client.cpp
@@ -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
7 entity.cpp
@@ -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
3  entity.h
@@ -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
26 gfxtimer.cpp
@@ -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
22 gfxtimer.h
@@ -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
29 packetparser.cpp
@@ -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
41 physicalentity.cpp
@@ -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
9 physicalentity.h
@@ -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
23 physicsmanager.cpp
@@ -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
3  physicsmanager.h
@@ -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;
};
View
14 server.cpp
@@ -60,7 +60,7 @@ int main()
x=-1.0f;
for(int i=0; i<4; ++i) {
C_Entity* b=p->M_CreateDynamicEntity(m->M_Get("box"), size);
- b->M_SetPosition(x, y);
+ b->SetPosition(x, y);
x+=0.15f;
boxes.push_back(b);
}
@@ -68,10 +68,10 @@ int main()
y-=2.0f/rows;
}
- top->M_SetPosition(0,2.0f);
- bottom->M_SetPosition(0,-2.0f);
- left->M_SetPosition(-2.0f,0.0f);
- right->M_SetPosition(2.0f,0.0f);
+ top->SetPosition(0,2.0f);
+ bottom->SetPosition(0,-2.0f);
+ left->SetPosition(-2.0f,0.0f);
+ right->SetPosition(2.0f,0.0f);
C_IpAddress ip; unsigned short port;
C_Packet packet;
@@ -121,7 +121,7 @@ int main()
packet.M_Clear();
c=pool.M_Add(new C_Connection(ip,port));
C_Entity* e=p->M_CreateDynamicEntity(m->M_Get("triangle"), 0.08f);
- e->M_SetPosition(0,0);
+ e->SetPosition(0,0);
c->M_SetEntity(e);
m->M_Get("triangle") >> packet;
m->M_Get("horizwall") >> packet;
@@ -164,7 +164,7 @@ int main()
newplayers.M_Clear();
}
packet.M_Clear();
- g_Sleep(60-((int)t->M_Get()*1000));
+ g_Sleep(250-((int)t->M_Get()*1000));
p->M_Simulate();
}
C_Singleton::M_DestroySingletons();
View
17 singleton.cpp
@@ -4,7 +4,8 @@ C_Renderer* C_Singleton::m_Renderer;
C_InputHandler* C_Singleton::m_InputHandler;
C_ShaderManager* C_Singleton::m_ShaderManager;
C_ModelManager* C_Singleton::m_ModelManager;
-C_GfxTimer* C_Singleton::m_GfxTimer;
+C_Timer* C_Singleton::m_Timer;
+C_PhysicsManager* C_Singleton::m_PhysicsManager;
C_Renderer* C_Singleton::M_Renderer()
{
@@ -26,10 +27,15 @@ C_ModelManager* C_Singleton::M_ModelManager()
if(!m_ModelManager) m_ModelManager = new C_ModelManager;
return m_ModelManager;
}
-C_GfxTimer* C_Singleton::M_GfxTimer()
+C_Timer* C_Singleton::M_Timer()
{
- if(!m_GfxTimer) m_GfxTimer = new C_GfxTimer;
- return m_GfxTimer;
+ if(!m_Timer) m_Timer = new C_Timer;
+ return m_Timer;
+}
+C_PhysicsManager* C_Singleton::M_PhysicsManager()
+{
+ if(!m_PhysicsManager) m_PhysicsManager = new C_PhysicsManager;
+ return m_PhysicsManager;
}
void C_Singleton::M_DestroySingletons()
@@ -38,6 +44,7 @@ void C_Singleton::M_DestroySingletons()
if(m_InputHandler) delete m_InputHandler;
if(m_ShaderManager) delete m_ShaderManager;
if(m_ModelManager) delete m_ModelManager;
- if(m_GfxTimer) delete m_GfxTimer;
+ if(m_Timer) delete m_Timer;
+ if(m_PhysicsManager) delete m_PhysicsManager;
}
View
9 singleton.h
@@ -3,7 +3,8 @@
#include "inputhandler.h"
#include "shadermanager.h"
#include "modelmanager.h"
-#include "gfxtimer.h"
+#include "timer.h"
+#include "physicsmanager.h"
#define DTGLIB_WINDOW_WIDTH 1280
#define DTGLIB_WINDOW_HEIGHT 800
@@ -15,13 +16,15 @@ class C_Singleton
static C_InputHandler* m_InputHandler;
static C_ShaderManager* m_ShaderManager;
static C_ModelManager* m_ModelManager;
- static C_GfxTimer* m_GfxTimer;
+ static C_Timer* m_Timer;
+ static C_PhysicsManager* m_PhysicsManager;
public:
static C_Renderer* M_Renderer();
static C_InputHandler* M_InputHandler();
static C_ShaderManager* M_ShaderManager();
static C_ModelManager* M_ModelManager();
- static C_GfxTimer* M_GfxTimer();
+ static C_Timer* M_Timer();
+ static C_PhysicsManager* M_PhysicsManager();
static void M_DestroySingletons();
};
View
10 timer.cpp
@@ -12,6 +12,16 @@ double C_Timer::M_Get() const
#endif
}
+double C_Timer::M_Now() const
+{
+ #ifdef _WIN32
+ return (GetTickCount64()) / 1000.0;
+ #else
+ struct timeval cur;
+ gettimeofday(&cur, NULL);
+ return (((cur.tv_sec*1000000)+(cur.tv_usec));
+}
+
void C_Timer::M_Reset()
{
#ifdef _WIN32
View
1  timer.h
@@ -20,5 +20,6 @@ class C_Timer
public:
C_Timer();
double M_Get() const;
+ double M_Now() const;
void M_Reset();
};
Please sign in to comment.
Something went wrong with that request. Please try again.