Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Camera follows player

  • Loading branch information...
commit 01dbfc773ae63009d13fdb334f4d1c27f09069b7 1 parent 34e0124
Antti Keränen authored
2  .ycm_extra_conf.py
@@ -11,7 +11,7 @@
11 11
12 12 # These are the compilation flags that will be used in case there's no
13 13 # compilation database set.
14   -flags = ["-Wall", "-Wextra", "-Werror", "-Wno-comment", "-std=c++0x", '-DUSE_CLANG_COMPLETER', '-x', 'c++']
  14 +flags = ["-Wall", "-Wextra", "-Werror", "-Wno-comment", "-std=c++0x", '-DUSE_CLANG_COMPLETER', '-x', 'c++', "-I", "."]
15 15
16 16 if compilation_database_folder:
17 17 database = ycm_core.CompilationDatabase( compilation_database_folder )
26 Makefile
... ... @@ -1,13 +1,33 @@
1   -CC=g++ -Os -Wall -Wextra -Werror -Wno-comment -std=c++0x
  1 +CC=g++ -g3 -Wall -Wextra -Werror -Wno-comment -std=c++0x
2 2 CFLAGS=-I/usr/local/include/GL -I/usr/local/include -L /usr/local/lib -L dtglib/
3 3 SERVERCFLAGS=-I/usr/local/include -L /usr/local/lib -Ldtglib/
4 4 SERVERLIBS=-ldtglib -lBox2D -lpthread
5 5 LIBS=-ldtglib -lGL -lGLU -lglfw -lXrandr -lBox2D -ldl -lpthread
6 6 OSXLIBS=-framework OpenGL -framework Cocoa -lglfw -lBox2D -ldtglib
7   -SOURCES=singleton.cpp renderer.cpp inputhandler.cpp shadermanager.cpp filereader.cpp modelmanager.cpp entity.cpp model.cpp gfxtimer.cpp timer.cpp
  7 +SOURCES=singleton.cpp \
  8 + renderer.cpp \
  9 + inputhandler.cpp \
  10 + shadermanager.cpp \
  11 + filereader.cpp \
  12 + modelmanager.cpp \
  13 + entity.cpp \
  14 + model.cpp \
  15 + gfxtimer.cpp \
  16 + timer.cpp \
  17 + packetparser.cpp
  18 +
8 19 MAIN=client.cpp
9 20 SERVERMAIN=server.cpp
10   -SERVERSOURCES=serversingleton.cpp filereader.cpp modelmanager.cpp model.cpp physicsmanager.cpp physicalentity.cpp timer.cpp ConnectionManager.cpp bullet.cpp contactlistener.cpp
  21 +SERVERSOURCES= serversingleton.cpp \
  22 + filereader.cpp \
  23 + modelmanager.cpp \
  24 + model.cpp \
  25 + physicsmanager.cpp \
  26 + physicalentity.cpp \
  27 + timer.cpp \
  28 + ConnectionManager.cpp \
  29 + bullet.cpp \
  30 + contactlistener.cpp
11 31 SERVERHEADERS=$(SERVERSOURCES:.cpp=.h)
12 32 SERVEROBJECTS=$(SERVERSOURCES:.cpp=.o)
13 33 HEADERS=$(SOURCES:.cpp=.h)
9 entity.cpp
... ... @@ -1,8 +1,8 @@
1 1 #include "singleton.h"
2 2 #include "entity.h"
3 3
4   -C_GfxEntity::C_GfxEntity() : m_Id(0), m_Model(), m_Scale(1.0f), m_ModelMatrix(glm::mat4(1.0f)) {}
5   -C_GfxEntity::C_GfxEntity(unsigned short id, const C_Model& m, float scale) : m_Id(id), m_Model(m), m_Scale(scale), m_ModelMatrix(glm::mat4(1.0f))
  4 +C_GfxEntity::C_GfxEntity() : m_Id(0), m_IsPlayer(false), m_Model(), m_Scale(1.0f), m_ModelMatrix(glm::mat4(1.0f)) {}
  5 +C_GfxEntity::C_GfxEntity(unsigned short id, const C_Model& m, float scale) : m_Id(id), m_IsPlayer(false), m_Model(m), m_Scale(scale), m_ModelMatrix(glm::mat4(1.0f))
6 6 {
7 7 M_Scale(scale);
8 8 glGenBuffers(1, &m_Vbo);
@@ -32,13 +32,11 @@ void C_GfxEntity::M_SetPosition(float x, float y)
32 32 m_Ex.M_Add(x);
33 33 m_Ey.M_Add(y);
34 34 m_TranslationMatrix = glm::translate(glm::mat4(1.0), glm::vec3(x/m_Scale,y/m_Scale,0.0f));
35   - //m_Pos.x=x*m_Scale;
36   - //m_Pos.y=y*m_Scale;
37 35 }
38 36
39 37 const C_Vec2 C_GfxEntity::GetPosition() const
40 38 {
41   - return C_Vec2(0,0);//m_Pos.x/m_Scale, m_Pos.y/m_Scale);
  39 + return C_Vec2(m_TranslationMatrix[3][0]*m_Scale, m_TranslationMatrix[3][1]*m_Scale);
42 40 }
43 41
44 42 void C_GfxEntity::M_ExtrapolatePosition(double dt)
@@ -48,7 +46,6 @@ void C_GfxEntity::M_ExtrapolatePosition(double dt)
48 46 float xxx = m_Ex.M_Current() + ((xx - m_Ex.M_Current()) * (dt/0.04));
49 47 float yyy = m_Ey.M_Current() + ((yy - m_Ey.M_Current()) * (dt/0.04));
50 48 m_TranslationMatrix = glm::translate(glm::mat4(1.0), glm::vec3(xxx/m_Scale,yyy/m_Scale,0.0f));
51   - //m_Pos.x=xxx*m_Scale; m_Pos.y=yyy*m_Scale;
52 49 }
53 50
54 51 void C_GfxEntity::M_Scale(float amount)
4 entity.h
@@ -2,6 +2,7 @@
2 2
3 3 #include "glm/glm.hpp"
4 4 #include "glm/gtc/matrix_transform.hpp"
  5 +#include "glm/gtc/matrix_access.hpp"
5 6 #include "model.h"
6 7 #include "vec2.h"
7 8 #include "ringbuffer.h"
@@ -10,6 +11,7 @@ class C_GfxEntity
10 11 {
11 12 private:
12 13 unsigned short m_Id;
  14 + bool m_IsPlayer;
13 15 C_Extrapolator<float> m_Ex;
14 16 C_Extrapolator<float> m_Ey;
15 17 //C_Vec2 m_Pos;
@@ -41,4 +43,6 @@ class C_GfxEntity
41 43 const glm::mat4& M_ModelMatrix();
42 44 void M_Draw() const;
43 45 unsigned int M_Id() const { return m_Id; }
  46 + bool IsPlayer() const { return m_IsPlayer; }
  47 + void SetPlayer(bool b) { m_IsPlayer=b; }
44 48 };
1  networkenum.h
@@ -6,6 +6,7 @@ struct NET
6 6 {
7 7 Connect,
8 8 Disconnect,
  9 + PlayerId,
9 10 ModelBegin,
10 11 ModelIndex,
11 12 ModelDimensions,
103 packetparser.cpp
... ... @@ -0,0 +1,103 @@
  1 +#include "packetparser.h"
  2 +
  3 +#include "singleton.h"
  4 +#include "model.h"
  5 +#include "networkenum.h"
  6 +#include <assert.h>
  7 +#include <vector>
  8 +
  9 +void C_PacketParser::M_Parse(C_Packet& p)
  10 +{
  11 + unsigned char header;
  12 + p >> header;
  13 + switch(header)
  14 + {
  15 + case NET::ModelBegin:
  16 + {
  17 + M_Model(p);
  18 + return;
  19 + }
  20 + case NET::EntityBegin:
  21 + {
  22 + M_GfxEntity(p, false);
  23 + return;
  24 + }
  25 + case NET::FullEntityBegin:
  26 + {
  27 + M_GfxEntity(p, true);
  28 + return;
  29 + }
  30 + case NET::EntityDeleted:
  31 + {
  32 + M_EntityDeleted(p);
  33 + return;
  34 + }
  35 + case NET::PlayerId:
  36 + {
  37 + unsigned short id;
  38 + p >> id;
  39 + C_Singleton::M_Renderer()->M_GetEntity(id)->SetPlayer(true);
  40 + return;
  41 + }
  42 + }
  43 + std::cout << "UNKNOWN HEADER: " << (int)header << std::endl;
  44 + assert(false);
  45 + return;
  46 +}
  47 +
  48 +void C_PacketParser::M_EntityDeleted(C_Packet& p)
  49 +{
  50 + unsigned short id;
  51 + p >> id;
  52 + C_Renderer* r=C_Singleton::M_Renderer();
  53 + r->M_DeleteEntity(r->M_GetEntity(id));
  54 +}
  55 +
  56 +void C_PacketParser::M_GfxEntity(C_Packet& p, bool full)
  57 +{
  58 + std::string name;
  59 + float scale,x,y,angle;
  60 + scale=x=y=angle=0.0f;
  61 + unsigned short id=65535;
  62 + if(full) p >> id >> name >> scale >> x >> y >> angle;
  63 + else p >> id >> x >> y >> angle;
  64 +
  65 + C_Renderer* r=C_Singleton::M_Renderer();
  66 + C_GfxEntity* e=r->M_GetEntity(id);
  67 + if(e)
  68 + {
  69 + e->M_SetPosition(x,y);
  70 + e->M_SetRotation(angle);
  71 + }
  72 + else if(full)
  73 + {
  74 + std::cout << "Creating entity with id: " << id << std::endl;
  75 + C_GfxEntity* e=C_GfxEntity::M_Create(id, C_Singleton::M_ModelManager()->M_Get(name), scale);
  76 + e->M_SetPosition(x,y);
  77 + e->M_SetRotation(angle);
  78 + }
  79 +}
  80 +
  81 +void C_PacketParser::M_Model(C_Packet& p)
  82 +{
  83 + unsigned char header;
  84 +
  85 + std::string name;
  86 + p >> name;
  87 +
  88 + std::vector<float> verts;
  89 + p >> header;
  90 + while(header == NET::ModelIndex)
  91 + {
  92 + float f;
  93 + p >> f;
  94 + verts.push_back(f);
  95 + p >> header;
  96 + }
  97 + assert(header == NET::ModelDimensions);
  98 + float w,h;
  99 + p >> w;
  100 + p >> h;
  101 +
  102 + C_Singleton::M_ModelManager()->M_Create(name, verts, w, h);
  103 +}
94 packetparser.h
... ... @@ -1,12 +1,6 @@
1 1 #pragma once
2 2
3   -#include "singleton.h"
4   -#include "model.h"
5   -#include "networkenum.h"
6   -#include <assert.h>
7   -#include <vector>
8 3 #include "dtglib/Packet.h"
9   -
10 4 using namespace dtglib;
11 5
12 6 class C_DummyParse {};
@@ -18,91 +12,3 @@ struct C_PacketParser
18 12 static void M_EntityDeleted(C_Packet& p);
19 13 static void M_Model(C_Packet& p);
20 14 };
21   -
22   -void C_PacketParser::M_Parse(C_Packet& p)
23   -{
24   - unsigned char header;
25   - p >> header;
26   - switch(header)
27   - {
28   - case NET::ModelBegin:
29   - {
30   - M_Model(p);
31   - return;
32   - }
33   - case NET::EntityBegin:
34   - {
35   - M_GfxEntity(p, false);
36   - return;
37   - }
38   - case NET::FullEntityBegin:
39   - {
40   - M_GfxEntity(p, true);
41   - return;
42   - }
43   - case NET::EntityDeleted:
44   - {
45   - M_EntityDeleted(p);
46   - return;
47   - }
48   - }
49   - std::cout << "UNKNOWN HEADER: " << (int)header << std::endl;
50   - assert(false);
51   - return;
52   -}
53   -
54   -void C_PacketParser::M_EntityDeleted(C_Packet& p)
55   -{
56   - unsigned short id;
57   - p >> id;
58   - C_Renderer* r=C_Singleton::M_Renderer();
59   - r->M_DeleteEntity(r->M_GetEntity(id));
60   -}
61   -
62   -void C_PacketParser::M_GfxEntity(C_Packet& p, bool full)
63   -{
64   - std::string name;
65   - float scale,x,y,angle;
66   - scale=x=y=angle=0.0f;
67   - unsigned short id=65535;
68   - if(full) p >> id >> name >> scale >> x >> y >> angle;
69   - else p >> id >> x >> y >> angle;
70   -
71   - C_Renderer* r=C_Singleton::M_Renderer();
72   - C_GfxEntity* e=r->M_GetEntity(id);
73   - if(e)
74   - {
75   - e->M_SetPosition(x,y);
76   - e->M_SetRotation(angle);
77   - }
78   - else if(full)
79   - {
80   - C_GfxEntity* e=C_GfxEntity::M_Create(id, C_Singleton::M_ModelManager()->M_Get(name), scale);
81   - e->M_SetPosition(x,y);
82   - e->M_SetRotation(angle);
83   - }
84   -}
85   -
86   -void C_PacketParser::M_Model(C_Packet& p)
87   -{
88   - unsigned char header;
89   -
90   - std::string name;
91   - p >> name;
92   -
93   - std::vector<float> verts;
94   - p >> header;
95   - while(header == NET::ModelIndex)
96   - {
97   - float f;
98   - p >> f;
99   - verts.push_back(f);
100   - p >> header;
101   - }
102   - assert(header == NET::ModelDimensions);
103   - float w,h;
104   - p >> w;
105   - p >> h;
106   -
107   - C_Singleton::M_ModelManager()->M_Create(name, verts, w, h);
108   -}
4 renderer.cpp
@@ -62,12 +62,10 @@ void C_Renderer::M_Draw()
62 62 {
63 63 if((*it)->M_ModelName() == "triangle" || (*it)->M_ModelName() == "bullet") M_Use(C_Singleton::M_ShaderManager()->M_Get("green"));
64 64 else M_Use(C_Singleton::M_ShaderManager()->M_Get("minimal"));
65   - /*
66   - if((*it)->M_ModelName() == "triangle")
  65 + if((*it)->IsPlayer())
67 66 {
68 67 SetView((*it)->GetPosition());
69 68 }
70   - */
71 69 glm::mat4 MVP=m_Projection*m_View*(*it)->M_ModelMatrix();
72 70 glUniformMatrix4fv(m_MVP, 1, GL_FALSE, glm::value_ptr(MVP));
73 71 (*it)->M_Draw();
23 server.cpp
@@ -43,15 +43,12 @@ int main()
43 43 if(!m->M_Load("box", "box.2dmodel")) exit(1);
44 44 if(!m->M_Load("bullet", "bullet.2dmodel")) exit(1);
45 45
46   - C_Entity* top=p->M_CreateDynamicEntity(m->M_Get("horizwall"), 2.0f);
47   - C_Entity* bottom=p->M_CreateDynamicEntity(m->M_Get("horizwall"), 2.0f);
48   - C_Entity* left=p->M_CreateDynamicEntity(m->M_Get("vertwall"), 2.0f);
49   - C_Entity* right=p->M_CreateDynamicEntity(m->M_Get("vertwall"), 2.0f);
  46 + C_Entity* top=p->M_CreateStaticEntity(m->M_Get("horizwall"), 2.0f);
  47 + C_Entity* bottom=p->M_CreateStaticEntity(m->M_Get("horizwall"), 2.0f);
  48 + C_Entity* left=p->M_CreateStaticEntity(m->M_Get("vertwall"), 2.0f);
  49 + C_Entity* right=p->M_CreateStaticEntity(m->M_Get("vertwall"), 2.0f);
  50 +
50 51 std::vector<C_Entity*> boxes;
51   - boxes.push_back(top);
52   - boxes.push_back(bottom);
53   - boxes.push_back(left);
54   - boxes.push_back(right);
55 52 std::vector<C_Entity*> players;
56 53
57 54 float x=-1.0f;
@@ -71,10 +68,10 @@ int main()
71 68 y-=2.0f/rows;
72 69 }
73 70
74   - top->M_SetPosition(0,1.0f);
75   - bottom->M_SetPosition(0,-1.0f);
76   - left->M_SetPosition(-1.6f,0.0f);
77   - right->M_SetPosition(1.6f,0.0f);
  71 + top->M_SetPosition(0,2.0f);
  72 + bottom->M_SetPosition(0,-2.0f);
  73 + left->M_SetPosition(-2.0f,0.0f);
  74 + right->M_SetPosition(2.0f,0.0f);
78 75
79 76 C_IpAddress ip; unsigned short port;
80 77 C_Packet packet;
@@ -131,6 +128,7 @@ int main()
131 128 m->M_Get("vertwall") >> packet;
132 129 m->M_Get("box") >> packet;
133 130 m->M_Get("bullet") >> packet;
  131 +
134 132 top->M_DumpFullInstance(packet);
135 133 bottom->M_DumpFullInstance(packet);
136 134 left->M_DumpFullInstance(packet);
@@ -145,6 +143,7 @@ int main()
145 143 {
146 144 (*it)->M_DumpFullInstance(packet);
147 145 }
  146 + packet << (unsigned char)NET::PlayerId << e->M_Id();
148 147 sock.M_Send(packet, ip, port);
149 148 packet.M_Clear();
150 149 players.push_back(e);

0 comments on commit 01dbfc7

Please sign in to comment.
Something went wrong with that request. Please try again.