libdonut  2.3.2
Application framework for cross-platform game development in C++20
example_rectangle.cpp

This example shows a very basic application that renders a lime green rectangle at a fixed size in the middle of a resizable window.

#include <donut/donut.hpp>
namespace {
class RectangleApplication final : public app::Application {
public:
RectangleApplication() {
resize();
}
protected:
void update(app::FrameInfo /*frameInfo*/) override {
for (const events::Event& event : eventPump.pollEvents()) {
quit();
} else if (event.is<events::WindowSizeChangedEvent>()) {
resize();
}
}
}
void display(app::TickInfo /*tickInfo*/, app::FrameInfo /*frameInfo*/) override {
constexpr vec2 RECTANGLE_SIZE{100.0f, 60.0f};
gfx::Framebuffer& framebuffer = window.getFramebuffer();
renderer.clearFramebufferColor(framebuffer, Color::BLACK);
gfx::RenderPass renderPass{};
.position = vec2{viewport.size / 2} - RECTANGLE_SIZE * 0.5f,
.size = RECTANGLE_SIZE,
.tintColor = Color::LIME,
});
renderer.render(framebuffer, renderPass, viewport, camera);
window.present();
}
private:
void resize() {
const ivec2 size = window.getDrawableSize();
viewport = {.position{0, 0}, .size = size};
camera = gfx::Camera::createOrthographic({.offset{0.0f, 0.0f}, .size = size});
}
events::EventPump eventPump{};
gfx::Window window{{.title = "Rectangle"}};
gfx::Viewport viewport{};
gfx::Camera camera{};
gfx::Renderer renderer{};
};
} // namespace
int main() {
RectangleApplication application{};
application.run();
}
static const Color LIME
Definition: Color.hpp:96
Main application base class.
Definition: Application.hpp:131
virtual void quit()
Initiate the shutdown process, meaning that the current frame will be the last to be processed and di...
virtual void update(FrameInfo frameInfo)
Per-frame update callback, called in the main loop once at the beginning of each frame,...
Definition: Application.hpp:304
virtual void display(TickInfo tickInfo, FrameInfo frameInfo)
Frame rendering callback, called in the main loop once at the end of each frame after processing tick...
Definition: Application.hpp:354
Persistent system for polling Event data and other user input from the host environment on demand.
Definition: EventPump.hpp:23
Combined view-projection matrix, defining the perspective for a Renderer to render from.
Definition: Camera.hpp:53
static Camera createOrthographic(const CameraOrthographicOptions &options, const mat4 &viewMatrix) noexcept
Create a camera with an orthographic projection.
Definition: Camera.hpp:63
Unique resource handle with exclusive ownership of a GPU framebuffer.
Definition: Framebuffer.hpp:15
Graphics drawing queue for batch rendering using a Renderer.
Definition: RenderPass.hpp:718
RenderPass & draw(const ModelInstance &model)
Enqueue a ModelInstance to be drawn when the render pass is rendered.
Persistent system for rendering the batched draw commands of a RenderPass onto a Framebuffer,...
Definition: Renderer.hpp:38
Graphical window that can be rendered to.
Definition: Window.hpp:81
int main()
Definition: example_rectangle.cpp:65
Transient information about the current frame of an Application.
Definition: FrameInfo.hpp:11
Transient information about the current tick of an Application.
Definition: TickInfo.hpp:13
Application was requested to quit by the user.
Definition: Event.hpp:129
Data structure containing information about an event.
Definition: Event.hpp:339
Window size was changed.
Definition: Event.hpp:169
Configuration of a 2D rectangle instance, optionally textured, for drawing as part of a RenderPass.
Definition: RenderPass.hpp:300
vec2 position
Position, in world coordinates, to render the rectangle at, with respect to its RectangleInstance::or...
Definition: RenderPass.hpp:321
Rectangular region of a framebuffer.
Definition: Viewport.hpp:13