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

Topics I've Started

game loop

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;
}

QueryPerformanceFrequency/QueryPerformanceCounter

18 March 2012 - 06:10 AM

MSDN says that the frequency cannot change while the system is running, but i have seen some claims that it does change. So is it safe to use them? And what are the other available options?

a beginner needing some feedback

09 August 2010 - 12:28 PM

This project/game is a "step up" from my previous project( Tic-Tac-Toe ), the game is called Memory and the objective is to match pairs of equal symbols in a grid. As for the game itself i would like to get feedback on the overall appearence/playability of the game.
This game is the first time i handle lost devices in Direct3D9, the game passed the tests i made on responding to a lost device but if anyone can confirm that the game handles lost devices effectively i would really appreciate it!
You can download the game from the link below:
Memory

Screenshot:


Thanks in advance

Loading Screens

03 August 2010 - 07:59 AM

How can i render an animation while loading level data, save files, 3d models, etc...?
The only idea i have on this is to use multiple threads but i dont know how to program multithreaded applications so anyone has a different idea?

Just another tic-tac-toe

03 August 2010 - 12:43 AM

After seeing other users here on gamedev successfully developing a Tic-tac-toe game, i decided to give it a shot!
I would like to hear what you guys think of the game, its implementatation and what you think is the next step for me.
And i would also like to know if anyone has a better method of checking for a win/draw condition in TTT than the one i used? The source code is big so i will not post it.
You can download the game and the source code from the links below:

Tic-Tac-Toe
TTT Source

Thanks!

PARTNERS