[Add] Now it is possible to draw regions of a sprite.

This commit is contained in:
Tamir Atias 2012-04-12 00:24:03 +03:00
parent 51502ccd3a
commit b5bc5149fb
6 changed files with 40 additions and 5 deletions

View File

@ -93,6 +93,7 @@
<ClInclude Include="..\..\src\Main\GLWindow.h" />
<ClInclude Include="..\..\src\Math\FPS.h" />
<ClInclude Include="..\..\src\Math\MathBox.h" />
<ClInclude Include="..\..\src\Math\Rect.h" />
<ClInclude Include="..\..\src\Math\Timer.h" />
<ClInclude Include="..\..\src\Math\Vec2.h" />
<ClInclude Include="..\..\src\Sprite\Sprite.h" />

View File

@ -75,6 +75,9 @@
<ClInclude Include="..\..\src\Global\Constants.h">
<Filter>Global</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Math\Rect.h">
<Filter>Math</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\Main\main.cpp">

View File

@ -2,6 +2,7 @@
Player::Player(void) {
PLAYER_SPEED = 15;
_rotationAngle = 0.0f;
}
Player::~Player(void) {

17
src/Math/Rect.h Normal file
View File

@ -0,0 +1,17 @@
#pragma once
struct Rect
{
float x;
float y;
float w;
float h;
Rect(float x, float y, float w, float h)
{
this->x = x;
this->y = y;
this->w = w;
this->h = h;
}
};

View File

@ -6,6 +6,7 @@ Sprite::Sprite() {
size = Vec2(0.0f, 0.0f);
scale = Vec2(1.0f, 1.0f);
position = Vec2(0.0f, 0.0f);
rotation = 0.0f;
}
Sprite::~Sprite() {
@ -15,6 +16,15 @@ void Sprite::Update(float dt) {
}
void Sprite::Draw() const {
DrawRegion(Rect(0.0f, 0.0f, (float)texture->GetWidth(), (float)texture->GetHeight()));
}
void Sprite::DrawRegion(const Rect& src) const {
const float uvX = src.x / (float)texture->GetWidth();
const float uvY = src.y / (float)texture->GetHeight();
const float uvW = src.w / (float)texture->GetWidth();
const float uvH = src.h / (float)texture->GetHeight();
// Awesome artwork to describe this:
// 0---------1
// . .
@ -22,7 +32,7 @@ void Sprite::Draw() const {
// . .
// 3---------2
Vec2 scaledSize(size.x*scale.x, size.y*scale.y);
Vec2 scaledSize(size.x*scale.x, size.y*scale.y);
Vec2 vertices[4] = {
Vec2(0.0f, 0.0f),
@ -32,10 +42,10 @@ void Sprite::Draw() const {
};
Vec2 texCoords[4] = {
Vec2(0.0f, 0.0f),
Vec2(1.0f, 0.0f),
Vec2(1.0f, 1.0f),
Vec2(0.0f, 1.0f),
Vec2(uvX, uvY),
Vec2(uvX + uvW, uvY),
Vec2(uvX + uvW, uvY + uvH),
Vec2(uvX, uvY + uvH),
};
glEnable(GL_TEXTURE_2D);
@ -68,3 +78,4 @@ void Sprite::SetTexture(Texture* texture) {
this->texture = texture;
this->size = Vec2((float)texture->GetWidth(), (float)texture->GetHeight());
}

View File

@ -1,6 +1,7 @@
#pragma once
#include "../Math/Vec2.h"
#include "../Math/Rect.h"
class Texture;
@ -11,6 +12,7 @@ public:
virtual void Update(float dt);
virtual void Draw() const;
virtual void DrawRegion(const Rect& src) const;
const Vec2& GetPosition() const { return position; }
float GetX(void) { return position.x; }