New at working with classes ... help!

Started by
9 comments, last by MauMan 19 years, 9 months ago
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
Try, try and fucking try again.
Advertisement
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
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
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
FTA, my 2D futuristic action MMORPG
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.
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?
-Vendal Thornheart=) Programming for a better tomorrow... well,for a better simulated tomorrow. ;)
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?
Try, try and fucking try again.
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.
------------------------See my games programming site at: www.toymaker.info
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.
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
Try, try and fucking try again.
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 )
{
...
}

---CyberbrineDreamsSuspected implementation of the Windows idle loop: void idle_loop() { *((char*)rand()) = 0; }

This topic is closed to new replies.

Advertisement