Skip to content

Commit

Permalink
Sprite and Shape use ptrs
Browse files Browse the repository at this point in the history
  Memory to be managed elsewhere
  • Loading branch information
Jerboa-app committed Sep 11, 2024
1 parent 122773e commit 8835ad5
Show file tree
Hide file tree
Showing 24 changed files with 570 additions and 346 deletions.
3 changes: 2 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_subdirectory(Particles)
add_subdirectory(Sprite)
add_subdirectory(Shape)
add_subdirectory(Shape)
add_subdirectory(SpriteBenchmark)
14 changes: 8 additions & 6 deletions examples/Shape/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ int main(int argv, char ** argc)
jGLInstance->setTextProjection(glm::ortho(0.0,double(resX),0.0,double(resY)));
jGLInstance->setMSAA(1);

std::vector<std::shared_ptr<jGL::Shape>> shapes;
std::vector<jGL::Shape> shapes;
std::vector<jGL::Transform> trans;
std::vector<glm::vec4> cols;

RNG rng;
uint64_t n = 1000000;
Expand All @@ -46,16 +47,17 @@ int main(int argv, char ** argc)

shapes.reserve(n);
trans.reserve(n);
cols.reserve(n);
for (unsigned i = 0; i < n; i++)
{
trans.push_back(jGL::Transform(rng.nextFloat(), rng.nextFloat(), 0.0, 0.001f));
cols.push_back(glm::vec4(rng.nextFloat(), rng.nextFloat(), rng.nextFloat(), 1.0));
shapes.push_back
(
std::make_shared<jGL::Shape>
(
trans[i],
glm::vec4(rng.nextFloat(), rng.nextFloat(), rng.nextFloat(), 1.0)
)
{
&trans[i],
&cols[i]
}
);

circles->add(shapes[i], std::to_string(i));
Expand Down
188 changes: 123 additions & 65 deletions examples/Sprite/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ int main(int argv, char ** argc)
#ifdef MACOS
conf.COCOA_RETINA = true;
#endif

jGL::DesktopDisplay display(glm::ivec2(resX, resY), "Sprite", conf);

glewInit();

jGLInstance = std::move(std::make_unique<jGL::GL::OpenGLInstance>(display.getRes()));

jGL::OrthoCam camera(resX, resY, glm::vec2(0.0,0.0));

camera.setPosition(0.0f, 0.0f);

jLog::Log log;

high_resolution_clock::time_point tic, tock;
Expand Down Expand Up @@ -85,83 +85,110 @@ int main(int argv, char ** argc)
{"highest", jGL::Transform(0.6f, 0.2f, 0.0f, 0.1f)}
};

std::vector<jGL::TextureRegion> animationFrames
{
jGL::TextureRegion(0, 0, 16, 16),
jGL::TextureRegion(16, 0, 16, 16),
jGL::TextureRegion(0, 16, 16, 16),
jGL::TextureRegion(16, 16, 16, 16)
};

std::map<std::string, jGL::TextureRegion> reg =
{
{"sAtlas1", jGL::TextureRegion(0, 0, 16, 16)},
{"sAtlas2", jGL::TextureRegion(16, 0, 16, 16)},
{"sAtlas3", jGL::TextureRegion(0, 16, 16, 16)},
{"sAtlas4", jGL::TextureRegion(16, 16, 16, 16)},
{"sAtlasFull", animationFrames[0]},
{"sAtlasAnimated", jGL::TextureRegion(0, 0, 16, 16)},
{"sJerboa", jGL::TextureRegion()},
{"sPi", jGL::TextureRegion()},
{"sHeart", jGL::TextureRegion()},
{"sRandom", jGL::TextureRegion()},
{"lowest", jGL::TextureRegion()},
{"middle", jGL::TextureRegion()},
{"highest", jGL::TextureRegion()}
};

glm::vec4 colour(1.0);

sprites->setProjection(camera.getVP());

sprites->add
(
{
trans["sJerboa"],
jGL::TextureRegion(),
jerboa
&trans["sJerboa"],
&reg["sJerboa"],
jerboa.get(),
&colour
},
"sJerboa"
);

sprites->add
(
{
trans["sAtlas1"],
jGL::TextureRegion(0, 0, 16, 16),
atlas
&trans["sAtlas1"],
&reg["sAtlas1"],
atlas.get(),
&colour
},
"sAtlas1"
);

sprites->add
(
{
trans["sAtlas2"],
jGL::TextureRegion(16, 0, 16, 16),
atlas
&trans["sAtlas2"],
&reg["sAtlas2"],
atlas.get(),
&colour
},
"sAtlas2"
);

sprites->add
(
{
trans["sAtlas3"],
jGL::TextureRegion(0, 16, 16, 16),
atlas
&trans["sAtlas3"],
&reg["sAtlas3"],
atlas.get(),
&colour
},
"sAtlas3"
);

sprites->add
(
{
trans["sAtlas4"],
jGL::TextureRegion(16, 16, 16, 16),
atlas
&trans["sAtlas4"],
&reg["sAtlas4"],
atlas.get(),
&colour
},
"sAtlas4"
);

sprites->add
(
{
trans["sAtlasFull"],
jGL::TextureRegion(),
atlas
&trans["sAtlasFull"],
&reg["sAtlasFull"],
atlas.get(),
&colour
},
"sAtlasFull"
);

std::vector<jGL::TextureRegion> animationFrames
{
jGL::TextureRegion(0, 0, 16, 16),
jGL::TextureRegion(16, 0, 16, 16),
jGL::TextureRegion(0, 16, 16, 16),
jGL::TextureRegion(16, 16, 16, 16)
};
uint8_t animationFrame = 0;

sprites->add
(
{
trans["sAtlasAnimated"],
animationFrames[animationFrame],
atlas
&trans["sAtlasAnimated"],
&reg["sAtlasAnimated"],
atlas.get(),
&colour
},
"sAtlasAnimated"
);
Expand All @@ -170,50 +197,54 @@ int main(int argv, char ** argc)
sprites->add
(
{
trans["sPi"],
jGL::TextureRegion(),
Pi
&trans["sPi"],
&reg["sPi"],
Pi.get(),
&colour
},
"sPi"
);

sprites->add
(
{
trans["sHeart"],
jGL::TextureRegion(),
heart
&trans["sHeart"],
&reg["sHeart"],
heart.get(),
&colour
},
"sHeart"
);

sprites->add
(
{
trans["sRandom"],
jGL::TextureRegion(),
random
&trans["sRandom"],
&reg["sRandom"],
random.get(),
&colour
},
"sRandom"
);

sprites->add
(
{
trans["lowest"],
jGL::TextureRegion(),
Pi
&trans["lowest"],
&reg["lowest"],
Pi.get(),
&colour
},
"lowest"
);

sprites->add
(
{
trans["middle"],
jGL::TextureRegion(),
heart,
0.5f
&trans["middle"],
&reg["middle"],
heart.get(),
&colour
},
"middle",
1000
Expand All @@ -222,9 +253,10 @@ int main(int argv, char ** argc)
sprites->add
(
{
trans["highest"],
jGL::TextureRegion(),
jerboa
&trans["highest"],
&reg["highest"],
jerboa.get(),
&colour
},
"highest",
100000
Expand All @@ -235,21 +267,27 @@ int main(int argv, char ** argc)
2
);

std::vector<jGL::Transform> shapeTrans =
std::vector<jGL::Transform> shapeTrans =
{
jGL::Transform(0.0, 0.0, 0.0, 1.0),
jGL::Transform(0.0, 0.0, 0.0, 1.0)
};
std::vector<glm::vec4> shapeCols =
{
glm::vec4(1.0, 0.0, 0.0, 0.3),
glm::vec4(1.0, 0.0, 0.0, 0.3)
};

auto bbPi = std::make_shared<jGL::Shape>
auto bbPi = jGL::Shape
(
shapeTrans[0],
glm::vec4(1.0, 0.0, 0.0, 0.3)
&shapeTrans[0],
&shapeCols[0]
);
auto bbHeart = std::make_shared<jGL::Shape>

auto bbHeart = jGL::Shape
(
shapeTrans[1],
glm::vec4(1.0, 0.0, 0.0, 0.3)
&shapeTrans[1],
&shapeCols[1]
);

shapes->add(bbPi, "pi");
Expand All @@ -275,7 +313,7 @@ int main(int argv, char ** argc)
trans["sHeart"] = jGL::Transform(0.5f, 0.5f, theta, 0.1f);
trans["sPi"] = jGL::Transform(0.2f, 0.2f, theta, scale);

sprites->getSprite("sAtlasAnimated").setTextureRegion(animationFrames[animationFrame]);
reg["sAtlasAnimated"] = animationFrames[animationFrame];
if (frameId % 15 == 0)
{
animationFrame = (animationFrame+1)%animationFrames.size();
Expand All @@ -287,26 +325,46 @@ int main(int argv, char ** argc)
sprites->draw();
shapes->draw();

if (frameId == 1)
{
sprites->remove("sJerboa");
shapes->remove("heart");
}
else if (frameId == 30)
{
sprites->add
(
{
&trans["sJerboa"],
&reg["sJerboa"],
jerboa.get(),
&colour
},
"sJerboa"
);
shapes->add(bbHeart, "heart");
}

delta = 0.0;
for (int n = 0; n < 60; n++)
{
delta += deltas[n];
}
delta /= 60.0;

std::stringstream debugText;

double mouseX, mouseY;
display.mousePosition(mouseX,mouseY);

debugText << "Delta: " << fixedLengthNumber(delta,6)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< " ( FPS: " << fixedLengthNumber(1.0/delta,4)
<< ")\n"
<< "Render draw time: \n"
<< "Render draw time: \n"
<< " " << fixedLengthNumber(rdt, 6) << "\n"
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< "Mouse (" << fixedLengthNumber(mouseX,4)
<< ","
<< fixedLengthNumber(mouseY,4)
<< ")\n"
<< "Mouse in Pi's BB? " << sprites->getSprite("sPi").getScreenBoundingBox(camera).in(mouseX, mouseY);

Expand Down Expand Up @@ -334,7 +392,7 @@ int main(int argv, char ** argc)

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

}

jGLInstance->finish();
Expand Down
Loading

0 comments on commit 8835ad5

Please sign in to comment.