Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //UVTranslator.h
- //Author: Adam Hulbert - hulbert.adam@gmail.com
- //Use: translates row and column position into a float[4] of UV coordinates
- #pragma once
- #include <string>
- class UVTranslator
- {
- public:
- //CTOR
- //float width_ : width of the entire spritesheet image
- //float height_ : height of the entire spritesheet image
- //float blockWidth_ : the width of each sprite in the sprite sheet (will only work when sprite sheets have equally spaced sprites),
- //float blockHeight_ : the height of each sprite in the sprite sheet (will only work when sprite sheets have equally spaced sprites), if 0, defaults to blockWidth_;
- UVTranslator(float width_, float height_, float blockWidth_, float blockHeight_);
- //default ctor, object is in an invalid state until Init is called.
- UVTranslator();
- //calls the matching CTOR
- void Init(float sheetWidth_, float sheetHeight_, float tileWidth_, float tileHeight_);
- //float* out_ : must be a float array initialised with 4 elements.
- //float col_ : 0 based column (starting from left)
- //float row_ : 0 based row (starting from the bottom)
- //float blockCols_ : number of columns to retreive in the UV (defaults to 1)
- //float rowWidth_ : number of rows to retreive in the UV (defaults to 1)
- void GetUV(float* out_, float row_, float col_, float tileCols_ = 1, float tileRows_ = 1);
- ~UVTranslator();
- private:
- float tileWidth;
- float tileHeight;
- float sheetWidth;
- float sheetHeight;
- };
- //UVTranslator.cpp
- #include "UVTranslator.h"
- UVTranslator::UVTranslator(float width_, float height_, float tileWidth_, float tileHeight_)
- : sheetWidth(width_), sheetHeight(height_), tileWidth(tileWidth_), tileHeight(tileHeight_)
- {
- }
- //needs to be initialised still after default ctor
- UVTranslator::UVTranslator(void)
- {
- }
- UVTranslator::~UVTranslator(void)
- {
- }
- void UVTranslator::Init(float width_, float height_, float tileWidth_, float tileHeight_)
- {
- sheetWidth = width_;
- sheetHeight = height_;
- tileWidth = tileWidth_;
- tileHeight = tileHeight_;
- }
- void UVTranslator::GetUV(float* out_, float row_, float col_, float blockCols_, float blockRows_)
- {
- //{ U_MIN , V_MIN , U_MIN + U_STEP, V_MIN + V_STEP };
- out_[0] = col_ * tileWidth / sheetWidth;
- out_[1] = row_ * tileHeight / sheetHeight;
- out_[2] = out_[0] + ((blockCols_ * tileWidth)/sheetWidth);
- out_[3] = out_[1] + ((blockRows_ * tileHeight)/sheetHeight);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement