buffer.h 900 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #ifndef BUFFER_H
  2. #define BUFFER_H
  3. #include "SDL.h"
  4. #include <type_traits>
  5. template<class T>
  6. struct Buffer{
  7. int mWidth;
  8. int mHeight;
  9. int mPixelCount;
  10. int mPitch;
  11. T *buffer;
  12. T& operator()(size_t x, size_t y){
  13. return buffer[y*mWidth + x];
  14. }
  15. Buffer(int w, int h, T * array)
  16. : mWidth(w), mHeight(h), mPixelCount(w*h),
  17. mPitch(w*sizeof(T)), buffer(array)
  18. {}
  19. ~Buffer(){
  20. delete [] buffer;
  21. };
  22. void clear(){
  23. if(std::is_same<T,float>::value){
  24. for(int i = 0; i < mPixelCount;++i){
  25. buffer[i] = 0.0f;
  26. }
  27. }
  28. else{
  29. memset(buffer,0, mPitch*mHeight);
  30. }
  31. }
  32. };
  33. #endif