SDL_Extensions.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, int myC)
  4. {
  5. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  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. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  36. }
  37. else
  38. {
  39. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  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-2;
  57. int k=2;
  58. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  59. {
  60. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  61. }
  62. else
  63. {
  64. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  65. }
  66. }
  67. }
  68. }
  69. SDL_FreeSurface(first);
  70. return ret;
  71. };
  72. ///**************/
  73. ///Rotates toRot surface by 90 degrees left
  74. ///**************/
  75. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  76. {
  77. 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);
  78. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  79. for(int i=0; i<ret->w; ++i)
  80. {
  81. for(int j=0; j<ret->h; ++j)
  82. {
  83. {
  84. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  85. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  86. {
  87. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  88. }
  89. else
  90. {
  91. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  92. }
  93. }
  94. }
  95. }
  96. SDL_FreeSurface(first);
  97. return ret;
  98. }
  99. ///*************/
  100. ///Rotates toRot surface by 180 degrees
  101. ///*************/
  102. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  103. {
  104. 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);
  105. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  106. for(int i=0; i<ret->w; ++i)
  107. {
  108. for(int j=0; j<ret->h; ++j)
  109. {
  110. {
  111. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  112. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  113. {
  114. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  115. }
  116. else
  117. {
  118. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  119. }
  120. }
  121. }
  122. }
  123. SDL_FreeSurface(first);
  124. return ret;
  125. }