Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.01 KB | None | 0 0
  1. // Template, major revision 3, beta
  2. // IGAD/NHTV - Jacco Bikker - 2006-2009
  3.  
  4. #include "string.h"
  5. #include "game.h"
  6. #include "surface.h"
  7. #include "stdlib.h"
  8. #include "template.h"
  9. #include <assert.h>
  10.  
  11. using namespace Tmpl8;
  12.  
  13. DWORD startTicks = ::GetTickCount();
  14. DWORD elapsedTicks = 0;
  15. int numFrames=0;
  16.  
  17. void Game::Init()
  18. {
  19.     m_WobbleAngle = 0.0;
  20.     m_Image = new Surface( "vidink.tga" );
  21. }
  22.  
  23. void Game::Wobble( float angle )
  24. {
  25.     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!
  26.     assert( m_Image->GetPitch() == m_Image->GetWidth() );
  27.  
  28.     Pixel* dstPtr ;
  29.     Pixel* srcPtr;
  30.     int x;
  31.     int y;
  32.     float imX;
  33.     float imY;
  34.     unsigned int imXi;
  35.     unsigned int imYi;
  36.  
  37.     float Yangle;
  38.     float Xangle;
  39.  
  40.     Yangle = angle;
  41.  
  42.     dstPtr = m_Surface->GetBuffer();
  43.     srcPtr = m_Image->GetBuffer();
  44.  
  45.     const float wobbleSize = 7.0f;
  46.     const float angleIncrement = 0.01f;
  47.  
  48.     //****************************************
  49.     // HA1 - start making assembler HERE
  50.     //****************************************
  51.  
  52. #define ASSEMBLER
  53. #ifndef ASSEMBLER
  54.     for( y=0; y<SCRHEIGHT; y++ )
  55.     {
  56.         Xangle = Yangle;
  57.         for( x=0;x<SCRWIDTH; x++ )
  58.         {
  59.             imX = (float)x + sin(Xangle) * wobbleSize;
  60.             imY = (float)y + cos(Xangle) * wobbleSize;
  61.  
  62.             imXi = (unsigned int) (imX);
  63.             imYi = (unsigned int) (imY);
  64.  
  65.             imXi &= 511;    // same as imXi %= 512 (512 is width of the bitmap), but faster
  66.             imYi &= 511;    // same as imYi %= 512 (512 is height of the bitmap), but faster
  67.  
  68.             *dstPtr++ = srcPtr[ imYi*512 + imXi];   // set screen pixel to rotated and modulo'd rotated bitmap pixel
  69.  
  70.             Xangle += angleIncrement;
  71.         }
  72.  
  73.         Yangle += angleIncrement;
  74.     }
  75.  
  76. #else
  77.  
  78. __asm{
  79.     //Push eax, ebx and ecx to preserve them
  80.     //not sure if this is needed, but o well can't hurt
  81.     push eax
  82.     push ebx
  83.     push ecx
  84.     xor eax, eax
  85.  
  86.     //for( y=0; y<SCRHEIGHT; y++ )
  87.     mov y, eax //eax = 0
  88. yloop:
  89.         //Xangle = Yangle
  90.         push dword ptr [Yangle]
  91.         pop dword ptr [Xangle]
  92.  
  93.         //for( x=0;x<SCRWIDTH; x++  )
  94.         mov x, eax //eax = 0
  95.         xloop:
  96.                 fld dword ptr [Xangle]      //Load Xangle in ST(0)
  97.                 fsincos                     //ST(0) = Sin(Xangle) and ST(1) = Cos(Xangle)
  98.                 //fld st(0)                 //ST(3) = ST(0)
  99.                 //fld dword ptr [wobbleSize]  //ST(0) = wobbleSize
  100.  
  101.                 fmul dword ptr [wobbleSize] //ST(0) = ST(0) * wobbleSize
  102.                 fiadd dword ptr[x]          //ST(0) = ST(0)+ x
  103.                 fistp dword ptr[imXi]       //imXi = rounded integer of ST(0)  and ST(0) = ST(1)
  104.  
  105.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  106.                 fiadd dword ptr[y]          //FPU = y + FPU
  107.                 fistp dword ptr[imYi]       //imXi = rounded integer of ST(0)
  108.  
  109.                 //imX = (float)x + sin(Xangle) * wobbleSize
  110.                 /*fld dword ptr [Xangle]        //FPU = Xangle
  111.                 fsin                            //FPU = sin(Xangle)
  112.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  113.                 fiadd dword ptr[x]          //FPU = x + FPU
  114.                 //fst dword ptr[imX]            //imX = FPU
  115.                 //imXi = (unsigned int) (FPU);
  116.                 fistp dword ptr[imXi]           //imXi = rounded integer of FPU
  117.  
  118.                 //imY = (float)y + cos(Xangle) * wobbleSize
  119.                 fld dword ptr [Xangle]      //FPU = Xangle
  120.                 fcos                            //FPU = cos(Xangle)
  121.                 fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
  122.                 fiadd dword ptr[y]          //FPU = y + FPU
  123.                 //fst dword ptr[imY]            //imY = FPU
  124.                 //imYi = (unsigned int) (FPU);
  125.                 fistp dword ptr[imYi]           //imYi = rounded integer of FPU*/
  126.  
  127.                 //imXi &= 511;
  128.                 mov eax, dword ptr [imXi]
  129.                 and eax, 511
  130.                 mov dword ptr [imXi], eax
  131.  
  132.                 //imYi &= 511;
  133.                 mov eax, dword ptr [imYi]
  134.                 and eax, 511
  135.                 mov dword ptr [imYi], eax
  136.  
  137.                 //imYi*512 + imXi
  138.                 mov eax, dword ptr [imYi]
  139.                 shl eax, 9
  140.                 add eax, dword ptr [imXi]
  141.  
  142.                 //*dstPtr = srcPtr[ imYi*512 + imXi];
  143.                 mov ebx, dword ptr[srcPtr]
  144.                 mov eax, dword ptr [ebx+eax*4]
  145.                 mov ebx, dword ptr[dstPtr]
  146.                 mov dword ptr [ebx], eax
  147.  
  148.                 //dstPtr++
  149.                 add dstPtr, 4
  150.  
  151.                 //Xangle += angleIncrement;
  152.                 fld dword ptr [angleIncrement]
  153.                 fadd dword ptr [Xangle]
  154.                 fstp dword ptr [Xangle]
  155.  
  156.                 //x<SCRWIDTH
  157.                 mov eax, x
  158.                 sub eax, SCRWIDTH-1
  159.                 jz endxloop //jump out of the loop if x == SCRWIDTH
  160.                 inc x
  161.                 jmp xloop
  162.         endxloop:
  163.  
  164.         //Yangle += angleIncrement;
  165.         fld Yangle
  166.         fadd angleIncrement
  167.         fstp Yangle
  168.  
  169.         //y<SCRHEIGHT
  170.         mov eax, y
  171.         sub eax, SCRHEIGHT-1
  172.         jz endyloop //jump out of the loop if x == SCRWIDTH
  173.         inc y
  174.         xor eax, eax //eax = 0
  175.         jmp yloop
  176. endyloop:
  177.  
  178.     //Pop eax, ebx and ecx back from the stack to restore
  179.     pop ecx
  180.     pop ebx
  181.     pop eax
  182.  
  183. }
  184. #endif
  185.  
  186.  
  187.  
  188.     //****************************************
  189.     // HA1 - stop making assembler HERE
  190.     //****************************************
  191. }
  192.  
  193. void Game::Tick( float a_DT )
  194. {
  195.     numFrames++;
  196.     m_WobbleAngle += (double) a_DT * 0.01;
  197.  
  198.     Wobble( (float)m_WobbleAngle );
  199.  
  200.     if(numFrames == 100)
  201.     {
  202.         elapsedTicks = ::GetTickCount() - startTicks;
  203.         float fps = (float) numFrames / ((float) elapsedTicks / 1000.0f );
  204.  
  205.         char tmps[256];
  206.         sprintf(tmps,"Average FPS: %f",fps);
  207.         MessageBox(NULL, tmps, "report", MB_OK );
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement