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; Develop a class and use that to maintain the grid. I don't know what your implementation is or what you ...

  1. #61
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Develop a class and use that to maintain the grid.
    I don't know what your implementation is or what you are trying to do so forgive me if the code is not what you need. Let's say you want a grid and you need each cell to turn a color or something. You can still use the recursive method I showed you, but instead of drawing vertices to the screen, you simply stick them in a vertex buffer or some type of array to contain the data. Notice that you still come out with the same result, but the generation code is about 50 times faster. It probably only took seconds to draw the grid using the code I gave you.

    And to access a linear array in 2D you can use row*width+column. So if you know the width of your grid and one other variable you can find the offset into the array. If your vertex buffer is setup exactly as the grid appears then you can change the colors of the vertices at any point in the grid to create colored squares.

    Write a class that controls the grid display.

  2. #62
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    you're right bubba, i just don't want to end up learning how to use the Vertex buffer objects just so i can do such a simple task...

    do you think if i were to create a quad thru a display list, just like i do already only, instead of using that quad to loop a grid, just make an if statement, if (mouseclick detection here) drawquad... (color red)... done................

    vertex buffers ultimately create polygons, but in a super fast way correct?

  3. #63
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Let's start over. Tell me exactly what you want your program to do. Then I can help you much more. And hell since I fragged my MFC project...I got plenty of time.

    Damn thing.

    EDIT: Fixed the MFC thingy. My advice: Don't use pre-compiled headers in MSVC........ever.
    Last edited by VirtualAce; 10-29-2005 at 06:42 AM.

  4. #64
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    The thing is bubba, when we draw the grids, we can't apply texture to them. I know it's faster and i've seen it, but it is hard to make it like a filled quad with texture.

    This is how our program works.
    s[3][k][j] is the 3 sections of the auditorium. Since we can't draw 3 sections at the same time, so we divided in 3 and draw out at three different times. Same thing with the n[3].
    Ok, so when the users want to buy a ticket to that night show, they can chose the night (n[3]), the sections (s[3][k][j]), which mean they can choose where they want to sit in the auditorium
    After the user click one quad, it'll turn color, then the tickets and price begin calculate.

    That is basically how our program works, bugs are free, everything is perfect except the speed (not that slow) when we run it in our school's computers. (I estimated about 5 to 10 FPS)
    Last edited by hdragon; 10-29-2005 at 03:59 PM.
    Hello, testing testing. Everthing is running perfectly...for now

  5. #65
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Okay so you have an auditorium with 3 tiers and seats on all tiers. Here is my advice.

    Look at the problem from an object oriented perspective. Each tier of seats is akin to what type of data structure in programming? An array of course. So that means this:


    Auditorium
    ..Tier 0
    ....Seats
    ..Tier 1
    ....Seats
    ..Tier 2
    ....Seats

    Regardless of day or night this is what the auditorium is composed of. Okay so break those down into data structures.

    We've already said that one tier of seats is akin to an array. I prefer to use a 1D array and access it in 2D.

    So make a class that wraps an array. A vector is not needed here because you will never be adding seats. A simple array is all that is needed.

    Ok, now there are several approaches to getting the day and night working. My personal preference would be to make each seat an object. Now each seat will also have a Customer state block which identified the customer using the seat. So create your base object for the entire system. We don't need a lot of special functions and stuff for seats....so we use a struct.

    Code:
    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) {}
    };
    Okay so each seat now has day and night customers attached to it. If no one has the seat we will set Customer.Name to EMPTY. This way we know that the seat is not currently occupied.

    So we have our customer object and seat object and now we need our collection of seats or array. We can now start to build the auditorium so to speak. But we need to build a class to handle a section of seats.

    Code:
    ..
    
    class CSeatSection
    {
      Seat *m_pSeatArray;
      int m_iWidth;
      int m_iHeight;
      int m_iSize;
      public:
       CSeatSection(void):m_pSeatArray(NULL),m_iWidth(0),m_iHeight(0) {}
       virtual ~CSeatSection(void)
       {
         if (SeatArray)
         {
            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;
              
              //Init seats row and column number
              int row=0,col=0;
              
              for (int offset=0;offset<m_iSize;offset++)
              {
                m_pSeatArray[i].Row=row;
                m_pSeatArray[i].Col=col;
                col++;
                if (col>m_iWidth)
                {
                   col=0;
                   row++;
                }
               }
               return false;
            } else return true;       //error
          } 
              
           
         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;      //error
          } 
    
          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;    //error
           }
    };
    Ok now we have a class that controls an entire seating section. Feel free to add functionality as you see fit.

    So we have this:

    CSeatSection
    ....SeatArray
    ........Seat
    ............Customer

    Each Seat has a day and night customer.
    Each CSeatSection is a collection of these seats.

    When you break it into objects it gets much easier.

    Ok now we are ready for the auditorium.

    Code:
    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,CSeat *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,CSeat *inSeat)
         {
           if (ID<SeatSections.size())
           {
             bool failed=SeatSections[ID]->SetSeatInfo(Row,Col,inSeat);
             if (failed) 
             {
                return true;
             } else return false;
           }
            return true;
          }
    };
    Now you have a good beginning to a better data structure. I'm sure there are some pointer errors here and there and typos but you should be able to get it working.

    Here is how to use it.

    1. Create an auditorium object
    2. Call CreateSeatSection with desired width and height of section.
    3. Save the DWORD ID it returns for later use.

    To set seat information
    Call CAuditorium::SetSeatInfo() and pass the ID of the section (returned to you when you created the section), the row and column of the seat, and a Seat pointer that will contain the returned information.

    To get seat information
    Call CAuditorium::GetSeatInfo() and pass the ID of the section, the row and column of the seat, and a Seat pointer that points to the Seat information for the seat.

    Both of these return non-zero if they fail, and zero if they succeed.

    Now you need to add more functions to these to do what you want, but it's a good start.

    All seat information should go through CAuditorium since it manages the entire auditorium. Add functions to CAuditorium to control each seat section - the seat section class then will control each seat it contains. Add functions to each seat so each seat can then control the customer information. Add functions to Customer so you can set the fields, get the fields, etc in the Customer object.

    So the final auditorium is a vector of CSeatSections which are static arrays of Seats with Customer(s).

    Now for the graphics, just call GetSeatInfo or some other function to find out whether or not it is occupied or not and if it's a day or night customer. Each seat now when you click on it could show its customer information. You are well on your way to having quite a robust scheduling, seating, and pricing program to track customer activity for any auditorium you may have.
    Last edited by VirtualAce; 10-29-2005 at 05:40 PM.

  6. #66
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    The code is incredible but i get these error:
    Code:
    c:\documents and settings\preferred user\my documents\lesson01\seats.h(35) : error C2601: 'Create' : local function definitions are illegal
    c:\documents and settings\preferred user\my documents\lesson01\seats.h(64) : error C2601: 'GetSeatInfo' : local function definitions are illegal
    c:\documents and settings\preferred user\my documents\lesson01\seats.h(76) : error C2601: 'SetSeatInfo' : local function definitions are illegal
    Error executing cl.exe.
    In theory, the bool functions are perfectly fine but..., you got any ideas?
    Hello, testing testing. Everthing is running perfectly...for now

  7. #67
    ---
    Join Date
    May 2004
    Posts
    1,379
    try defining the functions in a .cpp file

  8. #68
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Somewhere you are missing a semicolon. Those functions are inlined and often I get that error if a semicolon is missing. Check after each function to make sure it has a matching closing brace. Obviously one is missing and then the definition for a new function is encountered which flags the error because C++ does not allow local functions - that is, you cannot have function definitions within function definitions.
    Last edited by VirtualAce; 10-30-2005 at 01:37 AM.

  9. #69
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    I've checked many times but can't seem to find any missing semi-col. I think the problem is around:
    Code:
    virtual ~CSeatSection(void)
    {
    .......
    }
    are the bool functions inside the ~CSeatSection(void)? Because when i separate them, the errors gone.

    to Sand_man:
    I defined it in a CPP file but still get the same errors
    Hello, testing testing. Everthing is running perfectly...for now

  10. #70
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    No the functions are self contained like this:

    Code:
    class MyClass
    {
      public:
         //Constructor
         MyClass(void)
         {
            //code here
         }
    
         //Destructor
         virtual ~MyClass(void)
         {
           //code here
         }
        
          //Member function
          bool SomeFunc(void)
          {
            //code here
          }
    };       //end of class definition
    The only diff is I've put the body of the function in the class definition. Depending on your compiler setup this should auto-inline the function, but not always so check your options.

    virtual ~CSeatSection should have an opening brace and a closing brace. That is the body of the destructor code.

  11. #71
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    So i adjusted some braces:
    Code:
    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;
    		}
    	
    	
    };
    The errors about the bool functions are gone, then one came up:
    Code:
    error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct Seat *' (or there is no acceptable conversion)
    Hello, testing testing. Everthing is running perfectly...for now

  12. #72
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    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;
    		}
    There is a pointer error here. I leave it to you to fix.

    Options:
    1. m_pSeatArray[offset]=*inSeat;
    2. m_pSeatArray[offset]=&inSeat;

  13. #73
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    This worked:
    Code:
    m_pSeatArray[offset]=*inSeat;
    Then i proceed to the CAuditorium class and i get errors about std::vector<>

    Here are the errors:
    Code:
    'vector' : is not a member of 'std'
    error C2143: syntax error : missing ';' before '<'
     error C2501: 'vector' : missing storage-class or type specifiers
     error C2059: syntax error : '<'
    error C2238: unexpected token(s) preceding ';'
    I'd included <string> and set 'using namespace std' in the file already. I just don't understand why the program doesn't recognized the 'vector'.
    Hello, testing testing. Everthing is running perfectly...for now

  14. #74
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Code:
    #include <vector>
    
    using namespace std;
    If you don't include vector it cannot find it. Even though MSVC still shows it in the little popup thingy when you write the function it still does not actually know where to find the vector template class.

    The MSVC IDE drop down boxes that show the functions can be deceiving. Just because they show you what the function is, does NOT mean you don't have to include the header, and also does not mean you won't have to link with another library.

    I don't use using namespace std because using std::<vector> xxxx really makes the code stand out for me. I also normally stick a v in front of all Vector objects so I know they are vectors. I'm sure coding in Hungary is much more difficult....but I'm not in Hungary or anywhere near it. Use whatever coding convention is good for you.

  15. #75
    Computer guy
    Join Date
    Sep 2005
    Location
    I'm lost!!!
    Posts
    200
    well, so <vector> solves the problem, but then 2 last problem(i think) came up:
    Code:
    error C2664: 'GetSeatInfo' : cannot convert parameter 3 from 'class CSeatSection *' to 'struct Seat *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    error C2664: 'SetSeatInfo' : cannot convert parameter 3 from 'class CSeatSection *' to 'struct Seat *'
            Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    I just quite not understand right there. The code is:
    Code:
    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, 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;
    	}
    
    	bool SetSeatInfo(DWORD ID, int Row, int Col,CSeatSection *inSeat)
    	{
    		if(ID < SeatSections.size())
    		{
    			bool failed=SeatSections[ID]->SetSeatInfo(Row,Col,inSeat);
    			if(failed)
    			{
    				return true;
    			}else return false;
    		}
    		return true;
    	}
    
    };
    What do you think?
    Hello, testing testing. Everthing is running perfectly...for now

Page 5 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