From b5bc5149fbf09511252aa0a9a767f550ae7155bf Mon Sep 17 00:00:00 2001 From: Tamir Atias Date: Thu, 12 Apr 2012 00:24:03 +0300 Subject: [PATCH] [Add] Now it is possible to draw regions of a sprite. --- Bin/VC10/VC10.vcxproj | 1 + Bin/VC10/VC10.vcxproj.filters | 3 +++ src/Actor/Player.cpp | 1 + src/Math/Rect.h | 17 +++++++++++++++++ src/Sprite/Sprite.cpp | 21 ++++++++++++++++----- src/Sprite/Sprite.h | 2 ++ 6 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/Math/Rect.h diff --git a/Bin/VC10/VC10.vcxproj b/Bin/VC10/VC10.vcxproj index e055682..bf3c88b 100644 --- a/Bin/VC10/VC10.vcxproj +++ b/Bin/VC10/VC10.vcxproj @@ -93,6 +93,7 @@ + diff --git a/Bin/VC10/VC10.vcxproj.filters b/Bin/VC10/VC10.vcxproj.filters index f65ab46..a9cbeb9 100644 --- a/Bin/VC10/VC10.vcxproj.filters +++ b/Bin/VC10/VC10.vcxproj.filters @@ -75,6 +75,9 @@ Global + + Math + diff --git a/src/Actor/Player.cpp b/src/Actor/Player.cpp index c76c97a..5b95168 100644 --- a/src/Actor/Player.cpp +++ b/src/Actor/Player.cpp @@ -2,6 +2,7 @@ Player::Player(void) { PLAYER_SPEED = 15; + _rotationAngle = 0.0f; } Player::~Player(void) { diff --git a/src/Math/Rect.h b/src/Math/Rect.h new file mode 100644 index 0000000..bf7d281 --- /dev/null +++ b/src/Math/Rect.h @@ -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; + } +}; diff --git a/src/Sprite/Sprite.cpp b/src/Sprite/Sprite.cpp index 3da7e26..f0e4f88 100644 --- a/src/Sprite/Sprite.cpp +++ b/src/Sprite/Sprite.cpp @@ -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()); } + diff --git a/src/Sprite/Sprite.h b/src/Sprite/Sprite.h index 2478f5b..6e87cd3 100644 --- a/src/Sprite/Sprite.h +++ b/src/Sprite/Sprite.h @@ -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; }