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 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "sdlfuncs.h"
  23. namespace segl {
  24. Uint32 getPixel(SDL_Surface *surface, int x, int y)
  25. {
  26. int bpp = surface->format->BytesPerPixel;
  27. /* Here p is the address to the pixel we want to retrieve */
  28. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  29. switch(bpp) {
  30. case 1:
  31. return *p;
  32. case 2:
  33. return *(Uint16 *)p;
  34. case 3:
  35. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  36. return p[0] << 16 | p[1] << 8 | p[2];
  37. else
  38. return p[0] | p[1] << 8 | p[2] << 16;
  39. case 4:
  40. return *(Uint32 *)p;
  41. default:
  42. return 0; /* shouldn't happen, but avoids warnings */
  43. }
  44. }
  45. void setPixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
  46. {
  47. int bpp = surface->format->BytesPerPixel;
  48. /* Here p is the address to the pixel we want to set */
  49. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  50. switch(bpp) {
  51. case 1:
  52. *p = pixel;
  53. break;
  54. case 2:
  55. *(Uint16 *)p = pixel;
  56. break;
  57. case 3:
  58. if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
  59. p[0] = (pixel >> 16) & 0xff;
  60. p[1] = (pixel >> 8) & 0xff;
  61. p[2] = pixel & 0xff;
  62. } else {
  63. p[0] = pixel & 0xff;
  64. p[1] = (pixel >> 8) & 0xff;
  65. p[2] = (pixel >> 16) & 0xff;
  66. }
  67. break;
  68. case 4:
  69. *(Uint32 *)p = pixel;
  70. break;
  71. }
  72. }
  73. void swapPixel(SDL_Surface *surface, int x1, int y1, int x2, int y2) {
  74. Uint32 a,b;
  75. a = getPixel(surface, x1, y1);
  76. b = getPixel(surface, x2, y2);
  77. setPixel(surface, x1, y1, b);
  78. setPixel(surface, x2, y2, a);
  79. }
  80. void mirrorSurfaceMiddleX(SDL_Surface *surface) {
  81. SDL_LockSurface(surface);
  82. int upto = surface->h / 2;
  83. for(int i=0; i<upto; i++) {
  84. for(int x=0; x<surface->w; x++) {
  85. swapPixel(surface, x, i, x, surface->h-1-i);
  86. }
  87. }
  88. SDL_UnlockSurface(surface);
  89. }
  90. } // namespace segl