Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. //UVTranslator.h
  2. //Author: Adam Hulbert - hulbert.adam@gmail.com
  3. //Use: translates row and column position into a float[4] of UV coordinates
  4.  
  5. #pragma once
  6.  
  7. #include <string>
  8.  
  9. class UVTranslator
  10. {
  11. public:
  12.     //CTOR
  13.     //float width_      : width of the entire spritesheet image
  14.     //float height_     : height of the entire spritesheet image
  15.     //float blockWidth_ : the width of each sprite in the sprite sheet (will only work when sprite sheets have equally spaced sprites),
  16.     //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_;
  17.     UVTranslator(float width_, float height_, float blockWidth_, float blockHeight_);
  18.    
  19.     //default ctor, object is in an invalid state until Init is called.
  20.     UVTranslator();
  21.  
  22.     //calls the matching CTOR
  23.     void Init(float sheetWidth_, float sheetHeight_, float tileWidth_, float tileHeight_);
  24.    
  25.     //float* out_       : must be a float array initialised with 4 elements.
  26.     //float col_        : 0 based column (starting from left)
  27.     //float row_        : 0 based row (starting from the bottom)
  28.     //float blockCols_  : number of columns to retreive in the UV (defaults to 1)
  29.     //float rowWidth_   : number of rows to retreive in the UV (defaults to 1)
  30.     void GetUV(float* out_, float row_, float col_, float tileCols_ = 1, float tileRows_ = 1);
  31.  
  32.     ~UVTranslator();
  33.  
  34. private:
  35.  
  36.     float tileWidth;
  37.     float tileHeight;
  38.     float sheetWidth;
  39.     float sheetHeight;
  40. };
  41.  
  42. //UVTranslator.cpp
  43. #include "UVTranslator.h"
  44.  
  45. UVTranslator::UVTranslator(float width_, float height_, float tileWidth_, float tileHeight_)
  46.     : sheetWidth(width_), sheetHeight(height_), tileWidth(tileWidth_), tileHeight(tileHeight_)
  47. {
  48. }
  49.  
  50. //needs to be initialised still after default ctor
  51. UVTranslator::UVTranslator(void)
  52. {
  53. }
  54.  
  55.  
  56. UVTranslator::~UVTranslator(void)
  57. {
  58. }
  59.  
  60. void UVTranslator::Init(float width_, float height_, float tileWidth_, float tileHeight_)
  61. {
  62.     sheetWidth = width_;
  63.     sheetHeight = height_;
  64.     tileWidth = tileWidth_;
  65.     tileHeight = tileHeight_;
  66. }
  67.  
  68.  
  69. void UVTranslator::GetUV(float* out_, float row_, float col_, float blockCols_, float blockRows_)
  70. {
  71.     //{ U_MIN , V_MIN , U_MIN + U_STEP, V_MIN + V_STEP };
  72.     out_[0] = col_ * tileWidth / sheetWidth;
  73.     out_[1] = row_ * tileHeight / sheetHeight;
  74.     out_[2] = out_[0] + ((blockCols_ * tileWidth)/sheetWidth);
  75.     out_[3] = out_[1] + ((blockRows_ * tileHeight)/sheetHeight);   
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement