程序代码:
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#define WINDOW_X 1440
#define WINDOW_Y 900
#define SIZE 500
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wprarm,LPARAM lprarm);
int Game_Init(void);
void Draw_Stars(void);
void Erase_Stars(void);
void Move_Stars(void);
int Game_Main(void);
int Game_ShutDown(void);
HDC window_hdc;
HWND window_hwnd;
typedef struct starxy
{
int x;
int y;
int speed;
}STAR;
STAR stars[SIZE];
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
static TCHAR classname[]=TEXT("window");
HWND hwnd;
MSG msg;
WNDCLASS winclass;
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WndProc;
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 = classname;
if(!RegisterClass(&winclass))
return 0;
if (!(hwnd = CreateWindow(
classname,
"T3D Game Console Star Demo",
WS_POPUP | WS_VISIBLE,
0,0,
WINDOW_X,WINDOW_Y,
NULL,
NULL,
hInstance,
NULL)))
return(0);
window_hwnd = hwnd;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
Game_Init();
while(TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(KEYDOWN(VK_ESCAPE))
PostQuitMessage(0);
Game_Main();
}
Game_ShutDown();
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wprarm,LPARAM lprarm)
{
HDC hdc;
PAINTSTRUCT ps;
switch(msg)
{
case WM_CREATE:
{
return 0;
}
case WM_PAINT:
{
hdc=BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return (DefWindowProc(hwnd,msg,wprarm,lprarm));
}
int Game_Init()
{
window_hdc = GetDC(window_hwnd);
for(int i=0;i<SIZE;i++)
{
stars[i].x = rand()%WINDOW_X;
stars[i].y = rand()%WINDOW_Y;
stars[i].speed = -2 + rand()%5;
}
return 0;
}
void Draw_Stars()
{
for (int i=0; i < SIZE; i++)
SetPixel(window_hdc, stars[i].x, stars[i].y, RGB(222,i,222));
}
void Erase_Stars(void)
{
for (int i=0; i < SIZE; i++)
SetPixel(window_hdc, stars[i].x, stars[i].y, RGB(0,0,0));
}
void Move_Stars(void)
{
for (int i=0; i < SIZE; i++)
{
stars[i].x+=stars[i].speed;
if (stars[i].x >= WINDOW_X)
stars[i].x -= WINDOW_X;
}
}
int Game_Main()
{
DWORD start_time = GetTickCount();
Erase_Stars();
Move_Stars();
Draw_Stars();
while((start_time - GetTickCount() < 33));
if (KEYDOWN(VK_ESCAPE))
SendMessage(window_hwnd,WM_CLOSE,0,0);
return 0;
}
int Game_ShutDown(void)
{
ReleaseDC(window_hwnd,window_hdc);
return 0;
}