This article is introductory, designed for familiarity with Qt + OpenGL for beginners who plan to learn Qt (as a cross-platform software development toolkit in the C ++ programming language) + OpenGL (as a graphic library).
What a newbie needs:1)
Qt Creator (has good on-line documentation and hints while typing).
Skchaet2)
doc.qt.nokia.com - official documentation in English
3)
doc.crossplatform.ru - documentation in Russian
4) Be sure to read about
Qt and
OpenGL5)
Excellent article to start exploring.What do we doSince this article focuses specifically on the basics, our task will be the following:
1) Disassemble how the application is created
2) How to draw objects
3) How to work with the mouse pointer and events (pressing the keys on the keyboard and mouse)
4) Work with a timer
5) Let's create our first banal game. We will use the timer to randomly move the square. After hovering over the square of the pointer and clicking on it with the left mouse button, in the case of hitting the square, we will add +1 to the points.
')
Create a project
When you open Qt Creator, we start creating a new project.
Choosing
a Qt Project Widget ->
GUI Qt ApplicationThe
Class Information section unchecks to create a form.
As a result of actions we will get a project with files:
opengl.pro - required to compile our project
mainwindow.h - to declare all global data
main.cppmainwindow.cpp - methods of our program
Connecting libraries
In the * .pro file of your project in the
Qt + = line, you need to add opengl in order to enable the use of the opengl library. Other libraries are connected in the same way.
In the
mainwindow.h file - if your name is selected by default, you need to connect:
#include <QGLWidget> #include <QtOpenGL> #include <QTimer>
Predestination for us the necessary methods and variables
Open
mainwindow.hFirst we change:
class MainWindow: public QMainWindowon
class MainWindow: public QGLWidgetThis is because
QMainWindow is a class for displaying a simple window, and since we will work with opengl, we will need a QGLWidget - this is a class for displaying graphics that implements the functions of the OpenGL library.
Now we will predetermine variables and methods.
protected: int geese_size;
We also have one slot in order to recalculate the new coordinates of the square on which to click on the timer.
protected slots: void geese_coord();
Image Building Principle
QGLWidget is so arranged that when the class is first initialized, it automatically calls methods in the following order:
At startup: initializeGL () -> resizeGL () -> paintGL ()
When resizing a window: resizeGL () -> paintGL ()
updateGL () calls paintGL ()
initializeGL - must be used for global image building, which is not necessary to specify when building a frame.
resizeGL - used to build the size of the window. If during the work the window size is changed, but not to change the viewing area, then when you increase the size, you can observe unpredictable phenomena.
paintGL - this method will build our every frame for display.
glClear(GL_COLOR_BUFFER_BIT);
Why double buffering
PaintGL does not immediately draw the picture on the screen, but enters it into the buffer, and on request swapBuffers () replaces the current image with what appears in the buffer. By itself, buffering allows you to more correctly replace the image so that there are no jumps on the screen.
Mouse Click Events
mousePressEvent () - the method is automatically called when you press the mouse buttons. In the transmitted parameters, you can get various information, for example, which button was pressed and which point by coordinates.
-This event in our example is used to determine where the mouse was clicked, then if our coordinates are in the square field, then add +1 to our points and rebuild our frame.
- Also, we use to determine the initial coordinates to highlight the area on the screen, while clamping and moving the pointer.
Mouse Move Event
mouseMoveEvent () - automatically called when you change the coordinates of the mouse pointer. But there is one. But, the default is setMouseTracking (false), so the event is triggered only when the mouse is pressed, in order for the method to be called even without clicking, you must set setMouseTracking (true).
- We use this method to get the current position of the pointer in order to restructure the selection of the area or draw our own cursor.
The event when the "mouse button" is pressed
mouseReleaseEvent () is automatically called if the mouse button is “unwound”. It also takes various parameters.
- In this case, we use the method to erase the selected area from the screen.
Event keystroke on the keyboard
keyPressEvent () - the method is called at the event when the button on the keyboard is pressed.
- In our example, we use this method in order to redefine the coordinates of our square and move it to a new place.
Timer
QTimer - allows us to create a stream that will listen to signals and run the corresponding slots.
- In this case, we create a timer that will wait for 750ms after which it finishes its work by sending us a
timeout () signal, but at the end of the signal we will not stop working, but will again start the slot to redefine the coordinates of the square that we need to click for order to score points.
QTimer *timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(geese_coord())); timer->start(750);
Assignment on this material for learning.
The code I have laid out works, but my homework will modify the code so that when you start the game there is a greeting, when you click on it, you are given a minute to receive points. After a minute, the number of points scored was displayed and it was offered to play again.
Conclusion!
Most of the little written here in our first primitive game is simply not necessary. But I want to note again: "The
article is introductory, designed for familiarity with Qt + OpenGL ". Also, if you notice programs written in this way, you can compile for any operating environment.
The finished version of the working code can be found here:
View and download the sourceDownload game for windowsDownload game for LinuxPS In the future, if not against it, I will continue to write the lessons. For example, in the following lesson, we will create a game with shooting for example ducks.
If you have questions - write in the comments or in a personal.