Jump to content

  • Log In with Google      Sign In   
  • Create Account

Aerodactyl55

Member Since 07 Jun 2009
Offline Last Active Oct 31 2013 09:40 AM

#5008038 game loop

Posted by Aerodactyl55 on 07 December 2012 - 02:20 AM

After so much frustration this is what worked for me:


int accumulator = ( int )( ( ( tNow - tPrev ) * 1000 ) / tFreq );
tPrev = tNow;

while( accumulator > 0 )
{
	 Update( DELTA_TIME )
	 accumulator -= UPDATE_TICKS;
}

//Compensate for the extra update loop that consumes the remainder
if( accumulator != 0 ) Sleep( abs( accumulator ) );

Render();


And i removed the interpolation.
Thanks everyone.


#5007160 game loop

Posted by Aerodactyl55 on 04 December 2012 - 12:55 PM

I recently decided to switch from a variable step to a fixed step game loop. So i created a small application to test the new loop. I dont know why but the new loop does not produce smooth movement. I hope maybe someone here will spot a mistake because i cant find it myself.

Here is the code:

#include "core.h"

struct STAR
{
IDirect3DTexture9* Tex;
D3DXVECTOR2   Pos;
D3DXVECTOR2   prevPos;
D3DXVECTOR2   Vel;
};

LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
BOOL Init();
void Update( float dt );
void Render();
void Cleanup();

Core::CGraphics gfx;
Core::CSprite sprite;
STAR star;

ULONGLONG tNow = 0;
ULONGLONG tPrev = 0;
ULONGLONG tFreq = 0;
DWORD accumulator = 0;
const int UPDATE_TICKS = 10;
const float DELTA_TIME = UPDATE_TICKS * 0.001f;
float lerp = 0.0f;
wchar_t str[64] = L"";

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{
if( !gfx.InitWindow( hInstance, WndProc, L"Game Loop Test", nCmdShow ) ) return 0;
if( !Init() )
{
  gfx.MsgBoxError( L"Init() Failed!" );
  return 0;
}
MSG msg = { 0 };
QueryPerformanceFrequency( ( LARGE_INTEGER* )&tFreq );
QueryPerformanceCounter( ( LARGE_INTEGER* )&tPrev );
float c = 1.0f / tFreq;
while( WM_QUIT != msg.message )
{
  if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
  {
   TranslateMessage( &msg );
   DispatchMessage( &msg );
  }
  else
  {
   QueryPerformanceCounter( ( LARGE_INTEGER* )&tNow );
   accumulator += ( DWORD )( ( ( tNow - tPrev ) * 1000 ) / tFreq );
   tPrev = tNow;
   while( accumulator >= UPDATE_TICKS )
   {
	Update( DELTA_TIME );
	accumulator -= UPDATE_TICKS;
   }
   lerp = accumulator / ( float )UPDATE_TICKS;
   Render();
  }
}
return static_cast< int >( msg.wParam );
}

BOOL Init()
{
if( !gfx.CreateDirect3D9() ) return FALSE;
if( FAILED( gfx.CreateDevice9() ) ) return FALSE;
if( FAILED( sprite.Init( &gfx ) ) ) return FALSE;
ZeroMemory( &star, sizeof( STAR ) );
if( FAILED( gfx.LoadTextureFromFile( L"star.png", &star.Tex ) ) ) return FALSE;
star.Pos.y = 284.0f;
star.Vel.x = 500.0f;
star.prevPos = star.Pos;
return TRUE;
}

void Update( float dt )
{
star.prevPos = star.Pos;
star.Pos = star.Pos + star.Vel * dt;
if( star.Pos.x + 32.0f > 800.0f )
{
  star.Pos.x = 768.0f;
  star.Vel.x = -star.Vel.x;
  star.prevPos = star.Pos;
}
else if( star.Pos.x < 0.0f )
{
  star.Pos.x = 0.0f;
  star.Vel.x = -star.Vel.x;
  star.prevPos = star.Pos;
}
}

void Render()
{
gfx.Clear();
gfx.BeginScene();
sprite.Begin();
D3DXVECTOR2 p;
p = star.prevPos * lerp + star.Pos * ( 1.0f - lerp );

sprite.Translate( &p );
sprite.Draw( star.Tex, NULL, DODGERBLUE_COLOR );
sprite.End();
gfx.EndScene();
gfx.Present();
}
void Cleanup()
{
Core::ReleaseCOM( star.Tex );
}

LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); break;
default: return DefWindowProc( hwnd, msg, wParam, lParam );
}
return 0;
}



#4851701 Sprites and D3D9

Posted by Aerodactyl55 on 20 August 2011 - 01:39 PM

1. I've noticed that the texture is scaling to the closest 64x64, 128x128, 256x256, 512x512, 1024x1024 etc. If my texture is 300 in height, it's going to be scaled to 512 in height. Can I disable this somehow?

Try using D3DXCreateTextureFromFileEx() instead of the D3DXCreateTextureFromFile().


#4851537 Sprites and D3D9

Posted by Aerodactyl55 on 20 August 2011 - 03:21 AM

You must create the sprite before using it. During Initialization call the D3DXCreateSprite() function after creating the device.


PARTNERS