buffer.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3. #include "SDL.h"
  4. #include <type_traits>
  5. //Templated struct to emulate GPU buffers such as
  6. //the frame buffer and the ZBuffer along with others
  7. //Also keeps track of a bunch of useful data about itself
  8. template<class T>
  9. struct Buffer{
  10. int mWidth;
  11. int mHeight;
  12. int mPixelCount;
  13. int mPitch;
  14. int mOrigin;
  15. T *buffer;
  16. T& operator()(size_t x, size_t y){
  17. return buffer[mOrigin + -y*mWidth + x];
  18. }
  19. Buffer(int w, int h, T * array)
  20. : mWidth(w), mHeight(h), mPixelCount(w*h),
  21. mPitch(w*sizeof(T)),mOrigin(mHeight*mWidth - mWidth), buffer(array)
  22. {}
  23. ~Buffer(){
  24. delete [] buffer;
  25. };
  26. //Cannot use memset to clear a float since the binary
  27. //Representation is more complex. We just iterate through the whole
  28. //thing and explicitely set it to zero instead
  29. void clear(){
  30. if(std::is_same<T,float>::value){
  31. for(int i = 0; i < mPixelCount;++i){
  32. buffer[i] = 0.0f;
  33. }
  34. }
  35. else{
  36. memset(buffer,0xD, mPitch*mHeight);
  37. }
  38. }
  39. };
  40. #endif