Jump to content
  • Advertisement
Sign in to follow this  
Claymz

C++ Win32 game

This topic is 2470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all!

Ive started making a game, similar to this one: http://www.newground...tal/view/409137, except im making it with Win32 and C++. Ive made the moving line with dots, that is, every frame a new dot is printed on screen, and because its fast, it seems like its a line. Is there any better way to do that? Also, when steering, if i hold down key 'Q' or 'E' (for left and right), how could i achieve so that there would be a max steering angle, so that if i hold one of these buttons, i would collide with my line, because now when i hold it down, i just keep getting in a more and more tight circle, like so: http://imageshack.us/photo/my-images/831/circleu.png/. Also, when using steering my velocity keeps getting lower, so im adding code for rotation aswell, if maybe something is wrong with it (im pretty sure this is also the cause of getting into smaller and smaller circle, as shown in the picture above):

Rotation code:

Vec2& Vec2::rotate(float t)
{
x = x * cosf(t) - y * sinf(t); //x represents the first component of the Vec2 direction(0.0f,-120.0f);
y = y * cosf(t) + x * sinf(t); //y represents the second component of the Vec2 direction(0.0f,-120.0f);
return *this;
}


Globals and window procedure:

BackBuffer* gBackBuffer = 0;
const int gClientWidth = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
const int gClientHeight = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
Vec2 player(500.0f,500.0f);
Vec2 direction(0.0f,-120.0f);
float angle = 0.0f;

LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc = 0;
switch( msg )
{
case WM_CREATE:

// Create the backbuffer.
gBackBuffer = new BackBuffer(hWnd, gClientWidth,gClientHeight);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(ghMainWnd);
break;
case 'Q':
direction.rotate(-0.1f);
break;
case 'E':
direction.rotate(0.1f);
break;
}
return 0;
case WM_DESTROY:
delete gBackBuffer;
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}



Message and game loop:

MSG msg;
ZeroMemory(&msg, sizeof(MSG));

// Get the performance timer frequency.
__int64 cntsPerSec = 0;
bool perfExists = QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec)!=0;
if( !perfExists )
{
MessageBox(0, "Performance timer does not exist!", 0, 0);
return 0;
}

double timeScale = 1.0 / (double)cntsPerSec;

// Get the current time.
__int64 lastTime = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&lastTime);

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{

// Get time
__int64 currTime = 0;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);

double deltaTime = (double)(currTime - lastTime) * timeScale;

// backbuffer DC
HDC bbDC = gBackBuffer->getDC();

Vec2 velocity = direction*2.0f;
player += deltaTime*velocity;

// Paint the dot to be controlled
HBRUSH oldBrush = (HBRUSH)SelectObject(bbDC, GetStockObject(BLACK_BRUSH));
Ellipse(bbDC, player.x-4, player.y-4, player.x+4, player.y+4);
SelectObject(bbDC, oldBrush);

gBackBuffer->present();
lastTime = currTime;
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!