• <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)建游戲內(nèi)核(1) 【C風格版】

             

            該版本根據(jù)接口與實現(xiàn)分離版改的, 因為接口與實現(xiàn)分離寫起來太煩瑣,所以直接使用結(jié)構(gòu)體。由于結(jié)構(gòu)體的數(shù)據(jù)成員可以直接訪問,可以減少編寫大量的接口函數(shù),同時對于簡單的數(shù)據(jù)結(jié)構(gòu)采用函數(shù)封裝, 只對復雜的功能模塊采用結(jié)構(gòu)體封裝,減少抽象對象的使用以最大限度增加代碼的透明度。


            共同頭文件:

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

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

            ////////////////////////////// typedef for built-in data type //////////////////////////////

            typedef unsigned 
            char   uchar;
            typedef unsigned 
            long   ulong;

            typedef 
            void*           void_ptr;
            typedef 
            const char*     pcstr;
            typedef 
            char*           pstr;
            typedef 
            char*           char_ptr;
            typedef uchar*          uchar_ptr;
            typedef 
            ulong*          ulong_ptr;
            typedef 
            float*          float_ptr;

            typedef void_ptr&       void_ptr_ref;
            typedef 
            ulong&          ulong_ref;
            typedef uchar&          uchar_ref;
            typedef 
            float&          float_ref;

            ////////////////////////////// typedef for D3D data type //////////////////////////////

            typedef D3DMATERIAL9*   D3DMATERIAL9_PTR;
            typedef D3DLIGHT9*      D3DLIGHT9_PTR;

            #endif

            工具函數(shù)接口與實現(xiàn):

            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);
            long get_screen_width();
            long get_screen_height();

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

            //-----------------------------------------------------------------------------
            // Return width of the screen of the primary display monitor, in pixels. 
            //-----------------------------------------------------------------------------
            long get_screen_width()
            {
                
            return GetSystemMetrics(SM_CXSCREEN);
            }

            //-----------------------------------------------------------------------------
            // Return height of the screen of the primary display monitor, in pixels. 
            //-----------------------------------------------------------------------------
            long get_screen_height()
            {
                
            return GetSystemMetrics(SM_CYSCREEN);
            }

            游戲框架接口與實現(xiàn):

            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 game_init, FRAMEWORK_FUNC game_frame, FRAMEWORK_FUNC game_shutdown);

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

            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 game_init, FRAMEWORK_FUNC game_frame, FRAMEWORK_FUNC game_shutdown)
            {
                MSG msg;

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

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

            posted on 2007-10-25 19:41 lovedday 閱讀(476) 評論(0)  編輯 收藏 引用

            公告

            導航

            統(tǒng)計

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評論

            久久久久亚洲av无码专区| 少妇精品久久久一区二区三区| 久久中文娱乐网| 日日狠狠久久偷偷色综合96蜜桃 | 亚洲中文字幕久久精品无码APP | 久久99国产精品久久久| 国产精品伊人久久伊人电影| 狠狠色噜噜色狠狠狠综合久久| 2021精品国产综合久久| 久久午夜综合久久| 狠狠狠色丁香婷婷综合久久五月 | 久久受www免费人成_看片中文| 久久亚洲欧美国产精品| 91精品国产高清久久久久久91| 午夜精品久久久久久久久| 久久久久久国产a免费观看不卡 | 中文精品久久久久人妻| 久久99国产亚洲高清观看首页| 99久久国产亚洲综合精品| 美女写真久久影院| 性欧美丰满熟妇XXXX性久久久| 久久精品中文字幕有码| 国产亚洲欧美成人久久片| 久久人爽人人爽人人片AV| 亚洲国产成人精品女人久久久| 日本精品久久久久中文字幕8 | 亚洲精品美女久久777777| 久久精品无码一区二区app| 久久精品国产99国产精偷| 无码专区久久综合久中文字幕| 伊人精品久久久久7777| 亚洲欧美国产精品专区久久| 人人狠狠综合88综合久久| 久久久99精品一区二区| 国产69精品久久久久9999| 成人a毛片久久免费播放| 青青草国产精品久久| 91久久香蕉国产熟女线看| 国产精品成人无码久久久久久| 成人亚洲欧美久久久久| 久久国产美女免费观看精品|