Sign in to follow this  
Talib

New at working with classes ... help!

Recommended Posts

Hi I've succesfully made a splashscreen in d3d with quads. Normally I don't use classes. However I would like to make my splashscreens dynamic and currently I have two functions: InitSplashGeometry() and DrawSplash(). Now I want to tie both together into a class so that I can new and delete my class. I want to put the InitSplashGeometry() into the constructor and have DrawSplash() as a member function. Here is my attempt: splash.h:
#ifndef __SPLASH_H__
#define __SPLASH_H__

#include <d3dx9.h>

class Splash  
{

public:
	Splash(LPDIRECT3DDEVICE9 pD3DDevice);
	virtual ~Splash();

private:
	struct CUSTOMVERTEX
	{
		float x, y, z;      // The untransformed, 3D position for the vertex
		D3DCOLOR color;        // The vertex color
		float       tu, tv;
	};

	#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1|D3DFVF_TEXCOORDSIZE2(1))

    CUSTOMVERTEX g_Vertices[] =
    {
        { -1.0f, 1.0f, 0.0f, 0x00ffffff,0.0f,0.0f, },
        {  1.0f, 1.0f, 0.0f, 0x00ffffff,1.0f,0.0f, },
        { -1.0f,-1.0f, 0.0f, 0x00ffffff,0.0f,1.0f, },
		{  1.0f,-1.0f, 0.0f, 0x00ffffff,1.0f,1.0f, },
    };

	
};

#endif
CUSTOMVERTEX g_Vertices[] = { { -1.0f, 1.0f, 0.0f, 0x00ffffff,0.0f,0.0f, }, { 1.0f, 1.0f, 0.0f, 0x00ffffff,1.0f,0.0f, }, { -1.0f,-1.0f, 0.0f, 0x00ffffff,0.0f,1.0f, }, { 1.0f,-1.0f, 0.0f, 0x00ffffff,1.0f,1.0f, }, }; This however results in: c:\program files\microsoft visual studio\myprojects\spacex\splash.h(28) : error C2059: syntax error : '{' c:\program files\microsoft visual studio\myprojects\spacex\splash.h(28) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body. why? Secondly. Apparently constructors can't have return values. How would one return error codes if something fails to initialize? Thanks Talib

Share this post


Link to post
Share on other sites
Usually you check whether or not the pointer is valid when a constructor is initialized, or add an "IsValid" inside the class to check against.

You have too many commas in your CUSTOMVERTEX g_Vertices[] =, kill off the last one. Also, you can't actually initialize variables from within the header like that, put it outside the class (above the class declaration, making it basically global) or initialize it via a loop in your cpp or inline in your .h.

-fel

Share this post


Link to post
Share on other sites
you cant initialize a member of a class like that. the only place you should initialize is in the constructor or initialization list. but you cant even do that with an array like that. so you would have to hard-code in the values or read it from a file. you might be able to make a seperate array, initialize it like that, then copy that one into your member array, if you think about it it should be possible. also, when you initialized the array, it looked like you were missing the last 2 members tu and tv

Share this post


Link to post
Share on other sites
Quote:
Original post by Talib
How would one return error codes if something fails to initialize?

You can't, which is why you should never put anything that might fail in a constructor.

Share this post


Link to post
Share on other sites
I'm not certain with the first question (I'm a 2-D programmer, and that looks like one of those crazy 3-D things =) ), but with the second one... perhaps you could use a try/catch block?

Anything wrapped in a try, if it throws an exception, will run the code in the "catch" block of code below it.

Perhaps you could also have a value for your objects that tells you when it's completely initialized, and then only set it to true at the very end of the constructor. That way, when you init your object you could do something like this:


object ThisObject();
if (ThisObject.Init == true) {
// it initialized
} else {
// it didn't
}


Does that help?

Share this post


Link to post
Share on other sites
Hi

What I mean with when something fails to initialize:


if( FAILED( g_pD3DDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pD3DVertexBuffer, NULL)))
{
return E_FAIL;
}


I want that in the constructor ... yet no return value is allowed. Any suggestions?

Share this post


Link to post
Share on other sites
I would not put that in a constructor myself but in a seperate initialise function. However there is one way to trap an error in a constructor and that is to use exceptions.

Share this post


Link to post
Share on other sites
A constructor should never fail. If yours can, you're asking for disasters with not-completely-constructed objects being passed around causing elusive bugs.

So move any code that can fail to another function.

Share this post


Link to post
Share on other sites
Thanks everybody.

So what you suggest is rather having a seperate member function for my initialization?

I think that would work and still tie my splashscreen into one dynamic object which I can new and delete.

One final question:

Does delete "trigger" the deconstructor or should I do that manually?

Thanks
Seeker

Share this post


Link to post
Share on other sites
Talib,

What you want is what alot of people want when they first start playing with OO languages but think about it a bit. How would you use it?

For the sake of argument let's say somehow you created a class Foo with a constructor that returns a value. How would you capture it?
Consider this:

Foo f( "This is an argument" );

See the problem? There's no way to capture the return value. C++'s solution to this is exceptions or having a separate Create or Init method.

So you would have some thing like:

Foo f;
if ( f.Create( "This is an argument" ) != E_FAIL )
{
...
}

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