低级定时控制:
1.定时器跟踪时间的所遇到的一些困难:
首先、定时器传递消息
第二、定时器并不那么准确
第三、在大多数游戏循环中,都希望能够强制主程序以指定桢数运行,而且桢数不能过高,定时器在这方面并不擅长。
2.游戏锁定桢数,需要查询系统时钟,然后运行差动测试来检查过去了多少时间。
Win32 API 具有这样的功能,称之为GetTickCount();
原型:DWORD GetTickCount(void)
返回:返回从Windows启动后的毫秒数
3例子:看下面是如何确认程序块以准确的30fps或33.33毫秒的延迟方式运行:
//get the starting time
DWORD start_time = GetTickCount();
//do work,draw frame,whatever
//now wait until 33 milliseconds has elapsed
while((GetTickCount() - start_time)<33)
4一个类似屏幕保护的程序例子:
// DEMO2_4.CPP - A complete windows program with a slighty
// different main event loop
// INCLUDES ///
#define WIN32_LEAN_AND_MEAN // just say no to MFC
#include <windows.h> // include all the windows headers
#include <windowsx.h> // include useful macros
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// DEFINES
// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
// GLOBALS
// FUNCTIONS //
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context
// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
// return success
return(0);
} break;
case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
// you would do all your painting here
EndPaint(hwnd,&ps);
// return success
return(0);
} break;
case WM_DESTROY:
{
// kill the application, this sends a WM_QUIT message
PostQuitMessage(0);
// return success
return(0);
} break;
default:break;
} // end switch
// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));
} // end WinProc
// WINMAIN
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
// first fill in the window class stucture
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
// register the window class
if (!RegisterClassEx(&winclass))
return(0);
// create the window
if (!(hwnd = CreateWindowEx(NULL, // extended style
WINDOW_CLASS_NAME, // class
"Your Basic Window++", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // initial x,y
400,400, // initial width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance of this application
NULL))) // extra creation parms
return(0);
// enter main event loop, but this time we use PeekMessage()
// instead of GetMessage() to retrieve messages
HDC hdc;
int x1 = rand()%100;
int x2 = 100 + rand()%100;
int y1 = rand()%100;
int y2 = 100 + rand()%100;
int vx1 = -2 + rand()%5;
int vy1 = -2 + rand()%5;
int vx2 = -2 + rand()%5;
int vy2 = -2 + rand()%5;
int count=0;
DWORD start_time;
HPEN p = CreatePen(PS_SOLID,1,RGB(rand()%256,rand()%256,rand()%256));
RECT rect;
GetClientRect(hwnd,&rect);
hdc = GetDC(hwnd);
while(TRUE)
{
// test if there is a message in queue, if so get it
start_time = GetTickCount();
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&msg);
// send the message to the window proc
DispatchMessage(&msg);
} // end if
// main game processing goes here
// Game_Main(); // or whatever your loop is called
count++;
if(count>=100)
{
count = 0;
p = CreatePen(PS_SOLID,1,RGB(rand()%256,rand()%256,rand()%256));
}
SelectObject(hdc,p);
if((x1+vx1)<0||(x1+vx1)>400)
{
vx1=-vx1;
x1 = x1 + vx1;
}
else
x1 = x1 + vx1;
if((x2+vx2)<0||(x2+vx2)>400)
{
vx2=-vx2;
x2 = x2 + vx2;
}
else
x2 = x2 + vx2;
if((y2+vy2)<0||(y2+vy2)>400)
{
vy2=-vy2;
y2 = y2 + vy2;
}
else
y2 = y2 + vy2;
if((y1+vy1)<0||(y1+vy1)>400)
{
vy1=-vy1;
y1 = y1 + vy1;
}
else
y1 = y1 + vy1;
MoveToEx(hdc,x1,y1,NULL);
LineTo(hdc,x2,y2);
while((GetTickCount() - start_time)<33);
} // end while
ReleaseDC(hwnd,hdc);
// return to Windows like this
return(msg.wParam);
} // end WinMain
///
该程序设计2个需要注意的知识点:(1)桢数锁定
(2)基础的碰撞检测
5当然,如果仅仅是延迟你的程序,可以使用Win32 API函数Sleep().只要将希望延迟的毫秒数的时间延迟传递给该函数,该程序就将执行。例如,要延迟1.0秒,应当说Sleep(1000).