您的位置:首页 > 其它

[游戏模版19] Win32 物理引擎 匀速运动

2014-05-18 14:26 288 查看
>_<:Learning the physical engine

>_<:resource

>_<:code



#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <cstdio>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <time.h>
#include <string>
#include <cmath>

// 全局变量:
HINSTANCE hInst;                                // 当前实例
HBITMAP bg , ball[2];
HDC hdc,mdc,bufdc;
HWND hWnd;
DWORD tPre,tNow,tCheck;
RECT rect;//窗口矩形
int x[2];
int y[2];
int vx[2];
int vy[2];

// 此代码模块中包含的函数的前向声明:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
void                MyPaint(HDC hdc);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR    lpCmdLine,
int       nCmdShow){

MSG msg;
MyRegisterClass(hInstance);
// 执行应用程序初始化:
if (!InitInstance (hInstance, nCmdShow)){
return FALSE;
}
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}

//  函数: MyRegisterClass()
//
//  目的: 注册窗口类。
ATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style            = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc    = WndProc;
wcex.cbClsExtra        = 0;
wcex.cbWndExtra        = 0;
wcex.hInstance        = hInstance;
wcex.hIcon            = NULL;
wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName    = "Beautifulzzzz";
wcex.lpszClassName    = "Beautifulzzzz";
wcex.hIconSm        = NULL;

return RegisterClassEx(&wcex);
}

//
//   函数: InitInstance(HINSTANCE, int)
//
//   目的: 保存实例句柄并创建主窗口
//
//   注释:
//
//        在此函数中,我们在全局变量中保存实例句柄并
//        创建和显示主程序窗口。
//        棋盘拼接以及调用InitGame()开始棋局
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
HBITMAP bmp;
hInst = hInstance; // 将实例句柄存储在全局变量中

hWnd = CreateWindow("Beautifulzzzz","Beautifulzzzz", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

MoveWindow(hWnd,10,10,600,450,true);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

hdc=GetDC(hWnd);
mdc=CreateCompatibleDC(hdc);
bufdc=CreateCompatibleDC(hdc);

bmp=CreateCompatibleBitmap(hdc,600,480);
SelectObject(mdc,bmp);

bg=(HBITMAP)LoadImageA(NULL,"bg.bmp",IMAGE_BITMAP,640,480,LR_LOADFROMFILE);
ball[0]=(HBITMAP)LoadImageA(NULL,"ball0.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE);
ball[1]=(HBITMAP)LoadImageA(NULL,"ball1.bmp",IMAGE_BITMAP,52,26,LR_LOADFROMFILE);

GetClientRect(hWnd,&rect);//取得内部窗口区域的大小;

x[0]=50;y[0]=50;vx[0]=4;vy[0]=4;
x[1]=380;y[1]=380;vx[1]=-4;vy[1]=-4;

SetTimer(hWnd,1,10,NULL);
MyPaint(hdc);

return TRUE;
}

//
//  函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  目的: 处理主窗口的消息。
//
//  WM_COMMAND    - 处理应用程序菜单
//  WM_PAINT    - 绘制主窗口
//  WM_DESTROY    - 发送退出消息并返回
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int wmId, wmEvent;
PAINTSTRUCT ps;

switch (message){
case WM_TIMER:
A:MyPaint(hdc);
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
goto A;// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DeleteDC(mdc);
DeleteDC(bufdc);
DeleteObject(bg);
DeleteObject(ball[0]);
DeleteObject(ball[1]);

KillTimer(hWnd,1);
ReleaseDC(hWnd,hdc);

PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

//MyPaint()
//1、窗口贴图
//2、计算小球贴图坐标并判断小球是否碰撞窗口边缘
void MyPaint(HDC hdc){
SelectObject(bufdc,bg);
BitBlt(mdc,0,0,640,480,bufdc,0,0,SRCCOPY);

SelectObject(bufdc,ball[0]);
BitBlt(mdc,x[0],y[0],26,26,bufdc,26,0,SRCAND);
BitBlt(mdc,x[0],y[0],26,26,bufdc,0,0,SRCPAINT);

SelectObject(bufdc,ball[1]);
BitBlt(mdc,x[1],y[1],26,26,bufdc,26,0,SRCAND);
BitBlt(mdc,x[1],y[1],26,26,bufdc,0,0,SRCPAINT);

BitBlt(hdc,0,0,640,480,mdc,0,0,SRCCOPY);

for(int i=0;i<2;i++){
//计算x轴方向贴图坐标与速度
x[i]+=vx[i];
if(x[i]<=0){
x[i]=0;
vx[i]=-vx[i];
}else if(x[i]>=rect.right-26){
x[i]=rect.right-26;
vx[i]=-vx[i];
}

//计算y轴方向坐标及速度
y[i]+=vy[i];
if(y[i]<=0){
y[i]=0;
vy[i]=-vy[i];
}else if(y[i]>=rect.bottom-26){
y[i]=rect.bottom-26;
vy[i]=-vy[i];
}
}

if((x[0]-x[1])*(x[0]-x[1])+(y[0]-y[1])*(y[0]-y[1])<=1000){
vx[0]=-vx[0];vy[0]=-vy[0];
vx[1]=-vx[1];vy[1]=-vy[1];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: