函数问题,我还是弄不懂?
//Game.h#pragma once
#include<windows.h>
#include <math.h>
//#define FX(x) 300-(100*sin(x))
class CGame
{
public:
CGame(HWND hWnd,int fps=1)
{
hwnd=hWnd;
nFps=fps;
Init();
}
void Update();
void Init();
virtual void Render();
protected:
virtual void Update(float fDeltaTime);
int nFps;
unsigned int nFrames;
DWORD t0;
float t;
int nFixedDeltaTime;
HWND hwnd;
private:
float m_x;
};
//Game.cpp
#include "Game.h"
void CGame::Init()
{
nFixedDeltaTime=1000/nFps;
t0=GetTickCount();
t = 0;
nFrames = 0;
m_x=0;
}
void CGame::Update()
{
DWORD dt = 0,t1;
t1=GetTickCount();
dt=t1 - t0;
if (dt >= nFixedDeltaTime)
{
t0 = t1;
Update(dt / 1000.0);
t += dt / 1000.0;
Render();
nFrames++;
}
}
void CGame::Update(float fDeltaTime)
{
m_x+=1;
if(m_x>800)m_x=0;
}
void CGame::Render()
{
float x=0,y=0;
RECT rect ={0,0,800,600};
HDC hdc=GetDC(hwnd);
HPEN hOldPen,hPen=CreatePen(PS_SOLID, 15, RGB(0,255,255));
hOldPen=(HPEN)SelectObject(hdc,hPen);
//SelectObject(hdc,hOldPen);
FillRect(hdc,&rect,(HBRUSH)GetStockObject(LTGRAY_BRUSH));
for(int i=0;i<m_x;i++){
x=i*3.1415926f/180.0f;
y=300-(100*sin(x));
//SetPixel(hdc,i,y,RGB(0,255,0));
Ellipse(hdc,i,y,i+10,y+10);
}
ReleaseDC(hwnd,hdc);
}
//main.cpp
#include "Game.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM );
CGame *pMyGame;
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("Hello Game");
WNDCLASS wndclass;
HWND hwnd;
MSG msg;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=NULL;
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL,TEXT("注册失败!"),szAppName,MB_ICONERROR);
return -1;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,800,600,
NULL,
NULL,
hInstance,
NULL );
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
pMyGame=new CGame(hwnd,60);
msg.message = WM_NULL;
BOOL bMessage;
PeekMessage(&msg, NULL, 0, 0,PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
bMessage = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if(bMessage)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
pMyGame->Update();
}
if (pMyGame)
{
delete pMyGame;
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
请问Game.h,Game.cpp和main.cpp是怎样执行的,还有那个Init()函数什么时候执行的?(尽量详细一点)