2
0

SDL_Extensions.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "stdafx.h"
  2. #include "SDL_Extensions.h"
  3. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B)
  4. {
  5. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  6. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  7. {
  8. p[0] = R;
  9. p[1] = G;
  10. p[2] = B;
  11. }
  12. else
  13. {
  14. p[0] = B;
  15. p[1] = G;
  16. p[2] = R;
  17. }
  18. SDL_UpdateRect(ekran, x, y, 1, 1);
  19. }
  20. ///**************/
  21. ///Reverses the toRot surface by the vertical axis
  22. ///**************/
  23. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  24. {
  25. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  26. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  27. for(int i=0; i<ret->w; ++i)
  28. {
  29. for(int j=0; j<ret->h; ++j)
  30. {
  31. {
  32. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  33. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  34. {
  35. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  36. }
  37. else
  38. {
  39. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  40. }
  41. }
  42. }
  43. }
  44. SDL_FreeSurface(first);
  45. return ret;
  46. }
  47. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  48. {
  49. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  50. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  51. for(int i=0; i<ret->w; ++i)
  52. {
  53. for(int j=0; j<ret->h; ++j)
  54. {
  55. {
  56. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel;
  57. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  58. {
  59. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  60. }
  61. else
  62. {
  63. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  64. }
  65. }
  66. }
  67. }
  68. SDL_FreeSurface(first);
  69. return ret;
  70. };
  71. ///**************/
  72. ///Rotates toRot surface by 90 degrees left
  73. ///**************/
  74. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  75. {
  76. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->h, toRot->w, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  77. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  78. for(int i=0; i<ret->w; ++i)
  79. {
  80. for(int j=0; j<ret->h; ++j)
  81. {
  82. {
  83. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  84. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  85. {
  86. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  87. }
  88. else
  89. {
  90. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  91. }
  92. }
  93. }
  94. }
  95. SDL_FreeSurface(first);
  96. return ret;
  97. }
  98. ///*************/
  99. ///Rotates toRot surface by 180 degrees
  100. ///*************/
  101. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  102. {
  103. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  104. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  105. for(int i=0; i<ret->w; ++i)
  106. {
  107. for(int j=0; j<ret->h; ++j)
  108. {
  109. {
  110. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  111. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  112. {
  113. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  114. }
  115. else
  116. {
  117. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  118. }
  119. }
  120. }
  121. }
  122. SDL_FreeSurface(first);
  123. return ret;
  124. }