Skip to main content
gamedev.net
🔒 Locked

using a loop during the render stage

Started by bigbrother2000 Nov 2, 2004 at 12:52 PM 6 replies 750+ views
Original Post
bigbrother2000
bigbrother2000
Hello everyone. I'm using a cuboid class which is used to make a cube, however i need a larger amount, say 100 so i have created an array of them eg. cube[100]. Although when it comes to rendering these to the screen am i having a bit of trouble because it seems you cannot use a loop in this section of the code like a for or while loop to increment a variable (cube[x]) to display all 100 cubes. Is there any other way to do this rather than typing m_pCube1[1]->Render(); 100 times. I am new to directx and any help would be appreciated.
Tueur
Tueur
i think it's becose it take too many time to render...
but that's just a guess...
Squirell
Squirell
The way I do it is like so:

/*class Cube defined elsewhere//Storage for all the cubesvector<Cube*> Cubes;//Cubes are initalized//Render loopfor(int i = 0; i < Cubes.size(); i++) {Cubes->Render();}


This has always worked for me. And I doubt theres any limit to the number of times you can loop. Post your code so we can help you find the error.
bigbrother2000
bigbrother2000
hi Squirell, thats very similar to mine:

//inheader file
CCuboid* m_pCube1[100];
//in game.cpp file
m_pCube1[100] = NULL;

//in initialisation function
m_pCube1[1] = new CCuboid(m_pD3DDevice);
m_pCube1[1]->SetSize(15, 15, 15);
m_pCube1[1]->SetTexture("1.bmp");

//in render function
//Begin the scene
m_pD3DDevice->BeginScene();

//Setup camera and perspective
SetupCamera();

for (int a = 1; a<100; a++) {
m_pCube1[a]->Render();
}

//End the scene
m_pD3DDevice->EndScene();


The code compiles without any errors however when running it crashes saying the .exe has encountered a problem and needs to close. However if i just change this to m_pCube1[1]->Render(); it show one cube without crashing.


DrunkenHyena
DrunkenHyena
Quote:
Original post by bigbrother2000
//in initialisation function
m_pCube1[1] = new CCuboid(m_pD3DDevice);
m_pCube1[1]->SetSize(15, 15, 15);
m_pCube1[1]->SetTexture("1.bmp");

You're actually initializing all 100 of your cubes, right? Because it looks like you're only initializing 1 and it's not even the first one in the array (m_pCube1[0] would be the first).
Stay Casual,KenDrunken Hyena</FON
Yratelev
Yratelev
Also, one last thing, all your cubes will be rendered on top of each other, because withing the loop you are not setting a different matrix as the world matrix.

(just so you dont think theres only 1 cube when you render all 100)

Yratelev
Yratelev
lubby
lubby
Could also be trying to render the same cube 100 times, probably a wise thing to do if its that simple a case.

CCuboid* m_pCube;//in game.cpp file//in initialisation functionm_pCube = new CCuboid(m_pD3DDevice);m_pCube->SetSize(15, 15, 15);m_pCube->SetTexture("1.bmp");//in render function//Begin the scenem_pD3DDevice->BeginScene();//Setup camera and perspectiveSetupCamera();for (int a = 0; a<100; a++) {   // translate to position   // now render   m_pCube1->Render();}//End the scenem_pD3DDevice->EndScene();


As some people above mentioned you start indexing and counting at 1, this is not a great idea, start your indexing at 0. Unless you have a good reason not to.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.