Archived

This topic is now archived and is closed to further replies.

kingpinzs

How to make a class

Recommended Posts

can some help me change this to a class?
void Panel()
{
float PanelWidth = 200.0f;
float PanelHeight = 200.0f;

g_pD3DDevice->CreateVertexBuffer(4 * sizeof(PANELVERTEX), D3DUSAGE_WRITEONLY,D3DFVF_PANELVERTEX, 
                                    D3DPOOL_MANAGED,&p_VertexBuffer );

PANELVERTEX* pVertices = NULL;
p_VertexBuffer->Lock(0, 4 * sizeof(PANELVERTEX), (BYTE**)&pVertices,0);
pVertices[0].color = pVertices[1].color = pVertices[2].color = pVertices[3].color = 0xffffffff;

pVertices[0].x = pVertices[3].x = - PanelWidth / 2.0f;
pVertices[1].x = pVertices[2].x = PanelWidth /2.0f;
pVertices[0].y = pVertices[1].y = PanelHeight / 2.0f;
pVertices[2].y = pVertices[3].y = - PanelHeight / 2.0f;
pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;
pVertices[1].u = pVertices[2].u = 0.625f;
pVertices[0].u = pVertices[3].u = 0.0f;
pVertices[0].v = pVertices[1].v = 0.0f;
pVertices[2].v = pVertices[3].v = 0.9375f;

p_VertexBuffer->Unlock();


D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture.jpg", 0, 0, 0, 0,
                            D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
                            D3DX_DEFAULT, 0, NULL,NULL,&m_pD3DTexture);

}
 

Share this post


Link to post
Share on other sites
I''ll give it a shot, but I''ll put everything in the .h file, to be evil


class CPanel
{
public:
CPanel() {;};
~CPanel() {;};
void Panel(){ /* insert panel Code Here*/};
private:
//insert any global variables that will only be used by your class here
};


I''m not sure what this will accomplish tho. Usually, a class needs more than a single function to be truely useful.



~~~~~
"Go on then" - benjamin bunny.
Download and play Slime King I.

Share this post


Link to post
Share on other sites
*sigh*

here... in a .h:

class cMyClass
{
public:
void Panel();
};

in a .cpp:

#include "insert_your_h_file_name_here.h"

void cMyClass:anel()
{
float PanelWidth = 200.0f;float PanelHeight = 200.0f;g_pD3DDevice->CreateVertexBuffer(4 * sizeof(PANELVERTEX), D3DUSAGE_WRITEONLY,D3DFVF_PANELVERTEX, D3DPOOL_MANAGED,&p_VertexBuffer );PANELVERTEX* pVertices = NULL;p_VertexBuffer->Lock(0, 4 * sizeof(PANELVERTEX), (BYTE**)&pVertices,0);pVertices[0].color = pVertices[1].color = pVertices[2].color = pVertices[3].color = 0xffffffff;pVertices[0].x = pVertices[3].x = - PanelWidth / 2.0f;pVertices[1].x = pVertices[2].x = PanelWidth /2.0f;pVertices[0].y = pVertices[1].y = PanelHeight / 2.0f;pVertices[2].y = pVertices[3].y = - PanelHeight / 2.0f;pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;pVertices[1].u = pVertices[2].u = 0.625f;pVertices[0].u = pVertices[3].u = 0.0f;pVertices[0].v = pVertices[1].v = 0.0f;pVertices[2].v = pVertices[3].v = 0.9375f;p_VertexBuffer->Unlock();D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0, NULL,NULL,&m_pD3DTexture);
}

and in your main c++ file:

#include "insert_your_h_file_name_here.h"

cMyClass myclass;

somewhere in a function to call panel:

myclass.panel();

Share this post


Link to post
Share on other sites
quote:
Original post by kingpinzs
I see. Thank you for your help.It took me a while to figure out that it needed to be in a class and not a function.
May I ask why this function needs a class?



~~~~~
" I...went from hopeless to sucky in a relatively short time." - entivore.
Download and play Slime King I.

Share this post


Link to post
Share on other sites
Becuase I have to make a lot of panels and instead of making alot of the same thing i am going to try to just make a class nad call it everytime I make a new panel. That is the only way I can figure out how to make a lot of 2d objects.

Share this post


Link to post
Share on other sites
quote:
Original post by kingpinzs
Becuase I have to make a lot of panels and instead of making alot of the same thing i am going to try to just make a class nad call it everytime I make a new panel. That is the only way I can figure out how to make a lot of 2d objects.

Ahh! You don''t need a class, you need a structure.
do this...
struct sPanal
{
float PanelWidth;
float PanelHeight;
//other variables you might need.
};


Then, in your code, just put this line:

sPanal Panaldata[100];

there! you have an array of 100 panels, you can then pass your panal data into your Panal function. No need to make 100 classes. You can also store data relevant to your panel.

I use this structure with my tile engine.
struct tileAtt
{//can you move this way?
bool bSolid;//solid? (for collisions)
bool bContainsTile;//true, contains tile, false otherwise
int iTextureInd;//Tile Number
bool bTexture;//is there a texture for this tile?
float fRot;//amount of rotation needed
float color3f[3];
};


Then in my engine class, I use the data to produce all the tiles I need.

~~~~~
" I...went from hopeless to sucky in a relatively short time." - entivore.
Download and play Slime King I.

Share this post


Link to post
Share on other sites
alright now I undersatnd I need to make a structure.
But how do I turn all this to a structure.

void camera2d(float WindowWidth, float WindowHeight)
{
D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

//Setup the orthogonal projection matrix and the default world/view matrix

D3DXMatrixOrthoLH(&Ortho2D, WindowWidth, WindowHeight, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &Identity);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &Identity);

//Make sure that the z-buffer and lighting are disabled

g_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

g_pD3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
g_pD3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
g_pD3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
g_pD3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

}




void Panel()
{
float PanelWidth = 200.0f;
float PanelHeight = 200.0f;

g_pD3DDevice->CreateVertexBuffer(4 * sizeof(PANELVERTEX), 0,D3DFVF_PANELVERTEX,
D3DPOOL_DEFAULT,&p_VertexBuffer );

PANELVERTEX* pVertices = NULL;

p_VertexBuffer->Lock(0, 4 * sizeof(PANELVERTEX), (BYTE**)&pVertices,0);
pVertices[0].color = pVertices[1].color = pVertices[2].color = pVertices[3].color = 0xffffffff;

pVertices[0].x = pVertices[3].x = - PanelWidth / 2.0f;
pVertices[1].x = pVertices[2].x = PanelWidth /2.0f;
pVertices[0].y = pVertices[1].y = PanelHeight / 2.0f;
pVertices[2].y = pVertices[3].y = - PanelHeight / 2.0f;
pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;
pVertices[1].u = pVertices[2].u = 0.625f;
pVertices[0].u = pVertices[3].u = 0.0f;
pVertices[0].v = pVertices[1].v = 0.0f;
pVertices[2].v = pVertices[3].v = 0.9375f;

p_VertexBuffer->Unlock();


D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture.jpg", 0, 0, 0, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, 0, NULL,NULL,&m_pD3DTexture);

}


   
void render2D()
{

D3DXMATRIX Position;
D3DXMatrixTranslation(&Position, 50.0f, 0.0f, 0.0f);
g_pD3DDevice->SetTransform(D3DTS_WORLD, &Position);
g_pD3DDevice->SetVertexShader (D3DFVF_PANELVERTEX);
g_pD3DDevice->SetStreamSource (0, p_VertexBuffer, sizeof(PANELVERTEX));
g_pD3DDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
g_pD3DDevice->SetTexture(0,m_pD3DTexture);

}



This is what I have so far.

struct panel
{
float panelwidth;
float panelhight;
float screenwidth;
float screenhight;

}

But now exactly what I want


I want soem thing that will do this

Panel->D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, 0, NULL,NULL,&m_pD3DTexture);
Panel2->D3DXCreateTextureFromFileEx(g_pD3DDevice, "Texture2.jpg", 0, 0, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT,
D3DX_DEFAULT, 0, NULL,NULL,&m_pD3DTexture);



[edited by - kingpinzs on October 6, 2003 6:58:01 PM]

[edited by - kingpinzs on October 6, 2003 6:58:47 PM]

Share this post


Link to post
Share on other sites
can anyone tell me why when you created a object on the heap you have to makeit a pointer...and when this happens you cant access any of class members or functions

Share this post


Link to post
Share on other sites
quote:
Original post by Rydis
can anyone tell me why when you created a object on the heap you have to makeit a pointer...and when this happens you cant access any of class members or functions


Well if you do something like this where Alien is a struct or a class with a default constructor:

Alien * greenMartian = new Alien;

All you've really done is created a pointer, not an Alien object itself. greenMartian, in its current state, points to a section of memory big enough to hold an Alien object. So doing this and assuming eyes is an int that your object can store:

cout << greenMartians->eyes;

That will get you something wierd until you actually initalize the object the being pointer at. I sometimes mix this up, but I don't think the constructor gets called when you do what I did above when I called new. If I am wrong on that point then the problem might be that your constructor doesn't do anything. Does your class's constructor assign default values to its data members? Otherwise its like doing this:

int whatever;
cout << whatever;

Someone feel free to correct me if I am wrong about when the constructor gets called.


[edited by - logic monkey on October 6, 2003 10:12:49 PM]

Share this post


Link to post
Share on other sites
ya it has assingments in the default constructor....but from what i get that you said...a pointer that points to an object dont seem to be right in the case...case the compiler im using as school(vc++.net) regonizes it clearly as a pointer to an object with no data members or functions...would this be fixed by using -> instead of the .. cause using the dot operator is useless so something like

Auction *newAuction = new Auction();
newAuction.Item; //this is impossiable cause its saying newAuction dont have the data member Item...but if you did
Auction newAuction;
newAuction.Item; //this is possiable

so will using -> fix this or what?

Share this post


Link to post
Share on other sites
quote:
Original post by Rydis
So will using -> fix this or what?

Yes.

newAuction->Item is shorthand for (*newAuction).Item, which dereferences newAuction before it accesses the data field. You obviously have to do this if you''re using a pointer.

Share this post


Link to post
Share on other sites
quote:
Original post by Rydis
ya it has assingments in the default constructor....but from what i get that you said...a pointer that points to an object dont seem to be right in the case...case the compiler im using as school(vc++.net) regonizes it clearly as a pointer to an object with no data members or functions...would this be fixed by using -> instead of the .. cause using the dot operator is useless so something like

Auction *newAuction = new Auction();
newAuction.Item; //this is impossiable cause its saying newAuction dont have the data member Item...but if you did
Auction newAuction;
newAuction.Item; //this is possiable

so will using -> fix this or what?


Yep, if the thing on the left of where you need to use the dot operator is a physically just a pointer, using the -> operator is the way to get to your data correctly. If it''s not a pointer, use the dot like usual. Sorry for answering a different question than the one you asked.

Share this post


Link to post
Share on other sites