Skip to content

Commit

Permalink
Update jGL, System uses and update queue. (#196)
Browse files Browse the repository at this point in the history
* Update jGL, System uses and update queue.

jGL now observes data using pointers for Sprite
and Shape.

System updates objects by first adding to add or
remove sets. These state changes can then be handled
with system overrides or the base. Since jGL now observes
data via pointers the data is always automatically updated.

* unpause sbt demo
  • Loading branch information
Jerboa-app authored Sep 21, 2024
1 parent 2827a7d commit a56d87e
Show file tree
Hide file tree
Showing 20 changed files with 193 additions and 209 deletions.
13 changes: 11 additions & 2 deletions demo/desktop/softBodyTetris/loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,27 @@ end

time = hop.timeMillis()

if deleted == nil then
deleted = false
end

if time-lastTime > 1000*5 then

hop.setColour(objects[1], 1.0,0.0,0.0,1.0)

if (not deleted) and #objects > 2 then
hop.deleteObject(objects[2])
deleted = true
end

if (hop.kineticEnergy() > 0.1) then
lastTime = hop.timeMillis()
else
mesh = meshes[math.random(#meshes)]

x = 0.5 + math.random()*0.5 -0.25
y = 1.25

r = math.random(255)
g = math.random(255)
b = math.random(255)
Expand All @@ -65,7 +74,7 @@ if time-lastTime > 1000*5 then
["mass"] = 1.0,
["inertia"] = 0.01,
["name"] = "current"

}

id = hop.loadObject(o)
Expand Down
10 changes: 5 additions & 5 deletions demo/desktop/softBodyTetris/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int main(int argc, char ** argv)
16,
1,
&mapSource,
&mapBounds
&mapBounds
);

sRender & rendering = manager.getSystem<sRender>();
Expand Down Expand Up @@ -164,7 +164,7 @@ int main(int argc, char ** argv)
tp0 = high_resolution_clock::now();

collisions.centreOn(world.get()->getMapCenter());

if (!paused)
{
physics.step(&manager, &collisions, world.get());
Expand All @@ -175,7 +175,7 @@ int main(int argc, char ** argv)
tr0 = high_resolution_clock::now();

rendering.setProjection(camera.getVP());
rendering.draw(jGLInstance, &manager, world.get());
rendering.draw(jGLInstance, &manager, world.get());

tr1 = high_resolution_clock::now();

Expand Down Expand Up @@ -212,7 +212,7 @@ int main(int argc, char ** argv)
"Mouse cell (" << fixedLengthNumber(tile.x,4) << ", " << fixedLengthNumber(tile.y,4) << ", " << tile.tileType <<
"\n" <<
"Camera [world] (" << fixedLengthNumber(cameraX,4) << ", " << fixedLengthNumber(cameraY,4) << ")" <<
"\n" <<
"\n" <<
"update time: " << fixedLengthNumber(pdt+rdt,6) <<
"\n" <<
"Phys update / draw time: " << fixedLengthNumber(pdt,6) << "/" << fixedLengthNumber(rdt,6) <<
Expand Down Expand Up @@ -245,7 +245,7 @@ int main(int argc, char ** argv)

deltas[frameId] = duration_cast<duration<double>>(t1 - t0).count();
frameId = (frameId+1) % 60;

}

jGLInstance->finish();
Expand Down
2 changes: 1 addition & 1 deletion demo/desktop/softBodyTetris/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ uint8_t frameId = 0;
double deltas[60];

bool debug = true;
bool paused = true;
bool paused = false;

const double deltaPhysics = 1.0/900.0;

Expand Down
16 changes: 5 additions & 11 deletions include/Component/cRenderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ namespace Hop::Object::Component

struct cRenderable
{
// offset in transform

// colour
float r;
float g;
float b;
float a;
glm::vec4 colour;

// atlas coord
float ux;
Expand All @@ -36,7 +30,7 @@ namespace Hop::Object::Component
bool stale;

cRenderable(std::string shader)
: r(1.0),g(0.0),b(0.0),a(1.0),
: colour(0,0,0,1),
ux(0.0),uy(0.0),vx(0.0),vy(0.0),
uA(0.0f),uB(0.0f),uC(0.0f),uD(0.0f),
priority(0),
Expand All @@ -45,7 +39,7 @@ namespace Hop::Object::Component

cRenderable(
)
: r(1.0),g(0.0),b(0.0),a(1.0),
: colour(0,0,0,1),
ux(0.0),uy(0.0),vx(0.0),vy(0.0),
uA(0.0f),uB(0.0f),uC(0.0f),uD(0.0f),
priority(0),
Expand All @@ -58,7 +52,7 @@ namespace Hop::Object::Component
float ua, float ub, float uc, float ud,
uint64_t p = 0
)
: r(r),g(g),b(b),a(a),
: colour(r, g, b, a),
ux(0.0),uy(0.0),vx(0.0),vy(0.0),
uA(ua),uB(ub),uC(uc),uD(ud),
priority(p),
Expand All @@ -69,7 +63,7 @@ namespace Hop::Object::Component
float r, float g, float b, float a,
uint64_t p = 0
)
: r(r),g(g),b(b),a(a),
: colour(r, g, b, a),
ux(0.0),uy(0.0),vx(0.0),vy(0.0),
uA(0.0f),uB(0.0f),uC(0.0f),uD(0.0f),
priority(p),
Expand Down
9 changes: 3 additions & 6 deletions include/Component/cSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Hop::Object::Component
{

cSprite()
: texturePath(""), tx(0), ty(0), lx(0), ly(0)
: texturePath(""), textureRegion()
{}

cSprite
Expand All @@ -26,14 +26,11 @@ namespace Hop::Object::Component
uint16_t lx,
uint16_t ly
)
: texturePath(path), tx(tx), ty(ty), lx(lx), ly(ly)
: texturePath(path), textureRegion(tx, ty, lx, ly)
{}

std::string texturePath;
uint16_t tx;
uint16_t ty;
uint16_t lx;
uint16_t ly;
jGL::TextureRegion textureRegion;
};
}
#endif /* CSPRITE_H */
2 changes: 1 addition & 1 deletion include/Debug/collisionMeshDebug.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Hop::Debugging

std::shared_ptr<jGL::Shader> circleShader, rectangleShader;

std::map<std::string, cTransform> circlePos;
std::vector<cTransform> circlePos;

};

Expand Down
14 changes: 7 additions & 7 deletions include/System/Physics/sCollision.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Hop::System::Physics
System to detect collisions and apply forces
*/

class sCollision : public System
class sCollision : public System
{

public:
Expand All @@ -36,7 +36,7 @@ namespace Hop::System::Physics

void update
(
EntityComponentSystem * m,
EntityComponentSystem * m,
AbstractWorld * w,
ThreadPool * workers = nullptr
);
Expand All @@ -62,9 +62,9 @@ namespace Hop::System::Physics
void setSurfaceFriction(double f) { resolver->setSurfaceFriction(f); }

int lua_setCOR(lua_State * lua)
{
{
int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua, "requires 1 argument, cor");
Expand All @@ -84,9 +84,9 @@ namespace Hop::System::Physics
}

int lua_setFriction(lua_State * lua)
{
{
int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua, "requires 1 argument, friction");
Expand All @@ -109,7 +109,7 @@ namespace Hop::System::Physics

std::unique_ptr<CollisionDetector> detector;
std::unique_ptr<CollisionResolver> resolver;

};

}
Expand Down
26 changes: 13 additions & 13 deletions include/System/Physics/sPhysics.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ namespace Hop::System::Physics
/*
System to update cPhysics components given forces
*/
class sPhysics : public System
class sPhysics : public System
{

public:

sPhysics()
: dt(1.0/900.0),
: dt(1.0/900.0),
dtdt(dt*dt),
gravity(9.81),
ngx(0.0),
Expand All @@ -47,7 +47,7 @@ namespace Hop::System::Physics

void step
(
EntityComponentSystem * m,
EntityComponentSystem * m,
sCollision * collisions,
AbstractWorld * world
);
Expand All @@ -63,7 +63,7 @@ namespace Hop::System::Physics
ngx = nx;
ngy = ny;
}

void applyForce(
EntityComponentSystem * m,
Id & i,
Expand All @@ -88,11 +88,11 @@ namespace Hop::System::Physics
// automatically compute stable simulation parameters
// updating all objects
void stabaliseObjectParameters(Hop::Object::EntityComponentSystem * m);

int lua_setTimeStep(lua_State * lua)
{
{
int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua, "requires 1 argument, delta");
Expand All @@ -112,9 +112,9 @@ namespace Hop::System::Physics
}

int lua_setSubSamples(lua_State * lua)
{
{
int n = lua_gettop(lua);

if (n != 1)
{
lua_pushliteral(lua, "requires 1 argument, subsamples");
Expand Down Expand Up @@ -143,7 +143,7 @@ namespace Hop::System::Physics
lua_pushnumber(lua, energy);
return 1;
}

void setTimeStep(double delta){dt = delta; dtdt = dt*dt;}
double getTimeStep() const { return dt; }

Expand All @@ -154,10 +154,10 @@ namespace Hop::System::Physics
double getGravity() const { return gravity; }
glm::vec2 getGravityDirection() const { return glm::vec2(ngx, ngy); }

// Lua
// Lua

int lua_setGravity(lua_State * lua);

private:

void update(EntityComponentSystem * m, ThreadPool * workers = nullptr);
Expand Down
2 changes: 2 additions & 0 deletions include/System/Rendering/sRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace Hop::System::Rendering
std::shared_ptr<jGL::SpriteRenderer> sprites = nullptr;
std::unique_ptr<CollisionMeshDebug> collisionMeshDebug = nullptr;
std::shared_ptr<TextureAssetStore> textures = nullptr;

void updateObjects();
};
}

Expand Down
14 changes: 7 additions & 7 deletions include/System/Sound/sSound.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef SSOUND_H
#define SSOUND_H

#define MA_NO_VORBIS
#define MA_NO_VORBIS
#define MINIAUDIO_IMPLEMENTATION
#include <miniaudio/miniaudio.h>
#include <System/Sound/vorbis.h>
Expand All @@ -20,14 +20,14 @@ namespace Hop::System::Sound
{

void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount);

std::string to_string(ma_result code);

enum class DECODER {MA, VORBIS, NONE};

class sSound : public System
{

public:

sSound()
Expand All @@ -54,7 +54,7 @@ namespace Hop::System::Sound
if (result != MA_SUCCESS)
{
vorbisConfig = ma_decoder_config_init_default();
vorbisConfig.pCustomBackendUserData = NULL;
vorbisConfig.pCustomBackendUserData = NULL;
vorbisConfig.ppCustomBackendVTables = pCustomBackendVTables;
vorbisConfig.customBackendCount = sizeof(pCustomBackendVTables) / sizeof(pCustomBackendVTables[0]);

Expand All @@ -78,7 +78,7 @@ namespace Hop::System::Sound
if (loopingFile == "")
{
loopingFile = filename;

ma_result result = decode(filename);

if (result != MA_SUCCESS)
Expand All @@ -94,7 +94,7 @@ namespace Hop::System::Sound
ma_data_source_set_looping(&decoder, MA_TRUE);

config = ma_device_config_init(ma_device_type_playback);

config.playback.format = decoder.outputFormat;
config.playback.channels = decoder.outputChannels;
config.sampleRate = decoder.outputSampleRate;
Expand All @@ -106,7 +106,7 @@ namespace Hop::System::Sound
ma_data_source_set_looping(&vorbisDecoder, MA_TRUE);

config = ma_device_config_init(ma_device_type_playback);

config.playback.format = vorbisDecoder.outputFormat;
config.playback.channels = vorbisDecoder.outputChannels;
config.sampleRate = vorbisDecoder.outputSampleRate;
Expand Down
Loading

0 comments on commit a56d87e

Please sign in to comment.