Surface.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include "Surface.h"
  16. #include <memory.h>
  17. #ifdef WIN32
  18. #include <windows.h>
  19. #endif
  20. Surface::Surface(): pixels(NULL), width(0), height(0){}
  21. Surface::Surface(const Surface &copy):
  22. pixels(NULL), width(copy.width), height(copy.height)
  23. {
  24. if (width && height) pixels = copy.pixels;
  25. }
  26. Surface& Surface::operator =(const Surface &copy){
  27. if (copy.width && copy.height){
  28. width = copy.width;
  29. height = copy.height;
  30. pixels = copy.pixels;
  31. }
  32. return *this;
  33. }
  34. Surface::~Surface(){}
  35. Surface* CreateSurface(const int width, const int height){
  36. Surface* image = new Surface();
  37. image->width = width;
  38. image->height = height;
  39. image->pixels.resize(width * height * 4);
  40. return image;
  41. }
  42. void CopyToSurface(Surface* dest, const int x_d, const int y_d, Surface* src){
  43. if (dest == NULL || src == NULL) return;
  44. for (int y_from = y_d, y_to = y_d + src->height; y_from < y_to; ++ y_from){
  45. memcpy(
  46. dest->pixels.data() + y_from * dest->width * 4 + x_d * 4,
  47. src->pixels.data() + (y_from - y_d) * src->width * 4, src->width * 4
  48. );
  49. }
  50. }
  51. Surface* CreateSubSurface(
  52. const int x, const int y, const int width, const int height, Surface* surface
  53. ){
  54. Surface* image = CreateSurface(width, height);
  55. if (surface != NULL){
  56. for (
  57. int y_from = y, y_to = y + image->height; y_from < y_to; ++ y_from
  58. ){
  59. memcpy(
  60. image->pixels.data() + (y_from - y) * image->width * 4,
  61. surface->pixels.data() + (y_from * surface->width + x) * 4,
  62. image->width * 4
  63. );
  64. }
  65. }
  66. return image;
  67. }
  68. Surface* CreateSurfaceFrom(
  69. const int width, const int height, unsigned char* pixels
  70. ){
  71. Surface* image = CreateSurface(width, height);
  72. if (pixels != NULL)
  73. memcpy(image->pixels.data(), pixels, width * height * 4);
  74. return image;
  75. }