Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sdlfuncs.cpp 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "sdlfuncs.h"
  2. namespace segl {
  3. Uint32 getPixel(SDL_Surface *surface, int x, int y)
  4. {
  5. int bpp = surface->format->BytesPerPixel;
  6. /* Here p is the address to the pixel we want to retrieve */
  7. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  8. switch(bpp) {
  9. case 1:
  10. return *p;
  11. case 2:
  12. return *(Uint16 *)p;
  13. case 3:
  14. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  15. return p[0] << 16 | p[1] << 8 | p[2];
  16. else
  17. return p[0] | p[1] << 8 | p[2] << 16;
  18. case 4:
  19. return *(Uint32 *)p;
  20. default:
  21. return 0; /* shouldn't happen, but avoids warnings */
  22. }
  23. }
  24. void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
  25. {
  26. int bpp = surface->format->BytesPerPixel;
  27. /* Here p is the address to the pixel we want to set */
  28. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  29. switch(bpp) {
  30. case 1:
  31. *p = pixel;
  32. break;
  33. case 2:
  34. *(Uint16 *)p = pixel;
  35. break;
  36. case 3:
  37. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  38. p[0] = (pixel >> 16) & 0xff;
  39. p[1] = (pixel >> 8) & 0xff;
  40. p[2] = pixel & 0xff;
  41. } else {
  42. p[0] = pixel & 0xff;
  43. p[1] = (pixel >> 8) & 0xff;
  44. p[2] = (pixel >> 16) & 0xff;
  45. }
  46. break;
  47. case 4:
  48. *(Uint32 *)p = pixel;
  49. break;
  50. }
  51. }
  52. void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) {
  53. Uint32 a,b;
  54. a = getPixel(surface, x1, y1);
  55. b = getPixel(surface, x2, y2);
  56. setPixel(surface, x1, y1, b);
  57. setPixel(surface, x2, y2, a);
  58. }
  59. void mirrorSurfaceMiddleX(SDL_Surface *surface) {
  60. SDL_LockSurface(surface);
  61. int upto = surface->h / 2;
  62. for(int i=0; i<upto; i++) {
  63. for(int x=0; x<surface->w; x++) {
  64. swapPixel(surface, x, i, x, surface->h-1-i);
  65. }
  66. }
  67. SDL_UnlockSurface(surface);
  68. }
  69. } // namespace segl