|
1 | 1 | #include "Core/Graphics/Common/Sprite.hpp" |
2 | 2 |
|
3 | 3 | namespace core::Graphics::Common { |
4 | | - void Sprite::updateRect() { |
5 | | - // 纹理坐标系需要归一化 |
6 | | - |
7 | | - Vector2U const size = m_texture->getSize(); |
8 | | - float const u_scale = 1.0f / static_cast<float>(size.x); |
9 | | - float const v_scale = 1.0f / static_cast<float>(size.y); |
10 | | - m_uv = m_rect; |
11 | | - m_uv.a.x *= u_scale; |
12 | | - m_uv.a.y *= v_scale; |
13 | | - m_uv.b.x *= u_scale; |
14 | | - m_uv.b.y *= v_scale; |
15 | | - |
16 | | - // 纹理坐标系 y 轴朝下,渲染坐标系 y 轴朝上 |
17 | | - |
18 | | - m_pos_rc = m_rect - m_center; |
19 | | - m_pos_rc.a.x *= m_scale; |
20 | | - m_pos_rc.a.y *= -m_scale; |
21 | | - m_pos_rc.b.x *= m_scale; |
22 | | - m_pos_rc.b.y *= -m_scale; |
23 | | - } |
24 | | - |
25 | 4 | void Sprite::setTextureRect(RectF const& rc) { |
26 | 5 | m_rect = rc; |
27 | | - updateRect(); |
28 | 6 | } |
29 | 7 | void Sprite::setTextureCenter(Vector2F const& pt) { |
30 | 8 | m_center = pt; |
31 | | - updateRect(); |
32 | 9 | } |
33 | 10 | void Sprite::setUnitsPerPixel(float const v) { |
34 | 11 | m_scale = v; |
35 | | - updateRect(); |
36 | | - } |
37 | | - |
38 | | - void Sprite::draw(RectF const& rc) { |
39 | | - m_renderer->setTexture(m_texture.get()); |
40 | | - |
41 | | - IRenderer::DrawVertex const vert[4] = { |
42 | | - IRenderer::DrawVertex(rc.a.x, rc.a.y, m_z, m_uv.a.x, m_uv.a.y, m_color[0].color()), |
43 | | - IRenderer::DrawVertex(rc.b.x, rc.a.y, m_z, m_uv.b.x, m_uv.a.y, m_color[1].color()), |
44 | | - IRenderer::DrawVertex(rc.b.x, rc.b.y, m_z, m_uv.b.x, m_uv.b.y, m_color[2].color()), |
45 | | - IRenderer::DrawVertex(rc.a.x, rc.b.y, m_z, m_uv.a.x, m_uv.b.y, m_color[3].color()), |
46 | | - }; |
47 | | - |
48 | | - m_renderer->drawQuad(vert); |
49 | | - } |
50 | | - void Sprite::draw(Vector3F const& p1, Vector3F const& p2, Vector3F const& p3, Vector3F const& p4) { |
51 | | - m_renderer->setTexture(m_texture.get()); |
52 | | - |
53 | | - IRenderer::DrawVertex const vert[4] = { |
54 | | - IRenderer::DrawVertex(p1.x, p1.y, p1.z, m_uv.a.x, m_uv.a.y, m_color[0].color()), |
55 | | - IRenderer::DrawVertex(p2.x, p2.y, p2.z, m_uv.b.x, m_uv.a.y, m_color[1].color()), |
56 | | - IRenderer::DrawVertex(p3.x, p3.y, p3.z, m_uv.b.x, m_uv.b.y, m_color[2].color()), |
57 | | - IRenderer::DrawVertex(p4.x, p4.y, p4.z, m_uv.a.x, m_uv.b.y, m_color[3].color()), |
58 | | - }; |
59 | | - |
60 | | - m_renderer->drawQuad(vert); |
61 | | - } |
62 | | - void Sprite::draw(Vector2F const& pos) { |
63 | | - draw(pos, Vector2F(1.0f, 1.0f)); |
64 | | - } |
65 | | - void Sprite::draw(Vector2F const& pos, float const scale) { |
66 | | - draw(pos, Vector2F(scale, scale)); |
67 | | - } |
68 | | - void Sprite::draw(Vector2F const& pos, float const scale, float const rotation) { |
69 | | - if (std::abs(rotation) < std::numeric_limits<float>::min()) { |
70 | | - draw(pos, Vector2F(scale, scale)); |
71 | | - return; |
72 | | - } |
73 | | - |
74 | | - draw(pos, Vector2F(scale, scale), rotation); |
75 | | - } |
76 | | - void Sprite::draw(Vector2F const& pos, Vector2F const& scale) { |
77 | | - m_renderer->setTexture(m_texture.get()); |
78 | | - |
79 | | - auto const rect = RectF( |
80 | | - m_pos_rc.a.x * scale.x, |
81 | | - m_pos_rc.a.y * scale.y, |
82 | | - m_pos_rc.b.x * scale.x, |
83 | | - m_pos_rc.b.y * scale.y |
84 | | - ); |
85 | | - |
86 | | - IRenderer::DrawVertex const vert[4] = { |
87 | | - IRenderer::DrawVertex(pos.x + rect.a.x, pos.y + rect.a.y, m_z, m_uv.a.x, m_uv.a.y, m_color[0].color()), |
88 | | - IRenderer::DrawVertex(pos.x + rect.b.x, pos.y + rect.a.y, m_z, m_uv.b.x, m_uv.a.y, m_color[1].color()), |
89 | | - IRenderer::DrawVertex(pos.x + rect.b.x, pos.y + rect.b.y, m_z, m_uv.b.x, m_uv.b.y, m_color[2].color()), |
90 | | - IRenderer::DrawVertex(pos.x + rect.a.x, pos.y + rect.b.y, m_z, m_uv.a.x, m_uv.b.y, m_color[3].color()), |
91 | | - }; |
92 | | - |
93 | | - m_renderer->drawQuad(vert); |
94 | | - } |
95 | | - void Sprite::draw(Vector2F const& pos, Vector2F const& scale, float const rotation) { |
96 | | - if (std::abs(rotation) < std::numeric_limits<float>::min()) { |
97 | | - draw(pos, scale); |
98 | | - return; |
99 | | - } |
100 | | - |
101 | | - m_renderer->setTexture(m_texture.get()); |
102 | | - |
103 | | - auto const rect = RectF( |
104 | | - m_pos_rc.a.x * scale.x, |
105 | | - m_pos_rc.a.y * scale.y, |
106 | | - m_pos_rc.b.x * scale.x, |
107 | | - m_pos_rc.b.y * scale.y |
108 | | - ); |
109 | | - |
110 | | - IRenderer::DrawVertex vert[4] = { |
111 | | - IRenderer::DrawVertex(rect.a.x, rect.a.y, m_z, m_uv.a.x, m_uv.a.y, m_color[0].color()), |
112 | | - IRenderer::DrawVertex(rect.b.x, rect.a.y, m_z, m_uv.b.x, m_uv.a.y, m_color[1].color()), |
113 | | - IRenderer::DrawVertex(rect.b.x, rect.b.y, m_z, m_uv.b.x, m_uv.b.y, m_color[2].color()), |
114 | | - IRenderer::DrawVertex(rect.a.x, rect.b.y, m_z, m_uv.a.x, m_uv.b.y, m_color[3].color()), |
115 | | - }; |
116 | | - |
117 | | - float const sin_v = std::sinf(rotation); |
118 | | - float const cos_v = std::cosf(rotation); |
119 | | - |
120 | | - #define rotate_xy(UNIT) \ |
121 | | - {\ |
122 | | - float const tx = vert[UNIT].x * cos_v - vert[UNIT].y * sin_v;\ |
123 | | - float const ty = vert[UNIT].x * sin_v + vert[UNIT].y * cos_v;\ |
124 | | - vert[UNIT].x = tx;\ |
125 | | - vert[UNIT].y = ty;\ |
126 | | - } |
127 | | - |
128 | | - rotate_xy(0); |
129 | | - rotate_xy(1); |
130 | | - rotate_xy(2); |
131 | | - rotate_xy(3); |
132 | | - |
133 | | - vert[0].x += pos.x; vert[0].y += pos.y; |
134 | | - vert[1].x += pos.x; vert[1].y += pos.y; |
135 | | - vert[2].x += pos.x; vert[2].y += pos.y; |
136 | | - vert[3].x += pos.x; vert[3].y += pos.y; |
137 | | - |
138 | | - m_renderer->drawQuad(vert); |
139 | 12 | } |
140 | 13 |
|
141 | 14 | bool Sprite::clone(ISprite** const pp_sprite) { |
142 | | - auto const right = new Sprite(m_renderer.get(), m_texture.get()); |
| 15 | + auto const right = new Sprite(m_texture.get()); |
143 | 16 | right->m_rect = m_rect; |
144 | | - right->m_pos_rc = m_pos_rc; |
145 | | - right->m_uv = m_uv; |
146 | 17 | right->m_center = m_center; |
147 | | - right->m_z = m_z; |
148 | 18 | right->m_scale = m_scale; |
149 | | - right->m_color[0] = m_color[0]; |
150 | | - right->m_color[1] = m_color[1]; |
151 | | - right->m_color[2] = m_color[2]; |
152 | | - right->m_color[3] = m_color[3]; |
153 | 19 | *pp_sprite = right; |
154 | 20 | return true; |
155 | 21 | } |
156 | 22 |
|
157 | | - Sprite::Sprite(IRenderer* const p_renderer, ITexture2D* const p_texture) |
158 | | - : m_renderer(p_renderer) |
159 | | - , m_texture(p_texture) |
160 | | - , m_z(0.5f) |
161 | | - , m_scale(1.0f) { |
162 | | - m_color[0] = Color4B(0xFFFFFFFFu); |
163 | | - m_color[1] = Color4B(0xFFFFFFFFu); |
164 | | - m_color[2] = Color4B(0xFFFFFFFFu); |
165 | | - m_color[3] = Color4B(0xFFFFFFFFu); |
| 23 | + Sprite::Sprite(ITexture2D* const p_texture) : m_texture(p_texture), m_scale(1.0f) { |
166 | 24 | Vector2U const size = m_texture->getSize(); |
167 | 25 | m_rect = RectF(0.0f, 0.0f, static_cast<float>(size.x), static_cast<float>(size.y)); |
168 | 26 | m_center = Vector2F(m_rect.b.x * 0.5f, m_rect.b.y * 0.5f); |
169 | | - updateRect(); |
170 | 27 | } |
171 | 28 | Sprite::~Sprite() = default; |
172 | 29 | } |
173 | 30 | namespace core::Graphics { |
174 | | - bool ISprite::create(IRenderer* const p_renderer, ITexture2D* const p_texture, ISprite** const pp_sprite) { |
175 | | - *pp_sprite = new Common::Sprite(p_renderer, p_texture); |
| 31 | + bool ISprite::create(ITexture2D* const p_texture, ISprite** const pp_sprite) { |
| 32 | + *pp_sprite = new Common::Sprite(p_texture); |
176 | 33 | return true; |
177 | 34 | } |
178 | 35 | } |
0 commit comments