SHARE
TWEET

Geometry class

Archon Feb 24th, 2011 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // (C) 2011 Tim Gurto
  2.  
  3. #ifndef GEOMETRY_H
  4. #define GEOMETRY_H
  5.  
  6. #include "d3d_wrappers.h"
  7.  
  8. typedef std::map<int, size_t> shapesIndex_t;
  9.  
  10. class Geometry{
  11.  
  12.    const static size_t ARRAY_CHUNK_SIZE;
  13.    
  14.    void pushBuffer();
  15.    int nextIndex_; //next shape index; removed shapes' indices are not reclaimed
  16.    shapesIndex_t shapesIndex_;
  17.    CUSTOMVERTEX *array_;
  18.    size_t
  19.       arraySize_,
  20.       totalVertices_;
  21.    
  22.  
  23. public:
  24.    Geometry(LPDIRECT3DVERTEXBUFFER9 *vertexBuffer);
  25.    ~Geometry();
  26.  
  27.    int addShape(CUSTOMVERTEX* start, size_t size); //returns index
  28.    void removeShape(int index);
  29.  
  30.    //transform shapes
  31.    void translate(int index, float x, float y);
  32.    void rotate(int index, double angle);
  33.    void scale(int index, float x, float y);
  34. };
  35.  
  36. #endif
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46. // (C) 2011 Tim Gurto
  47.  
  48. #include "Geometry.h"
  49. #include <cstdlib>
  50. #include <iostream>
  51.  
  52. const size_t Geometry::ARRAY_CHUNK_SIZE = 48;
  53.  
  54. Geometry::Geometry(LPDIRECT3DVERTEXBUFFER9 *vertexBuffer):
  55. totalVertices_(0),
  56. arraySize_(ARRAY_CHUNK_SIZE),
  57. array_((CUSTOMVERTEX*) malloc(sizeof(CUSTOMVERTEX) * arraySize_)){
  58.    //if (!array_) ...
  59. }
  60.  
  61. Geometry::~Geometry(){
  62.    free(array_);
  63. }
  64.  
  65. int Geometry::addShape(CUSTOMVERTEX* start, size_t size){
  66.    //ensure sufficient space in array
  67.    if (totalVertices_ + size > arraySize){
  68.       CUSTOMVERTEX *result = realloc(array_, (arraySize + ARRAY_CHUNK_SIZE));
  69.       if (result){
  70.          array_ = result;
  71.          arraySize += ARRAY_CHUNK_SIZE;
  72.       }
  73.  
  74.       if (!result){
  75.          //not resized; array_ is unchanged
  76.          std::cerr << "Can't add shape: insufficient memory" << std::endl;
  77.          return -1;
  78.       }
  79.    }
  80.    if (!array_){
  81.       //can't create shape
  82.       std::cerr << "Can't add shape: insufficient memory" < std::endl;
  83.       retyrn -1;
  84.    }
  85.  
  86.    //add shape
  87.  
  88.  
  89. }
RAW Paste Data
Top