• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當自強而不息

            創建游戲內核(1) 【接口與實現分離版】

             
            關于該內核的細節說明請參考創建游戲內核(1),這個版本主要是按功能劃分模塊的思想,并嚴格按照接口與實現相分離的原則來寫的,沒有用面向對象的思想來寫,沒有繼承沒有多態。大家可以對比兩個版本,比較優劣。

            以下是游戲框架的接口:

            /*************************************************************************
            PURPOSE:
                Defines for main window framework.
            *************************************************************************/


            #ifndef _CORE_FRAMEWORK_H_
            #define _CORE_FRAMEWORK_H_

            extern HWND g_hwnd;

            typedef BOOL (*FRAMEWORK_FUNC)();

            BOOL build_window(HINSTANCE inst, 
            const char* class_name, const char* caption, 
                              DWORD style, DWORD x_pos, DWORD y_pos, DWORD width, DWORD height);

            void run_game(FRAMEWORK_FUNC do_init, FRAMEWORK_FUNC do_frame, FRAMEWORK_FUNC do_shutdown);

            HINSTANCE get_window_inst();
            void get_class_name(char* class_name, int length);

            #endif

            接著是游戲框架的實現:

            /*************************************************************************
            PURPOSE:
                Implenents for main window framework.
            *************************************************************************/


            #include "core_common.h"
            #include "core_framework.h"
            #include "window_func.h"

            HWND g_hwnd;

            //-----------------------------------------------------------------------------
            // The message procedure.
            //-----------------------------------------------------------------------------
            LRESULT CALLBACK window_proc(HWND hwnd, UINT msg_id, WPARAM w_param, LPARAM l_param)
            {
                
            switch(msg_id)
                {
                
            case WM_DESTROY:
                    PostQuitMessage(0);
                    
            return 0;
                }

                
            return DefWindowProc(hwnd, msg_id, w_param, l_param);
            }

            //-----------------------------------------------------------------------------
            // Register window aclas, create window and show it.
            //-----------------------------------------------------------------------------
            BOOL build_window(HINSTANCE inst, const char* class_name, const char* caption, 
                              DWORD style, DWORD x_pos, DWORD y_pos, DWORD width, DWORD height)
            {    
                WNDCLASSEX win_class;

                
            // create window class and register it
                win_class.cbSize        = sizeof(win_class);
                win_class.style         = CS_CLASSDC;
                win_class.lpfnWndProc   = window_proc;
                win_class.cbClsExtra    = 0;
                win_class.cbWndExtra    = 0;
                win_class.hInstance     = inst;
                win_class.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
                win_class.hCursor       = LoadCursor(NULL, IDC_ARROW);
                win_class.hbrBackground = NULL;
                win_class.lpszMenuName  = NULL;
                win_class.lpszClassName = class_name;
                win_class.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

                
            if(! RegisterClassEx(&win_class))
                    
            return FALSE;
                
                
            // create the main window
                g_hwnd = CreateWindow(class_name, caption, style, x_pos, y_pos, width, height, NULL, NULL, inst, NULL);

                
            if(g_hwnd == NULL)
                    
            return FALSE;

                ShowWindow(g_hwnd, SW_NORMAL);
                UpdateWindow(g_hwnd);

                
            return TRUE;
            }

            //-----------------------------------------------------------------------------
            // Run game framework.
            //-----------------------------------------------------------------------------
            void run_game(FRAMEWORK_FUNC do_init, FRAMEWORK_FUNC do_frame, FRAMEWORK_FUNC do_shutdown)
            {
                MSG msg;

                
            // intialize game
                if(! do_init())
                    
            return;

                
            // start message pump, waiting for signal to quit.
                ZeroMemory(&msg, sizeof(MSG));

                
            while(msg.message != WM_QUIT)
                {
                    
            if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
                    {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                    }
                    
                    
            // draw a frame
                    if(! do_frame())
                        
            break;
                }

                
            // run shutdown function
                do_shutdown();

                
            // get window class name
                char class_name[MAX_PATH];
                get_class_name(class_name, 
            sizeof(class_name));

                
            // unregister window class
                UnregisterClass(class_name, get_window_inst());
            }

            //-----------------------------------------------------------------------------
            // Return window instnace.
            //-----------------------------------------------------------------------------
            HINSTANCE get_window_inst()
            {
                
            return GetModuleHandle(NULL);
            }

            //-----------------------------------------------------------------------------
            // Get window class name.
            //-----------------------------------------------------------------------------
            void get_class_name(char* class_name, int length)
            {
                GetClassName(g_hwnd, class_name, length);
            }


            這是公共頭文件core_common.h的定義:

            /**************************************************
            PURPOSE:
                Include common game core header file.
            **************************************************/


            #ifndef _CORE_COMMON_H_
            #define _CORE_COMMON_H_

            #define DIRECTINPUT_VERSION 0x0800

            // Windows includes
            #include <windows.h>

            // Standard ANSI-C includes
            #include <stdio.h>

            // DirectX includes
            #include <d3d9.h>
            #include <d3dx9.h>
            #include <dmusici.h>
            #include <dsound.h>
            #include <dplay8.h>
            #include <dpaddr.h>
            #include <dinput.h>
            #include <dshow.h>
            #include <dxfile.h>

            #pragma warning(disable : 4996)

            #define release_com(x) { if(x) { x->Release(); x = NULL; } }

            #endif
             

            我還將公用的windows窗口操作函數封裝在了1個文件中:

            /***********************************************************************************
            PURPOSE:
                Interface for windows draw.
            ***********************************************************************************/


            void show_error_msg(BOOL is_fatal, char* text, );
            void move_window(HWND hwnd, long x_pos, long y_pos);
            void resize_window(HWND hwnd, long width, long height);
            long get_client_width(HWND hwnd);
            long get_client_height(HWND hwnd);
            long get_window_width(HWND hwnd);
            long get_window_height(HWND hwnd);

            /***********************************************************************************
            PURPOSE:
                Capsulates for windows draw.
            ***********************************************************************************/


            #include <windows.h>
            #include <stdio.h>
            #include <
            string.h>

            #pragma warning(disable : 4996)

            //-----------------------------------------------------------------------------
            // Show error message box.
            //-----------------------------------------------------------------------------
            void show_error_msg(BOOL is_fatal, char* text, )
            {
                
            char caption_text[12];
                
            char error_text[2048];
                va_list valist;

                
            // Build the message box caption based on fatal flag
                strcpy(caption_text, is_fatal ? "Fatal error" : "error");

                
            // Build variable text buffer
                va_start(valist, text);
                vsprintf(error_text, text, valist);
                va_end(valist);

                
            // display the message box
                MessageBox(NULL, error_text, caption_text, MB_OK | MB_ICONEXCLAMATION);

                
            // Post a quit message if error was fatal.
                if(is_fatal)
                    PostQuitMessage(0);
            }

            //-----------------------------------------------------------------------------
            // move window to new position.
            //-----------------------------------------------------------------------------
            void move_window(HWND hwnd, long x_pos, long y_pos)
            {
                RECT client_rect;

                GetClientRect(hwnd, &client_rect);
                MoveWindow(hwnd, x_pos, y_pos, client_rect.right, client_rect.bottom, TRUE);
            }

            //-----------------------------------------------------------------------------
            // Resize window to new width and height.
            //-----------------------------------------------------------------------------
            void resize_window(HWND hwnd, long width, long height)
            {
                RECT window_rect, client_rect;
                
            long new_window_width, new_window_height;

                
            // Retrieves the dimensions of the bounding rectangle of the specified window. 
                // The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen. 
                GetWindowRect(hwnd, &window_rect);

                
            // Retrieves the coordinates of a window's client area. 
                // 
                // The client coordinates specify the upper-left and lower-right corners of the client area.
                // Because client coordinates are relative to the upper-left corner of a window's client area, 
                // the coordinates of the upper-left corner are (0,0). 
                GetClientRect(hwnd, &client_rect);

                new_window_width  = (window_rect.right - window_rect.left) - client_rect.right + width;
                new_window_height = (window_rect.bottom - window_rect.top) - client_rect.bottom + height;

                
            // Changes the position and dimensions of the specified window. 
                // 
                // For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. 
                // For a child window, they are relative to the upper-left corner of the parent window's client area. 
                MoveWindow(hwnd, window_rect.left, window_rect.top, new_window_width, new_window_height, TRUE);
            }

            //-----------------------------------------------------------------------------
            // Get window client width.
            //-----------------------------------------------------------------------------
            long get_client_width(HWND hwnd)
            {
                RECT client_rect;

                GetClientRect(hwnd, &client_rect);

                
            return (client_rect.right - client_rect.left);
            }

            //-----------------------------------------------------------------------------
            // Get window client height.
            //-----------------------------------------------------------------------------
            long get_client_height(HWND hwnd)
            {
                RECT client_rect;

                GetClientRect(hwnd, &client_rect);

                
            return (client_rect.bottom - client_rect.top);
            }

            //-----------------------------------------------------------------------------
            // Get window width.
            //-----------------------------------------------------------------------------
            long get_window_width(HWND hwnd)
            {
                RECT window_rect;

                GetWindowRect(hwnd, &window_rect);

                
            return (window_rect.right - window_rect.left);
            }

            //-----------------------------------------------------------------------------
            // Get window height.
            //-----------------------------------------------------------------------------
            long get_window_height(HWND hwnd)
            {
                RECT window_rect;

                GetWindowRect(hwnd, &window_rect);

                
            return (window_rect.bottom - window_rect.top);
            }
             

            posted on 2007-09-30 23:56 lovedday 閱讀(292) 評論(0)  編輯 收藏 引用

            公告

            導航

            統計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            亚洲精品国产第一综合99久久| 久久亚洲精品国产精品| 亚洲国产天堂久久综合网站| 久久天天日天天操综合伊人av| 一本色道久久88—综合亚洲精品| 国产精品一区二区久久不卡| 久久久综合香蕉尹人综合网| 色天使久久综合网天天| 狠狠色综合久久久久尤物| 色88久久久久高潮综合影院| 久久精品国产久精国产| 久久久久亚洲精品无码蜜桃| 久久天天躁夜夜躁狠狠躁2022| 国产精品青草久久久久福利99| 久久91精品国产91久久小草 | 日韩va亚洲va欧美va久久| 久久天天躁狠狠躁夜夜不卡 | 国产一久久香蕉国产线看观看| 久久午夜福利电影| 精品少妇人妻av无码久久| 亚洲欧美久久久久9999| 久久久精品日本一区二区三区 | 久久亚洲国产成人精品性色| 久久久久九九精品影院| …久久精品99久久香蕉国产| 久久99精品久久久久婷婷| 亚洲国产成人乱码精品女人久久久不卡| 久久99精品久久久久婷婷| 狠狠色丁香久久婷婷综合| 国产成人久久久精品二区三区| 久久99热只有频精品8| 久久久一本精品99久久精品88| 久久无码国产| 久久亚洲2019中文字幕| 99久久国产免费福利| 91麻精品国产91久久久久 | 国产精品永久久久久久久久久| 狠色狠色狠狠色综合久久 | 国产亚洲婷婷香蕉久久精品| 精品久久久无码人妻中文字幕豆芽| 色综合久久无码中文字幕|