home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / oddech / orbital / orbital.exe / src / zooms.cpp < prev   
C/C++ Source or Header  |  2002-07-14  |  8KB  |  257 lines

  1. ///////////////////////////////////////////////
  2. // 
  3. //  Snipe2d ludum dare 48h compo entry
  4. //
  5. //  Jari Komppa aka Sol 
  6. //  http://iki.fi/sol
  7. // 
  8. ///////////////////////////////////////////////
  9. // License
  10. ///////////////////////////////////////////////
  11. // 
  12. //     This software is provided 'as-is', without any express or implied
  13. //     warranty.    In no event will the authors be held liable for any damages
  14. //     arising from the use of this software.
  15. // 
  16. //     Permission is granted to anyone to use this software for any purpose,
  17. //     including commercial applications, and to alter it and redistribute it
  18. //     freely, subject to the following restrictions:
  19. // 
  20. //     1. The origin of this software must not be misrepresented; you must not
  21. //        claim that you wrote the original software. If you use this software
  22. //        in a product, an acknowledgment in the product documentation would be
  23. //        appreciated but is not required.
  24. //     2. Altered source versions must be plainly marked as such, and must not be
  25. //        misrepresented as being the original software.
  26. //     3. This notice may not be removed or altered from any source distribution.
  27. // 
  28. // (eg. same as ZLIB license)
  29. //
  30. ///////////////////////////////////////////////
  31. //
  32. // Houses are taken from a satellite picture of glasgow.
  33. //
  34. // The sources are a mess, as I didn't even try to do anything
  35. // really organized here.. and hey, it's a 48h compo =)
  36. //
  37. #include "snipe2d.h"
  38.  
  39. void zoom(SDL_Surface * src, float ofsx, float ofsy, float scale)
  40. {
  41.     if ( SDL_LockSurface(src) < 0 ) 
  42.         return;
  43.     if ( SDL_LockSurface(gScreen) < 0 ) 
  44.     {
  45.         SDL_UnlockSurface(src);
  46.         return;
  47.     }
  48.  
  49.     int i, j, c, d, yofs, xofs, imgwidth;
  50.     int xinc, yinc;
  51.     imgwidth = (int)((639 << SHIFT_AMOUNT) * scale);
  52.     yinc = (int)(scale * (1 << SHIFT_AMOUNT));
  53.     xinc = (int)(scale * (1 << SHIFT_AMOUNT));
  54.     yofs = (int)((1 << SHIFT_AMOUNT) * ofsy);
  55.     xofs = (int)((1 << SHIFT_AMOUNT) * ofsx);
  56.     d = (yofs >> SHIFT_AMOUNT) * (src->pitch / 2);
  57.     d <<= SHIFT_AMOUNT;
  58.     d += xofs;
  59.     short *t = (short*)gScreen->pixels; 
  60.     for (i = 0; i < 480; i++)
  61.     {
  62.         c = d;
  63.         // top and down clip
  64.         if (yofs < (1 << SHIFT_AMOUNT) || yofs > (598 << SHIFT_AMOUNT))
  65.         {
  66.             for (j = 0; j < 640; j++)
  67.             {
  68.                 *t = 0x01e0;
  69.                 t++;
  70.             }
  71.         }
  72.         else // x min clip
  73.         if (xofs < (1 << SHIFT_AMOUNT))
  74.         {
  75.             int count = -(xofs / xinc);
  76.             for (j = 0; j < count; j++)
  77.             {
  78.                 *t = 0x01e0;
  79.                 t++;
  80.             }
  81.             c += xinc * count;
  82.             for (j = count; j < 640; j++)
  83.             {
  84.                 *t = ((short*)src->pixels)[(c) >> SHIFT_AMOUNT];
  85.                 t++;
  86.                 c += xinc;
  87.             }
  88.         }
  89.         else // x max clip
  90.         if (xofs + imgwidth > (800 << SHIFT_AMOUNT))
  91.         {            
  92.             int count = 640 - (((xofs + imgwidth) - (800 << SHIFT_AMOUNT)) / xinc);
  93.  
  94.             for (j = 0; j < count; j++)
  95.             {
  96.                 *t = ((short*)src->pixels)[(c) >> SHIFT_AMOUNT];
  97.                 t++;
  98.                 c += xinc;
  99.             }
  100.             for (j = count; j < 640; j++)
  101.             {
  102.                 *t = 0x01e0;
  103.                 t++;
  104.             }            
  105.         }
  106.         else // no clipping
  107.         {
  108.             for (j = 0; j < 640; j++)
  109.             {
  110.                 *t = ((short*)src->pixels)[(c) >> SHIFT_AMOUNT];
  111.                 t++;
  112.                 c += xinc;
  113.             }
  114.         }
  115.         t += (gScreen->pitch / 2) - 640;
  116.         yofs += yinc;
  117.         d = (yofs >> SHIFT_AMOUNT) * (src->pitch / 2);
  118.         d <<= SHIFT_AMOUNT;
  119.         d += xofs;
  120.  
  121.     }
  122.  
  123.     SDL_UnlockSurface(src);
  124.     SDL_UnlockSurface(gScreen);
  125.  
  126. }
  127.  
  128. /*
  129.           (X&1)==0        (X&1==1)
  130.          +---------------------------------
  131. (Y&1)==0 | u+=.25,v+=.00  u+=.50,v+=.75
  132. (Y&1)==1 | u+=.75,v+=.50  u+=.00,v+=.25
  133. */
  134. int unreal_dither[8] =
  135. {
  136.     (int)((1 << SHIFT_AMOUNT) * 0.25),
  137.     (int)((1 << SHIFT_AMOUNT) * 0.00),
  138.  
  139.     (int)((1 << SHIFT_AMOUNT) * 0.50),
  140.     (int)((1 << SHIFT_AMOUNT) * 0.75),
  141.  
  142.     (int)((1 << SHIFT_AMOUNT) * 0.75),
  143.     (int)((1 << SHIFT_AMOUNT) * 0.50),
  144.  
  145.     (int)((1 << SHIFT_AMOUNT) * 0.00),
  146.     (int)((1 << SHIFT_AMOUNT) * 0.25)
  147. };
  148.  
  149.  
  150. void zoom_unreal(SDL_Surface * src, float ofsx, float ofsy, float scale)
  151. {
  152.     if ( SDL_LockSurface(src) < 0 ) 
  153.         return;
  154.     if ( SDL_LockSurface(gScreen) < 0 ) 
  155.     {
  156.         SDL_UnlockSurface(src);
  157.         return;
  158.     }
  159.  
  160.     int i, j, c, yofs, xofs, imgwidth;
  161.     int xinc, yinc;
  162.     int srcpitch = src->pitch / 2;
  163.     imgwidth = (int)((640 << SHIFT_AMOUNT) * scale);
  164.     yinc = (int)(scale * (1 << SHIFT_AMOUNT));
  165.     xinc = (int)(scale * (1 << SHIFT_AMOUNT));
  166.     yofs = (int)((1 << SHIFT_AMOUNT) * ofsy);
  167.     xofs = (int)((1 << SHIFT_AMOUNT) * ofsx);
  168.     short *t = (short*)gScreen->pixels; 
  169.     for (i = 0; i < 480; i++)
  170.     {
  171.         // top and down clip
  172.         if (yofs < (1 << SHIFT_AMOUNT) || yofs > (598 << SHIFT_AMOUNT))
  173.         {
  174.             c = xofs;
  175.             for (j = 0; j < 640; j++)
  176.             {
  177.                 if (((c >> SHIFT_AMOUNT) & 31) == 0 || ((yofs >> SHIFT_AMOUNT) & 31) == 0)
  178.                     *t = 0x0300;
  179.                 else
  180.                     *t = 0x01e0;
  181.                 t++;
  182.                 c += xinc;
  183.             }
  184.         }
  185.         else // x min clip
  186.         if (xofs < (1 << SHIFT_AMOUNT))
  187.         {
  188.             int count = -(xofs / xinc) + 1;
  189.             if (count < 0) count = 0; // kludge
  190.             c = xofs;
  191.             for (j = 0; j < count; j++)
  192.             {
  193.                 if (((c >> SHIFT_AMOUNT) & 31) == 0 || ((yofs >> SHIFT_AMOUNT) & 31) == 0)
  194.                     *t = 0x0300;
  195.                 else
  196.                     *t = 0x01e0;
  197.                 t++;
  198.                 c += xinc;
  199.             }
  200.             for (j = count; j < 640; j++)
  201.             {
  202.                 int p = ((i & 1) + ((j & 1) << 1)) << 1;
  203.                 int v = (((yofs + unreal_dither[p]) >> SHIFT_AMOUNT) * srcpitch) << SHIFT_AMOUNT;
  204.                 v += c + unreal_dither[p+1];
  205.                 *t = ((short*)src->pixels)[(v) >> SHIFT_AMOUNT];
  206.                 t++; 
  207.                 c += xinc;
  208.             }
  209.         }
  210.         else // x max clip
  211.         if (xofs + imgwidth > (800 << SHIFT_AMOUNT))
  212.         {            
  213.             int count = 638 - (((xofs + imgwidth) - (800 << SHIFT_AMOUNT)) / xinc);
  214.             
  215.             c = xofs;
  216.             for (j = 0; j < count; j++)
  217.             {
  218.                 int p = ((i & 1) + ((j & 1) << 1)) << 1;
  219.                 int v = (((yofs + unreal_dither[p]) >> SHIFT_AMOUNT) * srcpitch) << SHIFT_AMOUNT;
  220.                 v += c + unreal_dither[p+1];
  221.                 *t = ((short*)src->pixels)[(v) >> SHIFT_AMOUNT];
  222.                 t++; 
  223.                 c += xinc;
  224.             }
  225.             for (j = count; j < 640; j++)
  226.             {
  227.                 if (((c >> SHIFT_AMOUNT) & 31) == 0 || ((yofs >> SHIFT_AMOUNT) & 31) == 0)
  228.                     *t = 0x0300;
  229.                 else
  230.                     *t = 0x01e0;
  231.                 t++;
  232.                 c += xinc;
  233.             }            
  234.         }
  235.         else // no clipping
  236.         {
  237.             c = xofs;
  238.             for (j = 0; j < 640; j++)
  239.             {
  240.                 int p = ((i & 1) + ((j & 1) << 1)) << 1;
  241.                 int v = (((yofs + unreal_dither[p]) >> SHIFT_AMOUNT) * srcpitch) << SHIFT_AMOUNT;
  242.                 v += c + unreal_dither[p+1];
  243.                 *t = ((short*)src->pixels)[(v) >> SHIFT_AMOUNT];
  244.                 t++; 
  245.                 c += xinc;
  246.             }
  247.         }
  248.         t += (gScreen->pitch / 2) - 640;
  249.         yofs += yinc;
  250.     }
  251.  
  252.  
  253.     SDL_UnlockSurface(src);
  254.     SDL_UnlockSurface(gScreen);
  255.  
  256. }
  257.