Sign in to follow this  
HinDRAncE

lil problem wrapping up my sprites

Recommended Posts

hello forum, i managed to wrap up my sprite inside a struct, the general idea is to use it inside a "window" class or struct that will have N number of sprites (one for the background, one for the border, etc). the number of sprites used in each window object will vary, as i plan to use the window class to create diferent types of windows. I figured i should use vectors to store the sprite struct, but the program just crashes when i use vectors: Sprite.h
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

struct sprite{
	D3DXVECTOR3 center;				// center of the sprite
    D3DXVECTOR3 position;			// position of the sprite to be drawn
	LPDIRECT3DTEXTURE9 Mysprite;    // the pointer to the sprite
	LPCWSTR FName;					//file name of the texture to load
	sprite::sprite();//null constructor
	sprite::sprite(float x, float y, float centerX,float centerY, LPCWSTR FileName);//constructor with parameters
	void sprite::DoDraw(LPDIRECT3DDEVICE9 D3DINTERFACE,LPD3DXSPRITE SPRITEINTERFACE);//draw the sprite
};
#endif


sprite.cpp:
#include "sprite.h"
sprite::sprite(float x, float y,float centerX, float centerY, LPCWSTR FileName){
	FName= FileName;
	center= D3DXVECTOR3(x,y,0.0f);
	position= D3DXVECTOR3(centerX,centerY,0.0f);
}
sprite::sprite(){//load a default texture
	FName= L"NULL.png";
	center= D3DXVECTOR3(0.0f,0.0f,0.0f);
	position= D3DXVECTOR3(0.0f,0.0f,0.0f);
}
void sprite::DoDraw(LPDIRECT3DDEVICE9 D3DINTERFACE,LPD3DXSPRITE SPRITEINTERFACE){
	if(Mysprite==NULL){//only load the texture if  there is no texture loaded
D3DXCreateTextureFromFileEx(D3DINTERFACE,    // the device pointer
                            FName,    // the new file name
                            D3DX_DEFAULT,    // default width
                            D3DX_DEFAULT,    // default height
                            D3DX_DEFAULT,    // no mip mapping
                            NULL,    // regular usage
                            D3DFMT_A8R8G8B8,    // 32-bit pixels with alpha
                            D3DPOOL_MANAGED,    // typical memory handling
                            D3DX_DEFAULT,    // no filtering
                            D3DX_DEFAULT,    // no mip filtering
                            D3DCOLOR_XRGB(255, 0, 255),    // the hot-pink color key
                            NULL,    // no image info struct
                            NULL,    // not using 256 colors
                            &Mysprite);    // load to sprite
	}//end if	
SPRITEINTERFACE->Draw(Mysprite, NULL, &center, &position, D3DCOLOR_ARGB(120, 255, 255, 255));//draw the sprite
}


swindow.h
#ifndef SWINDOW_H
#define SWINDOW_H
//-------------------------------------------------------------------------------------
//SPRITE WINDOW 19-14-18 HinDRAncE | draw a window with sprites
//-------------------------------------------------------------------------------------
#include "sprite.h"
struct spriteWindow{
float x;
float y;
//sprite graphs;<--works
std::vector<sprite> graphs;//doesnt
bool isvisible;
spriteWindow::spriteWindow(LPCWSTR GRAPHNAME, float x, float y);
spriteWindow::spriteWindow();

void spriteWindow::SetGraph(LPCWSTR GRAPHNAME,float x,float y);
void Draw(LPDIRECT3DDEVICE9 D3DINTERFACE,LPD3DXSPRITE SPRITEINTERFACE);
};
#endif


swindow.cpp
#include "swindow.h"

spriteWindow::spriteWindow(LPCWSTR GRAPHNAME, float x, float y){
	/*graphs.FName=GRAPHNAME;
	graphs.position=D3DXVECTOR3(x,y,0.0f);*///<--works
	graphs.push_back(sprite(0,0,0,0,L"null.bmp"));//<--crashes at load time
}
spriteWindow::spriteWindow(){
	graphs.position=D3DXVECTOR3(0,0,0.0f);
}

void spriteWindow::Draw(LPDIRECT3DDEVICE9 D3DINTERFACE,LPD3DXSPRITE SPRITEINTERFACE){
	graphs[0].DoDraw(D3DINTERFACE,SPRITEINTERFACE);//just an example, it will crash before we even try to draw
}

void spriteWindow::SetGraph(LPCWSTR GRAPHNAME,float x,float y){//change the graphic
	graphs.FName=GRAPHNAME;
	graphs.position=D3DXVECTOR3(x,y,0.0f);
}


At draw time:
sWindow MyWindow;
d3ddev->BeginScene();
//doing some 3d stuff here
d3dspt->Begin(D3DXSPRITE_ALPHABLEND);    // begin sprite drawing
MyWindow.Draw(d3ddev,d3dspt);
d3dspt->End();    // end sprite drawing
//doing some text drawing here
d3ddev->EndScene();


Also, what do you think of the way i'm doing things? i'm trying to make this as modular as possible and i plan to add mouse click functionality to the window class later on.

Share this post


Link to post
Share on other sites
ok, i think i better try inheritance...have a GUI base class that derives into a window, that derives into a component (say a button, a graphic, a text box). i'll let you know how that goes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this