I'm trying to get a sprite to appear on my screen in the pong game I'm making but there seems to be some sort of bug that occurs. Right after the line that loads the texture executes, I get an unhandled exception. Here's the code:
Object.h
#ifndef OBJECT_H
#define OBJECT_H
#include <d3d9.h>
#include <d3dx9.h>
#include <string>
#include "Graphics.h"
class Object
{
public:
Object();
~Object();
void setTexture( LPDIRECT3DTEXTURE9 t );
void setPosition( float x, float y );
LPDIRECT3DTEXTURE9 getTexture() { return this -> texture; }
LPD3DXVECTOR3 getPosition() { return this -> position; }
private:
LPDIRECT3DTEXTURE9 texture;
LPD3DXVECTOR3 position;
};
#endif OBJECT_H
Object.cpp
#include "Object.h"
#include "Globals.h"
Object::Object()
{
texture = NULL;
position = NULL;
}
Object::~Object()
{
RELEASE( texture );
delete position;
position = NULL;
}
void Object::setTexture( LPDIRECT3DTEXTURE9 t )
{
texture = t;
}
void Object::setPosition( float x, float y )
{
position -> x = x;
position -> y = y;
}
Graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include "DxManager.h"
#include <string>
class Graphics
{
public:
bool InitGraphics( HWND &window );
void StartScene();
void Render();
void EndScene();
void Update();
LPDIRECT3DTEXTURE9 LoadTexture( std::string file );
DxManager getD3DManager() { return this -> D3DManager; }
private:
DxManager D3DManager;
};
#endif GRAPHICS_H
Graphics.cpp
#include "Graphics.h"
#include "Globals.h"
bool Graphics::InitGraphics( HWND &window )
{
if( !D3DManager.InitD3D( window ) )
{
GameOver = true;
MessageBox( window, "Error initializing DirectX9", "Error", MB_OK );
return false;
}
return true;
}
void Graphics::StartScene()
{
D3DManager.getDirect3DDevice() -> Clear( 0, NULL, D3DCLEAR_TARGET, Black, 0.0f, 0 );
D3DManager.getDirect3DDevice() -> BeginScene();
D3DManager.getSpriteObject() -> Begin( D3DXSPRITE_ALPHABLEND );
}
void Graphics::Render()
{
}
void Graphics::EndScene()
{
D3DManager.getSpriteObject() -> End();
D3DManager.getDirect3DDevice() -> EndScene();
D3DManager.getDirect3DDevice() -> Present( NULL, NULL, NULL, NULL );
}
void Graphics::Update()
{
StartScene();
Render();
EndScene();
}
LPDIRECT3DTEXTURE9 Graphics::LoadTexture( std::string file )
{
LPDIRECT3DTEXTURE9 texture;
D3DXIMAGE_INFO info;
D3DXGetImageInfoFromFile( file.c_str(), &info );
D3DXCreateTextureFromFileEx(
D3DManager.getDirect3DDevice(),
file.c_str(),
info.Width, info.Height,
1,
D3DPOOL_DEFAULT,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
Transparent,
&info,
NULL,
&texture );
return texture;
}
WinMain.cpp
Graphics g;
g.InitGraphics( window );
Object Ball;
Ball.setTexture( g.LoadTexture( "Resources/Ball.png" ) );
Ball.setPosition( 500, 100 );
while ( GameOver != true )
{
if ( PeekMessage( &message, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &message );
DispatchMessage( &message );
}
//g.Update();
g.StartScene();
g.getD3DManager().getSpriteObject() -> Draw( Ball.getTexture(), NULL, NULL, Ball.getPosition(), White );
g.EndScene();
}
Is there anything that I'm doing wrong? Thanks for the help in advance.