• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MylesEvans

Problem with creating a class for drawing....

11 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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

 

Good luck, cant help.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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)?

1

Share this post


Link to post
Share on other sites

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; 
}

 

0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites

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. 
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
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.

0

Share this post


Link to post
Share on other sites
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;
}
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0