Surface.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "Surface.h"
  2. #ifdef WIN32
  3. #include <windows.h>
  4. #endif
  5. #include <memory.h>
  6. Surface::Surface():
  7. pixels( NULL ),
  8. width( 0 ),
  9. height( 0 )
  10. {
  11. }
  12. Surface::Surface( const Surface &copy ):
  13. pixels( NULL ),
  14. width( copy.width ),
  15. height( copy.height )
  16. {
  17. if( width && height )
  18. {
  19. pixels = new unsigned char[ width * height * 4 ];
  20. memcpy( pixels, copy.pixels, width * height * 4 );
  21. }
  22. }
  23. Surface&
  24. Surface::operator =( const Surface &copy )
  25. {
  26. if( width && height )
  27. {
  28. delete[] pixels;
  29. }
  30. if( copy.width && copy.height )
  31. {
  32. width = copy.width;
  33. height = copy.height;
  34. pixels = new unsigned char[ width * height * 4 ];
  35. memcpy( pixels, copy.pixels, width * height * 4 );
  36. }
  37. return *this;
  38. }
  39. Surface::~Surface()
  40. {
  41. if( width && height )
  42. {
  43. delete[] pixels;
  44. }
  45. }
  46. Surface*
  47. CreateSurface( const int width, const int height )
  48. {
  49. Surface* image = new Surface();
  50. image->width = width;
  51. image->height = height;
  52. image->pixels = new unsigned char[ width * height * 4 ];
  53. return image;
  54. }
  55. void
  56. CopyToSurface( Surface* dest, const int x_d, const int y_d, Surface* src )
  57. {
  58. if( dest == NULL || src == NULL )
  59. {
  60. return;
  61. }
  62. for( int y_from = y_d, y_to = y_d + src->height; y_from < y_to; ++y_from )
  63. {
  64. memcpy( dest->pixels + y_from * dest->width * 4 + x_d * 4, src->pixels + ( y_from - y_d ) * src->width * 4, src->width * 4 );
  65. }
  66. }
  67. Surface*
  68. CreateSubSurface( const int x, const int y, const int width, const int height, Surface* surface )
  69. {
  70. Surface* image = CreateSurface( width, height );
  71. if( surface != NULL )
  72. {
  73. for( int y_from = y, y_to = y + image->height; y_from < y_to; ++y_from )
  74. {
  75. memcpy( image->pixels + ( y_from - y ) * image->width * 4, surface->pixels + ( y_from * surface->width + x ) * 4, image->width * 4) ;
  76. }
  77. }
  78. return image;
  79. }
  80. Surface*
  81. CreateSurfaceFrom( const int width, const int height, unsigned char* pixels )
  82. {
  83. Surface* image = CreateSurface( width, height );
  84. if( pixels != NULL )
  85. {
  86. memcpy( image->pixels, pixels, width * height * 4 );
  87. }
  88. return image;
  89. }
  90. void
  91. SetSurfaceSize( Surface* &surface, const int &width, const int &height )
  92. {
  93. unsigned char* pixels = new unsigned char[ width * height * 4 ];
  94. memset( pixels, 0x00, width * height * 4 );
  95. for( int y = 0; y < height; y++ )
  96. {
  97. if( y < surface->height )
  98. {
  99. int size_to_copy = ( surface->width < width ) ? surface->width * 4 : width * 4;
  100. memcpy( pixels + y * width * 4, surface->pixels + y * surface->width * 4, size_to_copy );
  101. }
  102. }
  103. delete surface;
  104. surface = CreateSurfaceFrom( width, height, pixels );
  105. delete[] pixels;
  106. }