PDA

View Full Version : QGLWidget Not Drawing Outside Of paintGL()



steadi
5th December 2012, 21:14
Hello There,

I am trying to create a level editor for my game using a QGLWidget but I have run into a slight problem. When I use the standard paintGL() function like this:


void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0,0);
glVertex2f(0,50);
glVertex2f(50,0);
glEnd();
}


everything works fine. However, if I try and use my own function to draw a triangle:



void GLWidget::DrawAlphaTile(int x, int y)
{
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex2f(x,y);
glVertex2f(x + 32, y);
glVertex2f(x + 32,y - 32);
glVertex2f(x,y - 32);
glEnd();
}


It does not work. Is there a way around this because there is a lot of user input involved with the integer parameters?

Thanks,
Matt

wysota
5th December 2012, 22:35
Why can't you just put that code in paintGL()?

steadi
5th December 2012, 23:24
Why can't you just put that code in paintGL()?

Because it needs to be calledmultiple times with different parameters.

wysota
6th December 2012, 00:46
Because it needs to be calledmultiple times with different parameters.

So call it from within paintGL(). I'm assuming this method is not public, is it?

steadi
6th December 2012, 17:41
So call it from within paintGL(). I'm assuming this method is not public, is it?

I am sorry but I have no idea where you are going with this mate. I need to be able to load in a .txt file of tile locations and z values and then draw them accordingly. How does one go about doing this? You cannot call a function from outside paintGL().

What would be the point of calling it from within if it needs to iterate through many QStringListItems, convert them and then call a function that will one by one sort through them and draw them to the screen at given coordinates.

wysota
6th December 2012, 19:41
What would be the point of calling it from within if it needs to iterate through many QStringListItems, convert them and then call a function that will one by one sort through them and draw them to the screen at given coordinates.

No, it doesn't need to do all that. If the function is about drawing an entity then it draws the entity, it doesn't load it, parse it, play the violin and do all other possible stuff. If you want a function to load some data, then do exactly that -- implement a function to load the data and when the data is loaded ask the UI to update itself (by calling update() or updateGL()). Eventually it will call paintGL() which can call a function of your choice that will draw the data parsed earlier.

steadi
8th December 2012, 14:49
No, it doesn't need to do all that. If the function is about drawing an entity then it draws the entity, it doesn't load it, parse it, play the violin and do all other possible stuff. If you want a function to load some data, then do exactly that -- implement a function to load the data and when the data is loaded ask the UI to update itself (by calling update() or updateGL()). Eventually it will call paintGL() which can call a function of your choice that will draw the data parsed earlier.

Okay. Lemme simplify this to something we can both understand:
I want to draw a square onto a QGLWidget everytime I press a button, but I cannot because the press button event is outside of paintGL()... How do i go about solving this?

wysota
8th December 2012, 15:39
Okay. Lemme simplify this to something we can both understand:
I want to draw a square onto a QGLWidget everytime I press a button, but I cannot because the press button event is outside of paintGL()... How do i go about solving this?

Exactly like I said, make the "addButton" method add a button to an internal list of added buttons and call updateGL(). In paintGL() iterate over the list and draw the buttons.