Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Template, major revision 3, beta
- // IGAD/NHTV - Jacco Bikker - 2006-2009
- #include "string.h"
- #include "game.h"
- #include "surface.h"
- #include "stdlib.h"
- #include "template.h"
- #include <assert.h>
- using namespace Tmpl8;
- DWORD startTicks = ::GetTickCount();
- DWORD elapsedTicks = 0;
- int numFrames=0;
- void Game::Init()
- {
- m_WobbleAngle = 0.0;
- m_Image = new Surface( "vidink.tga" );
- }
- void Game::Wobble( float angle )
- {
- 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!
- assert( m_Image->GetPitch() == m_Image->GetWidth() );
- Pixel* dstPtr ;
- Pixel* srcPtr;
- int x;
- int y;
- float imX;
- float imY;
- unsigned int imXi;
- unsigned int imYi;
- float Yangle;
- float Xangle;
- Yangle = angle;
- dstPtr = m_Surface->GetBuffer();
- srcPtr = m_Image->GetBuffer();
- const float wobbleSize = 7.0f;
- const float angleIncrement = 0.01f;
- //****************************************
- // HA1 - start making assembler HERE
- //****************************************
- #define ASSEMBLER
- #ifndef ASSEMBLER
- for( y=0; y<SCRHEIGHT; y++ )
- {
- Xangle = Yangle;
- for( x=0;x<SCRWIDTH; x++ )
- {
- imX = (float)x + sin(Xangle) * wobbleSize;
- imY = (float)y + cos(Xangle) * wobbleSize;
- imXi = (unsigned int) (imX);
- imYi = (unsigned int) (imY);
- imXi &= 511; // same as imXi %= 512 (512 is width of the bitmap), but faster
- imYi &= 511; // same as imYi %= 512 (512 is height of the bitmap), but faster
- *dstPtr++ = srcPtr[ imYi*512 + imXi]; // set screen pixel to rotated and modulo'd rotated bitmap pixel
- Xangle += angleIncrement;
- }
- Yangle += angleIncrement;
- }
- #else
- __asm{
- //Push eax, ebx and ecx to preserve them
- //not sure if this is needed, but o well can't hurt
- push eax
- push ebx
- push ecx
- xor eax, eax
- //for( y=0; y<SCRHEIGHT; y++ )
- mov y, eax //eax = 0
- yloop:
- //Xangle = Yangle
- push dword ptr [Yangle]
- pop dword ptr [Xangle]
- //for( x=0;x<SCRWIDTH; x++ )
- mov x, eax //eax = 0
- xloop:
- fld dword ptr [Xangle] //Load Xangle in ST(0)
- fsincos //ST(0) = Sin(Xangle) and ST(1) = Cos(Xangle)
- //fld st(0) //ST(3) = ST(0)
- //fld dword ptr [wobbleSize] //ST(0) = wobbleSize
- fmul dword ptr [wobbleSize] //ST(0) = ST(0) * wobbleSize
- fiadd dword ptr[x] //ST(0) = ST(0)+ x
- fistp dword ptr[imXi] //imXi = rounded integer of ST(0) and ST(0) = ST(1)
- fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
- fiadd dword ptr[y] //FPU = y + FPU
- fistp dword ptr[imYi] //imXi = rounded integer of ST(0)
- //imX = (float)x + sin(Xangle) * wobbleSize
- /*fld dword ptr [Xangle] //FPU = Xangle
- fsin //FPU = sin(Xangle)
- fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
- fiadd dword ptr[x] //FPU = x + FPU
- //fst dword ptr[imX] //imX = FPU
- //imXi = (unsigned int) (FPU);
- fistp dword ptr[imXi] //imXi = rounded integer of FPU
- //imY = (float)y + cos(Xangle) * wobbleSize
- fld dword ptr [Xangle] //FPU = Xangle
- fcos //FPU = cos(Xangle)
- fmul dword ptr [wobbleSize] //FPU = FPU * wobbleSize
- fiadd dword ptr[y] //FPU = y + FPU
- //fst dword ptr[imY] //imY = FPU
- //imYi = (unsigned int) (FPU);
- fistp dword ptr[imYi] //imYi = rounded integer of FPU*/
- //imXi &= 511;
- mov eax, dword ptr [imXi]
- and eax, 511
- mov dword ptr [imXi], eax
- //imYi &= 511;
- mov eax, dword ptr [imYi]
- and eax, 511
- mov dword ptr [imYi], eax
- //imYi*512 + imXi
- mov eax, dword ptr [imYi]
- shl eax, 9
- add eax, dword ptr [imXi]
- //*dstPtr = srcPtr[ imYi*512 + imXi];
- mov ebx, dword ptr[srcPtr]
- mov eax, dword ptr [ebx+eax*4]
- mov ebx, dword ptr[dstPtr]
- mov dword ptr [ebx], eax
- //dstPtr++
- add dstPtr, 4
- //Xangle += angleIncrement;
- fld dword ptr [angleIncrement]
- fadd dword ptr [Xangle]
- fstp dword ptr [Xangle]
- //x<SCRWIDTH
- mov eax, x
- sub eax, SCRWIDTH-1
- jz endxloop //jump out of the loop if x == SCRWIDTH
- inc x
- jmp xloop
- endxloop:
- //Yangle += angleIncrement;
- fld Yangle
- fadd angleIncrement
- fstp Yangle
- //y<SCRHEIGHT
- mov eax, y
- sub eax, SCRHEIGHT-1
- jz endyloop //jump out of the loop if x == SCRWIDTH
- inc y
- xor eax, eax //eax = 0
- jmp yloop
- endyloop:
- //Pop eax, ebx and ecx back from the stack to restore
- pop ecx
- pop ebx
- pop eax
- }
- #endif
- //****************************************
- // HA1 - stop making assembler HERE
- //****************************************
- }
- void Game::Tick( float a_DT )
- {
- numFrames++;
- m_WobbleAngle += (double) a_DT * 0.01;
- Wobble( (float)m_WobbleAngle );
- if(numFrames == 100)
- {
- elapsedTicks = ::GetTickCount() - startTicks;
- float fps = (float) numFrames / ((float) elapsedTicks / 1000.0f );
- char tmps[256];
- sprintf(tmps,"Average FPS: %f",fps);
- MessageBox(NULL, tmps, "report", MB_OK );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement