I'm back! :).
Well, I am now working on a basic sprite class that uses LPD3DXSPRITE. I have it where it can draw the sprite. Yay...me happy right? Wrong! Why? Because when I go to exit there seems to be a problem. I get a run time error from Visual C++ saying that the program has triggered a break point. Then it shows me the disassembly. I have tried debugging over and over again...and I can't seem to figure out why it's happening. I can tell it's happening at the end, as that is when the break point is triggered, when I exit. I have checked my release functions and that stuff, and they all seem to be fine. Maybe y'all can help me out. I will show y'all the wrapper class, and the demo I created for it.
EGLSprite.h
/*EGLSprite
*this is the class for the EGL sprite class, so people can use 2D images
*this will lie in the EGLGraphics namespace
*/
//inclusion guards
#ifndef EGLSPRITE_H_
#define EGLSPRITE_H_
//files that we need to include
#include <string>
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
namespace EGLGraphics
{
class EGLSprite
{
//public
public:
//constructor
EGLSprite();
//destructor
~EGLSprite();
//Load Function, to load from a disk file
HRESULT LoadEGLSprite(LPDIRECT3DDEVICE9 pDevice, std::string filename);
//render function
HRESULT RenderEGLSprite(LPDIRECT3DDEVICE9 pDevice);
//delete the sprite
void ReleaseEGLSprite();
//get the sprite we are using
LPD3DXSPRITE GetSprite() {return sprite;}
//private mode:
private:
//texture
LPDIRECT3DTEXTURE9 spriteTexture;
//sprite
LPD3DXSPRITE sprite;
std::string filename;
//sprites center
D3DXVECTOR3 center;
D3DXVECTOR3 position;
};
}
#endif
EGLSprite.cpp
/*EGLSprite.cpp
*class definitions for the EGLSprite
*/
//include the EGLSprite.h
#include "EGLSprite.h"
//constructor
EGLGraphics::EGLSprite::EGLSprite()
{
sprite=NULL;
spriteTexture=NULL;
center=D3DXVECTOR3(0.0f, 0.0f, 0.0f);
position=D3DXVECTOR3(50.0f, 50.0f, 0.0f);
}
EGLGraphics::EGLSprite::~EGLSprite()
{
}
HRESULT EGLGraphics::EGLSprite::LoadEGLSprite(LPDIRECT3DDEVICE9 pDevice, std::string filename)
{
if(!pDevice)
{
return -1;
}
if(this->sprite)
{
sprite->Release();
}
if(this->spriteTexture)
{
this->spriteTexture->Release();
this->spriteTexture=NULL;
}
D3DXCreateSprite(pDevice, &sprite);
//create a texture surface from a file
HRESULT result=D3DXCreateTextureFromFile(pDevice, filename.c_str(), &spriteTexture);
if(FAILED(result))
{
return -1;
}
if(FAILED(result))
{
return -1;
}
return 0;
}
HRESULT EGLGraphics::EGLSprite::RenderEGLSprite(LPDIRECT3DDEVICE9 pDevice)
{
if(!pDevice)
{
return -1;
}
HRESULT result;
result=this->sprite->Draw(this->spriteTexture, NULL, ¢er, &position, D3DCOLOR_XRGB(255, 255, 255));
if(FAILED(result))
{
return -1;
}
return 0;
}
void EGLGraphics::EGLSprite::ReleaseEGLSprite()
{
if(sprite)
{
this->sprite->Release();
}
sprite=0;
if(spriteTexture)
{
this->spriteTexture->Release();
}
spriteTexture=0;
}
Demo File
//this is the demo for the EGL library
//include iostream
#include <iostream>
//first include the EGL.h file, as that is the main file
#include "EGL.h"
//include the EGLGraphics.h file
#include "EGLGraphics.h"
//start main
int main(int argc, char* argv[])
{
//create a SDL_Event, so we can poll for events and handle input
SDL_Event evnt;
//get the current keystates
Uint8* keystates=NULL;
//CONSTANTS TO HOLD THE WINDOWS WIDTH AND HEIGHT AND ASPECT RATIO
const int WIDTH=800;
const int HEIGHT=600;
const float ASPECTRATIO=(float)WIDTH/(float)HEIGHT;
//init the window using the built in InitWindow from EGL's namespace
if(EGL::InitEGLWindow(800, 600)==false)
{
std::cout<<"could not create the window"<<std::endl;
}
if(EGL::referenceDevice==NULL)
{
std::cout<<"the device is null...we need out..."<<std::endl;
return 1;
}
EGLGraphics::EGLSprite dog;
dog.LoadEGLSprite(EGL::referenceDevice, "sprite.png");
//turn lighting off
EGL::referenceDevice->SetRenderState(D3DRS_LIGHTING, false);
std::cout<<"window is now inited, and about to enter game loop"<<std::endl;
//before we loop, make sure are device is not null
if(EGL::referenceDevice==NULL)
{
std::cout<<"device is null...QUIT..."<<std::endl;
return 1;
}
D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
//aim the camera at the orgin (0, 0, 0)
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
//the y-axis is up
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
//view matrix
D3DXMATRIX view;
//define where the camera should look at
D3DXMatrixLookAtLH(&view, &position, &target, &up);
//set to transform to the view matrix now
EGL::referenceDevice->SetTransform(D3DTS_VIEW, &view);
//projection matrix
D3DXMATRIX projection;
D3DXMatrixPerspectiveFovLH(&projection, D3DXToRadian(90), ASPECTRATIO, 1.0f, 1000.0f);
//projection transform
EGL::referenceDevice->SetTransform(D3DTS_PROJECTION, &projection);
//now loop while we are not done
while(1)
{
//while there is a event the check
while(SDL_PollEvent(&evnt))
{
if(evnt.type==SDL_QUIT)
{
return 0;
}
break;
}
keystates=SDL_GetKeyState(NULL);
EGL::ClearScreen(EGL::referenceDevice, EGL::ClearRGBColor(0, 255, 255));
EGL::BeginRender(EGL::referenceDevice);
//draw the dog
dog.GetSprite()->Begin(D3DXSPRITE_ALPHABLEND);
dog.RenderEGLSprite(EGL::referenceDevice);
dog.GetSprite()->End();
EGL::EndRender(EGL::referenceDevice);
EGL::Present(EGL::referenceDevice);
}
dog.ReleaseEGLSprite();
//release the device with a homemade templated release function
EGL::Release<EGL::EGLDevice>(EGL::referenceDevice);
return 0;
}
Yes, the sprite renders exactly how I want it to. If I could only now seem to smart enough to figure out why DirectX hates me when I add stuff to my graphics namespace (being sarcastic, as I can...but it seems like all my wrappers in there break at first) but not when I add stuff to anything else (Like...I got my window working just fine, and I did lighting just fine also). hehe.
So can anyone help me...again! This gets annoying always asking for help, when something turns out to be so simple.
Thanks to all who help!
EDIT: well, I started to do more debugging and I finally got it to tell me more about the error. It says that there is a missing operator.
Chad.