Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » creating graphic primitive with loop problem

This thread is locked; no one can reply to it. rss feed Print
creating graphic primitive with loop problem
Pyreal
Member #15,537
March 2014

Working on a problem that requires me to create a triangle with primitives. Then use a loop to create smaller triangles till it becomes small. I have the basic set up but once I run my program is shows my primitives and then closes.

Any idea what might be wrong?

{"name":"2oIYqS7.jpg","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/a\/da9e04377024abfc28068eb0bf2c6700.jpg","w":494,"h":722,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/a\/da9e04377024abfc28068eb0bf2c6700"}2oIYqS7.jpg

#SelectExpand
1 /* Type your variable declarations here */ 2 int xCoordinateOfTopVertex = 200; 3 int yCoordianteOfTopVertex = 100; 4 int xCoordianteOfLeftVertex = 100; 5 int yCoordianteOfLeftVertex = 300; 6 int xCoordianteOfRightVertex = 300; 7 int yCoordianteOfRightVertex = 300; 8 //int height = (yCoordianteOfTopVertex + yCoordianteOfLeftVertex + yCoordianteOfRightVertex); 9 //int width = (xCoordinateOfTopVertex + xCoordianteOfLeftVertex + xCoordianteOfRightVertex); 10 11 /** 12 * Called when the program is launched. 13 */ 14 void initalizeSprites(){ 15 /* type your sprite initialization code here */ 16 17 } 18 19 /** 20 * Called when the program ends. 21 */ 22 void cleanup() { 23 /* type your clean up code here */ 24 25 } 26 27 28 /** 29 * Paint the current frame of the game. 30 * Is called approximately 15 times every second. 31 */ 32 void paintFrame() { 33 /* Screen is cleared */ 34 /* Type your rendering code here */ 35 while (xCoordianteOfLeftVertex >= 0 && xCoordianteOfRightVertex >= 0) 36{ 37 38al_draw_triangle(xCoordianteOfLeftVertex, yCoordianteOfLeftVertex, 39 40xCoordinateOfTopVertex, yCoordianteOfTopVertex, xCoordianteOfRightVertex, yCoordianteOfRightVertex, al_map_rgba_f(0, 0, 0, 1), 3); 41 42 43 44 45 46 xCoordianteOfLeftVertex = xCoordianteOfLeftVertex - 10; 47 yCoordianteOfLeftVertex = yCoordianteOfLeftVertex - 10; 48 xCoordinateOfTopVertex = xCoordinateOfTopVertex - 20; 49 yCoordianteOfTopVertex = yCoordianteOfTopVertex - 20; 50 xCoordianteOfRightVertex = xCoordianteOfRightVertex - 10; 51 yCoordianteOfRightVertex = yCoordianteOfRightVertex - 10; 52 53 54 55 } 56}

Chris Katko
Member #1,881
January 2002
avatar

I think we need more details, or code.

Pyreal said:

I have the basic set up but once I run my program is shows my primitives and then closes.

So it runs, then quits? Do you have any routines to wait for keyboard input after it runs? Are you sure they work? Are you sure you installed the keyboard handler?

What I'm trying to get at is whether it's crashing, or running fine and just exiting.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Pyreal
Member #15,537
March 2014

I'm working within the framework our teacher set up in the intro level C++ class, so I'm not sure i'd be able to answer your questions accurately. the exe is still active but the primitive vanishes. heres a short video.

http://www.youtube.com/watch?v=DoOn2lzjAR0&feature=youtu.be

Edgar Reynaldo
Member #8,592
May 2007
avatar

Pyreal said:

 xCoordianteOfLeftVertex = xCoordianteOfLeftVertex - 10;
 yCoordianteOfLeftVertex = yCoordianteOfLeftVertex - 10;
 xCoordinateOfTopVertex = xCoordinateOfTopVertex - 20;
 yCoordianteOfTopVertex = yCoordianteOfTopVertex - 20;
 xCoordianteOfRightVertex = xCoordianteOfRightVertex - 10;
 yCoordianteOfRightVertex = yCoordianteOfRightVertex - 10;

Those are big ugly variable names. You can avoid all that by using t,l,b,and r for top, left, bottom, and right. But that's not the problem. The problem is that you are moving your entire triangle up and left. That's not what your diagram shows to do. In the diagram, the top vertex moves down by 10 and the bottom left and right vertexes move up by 10. The left vertex moves right, and the right vertex moves left. When they cross, you know you can stop drawing. Similarly, you can stop drawing when the top and bottom vertexes cross.

I would give code, but you need to figure the rest out on your own.

Does paintFrame get called by your teacher's framework? What makes the program stop? Does it wait for user input? Or a close button, or what?

Pyreal
Member #15,537
March 2014

Thanks for the reply, I'll see if I can work it out with your pointers.

paintFrame is called to display objects on the screen. The program continues running until you close it out manually. My issue is my primitives are drawn on screen but quickly disappear once they complete leaving me with a empty screen.

If your interested this is the entire framework

http://pastebin.com/beh26eZU

Edgar Reynaldo
Member #8,592
May 2007
avatar

Pyreal said:

               
class Game {
public:
       
        /* Type your variable declarations here */
        int xCoordinateOfTopVertex = 200;
        int yCoordianteOfTopVertex = 100;
        int xCoordianteOfLeftVertex = 100;
        int yCoordianteOfLeftVertex = 300;
        int xCoordianteOfRightVertex = 300;
        int yCoordianteOfRightVertex = 300;
        //int height = (yCoordianteOfTopVertex + yCoordianteOfLeftVertex + yCoordianteOfRightVertex);
        //int width = (xCoordinateOfTopVertex + xCoordianteOfLeftVertex + xCoordianteOfRightVertex);

First, that won't compile unless those variables are static and const.

You can change them to be static and const, and then use local variables to keep track of your drawing. I wouldn't want them const, but if you teacher doesn't care it doesn't matter. You could also make then non const non static members, which means every instance of a Game would have those variables to access.

Second, the formulas for height and width are way off. The height is the bottom vertex y minus the top vertex y. The width is the right vertex x minus the left vertex x.

Your triangle disappears because you move it off screen to the top left in the code you showed before. Your vertex movement was wrong, and your check condition is incorrect as well. See what I said before about the check conditions.

Also, you need to reset the drawing position of the triangle each time you want to draw it.

SiegeLord
Member #7,827
October 2006
avatar

First, that won't compile unless those variables are static and const.

It will if you set the compiler into C++11 mode, it's a new language feature.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Edgar Reynaldo
Member #8,592
May 2007
avatar

Pyreal
Member #15,537
March 2014

I believe they are set up in the Game.h which is the teachers framework. I think my issue might be using a while loop instead of a do while loop. I'm going to give it a try and see if it still disappears.

Edgar Reynaldo
Member #8,592
May 2007
avatar

No, this is your problem, like I said earlier.

Pyreal said:

 xCoordianteOfLeftVertex = xCoordianteOfLeftVertex - 10;
 yCoordianteOfLeftVertex = yCoordianteOfLeftVertex - 10;
 xCoordinateOfTopVertex = xCoordinateOfTopVertex - 20;
 yCoordianteOfTopVertex = yCoordianteOfTopVertex - 20;
 xCoordianteOfRightVertex = xCoordianteOfRightVertex - 10;
 yCoordianteOfRightVertex = yCoordianteOfRightVertex - 10;

You are moving the bottom left vertex up and left. You are moving the top vertex even farther up and left. And you are moving the right vertex up and left as well. That's not what the algorithm tells you to do.

Pyreal
Member #15,537
March 2014

I've got my code drawing the triangle correctly but once it completes and meets the while loop requirement it the screen clears. Is it my loop?

#SelectExpand
1{ 2 3 while (xCoordianteOfLeftVertex != xCoordianteOfRightVertex) { 4 5 al_draw_triangle(xCoordianteOfLeftVertex, yCoordianteOfLeftVertex, 6 7 xCoordinateOfTopVertex, yCoordianteOfTopVertex, xCoordianteOfRightVertex, yCoordianteOfRightVertex, al_map_rgba_f(0, 0, 0, 1), 1); 8 9 10 11 xCoordianteOfLeftVertex = xCoordianteOfLeftVertex + 10; 12 yCoordianteOfLeftVertex = yCoordianteOfLeftVertex - 10; 13 14 15 yCoordianteOfTopVertex = yCoordianteOfTopVertex + 10; 16 17 xCoordianteOfRightVertex = xCoordianteOfRightVertex - 10; 18 yCoordianteOfRightVertex = yCoordianteOfRightVertex - 10; 19 20 21 } 22 23 } 24 }

Edgar Reynaldo
Member #8,592
May 2007
avatar

You must be clearing again and then flipping the display again after drawing your triangles thats why they appear when you flip the display the first time after drawing them and then disappear. Because you're flipping the display twice.

Pyreal
Member #15,537
March 2014

If anyone is wondering, the issue was the variables had to be within the paint frame and not where the teacher normally initializes variables. So i'm not sure if it was an oversight on my part or the professors.

Edgar Reynaldo
Member #8,592
May 2007
avatar

I told you that was part of the problem earlier. Your triangle moved off screen because the variables weren't local, but global to the class. I told you you weren't re-initializing the values of your variables, leading to the persistent off screen triangle.

Go to: