examples

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

commit f09b54a62fa959639adb1d20972e8f4a272dd383
parent 483fbed7782d799ef4a4b04619e61b8949b4674d
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Fri, 28 Jun 2024 00:37:07 +0100

sdl-gl: Add functional UI panel

Diffstat:
Msrc/sdl-gl.cpp | 52++++++++++++++++++++++++++++++++++++++++------------
1 file changed, 40 insertions(+), 12 deletions(-)

diff --git a/src/sdl-gl.cpp b/src/sdl-gl.cpp @@ -11,12 +11,14 @@ #include <algorithm> #include <cassert> #include <exception> +#include <format> #include <memory> #include <numbers> #include <print> #include <random> #include <ranges> #include <vector> + #include <unistd.h> #include <sys/wait.h> #include <err.h> @@ -176,6 +178,11 @@ point operator *(point lhs, rotor rhs) return {lhs.x * rhs.s - lhs.y * rhs.xy, lhs.x * rhs.xy + lhs.y * rhs.s}; } +point& operator *=(point& lhs, rotor rhs) +{ + return lhs = lhs * rhs; +} + struct circle { point pos; @@ -264,22 +271,32 @@ void panel::draw() const auto br = window_to_gl(pos + size); glBegin(GL_TRIANGLE_STRIP); - glColor3d(.3, .3, .3); + glColor3d(.5, .5, .5); glVertex(tl); glVertex(tr); glVertex(bl); glVertex(br); glEnd(); + point half{1 / (2.f * screen.x), 1 / (2.f * screen.y)}; + + tl.y -= half.y; + tr.y -= half.y; + bl.y += half.y; + br.y += half.y; + tr.x -= half.x; + br.x -= half.x; + tl.x += half.x; + bl.x += half.x; glBegin(GL_LINE_STRIP); - glColor3d(.15, .15, .15); + glColor3d(.25, .25, .25); glVertex(bl); glVertex(br); glVertex(tr); glEnd(); glBegin(GL_LINE_STRIP); - glColor3d(.5, .5, .5); + glColor3d(.7, .7, .7); glVertex(bl); glVertex(tl); glVertex(tr); @@ -512,8 +529,10 @@ void init_shape() 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)); + point p{0, 1}; + p *= rotor::angle(turns); + p.x *= 1 - i % 2 * .45f; + shape.vertices.push_back(p); } for (int i = 0; i <= n / 2; ++i) shape.wireframe.push_back(i * 2); @@ -614,10 +633,10 @@ struct font return br_screen; } - screen_coord draw(char const* str, screen_coord pos) + screen_coord draw(std::string_view str, screen_coord pos) { - while (*str) - pos.x = draw(*str++, pos).x; + for (auto c : str) + pos.x = draw(c, pos).x; return pos; } }; @@ -626,10 +645,19 @@ font* fnt; void draw_UI() { - panel{50, 50, 149, 49}.draw(); - glColor3f(1,1,1); - fnt->draw("Hello, World!", {51, 51}); - panel{50, 100, 149, 49}.draw(); + std::vector<std::string> strings; + strings.push_back(std::format("{} shapes", shapes.size())); + strings.push_back(std::format("{} objects", objs.size())); + size_t width = 2 + fnt->hdr.width * std::ranges::max(strings | std::views::transform([](auto const& s){ return s.size(); })); + size_t height = 2 + fnt->hdr.height * strings.size(); + panel{0, 0, (int)width, (int)height}.draw(); + glColor3f(0, 0, 0); + int y = 1; + for (auto const& s : strings) + { + fnt->draw(s, {1, y}); + y += fnt->hdr.height; + } } int main()