gpt4 book ai didi

C++ DirectX11 : NullPtr error using DirectX tool kit

转载 作者:太空宇宙 更新时间:2023-11-04 13:22:25 25 4
gpt4 key购买 nike

我正在尝试为我的游戏实现一个字体类,但在运行时我不断收到以下错误

Exception thrown: write access violation. this was nullptr.

在 Font.cpp 的初始化函数中

void Font::Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
{
if (device != NULL && deviceContext != NULL)
{
m_spriteBatch = new SpriteBatch(deviceContext);//<----This line
m_spriteFont = new SpriteFont(device, L"Fonts/Arial.spritefont");
}
}

这是我的 Font.h

#ifndef FONT_H
#define FONT_H


#include <d3d11.h>
#include <directxmath.h>
#include <fstream>
#include "SpriteBatch.h"
#include "SpriteFont.h"

using namespace std;
using namespace DirectX;

class Font
{

public:
Font();
~Font();
void Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext);
void Render();

private:
SpriteFont* m_spriteFont;
SpriteBatch* m_spriteBatch;
};

#endif

这是我的 Font.cpp

#include "Font.h"
#include "SimpleMath.h"

Font::Font()
{
m_spriteFont = NULL;
m_spriteBatch = NULL;
}


Font::~Font()
{
if (m_spriteFont)
{
delete m_spriteFont;
m_spriteFont = NULL;

}
if (m_spriteBatch)
{
delete m_spriteBatch;
m_spriteBatch = NULL;

}
}
void Font::Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
{
if (device != NULL && deviceContext != NULL)
{
m_spriteBatch = new SpriteBatch(deviceContext);
m_spriteFont = new SpriteFont(device, L"Fonts/Arial.spritefont");
}
}
void Font::Render()
{
m_spriteBatch->Begin();
m_spriteFont->DrawString(m_spriteBatch, L"TEST", SimpleMath::Vector2(300, 300));
m_spriteBatch->End();
}

这是我的 GameScene.cpp

#include "GameScene.h"
#include "TextureShader.h"


GameScene::GameScene(void)
{
prevtime = 0;
collision = false;
Sprite_Box_Height = 0;
Sprite_Box_Width = 0;
}


GameScene::~GameScene(void)
{
if(m_player)
{
delete m_player;
}

if (m_enemy)
{
delete m_enemy;
}

if (m_ball)
{
delete m_ball;
}
if (m_timer)
{
delete m_timer;
}
}

bool GameScene::Initialize()
{
TextureShader* shader = (TextureShader*)ResourceManager::GetInstance()->GetShaderByName("texture.fx");
if(shader == NULL)
{
return false;
}
//create objects for all entities and initialize them
m_ball = new Ball();
m_ball->Initialize(Engine::GetEngine()->GetGraphics()->GetDevice(), Engine::GetEngine()->GetGraphics()->GetDeviceContext(), shader);
m_player = new Player();
m_player->Initialize(Engine::GetEngine()->GetGraphics()->GetDevice(), Engine::GetEngine()->GetGraphics()->GetDeviceContext(), shader);
m_enemy = new Enemy();
m_enemy->Initialize(Engine::GetEngine()->GetGraphics()->GetDevice(), Engine::GetEngine()->GetGraphics()->GetDeviceContext(), shader);
m_font->Init(Engine::GetEngine()->GetGraphics()->GetDevice(), Engine::GetEngine()->GetGraphics()->GetDeviceContext());

return true;
}

void GameScene::Update()
{
//Updade all Entities collision
m_player->Update();
m_enemy->Update();
m_ball->Update();
Collision();
}

void GameScene::Render(ID3D11DeviceContext* deviceContext, XMFLOAT4X4 viewMatrix, XMFLOAT4X4 projectionMatrix)
{
m_font->Render();
}
void GameScene::Collision()
{
//Get the current position of the ball and the player
BallPos = m_ball->GetBallPosition();
PlayerPos = m_player->GetPlayerPosition();
EnemyPos = m_enemy->GetPlayerPosition();
BallVel = m_ball->GetBallVelocity();
ballVelX = BallVel.m128_f32[0];
ballVelY = BallVel.m128_f32[1];


//Set the size of the sprite
Sprite_Box_Height = 50;
Sprite_Box_Width = 20;

//if there is a collision between the ball and a paddle get the current time
//and reverse the ball's velocity.
if (m_collision)
{
float currentTime = m_timer->GetTime();
//Wait for the ball to exit the area before setting collison back to false
//to prevent the ball from getting stuck
if (prevtime > currentTime - 100)
{
m_collision = true;
}
else
{
m_collision = false;
}
}
else
{
//#################################################################//
//set collision to true when the ball is in the area of the Paddle //
//check if ball is in the player's x vecinity //
//#################################################################//

//check if ball has near the same position player's paddle
if (BallPos.m128_f32[0] < PlayerPos.m128_f32[0] + Sprite_Box_Width && BallPos.m128_f32[0] > PlayerPos.m128_f32[0] - Sprite_Box_Width)
{
//check if ball hits the top of the player's paddle
if (BallPos.m128_f32[1] < PlayerPos.m128_f32[1] + Sprite_Box_Height && BallPos.m128_f32[1] > PlayerPos.m128_f32[1] - Sprite_Box_Height)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(-ballVelX, ballVelY);
m_collision = true;
}
}
else if (BallPos.m128_f32[0] < PlayerPos.m128_f32[0] + Sprite_Box_Width && BallPos.m128_f32[0] > PlayerPos.m128_f32[0] - Sprite_Box_Width)
{
//check if ball is in the player's y vecinity
if (BallPos.m128_f32[1] < PlayerPos.m128_f32[1] + Sprite_Box_Height && BallPos.m128_f32[1] > PlayerPos.m128_f32[1] - Sprite_Box_Height)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(-ballVelX, ballVelY);
m_collision = true;
}
}

else if (BallPos.m128_f32[0] < EnemyPos.m128_f32[0] + Sprite_Box_Width && BallPos.m128_f32[0] > EnemyPos.m128_f32[0] - Sprite_Box_Width)
{
//check if ball is in the player's y vecinity
if (BallPos.m128_f32[1] < EnemyPos.m128_f32[1] + Sprite_Box_Height && BallPos.m128_f32[1] > EnemyPos.m128_f32[1] - Sprite_Box_Height)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(-ballVelX, ballVelY);
m_collision = true;
}
}
//###################################################################//
//set collision to true when the ball hits the edge of the screen //
//by checking if ball's position is passed the edge and sets it back //
//to a position just inside the screen //
//###################################################################//
//if the ball hits the TOP edge of the screen change ball's velocity
if (BallPos.m128_f32[1] > 300.0f)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(ballVelX, -ballVelY);
m_collision = true;
}
//if the ball hits the BOTTOM edge of the screen change ball's velocity
else if (BallPos.m128_f32[1] < -300.0f)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(ballVelX, -ballVelY);
m_collision = true;
}
//if the ball hits the RIGHT edge of the screen change ball's velocity
else if (BallPos.m128_f32[0] > 400.0f)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(-ballVelX, ballVelY);
m_collision = true;
}
//if the ball hits the LEFT edge of the screen change ball's velocity
else if (BallPos.m128_f32[0] < -400.0f)
{
prevtime = m_timer->GetTime();
m_ball->SetBallVelocity(-ballVelX, ballVelY);
m_collision = true;
}
}
}

提前感谢您的帮助!!!

最佳答案

DirectX 工具包中的原因之一 tutorials我建议使用 std::unique_ptr 而不是原始指针,因为它确实简化了代码。

// Font.h

#pragma once

#include <d3d11.h>
#include <directxmath.h>
#include <fstream>
#include <memory>
#include "SpriteBatch.h"
#include "SpriteFont.h"

class Font
{

public:
Font() {}
void Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext);
void Render();

private:
std::unique_ptr<DirectX::SpriteFont> m_spriteFont;
std::unique_ptr<DirectX::SpriteBatch> m_spriteBatch;
};

// Font.cpp
#include "Font.h"
#include "SimpleMath.h"

using namespace std;
using namespace DirectX;

void Font::Init(ID3D11Device* device, ID3D11DeviceContext* deviceContext)
{
if (device != NULL && deviceContext != NULL)
{
m_spriteBatch = std::make_unique<SpriteBatch>(deviceContext);
m_spriteFont = std::make_unique<SpriteFont>(device, L"Fonts/Arial.spritefont");
}
}

void Font::Render()
{
m_spriteBatch->Begin();
m_spriteFont->DrawString(m_spriteBatch.get(), L"TEST", SimpleMath::Vector2(300, 300));
m_spriteBatch->End();
}

You should avoid having using namespace statements in .h files as it basically defeats the whole purpose of using C++ namespaces in the first place. You should put using namespace statements only in .cpp files, and then use fully qualified names in headers.

从您发布的代码中没有明显的理由说明您为什么会收到异常。我建议您进入代码以查看异常的真正来源。如果您使用的是 NuGet,那么您可能希望改用项目到项目的引用。查看DirectX Tool Kit维基了解详情。

您可以尝试使用 Fonts\\Arial.spritefont 而不是 Fonts/Arial.spritefont 看看这是否有区别。

With modern compilers, the old-school C-style if #ifndef FONT_H #define FONT_H .. #endif is not necessarily. Just use #pragma once.

注意:如果您使用的是 VS 2012 而不是 VS 2013 或 VS 2015,则无法使用 std::make_unique。您可以改为执行以下操作:

m_spriteBatch.reset( new SpriteBatch(deviceContext) );
m_spriteFont.reset( new SpriteFont(device, L"Fonts/Arial.spritefont") );

关于C++ DirectX11 : NullPtr error using DirectX tool kit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34667559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com