Skylicht Engine
Loading...
Searching...
No Matches
CGraphics2D.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "pch.h"
28
29#include "Utils/CSingleton.h"
30#include "Graphics2D/SpriteFrame/CSpriteFrame.h"
31#include "Graphics2D/SpriteFrame/CGlyphFont.h"
32#include "Material/CMaterial.h"
33
34namespace Skylicht
35{
36 class CCamera;
37 class CCanvas;
38
60 class SKYLICHT_API CGraphics2D
61 {
62 public:
63 DECLARE_SINGLETON(CGraphics2D)
64
65 protected:
66 int m_currentW;
67 int m_currentH;
68
69 IVideoDriver* m_driver;
70
71 std::vector<IMeshBuffer*> m_allBuffers;
72 int m_bufferID;
73
74 std::vector<CCanvas*> m_canvas;
75
76 protected:
77
78 video::SMaterial m_2dMaterial;
79 video::SMaterial m_customMaterial;
80
81 int m_vertexColorShader;
82
83 IMeshBuffer* m_buffer;
84 scene::SVertexBuffer* m_vertices;
85 scene::CIndexBuffer* m_indices;
86
87 public:
88 CGraphics2D();
89 virtual ~CGraphics2D();
90
91 void resize();
92
93 core::dimension2du getScreenSize();
94
95 bool isHD();
96
97 bool isWideScreen();
98
99 void addCanvas(CCanvas* canvas);
100
101 void removeCanvas(CCanvas* canvas);
102
103 void render(CCamera* camera);
104
105 void beginRenderGUI(const core::matrix4& projection, const core::matrix4& view);
106
107 void endRenderGUI();
108
109 void prepareBuffer();
110
111 void nextBuffer();
112
113 public:
114
115 void flushBuffer(IMeshBuffer* meshBuffer, video::SMaterial& material);
116
117 void flush();
118
119 void flushWithMaterial(CMaterial* material);
120
121 void addExternalBuffer(IMeshBuffer* meshBuffer, const core::matrix4& absoluteMatrix, int shaderID, CMaterial* material = NULL);
122
123 void addImageBatch(ITexture* img, const SColor& color, const core::matrix4& absoluteMatrix, int shaderID, CMaterial* material = NULL, float pivotX = 0, float pivotY = 0);
124
125 void addImageBatch(ITexture* img, const core::rectf& dest, const core::rectf& source, const SColor& color, const core::matrix4& absoluteMatrix, int shaderID, CMaterial* material = NULL, float pivotX = 0, float pivotY = 0);
126
127 void addModuleBatch(SModuleOffset* module, const SColor& color, const core::matrix4& absoluteMatrix, float offsetX, float offsetY, int shaderID, CMaterial* material = NULL);
128
129 void addModuleBatch(SModuleOffset* module,
130 const SColor& color,
131 const core::matrix4& absoluteMatrix,
132 const core::rectf& r,
133 float anchorLeft,
134 float anchorRight,
135 float anchorTop,
136 float anchorBottom,
137 int shaderID,
138 CMaterial* material = NULL);
139
140 void addModuleBatch(SModuleOffset* module,
141 const SColor& color,
142 const core::matrix4& absoluteMatrix,
143 const core::rectf& r,
144 int shaderID,
145 CMaterial* material = NULL);
146
147 void addModuleBatchLR(SModuleOffset* module,
148 const SColor& color,
149 const core::matrix4& absoluteMatrix,
150 const core::rectf& r,
151 float anchorLeft,
152 float anchorRight,
153 int shaderID,
154 CMaterial* material = NULL);
155
156 void addModuleBatchTB(SModuleOffset* module,
157 const SColor& color,
158 const core::matrix4& absoluteMatrix,
159 const core::rectf& r,
160 float anchorTop,
161 float anchorBottom,
162 int shaderID,
163 CMaterial* material = NULL);
164
165 void addFrameBatch(SFrame* frame, const SColor& color, const core::matrix4& absoluteMatrix, int materialID, CMaterial* material = NULL);
166
167 void addFrameBatch(const core::rectf& pos, SFrame* frame, const SColor& color, const core::matrix4& absoluteMatrix, int materialID, CMaterial* material = NULL);
168
169 void addRectangleBatch(const core::rectf& pos, const core::rectf& uv, const SColor& color, const core::matrix4& absoluteTransform, int shaderID, CMaterial* material = NULL);
170
171 void addEclipseBatch(const core::rectf& pos, const core::rectf& uv, const SColor& color, const core::matrix4& absoluteTransform, int shaderID, float a = 0.0f, float b = 360.0f, CMaterial* material = NULL);
172
173 void beginDrawDepth();
174
175 void endDrawDepth();
176
177 void beginDepthTest();
178
179 void endDepthTest();
180
181 void setWriteDepth(video::SMaterial& mat);
182
183 void setDepthTest(video::SMaterial& mat);
184
185 void setNoDepthTest(video::SMaterial& mat);
186
187 void draw2DTriangle(
188 const core::position2df& a,
189 const core::position2df& b,
190 const core::position2df& c,
191 const SColor& color);
192
193 void draw2DLine(const core::position2df& start, const core::position2df& end, const SColor& color);
194
195 void draw2DLines(const std::vector<core::position2df>& points, const SColor& color);
196
197 void draw2DRectangle(const core::rectf& pos, const SColor& color);
198
199 void draw2DRectangle(const core::vector3df& upleft, const core::vector3df& lowerright, const SColor& color);
200
201 void draw2DRectangleOutline(const core::rectf& pos, const SColor& color);
202
203 void draw2DRectangleOutline(const core::vector3df& upleft, const core::vector3df& lowerright, const SColor& color);
204
205 void drawText(const core::position2df& pos, IFont* font, const SColor& color, const std::wstring& string, int materialID, CMaterial* material = NULL);
206
207 float measureCharWidth(IFont* font, wchar_t c);
208
209 core::dimension2df measureText(IFont* font, const std::wstring& string);
210
211 video::SMaterial& getMaterial()
212 {
213 return m_2dMaterial;
214 }
215
216 IMeshBuffer* getCurrentBuffer()
217 {
218 return m_buffer;
219 }
220
221 private:
222
223 void updateRectBuffer(video::S3DVertex* vtx, const core::rectf& r, const core::matrix4& mat);
224
225 void updateRectTexcoordBuffer(
226 video::S3DVertex* vtx,
227 const core::rectf& r, float texWidth, float texHeight,
228 const core::rectf& drawRect, const core::rectf& rect, float w, float h,
229 SModuleRect* moduleRect);
230 };
231
232}
This is an object class used to set up the camera, including its position, viewing angle,...
Definition CCamera.h:70
This class manages GUI components, including creating and deleting images and sprites.
Definition CCanvas.h:79
The object class describes material information such as which shader it's associated with,...
Definition CMaterial.h:84
Definition IFont.h:32
Definition CIndexBuffer.h:15
Struct for holding a mesh with a single material.
Definition IMeshBuffer.h:42
Interface of a Video Driver dependent Texture.
Definition ITexture.h:119
Interface to driver which is able to perform 2d and 3d graphics functions.
Definition IVideoDriver.h:388
Class representing a 32 bit ARGB color.
Definition SColor.h:285
Struct for holding parameters for a material renderer.
Definition SMaterial.h:255
#define DECLARE_SINGLETON(className)
Declare the standard singleton accessors for a class.
Definition CSingleton.h:36
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
vector3d< f32 > vector3df
Typedef for a f32 3d vector.
Definition vector3d.h:445
CMatrix4< f32 > matrix4
Typedef for f32 matrix.
Definition matrix4.h:2241
dimension2d< u32 > dimension2du
Typedef for an unsigned integer dimension.
Definition dimension2d.h:212
rect< f32 > rectf
Rectangle with float values.
Definition rect.h:271
vector2d< f32 > position2df
Definition position2d.h:21
dimension2d< f32 > dimension2df
Typedef for an f32 dimension.
Definition dimension2d.h:210
Definition CSpriteFrame.h:93
Definition CSpriteFrame.h:69
Definition CSpriteFrame.h:47
standard vertex used by the Irrlicht engine.
Definition S3DVertex.h:55