Making a Game with OpenGL – Our First Joystick-Supported Game

Tue, Jun 6, 2017 4-minute read

In this post, we will build every object from scratch with freeglut and put together a simple game you can control with a joystick.

This was the project I made for the elective Computer Graphics course I took this year. It is written in C++ on top of freeglut, and I worked on it using the Visual C++ 2010 editor along with Sublime Text.

Before anything else, let’s define the screen size and the functions we are going to use.

glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("FREEGLUT OYUN");
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutForceJoystickFunc();
	glutJoystickFunc(joystick,25);
	glutForceJoystickFunc();
	glutMainLoop();

Two functions are worth singling out here:

  • glutForceJoystickFunc()

  • glutJoystickFunc(joystick,25)

We already walked through what glutJoystickFunc does in an earlier post. You can read that post here.

glutForceJoystickFunc, on the other hand, fixed a problem I ran into on Windows: when a joystick analog button was held down, it would stop exchanging data continuously. With this function in place, the joystick is polled for data at all times. One side effect is that when the game first starts, if no joystick is active, the game does not begin and simply waits frozen.

In short, the idea of the game is to avoid the opponents while the starting box makes its way to the end. Once it gets there, new shapes appear at different speeds, and that is what keeps the game going.

Below is the function I wrote to reset everything. (Sorry the variable names are so meaningless.)

void sifirla()
{
	asd = (rand()%4)+1;
	wasd = (rand()%4)+1;
	hizDS1=(float)((rand()%6)+1)/10;
	donus=rand()%1+1;
	r=(float)(rand()%10)/10;
	g=(float)(rand()%10)/10;
	b=(float)(rand()%10)/10;
	hizDS2=(float)((rand()%6)+1)/10;
	joyy=-40;
	joyx=0;
	glutPostRedisplay();
}

All of the game’s code is up on my GitHub account.

You can find it here: https://github.com/ffahri/glutsimplegame

There are also a couple of details that did not make it into the video. Using buttons 1 and 4 on the right side of the joystick, we can scale our own box. And in the same spot, pressing button 3 lets us change the color.

Gameplay video

A simple game we developed with FreeGlut

So what did taking this course actually teach me?

To begin with, I picked up the underlying graphics theory, the kind that carries over to technologies like OpenGL, DirectX, and Vulkan. From there we put that theory into practice with OpenGL. Along the way I also learned where functions such as Mandelbrot and Bezier come into play, and how to tweak them to produce new shapes and images.

– Turkish Version –

OpenGL ile oyun yapmak – Joystick destekli ilk oyunumuz

Bu yazımızda freeglut kullanarak her nesneyi sıfırdan oluşturup, joystick ile kontrol edilebilen basit bir oyun yapacağız.

Bu oyun, bu sene aldığım seçmeli Bilgisayar Grafikleri dersinin projesiydi. C++ ile freeglut üzerine geliştirdim ve bunu yaparken Visual C++ 2010 editörünün yanında Sublime Text editörünü de kullandım.

Her şeyden önce, ekran boyutunu ve kullanacağımız fonksiyonların tanımlarını yapalım.

glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("FREEGLUT OYUN");
	init();
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutForceJoystickFunc();
	glutJoystickFunc(joystick,25);
	glutForceJoystickFunc();
	glutMainLoop();

Burada öne çıkan 2 fonksiyonumuz var. Bunlar:

  • glutForceJoystickFunc()

  • glutJoystickFunc(joystick,25)

glutJoystickFunc tanımını daha önceki bir yazımızda yapmıştık. Buradan o yazıya gidebilirsiniz.

glutForceJoystickFunc ise Windows’ta karşılaştığım bir soruna çözüm oldu: joystick analog tuşları basılı tutulduğunda devamlı veri alışverişi yapmıyordu. Bu fonksiyon sayesinde joystickten her an veri bekleniyor. Bunun bir yan etkisi olarak, oyun ilk açıldığı an eğer aktif bir joystick yoksa oyun başlamıyor ve donuk bir şekilde bekliyor.

Kısaca oyunumuzun fikri, başlangıç kutucuğu sona ulaşana kadar rakiplere çarpmamaya çalışmak. Ulaştığında ise karşımıza yeni şekiller farklı hızlarda çıkıyor ve oyunun devamlılığı böyle sağlanıyor.

Aşağıda sıfırlamak için yazdığım fonksiyonu görebilirsiniz. (Değişken isimleri bu kadar anlamsız olduğu için üzgünüm.)

void sifirla()
{
	asd = (rand()%4)+1;
	wasd = (rand()%4)+1;
	hizDS1=(float)((rand()%6)+1)/10;
	donus=rand()%1+1;
	r=(float)(rand()%10)/10;
	g=(float)(rand()%10)/10;
	b=(float)(rand()%10)/10;
	hizDS2=(float)((rand()%6)+1)/10;
	joyy=-40;
	joyx=0;
	glutPostRedisplay();
}

Oyunun tüm kodları github hesabımda.

Buradan ulaşabilirsiniz. https://github.com/ffahri/glutsimplegame

Videoya eklemediğim birkaç detay da var. Joystickte sağ tarafta bulunan 1 ve 4 tuşları ile scale yapıp kendi kutumuzu ölçeklendirebiliriz. Yine aynı yerde 3 tuşuna bastığımızda ise renk değiştirebiliyoruz.

Oyunun oynanış videosu

FreeGlut ile geliştirdiğimiz basit bir oyun

Peki bu dersi seçerek neler öğrendim?

Öncelikle grafik teorisini öğrendim; bu, OpenGL, DirectX ve Vulkan gibi teknolojiler için geçerli olan bir temel. Daha sonra bu teorileri OpenGL kullanarak pratiğe döktük. Bu süreçte Mandelbrot, Bezier gibi fonksiyonların nerelerde kullanıldığını ve bunları değiştirip yeni şekiller, görüntüler elde etmeyi de öğrendim.