00001 #include "gui_sprite.hpp" 00002 #include "gui_material.hpp" 00003 #include "gui_manager.hpp" 00004 00005 namespace gui 00006 { 00007 vertex::vertex() : col(color::WHITE) 00008 { 00009 } 00010 00011 vertex::vertex(const vector2f& mPos, const vector2f& mUV, const color& mCol) : 00012 pos(mPos), uvs(mUV), col(mCol) 00013 { 00014 } 00015 00016 sprite::sprite(const manager* pManager, utils::refptr<material> pMat) : 00017 pManager_(pManager), mHotSpot_(vector2f::ZERO) 00018 { 00019 mQuad_.mat = pMat; 00020 fWidth_ = pMat->get_width(); 00021 fHeight_ = pMat->get_height(); 00022 00023 mQuad_.v[0].pos = vector2f(0, 0); 00024 mQuad_.v[1].pos = vector2f(0+fWidth_, 0); 00025 mQuad_.v[2].pos = vector2f(0+fWidth_, 0+fHeight_); 00026 mQuad_.v[3].pos = vector2f(0, 0+fHeight_); 00027 00028 float u = fWidth_/pMat->get_real_width(); 00029 float v = fHeight_/pMat->get_real_height(); 00030 00031 mQuad_.v[0].uvs = vector2f(0, 0); 00032 mQuad_.v[1].uvs = vector2f(u, 0); 00033 mQuad_.v[2].uvs = vector2f(u, v); 00034 mQuad_.v[3].uvs = vector2f(0, v); 00035 } 00036 00037 sprite::sprite(const manager* pManager, utils::refptr<material> pMat, float fWidth, float fHeight) : 00038 pManager_(pManager), mHotSpot_(vector2f::ZERO) 00039 { 00040 mQuad_.mat = pMat; 00041 fWidth_ = fWidth; 00042 fHeight_ = fHeight; 00043 00044 mQuad_.v[0].pos = vector2f(0, 0); 00045 mQuad_.v[1].pos = vector2f(0+fWidth_, 0); 00046 mQuad_.v[2].pos = vector2f(0+fWidth_, 0+fHeight_); 00047 mQuad_.v[3].pos = vector2f(0, 0+fHeight_); 00048 00049 float u = fWidth_/pMat->get_real_width(); 00050 float v = fHeight_/pMat->get_real_height(); 00051 00052 mQuad_.v[0].uvs = vector2f(0, 0); 00053 mQuad_.v[1].uvs = vector2f(u, 0); 00054 mQuad_.v[2].uvs = vector2f(u, v); 00055 mQuad_.v[3].uvs = vector2f(0, v); 00056 } 00057 00058 sprite::sprite(const manager* pManager, utils::refptr<material> pMat, float fU, float fV, float fWidth, float fHeight) : 00059 pManager_(pManager), mHotSpot_(vector2f::ZERO) 00060 { 00061 mQuad_.mat = pMat; 00062 fWidth_ = fWidth; 00063 fHeight_ = fHeight; 00064 00065 mQuad_.v[0].pos = vector2f(0, 0); 00066 mQuad_.v[1].pos = vector2f(0+fWidth_, 0); 00067 mQuad_.v[2].pos = vector2f(0+fWidth_, 0+fHeight_); 00068 mQuad_.v[3].pos = vector2f(0, 0+fHeight_); 00069 00070 float u1 = fU/pMat->get_real_width(); 00071 float v1 = fV/pMat->get_real_height(); 00072 float u2 = (fU + fWidth_)/pMat->get_real_width(); 00073 float v2 = (fV + fHeight_)/pMat->get_real_height(); 00074 00075 mQuad_.v[0].uvs = vector2f(u1, v1); 00076 mQuad_.v[1].uvs = vector2f(u2, v1); 00077 mQuad_.v[2].uvs = vector2f(u2, v2); 00078 mQuad_.v[3].uvs = vector2f(u1, v2); 00079 } 00080 00081 sprite::~sprite() 00082 { 00083 } 00084 00085 void sprite::render(float fX, float fY) const 00086 { 00087 mQuad_.v[0].pos = vector2f(fX, fY) - mHotSpot_; 00088 mQuad_.v[1].pos = vector2f(fX+fWidth_, fY) - mHotSpot_; 00089 mQuad_.v[2].pos = vector2f(fX+fWidth_, fY+fHeight_) - mHotSpot_; 00090 mQuad_.v[3].pos = vector2f(fX, fY+fHeight_) - mHotSpot_; 00091 00092 pManager_->render_quad(mQuad_); 00093 } 00094 00095 void sprite::render_ex(float fX, float fY, float fRot, float fHScale, float fVScale) const 00096 { 00097 float x1 = -mHotSpot_.x*fHScale; 00098 float x2 = (fWidth_ - mHotSpot_.x)*fHScale; 00099 float y1 = -mHotSpot_.y*fVScale; 00100 float y2 = (fHeight_ - mHotSpot_.y)*fVScale; 00101 00102 if (fRot != 0.0f) 00103 { 00104 float cost = cos(fRot); 00105 float sint = sin(fRot); 00106 00107 mQuad_.v[0].pos = vector2f(x1*cost - y1*sint + fX, x1*sint + y1*cost + fY); 00108 mQuad_.v[1].pos = vector2f(x2*cost - y1*sint + fX, x2*sint + y1*cost + fY); 00109 mQuad_.v[2].pos = vector2f(x2*cost - y2*sint + fX, x2*sint + y2*cost + fY); 00110 mQuad_.v[3].pos = vector2f(x1*cost - y2*sint + fX, x1*sint + y2*cost + fY); 00111 } 00112 else 00113 { 00114 mQuad_.v[0].pos = vector2f(x1 + fX, y1 + fY); 00115 mQuad_.v[1].pos = vector2f(x2 + fX, y1 + fY); 00116 mQuad_.v[2].pos = vector2f(x2 + fX, y2 + fY); 00117 mQuad_.v[3].pos = vector2f(x1 + fX, y2 + fY); 00118 } 00119 00120 pManager_->render_quad(mQuad_); 00121 } 00122 00123 void sprite::render_2v(float fX1, float fY1, float fX3, float fY3) 00124 { 00125 mQuad_.v[0].pos = vector2f(fX1, fY1); 00126 mQuad_.v[1].pos = vector2f(fX3, fY1); 00127 mQuad_.v[2].pos = vector2f(fX3, fY3); 00128 mQuad_.v[3].pos = vector2f(fX1, fY3); 00129 00130 pManager_->render_quad(mQuad_); 00131 } 00132 00133 void sprite::render_4v(float fX1, float fY1, 00134 float fX2, float fY2, 00135 float fX3, float fY3, 00136 float fX4, float fY4) 00137 { 00138 mQuad_.v[0].pos = vector2f(fX1, fY1); 00139 mQuad_.v[1].pos = vector2f(fX2, fY2); 00140 mQuad_.v[2].pos = vector2f(fX3, fY3); 00141 mQuad_.v[3].pos = vector2f(fX4, fY4); 00142 00143 pManager_->render_quad(mQuad_); 00144 } 00145 00146 void sprite::render_quads(const std::vector<std::array<vertex,4>>& lQuadsArray) const 00147 { 00148 pManager_->render_quads(mQuad_, lQuadsArray); 00149 } 00150 00151 void sprite::render_static() const 00152 { 00153 pManager_->render_quad(mQuad_); 00154 } 00155 00156 void sprite::set_quad(const std::array<vertex,4>& lVertexArray) 00157 { 00158 mQuad_.v = lVertexArray; 00159 } 00160 00161 void sprite::set_color(const color& mColor, uint uiIndex) 00162 { 00163 if (uiIndex != uint(-1)) 00164 mQuad_.v[uiIndex].col = mColor; 00165 else 00166 { 00167 mQuad_.v[0].col = mColor; 00168 mQuad_.v[1].col = mColor; 00169 mQuad_.v[2].col = mColor; 00170 mQuad_.v[3].col = mColor; 00171 } 00172 } 00173 00174 void sprite::set_desaturated(bool bDesaturated) 00175 { 00176 } 00177 00178 void sprite::set_blend_mode(blend_mode mBlendMode) 00179 { 00180 mQuad_.blend = mBlendMode; 00181 } 00182 00183 void sprite::set_hot_spot(const vector2f& mHotSpot) 00184 { 00185 mHotSpot_ = mHotSpot; 00186 } 00187 00188 void sprite::set_hot_spot(float fX, float fY) 00189 { 00190 mHotSpot_.x = fX; 00191 mHotSpot_.y = fY; 00192 } 00193 00194 void sprite::set_texture_rect(const std::array<float,4>& lTextureRect, bool bNormalized) 00195 { 00196 if (bNormalized) 00197 { 00198 mQuad_.v[0].uvs = vector2f(lTextureRect[0], lTextureRect[1]); 00199 mQuad_.v[1].uvs = vector2f(lTextureRect[2], lTextureRect[1]); 00200 mQuad_.v[2].uvs = vector2f(lTextureRect[2], lTextureRect[3]); 00201 mQuad_.v[3].uvs = vector2f(lTextureRect[0], lTextureRect[3]); 00202 } 00203 else 00204 { 00205 float fWidth = mQuad_.mat->get_width(); 00206 float fHeight = mQuad_.mat->get_height(); 00207 00208 mQuad_.v[0].uvs = vector2f(lTextureRect[0]/fWidth, lTextureRect[1]/fHeight); 00209 mQuad_.v[1].uvs = vector2f(lTextureRect[2]/fWidth, lTextureRect[1]/fHeight); 00210 mQuad_.v[2].uvs = vector2f(lTextureRect[2]/fWidth, lTextureRect[3]/fHeight); 00211 mQuad_.v[3].uvs = vector2f(lTextureRect[0]/fWidth, lTextureRect[3]/fHeight); 00212 } 00213 } 00214 00215 void sprite::set_texture_rect(float fX1, float fY1, float fX3, float fY3, bool bNormalized) 00216 { 00217 if (bNormalized) 00218 { 00219 mQuad_.v[0].uvs = vector2f(fX1, fY1); 00220 mQuad_.v[1].uvs = vector2f(fX3, fY1); 00221 mQuad_.v[2].uvs = vector2f(fX3, fY3); 00222 mQuad_.v[3].uvs = vector2f(fX1, fY3); 00223 } 00224 else 00225 { 00226 float fWidth = mQuad_.mat->get_width(); 00227 float fHeight = mQuad_.mat->get_height(); 00228 00229 mQuad_.v[0].uvs = vector2f(fX1/fWidth, fY1/fHeight); 00230 mQuad_.v[1].uvs = vector2f(fX3/fWidth, fY1/fHeight); 00231 mQuad_.v[2].uvs = vector2f(fX3/fWidth, fY3/fHeight); 00232 mQuad_.v[3].uvs = vector2f(fX1/fWidth, fY3/fHeight); 00233 } 00234 } 00235 00236 void sprite::set_texture_coords(const std::array<float,8>& lTextureCoords, bool bNormalized) 00237 { 00238 if (bNormalized) 00239 { 00240 mQuad_.v[0].uvs = vector2f(lTextureCoords[0], lTextureCoords[1]); 00241 mQuad_.v[1].uvs = vector2f(lTextureCoords[2], lTextureCoords[3]); 00242 mQuad_.v[2].uvs = vector2f(lTextureCoords[4], lTextureCoords[5]); 00243 mQuad_.v[3].uvs = vector2f(lTextureCoords[6], lTextureCoords[7]); 00244 } 00245 else 00246 { 00247 float fWidth = mQuad_.mat->get_width(); 00248 float fHeight = mQuad_.mat->get_height(); 00249 00250 mQuad_.v[0].uvs = vector2f(lTextureCoords[0]/fWidth, lTextureCoords[1]/fHeight); 00251 mQuad_.v[1].uvs = vector2f(lTextureCoords[2]/fWidth, lTextureCoords[3]/fHeight); 00252 mQuad_.v[2].uvs = vector2f(lTextureCoords[4]/fWidth, lTextureCoords[5]/fHeight); 00253 mQuad_.v[3].uvs = vector2f(lTextureCoords[6]/fWidth, lTextureCoords[7]/fHeight); 00254 } 00255 } 00256 00257 void sprite::set_texture_coords(float fX1, float fY1, float fX2, float fY2, float fX3, float fY3, 00258 float fX4, float fY4, bool bNormalized) 00259 { 00260 if (bNormalized) 00261 { 00262 mQuad_.v[0].uvs = vector2f(fX1, fY1); 00263 mQuad_.v[1].uvs = vector2f(fX2, fY2); 00264 mQuad_.v[2].uvs = vector2f(fX3, fY3); 00265 mQuad_.v[3].uvs = vector2f(fX4, fY4); 00266 } 00267 else 00268 { 00269 float fWidth = mQuad_.mat->get_width(); 00270 float fHeight = mQuad_.mat->get_height(); 00271 00272 mQuad_.v[0].uvs = vector2f(fX1/fWidth, fY1/fHeight); 00273 mQuad_.v[1].uvs = vector2f(fX2/fWidth, fY2/fHeight); 00274 mQuad_.v[2].uvs = vector2f(fX3/fWidth, fY3/fHeight); 00275 mQuad_.v[3].uvs = vector2f(fX4/fWidth, fY4/fHeight); 00276 } 00277 } 00278 00279 void sprite::set_dimensions(float fWidth, float fHeight) 00280 { 00281 fWidth_ = fWidth; 00282 fHeight_ = fHeight; 00283 } 00284 00285 float sprite::get_width() const 00286 { 00287 return fWidth_; 00288 } 00289 00290 float sprite::get_height() const 00291 { 00292 return fHeight_; 00293 } 00294 00295 color sprite::get_color() const 00296 { 00297 return mQuad_.v[0].col; 00298 } 00299 00300 blend_mode sprite::get_blend_mode() const 00301 { 00302 return mQuad_.blend; 00303 } 00304 00305 std::array<float,4> sprite::get_texture_rect() const 00306 { 00307 return {{mQuad_.v[0].uvs.x, mQuad_.v[0].uvs.y, mQuad_.v[3].uvs.x, mQuad_.v[3].uvs.y}}; 00308 } 00309 00310 std::array<float,8> sprite::get_texture_coords(bool bNormalized) const 00311 { 00312 if (bNormalized) 00313 { 00314 return {{mQuad_.v[0].uvs.x, mQuad_.v[0].uvs.y, mQuad_.v[1].uvs.x, mQuad_.v[1].uvs.y, 00315 mQuad_.v[2].uvs.x, mQuad_.v[2].uvs.y, mQuad_.v[3].uvs.x, mQuad_.v[3].uvs.y}}; 00316 } 00317 else 00318 { 00319 float fWidth = mQuad_.mat->get_width(); 00320 float fHeight = mQuad_.mat->get_height(); 00321 00322 return {{mQuad_.v[0].uvs.x*fWidth, mQuad_.v[0].uvs.y*fHeight, 00323 mQuad_.v[1].uvs.x*fWidth, mQuad_.v[1].uvs.y*fHeight, 00324 mQuad_.v[2].uvs.x*fWidth, mQuad_.v[2].uvs.y*fHeight, 00325 mQuad_.v[3].uvs.x*fWidth, mQuad_.v[3].uvs.y*fHeight}}; 00326 } 00327 } 00328 }