Don't you hate having to make of vertex's every time you want to make a box?
What about making the border around the box?
If you said yes then this tutorial is for you. I have put together some prefab functions that could save a lot of time and effort when drawing boxes and the border around them.
These may seem simple but they help a lot when you are drawing menus and many other things.
To draw a Box I use this Function:
void DrawBox(int x, int y, int wi, int he)
{ //x, y, width, height
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
}
Basically this puts all the boxes you draw into one little code segment.
Instead of this:
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
You get this:
DrawBox(x, y, wi, he);
DrawBox(x, y, wi, he);
DrawBox(x, y, wi, he);
Oh But now you want a box with a border around it...
OK here's the old code:
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
glLineWidth(line);
glBegin(GL_LINE_LOOP);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
And here is the new one:
DrawBox(x, y, wi, he);
DrawLines(1.0f, x, y, wi, he);
You asking what the DrawLines command is?
Well this is it:
void DrawLines(float line, int x, int y, int wi, int he)
{
glLineWidth(line);
glBegin(GL_LINES);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex2i(x, y);
glVertex2i(x + wi, y);
glVertex2i(x + wi, y + he);
glVertex2i(x, y + he);
glEnd();
}
With the help of these new prefab functions you can compress your 500 line code, to draw your menu, into approximately 75 lines!