examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 37cd18cbc774dd9ec50c3fe7ed68607b01e4f98a
parent c7505e33169e91a9749d182c0a808f6178d83569
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Thu,  5 Dec 2024 00:23:22 +0000

sdl-gl: Allow selection of shape spawning via GUI

Diffstat:
Msrc/sdl-gl.cpp | 47+++++++++++++++++++++++++++++++++++++++++------
1 file changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/sdl-gl.cpp b/src/sdl-gl.cpp @@ -206,12 +206,14 @@ point mouse_pos; struct shp { + std::string name; std::vector<point> vertices; std::vector<unsigned char> wireframe; GLuint mode; }; std::vector<shp> shapes; +unsigned current_shape = 0; struct object { @@ -277,6 +279,7 @@ struct panel panel menu; bool details; +std::vector<panel> panels; void panel::draw() const { @@ -338,10 +341,7 @@ void key(SDL_KeyboardEvent& e) case SDLK_a: if (e.state) - { - 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()])); - } + objs.push_back(std::make_shared<object>(circle{mouse_pos, size_dis(gen)}, rgb::random(), norm_dis(gen), 0, shapes[current_shape])); break; case SDLK_d: @@ -406,6 +406,7 @@ coroutine_task mouse_task() { for (;;) { + loop: SDL_MouseButtonEvent const& e = co_await click_event; point drag_pos = pan - window_to_local({e.x, e.y}); if (menu.colliding(mouse_coord)) @@ -414,6 +415,15 @@ coroutine_task mouse_task() details = !details; continue; } + + for (auto const& [i, p] : std::views::enumerate(panels)) + if (p.colliding(mouse_coord)) + { + if (e.button == SDL_BUTTON_LEFT) + current_shape = i; + goto loop; + } + switch (e.button) { case SDL_BUTTON_RIGHT: @@ -516,6 +526,7 @@ void init_shape() constexpr int n = 10; shapes.emplace_back(); auto& shape = shapes.back(); + shape.name = "star"; shape.mode = GL_TRIANGLE_FAN; shape.vertices.emplace_back(0,0); for (int i = 0; i <= n; ++i) @@ -527,6 +538,7 @@ void init_shape() constexpr int n = 3; shapes.emplace_back(); auto& shape = shapes.back(); + shape.name = "triangle"; shape.mode = GL_TRIANGLES; for (int i = 0; i < n; ++i) shape.vertices.push_back(point{0, 1} * rotor::angle((float)i / n)); @@ -537,6 +549,7 @@ void init_shape() constexpr int n = 4; shapes.emplace_back(); auto& shape = shapes.back(); + shape.name = "square"; shape.mode = GL_TRIANGLE_FAN; for (int i = 0; i < n; ++i) shape.vertices.push_back(point{0, 1} * rotor::angle((float)i / n)); @@ -547,6 +560,7 @@ void init_shape() constexpr int n = 64; shapes.emplace_back(); auto& shape = shapes.back(); + shape.name = "crescent"; shape.mode = GL_TRIANGLE_STRIP; for (int i = 0; i <= n; ++i) { @@ -663,7 +677,7 @@ struct font } }; -font* fnt; +std::unique_ptr<font> fnt; void draw_UI() { @@ -685,6 +699,27 @@ void draw_UI() y += fnt->hdr.height; } + y += 5; + + panels.clear(); + + for (auto const& [i, s] : std::views::enumerate(shapes)) + { + auto& p = panels.emplace_back( + screen_coord{0, y}, + screen_coord{width, (int)fnt->hdr.height}, + rgb{.4, .4, .4} + ); + if (p.colliding(mouse_coord)) + p.colour = rgb{.3, .6, .3}; + p.draw(); + if (current_shape == i) + glColor3f(1, 1, 1); + else + glColor3f(0, 0, 0); + fnt->draw(s.name, {1, y}); + y += fnt->hdr.height; + } if (not details) return; @@ -716,7 +751,7 @@ int main() initSDL(); atexit(SDL_Quit); - fnt = new font{"/usr/share/kbd/consolefonts/lat9-16.psf.gz"}; + fnt.reset(new font{"/usr/share/kbd/consolefonts/lat9-16.psf.gz"}); for (;;) {