Can I have some Sample Code? (OpenGL, glDrawPixels();)

This is a discussion on Can I have some Sample Code? (OpenGL, glDrawPixels();) within the Game Programming forums, part of the General Programming Boards category; I'll try to explain it a bit.... Main.cpp is where all the headers come together, there are almost no function ...

  1. #46
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    I'll try to explain it a bit....

    Main.cpp is where all the headers come together, there are almost no function definitions in main, main uses all of the headers and (in theory) is ment to be just one big long line of function calls and such...

    Function.h is where our display lists are held, as well as the glPrint (so the lists can use glprint), also this is where our grid creation class is, as well as the structure matrix that holds all the data for each "seat" (1 per quad)....

    Font, does all the fonts... not much to explain
    Texture, does all texture coding.... not much to explain here either...

    filesave, does all saving and loading of data, took us a while to figure this one out, but it works just perfectly

  2. #47
    ---
    Join Date
    May 2004
    Posts
    1,379
    OK I won't say it but there are a lot of for loops and I can't think of anything else it could be.

  3. #48
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Quote Originally Posted by sand_man
    OK I won't say it but there are a lot of for loops and I can't think of anything else it could be.
    What do you mean?
    Hello, testing testing. Everthing is running perfectly...for now

  4. #49
    ---
    Join Date
    May 2004
    Posts
    1,379
    I meant exactly what I said.
    Quote Originally Posted by Shamino
    don't say its the loops, its not
    So I said,
    Quote Originally Posted by sand_man
    OK I won't say it but there are a lot of for loops and I can't think of anything else it could be.

  5. #50
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Ok.........real quick. Have we not already said take the f....king for loops out of your code. In fact, in your case, forget the C language even allows for loops because you are misusing - emphasis - misusing them.

    I'll explain why your code is going so slow. Let's look at this monster you stuck in your code:

    Code:
    for(l=0; l<3; l++)
    {
      for(j=1; j<=28; j++)
      {
        for(k=1; k<=14; k++)
       {
         if(n[h].is[l][k][j] == 0)
        {
           n[h].s[l][k][j].sold = false;
        } 
        else
        {
          n[h].s[l][k][j].sold = true;
        }
       } 
     }
    }
    There are several things completely wrong about this approach.
    • Each loop is considered an O(n) loop. Each loop will go from start to n - so we count it as O(n). Okay you have this: O(n) * O(n) * O(n) which equals an O(n^3) algorithm. O(n^3) is by far the worst I've ever witnessed. Dump that approach.
    • n[h].s[l][k][j].sold = false; This is a mess. First you have an array called n. Then you have a 3 - count em - 3 dimensional array with each element having properties in itself. Bad idea bud.


    I'll go through this slowly. If you understand anything about memory you will understand why a 3 dimensional array is absolutely horrible. In essence here, you actually have about a 4 dimensional to 5 dimensional array in your code. Okay all an array is, is a big huge collection of numbers, values, strings, objects, etc.

    So this is an array:

    012345678

    Now you can create this same array in 2D:

    012
    345
    678

    If you notice I've created a tic tac toe board. Remember though that even if you declare an array in 2D in C/C++ it is still a linear array in memory.

    This: int array[3][3]
    is this: 012345678

    Okay so if all arrays are linear in the computer memory then how does the C compiler know how to access the value when you specify it's location in 2D? Simple. Depending on how the compiler stores it it usually does:

    element=row*width+column

    So if we declare int array[3][3] - the compiler knows the width of this array is 3 elements. Then when you do:

    array[1][1]=some_value;

    It internally does this: array[1*width+1]=some_value.
    So this means that creating 2D arrays in any compiler or any language inherently FORCES the compiler/interpreter to do an internal multiply transparent to the programmer so that it can correctly access the array in memory. Okay now you have a 3 dimensional array. This means you are forcing at least 2 multiplies with every occurrence of s[l][k][j]. This is a huge mess. Dump it. Now stick this array crap in a loop, and not just a loop, but an O(n^3) loop and you have this to start with.

    num_cycles=(length of l loop)*(length of k loop)*(length of j loop).

    This is bad right now. Without even adding the array crap the absolute BEST this algo can do is num_cycles. Horrible.

    Now let's just add in the 2D array multiply stuff. Remember...you have 3 - but we will just use 2 for the example.

    Total_multiplies=num_cycles*accesses_to_array.

    Ok so let's say l is 1 to 10, k is 1 to 10, and j is 1 to 10.

    We have this: (10)*(10)*(10) or (10^3) or 1000.

    If you access the array only once during the inner loop, you are forcing 1000 multiplies. And this is only ONE - read ONE ONE ONE of your awful loops. If you access the array twice in the inner loop you are forcing 2000 multiplies. Now let's say your arrays are all powers of 2 or somehow the compiler constructs them so they fall on the correct boundaries so that bitshifting can be used to obtain the element index in memory. You are still doing at least 1000 bitshifts for one damn loop. Not good.

    Now you have at least 15 O(n^2) loops and 4 or 5 O(n^3) loops. You have at least 1 array that in itself has a 3D or 4D array in it. You have 1 loop that actually is so huge I can't even break it down. You start with 1 to 3, then you stick about 10 other O(n^2) loops inside of it, then you also have a switch statement inside of the loop, which means it executes about a billion times.

    Overall I would say this is your solution:

    • Start->Programs->Accessories
    • Select Windows Explorer
    • Navigate to your project folder
    • Right click the folder and select Delete.
    • When it asks you if you want to send this to the recycle bin, click yes.
    • Right click the recycle bin and select Empty Recycle Bin.
    • Re-evaluate what you want to do with the code
    • Start from scratch and use better data structures, stay away from O(n^99999999) loops and stay away from 12D arrays.
    • This should fix your problem.


    Your code is far too broken to fix. You must start over in order for us to help you. It's not the video card, it's the programmer.

    I'm not being mean....I'm being realistic. Heed our advice and heed what has been said so far in this thread. Get rid of those friggin for loops for the last time. If you don't want to make an effort and do what we say.......then please stop posting. Otherwise we will be more than glad to help......but not with things we've already covered.

    Sometimes....there are no words.


    It is the loops......It is the loops.........It is the loops.........It is the loops..........It is the loops..........Get rid of them.......Stop arguing about your loops - get rid of them..................

    Stay away from this type of coding:
    Code:
    struct Object
    {
      int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
      int ObjectArray[10][10][10][10];
    }
    
    Object MyObjects[100][100][100];
    
    
    for (int a=0;a<100;a++)
    {
      for (int b=0;b<100;b++)
      {
       for (int c=0;c<100;c++)
       {
         for (int d=0;d<100;d++)
         {
          }
        }
       }
    }
    Last edited by VirtualAce; 10-28-2005 at 12:55 AM.

  6. #51
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Each loop is considered an O(n) loop. Each loop will go from start to n - so we count it as O(n). Okay you have this: O(n) * O(n) * O(n) which equals an O(n^3) algorithm. O(n^3) is by far the worst I've ever witnessed. Dump that approach.
    How is his algorithm considered O(n^3) ? He does 3 * 28 * 14 loop iterations which is the same size as his array. That sounds like O(n) to me.

  7. #52
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Let's say we have 3 loops from 0 to 9.

    Code:
    int value=0;
    
    for (int i=0;i<10;i++)
    {
      for (int j=0;j<10;j++)
      {
        for (int k=0;k<10;k++)
        {
          value++;
        }
      }
    }
    What is value going to be? 1000. 10*10*10 or 10^3. N in this case is 10.

    Code:
    int n=30;
    int value=0;
    
    for (int i=0;i<n;i++)
    {
      for (int j=0;j<n;j++)
      {
        for (int k=0;k<n;k++)
        {
          value++;
        }
      }
    }
    Value will be this: n^3. Now imagine accessing a 2D array inside of the inner loop or even a 3D array. Lots of multiplies - the computer is wasting a lot of time - ie: the programmer is wasting a lot of cycles on trivial stuff.


    Now granted his loops don't all count to the same numbers, but the principle is the same. The loops are killing the code. And all this to draw a grid? A grid can be drawn with two for loops. TWO. One if you do some math.
    Last edited by VirtualAce; 10-28-2005 at 01:04 AM.

  8. #53
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    N in this case is 10.
    N is only 10 in that case because you are arbitrarily making it 10. If the array size is 10, then N would be 10. If the array size is 1000, then N would be 1000. In your case there is no array, so N doesn't exist (note that "array" in this context doesn't have to be an actuall array. Pretty much any data structure that requires iteration could be substituted here). Just because there are 3 nested loops doesn't mean you can automatically assign the algorithm an efficiency of O(n^3). You have to look at the purpose of the loops, and what they are iterating over.

  9. #54
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Oh, and I'm not arguing that the OP's code is correct or anything along those lines. I kind of skipped to the end of this little 4 page discussion, and just read the last few posts.

  10. #55
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Oh, and I'm not arguing that the OP's code is correct...
    And that would be a good thing. But regardless of the actual efficiency it doesn't take a rocket scientist to figure out that it's not very efficient.

    Shamino do me a favor and at least try this: I don't know GL so I'm taking some of your code hoping it works.

    Code:
    void DrawGrid(int cellsize,float left,float top,float right,float bottom)
    {
      if ((right-left)<=cellsize) return;
    
      glVertex2d(left, top);
      glVertex2d(right, top);
    
      glVertex2d(right, top);
      glVertex2d(right, bottom);
    
      glVertex2d(right, bottom);
      glVertex2d(left, bottom);
    
      glVertex2d(left, bottom);
      glVertex2d(left, top);
    
      float midx=(left+right)*0.5f;
      float midy=(top+bottom)*0.5f;
    
      DrawGrid(cellsize,left,top,midx,midy);
      DrawGrid(cellsize,midx,top,right,midy);
      DrawGrid(cellsize,left,midy,midx,bottom);
      DrawGrid(cellsize,midx,midy,right,bottom);
    }
    Put that in a render loop and in between your glBegin() and glEnd() function calls and see what happens. Just try it.

    Code:
    void Render(float timeDelta)
    {
      glBegin();
    
      DrawGrid(GRID_CELLSIZE,0,0,GRID_WIDTH,GRID_HEIGHT);
    
      glEnd();
    }
    Now just insert the correct values for cellsze, width, and height and watch. This function only works for grids starting at 0,0 - but you can modify it to create a grid anywhere on screen.
    You can also stick these quads into a vertex buffer with some more code.

    And I forgot about the multiplies you are incurring when you call glTranslatef() inside of your horrendous loops.
    Last edited by VirtualAce; 10-28-2005 at 01:40 AM.

  11. #56
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    So buba, if the case is the loop, do you think if i take out the 4D array and do it step by step, it will stop multiply some of the value. Like this:

    Code:
      for(j=1; j<=28; j++)
      {
        for(k=1; k<=14; k++)
       {
          if(n[0].is[1][k][j] == 1)
         {
           n[0].s[1][k][j].sold = false;
           } 
        else
        {
          n[0].s[1][k][j].sold = true;
        }
     
     }
    }
    so the second loop will be the n[1].s[1][k][j], and so on. It's kinda long but it did not make a big array
    Hello, testing testing. Everthing is running perfectly...for now

  12. #57
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Ok, i did it, i separated the loop so it's now not gonna be a big giant loop
    Code:
    				//---------The first night sections-------------------------//
    			if(n[0].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[0].s[0][k][j].active)					
    
    						if( (myAbsd(n[0].s[0][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[0].s[0][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[0].is[0][k][j] == 0)
    							{
    								n[0].is[0][k][j] = 1;
    								n[0].s[0][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[0].is[0][k][j] = 0;
    								n[0].s[0][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    
    				if(n[0].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[0].s[1][k][j].active)					
    
    						if( (myAbsd(n[0].s[1][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[0].s[1][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[0].is[1][k][j] == 0)
    							{
    								n[0].is[1][k][j] = 1;
    								n[0].s[1][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[0].is[1][k][j] = 0;
    								n[0].s[1][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    				if(n[0].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[0].s[2][k][j].active)					
    
    						if( (myAbsd(n[0].s[2][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[0].s[2][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[0].is[2][k][j] == 0)
    							{
    								n[0].is[2][k][j] = 1;
    								n[0].s[2][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[0].is[2][k][j] = 0;
    								n[0].s[2][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    
    				//------------------------------------------------------------------
    				//==================================================================
    
    				//-----The second night sections-----------------------------------
    				if(n[1].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[1].s[0][k][j].active)					
    
    						if( (myAbsd(n[1].s[0][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[1].s[0][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[1].is[0][k][j] == 0)
    							{
    								n[1].is[0][k][j] = 1;
    								n[1].s[0][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[1].is[0][k][j] = 0;
    								n[1].s[0][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    
    				if(n[1].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[1].s[1][k][j].active)					
    
    						if( (myAbsd(n[1].s[1][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[1].s[1][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[1].is[1][k][j] == 0)
    							{
    								n[1].is[1][k][j] = 1;
    								n[1].s[1][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[1].is[1][k][j] = 0;
    								n[1].s[1][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    
    				if(n[1].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[1].s[2][k][j].active)					
    
    						if( (myAbsd(n[1].s[2][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[1].s[2][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[1].is[2][k][j] == 0)
    							{
    								n[1].is[2][k][j] = 1;
    								n[1].s[2][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[1].is[2][k][j] = 0;
    								n[1].s[2][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    				//-----------------------------------------------------------------
    				//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    
    				//--------The third night sections----------------------------------
    
    				if(n[2].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[2].s[0][k][j].active)					
    
    						if( (myAbsd(n[2].s[0][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[2].s[0][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[2].is[0][k][j] == 0)
    							{
    								n[2].is[0][k][j] = 1;
    								n[2].s[0][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[2].is[0][k][j] = 0;
    								n[2].s[0][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    
    				if(n[2].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[2].s[1][k][j].active)					
    
    						if( (myAbsd(n[2].s[1][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[2].s[1][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[2].is[1][k][j] == 0)
    							{
    								n[2].is[1][k][j] = 1;
    								n[2].s[1][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[2].is[1][k][j] = 0;
    								n[2].s[1][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    				if(n[2].Active)
    				for(j=1;j<=28;j++)
    				{
    					for(k=1;k<=14;k++)
    					{
    							if(n[2].s[2][k][j].active)					
    
    						if( (myAbsd(n[2].s[2][k][j].x  - mouse.x) < 10.0) &&		
    							(myAbsd(n[2].s[2][k][j].y - mouse.y) < 10.0))
    						{					
    							if(n[2].is[2][k][j] == 0)
    							{
    								n[2].is[2][k][j] = 1;
    								n[2].s[2][k][j].sold = true;
    								tic+= 1;
    								price += Tprice;		
    							}else
    							{
    								n[2].is[2][k][j] = 0;
    								n[2].s[2][k][j].sold = false;
    
    									if(tic > 0)
    									{
    										tic-=1;
    										price -= Tprice;
    									}else{
    										totaltic -=1;
    										totalprice -= Tprice;
    										}							
    									}
    								}					
    							}
    						}
    
    				//-------------------------------------------------------------------
    				//==================================================================
    So what do you think bubba? Will it gonna do some huge multiplications as you've said before.

  13. #58
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    You might consider running a profile of your program, provided you are using MSVC. You can find the option under the Build->Profile... menu.

    If there is not profiling option in your debugger, you could setup something simple to time everything and dump it all to a text file, through that would be more trouble.

    Profiling is a process where every function is timed, so you can see where your program is working the hardest. And it gives you places where you can concentrate on optimization.

  14. #59
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    Hmmm, Big arrays are okay... looping through them to manipulate them, bad....

    Lets see, if we instead of making arrays, what other types of data could we use? I can't even begin to wonder :\

    This is where my C++ skills start to become lacking, how do you declare large amounts of the same thing without making an array out of it....

    hmm, i'll figure this oen out

  15. #60
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    technically hdragon I think that is the exact same thing only with longer code :d

    and bubba, i've taken that code and used it already, it runs perfect, the key issue is that i cant turn a certain area of pixels (20x20) a different color, unless some kind of quad is there or polygon...
    Last edited by Shamino; 10-28-2005 at 07:29 PM.

Page 4 of 7 FirstFirst 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SMS sample code
    By hagenuk in forum C Programming
    Replies: 1
    Last Post: 05-30-2008, 12:47 PM
  2. Sample code please help!!!
    By aewing30 in forum C Programming
    Replies: 6
    Last Post: 05-29-2008, 11:51 AM
  3. The code sample of classes in the tutorial. :o
    By Wall in forum C++ Programming
    Replies: 3
    Last Post: 08-20-2004, 10:18 AM
  4. looking for book with sample code
    By stella in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2003, 11:48 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21