2D C++ OpenGL/GLFW Basecode

I’m teaching some games programming stuff this year, and we’d started off using SDL, but the students are having a hard time with it – the main problems being:
– It’s bulky,
– It demands control of the mainline and then bloats it,
– The documentation is okay, but significantly less than stellar.

This isn’t to say that I have much against SDL – it does a lot of good things, but I’m concerned it’s providing too much specific functionality, which I don’t want to rely on and be tied to when (not if) things change in the future. So with this in mind, I’ve spent the evening reading about OpenGL frameworks and have decided to take the class in a new direction – namely GLFW, the cross-platform OpenGL framework.

We’re mainly going to be working in 2D, so I’ve put together some OpenGL/GLFW basecode that initialises a window, sets orthogonal projection (i.e. things further away don’t get any smaller) and just draws a line from the top-left to the bottom-right (so is easy to strip out when you want to adapt it to your own purposes) – and the entire thing is only around 100 lines of code! (and that’s with stacks of whitespace). The equivalent in SDL is closer to 350 lines and is significantly more complex/complex-looking.

Check it out…

  1. // OpenGL/GLFW Basecode | r3dux
  2.  
  3. #include <iostream>
  4. #include <GL/glfw.h> // Include OpenGL Framework library
  5. using namespace std;
  6.  
  7. void initGL(int width, int height)
  8. {
  9. 	// ----- Window and Projection Settings -----
  10.  
  11. 	// Set the window title
  12. 	glfwSetWindowTitle("GLFW Basecode");
  13.  
  14. 	// Setup our viewport to be the entire size of the window
  15. 	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
  16.  
  17. 	// Change to the projection matrix, reset the matrix and set up orthagonal projection (i.e. 2D)
  18. 	glMatrixMode(GL_PROJECTION);
  19. 	glLoadIdentity();
  20. 	glOrtho(0, width, height, 0, 0, 1); // Paramters: left, right, bottom, top, near, far
  21.  
  22. 	// ----- OpenGL settings -----
  23.  
  24. 	glfwSwapInterval(1); 		// Lock to vertical sync of monitor (normally 60Hz, so 60fps)
  25.  
  26. 	glEnable(GL_SMOOTH);		// Enable (gouraud) shading
  27.  
  28. 	glDisable(GL_DEPTH_TEST); 	// Disable depth testing
  29.  
  30. 	glEnable(GL_BLEND);		// Enable blending (used for alpha) and blending function to use
  31. 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  32.  
  33. 	glLineWidth(5.0f);		// Set a 'chunky' line width
  34.  
  35. 	glEnable(GL_LINE_SMOOTH);	// Enable anti-aliasing on lines
  36.  
  37. 	glPointSize(5.0f);		// Set a 'chunky' point size
  38.  
  39. 	glEnable(GL_POINT_SMOOTH);	// Enable anti-aliasing on points
  40. }
  41.  
  42. void drawScene()
  43. {
  44. 	// Clear the screen
  45. 	glClear(GL_COLOR_BUFFER_BIT);
  46.  
  47. 	// Reset the matrix
  48. 	glMatrixMode(GL_MODELVIEW);
  49. 	glLoadIdentity();
  50.  
  51. 	// ----- Draw stuff! -----
  52.  
  53. 	glBegin(GL_LINES);
  54. 		glColor3ub(255, 0, 0);
  55. 		glVertex2f(0.0f, 0.0f);
  56.  
  57. 		glColor3ub(0, 0, 255);
  58. 		glVertex2f(800.0f, 600.0f);
  59. 	glEnd();
  60.  
  61. 	// ----- Stop Drawing Stuff! ------
  62.  
  63. 	glfwSwapBuffers(); // Swap the buffers to display the scene (so we don't have to watch it being drawn!)
  64. }
  65.  
  66. int main()
  67. {
  68. 	// Frame counter and window settings variables
  69. 	int frame      = 0, width     = 800, height      = 600;
  70. 	int redBits    = 8, greenBits = 8,   blueBits    = 8;
  71. 	int alphaBits  = 8, depthBits = 0,   stencilBits = 0;
  72.  
  73. 	// Flag to keep our main loop running
  74. 	bool running = true;
  75.  
  76. 	// Initialise glfw
  77. 	glfwInit();
  78.  
  79. 	// Create a window
  80. 	if(!glfwOpenWindow(width, height, redBits, greenBits, blueBits, alphaBits, depthBits, stencilBits, GLFW_WINDOW))
  81. 	{
  82. 		cout << "Failed to open window!" << endl;
  83. 		glfwTerminate();
  84. 		return 0;
  85.     	}
  86.  
  87. 	// Call our initGL function to set up our OpenGL options
  88. 	initGL(width, height);
  89.  
  90. 	while (running == true)
  91. 	{
  92.     		// Increase our frame counter
  93.         	frame++;
  94.  
  95. 		// Draw our scene
  96. 		drawScene();
  97.  
  98. 		// exit if ESC was pressed or window was closed
  99. 		running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
  100. 	}
  101.  
  102. 	glfwTerminate();
  103.  
  104. 	return 0;
  105. }

How streamlined is that?!? Sweeeeeeet!

Next task – do stuff with it! =D

P.S. If you wanted to add GLEW to it to take care of all your extension wrangling needs, then just add the glew.h header before glfw.h, include the glew library in your project and add the following to the top of the initGL function:

//  ----- Initialise GLEW -----
 
GLenum err = glewInit();
if (GLEW_OK != err)
{
	cout << "GLEW initialisation error: " << glewGetErrorString(err) << endl;
	exit(-1);
}
cout << "GLEW intialised successfully. Using GLEW version: " << glewGetString(GLEW_VERSION) << endl;

Cheers!

Update: Modified initGL() function to disable the depth testing correctly via glDisable(GL_DEPTH_TEST) and not glDisable(GL_DEPTH) – silly mistake.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.