Play with Qt (1) and play with Qt (

Source: Internet
Author: User

Play with Qt (1) and play with Qt (

Recently I have been reading some things about the game engine. I have several small game ideas. In fact, the implementation is quite troublesome. I want to find a game engine to see if it can be coded. A lot of 2D engines were found after the revolution, among which the domestic cocos2dx seems to be widely used, but many people disagree with this. As a result, I decided to try it out. I had no choice but to make some small achievements early in the morning. However, it seems that I have to spend some time trying to implement it. I think I 'd like to find another one. I used Qt before, so I picked it up again.

 

I want to improve rendering performance on Qt. I still want to use OpenGL. I have never been very clear about OpenGL, so I have been studying it for a lot of time. I think a lot of people are confused about this display server, OpenGL, and so on. Let's talk about these things to enrich our knowledge.

1. For the Display Server, the most recently viewed is Ubuntu17.10. The default display server is changed to wayland. According to my understanding, with the display server, we can use the window system. The client of the display server is the window system, and the display server provides Screen Drawing for our window system, input events and other functions. For input events, mouse and keyboard events are common.

2. Then OpenGL is used. OpenGL is a cross-platform graphic interface. OpenGL is related to the graphics card. OpenGL can be used only when the video card provides support, of course, OpenGL is negotiated with the graphics card manufacturer. With OpenGL, we can use a video card to process graphics and images, and then hand them to the Display Server for display.

3. Note that OpenGL cannot communicate directly with the Display Server. That is to say, the graphics images processed by OpenGL cannot be directly sent to the display server, there is one thing in the middle for processing. This middleware is called wgl on the platform, glx on linux, and agl on macos according to the platform. Now we can use a window to display the graphics processed by OpenGL. That is, we often say that OpenGL is used for rendering.

4. In order to unify wgl, glx, and agl to achieve platform unification, glfw, glu, and other things were born. These things encapsulated wgl, glx, agl combines the display servers of various platforms to create windows. We can use a set of code to implement cross-platform Rendering using OpenGL in windows.

5. Then the problem arises, Because OpenGL interfaces on different operating systems are inconsistent. compiling on different platforms may be incompatible with each other, which makes it uncomfortable. As a result, glew and gglad were born to realize the unification of OpenGL interfaces of various operating systems. In combination with the above mentioned, it would be nice to implement a comprehensive cross-platform approach.

 

Now we know that we must at least achieve the first three points above to use GPU for accelerated rendering. Let's take a look at the applications of these things, which are nothing more than various engines and graphics libraries, such:

1. Cocos2dx directly uses glfw of, and OpenGL implements the UI, drawing, and so on, and becomes a game engine.

2. Qt is quite good. He has implemented 4th and 5 on his own, so he has implemented cross-platform. But it is not independent, so we cannot use it.

 

However, Qt is not rendered using OpenGL. Display in Qt is divided into three types: QWidget, QGraphics, and QQuick.

1. In QWidget, the implementation of controls is basically encapsulation of corresponding controls on each platform. QWindow is used in QWidget to create a window. The system plug-in cannot be used in a separate QWindow. Only the window is provided. Therefore, in theory, OpenGL can be used for plotting in QWIndow. For future development and 2D development, Qt improves 3D graphics performance to meet game and other development needs. After Qt5.0, QWidget components are extracted from the gui module as the widgets module separately, this is also reasonable.

2. In order to improve the rendering performance of a large number of simple components, Qt has created the QGraphics class, but they still belong to the widgets module and are not necessarily rendered using OpenGL, to use OpenGL for rendering, you need to build a bridge between QWidget and OpenGL, Which is QGLWidget.

3. The QQuick class uses OpenGL for rendering and supports multi-threaded rendering. Qt only provides the qml interface for ease of use, the exposed QQuickItem class is used for custom controls. In practice, in a unix-like environment, all the controls in QQuick also provide C ++ interfaces for programming, except that the Qt document does not contain or corresponds to the Qt module, you must include the header file. These header files are private to Qt, And the header file formats are basically * _ p. h. Link the QtQuickTemplate2 and QtQuickControls2 libraries related to QtQuick. For example, the following section uses the QQuick C ++ Control for mac.

 1 #include <QGuiApplication> 2 #include <QQmlApplicationEngine> 3  4 #include <QQuickView> 5 #include <QQuickItem> 6 #include <QObject> 7  8 #include "QtQuick/private/qquickimage_p.h" 9 #include "QtQuick/private/qquickrectangle_p.h"10 #include "QtQuickTemplates2/private/qquickbutton_p.h"11 #include "QtQuickTemplates2/private/qquicklabel_p.h"12 13 int main(int argc, char *argv[])14 {15     QGuiApplication app(argc, argv);16 17     QQuickView view;18     view.resize(600, 800);19 20     QQuickItem* parentItem = view.contentItem();21 22     QQuickImage* imgItem = new QQuickImage(parentItem);23     imgItem->setSource(QUrl::fromLocalFile("/Users/Bearyin/Pictures/P30429-143922.jpg"));24     imgItem->setSize(QSizeF(600, 800));25 26 27     QObject::connect(&view, &QQuickView::widthChanged, [&](int){28         imgItem->setSize(view.size());29     });30 31     QObject::connect(&view, &QQuickView::heightChanged, [&](int){32         imgItem->setSize(view.size());33     });34 35 36     QQuickRectangle* rectItem = new QQuickRectangle;37     rectItem->setSize(QSizeF(100, 100));38     rectItem->setColor(QColor(255, 255, 0));39 40     QQuickLabel* labelItem = new QQuickLabel;41     labelItem->setText("Hello World");42     labelItem->setColor(QColor(255, 0, 0));43 //    labelItem->setPosition(QPointF(200, 200));44     labelItem->setSize(QSize(100, 100));45     labelItem->setBackground(rectItem);46 47 48     QQuickButton* btItem = new QQuickButton(parentItem);49     btItem->setSize(QSizeF(100, 100));50     btItem->setPosition(QPointF(0, 0));51     btItem->setBackground(labelItem);52     btItem->setText("Hello World");53 54 55     QObject::connect(btItem, &QQuickButton::clicked, [&](){56         rectItem->setColor(QColor(0, 255, 0));57     });58 59     view.show();60 61 62     return app.exec();63 }
View Code

This is the pro file:

 1 QT += quick 2 CONFIG += c++11 3  4 QT_PRIVATE += core-private gui-private qml-private quick-private 5  6 DEFINES += QT_DEPRECATED_WARNINGS 7  8 SOURCES += main.cpp 9 10 qnx: target.path = /tmp/$${TARGET}/bin11 else: unix:!android: target.path = /opt/$${TARGET}/bin12 !isEmpty(target.path): INSTALLS += target13 14 15 INCLUDEPATH += \16 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtQuick.framework/Versions/5/Headers/5.9.2/QtQuick \17 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtQuickTemplates2.framework/Versions/5/Headers/5.9.2/QtQuickTemplates2 \18 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtQml.framework/Versions/5/Headers/5.9.2 \19 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtCore.framework/Versions/5/Headers/5.9.2 \20 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtGui.framework/Versions/5/Headers/5.9.2 \21 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtQuick.framework/Versions/5/Headers/5.9.2 \22 /Users/Bearyin/Software/Qt5.9.2/5.9.2/clang_64/lib/QtQuickTemplates2.framework/Versions/5/Headers/5.9.223 24 LIBS += -framework QtQuickTemplates2
View Code

The running result on my side is like this. You can modify the image and other paths on your own:

 

I have introduced so many things, but it doesn't matter whether the game is written or not. Now we can play Qt and learn something. However, the Code is a little tasteless, so I thought of an idea to motivate myself: to strip QtQuick from Qt.

If we can strip it out and make some improvements, I believe that QtQuick can be used independently as a good game engine. So the next article will let you know when I will make progress. In addition, this article is just my own opinion. If you have any errors or questions, please point it out.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.