Skip to content

Commit a55aec9

Browse files
committed
started on script interface (rules, dynamic classes), work in progress
1 parent 32319ee commit a55aec9

15 files changed

+251
-62
lines changed

qor.vcxproj

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
<ClInclude Include="src\Settings.h" />
121121
<ClInclude Include="src\NodeAttributes.h" />
122122
<ClInclude Include="src\IMeshContainer.h" />
123+
<ClInclude Include="src\Item.h" />
123124
<ClInclude Include="src\Scene.h" />
124125
<ClInclude Include="src\EnvironmentNode.h" />
125126
<ClInclude Include="src\NodeFactory.h" />
@@ -145,6 +146,7 @@
145146
<ClInclude Include="src\IPhysicsObject.h" />
146147
<ClInclude Include="src\Shader.h" />
147148
<ClInclude Include="src\IRenderable.h" />
149+
<ClInclude Include="src\Inventory.h" />
148150
<ClInclude Include="src\Shadow.h" />
149151
<ClInclude Include="src\Node.h" />
150152
<ClInclude Include="src\DummyPartitioner.h" />
@@ -163,6 +165,7 @@
163165
<ClInclude Include="src\Console.h" />
164166
<ClInclude Include="src\RenderBuffer.h" />
165167
<ClInclude Include="src\ISuperParent.h" />
168+
<ClInclude Include="src\Rules.h" />
166169
<ClInclude Include="src\IStaticInstance.h" />
167170
<ClInclude Include="src\Util.h" />
168171
<ClInclude Include="src\IFallible.h" />

src/IConfig.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#define _ICONFIG_H
33

44
#include "PropertyList.h"
5-
#include "FileSystem.h"
5+
#include "Filesystem.h"
6+
#include "IFallible.h"
67

78
class IConfig
89
{
@@ -16,8 +17,8 @@ class IConfig
1617
virtual ~IConfig() {}
1718

1819
bool open(const std::string& fn) {
19-
if(FileSystem::hasExtension(fn,"ini"))
20-
m_Properties.open(fn.c_str());
20+
if(FS::hasExtension(fn,"ini"))
21+
return m_Properties.open(fn.c_str());
2122
}
2223

2324
const PropertyList& properties() const { return m_Properties; }

src/IScriptable.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#ifndef _SCRIPTABLE_H
22
#define _SCRIPTABLE_H
33

4-
class IScriptable
4+
#include <string>
5+
6+
class IScriptable: public IConfig
57
{
68
private:
79

810
public:
9-
IScriptable() {
11+
IScriptable(const std::string& fn):
12+
IConfig(fn)
13+
{
14+
open(fn);
15+
}
16+
bool open(const std::string& fn) {
17+
1018
}
1119
virtual ~IScriptable() {}
1220
};
1321

1422
#endif
23+

src/Indicator.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ void Indicator :: renderSelf(IPartitioner* partitioner, unsigned int flags) cons
77
if(!(flags & RENDER_AMBIENT_PASS))
88
return;
99

10+
if(!m_spTexture)
11+
return;
12+
1013
Renderer::get().shaders(Renderer::UNBIND_SHADERS);
1114
Renderer::get().lighting(Renderer::UNBIND_LIGHTING);
1215

src/Inventory.h

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef _INVENTORY_H
2+
#define _INVENTORY_H
3+
4+
#include <vector>
5+
6+
class TypeSpec;
7+
class Inventory
8+
{
9+
private:
10+
11+
// item primary type, quantity
12+
std::map<unsigned int, unsigned int> m_Contents;
13+
14+
public:
15+
16+
Inventory(unsigned int id) {}
17+
virtual ~Inventory() {}
18+
19+
bool has(unsigned int global_id) const {
20+
return m_Contents.find(global_id) != m_Contents.end();
21+
}
22+
23+
// adding directly does not follow rules
24+
bool add(unsigned int type, unsigned int quantity = 1) {
25+
if(m_Contents.find(type) == m_Contents.end())
26+
return false;
27+
m_Contents.push_back(type);
28+
return true;
29+
}
30+
31+
bool remove(unsigned int type) {
32+
auto itr = m_Contents.find(type);
33+
if(itr == m_Contents.end())
34+
return false;
35+
m_Contents.erase(itr);
36+
return true;
37+
}
38+
39+
bool anyOfType(const TypeSpec& type) const {
40+
// TODO: quick pass to check for primary classes of items,
41+
// then pass again for deep inspection
42+
return false;
43+
}
44+
45+
unsigned int numOfType(const TypeSpec& type) const {
46+
// TODO: Do a deep type inspection for each contents entry
47+
// and return all that match with type
48+
return 0;
49+
}
50+
51+
std::vector<unsigned int> getMatches(const TypeSpec& type) const {
52+
// TODO: get a list of each object matching the type
53+
// do deep inspection
54+
return std::vector<unsigned int>();
55+
}
56+
57+
bool empty() const { return m_Contents.empty(); }
58+
};
59+
60+
#endif
61+

src/Item.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef _ITEM_H
2+
#define _ITEM_H
3+
4+
class Item
5+
{
6+
public:
7+
8+
Item() {}
9+
virtual ~Item() {}
10+
};
11+
12+
#endif
13+

src/Navigation.h

+25-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,41 @@
44
#include <memory>
55
#include <functional>
66
#include <vector>
7+
#include <cmath>
78
#include "Freq.h"
9+
#include "math/common.h"
810
template<class T> class Navigation;
911

1012
struct LinearInterpolation
1113
{
1214
template<class T>
13-
T operator()(const T& a, const T& b, float t) const // where t ranges from 0..1
14-
{
15-
if(t <= 0.0f)
16-
return a;
17-
if(t >= 1.0f)
18-
return b;
15+
T operator()(const T& a, const T& b, float t) const {
1916
return a * (1.0f - t) + b * t;
2017
}
2118
};
2219

2320
struct CosineInterpolation
2421
{
2522
template<class T>
26-
T operator()(const T& a, const T& b, float t) const // where t ranges from 0..1
27-
{
28-
return T(); // TODO
23+
T operator()(const T& a, const T& b, float t) const {
24+
float ft = (1.0f - cos(M_PI * t)) * 0.5f;
25+
return a + (b - a) * ft;
26+
}
27+
};
28+
29+
struct SquareRootInterpolation
30+
{
31+
template<class T>
32+
T operator()(const T& a, const T& b, float t) const {
33+
return a + (b - a) * std::sqrt(t);
34+
}
35+
};
36+
37+
struct NoInterpolation
38+
{
39+
template<class T>
40+
T operator()(const T& a, const T& b, float t) const {
41+
return a;
2942
}
3043
};
3144

@@ -58,12 +71,13 @@ class Waypoint
5871
m_ulStartTime = Freq::get().getElapsedTime(); //ms
5972
m_ulAlarmTime = m_ulStartTime + time.get();
6073
}
74+
virtual ~Waypoint() {}
6175

62-
void poll() {
76+
virtual void poll() {
6377
// TODO: not necessary, but trigger callbacks if possible
6478
}
6579

66-
bool elapsed() {
80+
virtual bool elapsed() {
6781
poll();
6882
return Freq::get().getElapsedTime() >= m_ulAlarmTime;
6983
}

src/Physics.h

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <memory>
55
#include <newton/Newton.h>
6+
//#include <PxPhysicsAPI.h>
67
#include "math/common.h"
78
//#include <btBulletCollisionCommon.h>
89
//#include <btBulletDynamicsCommon.h>

src/ResourceCache.h

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ class ResourceCache
125125
typename std::map<std::string, std::shared_ptr<T>>::iterator itr;
126126
if((itr = m_Resources.find(name)) == m_Resources.end()) {
127127
T* r = (flags & F_PASS_NAME) ? new T(name) : new T(); // might throw
128+
//T* r = new T(name);
128129
m_Resources[name].reset(r);
129130
return m_Resources[name];
130131
}

src/Rules.h

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef _RULES_H
2+
#define _RULES_H
3+
4+
#include <vector>
5+
#include <boost/dynamic_bitset.hpp>
6+
// Rules
7+
// Lays out the game mechanics, items, weapons, and provides scriptable interface to them
8+
9+
// eventually stuff like this (less stringly tho, using ID system preferably):
10+
// rules->addType("rocket")
11+
// rules->classSpec("rocket")->inherit("ammo")
12+
// inventory->addItem("rocket", 5);
13+
// if (inventory->anyOfType("ammo")) {
14+
// // etc...
15+
// }
16+
17+
class ClassSpec
18+
{
19+
public:
20+
21+
ClassSpec(const std::string& name):
22+
m_sName(name)
23+
{}
24+
virtual ~ClassSpec() {}
25+
26+
bool inherit(unsigned int id) {
27+
if(m_Types.find(id) != m_Types.end())
28+
return false;
29+
m_Types.push_back(id);
30+
return true;
31+
}
32+
unsigned int id() const { return m_ID; }
33+
const std::string& name() const { return m_sName; }
34+
35+
private:
36+
37+
unsigned int m_ID; // ID of type, also index in Rules' class member m_Types
38+
39+
std::string m_sName; // name for scripting and lookup
40+
std::vector<unsigned int> m_Types; // inherited type info
41+
42+
// User data (varies depending on type)
43+
std::map<unsigned int, std::vector<unsigned int>> m_Tags; // type -> user tags for type
44+
std::map<unsigned int, std::string> m_TagNames;
45+
46+
boost::dynamic_bitset<> m_Flags;
47+
std::map<unsigned int, std::string> m_FlagNames;
48+
}
49+
50+
class Rules : public IScriptable
51+
{
52+
private:
53+
54+
public:
55+
56+
std::vector<ClassSpec> m_Types; // indexed by type id (array position)
57+
58+
Rules(std::string fn):
59+
IScriptable(fn)
60+
{
61+
m_Types.push_back(ClassSpec("object"));
62+
}
63+
virtual ~Rules() {}
64+
65+
unsigned int addType(ClassSpec spec) {
66+
m_Types.push_back(spec);
67+
return m_Types.size()-1;
68+
}
69+
70+
unsigned int type(std::string name) {
71+
for(auto itr = m_Types.begin();
72+
itr != m_Types.end();
73+
++itr)
74+
if(spec.name() == name)
75+
return 0;
76+
else
77+
return itr->id();
78+
}
79+
80+
// makes a new type that inherits from other types
81+
unsigned int inherit(std::string name, std::vector<unsigned int> types) {
82+
// TODO:
83+
return 0;
84+
}
85+
86+
// deep lookup if a type matches another type
87+
// warning: one-way relationship possible here
88+
// square is rectangle, rectangle is not always square, etc.
89+
bool isType(unsigned int type) const {
90+
91+
}
92+
};
93+
94+
#endif
95+

src/Scene.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ class Scene : public IFallible, public IRealtime
5555
std::vector<std::shared_ptr<Mesh>> meshes;
5656
};
5757

58-
//std::map<std::string, Texture*> m_Textures;
59-
//std::map<std::string, Mesh*> m_MeshPool;
60-
6158
ResourceCache<Mesh> m_Meshes;
62-
//ResourceCache<Material> m_Materials;
6359
ResourceCache<Texture> m_Textures;
60+
//ResourceCache<Buffer> m_Sounds;
61+
//ResourceCache<Stream> m_Streams;
6462

6563
//Camera* m_pCamera;
6664
std::unique_ptr<Physics> m_spPhysics;

src/Texture.cpp

+7-11
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
#include <string>
66
#include "Renderer.h"
77

8-
unsigned int Texture :: loadTex(std::string fn,
9-
//TransOptions trans,
10-
MipmapOptions mip,
11-
WrapOptions wrap,
12-
FlipOptions flip)
8+
unsigned int Texture :: loadTex(std::string fn, unsigned int flags)
139
{
1410
//return 1; // bypass textures
1511

@@ -58,18 +54,18 @@ unsigned int Texture :: loadTex(std::string fn,
5854
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, filter);
5955

6056

61-
if(wrap==REPEAT)
57+
if(flags & CLAMP)
6258
{
63-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
64-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
59+
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
60+
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
6561
}
6662
else
6763
{
68-
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
69-
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
64+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
65+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
7066
}
7167

72-
if(mip){
68+
if(flags & MIPMAP){
7369

7470
// GLU version:
7571
// gluBuild2DMipmaps(GL_TEXTURE_2D,ilGetInteger(IL_IMAGE_BPP),ilGetInteger(IL_IMAGE_WIDTH),

0 commit comments

Comments
 (0)