對(duì)這方面感興趣或者想學(xué)習(xí)C/C++可以加群:558502932,有問(wèn)題可以在群內(nèi)大家一起交流學(xué)習(xí)。 對(duì)于正在學(xué)習(xí)C/C++的同學(xué)或者朋友來(lái)說(shuō),如何用C/C++寫程序都有些模糊的概念,畢竟如果只是學(xué)習(xí)C/C++的理論知識(shí),很多人甚至都不知道C/C++學(xué)了能干嘛。 有很多人都想去往游戲方面發(fā)展,畢竟能寫出一個(gè)游戲供大家學(xué)習(xí)是一件特別自豪的事情。但是要寫出一個(gè)游戲,不是那么容易的事情,今天我就來(lái)分享一下,我們講師寫過(guò)的一個(gè)游戲框架,希望能對(duì)你們有所幫助。 對(duì)這方面感興趣或者想學(xué)習(xí)C/C++可以加群:558502932 代碼如下: /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // WindowsFrame.cpp: This is a Windows-Game Forms // Author: MOYG // Date: Dec-19-2015 // Version 1.0 // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // INCLUDEs //////////////////////////////////////////////////////////////////////////////////////////////////// #include 'windows.h' #include 'time.h“ // DEFINES //////////////////////////////////////////////////////////////////////////////////////////////////// #define WINDOW_WIDTH 800 //Window width of a macro definition #define WINDOW_HEIGHT 600 //Window height of a macro definition #define WINDOW_TITLE L'【潭州教育】程序核心框架' //The window title of marco definition // GLOBALS ///////////////////////////////////////////////////////////////////////////////////////////////////// LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam ); //The process window function // WINMAIN ///////////////////////////////////////////////////////////////////////////////////////////////////// int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { //【1】 窗口創(chuàng)建四部曲之一:開始設(shè)計(jì)一個(gè)完整的窗口類 //【First】 One of the windows to create tetralogy:Start to design a whole window calss WNDCLASSEX wndClass = { 0 }; //With WNDCALSSEX definition of a window class wndClass.cbSize = sizeof(WNDCLASSEX); //Number of bytes to set up the structure size wndClass.style = CS_HREDRAW | CS_VREDRAW; //Set the window style wndClass.lpfnWndProc = WndProc; //Set the pointer to the window procedure function wndClass.cbClsExtra = 0; //Additional memory window class wndClass.cbWndExtra = 0; //Additional memory window wndClass.hInstance = hInstance; //Specified contains the window handle to an instance of the process of program //Local load custom ico wndClass.hIcon = (HICON)::LoadImage(NULL, L'icon.ico', IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE); wndClass.hCursor = LoadCursor(NULL, IDC_ARROW); //Specifies the window handle of the cursor wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH); //The handle of Brush wndClass.lpszMenuName = NULL; //Specify the menu resource wndClass.lpszClassName = L'ForTheDreamOfGameDevelop'; //【2】窗口創(chuàng)建四部曲之二:注冊(cè)窗口類 //【Second】 Second of the window to create tetralogy: RegisterClass if (!RegisterClassEx(&wndClass)) return -1; //【3】窗口創(chuàng)建四部曲之三:正式創(chuàng)建窗口 //【Third】 Third of the window to create tetralogy:Formal creation window HWND hwnd = CreateWindow(L'ForTheDreamOfGameDevelop', WINDOW_TITLE, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL, hInstance, NULL); //【4】窗口創(chuàng)建四部曲之四:窗口移動(dòng)、顯示與更新 //【Four】 Fourth of the window to create tetralogy:Windows moblie,display and update MoveWindow(hwnd, 250, 80, WINDOW_WIDTH, WINDOW_HEIGHT, true); //In the left corner of the window(250,70); ShowWindow(hwnd,nShowCmd); //Display window UpdateWindow(hwnd); //Update window //【5】消息循環(huán)過(guò)程 //windows information rotation MSG msg = { 0 }; //init msg while (msg.message != WM_QUIT) { //查看應(yīng)用程序消息隊(duì)列,有消息時(shí)將隊(duì)列中消息派發(fā)出去 if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); //將虛擬消息轉(zhuǎn)換為字符信息 DispatchMessage(&msg); //分發(fā)一個(gè)消息給窗口程序 } } //【6】窗口類的注銷 //【Sixth】 logout window class UnregisterClass(L'ForDearmOfGameClassDevelop', wndClass.hInstance); return 0; } LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_PAINT: //重繪消息 ValidateRect(hwnd, NULL); //更新客戶的顯示 break; case WM_KEYDOWN: //若是鍵盤按下任意鍵消息 if (wParam == VK_ESCAPE) //如果按下的是ESC鍵 DestroyWindow(hwnd); //銷毀窗口,并發(fā)送一條WM_DESTORY消息 break; case WM_DESTROY: //若是窗口銷毀消息 PostQuitMessage(0); //像系統(tǒng)表示有個(gè)線程有終止請(qǐng)求,用來(lái)響應(yīng)WM_DESTROY消息 break; default: return DefWindowProc(hwnd, message, wParam, lParam); //調(diào)用默認(rèn)的窗口過(guò)程 } return 0; } 以上,就是游戲框架設(shè)計(jì)的源程序,希望對(duì)你們有所幫助,當(dāng)然,你們?nèi)绻信d趣可以運(yùn)行試試,如果有什么問(wèn)題,可以加群:558502932,把問(wèn)題發(fā)到群里,或者找群內(nèi)管理問(wèn)一下,會(huì)幫你解答的。 希望你們學(xué)習(xí)C/C++都能學(xué)有所成。 |
|