commit 483fbed7782d799ef4a4b04619e61b8949b4674d
parent a9a3d1db2af2ca9a43e1884bfda67dfc32afc665
Author: Henry Wilson <henry@henryandlizzy.uk>
Date: Thu, 27 Jun 2024 00:09:21 +0100
sdl-gl: Add more shapes
Diffstat:
M | src/sdl-gl.cpp | | | 109 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- |
1 file changed, 87 insertions(+), 22 deletions(-)
diff --git a/src/sdl-gl.cpp b/src/sdl-gl.cpp
@@ -191,21 +191,24 @@ point pan = {0, 0};
point mouse_pos;
+struct shp
+{
+ std::vector<point> vertices;
+ std::vector<unsigned char> wireframe;
+ GLuint mode;
+};
+
+std::vector<shp> shapes;
+
struct object
{
circle pos;
rgb col;
float spin;
float rotation;
+ shp& shape;
};
-std::vector<std::shared_ptr<object>> objs = {
- std::make_shared<object>(circle{{-1.5f, -1}, 1.f}, rgb{.7,.2,.2}, .2),
- std::make_shared<object>(circle{{0, 1}, .5f}, rgb{.2,.7,.2}, 0),
- std::make_shared<object>(circle{{1.5f, -1}, 1.f}, rgb{.2,.2,.7}, -.2),
-};
-
-std::vector<point> shape;
-std::span<point> shape_wireframe;
+std::vector<std::shared_ptr<object>> objs = {};
constexpr auto deref = std::views::transform([](auto const& p) -> auto& { return *p; });
@@ -223,8 +226,8 @@ void presentScene(void)
if (not wireframe)
{
- glBegin(GL_TRIANGLE_FAN);
- for (auto p : shape)
+ glBegin(o.shape.mode);
+ for (auto p : o.shape.vertices)
{
p *= o.pos.radius;
auto pp = (p * r + pan + o.pos.pos) * scale;
@@ -234,8 +237,9 @@ void presentScene(void)
else
{
glBegin(GL_LINE_LOOP);
- for (auto p : shape_wireframe)
+ for (auto idx : o.shape.wireframe)
{
+ auto p = o.shape.vertices[idx];
p *= o.pos.radius;
auto pp = (p * r + pan + o.pos.pos) * scale;
glVertex2f(pp.x / aspect_ratio, pp.y);
@@ -282,8 +286,6 @@ void panel::draw() const
glEnd();
}
-
-
void key(SDL_KeyboardEvent& e)
{
static std::uniform_real_distribution<float> size_dis{.5f, 1.5f};
@@ -304,7 +306,10 @@ void key(SDL_KeyboardEvent& e)
case SDLK_a:
if (e.state)
- objs.push_back(std::make_shared<object>(circle{mouse_pos, size_dis(gen)}, rgb::random(), norm_dis(gen), 0));
+ {
+ static int i = 0;
+ objs.push_back(std::make_shared<object>(circle{mouse_pos, size_dis(gen)}, rgb::random(), norm_dis(gen), 0, shapes[i++ % shapes.size()]));
+ }
break;
case SDLK_d:
@@ -314,15 +319,26 @@ void key(SDL_KeyboardEvent& e)
case SDLK_r:
if (e.state)
- {
for (auto& obj : objs | deref)
if (obj.pos.colliding(mouse_pos))
{
std::swap(obj.col.r, obj.col.g);
std::swap(obj.col.g, obj.col.b);
}
+ break;
- }
+ case SDLK_EQUALS:
+ if (e.state)
+ for (auto& obj : objs | deref)
+ if (obj.pos.colliding(mouse_pos))
+ obj.pos.radius *= 1.2;
+ break;
+
+ case SDLK_MINUS:
+ if (e.state)
+ for (auto& obj : objs | deref)
+ if (obj.pos.colliding(mouse_pos))
+ obj.pos.radius /= 1.2;
break;
case SDLK_s:
@@ -457,12 +473,60 @@ void doInput()
void init_shape()
{
- constexpr int n = 10;
- shape.emplace_back(0,0);
- for (int i = 0; i <= n; ++i)
- shape.push_back(point{0, (.65f + i % 2) / -1.65f} * rotor::angle((float)i / n));
- shape_wireframe = shape;
- shape_wireframe = shape_wireframe.subspan(1, shape.size() - 2);
+ {
+ constexpr int n = 10;
+ shapes.emplace_back();
+ auto& shape = shapes.back();
+ shape.mode = GL_TRIANGLE_FAN;
+ shape.vertices.emplace_back(0,0);
+ for (int i = 0; i <= n; ++i)
+ shape.vertices.push_back(point{0, (.65f + i % 2) / -1.65f} * rotor::angle((float)i / n));
+ for (int i = 0; i < n; ++i)
+ shape.wireframe.push_back(i+1);
+ }
+ {
+ constexpr int n = 3;
+ shapes.emplace_back();
+ auto& shape = shapes.back();
+ shape.mode = GL_TRIANGLES;
+ for (int i = 0; i < n; ++i)
+ shape.vertices.push_back(point{0, 1} * rotor::angle((float)i / n));
+ for (int i = 0; i < n; ++i)
+ shape.wireframe.push_back(i);
+ }
+ {
+ constexpr int n = 4;
+ shapes.emplace_back();
+ auto& shape = shapes.back();
+ shape.mode = GL_TRIANGLE_FAN;
+ for (int i = 0; i < n; ++i)
+ shape.vertices.push_back(point{0, 1} * rotor::angle((float)i / n));
+ for (int i = 0; i < n; ++i)
+ shape.wireframe.push_back(i);
+ }
+ {
+ constexpr int n = 20;
+ shapes.emplace_back();
+ auto& shape = shapes.back();
+ shape.mode = GL_TRIANGLE_STRIP;
+ for (int i = 0; i <= n; ++i)
+ {
+ float turns = i * .5f / n;
+ float radians = turns * 2 * std::numbers::pi;
+ shape.vertices.push_back(point{0, 1 - i % 2 * .5f * sin(radians)} * rotor::angle(turns));
+ }
+ for (int i = 0; i <= n / 2; ++i)
+ shape.wireframe.push_back(i * 2);
+ for (int i = n / 2; i > 0; --i)
+ shape.wireframe.push_back(i * 2 - 1);
+ }
+}
+
+void init_scene()
+{
+ objs.push_back(std::make_shared<object>(circle{{-1.5f, -1}, 1.f}, rgb{.7,.2,.2}, .2, 0, shapes[0]));
+ objs.push_back(std::make_shared<object>(circle{{0, 1}, .5f}, rgb{.2,.7,.2}, 0, 0, shapes[0]));
+ objs.push_back(std::make_shared<object>(circle{{1.5f, -1}, 1.f}, rgb{.2,.2,.7}, -.2, 0, shapes[0]));
}
struct psf2
@@ -571,6 +635,7 @@ void draw_UI()
int main()
{
init_shape();
+ init_scene();
mouse_task();
initSDL();
atexit(SDL_Quit);