從零開(kāi)發(fā)游戲引擎系列(七)窗口

該系列教程源自youtube的cherno的視頻-GAME ENGINE series!

視頻地址: https://www.youtube.com/watch?v=vtWdgtMo1T4

引擎源代碼地址: https://github.com/TheCherno/Hazel

將使用第三方庫(kù)glfw: https://github.com/glfw/glfw

當(dāng)前cmd目錄下E:\GitResp\GameEngine\Hazel 執(zhí)行

git submodule add https://github.com/glfw/glfw Hazel/vendor/glfw

premake5.lua 中projectHazel增加鏈接庫(kù)配置

    links
    {
        "glfw",
        "opengl32.lib"
    }

includedirs增加glfw的頭文件包含路徑

    includedirs
    {
        "%{prj.name}/src",
        "%{prj.name}/vendor/spdlog/include"
        "%{prj.name}/vendor/glfw/include"
    }

新增配置工程Hazel/vendor/glfw/premake5.lua

project "glfw"
    kind "StaticLib"
    language "C"

    targetdir ("bin/" .. outputdir .. "/%{prj.name}")
    objdir ("bin-int/" .. outputdir .. "/%{prj.name}")

    files
    {
        "include/GLFW/glfw3.h",
        "include/GLFW/glfw3native.h",
        "src/glfw_config.h",
        "src/context.c",
        "src/init.c",
        "src/input.c",
        "src/monitor.c",
        "src/vulkan.c",
        "src/window.c"
    }
    filter "system:linux"
        pic "On"

        systemversion "latest"
        staticruntime "On"

        files
        {
            "src/x11_init.c",
            "src/x11_monitor.c",
            "src/x11_window.c",
            "src/xkb_unicode.c",
            "src/posix_time.c",
            "src/posix_thread.c",
            "src/glx_context.c",
            "src/egl_context.c",
            "src/osmesa_context.c",
            "src/linux_joystick.c"
        }

        defines
        {
            "_GLFW_X11"
        }

    filter "system:windows"
        systemversion "latest"
        staticruntime "On"

        files
        {
            "src/win32_init.c",
            "src/win32_joystick.c",
            "src/win32_monitor.c",
            "src/win32_time.c",
            "src/win32_thread.c",
            "src/win32_window.c",
            "src/wgl_context.c",
            "src/egl_context.c",
            "src/osmesa_context.c"
        }

        defines 
        { 
            "_GLFW_WIN32",
            "_CRT_SECURE_NO_WARNINGS"
        }

    filter "configurations:Debug"
        runtime "Debug"
        symbols "on"

    filter "configurations:Release"
        runtime "Release"
        optimize "on"

Hazel/src/hzpch.h 增加

#include "Hazel/Log.h"

Hazel/src/Hazel/Core.h 增加 自定義assert宏

#ifdef HZ_ENABLE_ASSERTS
    #define HZ_ASSERT(x, ...) { if(!(x)) { HZ_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } }
    #define HZ_CORE_ASSERT(x, ...) { if(!(x)) { HZ_CORE_ERROR("Assertion Failed: {0}", __VA_ARGS__); __debugbreak(); } }
#else
    #define HZ_ASSERT(x, ...)
    #define HZ_CORE_ASSERT(x, ...)
#endif

新增文件Hazel/src/Hazel/Window.h,定義了抽象窗口基類

#pragma once

#include "hzpch.h"

#include "Hazel/Core.h"
#include "Hazel/Events/Event.h"

namespace Hazel {

    struct WindowProps//窗口屬性
    {
        std::string Title;
        unsigned int Width;
        unsigned int Height;

        WindowProps(const std::string& title = "Hazel Engine",
                    unsigned int width = 1280,
                    unsigned int height = 720)
            : Title(title), Width(width), Height(height)
        {
        }
    };

    // Interface representing a desktop system based Window
    class HAZEL_API Window //窗口抽象類
    {
    public:
        using EventCallbackFn = std::function<void(Event&)>;

        virtual ~Window() {}

        virtual void OnUpdate() = 0;//每一幀調(diào)用

        virtual unsigned int GetWidth() const = 0;
        virtual unsigned int GetHeight() const = 0;

        // Window attributes
        virtual void SetEventCallback(const EventCallbackFn& callback) = 0;//設(shè)置窗口事件回調(diào),平臺(tái)觸發(fā)
        virtual void SetVSync(bool enabled) = 0;
        virtual bool IsVSync() const = 0;

        static Window* Create(const WindowProps& props = WindowProps());
    };

}

實(shí)現(xiàn)windows平臺(tái)的窗口類:
Hazel/src/Platform/Windows/WindowsWindow.h

#pragma once

#include "Hazel/Window.h"

#include <GLFW/glfw3.h>

namespace Hazel {

    class WindowsWindow : public Window
    {
    public:
        WindowsWindow(const WindowProps& props);
        virtual ~WindowsWindow();

        void OnUpdate() override;

        inline unsigned int GetWidth() const override { return m_Data.Width; }
        inline unsigned int GetHeight() const override { return m_Data.Height; }

        // Window attributes
        inline void SetEventCallback(const EventCallbackFn& callback) override { m_Data.EventCallback = callback; }
        void SetVSync(bool enabled) override;
        bool IsVSync() const override;
    private:
        virtual void Init(const WindowProps& props);
        virtual void Shutdown();
    private:
        GLFWwindow* m_Window;

        struct WindowData
        {
            std::string Title;
            unsigned int Width, Height;
            bool VSync;

            EventCallbackFn EventCallback;
        };

        WindowData m_Data;
    };

} 

Hazel/src/Platform/Windows/WindowsWindow.cpp

#include "hzpch.h"
#include "WindowsWindow.h"

namespace Hazel {

    static bool s_GLFWInitialized = false;

    Window* Window::Create(const WindowProps& props)
    {
        return new WindowsWindow(props);
    }

    WindowsWindow::WindowsWindow(const WindowProps& props)
    {
        Init(props);
    }

    WindowsWindow::~WindowsWindow()
    {
        Shutdown();
    }

    void WindowsWindow::Init(const WindowProps& props)
    {
        m_Data.Title = props.Title;
        m_Data.Width = props.Width;
        m_Data.Height = props.Height;

        HZ_CORE_INFO("Creating window {0} ({1}, {2})", props.Title, props.Width, props.Height);

        if (!s_GLFWInitialized)
        {
            // TODO: glfwTerminate on system shutdown
            int success = glfwInit();
            HZ_CORE_ASSERT(success, "Could not intialize GLFW!");

            s_GLFWInitialized = true;
        }

        m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr);
        glfwMakeContextCurrent(m_Window);
        glfwSetWindowUserPointer(m_Window, &m_Data);
        SetVSync(true);
    }

    void WindowsWindow::Shutdown()
    {
        glfwDestroyWindow(m_Window);
    }

    void WindowsWindow::OnUpdate()
    {
        glfwPollEvents();
        glfwSwapBuffers(m_Window);
    }

    void WindowsWindow::SetVSync(bool enabled)
    {
        if (enabled)
            glfwSwapInterval(1);
        else
            glfwSwapInterval(0);

        m_Data.VSync = enabled;
    }

    bool WindowsWindow::IsVSync() const
    {
        return m_Data.VSync;
    }

}

在Application類中 創(chuàng)建窗口焕襟,分發(fā)事件:
Hazel/src/Hazel/Application.h

#include "Window.h"

    ...

    private:
        std::unique_ptr<Window> m_Window;
        bool m_Running = true;

Hazel/src/Hazel/Application.cpp

#include <GLFW/glfw3.h>
    ...
    Application::Application()
    {
        m_Window = std::unique_ptr<Window>(Window::Create());
    }
    ...

    void Application::Run()
    {

        while (m_Running)
        {
            glClearColor(1, 0, 1, 1);
            glClear(GL_COLOR_BUFFER_BIT);
            m_Window->OnUpdate();
        }
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末履羞,一起剝皮案震驚了整個(gè)濱河市毙驯,隨后出現(xiàn)的幾起案子咕幻,更是在濱河造成了極大的恐慌线得,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蓝翰,死亡現(xiàn)場(chǎng)離奇詭異光绕,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)畜份,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)奇钞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人漂坏,你說(shuō)我怎么就攤上這事。” “怎么了顶别?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵谷徙,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我驯绎,道長(zhǎng)完慧,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任剩失,我火速辦了婚禮屈尼,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拴孤。我一直安慰自己脾歧,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布演熟。 她就那樣靜靜地躺著鞭执,像睡著了一般。 火紅的嫁衣襯著肌膚如雪芒粹。 梳的紋絲不亂的頭發(fā)上兄纺,一...
    開(kāi)封第一講書(shū)人閱讀 49,764評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音化漆,去河邊找鬼估脆。 笑死,一個(gè)胖子當(dāng)著我的面吹牛座云,可吹牛的內(nèi)容都是我干的疙赠。 我是一名探鬼主播,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼疙教,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼棺聊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起贞谓,我...
    開(kāi)封第一講書(shū)人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤限佩,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后裸弦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體祟同,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年理疙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了晕城。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡窖贤,死狀恐怖砖顷,靈堂內(nèi)的尸體忽然破棺而出贰锁,到底是詐尸還是另有隱情,我是刑警寧澤滤蝠,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布豌熄,位于F島的核電站,受9級(jí)特大地震影響物咳,放射性物質(zhì)發(fā)生泄漏锣险。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一览闰、第九天 我趴在偏房一處隱蔽的房頂上張望芯肤。 院中可真熱鬧,春花似錦压鉴、人聲如沸崖咨。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)掩幢。三九已至,卻和暖如春上鞠,著一層夾襖步出監(jiān)牢的瞬間际邻,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工芍阎, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留世曾,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓谴咸,卻偏偏與公主長(zhǎng)得像轮听,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子岭佳,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容