Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // (C) 2011 Tim Gurto
- #ifndef GEOMETRY_H
- #define GEOMETRY_H
- #include "d3d_wrappers.h"
- typedef std::map<int, size_t> shapesIndex_t;
- class Geometry{
- const static size_t ARRAY_CHUNK_SIZE;
- void pushBuffer();
- int nextIndex_; //next shape index; removed shapes' indices are not reclaimed
- shapesIndex_t shapesIndex_;
- CUSTOMVERTEX *array_;
- size_t
- arraySize_,
- totalVertices_;
- public:
- Geometry(LPDIRECT3DVERTEXBUFFER9 *vertexBuffer);
- ~Geometry();
- int addShape(CUSTOMVERTEX* start, size_t size); //returns index
- void removeShape(int index);
- //transform shapes
- void translate(int index, float x, float y);
- void rotate(int index, double angle);
- void scale(int index, float x, float y);
- };
- #endif
- // (C) 2011 Tim Gurto
- #include "Geometry.h"
- #include <cstdlib>
- #include <iostream>
- const size_t Geometry::ARRAY_CHUNK_SIZE = 48;
- Geometry::Geometry(LPDIRECT3DVERTEXBUFFER9 *vertexBuffer):
- totalVertices_(0),
- arraySize_(ARRAY_CHUNK_SIZE),
- array_((CUSTOMVERTEX*) malloc(sizeof(CUSTOMVERTEX) * arraySize_)){
- //if (!array_) ...
- }
- Geometry::~Geometry(){
- free(array_);
- }
- int Geometry::addShape(CUSTOMVERTEX* start, size_t size){
- //ensure sufficient space in array
- if (totalVertices_ + size > arraySize){
- CUSTOMVERTEX *result = realloc(array_, (arraySize + ARRAY_CHUNK_SIZE));
- if (result){
- array_ = result;
- arraySize += ARRAY_CHUNK_SIZE;
- }
- if (!result){
- //not resized; array_ is unchanged
- std::cerr << "Can't add shape: insufficient memory" << std::endl;
- return -1;
- }
- }
- if (!array_){
- //can't create shape
- std::cerr << "Can't add shape: insufficient memory" < std::endl;
- retyrn -1;
- }
- //add shape
- }
Add Comment
Please, Sign In to add comment