Skip to content

Commit a90d693

Browse files
committed
Back up tiles before world views and everything stops working
1 parent 410fa74 commit a90d693

3 files changed

Lines changed: 129 additions & 9 deletions

File tree

‎cpp/typeid.cpp‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,32 @@ int main() {
3434
{
3535
int i, i1;
3636
int& ia = i;
37-
class Class {};
38-
Class c;
37+
assert(typeid(i) == typeid(int) );
38+
assert(typeid(ia) == typeid(int&));
39+
assert(typeid(i) == typeid(i1) );
40+
}
3941

40-
assert(typeid(i) == typeid(int) );
41-
assert(typeid(ia) == typeid(int&));
42-
assert(typeid(i) == typeid(i1) );
43-
assert(typeid(i) != typeid(c) );
42+
// Works differently for virtual and non virtual classes!
43+
// http://stackoverflow.com/questions/11484010/c-typeid-used-on-derived-class-doesnt-return-correct-type
44+
{
45+
class C {};
46+
class D : public C {};
47+
C c;
48+
D d;
49+
C *dp = &d;
50+
assert(typeid(C) == typeid(c) );
51+
assert(typeid(*dp) == typeid(C) );
52+
assert(typeid(*dp) != typeid(D) );
53+
54+
class PolyBase {
55+
void virtual f() {};
56+
};
57+
class PolyDerived : public PolyBase {
58+
void virtual f() {};
59+
};
60+
PolyDerived pd;
61+
PolyBase *pdp = &pd;
62+
assert(typeid(*pdp) == typeid(PolyDerived));
4463
}
4564

4665
/*

‎opengl/glfw_compute_shader_heat_equation.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,16 @@ int main(int argc, char **argv) {
315315
* Physics allows it to be in 0 / infinity.
316316
*
317317
* Anything greater than 1.0 leads to numeric instabilities
318-
* for our simplistic method, as equilibrium is reached in less
319-
* time than one time step, and goes over.
318+
* for our simplistic method. For example, the following:
319+
*
320+
* 1.0
321+
*
322+
* 1.0 0.0 1.0
323+
*
324+
* 1.0
325+
*
326+
* the center point goes above its surroundings on the next time step (2.0)
327+
* for a conduction coefficient of 2.0.
320328
*
321329
* Negative values make temperatures unbounded and breaks energy conservation.
322330
*

‎sdl/tiles.cpp‎

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
TODO:
77
88
- separate objects from controllers. Give controllers observed state instead of full world.
9-
- hold mode: no need to press keys, if you hold them input is taken for every frame
109
- redo previous human action
1110
- pause
1211
- use protobuf serialization for full world state, controller world view, and controller actions
@@ -27,6 +26,7 @@
2726
#include <ctime>
2827
#include <iostream>
2928
#include <memory>
29+
#include <typeinfo>
3030
#include <vector>
3131

3232
const unsigned int COLOR_MAX = 255;
@@ -87,6 +87,18 @@ class DrawableObject {
8787
Object *object;
8888
};
8989

90+
class WorldView {
91+
public:
92+
WorldView(unsigned int width, unsigned int height, std::unique_ptr<std::vector<std::unique_ptr<Object>>> objects) :
93+
width(width), height(height), objects(std::move(objects)) {}
94+
unsigned int getHeight() const { return this->height; }
95+
unsigned int getWidth() const { return this->width; }
96+
private:
97+
unsigned int width;
98+
unsigned int height;
99+
std::unique_ptr<std::vector<std::unique_ptr<Object>>> objects;
100+
};
101+
90102
class World {
91103
public:
92104
World(
@@ -104,6 +116,7 @@ class World {
104116
/// Collect desired actions from all objects, and resolve them
105117
unsigned int getHeight() const;
106118
unsigned int getNHumanActions() const;
119+
const std::vector<std::unique_ptr<Object>>& getObjects() const;
107120
SDL_Renderer * getRenderer() const;
108121
unsigned int getTileWidthPix() const;
109122
unsigned int getTileHeightPix() const;
@@ -130,6 +143,7 @@ class World {
130143
std::vector<SDL_Texture *> textures;
131144

132145
SDL_Texture * createSolidTexture(unsigned int r, unsigned int g, unsigned int b, unsigned int a);
146+
std::unique_ptr<WorldView> createWorldView(const std::unique_ptr<Object> &object) const;
133147
};
134148

135149
Object::Object(){}
@@ -198,6 +212,56 @@ class HumanPlayerObject : public Object {
198212
virtual bool takesHumanAction() const { return true; }
199213
};
200214

215+
/// Dumb: no understanding of walls.
216+
class FollowPlayerObject : public Object {
217+
public:
218+
FollowPlayerObject(unsigned int x, unsigned int y) : Object(x, y) {}
219+
virtual Action act(World *world) {
220+
Action a;
221+
for (auto const& object : world->getObjects()) {
222+
if (typeid(*object) == typeid(HumanPlayerObject)) {
223+
if (this->getX() < object->getX()) {
224+
a.setDirectionX(Action::DirectionX::X_RIGHT);
225+
} else if (this->getX() > object->getX()) {
226+
a.setDirectionX(Action::DirectionX::X_LEFT);
227+
}
228+
if (this->getY() < object->getY()) {
229+
a.setDirectionY(Action::DirectionY::Y_UP);
230+
} else if (this->getY() > object->getY()) {
231+
a.setDirectionY(Action::DirectionY::Y_DOWN);
232+
}
233+
break;
234+
}
235+
}
236+
return a;
237+
};
238+
};
239+
240+
/// Dumb: no understanding of walls.
241+
class FleePlayerObject : public Object {
242+
public:
243+
FleePlayerObject(unsigned int x, unsigned int y) : Object(x, y) {}
244+
virtual Action act(World *world) {
245+
Action a;
246+
for (auto const& object : world->getObjects()) {
247+
if (typeid(*object) == typeid(HumanPlayerObject)) {
248+
if (this->getX() < object->getX()) {
249+
a.setDirectionX(Action::DirectionX::X_LEFT);
250+
} else {
251+
a.setDirectionX(Action::DirectionX::X_RIGHT);
252+
}
253+
if (this->getY() < object->getY()) {
254+
a.setDirectionY(Action::DirectionY::Y_DOWN);
255+
} else {
256+
a.setDirectionY(Action::DirectionY::Y_UP);
257+
}
258+
break;
259+
}
260+
}
261+
return a;
262+
};
263+
};
264+
201265
class SingleTextureDrawableObject : public DrawableObject {
202266
public:
203267
SingleTextureDrawableObject(Object *object, SDL_Texture *texture) :
@@ -245,6 +309,8 @@ World::World(
245309
createSolidTexture(COLOR_MAX, COLOR_MAX, 0, 0);
246310
createSolidTexture(COLOR_MAX, 0, COLOR_MAX, 0);
247311
createSolidTexture(0, COLOR_MAX, COLOR_MAX, 0);
312+
createSolidTexture(COLOR_MAX, COLOR_MAX, COLOR_MAX, 0);
313+
createSolidTexture(COLOR_MAX / 2, COLOR_MAX / 2, COLOR_MAX / 2, 0);
248314
}
249315
this->initPhysics();
250316
}
@@ -331,6 +397,24 @@ void World::initPhysics() {
331397
}
332398
this->addObject(std::move(object));
333399
}
400+
{
401+
auto object = std::unique_ptr<Object>(new FollowPlayerObject(3 * this->getWidth() / 4, 3 * this->getHeight() / 4));
402+
if (display) {
403+
drawableObjects.push_back(std::unique_ptr<DrawableObject>(
404+
new SingleTextureDrawableObject(object.get(), this->textures[5])
405+
));
406+
}
407+
this->addObject(std::move(object));
408+
}
409+
{
410+
auto object = std::unique_ptr<Object>(new FleePlayerObject(this->getWidth() / 4, 3 * this->getHeight() / 4));
411+
if (display) {
412+
drawableObjects.push_back(std::unique_ptr<DrawableObject>(
413+
new SingleTextureDrawableObject(object.get(), this->textures[6])
414+
));
415+
}
416+
this->addObject(std::move(object));
417+
}
334418

335419
}
336420

@@ -343,6 +427,8 @@ void World::resetPhysics() {
343427
void World::update(const std::vector<std::unique_ptr<Action>> &humanActions) {
344428
auto humanActionsIt = humanActions.begin();
345429
for (auto &object : this->objects) {
430+
auto worldView = createWorldView(object);
431+
346432
Action a;
347433
if (object->takesHumanAction()) {
348434
a = **humanActionsIt;
@@ -381,6 +467,7 @@ void World::update(const std::vector<std::unique_ptr<Action>> &humanActions) {
381467

382468
unsigned int World::getHeight() const { return this->height; }
383469
unsigned int World::getNHumanActions() const { return this->nHumanActions; }
470+
const std::vector<std::unique_ptr<Object>>& World::getObjects() const { return this->objects; }
384471
SDL_Renderer * World::getRenderer() const { return this->renderer; }
385472
unsigned int World::getTileHeightPix() const { return this->tileHeightPix; }
386473
unsigned int World::getTileWidthPix() const { return this->tileWidthPix; }
@@ -405,6 +492,12 @@ SDL_Texture * World::createSolidTexture(unsigned int r, unsigned int g, unsigned
405492
return texture;
406493
}
407494

495+
// TODO.
496+
std::unique_ptr<WorldView> World::createWorldView(const std::unique_ptr<Object> &object) const {
497+
auto objects = std::unique_ptr<std::vector<std::unique_ptr<Object>>>();
498+
return std::unique_ptr<WorldView>(new WorldView(1, 2, std::move(objects)));
499+
}
500+
408501
void printHelp() {
409502
std::cerr <<
410503
"# CLI Options\n"

0 commit comments

Comments
 (0)