aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-01 01:25:14 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-01 01:25:14 -0400
commitf2411141e364766b1294f1b3d9e1a307b9de0c24 (patch)
tree3fb80cf5a4357f0b060035ef8c8e4f6fa191bb2f
parentefec4f7b42b12d4765da5a886fcf2c5279c8caaf (diff)
Game loop now updates position every tick, and added circular movement to dog
-rw-r--r--Scripts/init.lua14
-rw-r--r--src/components/Position.hpp4
-rw-r--r--src/components/Render.hpp6
-rw-r--r--src/components/Velocity.hpp4
-rw-r--r--src/engine.cpp44
-rw-r--r--src/render.cpp51
-rw-r--r--src/render.hpp2
-rw-r--r--src/script.cpp6
8 files changed, 90 insertions, 41 deletions
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 596fe38..ad69ae6 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -12,12 +12,12 @@ bird = {
dog = {
Velocity = {
- x = 0.01,
- y = 10.5
+ x = 0.0,
+ y = 0.0
},
Position = {
- x = 6.5,
- y = 1.3
+ x = 50,
+ y = 0
},
Render = {
texture = "assets/tex.png",
@@ -26,8 +26,11 @@ dog = {
Init = function(self)
print(self.Position.x .. "," .. self.Position.y)
end,
+ counter = 0;
Idle = function(self)
- self.Position.x = self.Position.x + 0.01;
+ self.Velocity.x = -100 * math.sin(math.rad(self.counter));
+ self.Velocity.y = 100 * math.cos(math.rad(self.counter));
+ self.counter = self.counter + 5;
end
}
@@ -41,7 +44,6 @@ animal = {
birdSpawn = game.spawn(bird);
dogSpawn = game.spawn(dog);
-dogSpawn.Position.x = 37.5
animalSpawn = game.spawn(animal);
print("Animal pos: " .. animalSpawn.Position.x)
diff --git a/src/components/Position.hpp b/src/components/Position.hpp
index 0462d1a..8a6bd74 100644
--- a/src/components/Position.hpp
+++ b/src/components/Position.hpp
@@ -24,8 +24,8 @@
struct Position : Component<Position>, entityx::Component<Position>
{
public:
- float x, y;
- Position(float _x, float _y): x(_x), y(_y) {}
+ double x, y;
+ Position(double _x, double _y): x(_x), y(_y) {}
Position(void): x(0), y(0) {}
Position FromLua(sol::object ref)
diff --git a/src/components/Render.hpp b/src/components/Render.hpp
index 4444fef..5a9b1a4 100644
--- a/src/components/Render.hpp
+++ b/src/components/Render.hpp
@@ -15,8 +15,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#ifndef RENDER_HPP_
-#define RENDER_HPP_
+#ifndef RENDERC_HPP_
+#define RENDERC_HPP_
#include <components/Component.hpp>
@@ -49,4 +49,4 @@ struct Render : Component<Render>, entityx::Component<Render>
};
-#endif//RENDER_HPP_
+#endif//RENDERC_HPP_
diff --git a/src/components/Velocity.hpp b/src/components/Velocity.hpp
index eacadf2..c2b85e8 100644
--- a/src/components/Velocity.hpp
+++ b/src/components/Velocity.hpp
@@ -25,9 +25,9 @@
struct Velocity : Component<Velocity>, entityx::Component<Velocity>
{
public:
- float x, y;
+ double x, y;
Velocity(): x(0), y(0) {}
- Velocity(float _x, float _y): x(_x), y(_y) {}
+ Velocity(double _x, double _y): x(_x), y(_y) {}
Velocity FromLua(sol::object ref)
{
diff --git a/src/engine.cpp b/src/engine.cpp
index a89a876..4579eb7 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -27,6 +27,7 @@
#include "components/Script.hpp"
#include "components/Position.hpp"
+#include "components/Velocity.hpp"
int Engine::init(void)
{
@@ -42,17 +43,48 @@ int Engine::init(void)
void Engine::logicLoop(void)
{
using namespace std::chrono_literals;
+ typedef std::chrono::high_resolution_clock mc;
- entityx::TimeDelta dt = 0;
+ entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */
+ double elapsed = 0;
while (shouldRun()) {
- systems.update<InputSystem>(dt);
- // All entities with an idle function should be run here
- entities.each<Scripted>([](entityx::Entity, Scripted &f){
- f.exec();
+ auto start = mc::now();
+
+ /***********************
+ * UPDATE FREQUENTLY *
+ ***********************/
+
+ entities.each<Position, Velocity>
+ ([&](entityx::Entity, Position &p, Velocity &v){
+ p.x += (v.x * dt/1000.0);
+ p.y += (v.y * dt/1000.0);
});
- std::this_thread::sleep_for(100ms);
+
+ systems.update<InputSystem>(dt);
+
+ /*******************
+ * LOGIC UPDATES *
+ *******************/
+
+ // Update 20 times a second
+ if (elapsed > 50) {
+ elapsed = 0;
+
+ // All entities with an idle function should be run here
+ entities.each<Scripted>([](entityx::Entity, Scripted &f){
+ f.exec();
+ });
+ }
+
+ auto end = mc::now();
+ auto diff = end - start;
+ auto micros = cr::duration_cast<cr::microseconds>(diff);
+ auto msc = micros.count();
+ dt = static_cast<double>(msc)/1000.0;
+ elapsed += dt;
+ std::this_thread::yield();
}
// Remove all Lua references from entities
diff --git a/src/render.cpp b/src/render.cpp
index 30fd396..5b3a1f1 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -19,6 +19,8 @@
*/
#include <render.hpp>
+#include <components/Render.hpp>
+#include <components/Position.hpp>
void RenderSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
[[maybe_unused]]entityx::EventManager& events)
@@ -26,16 +28,10 @@ void RenderSystem::configure([[maybe_unused]]entityx::EntityManager& entities,
init();
}
-void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events,
[[maybe_unused]] entityx::TimeDelta dt)
{
- static GLfloat tri_data[] = {
- -1.0, -1.0, 0.0,
- 1.0, -1.0, 0.0,
- 0.0, 1.0, 0.0,
- };
-
GLuint s = worldShader.getProgram();
GLuint v = worldShader.getUniform("view");
GLuint p = worldShader.getUniform("projection");
@@ -50,10 +46,18 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
glm::vec3(0.0f, 0.0f, 0.0f), // Facing
glm::vec3(0.0f, 1.0f, 0.0f)); // Up
- glm::mat4 projection = glm::perspective(45.0f,
- ((float)width/(float)height),
- 0.01f,
- 2048.0f);
+ //glm::mat4 projection = glm::perspective(45.0f,
+ // ((float)width/(float)height),
+ // 0.01f,
+ // 2048.0f);
+
+ glm::mat4 projection = glm::ortho(-((float)width/2), // Left
+ ((float)width/2), // Right
+ -((float)height/2), // Bottom
+ ((float)height/2), // Top
+ -10.0f, // zNear
+ 10.0f // zFar
+ );
glm::mat4 model = glm::mat4(1.0f);
@@ -75,14 +79,25 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entites,
* DRAWING *
*************/
- GLuint tri_vbo;
-
- glGenBuffers(1, &tri_vbo);
- glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW);
- glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glDrawArrays(GL_TRIANGLES, 0, 3);
+ entities.each<Render, Position>(
+ [this, a](entityx::Entity, Render, Position &p){
+
+ GLuint tri_vbo;
+ GLfloat tri_data[] = {
+ (float)p.x-10.0f, (float)p.y-10.0f, 00.0f,
+ (float)p.x+10.0f, (float)p.y-10.0f, 00.0f,
+ (float)p.x+00.0f, (float)p.y+10.0f, 00.0f,
+ };
+
+ glGenBuffers(1, &tri_vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW);
+
+ glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glDrawArrays(GL_TRIANGLES, 0, 3);
+
+ });
/*************
diff --git a/src/render.hpp b/src/render.hpp
index fa609a3..21869a3 100644
--- a/src/render.hpp
+++ b/src/render.hpp
@@ -65,7 +65,7 @@ public:
/**
* Updates the render system.
*/
- void update([[maybe_unused]] entityx::EntityManager& entites,
+ void update([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events,
[[maybe_unused]] entityx::TimeDelta dt) final;
diff --git a/src/script.cpp b/src/script.cpp
index 89ae9e0..260df13 100644
--- a/src/script.cpp
+++ b/src/script.cpp
@@ -53,7 +53,7 @@ void ScriptSystem::receive([[maybe_unused]] const EntitySpawnEvent &toSpawn)
int ScriptSystem::init(void)
{
- lua.open_libraries(sol::lib::base);
+ lua.open_libraries(sol::lib::base, sol::lib::math);
scriptExport();
doFile();
@@ -87,7 +87,7 @@ void ScriptSystem::scriptExport()
[this](sol::table t){ return spawn(t);};
lua.new_usertype<Position>("Position",
- sol::constructors<Position(float x, float y), Position()>(),
+ sol::constructors<Position(double x, double y), Position()>(),
"x", &Position::x,
"y", &Position::y);
@@ -101,7 +101,7 @@ void ScriptSystem::scriptExport()
"texture", &Render::texture);
lua.new_usertype<Velocity>("Velocity",
- sol::constructors<Velocity(float, float), Velocity()>(),
+ sol::constructors<Velocity(double, double), Velocity()>(),
"x", &Velocity::x,
"y", &Velocity::y);