Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem with creating a class for drawing....


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 15 January 2013 - 03:43 PM

class QuadDrawing {
private:
    float QuadoffX;
    float QuadoffY;
    

public:
    QuadDrawing(float, float);
    void Draw();
};

QuadDrawing::QuadDrawing( float Xoff, float Yoff)
{
    Xoff = QuadoffX;
    Yoff = QuadoffY;
}

void QuadDrawing::Draw()
{
    glPushMatrix();
    glTranslatef(QuadOffX,QuadoffY,0.0f);
    glBegin(GL_QUADS);
    //Set color
    glColor4f(1.0,0.0,0.0,0.0);

    

    glVertex2f(90.0,50);
    glVertex2f(90.0,-80);
    glVertex2f(0.0,-80);
    glVertex2f(0.0,50);
    

    glEnd();
    
    glPopMatrix();

}

int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //The frame rate regulator
    Timer fps;
    Drawing Square1(300.0, 300.0);
    int x = 0, y = 0;
    //Wait for user exit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();
        
        //While there are events to handle
        while( SDL_PollEvent( &event ) )
        {
            if( event.type == SDL_QUIT )
            {
                quit = true;
            }
            
            else if( event.type == SDL_KEYDOWN )
            {
                //Handle keypress with current mouse position
                
                SDL_GetMouseState( &x, &y );

                handleKeys (event.key.keysym.unicode. x. y);
                
                }
        }

    MenuState();
    Square1.Draw();
    
    update();
        
       
        
        
        
        
        
        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }

    //Clean up
    clean_up();

    return 0;
}

 

The problem is I want to draw a square to test the class I made which should just take two floating point numbers and draw a square at those points, but the square is never drawn...instead the window comes up but stays completely blank. The window was even clear (no clear color) so I thought that was the problem and added glClear( GL_COLOR_BUFFER_BIT ); to the Draw function in the class making it...

 

void QuadDrawing::Draw()
{

   glClear( GL_COLOR_BUFFER_BIT);


    glPushMatrix();
    glTranslatef(QuadOffX,QuadoffY,0.0f);
    glBegin(GL_QUADS);
    //Set color
    glColor4f(1.0,0.0,0.0,0.0);

    

    glVertex2f(90.0,50);
    glVertex2f(90.0,-80);
    glVertex2f(0.0,-80);
    glVertex2f(0.0,50);
    

    glEnd();
    
    glPopMatrix();

}

 

Still didn't work and the window come up clear/blank (not even the clear color).

 

 

But the thing is if I just make it a function and not a class and pass the floating X/Y to draw the square there it works fine. So its a problem with the class.


Edited by GDsnakes, 15 January 2013 - 06:19 PM.


Sponsor:

#2 BaneTrapper   Members   -  Reputation: 1152

Like
0Likes
Like

Posted 15 January 2013 - 05:58 PM

Put your code inside of "(code) my code goes here (/code)" swap () with [], its a hell to read it.

 

Good luck, cant help.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#3 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 15 January 2013 - 07:41 PM

While reading this keep in mind I have little experience using openGL.

 

The code bellow was taken from http://stackoverflow.com/questions/5877728/want-an-opengl-2d-example-vc-draw-a-rectangle. However yours is very similar, so it should work. Try making the draw function static. If it works then that means you need to use callbacks for drawing from within classes, or have drawing done statically, by the way static is never recommended in C++. If it doesn't work then I'm stumped, it might just be some little bit that is out of order, I wish I knew openGL better.

glPushMatrix();  //Make sure our transformations don't affect any other transformations in other code
    glTranslateF(pRect->x, pRect->y);  //Translate rectangle to its assigned x and y position
    //Put other transformations here
    glBegin(GL_QUADS);   //We want to draw a quad, i.e. shape with four sides
      glColor3F(1, 0, 0); //Set the colour to red 
      glVertex2F(0, 0);            //Draw the four corners of the rectangle
      glVertex2F(0, pRect->h);
      glVertex2F(pRect->w, pRect->h);
      glVertex2F(pRect->w, 0);       
    glEnd();
  glPopMatrix();

 

P.S. The functions gluOrtho2D and glFlush might need to be used. Lastly glRectf can draw rectangles too.



#4 Trienco   Crossbones+   -  Reputation: 2091

Like
1Likes
Like

Posted 15 January 2013 - 11:20 PM

The code for all the setup isn't shown. What does your viewport, view and projection look like?

 

Since you don't clear the depth buffer, did you make sure that depth testing is disabled?

 

Are you sure that based on your view/projection "front" is where you think it is?

 

Why are you setting alpha to 0 (100% transparent, depending on blend mode)?


f@dzhttp://festini.device-zero.de

#5 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 16 January 2013 - 07:55 AM

The set up:

 

bool init() {
 //Initialize SDL if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
 return false;
 } 
//Create Window if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL ) { 
return false;
 } 
//Initialize OpenGL if( initGL() == false ) {
 return false;
 }
 //Set caption SDL_WM_SetCaption( "OpenGL Test", NULL );
 return true;
 }

bool initGL() { 
//Initialize Projection Matrix
 glMatrixMode( GL_PROJECTION );
 glLoadIdentity();
 //Initialize Modelview Matrix 
glMatrixMode( GL_MODELVIEW ); 
glLoadIdentity(); 
//Initialize clear color glClearColor( 0.f, 0.f, 0.f, 1.f );
 //Check for error GLenum error = glGetError();
 if( error != GL_NO_ERROR ) {
 printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) ); return false;
 }
 return true; 
}

 



#6 JTippetts   Moderators   -  Reputation: 8265

Like
1Likes
Like

Posted 16 January 2013 - 08:27 AM

So your projection matrix is identity, but you are trying to draw a quad that is 90x130 units in size? When projection is identity, the screen will be mapped to the ranges (-1,1) on X and Y, so trying to draw a quad from (0, -80) to (90,50) will draw a quad that is vastly larger than the screen. You really should set your projection matrix to something other than identity; ie, a projection or orthographic matrix.

Also, as Trienco indicated, since you added the call to glClear(GL_COLOR_BUFFER_BIT) your depth buffer is no longer being cleared. Yet you never disable depth writing or depth testing, so even if you did get something to draw on the first frame, the depth buffer would never be cleared so if the camera or any objects moved on subsequent frames, the old depth buffer would still be around clipping fragments you probably don't want clipped. If you are going to disable clearing depth buffer in glClear() then you should also disable depth testing and depth writing.

#7 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 16 January 2013 - 01:36 PM

Ugh, sorry guys. I copied that from the wrong file I have. That's not the setup (after reading your suggestions I realized my mistake. Sorry for wasting time)....

 

#include "SDL.h"
#include <SDL_opengl.h>

//Screen attributes
const int SCREEN_WIDTH = 1500;
const int SCREEN_HEIGHT = 1500;
const int SCREEN_BPP = 32;

bool initGL()
{
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glOrtho(0, 1500, 1500, 0, -1, 1);
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //Initialize clear color
   glClearColor(1.0,.4,0.0,0.0);

    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        return false;
    }

    //Enable unicode
    SDL_EnableUNICODE( SDL_TRUE );

    //Initialize OpenGL
    if( initGL() == false )
    {
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "Project Birth Game Window. Guns&Player.", NULL );

    return true;
}




 

 

So this is what my set-up actually looks like. 
I just want to clarify that I was drawing Quads and whatnot without a problem when I was just calling a "draw" function, but as soon as I made it into a class I've been getting the problems described above in the OP. 


#8 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 16 January 2013 - 08:26 PM

What could possibly be the problem? I call a function to draw a square and it works fine....but if that EXACT same function is part of a class it doesn't work....Window is up, but screen clear.



#9 Trienco   Crossbones+   -  Reputation: 2091

Like
3Likes
Like

Posted 16 January 2013 - 10:59 PM

One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)


f@dzhttp://festini.device-zero.de

#10 kd7tck   Members   -  Reputation: 715

Like
0Likes
Like

Posted 16 January 2013 - 11:38 PM

One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)

Trienco that looks like it might be why it doesn't work.

 

GDsnakes you should make all function inputs const unless you have to modify it.



#11 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 17 January 2013 - 04:24 PM

One kind of obvious thing that jumps at me is your constructor. Take a good look at it and if you don't notice it, describe to me in detail what it does ,-)

Trienco that looks like it might be why it doesn't work.

 

GDsnakes you should make all function inputs const unless you have to modify it.

I changed it to this. Still the exact same same issues.

 

QuadDrawing::QuadDrawing( float Xoff, float Yoff)
{
QuadoffX = Xoff;
QuadoffY = Yoff;
}


#12 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 17 January 2013 - 06:35 PM

Fixed it. Needed to Swap Buffers.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS