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

  1. #76
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    teehee i have too much to do during the weekends to sit here and code, hdragon will teach me all of it on monday :d

  2. #77
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    Actually we didn't do anything much, just try with some of the code to create the seats. It's kinda confusing but i think i'll get it.
    Hello, testing testing. Everthing is running perfectly...for now

  3. #78
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Oh,Shamino, and I have nothing better to do than help you on my weekend. HDragon I cannot solve every error for you. Learn the code and fix the problems.

  4. #79

    Join Date
    May 2005
    Posts
    1,042
    Bubba has gone very far out of his way to help you. Just keep that in mind (and recognize his effort).
    I'm not immature, I'm refined in the opposite direction.

  5. #80
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I think the underlying structure of this is quite simple to understand. The class hiearchy is completely flat and only consists of two major classes. I chose this because:

    1. A customer is not a seat. (essentially)
    2. A seat is not a seat section.
    3. A seat section is not an auditorium.

    So:

    1. A seat has a customer.
    2. A seat section has seats
    3. An auditorium is a collection of seat sections, with seats that have customers.

    When you fix the pointer errors you should be ready to use it. Notice the constructor cannot fail. That is if you instantiate the class globally, it is guaranteed never to fail because the constructor does not allocate memory or perform any other action that could assert or throw an exception. This does necessitate the need for a Create() function which is similar to most people's init. I choose to use Create() even though technically the object was already created at instantiation. However the object has no use until you call Create() which really gets the ball rolling. Change it to Init() if you want and return a boolean value to indicate success or failure.

    The only leak that might happen is in the destructor code for the CAuditorium. When a empty() is called on a vector it "should" iterate through the vector and call the destructor for each object in the vector. The destructor for CSeatSection then de-allocates the array and sets it to NULL. Use the debugger to ensure this is what is happening. Notice that upon creation of the object in Create() a temporary is used and then pushed to the vector.

    Use the debugger to obtain the addresses of the pointer and the object in the vector.
    Just because you pushed the object into the vector does not mean that temp does not point to that same object. If you delete temp, then you delete the object in the vector. The empty() function for the vector will call the destructor for the CSeatSection and thus the memory will be de-allocated that way.

    Perhaps you should read a book on STL to understand what is taking place. There is no copy constructor for CSeatSection but it really needs one since it does allocate memory. The copy constructor should allocate another block of memory so that the object and a copy of the object do not point to the same memory. Without it, if the copy goes out of scope and it's memory is de-allocated, then the memory in the other object has been de-allocated since they both are using the same block of memory but the object has not gone out of scope. Future references and access to memory in this object will cause an exception and a nice message from Windows. A copy constructor which allocates another block of memory for copies of the object would alleviate this problem. Without it, both objects use the same block.

    Since constructors cannot return values there isn't a really good way to test to see if the copy constructor failed or not unless you implement an exception class. Attempt to allocate the memory and throw an exception if it fails. This will cause the program to exit, but it's better than nothing. And it really should terminate since the object cannot be created.

    The copy constructor for CSeatSection would take this form:

    CSeatSection(const CSeatSection &section);
    Last edited by VirtualAce; 10-31-2005 at 04:18 AM.

  6. #81
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    What we plan to do is this.....

    Loops aren't a big deal when you aren't dealing with arrays/matrices, correct? loops are quick quick quick!

    BUT, when you have a three dimensional array, it has to perform hundreds of multiplication operations.....

    The idea is, to eliminate the dimensions of our array

    so instead of looping a [3][14][28], we can do a a[14][28], a b[14][28], and a c[14][28], we could even take it a step further and give it 1 dimension, by making 14 variables.. course this is probably a bit over the top, but hey, it wouldn't lag, i don't think...

  7. #82
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I don't think I can help you any more with this whole thing. If you cannot understand the code, data structure, or remove the errors from it perhaps you've bitten off more than you can chew at this time in your programming adventure.

    I would suggest doing some research, learning more, and then attempt this again.

    I will not post oodles of code just so you can go back to what you had. Your data structures and approach to this problem are lacking. It's a simple problem with a simple solution and yet you insist on using something else. Mine is not the only solution but what do you have to lose? Yours obviously isn't either.

    Only a fool continues walking down a path even though he's been forewarned it's a dead end.
    Last edited by VirtualAce; 10-31-2005 at 01:37 PM.

  8. #83
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    I just don't understand right here
    Code:
    bool GetSeatInfo(DWORD ID, int Row, int Col, CSeatSection *outSeat)
    {
    	if(ID<SeatSections.size())
    	{
    		bool failed=SeatSections[ID]->GetSeatInfo(Row,Col,outSeat);
    		if(failed)
    		{
    			outSeat=NULL;
    			return true;
    		}else return false;
    	}
    
    	outSeat=NULL;
    	return true;
    }
    It bugged me from last night till now. So you set the bool value equal to the SeatSections[ID] which will get information from GetSeatInfo(Row, Col, outSeat) from the CSeatSection class.

    So it only gets data, not convert, but my errors said that it can't convert parameter 3 from 'class CSeatSection *' to 'struct Seat *'.

    I know i kinda slow and not very bright but these kind of format kinda new to me. Please help me.
    Hello, testing testing. Everthing is running perfectly...for now

  9. #84
    ---
    Join Date
    May 2004
    Posts
    1,379
    I'm not sure why you are getting that error, but something I did notice is that it is being passed 3 params to a method that requires 4

  10. #85
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    So it only gets data, not convert, but my errors said that it can't convert parameter 3 from 'class CSeatSection *' to 'struct Seat *'.
    It is a pointer problem. I found the error. The function is trying to retrieve a seat, not a seat section. So the compiler cannot convert from struct seat to class CSeatSection.

    Code:
    bool GetSeatInfo(DWORD ID, int Row, int Col, CSeatSection *outSeat)
    {
    	if(ID<SeatSections.size())
    	{
    		bool failed=SeatSections[ID]->GetSeatInfo(Row,Col,outSeat);
    		if(failed)
    		{
    			outSeat=NULL;
    			return true;
    		}else return false;
    	}
    
    	outSeat=NULL;
    	return true;
    }
    Code:
    bool GetSeatInfo(int row, int col, Seat *outSeat)
    		{
    			int offset=row*m_iWidth+col;
    			if(offset<m_iSize)
    			{
    				outSeat=&m_pSeatArray[offset];
    				return false;
    			}
    			outSeat = NULL;
    			return true;
    		}
    See. The prototype for CSeatSection::GetSeat requires a pointer to a seat, not a SeatSection. Seat sections are indentified by ID's. My bad. The corrected function in CAuditorium should be this.

    Code:
    bool GetSeatInfo(DWORD ID, int Row, int Col, Seat *outSeat)
    {
      if(ID<SeatSections.size())
      {
        bool failed=SeatSections[ID]->GetSeatInfo(Row,Col,outSeat);
        if(failed)
       {
          outSeat=NULL;
          return true;
       }else return false;
      }
    
      outSeat=NULL;
      return true;
    }
    This is not the best way to do things. But it does provide structure to the code. Repeating functions in the vector manager class that are already in the CSeatSection class is not the best so feel free to alter it at your leisure.

  11. #86
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Thanks very much for your time Bubba, it is more appreciated than you think, you are turning this project, from simply a project we have to do to pass this class, into much much more, a learning experience.

    A learning experience is as valuable as gold to hdragon and I and we do not take your help for granted, again, thank you for your time

  12. #87
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I am finally beginning to understand your code Bubba, and, well, I feel dumb.....


    All this time we havn't been approaching the problem in an object oriented way

    An auditorium is an object, that can hold sections of seats, seats are an object, which can hold customers....

    object object object, all working together, instead of one big object....

    object orientated programming....

    Obviously something you can't learn by just learning syntax, sure the tools are there (syntax), but do you know how to use them?

    Workin on it Bubba, gimme a few days and I'll get back to you

  13. #88
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well if you put forth effort and listen to people who do know a bit more - simply because they've been right where you are at one time in their programming experience - then we have no problem at all helping others.

    I love to help with problems as long as the OP demonstrates effort on his/her own. You've done that and even though the code you posted had some hideous loops it did show that you tried to solve the problem.

    I don't know everything there is to know about computers or programming or C/C++ but what I do know I freely share because I think that is the only way to grow the trade and produce better programs. And that, in a nutshell, is what this board is all about. Information sharing and programmers helping other programmers code with excellence and quality. We are a testy bunch but that's because we've answered some of these questions about 1000 times so bear with our grumpiness.

    That code should do what you want. Now all you need is the display portion. The display portion can be created by creating a vertex buffer using the quadtree recursion I showed you. Instead of writing vertexes to the screen, save them in an array. So the x,y,x2, and y2 become the 4 vertices of the quad.

    Array[x][y]=Vertex(x,y,1.0f);
    Array[x2][y]=Vertex(x2,y,1.0f);
    Array[x][y2]=Vertex(x,y2,1.0f);
    Array[x2][y2]=Vertex(x2,y2,1.0f);

    Note that your array must be a power of 2 plus 1 to ensure you have enough storage in the array for the number of recursions. Do a search on google under terrain generation algorithms. There is a page which describes the quad-tree approach to generating a grid.

    You might want to change the algo to operate on recursion depth instead of cellsize.

  14. #89
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I'll try to learn vertex buffers and how to implement them even with our crappy school integrated video cards, currently the NeHe tut on VBO's doesn't work because our video card doesn't support ARB extensions... Crap!

    If you havn't noticed, hdragon is more of the coder, and I'm more of a designer at heart.....

    hdragon coded more than half the project, and most of the ideas on how to do it came from me (some of which are obviously hideous and un object oriented, but I'm learning)

    I think honestly, for this project, due to time limits, we might end up taking the easy way out, but we WILL rewrite this program the RIGHT way....

  15. #90
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Here it is, error free..

    Code:
    #include <windows.h>											// Header File For Windows
    #include <gl\gl.h>												// Header File For The OpenGL32 Library
    #include <gl\glu.h>												// Header File For The GLu32 Library
    #include <gl\glaux.h>											// Header File For The GLaux Library
    #include "NeHeGL.h"												// Header File For NeHeGL
    #include <vector>
    
    using namespace std;
    #pragma comment( lib, "opengl32.lib" )							// Search For OpenGL32.lib While Linking
    #pragma comment( lib, "glu32.lib" )								// Search For GLu32.lib While Linking
    #pragma comment( lib, "glaux.lib" )								// Search For GLaux.lib While Linking
    
    struct Customer
    {
      char Name[40];
      char Address[40];
      char Age[3];
      char TimeOfPurchase[5];
    }; 
    
    
    struct Seat
    {
      //Customers for this seat
      Customer  DayCustomer;
      Customer  NightCustomer;
      int Row;
      int Column;
      float Price;
      Seat(void):Row(0),Column(0),Price(0.0f) {}
    };
    
    class CSeatSection
    {
    	Seat *m_pSeatArray;
    	int m_iWidth, m_iHeight, m_iSize;
    
    public:
    
    	CSeatSection(void):m_pSeatArray(NULL), m_iWidth(0), m_iHeight(0){}
    
    
    	virtual ~CSeatSection(void)
    	{
    
    		if(m_pSeatArray)
    		{
    			delete [] m_pSeatArray;
    			m_pSeatArray = NULL;
    		}
    	}
    
    		bool Create(int Width, int Height)
    		{
    			m_pSeatArray = new Seat[Width * Height];
    
    			if(m_pSeatArray)
    			{
    				m_iWidth = Width;
    				m_iHeight = Height;
    				m_iSize = Width * Height;
    
    				int row=0, col=0;
    
    				for(int offset=0; offset < m_iSize; offset++)
    				{
    					m_pSeatArray[offset].Row = row;
    					m_pSeatArray[offset].Column = col;
    					col++;
    					if(col > m_iWidth)
    					{
    						col = 0;
    						row++;
    					}
    				}
    				return false;
    			}else return true;
    
    			}
    
    		bool GetSeatInfo(int row, int col, Seat *outSeat)
    		{
    			int offset=row*m_iWidth+col;
    			if(offset<m_iSize)
    			{
    				outSeat=&m_pSeatArray[offset];
    				return false;
    			}
    			outSeat = NULL;
    			return true;
    		}
    
    		bool SetSeatInfo(int row, int col, Seat *inSeat)
    		{
    			int offset=row*m_iWidth+col;
    			if(offset<m_iSize)
    			{
    				m_pSeatArray[offset]=*inSeat;
    				return false;
    			}
    			return true;
    		}
    	
    	
    };
    
    
    class CAuditorium
    {
       std::vector<CSeatSection *> SeatSections;
    
       public:
         virtual ~CAuditorium(void)
         {
            SeatSections.empty();
         }
    
         DWORD CreateSeatSection(int Width,int Height)
         {
            CSeatSection *temp=new CSeatSection();
            temp->Create(Width,Height);
            SeatSections.push_back(temp);
    
            return SeatSections.size()-1;
         }
    
    	bool GetSeatInfo(DWORD ID, int Row, int Col, Seat *outSeat)
    	{
    	if(ID<SeatSections.size())
    	{
    	  bool failed=SeatSections[ID]->GetSeatInfo(Row,Col,outSeat);
    	  if(failed)
    	 {
    	    outSeat=NULL;
    	    return true;
    	  }else return false;
    	 }
    
    	 outSeat=NULL;
    	 return true;
    	}
    
         bool SetSeatInfo(DWORD ID,int Row,int Col, Seat *inSeat)
         {
           if (ID<SeatSections.size())
           {
             bool failed=SeatSections[ID]->SetSeatInfo(Row,Col,inSeat);
             if (failed) 
             {
                return true;
             } else return false;
           }
            return true;
          }
    };
    In true, object oriented, C++ programming

    Funny how you can debug a program without fully understanding it...
    Last edited by Shamino; 11-01-2005 at 04:25 AM.

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, 11:47 AM
  2. Sample code please help!!!
    By aewing30 in forum C Programming
    Replies: 6
    Last Post: 05-29-2008, 10: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, 09:18 AM
  4. looking for book with sample code
    By stella in forum C++ Programming
    Replies: 5
    Last Post: 06-25-2003, 10:48 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM