Game Log: Day 1

I’ve decided to make something. An experience. I don’t quite know what it will be yet, but I think the important thing for me is to just start making. With that in mind, I’ve decided the first thing I need to do is get a square rendered in a window.

This “just start making” kind of mindset almost leads to a greedy-algorithm type of thinking. Writing a display function with some OpenGL is faster than learning a game engine like Unity or Unreal, so I went with doing the low level graphics programming.

I also decided to use C++ as my language. There’s a lot of fancier languages out there these days, but the vast majority of knowledge in the gaming and graphics space is for C/C++. If I’m gonna need help, it’s better to not be treading (entirely) new ground.

Since I’m on OS X, there’s a few things I had to learn in order to get started. Some stuff isn’t particularly obvious, so I figured I’d put it all in one place to help others trying to do the same thing.

  1. Install Xcode. This makes OpenGl and GLUT available as frameworks to include in your code. Make sure it installs the command line tools as well. I should note that I’m not using Xcode as my editor, but regardless, the comupter needs it installed.

  2. On OS X, the way to include frameworks via the command line is slightly different. GCC uses the -framework option, as opposed to -l on other systems. I’ll have my makefile written out below as an example.

  3. When you’re using OS X, the way you include OpenGL and GLUT is slightly different than on other operating systems. You can get around this with an #ifdef __APPLE__, so it’s really not a big deal. The two syntaxes look like the following:

  // OS X
  #include <OpenGL/gl.h>
  #include <GLUT/glut.h>

  // Everyone else
  #include <GL/gl.h>
  #include <GL/glut.h>

Once those three issues are sorted out, it’s easy to start using OpenGL and GLUT. Here’s the source code I wrote to draw a white square on a black background:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifdef __APPLE__
#  include <OpenGL/gl.h>
#  include <GLUT/glut.h>
#else
#  include <GL/gl.h>
#  include <GL/glut.h>
#endif

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT);

  glColor3f(1.0, 1.0, 1.0);

  glBegin(GL_POLYGON);
    glVertex3f(0.25, 0.25, 0.0);
    glVertex3f(0.75, 0.25, 0.0);
    glVertex3f(0.75, 0.75, 0.0);
    glVertex3f(0.25, 0.75, 0.0);
  glEnd();

  glFlush();
}

int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(750, 750);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("square");

  glClearColor(0.0, 0.0, 0.0, 0.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

  glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}

Now that everything is in place, I can build my code by doing the following:

  g++ game.cpp -framework OpenGL \
               -framework GLUT -o bin/game

There we have it! A white square on a black background.It’s nice to have this “hello world” program out of the way. Expect to hear more as I move forward on this idea, whatever this is.