• <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>

            天行健 君子當自強而不息

            創(chuàng)建游戲內核(1)【OO改良版】

             

            這是面向對象游戲內核的改良版,主要對原來游戲內核對象組織一些不合理之處做出了改良,并且遵循以下原則:

            1、淺分層、薄膠合的原則,對于簡單的功能只用函數(shù)封裝,如果需要實現(xiàn)的功能模塊較復雜,并且需要用到較多的輔助數(shù)據(jù)結構則用類封裝,以減少對象的使用。

            2、本版本不使用模板,盡量減少封裝層數(shù),最大限度地增加透明度。

            3、盡量不在構造函數(shù)內用表達式初始化任何數(shù)據(jù),以使用memet簡化類數(shù)據(jù)成員的初始化,只使用默認構造函數(shù),不使用其他任何形式的構造函數(shù)。

            4、盡量不在類內部保存冗余的數(shù)據(jù),如果這個數(shù)據(jù)真正存儲在另一個地方,則不在其他類中做多余的保存,保證數(shù)據(jù)存儲的唯一性。

            5、不重載函數(shù),不使用參數(shù)默認值。  


            關于該內核的細節(jié)說明請參閱創(chuàng)建游戲內核(1)。

             

            公共頭文件:

            /**************************************************
            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>
            #include <
            string.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; } }
            #define free_memory(x)  { free(x); (x) = NULL; }

            #define STREQ(a, b) (*(a) == (*b) && strcmp((a), (b)) == 0)

            typedef unsigned 
            char uchar;
            typedef unsigned 
            long ulong;

            #endif

            游戲框架頭文件:

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


            #ifndef _CORE_FRAMEWORK_H_
            #define _CORE_FRAMEWORK_H_

            extern HWND g_hwnd;

            HINSTANCE get_window_inst();
            void get_class_name(char* class_name, int length);
            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);

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

            class FRAMEWORK
            {
            public:
                
            virtual BOOL init()         { return TRUE; }
                
            virtual BOOL frame()        { return TRUE; }
                
            virtual BOOL shutdown()     { return TRUE; }

                
            void run();
            };

            static FRAMEWORK* g_framework = NULL;

            #endif

             

            框架實現(xiàn)代碼:

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


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

            HWND g_hwnd;

            //-----------------------------------------------------------------------------
            // 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);
            }

            //-----------------------------------------------------------------------------
            // 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);
            }

            //-----------------------------------------------------------------------------
            // 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 FRAMEWORK::run()
            {
                MSG _msg;

                
            // intialize game
                if(! 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(! frame())
                        
            break;
                }

                
            // run shutdown function
                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());
            }
             

            測試代碼:
             
            /*****************************************************************************
            PURPOSE:
                Test for class FRAMEWORK.
            *****************************************************************************/


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

            class APP : public FRAMEWORK
            {
            public:
                BOOL init();
                BOOL frame();
                BOOL shutdown();
                
            private:
                
            char* m_name;
            };

            //-----------------------------------------------------------------------------
            // Initialize application.
            //-----------------------------------------------------------------------------
            BOOL APP::init()
            {
                
            if((m_name = new char[9]))
                {
                    strcpy(m_name, "no_name");
                    
            return TRUE;
                }

                
            return FALSE;
            }

            //-----------------------------------------------------------------------------
            // Do a frame.
            //-----------------------------------------------------------------------------
            BOOL APP::frame()
            {
                
            // If user clicks button CANCEL, then exit application.
                if(MessageBox(g_hwnd, m_name, "My name is", MB_OKCANCEL) == IDCANCEL)
                    
            return FALSE;

                
            return TRUE;
            }

            //-----------------------------------------------------------------------------
            // shutdown application.
            //-----------------------------------------------------------------------------
            BOOL APP::shutdown()
            {
                delete[] m_name;
                m_name = NULL;

                
            return TRUE;
            }

            //--------------------------------------------------------------------------------
            // Main function, routine entry.
            //--------------------------------------------------------------------------------
            int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
            {
                APP app;

                
            if(! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 640, 480))
                    
            return -1;
                
                app.run();

                
            return 0;
            }

            posted on 2007-10-06 13:11 lovedday 閱讀(430) 評論(0)  編輯 收藏 引用

            公告

            導航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關鏈接

            搜索

            最新評論

            久久综合狠狠色综合伊人| 四虎影视久久久免费| 久久综合丁香激情久久| 久久精品国产99国产电影网| 久久频这里精品99香蕉久| 精品永久久福利一区二区| 久久亚洲天堂| 日本精品久久久久中文字幕8| 久久亚洲精品视频| 久久夜色精品国产www| 亚洲欧美成人综合久久久 | 天天综合久久久网| 久久精品亚洲男人的天堂| 久久成人精品视频| 亚洲精品tv久久久久| 久久久久久av无码免费看大片| 狠狠色丁香久久婷婷综合_中 | 精品无码久久久久久久久久| 国内精品久久久久影院一蜜桃| 国产精品99久久久久久猫咪| 精品999久久久久久中文字幕| 久久综合视频网站| 国产成人精品白浆久久69| 麻豆国内精品久久久久久| 狠狠色丁香久久婷婷综合五月 | 国产偷久久久精品专区| 久久综合亚洲色一区二区三区| 99久久人妻无码精品系列蜜桃| 亚洲精品乱码久久久久久蜜桃| 99久久精品国产麻豆| 国产国产成人精品久久| 亚洲人AV永久一区二区三区久久| 精品国际久久久久999波多野| 亚洲精品99久久久久中文字幕| 久久婷婷久久一区二区三区| 日产精品99久久久久久| 99久久人妻无码精品系列蜜桃| 欧美亚洲国产精品久久久久| 国产福利电影一区二区三区,免费久久久久久久精 | 国产成人久久精品麻豆一区| 无码人妻久久久一区二区三区 |