小菜鸟的C++游戏编程学习日记(二)
来源:互联网 发布:seo.chinaz 编辑:程序博客网 时间:2024/04/28 15:31
小菜鸟的C++游戏编程学习日记(二)
作者:杨帆
参考书籍:Beginning Game ProGramming(中文版)
学习时间:2016.7.27
#include <windows.h>#include <iostream>#include <time.h> //include header sourceusing 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 herebool GameInit(){ //start up the random number generator srand((unsigned)time(NULL)); return true;}//update function called from inside game loopvoid 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 functionLRESULT 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 windowATOM 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 itBOOL 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 functionint 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;}
这里对比昨天主要是对消息循环进行了新的处理,使得游戏过程有了一个完整的循环过程。并且藉此和之前看过的一点视频学习了关于贴图的知识,利用之前有的资源写了另一份代码。效果如下(没错就是会动的):
0 0
- 小菜鸟的C++游戏编程学习日记(二)
- 小菜鸟的C++游戏编程学习日记(一)
- 对于c,入个门!【菜鸟学习日记】
- 菜鸟学习日记:关于scolltop的问题
- 菜鸟学习日记: niceScolltop插件的使用
- 小菜鸟学习SpringMVC(二) 注解开发
- 【Visual C++】游戏编程学习笔记之二:定时器的使用
- 学习GIS的小菜鸟
- 菜鸟学习日记详细版--C和指针
- 菜鸟小屁的成长日记2017.1.10
- 菜鸟小屁的成长日记2017.1.11
- 菜鸟小屁的成长日记2017.1.12
- 【菜鸟小屁的成长日记】之ElasticSearchElasticsearch<入门>篇
- struts2.0----菜鸟日记二
- 给菜鸟们的小提示(二)
- C游戏编程之二:我的最爱车型
- 菜鸟者学习C语言编程
- 游戏编程日记
- Date和Calendar类的一些使用
- RxJava
- windows 下实现socket编程_传送文件
- 语音合成与语音识别小例子
- sdut oj1197 约瑟夫问题(循环链表)
- 小菜鸟的C++游戏编程学习日记(二)
- UVA10001Garden of Eden
- numpy KNN简单实现
- Android Zxing二维码扫描图片拉伸、扫描框过小、扫描框扩大后(或不拉伸后)闪退问题
- java数据结构(单链表)
- 倒计时到0停止,用在手机获取验证码的倒计时
- C Primer Plus笔记-第一章
- 如何利用pt-archiver工具归档和清理数据
- 买饭问题