小菜鸟的C++游戏编程学习日记(二)
2016-07-27 21:45
316 查看
小菜鸟的C++游戏编程学习日记(二)
作者:杨帆
参考书籍:Beginning Game ProGramming(中文版)
学习时间:2016.7.27
#include <windows.h> #include <iostream> #include <time.h> //include header source using namespace std; const string APPTITLE = "Game Loop"; HWND hWnd; HDC hDc; bool gameover = false; //local and draws a bitmap from file and then free the memory //(not really suitable for a game loop but it's self contained) void DrawBitmap(char* filename,int x,int y) { //load the bitmap image HBITMAP image = (HBITMAP)LoadImage(0, "000.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); //read the bitmap's properties BITMAP bmp; GetObject(image, sizeof(BITMAP), &bmp); //creat a device context for the bitmap HDC hdcImage = CreateCompatibleDC(hDc); SelectObject(hdcImage, image); //draw the bitmap to the window (bit block transfer) BitBlt( hDc, //destination device context x, y, //x,y location on destination bmp.bmWidth, bmp.bmHeight, //width,height of source bitmap hdcImage, //source bitmap device context 0, 0, //start x,y on source bitmap SRCCOPY //bit method ); //delete the device context and bitmap DeleteDC(hdcImage); DeleteObject((HBITMAP)image); } //startup and loading code goes here bool GameInit() { //start up the random number generator srand((unsigned)time(NULL)); return true; } //update function called from inside game loop void GameRun() { if (gameover == true) { return; } //get the drawing surface RECT rect; GetClientRect(hWnd, &rect); //draw bitmap at random location int x = rand() % (rect.right - rect.left); int y = rand() % (rect.bottom - rect.top); DrawBitmap("000.bmp", x, y); } //shut down code void GameEnd() { //free the device ReleaseDC(hWnd,hDc); } //window callback function LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam) { switch (message) { case WM_DESTROY: gameover = true; PostQuitMessage(0); break; default: break; } return DefWindowProc(hwnd, message, wparam, lparam); } //register the window ATOM MyRegisterWindowClass(HINSTANCE hInstance) { // creat a window class struct WNDCLASSEX wc; wc.cbClsExtra = 0; wc.cbSize = sizeof(WNDCLASSEX); wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = NULL; wc.hIconSm = NULL; wc.hInstance = hInstance; wc.lpfnWndProc = (WNDPROC)WinProc; wc.lpszClassName = APPTITLE.c_str(); wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; return RegisterClassEx(&wc); } //helper function to create the window and refresh it BOOL InitInstance(HINSTANCE hInstance,int nCmdShow) { //creat new window hWnd = CreateWindow( APPTITLE.c_str(), //ClassName APPTITLE.c_str(), //WindowName WS_OVERLAPPEDWINDOW, //style CW_USEDEFAULT, CW_USEDEFAULT, //x,y position in window (use default) 800, 500, //window size NULL, NULL, //WndParent hMenu hInstance, //Window instance NULL //lParam ); //was here an error creating the window if (!hWnd) { return 0; } //display the window ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); //get device context for drawing hDc = GetDC(hWnd); return 1; } //Entry point function int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { MSG msg; //creat window MyRegisterWindowClass(hInstance); if (!InitInstance(hInstance,nCmdShow)) { return 0; } //initialize the game if (!GameInit()) { return 0; } //main message loop while (!gameover) { //process windows events if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } //process game loop GameRun(); } //free game resource GameEnd(); return msg.wParam; }
这里对比昨天主要是对消息循环进行了新的处理,使得游戏过程有了一个完整的循环过程。并且藉此和之前看过的一点视频学习了关于贴图的知识,利用之前有的资源写了另一份代码。效果如下(没错就是会动的):
相关文章推荐
- 一个java菜鸟求职辛酸史之二
- 自建应用新花样,菜鸟也会做应用
- JVM菜鸟进阶高手之路十一(eden survivor分配问题)
- 菜鸟也学Ogre-帧监听和无缓冲输入
- [菜鸟学爪哇]Day 2
- 菜鸟学python之变量类型和对象
- 和菜鸟一起学android4.0.3源码之bluetooth移植心得
- 【菜鸟Opencv 3】-VideoWriter的用法 视频输出
- 菜鸟进阶--node+vue实现一个商城Demo(1):项目介绍
- 万里长征,始于足下——菜鸟程序员的学习总结(一)
- 菜鸟好文推荐(三)——正确使用Java事件通知
- web菜鸟学习心得--css导入方式
- 菜鸟浅谈Linux内核编译过程...
- “菜鸟”程序员和“大神”程序员差距在哪里
- C语言菜鸟基础教程之单精度浮点数与双精度浮点数
- 菜鸟入侵
- 菜鸟的日记
- 菜鸟教程python基础总结
- 菜鸟初始代码旅程——修改记录
- 菜鸟学设计模式系列笔记之创建型模式开篇