tutorialsc++
5. text
one line of text, and the whole file
add this inside paint, under the line
// the font, anything we dont set keeps its default
gui::text_style_t style;
style.font.size = 24.0f;
// the text sits inside the rect it is given, left aligned and centred top to bottom
_r.draw_text({ 40, 200, 400, 40 }, "hello", style, gui::colour_t::rgb(0xFFFFFF));font.size | in logical px, same as everything else |
font.weight | 400 is normal, 700 is bold |
halign, valign | where it sits in the rect, gui::align_t::start, centre or end |
you should see hello under the shapes. that is the whole track, you can draw in a window of your own
the whole file
//
// my first window
//
#include "host/host.h"
#include "host/pump.h"
int main()
{
// what we want, anything we dont set keeps its default
gui::host_desc_t desc;
desc.title = "my window";
desc.size = { 640, 480 };
desc.kind = gui::host_kind_t::chromed;
desc.present = gui::present_kind_t::opaque;
desc.always_on_top = false;
// the window itself, create hands back false if the os said no
gui::host_t host;
if (!host.create(desc))
return 1;
// called whenever the window needs drawing, the renderer is what we draw with
auto paint = [](gui::renderer_t& _r, gui::sizei_t _size)
{
// the background first, a rect the size of the whole window
_r.fill_rect({ 0, 0, float(_size.w), float(_size.h) }, gui::colour_t::rgb(0x202830));
// a plain box, then the same box with rounded corners, 12 is how round
_r.fill_rect({ 40, 40, 120, 80 }, gui::colour_t::rgb(0xE06040));
_r.fill_round_rect({ 180, 40, 120, 80 }, 12.0f, gui::colour_t::rgb(0x40A0E0));
// an ellipse fills the rect it is given, so a square rect makes a circle
_r.fill_ellipse({ 320, 40, 80, 80 }, gui::colour_t::rgb(0x60C060));
// stroke draws just the outline, 3 px thick
_r.stroke_rect({ 420, 40, 120, 80 }, gui::colour_t::rgb(0xE0C040), 3.0f);
// a line from one point to another, 2 px thick
_r.draw_line({ 40, 160 }, { 540, 160 }, gui::colour_t::rgb(0xFFFFFF), 2.0f);
// the font, anything we dont set keeps its default
gui::text_style_t style;
style.font.size = 24.0f;
// the text sits inside the rect it is given, left aligned and centred top to bottom
_r.draw_text({ 40, 200, 400, 40 }, "hello", style, gui::colour_t::rgb(0xFFFFFF));
};
// hand it to the window, we never call it ourselves
host.set_paint(paint);
// put it on screen, then sit in the loop til it closes
host.show(true);
gui::run(host);
return 0;
}where next
| you want | go to |
|---|---|
| every call the renderer has | the c++ api |
| buttons, labels and lists instead of raw shapes | widgets |
| how the window and the drawing fit together | the systems |