Sign in to follow this  
Albrektson

AllegroGL 2D Game - Z-sorting quads?

Recommended Posts

When using AllegroGL to create what would look like a 2D-game using textured quads, how do I sort the quads on the Z-axis (like a layer order)? Here's what I'm working with:
#include <allegro.h>
#include <alleggl.h>

int main() {
	
	/* Initialize both Allegro and AllegroGL */
	allegro_init();
	install_allegro_gl();
	
	/* Tell Allegro we want to use the keyboard */
	install_keyboard();

	/* Suggest a good screen mode for OpenGL */
	allegro_gl_set(AGL_Z_DEPTH, 8);
	allegro_gl_set(AGL_COLOR_DEPTH, 32);
	allegro_gl_set(AGL_SUGGEST, AGL_Z_DEPTH | AGL_COLOR_DEPTH);

	/* Set the graphics mode */
	set_gfx_mode(GFX_OPENGL_WINDOWED, 800, 600, 0, 0);
	
	/* Clear the screen */
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



//I am setting a state where I am editing the projection matrix...
  glMatrixMode(GL_PROJECTION);

//Clearing the projection matrix...
  glLoadIdentity();

//Creating an orthoscopic view matrix going from -1 -> 1 in each
//dimension on the screen (x, y, z). 
glOrtho(0, 800, 600, 0, -1, 1);


//Now editing the model-view matrix.
	glMatrixMode(GL_MODELVIEW);
	//Clearing the projection matrix...
	glLoadIdentity();
	



	
//Load an identity matrix so we don't multiply the current model-view matrix by the last matrix.  AKA reset the matrix.
glLoadIdentity();
//Move 0.2 on the x axis.
glTranslatef(100, 100, 0);
	
	//Begin drawing quads
glBegin(GL_QUADS);
  //Define the color
glColor3ub(255, 255, 255);
  //Draw our four points, clockwise.
glVertex3f(0, 0, 0);
glVertex3f(320, 0, 0); 
glVertex3f(320, 240, 0);
glVertex3f(0, 240, 0);

glEnd();




//Load an identity matrix so we don't multiply the current model-view matrix by the last matrix.  AKA reset the matrix.
glLoadIdentity();
//Move 0.2 on the x axis.
glTranslatef(200, 200, 0);

glBegin(GL_QUADS);
  //Define the color
glColor3ub(128, 255, 255);
  //Draw our four points, clockwise.
glVertex3f(0, 0, 0);
glVertex3f(320, 0, 0); 
glVertex3f(320, 240, 0);
glVertex3f(0, 240, 0);

glEnd();




	
	/* Flip the backbuffer on screen */
	allegro_gl_flip();
	
	/* Wait for the user to press a key */
	readkey();
	
	/* Finished. */
	return 0;
}
END_OF_MAIN();



And btw - is there a simpler way of drawing quads based on a height, width and X,Y,Z values for positioning of the quad?

Share this post


Link to post
Share on other sites
Turned out to be that glEnable(GL_DEPTH_TEST); was a required param.

I'll let the thread stay for now in case anyone has some comments that might be of interest.

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