directx11引擎在第三次代码改进后没有突然运行,我不知道为什么(代码片段)

author author     2022-12-30     748

关键词:

我正在使用DirectX 11引擎学习C ++游戏教程。在前两部分中,我的代码很好,但是在第三部分(创建SwapChain)中,DirectX窗口决定不显示,让我留在控制台。我真的不知道如何描述代码,所以我只插入所有的类,头文件等。main.cpp:

#include "AppWindow.h"



int main()

    AppWindow app;
    if (app.Init())
    
        while (app.isRun())
        
            app.brodcast();
        




    



    return 0;

AppWindow.cpp:

#include "AppWindow.h"

void AppWindow::onCreate()

    GraphicsEngine::get()->init();
    m_swap_chain = GraphicsEngine::get()->crreateSwapChain();

    RECT rc = this->getClientWindowRect();
    m_swap_chain->init(this->m_hwnd, rc.right-rc.left, rc.bottom-rc.top);







void AppWindow::onUpdate()



void AppWindow::onDestroy()

    Window::onDestroy();
    GraphicsEngine::get()->release();

AppWindow.h:

#pragma once
#include "Window.h"
#include "GraphicsEngine.h"
#include "SwapChain.h"


class AppWindow: public Window

public:
    // Inherited via Window
    virtual void onCreate() override;
    virtual void onUpdate() override;
    virtual void onDestroy() override;
private:
    SwapChain * m_swap_chain;
;

Window.cpp:

#include "Window.h"


Window* window = nullptr;




LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)

    switch (msg)
    
    case WM_CREATE:
    
        //Event fired when the window will be created
        //collected here...
        Window* window = (Window*)((LPCREATESTRUCT)lparam)->lpCreateParams;
        //...and then stored here for later look up
        SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR)window);
        window->setHWND(hwnd);
        window->onCreate();
        break;
    

    case WM_DESTROY:
    
        //Event fired when the window will be destroyed
        window->onDestroy();
        ::PostQuitMessage(0);
        break;
    


    default:
        return ::DefWindowProc(hwnd, msg, wparam, lparam);



    

    return NULL;



bool Window::Init()

    WNDCLASSEX wc;
    wc.cbClsExtra = NULL;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = NULL;
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = NULL;
    wc.lpszClassName = "MyWindowClass";
    wc.lpszMenuName = "";
    wc.style = NULL;
    wc.lpfnWndProc = &WndProc;

    if (!::RegisterClassEx(&wc)) //if the registration of the class will fail, the function will return false
        return false;
    if (!window)
        window = this;
    //creation of the window
    m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MyWindowClass", "DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768, NULL, NULL, NULL, NULL);

    //if the creation fails then the method will return false
    if (!m_hwnd)
        return false;

    //show up the window
    ::ShowWindow(m_hwnd, SW_SHOW);
    ::UpdateWindow(m_hwnd);


    //set this flag to  true to indicate that the window is initialized and running
    m_is_run = true;

    return true;


bool Window::brodcast()

    MSG msg;

    while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
    
        TranslateMessage(&msg);
        DispatchMessage(&msg);


    

    window->onUpdate();

    Sleep(0);

    return true;


bool Window::Release()

    //destroy the window
    if (!::DestroyWindow(m_hwnd))
        return false;


    return true;


void Window::onDestroy()

    m_is_run = false;


bool Window::isRun()

    return m_is_run;


RECT Window::getClientWindowRect()

    RECT rc;
    ::GetClientRect(this->m_hwnd, &rc);
    return rc;


void Window::setHWND(HWND hwnd)

    this->m_hwnd = hwnd;

Window.h:

#pragma once
#include <windows.h>





class Window

public:
    //Initialize the window
    bool Init();
    bool brodcast();
    //Release the window
    bool Release();
    bool isRun();

    RECT getClientWindowRect();
    void setHWND(HWND hwnd);

    //EVENTS
    virtual void onCreate()=0;
    virtual void onUpdate()=0;
    virtual void onDestroy()=0;

protected:
    HWND m_hwnd;
    bool m_is_run;
;

GraphicsEngine.cpp:

#include "GraphicsEngine.h"
#include <d3d11.h>
#include "SwapChain.h"


bool GraphicsEngine::init()

    D3D_DRIVER_TYPE driver_types[] =
    
        D3D_DRIVER_TYPE_HARDWARE,
        D3D_DRIVER_TYPE_WARP,
        D3D_DRIVER_TYPE_REFERENCE
    ;
    UINT num_driver_types = ARRAYSIZE(driver_types);

    D3D_FEATURE_LEVEL feature_levels[]=
    
        D3D_FEATURE_LEVEL_11_0
    ;
    UINT num_feature_levels = ARRAYSIZE(feature_levels);

    HRESULT res = 0;
    for (UINT driver_type_index = 0; driver_type_index < num_driver_types;)
    
        res = D3D11CreateDevice(NULL, driver_types[driver_type_index], NULL, NULL, feature_levels, 
            num_feature_levels, D3D10_1_SDK_VERSION, &m_d3d_device, &m_feature_level, &m_imm_context);

        if (SUCCEEDED(res))
            break;

        ++driver_type_index;
    

    if (FAILED(res))
    
        return false;
    

    m_d3d_device->QueryInterface(__uuidof(IDXGIDevice), (void**)&m_dxgi_device);
    m_dxgi_device->GetParent(__uuidof(IDXGIAdapter), (void**)&m_dxgi_adapter);
    m_dxgi_adapter->GetParent(__uuidof(IDXGIFactory), (void**)&m_dxgi_factory);
    return true;


bool GraphicsEngine::release()

    m_dxgi_device->Release();
    m_dxgi_adapter->Release();
    m_dxgi_factory->Release();
    m_imm_context->Release();
    m_d3d_device->Release();

    return true;


GraphicsEngine * GraphicsEngine::get()

    static GraphicsEngine engine;
    return &engine;


SwapChain * GraphicsEngine::crreateSwapChain()

    return new SwapChain();

GraphicsEngine.h

#pragma once
#include <d3d11.h>

class SwapChain;

class GraphicsEngine

public:
    //initialize graphics engine and DirectX 11 device
    bool init();
    //release all the resources loaded
    bool release();

    static GraphicsEngine* get();

    SwapChain* crreateSwapChain();
private:
    ID3D11Device * m_d3d_device;
    D3D_FEATURE_LEVEL m_feature_level;
    ID3D11DeviceContext * m_imm_context;
    IDXGIDevice * m_dxgi_device;
    IDXGIAdapter * m_dxgi_adapter;
    IDXGIFactory * m_dxgi_factory;
    friend class SwapChain;

;

SwapChain.cpp:

#include "SwapChain.h"
#include "GraphicsEngine.h"

bool SwapChain::init(HWND hwnd, UINT width, UINT height)

    ID3D11Device *device = GraphicsEngine::get()->m_d3d_device;

    DXGI_SWAP_CHAIN_DESC desc;
    ZeroMemory(&desc, sizeof(desc));
    desc.BufferCount = 1;
    desc.BufferDesc.Width = width;
    desc.BufferDesc.Height = height;
    desc.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.BufferDesc.RefreshRate.Numerator = 60;
    desc.BufferDesc.RefreshRate.Denominator = 1;
    desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    desc.OutputWindow = hwnd;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.Windowed = TRUE;

    //Create the SwapChain for the window initialized by the HWND paramater
    HRESULT hr = GraphicsEngine::get()->m_dxgi_factory->CreateSwapChain(device, &desc, &m_swap_chain);

    if (FAILED(hr))
    
        return false;
    



    return true;


bool SwapChain::release()

    m_swap_chain->Release();
    delete this;
    return true;

SwapChain.h:

#pragma once
#include <d3d11.h>
class SwapChain

public:
    //Initialize a SwapChain for a window
    bool init(HWND hwnd, UINT width, UINT height);
    //release the SwapChain
    bool release();
private:
    IDXGISwapChain* m_swap_chain;
;
答案

在window.cpp中,我在此获得异常-> m_hwnd = hwnd;

问题出在这行代码:

Window* window = (Window*)((LPCREATESTRUCT)lparam)->lpCreateParams;

传入的lparam为空值,使window为空指针,最终导致访问错误。

如何解决?

当调用CreateWindowEx时,您需要在最终的void *参数中传递一个指向Window类的指针。

//creation of the window
m_hwnd = ::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "MyWindowClass", "DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768, NULL, NULL, NULL, window);

注意:(您可能遇到的问题)

如果计算机上存在Direct3D 11.1运行时,并且pFeatureLevels设置为NULL,此函数不会创建D3D_FEATURE_LEVEL_11_1设备。创建一个D3D_FEATURE_LEVEL_11_1设备,则必须显式提供一个D3D_FEATURE_LEVEL数组包括D3D_FEATURE_LEVEL_11_1。如果您提供D3D_FEATURE_LEVEL在不包含D3D_FEATURE_LEVEL_11_1的计算机上的数组安装了Direct3D 11.1运行时,此功能将立即生效失败并显示E_INVALIDARG。

参考:D3D11CreateDevice function

您可能需要使用D3D11_SDK_VERSION代替D3D10_1_SDK_VERSION中的D3D11CreateDevice

参考:How To: Get the Device Feature Level

第三次冲刺

...一小组进度情况小组各位成员都按着应有的进度实施着,在第二次冲刺过程中,解决了许多问题,尤是软件的兼容问题,随着手机系统版本的更新,我们必须保证此软件的适用性,大家都在尽最大努力去写好它。。二我的进度我... 查看详情

第三次scream冲刺

...计,并完善类的内容。二、个人在小组完成任务情况  在第三次scream冲刺中,我负责代码总和,汇总组员设计的代码进行改正拼装,然后对于不足的地方进行相对应的补充,让整体功能可以实现基本的运行。三、每日例会内容... 查看详情

吃鸡出现运行引擎dx11运行引擎需要dx11功能级别10.0怎么解决

...的右上角我们可以看到有搜索栏。3、在搜索栏中键入“DirectX”进行搜索,在搜索结果中找到“DirectX修复工具”、DirectX、DirectX11。4、在页面上我们可以选择下载安装DirectX或者DirectX11,然后将DirectX修复工具下载安装到电脑中。5... 查看详情

第三次作业

我的仓库心得体会这次的作业就是上次的改进版,不过又比上次更深入。这次的电梯调度我首先是从公交车的方案出发,就是每以层都停,但发现很耗时于是改进为如果到这层时间点上如果有乘客在等电梯那就停否则跳过从1层... 查看详情

现代软件工程第三次作业-自我评价的改进

自我评价的改进温浩:通过调查问卷的测评,我对自己的专业技能有了较为直观的认识,经过反思与总结,我认为自己需要做如下改进:1.从被动编程向主动编程转变。拿到一个项目,要真的的了解需求,主动发掘需求中的盲点... 查看详情

第三次过程性考核(代码片段)

...序的整数序列中,使结果序列仍然有序。输入格式:输入在第一行先给出非负整数N(<10);第二行给出N个从小到大排好顺序的整数;第三行给出一个整数X。输出格式:在一行内输出将X插入后仍然从小到大有序的整数序列,... 查看详情

layedit第三次改造

原文链接:http://www.bianbingdang.com/article_detail/105.html作为技术人员,在网上写代码,常常涉及代码块的编写。一个好的代码块,让页面美化很多。由于本站使用layui作为前端开发,layui自带的编辑器layedit的功能确实有些欠缺。随后... 查看详情

第三次过程性考核(代码片段)

...意:题目保证最大和最小值都是唯一的。输入格式:输入在第一行中给出一个正整数N(≤10),第二行给出N个整数,数字间以空格 查看详情

React + Redux:为啥在第三次加载时填充道具?

】React+Redux:为啥在第三次加载时填充道具?【英文标题】:React+Redux:Whyarepropspopulatedonthirdtimeload?React+Redux:为什么在第三次加载时填充道具?【发布时间】:2018-04-1221:52:33【问题描述】:查看我的控制台日志PROPS,前两个实例PRO... 查看详情

登录表单在第一次尝试在实时服务器上失败,但在第二次或第三次尝试

】登录表单在第一次尝试在实时服务器上失败,但在第二次或第三次尝试【英文标题】:loginformfailson1stattemptonliveserverbutworksonsecondorthird【发布时间】:2014-05-2716:54:12【问题描述】:我的登录表单在InternetExplorer中运行良好,但在C... 查看详情

UITableview 第三次没有加载数据?

】UITableview第三次没有加载数据?【英文标题】:UITableviewnotloadeddataon3rdtime?【发布时间】:2016-03-2909:08:11【问题描述】:不知道该怎么办。但是我在加载数据时加载了数据和加载表,[managerPOST:pathparameters:parameterssuccess:^(NSURLSession... 查看详情

InternetOpenUrl 在第三次和后续调用中挂起并失败

】InternetOpenUrl在第三次和后续调用中挂起并失败【英文标题】:InternetOpenUrlhangsandfailsonthirdandsubsequentcalls【发布时间】:2016-02-1618:28:49【问题描述】:这是一些奇怪但可重现的行为。我可以每个URL准确地调用InternetOpenUrl两次,并... 查看详情

巫师三次世代用directx11还是12

都行。使命召唤DX11左上角是GAMESCOPEDX12是VKD3D两个调整画质可以分辨调整在相同画质下目测12的清晰度更高但不知道为什么截图上11貌似更锐利帧生成时间11更快所以更稳目前来说11比12好12会有阴影的bug不知道是不是优化问题可以... 查看详情

访问 XMMATRIX 时 Dx11 崩溃

...RIX【发布时间】:2014-03-0221:29:40【问题描述】:我一直在DirectX11中创建一个植绒群项目,并在尝试创建指向对象的指针向量时出现错误,并且在第三次推回迭代时它会崩溃并跳转到此:XMFINLINE_XMMATRIX&_XMMATRIX::operator=(CONST_XMMATRI... 查看详情

使用 Direct 3D (C++) 的 DIrectX 11 2D 游戏引擎

】使用Direct3D(C++)的DIrectX112D游戏引擎【英文标题】:DIrectX112DGameEngineusingDirect3D(C++)【发布时间】:2016-10-1617:55:03【问题描述】:我在Internet上搜索了有关使用Direct3D在DirectX11中创建2D游戏引擎的帮助(而不是Direct2D包装器,大多数... 查看详情

2018/11/2周五集训队第三次比赛补题题解(代码片段)

这次题目都比较亲民啊。。。没有什么算法题倒是A.珠心算测试注意是数量的个数,不是等式的个数。。。而且我上来两发RE,也是很迷,直接用了map代码#include<bits/stdc++.h>usingnamespacestd;map<int,int>num;map<int,int>bk;map<i... 查看详情

directx12和directx11选哪个,有什么区别

如下:directx12和directx11比较为:directx12能100%API支持心渲染引擎、提高了多线程效率、软件平台更新。一、核心渲染引擎支持1、directx12:directx12对于核心渲染引擎是100%API支持。2、directx11:directx11对于核心渲染引擎仅仅是特性集... 查看详情

开始 DirectX 11 游戏编程错误

】开始DirectX11游戏编程错误【英文标题】:BeginningDirectX11GameProgrammingError【发布时间】:2012-07-3022:01:32【问题描述】:这是它在第33页给我的代码:#include<Windows.h>intWINAPIwWinMain(HINSTANCEhInstance,HINSTANCEprevInstance,LPWSTRcmdLine,intcmdSho... 查看详情