Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.24 KB | None | 0 0
  1. void Game::Wobble( float angle )
  2. {
  3.     assert( m_Surface->GetPitch() == m_Surface->GetWidth() );       // if the buffer width and pixel width are not the same, the following code will not work because it assumes they are!
  4.     assert( m_Image->GetPitch() == m_Image->GetWidth() );
  5.  
  6.     Pixel* dstPtr ;
  7.     Pixel* srcPtr;
  8.     int x;
  9.     int y;
  10.     float imX;
  11.     float imY;
  12.     unsigned int imXi;
  13.     unsigned int imYi;
  14.  
  15.     float Yangle;
  16.     float Xangle;
  17.  
  18.     Yangle = angle;
  19.  
  20.     dstPtr = m_Surface->GetBuffer();
  21.     srcPtr = m_Image->GetBuffer();
  22.  
  23.     const float wobbleSize = 7.0f;
  24.     const float angleIncrement = 0.01f;
  25.  
  26.     //****************************************
  27.     // HA1 - start making assembler HERE
  28.     //****************************************
  29.  
  30. #define ASSEMBLER
  31. #ifndef ASSEMBLER
  32.     for( y=0; y<SCRHEIGHT; y++ )
  33.     {
  34.         Xangle = Yangle;
  35.         for( x=0;x<SCRWIDTH; x++ )
  36.         {
  37.             imX = (float)x + sin(Xangle) * wobbleSize;
  38.             imY = (float)y + cos(Xangle) * wobbleSize;
  39.  
  40.             imXi = (unsigned int) (imX);
  41.             imYi = (unsigned int) (imY);
  42.  
  43.             imXi &= 511;    // same as imXi %= 512 (512 is width of the bitmap), but faster
  44.             imYi &= 511;    // same as imYi %= 512 (512 is height of the bitmap), but faster
  45.  
  46.             *dstPtr++ = srcPtr[ imYi*512 + imXi];   // set screen pixel to rotated and modulo'd rotated bitmap pixel
  47.  
  48.             Xangle += angleIncrement;
  49.         }
  50.  
  51.         Yangle += angleIncrement;
  52.     }
  53.  
  54. #else
  55.  
  56. __asm{
  57.     //Push eax, ebx and ecx to preserve them
  58.     //not sure if this is needed, but o well can't hurt
  59.     push eax
  60.     push ebx
  61.     push ecx
  62.     xor eax, eax
  63.  
  64.     //for( y=0; y<SCRHEIGHT; y++ )
  65.     mov y, eax //eax = 0
  66. yloop:
  67.         //Xangle = Yangle
  68.         push dword ptr [Yangle]
  69.         pop dword ptr [Xangle]
  70.  
  71.         //for( x=0;x<SCRWIDTH; x++  )
  72.         mov x, eax //eax = 0
  73.         xloop:
  74.                 fld dword ptr [Xangle]      //Load Xangle in ST(0)
  75.                 fsincos                     //ST(0) = Sin(Xangle) and ST(1) = Cos(Xangle)
  76.                 //fld st(0)                 //ST(3) = ST(0)
  77.                 //fld dword ptr [wobbleSize]  //ST(0) = wobbleSize
  78.  
  79.                 fmul dword ptr [wobbleSize] //ST(0) = ST(0) * wobbleSize
  80.                 fiadd dword ptr[x]          //ST(0) = ST(0)+ x
  81.                 fistp dword ptr[imXi]       //imXi = rounded integer of ST(0)  and ST(0) = ST(1)
  82.  
  83.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  84.                 fiadd dword ptr[y]          //FPU = y + FPU
  85.                 fistp dword ptr[imYi]       //imXi = rounded integer of ST(0)
  86.  
  87.                 //imX = (float)x + sin(Xangle) * wobbleSize
  88.                 /*fld dword ptr [Xangle]        //FPU = Xangle
  89.                 fsin                            //FPU = sin(Xangle)
  90.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  91.                 fiadd dword ptr[x]          //FPU = x + FPU
  92.                 //fst dword ptr[imX]            //imX = FPU
  93.                 //imXi = (unsigned int) (FPU);
  94.                 fistp dword ptr[imXi]           //imXi = rounded integer of FPU
  95.  
  96.                 //imY = (float)y + cos(Xangle) * wobbleSize
  97.                 fld dword ptr [Xangle]      //FPU = Xangle
  98.                 fcos                            //FPU = cos(Xangle)
  99.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  100.                 fiadd dword ptr[y]          //FPU = y + FPU
  101.                 //fst dword ptr[imY]            //imY = FPU
  102.                 //imYi = (unsigned int) (FPU);
  103.                 fistp dword ptr[imYi]           //imYi = rounded integer of FPU*/
  104.  
  105.                 //imXi &= 511;
  106.                 mov eax, dword ptr [imXi]
  107.                 and eax, 511
  108.                 mov dword ptr [imXi], eax
  109.  
  110.                 //imYi &= 511;
  111.                 mov eax, dword ptr [imYi]
  112.                 and eax, 511
  113.                 mov dword ptr [imYi], eax
  114.  
  115.                 //imYi*512 + imXi
  116.                 mov eax, dword ptr [imYi]
  117.                 shl eax, 9
  118.                 add eax, dword ptr [imXi]
  119.  
  120.                 //*dstPtr = srcPtr[ imYi*512 + imXi];
  121.                 mov ebx, dword ptr[srcPtr]
  122.                 mov eax, dword ptr [ebx+eax*4]
  123.                 mov ebx, dword ptr[dstPtr]
  124.                 mov dword ptr [ebx], eax
  125.  
  126.                 //dstPtr++
  127.                 add dstPtr, 4
  128.  
  129.                 //Xangle += angleIncrement;
  130.                 fld dword ptr [angleIncrement]
  131.                 fadd dword ptr [Xangle]
  132.                 fstp dword ptr [Xangle]
  133.  
  134.                 //x<SCRWIDTH
  135.                 mov eax, x
  136.                 sub eax, SCRWIDTH-1
  137.                 jz endxloop //jump out of the loop if x == SCRWIDTH
  138.                 inc x
  139.                 jmp xloop
  140.         endxloop:
  141.  
  142.         //Yangle += angleIncrement;
  143.         fld Yangle
  144.         fadd angleIncrement
  145.         fstp Yangle
  146.  
  147.         //y<SCRHEIGHT
  148.         mov eax, y
  149.         sub eax, SCRHEIGHT-1
  150.         jz endyloop //jump out of the loop if x == SCRWIDTH
  151.         inc y
  152.         xor eax, eax //eax = 0
  153.         jmp yloop
  154. endyloop:
  155.  
  156.     //Pop eax, ebx and ecx back from the stack to restore
  157.     pop ecx
  158.     pop ebx
  159.     pop eax
  160.  
  161. }
  162. #endif
  163.  
  164.  
  165.  
  166.     //****************************************
  167.     // HA1 - stop making assembler HERE
  168.     //****************************************
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement