aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-10-08 03:03:16 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-10-08 03:03:16 -0400
commita422f32613441b5313e4a3bb0fab61f8cb87914c (patch)
tree0a6c8f17e6a8df0b6c377813fa700064f68ced93
parent5f53889d4357d8dba6e726ed38358eca96dbeb47 (diff)
parent5b1c22529a946a782a8376de2b34c28348d078d1 (diff)
Using polymorphism for world layer storage now, and added demo backdrops
-rw-r--r--Assets/world/world1/layers/0/forestTileBack.pngbin0 -> 27599 bytes
-rw-r--r--Assets/world/world1/layers/deco/forestTileBack.pngbin0 -> 19045 bytes
-rw-r--r--Assets/world/world1/layers/deco/forestTileFront.pngbin0 -> 15814 bytes
-rw-r--r--Assets/world/world1/layers/deco/forestTileMid.pngbin0 -> 18365 bytes
-rw-r--r--Assets/world/world1/layers/deco/normal.pngbin4243 -> 146 bytes
-rw-r--r--Makefile5
-rw-r--r--Scripts/init.lua9
-rw-r--r--Scripts/world.lua125
-rw-r--r--src/components/Physics.hpp30
-rw-r--r--src/components/Position.hpp13
-rw-r--r--src/engine.cpp10
-rw-r--r--src/render.cpp3
-rw-r--r--src/script.cpp19
-rw-r--r--src/script.hpp15
-rw-r--r--src/text.cpp22
-rw-r--r--src/text.hpp2
-rw-r--r--src/world.cpp88
-rw-r--r--src/world.hpp13
18 files changed, 179 insertions, 175 deletions
diff --git a/Assets/world/world1/layers/0/forestTileBack.png b/Assets/world/world1/layers/0/forestTileBack.png
new file mode 100644
index 0000000..f1c1a8b
--- /dev/null
+++ b/Assets/world/world1/layers/0/forestTileBack.png
Binary files differ
diff --git a/Assets/world/world1/layers/deco/forestTileBack.png b/Assets/world/world1/layers/deco/forestTileBack.png
new file mode 100644
index 0000000..c5e9dac
--- /dev/null
+++ b/Assets/world/world1/layers/deco/forestTileBack.png
Binary files differ
diff --git a/Assets/world/world1/layers/deco/forestTileFront.png b/Assets/world/world1/layers/deco/forestTileFront.png
new file mode 100644
index 0000000..234520c
--- /dev/null
+++ b/Assets/world/world1/layers/deco/forestTileFront.png
Binary files differ
diff --git a/Assets/world/world1/layers/deco/forestTileMid.png b/Assets/world/world1/layers/deco/forestTileMid.png
new file mode 100644
index 0000000..b51d0bc
--- /dev/null
+++ b/Assets/world/world1/layers/deco/forestTileMid.png
Binary files differ
diff --git a/Assets/world/world1/layers/deco/normal.png b/Assets/world/world1/layers/deco/normal.png
index c0aab94..767f7e3 100644
--- a/Assets/world/world1/layers/deco/normal.png
+++ b/Assets/world/world1/layers/deco/normal.png
Binary files differ
diff --git a/Makefile b/Makefile
index 9c9fafe..f79435c 100644
--- a/Makefile
+++ b/Makefile
@@ -82,4 +82,7 @@ $(OUTDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT)
@sed -e 's/.*://' -e 's/\\$$//' < $(OUTDIR)/$*.$(DEPEXT).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(OUTDIR)/$*.$(DEPEXT)
@rm -f $(OUTDIR)/$*.$(DEPEXT).tmp
-.PHONY: all remake clean cleaner resources
+mem: $(EXEC)
+ valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$(EXEC)
+
+.PHONY: all remake clean cleaner resources mem
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 46fe040..da2517f 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -35,12 +35,11 @@ player = {
y = 0.0
},
Physics = {
- gravity = true,
hitbox = {
- {x = -0.5, y = -0.8},
- {x = 0.5, y = -0.8},
- {x = -0.5, y = 0.8},
- {x = 0.5, y = 0.8},
+ ll = {x = -0.5, y = -0.8},
+ lr = {x = 0.5, y = -0.8},
+ ul = {x = -0.5, y = 0.8},
+ ur = {x = 0.5, y = 0.8},
}
},
Name = "bord",
diff --git a/Scripts/world.lua b/Scripts/world.lua
index f1d2b66..1fb74d2 100644
--- a/Scripts/world.lua
+++ b/Scripts/world.lua
@@ -11,126 +11,21 @@ newWorld = {
normal = { file = "Assets/world/world1/layers/1/normal.png" },
hitbox = "Assets/world/world1/layers/1/hitbox.png"
});
- self:createDecoLayer(10, {
- texture = { file = "Assets/world/world1/layers/deco/texture.png" },
+ self:createDecoLayer(7, {
+ texture = { file = "Assets/world/world1/layers/deco/forestTileBack.png" },
+ normal = { file = "Assets/world/world1/layers/deco/normal.png" },
+ });
+ self:createDecoLayer(5.5, {
+ texture = { file = "Assets/world/world1/layers/deco/forestTileMid.png" },
+ normal = { file = "Assets/world/world1/layers/deco/normal.png" },
+ });
+ self:createDecoLayer(4, {
+ texture = { file = "Assets/world/world1/layers/deco/forestTileFront.png" },
normal = { file = "Assets/world/world1/layers/deco/normal.png" },
});
end,
Generate = function(self)
- print("Generating");
end
}
game.worldRegister(newWorld);
-
---world = {
--- Seed = 5345345,
--- Layers = 2,
---
--- -- This is run when the world is registered and not after,
--- -- although it is possible to register materials later
--- Register = function(self)
---
--- -- TODO make world have global textures to speed up rendering
--- self:registerMaterial("grass", {
--- -- TODO combine both of these into 1
--- texture = {
--- file = "Assets/world.png",
--- offset = { x = 0, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- normal = {
--- file = "Assets/world_normal.png",
--- offset = { x = 0, y = 0 },
--- size = { x = 64, y = 64 }
--- }
--- });
--- self:registerMaterial("dirt", {
--- texture = {
--- file = "Assets/world.png",
--- offset = { x = 64, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- normal = {
--- file = "Assets/world_normal.png",
--- offset = { x = 64, y = 0 },
--- size = { x = 64, y = 64 }
--- }
--- });
--- self:registerMaterial("stone", {
--- texture = {
--- file = "Assets/world.png",
--- offset = { x = 128, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- normal = {
--- file = "Assets/world_normal.png",
--- offset = { x = 128, y = 0 },
--- size = { x = 64, y = 64 }
--- }
--- });
--- self:registerMaterial("flower", {
--- texture = {
--- file = "Assets/world.png",
--- offset = { x = 192, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- normal = {
--- file = "Assets/world_normal.png",
--- offset = { x = 192, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- passable = true
--- });
--- self:registerMaterial("trunk", {
--- texture = {
--- file = "Assets/world.png",
--- offset = { x = 256, y = 0 },
--- size = { x = 64, y = 64 }
--- },
--- normal = {
--- file = "Assets/world_normal.png",
--- offset = { x = 256, y = 0 },
--- size = { x = 64, y = 64 }
--- }
--- });
--- end,
---
--- Generate = function(self)
--- math.randomseed(self.Seed)
--- xsize, ysize, zsize = self:setSize(250, 128, 3)
--- for Z = 0,zsize-1 do
--- for X = 0,xsize-1 do
--- if Z == 0 then
--- YGen = math.floor(6*math.sin(X/20)) + 64
--- elseif Z == 1 then
--- YGen = math.floor(9*math.sin(X/20)) + 64
--- else
--- YGen = math.floor(15*math.sin(X/20)) + 64
--- end
--- YDepth = math.random(3,5)
--- for Y = 0,ysize-1 do
--- if Y == YGen then
--- self:setData(X, Y, Z, "grass");
--- elseif Y < YGen and Y > (YGen - YDepth) then
--- self:setData(X, Y, Z, "dirt");
--- elseif Y < YGen then
--- self:setData(X, Y, Z, "stone");
--- elseif Y == YGen + 1 then
--- if math.random(0, 100) == 53 then
--- self:setData(X, Y, Z, "flower");
--- elseif math.random(0, 100) == 45 then
--- self:setData(X, Y, Z, "trunk");
--- end
--- end
--- --print(X..","..Y..","..Z);
--- end
--- end
--- end
--- self:setData(1000, 1345, 5, "grass"); -- Test error checking
--- print("Done with world gen");
--- end
---}
-
---world:Generate()
---game.worldRegister(world)
diff --git a/src/components/Physics.hpp b/src/components/Physics.hpp
index 378e87f..cb4d08a 100644
--- a/src/components/Physics.hpp
+++ b/src/components/Physics.hpp
@@ -25,8 +25,38 @@ struct Physics : Component<Physics>
{
public:
bool standing = true;
+ bool gravity = true;
+ glm::vec2 corners[4] = {
+ glm::vec2(-0.5, -0.5), // lower left
+ glm::vec2( 0.5, -0.5), // lower right
+ glm::vec2(-0.5, 0.5), // upper left
+ glm::vec2( 0.5, 0.5) // upper right
+ };
+
Physics FromLua([[maybe_unused]] sol::object ref)
{
+ if (ref.get_type() == sol::type::table) {
+ sol::table tab = ref;
+
+ if (tab["gravity"].get_type() == sol::type::boolean)
+ this->gravity = tab["gravity"];
+
+ if (tab["hitbox"].get_type() == sol::type::table) {
+ sol::table hitbox = tab["hitbox"];
+ if (hitbox["ll"] == sol::type::table)
+ corners[0] = Script::to<glm::vec2>(hitbox["ll"]);
+ if (hitbox["lr"] == sol::type::table)
+ corners[1] = Script::to<glm::vec2>(hitbox["lr"]);
+ if (hitbox["ul"] == sol::type::table)
+ corners[2] = Script::to<glm::vec2>(hitbox["ul"]);
+ if (hitbox["ur"] == sol::type::table)
+ corners[3] = Script::to<glm::vec2>(hitbox["ur"]);
+ }
+ } else {
+ throw std::string(
+ "Physics component table formatted incorrectly"
+ );
+ }
return *this;
}
diff --git a/src/components/Position.hpp b/src/components/Position.hpp
index fc5cb9a..bfa4b41 100644
--- a/src/components/Position.hpp
+++ b/src/components/Position.hpp
@@ -24,23 +24,24 @@
struct Position : Component<Position>
{
public:
- float x, y;
+ float x, y, z;
- Position(float _x = 0, float _y = 0) :
- x(_x), y(_y) {}
+ Position(float _x = 0, float _y = 0, float _z = 0) :
+ x(_x), y(_y), z(_z) {}
Position FromLua(sol::object ref)
{
- glm::vec2 vec = Script::to<glm::vec2>(ref);
+ glm::vec3 vec = Script::to<glm::vec3>(ref);
this->x = vec.x;
this->y = vec.y;
+ this->z = vec.z;
return *this;
}
- glm::vec2 vec()
+ glm::vec3 vec()
{
- return glm::vec2(x, y);
+ return glm::vec3(x, y, z);
}
void serialize(cereal::JSONOutputArchive& ar) final {
diff --git a/src/engine.cpp b/src/engine.cpp
index b1d9a56..81e0272 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -59,13 +59,11 @@ int Engine::init(void)
// Load game script and entity data
auto* script = systems.system<ScriptSystem>().get();
script->addToGameNamespace("loadFont",
- [this](std::string name, std::string file, int size) {
- systems.system<TextSystem>().get()->loadFont(name, file, size);
- });
+ bindInstance(&TextSystem::loadFont,
+ systems.system<TextSystem>().get()));
script->addToGameNamespace("puts",
- [this](std::string name, float x, float y, std::string text) {
- systems.system<TextSystem>().get()->put(name, x, y, text);
- });
+ bindInstance(&TextSystem::put,
+ systems.system<TextSystem>().get()));
script->init();
diff --git a/src/render.cpp b/src/render.cpp
index 7d9f9e9..9362134 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -339,9 +339,8 @@ int RenderSystem::init(void)
context = SDL_GL_CreateContext(window.get());
- GLenum err;
glewExperimental = GL_TRUE;
- if((err=glewInit()) != GLEW_OK){
+ if (auto err = glewInit(); err != GLEW_OK){
std::cerr << "GLEW was not able to initialize! Error: " <<
glewGetErrorString(err) << std::endl;
return -1;
diff --git a/src/script.cpp b/src/script.cpp
index 4cfcddd..4fda543 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -87,16 +87,11 @@ void ScriptSystem::doFile(void)
void ScriptSystem::scriptExport(void)
{
- std::function<sol::table(sol::table)> entitySpawn =
- [this](sol::table t){ return spawn(t);};
-
- std::function<World* (sol::object)> worldRegister =
- [this](sol::object t){ return worldSystem.addWorld(t); };
-
lua.new_usertype<Position>("Position",
- sol::constructors<Position(float x, float y), Position()>(),
+ sol::constructors<Position(float x, float y, float z), Position()>(),
"x", &Position::x,
- "y", &Position::y);
+ "y", &Position::y,
+ "z", &Position::z);
lua.new_usertype<Name>("Name",
sol::constructors<Name(std::string), Name()>(),
@@ -125,7 +120,8 @@ void ScriptSystem::scriptExport(void)
lua.new_usertype<Physics>("Physics",
sol::constructors<Physics(void), Physics()>(),
- "standing", &Physics::standing);
+ "standing", &Physics::standing,
+ "gravity", &Physics::gravity);
lua.new_usertype<World>("World",
sol::constructors<World(sol::object), World(void)>(),
@@ -139,8 +135,9 @@ void ScriptSystem::scriptExport(void)
"createDecoLayer", &World::registerDecoLayer);
game = lua["game"].get_or_create<sol::table>();
- game.set_function("spawn", entitySpawn);
- game.set_function("worldRegister", worldRegister);
+ game.set_function("spawn", bindInstance(&ScriptSystem::spawn, this));
+ game.set_function("worldRegister", bindInstance(&WorldSystem::addWorld,
+ &worldSystem));
}
sol::table ScriptSystem::spawn(sol::object param)
diff --git a/src/script.hpp b/src/script.hpp
index 0ac9e63..24cc142 100644
--- a/src/script.hpp
+++ b/src/script.hpp
@@ -26,6 +26,21 @@
#include "world.hpp"
+/**
+ * Utility for pairing class instances to their member function calls.
+ * This is useful for adding functions to the Lua game namespace.
+ *
+ * @param func The member function to call
+ * @param instance The instance to bind to
+ * @return A function that calls the member function using the given instance
+ */
+template<class C, typename R, typename... Args>
+auto bindInstance(R (C::* func)(Args...), C *instance)
+{
+ return [instance, func](Args... args) { (instance->*func)(args...); };
+}
+
+
struct EntitySpawnEvent
{
sol::object ref;
diff --git a/src/text.cpp b/src/text.cpp
index 6917a2c..fb82875 100644
--- a/src/text.cpp
+++ b/src/text.cpp
@@ -4,6 +4,14 @@
#include <iostream>
+TextSystem::~TextSystem(void)
+{
+ for (auto [name, face] : fonts)
+ FT_Done_Face(face);
+
+ FT_Done_FreeType(freetype);
+}
+
void TextSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events)
{
@@ -25,12 +33,11 @@ void TextSystem::update([[maybe_unused]] entityx::EntityManager& entites,
shouldUpdateVBOs = false;
updateVBOs();
- for (auto& data : fontData) {
- auto& d = data.second;
- if (d.text.size() == 0)
- continue;
-
- events.emit<NewRenderEvent>(d.vbo, d.tex, 0, d.buffer.size());
+ for (auto& [name, font] : fontData) {
+ if (font.text.size() != 0) {
+ events.emit<NewRenderEvent>(font.vbo, font.tex, 0,
+ font.buffer.size());
+ }
}
}
}
@@ -145,8 +152,7 @@ void TextSystem::put(const std::string& font,
void TextSystem::updateVBOs(void)
{
- for (auto& data : fontData) {
- auto& d = data.second;
+ for (auto& [name, d] : fontData) {
d.buffer.clear();
for (auto& text : d.text) {
float tx = text.x;
diff --git a/src/text.hpp b/src/text.hpp
index 7cf90b2..1ef2afa 100644
--- a/src/text.hpp
+++ b/src/text.hpp
@@ -77,6 +77,8 @@ struct Font {
class TextSystem : public entityx::System<TextSystem>
{
public:
+ ~TextSystem(void);
+
/**
* Prepares the system for running.
*/
diff --git a/src/world.cpp b/src/world.cpp
index fb846eb..8987eac 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -68,17 +68,17 @@ World::getSize()
/* RENDERING */
void World::generateMesh()
{
- for (auto &l : solidLayers) {
+ for (auto &l : drawLayers) {
// Preallocate size of vertexes
- float Z = l.drawLayer;
- auto to = l.texture.offset;
- auto ts = l.texture.size;
+ float Z = l->drawLayer;
+ auto to = l->texture.offset;
+ auto ts = l->texture.size;
float tr = 1.0f;
- float w = l.texture.width/unitSize;
- float h = l.texture.height/unitSize;
+ float w = l->texture.width/unitSize;
+ float h = l->texture.height/unitSize;
GLfloat mesh[36] = {0 , 0 , Z, to.x , to.y+ts.y, tr,
0+w, 0 , Z, to.x+ts.x, to.y+ts.y, tr,
@@ -88,21 +88,17 @@ void World::generateMesh()
0+w, 0+h, Z, to.x+ts.x, to.y , tr,
0 , 0+h, Z, to.x , to.y , tr};
- glBindBuffer(GL_ARRAY_BUFFER, l.layerVBO);
+ glBindBuffer(GL_ARRAY_BUFFER, l->layerVBO);
glBufferData(GL_ARRAY_BUFFER,
36 * sizeof(GLfloat),
mesh,
GL_STATIC_DRAW);
- meshAdd.push_back(WorldMeshUpdateEvent(l.layerVBO,
- l.texture.tex,
- l.normal.tex,
+ meshAdd.push_back(WorldMeshUpdateEvent(l->layerVBO,
+ l->texture.tex,
+ l->normal.tex,
36));
}
-
- for (auto &l : drawLayers) {
- (void)l;
- }
}
/* SEED */
@@ -123,21 +119,73 @@ double World::getHeight(double x, double y, double z)
(void)y;
double Y = 0.0f;
for (auto &l : solidLayers) {
- if (z == l.drawLayer) {
+ if (z == l->drawLayer) {
int wx = x*unitSize;
int h = 0.0;
- for (auto b : l.hitbox[wx]) {
+ for (auto b : l->hitbox[wx]) {
if (b == true)
Y = h;
h++;
}
- return (Y/unitSize);
+ return ((Y+1)/unitSize);
}
}
return 0;
}
+std::vector<std::pair<glm::vec2, glm::vec2>>
+World::getIntersectingPlanes(glm::vec2 origin, glm::vec2 dest)
+{
+ (void)origin;
+ (void)dest;
+ //glm::ivec2 worldOrigin = origin*unitSize;
+ //glm::ivec2 worldDest = dest*unitSize;
+
+ return std::vector<std::pair<glm::vec2, glm::vec2>>();
+}
+
+glm::vec3 World::collide(glm::vec3 &start, glm::vec3 &end, Physics &phys)
+{
+ (void)start;
+ (void)end;
+ (void)phys;
+ for (auto &l : solidLayers) {
+ if (end.z == l->drawLayer) {
+ glm::vec2 len = end-start;
+ glm::vec2 dir = glm::normalize(len);
+ float step = 1.0f/unitSize;
+
+ // TODO move this
+ glm::vec2 pos = start;
+
+ for (float i = 0; i < len.length(); i+=step) {
+ pos += dir;
+
+ if (dir.x > 0.0f) {
+ // Moving to the right
+ //glm::vec2 origin = pos + phys.corners[1]; // bottom right
+ //glm::vec2 orDir = glm::vec2(0, 1);
+
+ } else if (dir.x < 0.0f) {
+ // Moving to the left
+
+ }
+
+ if (dir.y > 0.0f) {
+ // Moving upwards
+
+ } else if (dir.y < 0.0f) {
+ // Moving downwards
+
+ }
+ }
+ }
+ }
+ return glm::vec3(0);
+}
+
+
/*********
* NEW *
*********/
@@ -145,7 +193,9 @@ void World::registerLayer(float z, sol::object obj)
{
if (obj.get_type() == sol::type::table) {
sol::table tab = obj;
- solidLayers.push_back(SolidLayer(z, tab));
+ SolidLayer s(z, tab);
+ solidLayers.push_back(std::make_shared<SolidLayer>(s));
+ drawLayers.push_back(std::make_shared<Layer>(s));
} else {
throw std::string("Layer must receive a table");
}
@@ -156,7 +206,7 @@ void World::registerDecoLayer(float z, sol::object obj)
{
if (obj.get_type() == sol::type::table) {
sol::table tab = obj;
- drawLayers.push_back(Layer(z, tab));
+ drawLayers.push_back(std::make_shared<Layer>(Layer(z, tab)));
} else {
throw std::string("Layer must receive a table");
}
diff --git a/src/world.hpp b/src/world.hpp
index 66d9e3f..0c1275e 100644
--- a/src/world.hpp
+++ b/src/world.hpp
@@ -31,6 +31,11 @@
#include "texture.hpp"
#include "events/render.hpp"
+#include <components/Position.hpp> // For entity position
+#include <components/Velocity.hpp> // For entity velocity
+#include <components/Physics.hpp> // For entity hitbox(es)
+
+
struct WorldMaterial
{
bool passable = false;
@@ -120,8 +125,11 @@ private:
unsigned int layers;
unsigned int unitSize;
- std::vector<SolidLayer> solidLayers;
- std::vector<Layer> drawLayers;
+ std::vector<std::shared_ptr<SolidLayer>> solidLayers;
+ std::vector<std::shared_ptr<Layer>> drawLayers;
+
+ std::vector<std::pair<glm::vec2, glm::vec2>>
+ getIntersectingPlanes(glm::vec2 origin, glm::vec2 dir);
protected:
// RENDER
@@ -150,6 +158,7 @@ public:
/* PHYSICS */
double getHeight(double x, double y, double z);
+ glm::vec3 collide(glm::vec3 &start, glm::vec3 &end, Physics &phys);
// NEW
unsigned int getUnitSize() {return unitSize;}