hx3d  1
2D/3D Simple Game Framework
sprite.cpp
1 /*
2  Sprite.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/sprite.hpp"
22 
23 #include "hx3d/graphics/geometries/sprite_geometry.hpp"
24 
25 namespace hx3d {
26 namespace graphics {
27 
29 Mesh() {
30  _geometry = Make<geom::SpriteGeometry>();
31  _geometry->setFaceCulling(Culling::Back);
32 
34 }
35 
36 void Sprite::setTexture(const Ptr<Texture>& texture) {
37  _texture = texture;
38 
39  transform.size.x = _texture->getWidth();
40  transform.size.y = _texture->getHeight();
41 
42  /* Set the correct texture coordinates, in case of previous framebuffer. */
43  Ptr<geom::SpriteGeometry> spriteGeo = std::dynamic_pointer_cast<geom::SpriteGeometry>(_geometry);
44  spriteGeo->activateTextureMode();
45 
46  _geometry->uploadAll();
47 }
48 
50  _texture = buffer.getColorBuffer();
51 
52  transform.size.x = _texture->getWidth();
53  transform.size.y = _texture->getHeight();
54 
55  /* Reverse the framebuffer ! */
56  Ptr<geom::SpriteGeometry> spriteGeo = std::dynamic_pointer_cast<geom::SpriteGeometry>(_geometry);
57  spriteGeo->activateFramebufferMode();
58 
59  _geometry->uploadAll();
60 }
61 
63  _texture = region.getTexture();
64 
65  transform.size.x = region.getMaxX() - region.getMinX();
66  transform.size.y = region.getMaxY() - region.getMinY();
67 
68  Ptr<geom::SpriteGeometry> spriteGeo = std::dynamic_pointer_cast<geom::SpriteGeometry>(_geometry);
69  spriteGeo->setFromRegion(region);
70 
71  _geometry->uploadAll();
72 }
73 
75  return _texture;
76 }
77 
79  float ratioW = transform.size.x / _texture->getWidth();
80  float ratioH = transform.size.y / _texture->getHeight();
81 
82  auto& uv = _geometry->getAttribute("Texture").getVector();
83  for (unsigned int i = 0; i < uv.size(); i+=2) {
84  if (uv[i] != 0)
85  uv[i] = ratioW;
86 
87  if (uv[i+1] != 0)
88  uv[i+1] = ratioH;
89  }
90 
91  _geometry->uploadAll();
92 }
93 
94 void Sprite::draw(Ptr<Shader> shader) {
95  Texture::use(_texture);
96  Mesh::draw(shader);
98 }
99 
100 } /* graphics */
101 } /* hx3d */
void activateFramebufferMode()
Prepare the sprite for render-to-texture mode.
void setTint(Color tint)
Set the mesh tint.
Definition: mesh.cpp:43
Ptr< Texture > getColorBuffer()
Get the framebuffer color buffer.
Sprite()
Create a sprite without texture.
Definition: sprite.cpp:28
float getMaxX()
Get the right coordinate.
void setTexture(const Ptr< Texture > &texture)
Set the sprite texture.
Definition: sprite.cpp:36
CounterClockwise culling: back face.
float getMaxY()
Get the top coordinate.
static Color White
White color.
Definition: color.hpp:107
float getMinY()
Get the bottom coordinate.
hx3d framework namespace
Definition: audio.hpp:26
virtual void draw(Ptr< Shader > shader) override
Draw the mesh using a shader.
Definition: sprite.cpp:94
void activateTextureMode()
Prepare the sprite for simple texture mode.
const Ptr< Texture > & getTexture()
Get the texture.
Defines a rectangle in a texture.
void setFromRegion(TextureRegion &region)
Set the geometry for a texture region.
void scaleTexture()
Scale the texture coordinates following the texture size.
Definition: sprite.cpp:78
Ptr< geom::BaseGeometry > _geometry
Current geometry.
Definition: mesh.hpp:96
glm::vec3 size
Size.
Definition: transform.hpp:84
Transform transform
Mesh transformation.
Definition: mesh.hpp:90
Displayable 2D/3D element.
Definition: mesh.hpp:41
Render-to-texture buffer.
Definition: framebuffer.hpp:37
float getMinX()
Get the left coordinate.
virtual void draw(Ptr< Shader > shader)
Draw the mesh using a shader.
Definition: mesh.cpp:30
static void disable()
Clear the current texture for drawing.
Definition: texture.cpp:136
static void use(Ptr< Texture > texture)
Use the current texture for drawing.
Definition: texture.cpp:127
Ptr< Texture > getTexture()
Get the sprite texture.
Definition: sprite.cpp:74
Sprite defined VBO geometry.
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34