-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdrawing.h
111 lines (95 loc) · 5.19 KB
/
drawing.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright (C) 2020-2021 Sami Väisänen
// Copyright (C) 2020-2021 Ensisoft http://www.ensisoft.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "config.h"
#include <string>
#include "graphics/types.h"
#include "graphics/color4f.h"
#include "graphics/transform.h"
// This is the simple drawing API that provides auxiliary drawing
// functionality mostly useful for drawing stuff outside the actual
// game content, for example in the editor application for drawing
// selection boxes etc.
//
// The drawing always takes place enclosed inside the rectangle which
// defines the bounding box for the shape/drawing operation. Shapes
// may or may not fill this rectangle completely.
//
// The coordinate of the bounding rect is relative to the origin of
// of the painter's logical view setting (see Painter::SetView)
// which means that the dimensions of the box may or may not equal
// pixels depending on what is the ratio of device viewport (pixels)
// to painters logical viewport size. The same mapping applies also
// to the position of the rect which is relative to the painter's
// view origin which maybe not be the same as the window (rendering
// surface origin) unless the painter has been configured so.
//
// In summary if you want to render to specific coordinates in the window
// and use pixels as the sizes for the shapes check the following:
//
// 1. Painter's device viewport is what you'd expect.
// For 1:1 drawing to the window you'd probably want 0,0 as the
// origin of the device viewport (window's top left corner) and
// the size and width of the viewport should match the window's
// *client* (i.e. the renderable surface) size. (Be aware of the
// differences between *window size* and (renderable) surface size
// aka as "client size".
//
// 2. Your pixel ratio in painter is 1:1, meaning 1 painter / game unit
// maps to 1 pixel. This is adjusted through a call to Painter::SetView
// You probably want to use Painter::SetTopLeftView.
namespace gfx
{
class Painter;
class Material;
class Drawable;
// Draw text inside the given rectangle.
bool DrawTextRect(Painter& painter,
const std::string& text,
const std::string& font,
unsigned font_size_px,
const FRect& rect,
const Color4f& color,
unsigned alignment = TextAlign::AlignVCenter | TextAlign::AlignHCenter,
unsigned properties = TextProp::None,
float line_height = 1.0f);
// Draw a rectangle filled with the desired color or material.
bool FillRect(Painter& painter, const FRect& rect, const Color4f& color);
bool FillRect(Painter& painter, const FRect& rect, const Material& material);
// Fill a shape within the specified rectangle with the desired color or material.
bool FillShape(Painter& painter, const FRect& rect, const Drawable& shape, const Color4f& color);
bool FillShape(Painter& painter, const FRect& rect, const Drawable& shape, const Material& material);
// Draw the outline of a rectangle. the rectangle is defined in pixels
// and positioned relative to the top left corer of the render target/surface.
// If rotation is non-zero the rect is first rotated *then* translated.
bool DrawRectOutline(Painter& painter, const FRect& rect, const Color4f& color, float line_width = 1.0f);
bool DrawRectOutline(Painter& painter, const FRect& rect, const Material& material, float line_width = 1.0f);
bool DrawShapeOutline(Painter& painter, const FRect& rect, const Drawable& shape,
const Color4f& color, float line_width = 1.0f);
bool DrawShapeOutline(Painter& painter, const FRect& rect, const Drawable& shape,
const Material& material, float line_width = 1.0f);
// Draw a line from the center of point A to the center of point B
// using the given line width (if possible) and with the given color.
// Points A and B are relative to the top left corner of the rendering
// target (e.g the window surface).
bool DebugDrawLine(Painter& painter, const FPoint& a, const FPoint& b, const Color4f& color, float line_width = 1.0f);
// Like above but instead use a material for rasterizing the line fragments.
bool DebugDrawLine(Painter& painter, const FPoint& a, const FPoint& b, const Material& material, float line_width = 1.0f);
bool DebugDrawCircle(Painter& painter, const FCircle& circle, const Color4f& color, float line_width = 1.0f);
bool DebugDrawCircle(Painter& painter, const FCircle& circle, const Material& material, float line_width = 1.0f);
bool DebugDrawRect(Painter& painter, const FRect& rect, const Color4f& color, float line_width = 1.0f);
bool DebugDrawRect(Painter& painter, const FRect& rect, const Material& material, float line_width = 1.0f);
} // namespace