Game Programming using Qt 5 Beginner's Guide - Second Edition

Book description

A complete guide to designing and building fun games with Qt and Qt Quick using associated toolsets

About This Book
  • A step by step guide to learn Qt by building simple yet entertaining games
  • Get acquainted with a small yet powerful addition - Qt Gamepad Module, that enables Qt applications to support the use of gamepad hardware
  • Understand technologies such as QML, OpenGL, and Qt Creator to design intuitive games
Who This Book Is For

If you want to create great graphical user interfaces and astonishing games with Qt, this book is ideal for you. No previous knowledge of Qt is required; however knowledge of C++ is mandatory.

What You Will Learn
  • Install the latest version of Qt on your system
  • Understand the basic concepts of every Qt game and application
  • Develop 2D object-oriented graphics using Qt Graphics View
  • Build multiplayer games or add a chat function to your games with Qt Network module
  • Script your game with Qt QML
  • Explore the Qt Gamepad module in order to integrate gamepad support in C++ and QML applications
  • Program resolution-independent and fluid UIs using QML and Qt Quick
  • Control your game flow in line with mobile device sensors
  • Test and debug your game easily with Qt Creator and Qt Test
In Detail

Qt is the leading cross-platform toolkit for all significant desktop, mobile, and embedded platforms and is becoming popular by the day, especially on mobile and embedded devices. It's a powerful tool that perfectly fits the needs of game developers. This book will help you learn the basics of Qt and will equip you with the necessary toolsets to build apps and games.

The book begins by how to create an application and prepare a working environment for both desktop and mobile platforms. You will learn how to use built-in Qt widgets and Form Editor to create a GUI application and then learn the basics of creating graphical interfaces and Qt's core concepts.

Further, you'll learn to enrich your games by implementing network connectivity and employing scripting. You will learn about Qt's capabilities for handling strings and files, data storage, and serialization.

Moving on, you will learn about the new Qt Gamepad module and how to add it in your game and then delve into OpenGL and Vulcan, and how it can be used in Qt applications to implement hardware-accelerated 2D and 3D graphics. You will then explore various facets of Qt Quick: how it can be used in games to add game logic, add game physics, and build astonishing UIs for your games.

By the end of this book, you will have developed the skillset to develop interesting games with Qt.

Style and approach

Learn Qt with the help of numerous sample games, introduced step-by-step in each chapter

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Game Programming Using Qt 5 Beginner's Guide Second Edition
  3. Dedication
  4. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  5. Contributors
    1. About the authors
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
      1. Conventions used
    5. Get in touch
    6. Reviews
  7. Introduction to Qt
    1. A journey through time
    2. The cross-platform programming
      1. Supported platforms
      2. GUI scalability
    3. Qt versions
    4. Structure of Qt framework
      1. Qt Essentials
      2. Qt Add-ons
      3. qmake
      4. Modern C++ standards
    5. Choosing the right license
      1. An open source license
      2. A commercial license
    6. Summary
  8. Installation
    1. Installing the Qt SDK
      1. Time for action – Installing Qt using an online installer
        1. What just happened?
    2. Qt Creator
      1. Qt Creator's modes
      2. Setting up compilers, Qt versions, and kits
      3. Time for action – Loading an example project
      4. Qt documentation
      5. Time for action – Running the Affine Transformations project
        1. What just happened?
    3. Summary
  9. Qt GUI Programming
    1. Creating GUI in Qt
      1. Time for action – Creating a Qt Widgets project
        1. What just happened?
      2. Design mode interface
      3. Time for action – Adding widgets to the form
    2. Layouts
      1. Time for action – Adding a layout to the form
    3. Signals and slots
      1. Creating signals and slots
      2. Connecting signals and slots
      3. Old connect syntax
      4. Signal and slot access specifiers
      5. Time for action – Receiving the button-click signal from the form
        1. What just happened?
      6. Automatic slot connection and its drawbacks
      7. Time for action – Changing the texts on the labels from the code
    4. Creating a widget for the tic-tac-toe board
      1. Choosing between designer forms and plain C++ classes
      2. Time for action – Creating a game board widget
        1. What just happened?
      3. Automatic deletion of objects
      4. Time for action – Functionality of a tic-tac-toe board
      5. Time for action – Reacting to the game board's signals
        1. What just happened?
    5. Advanced form editor usage
      1. Time for action – Designing the game configuration dialog
      2. Accelerators and label buddies
      3. The tab order
      4. Time for action – Public interface of the dialog
    6. Polishing the application
      1. Size policies
      2. Protecting against invalid input
      3. Main menu and toolbars
      4. Time for action – Creating a menu and a toolbar
        1. What just happened?
      5. The Qt resource system
      6. Time for action – Adding icons to the project
      7. Have a go hero – Extending the game
    7. Pop quiz
    8. Summary
  10. Custom 2D Graphics with Graphics View
    1. Graphics View architecture
      1. Time for action – Creating a project with a Graphics View
        1. What just happened?
    2. Coordinate systems
      1. The item's coordinate system
      2. The scene's coordinate system
      3. The viewport's coordinate system
      4. Origin point of the transformation
        1. What just happened?
        2. Have a go hero – Applying multiple transformations
      5. Parent–child relationship between items
      6. Time for action – Using child items
        1. Have a go hero – Implementing the custom rectangle as a class
      7. Conversions between coordinate systems
    3. Overview of functionality
      1. Standard items
      2. Anti-aliasing
      3. Pens and brushes
      4. Item selection
      5. Keyboard focus in graphics scene
      6. Painter paths
        1. Time for action – Adding path items to the scene
      7. Z-order of items
      8. Ignoring transformations
        1. Time for action – Adding text to a custom rectangle
      9. Finding items by position
      10. Showing specific areas of the scene
      11. Saving a scene to an image file
        1. What just happened?
        2. Have a go hero – Rendering only specific parts of a scene
    4. Custom items
      1. Time for action – Creating a sine graph project
      2. Time for action – Creating a graphics item class
        1. What just happened?
      3. Events
      4. Time for action – Implementing the ability to scale the scene
        1. What just happened?
      5. Time for action – Taking the zoom level into account
      6. Time for action – Reacting to an item's selection state
        1. What just happened?
      7. Time for action – Event handling in a custom item
      8. Time for action – Implementing the ability to create and delete elements with mouse
      9. Time for action – Changing the item's size
      10. Have a go hero – Extending the item's functionality
    5. Widgets inside Graphics View
    6. Optimization
      1. A binary space partition tree
      2. Caching the item's paint function
      3. Optimizing the view
      4. OpenGL in the Graphics View
    7. Pop quiz
    8. Summary
  11. Animations in Graphics View
    1. The jumping elephant or how to animate the scene
      1. The game play
      2. Time for action - Creating an item for Benjamin
      3. The playing field
      4. Time for action - Making Benjamin move
        1. What just happened?
      5. Parallax scrolling
        1. Time for action - Moving the background
        2. What just happened?
        3. Have a go hero - Adding new background layers
    2. The Animation framework
      1. Properties
      2. Time for action - Adding a jump animation
    3. Property animations
      1. Time for action - Using animations to move items smoothly
        1. What just happened?
      2. Have a go hero - Letting the item handle Benjamin's jump
      3. Time for action - Keeping multiple animations in sync
        1. What just happened?
      4. Chaining multiple animations
    4. Adding gamepad support
      1. Working with gamepads in Qt
      2. Time for action - Handling gamepad events
    5. Item collision detection
      1. Time for action - Making the coins explode
        1. What just happened?
    6. Finishing the game
      1. Have a go hero - Extending the game
      2. A third way of animation
    7. Pop quiz
    8. Summary
  12. Qt Core Essentials
    1. Text handling
      1. String encodings
      2. QByteArray and QString
      3. Using other encodings
      4. Basic string operations
      5. The string search and lookup
      6. Dissecting strings
      7. Converting between numbers and strings
      8. Internationalization
      9. Using arguments in strings
      10. Regular expressions
        1. Time for action – A simple quiz game
        2. What just happened?
        3. Extracting information out of a string
        4. Finding all pattern occurrences
    2. Containers
      1. Main container types
      2. Convenience containers
      3. Allowed item types
      4. Implicit sharing
        1. Pointer invalidation
          1. What just happened?
        2. Unnecessary allocation
      5. Range-based for and Qt foreach macro
        1. What just happened?
    3. Data storage
      1. Files and devices
        1. Traversing directories
        2. Reading and writing files
        3. Devices
          1. Time for action – Implementing a device to encrypt data
          2. What just happened?
          3. Have a go hero – A GUI for the Caesar cipher
      2. Text streams
      3. Binary streams
        1. Time for action – Serialization of a custom structure
        2. What just happened?
      4. XML streams
        1. Time for action – Implementing an XML parser for player data
        2. What just happened?
        3. What just happened?
        4. Have a go hero – An XML serializer for player data
      5. QVariant
      6. QSettings
        1. Settings hierarchy
        2. Customizing the settings location and format
      7. JSON files
        1. Time for action – The player data JSON serializer
        2. Time for action – Implementing a JSON parser
        3. What just happened?
    4. Pop quiz
    5. Summary
  13. Networking
    1. QNetworkAccessManager
      1. Setting up a local HTTP server
      2. Preparing a URL for testing
      3. Time for action – Downloading a file
      4. Have a go hero – Extending the basic file downloader
      5. Single network manager per application
      6. Time for action – Displaying a proper error message
      7. Downloading files over FTP
      8. Downloading files in parallel
      9. The finished signal
      10. Time for action – Writing the OOP conform code using QSignalMapper
        1. What just happened?
      11. The error signal
      12. The readyRead signal
      13. Time for action – Showing the download progress
        1. What just happened?
      14. Using a proxy
    2. Connecting to Google, Facebook, Twitter, and co.
      1. Time for action – Using Google's Distance Matrix API
      2. Time for action – Constructing the query
      3. Time for action – Parsing the server's reply
      4. Have a go hero – Choosing XML as the reply's format
    3. Controlling the connectivity state
      1. QNetworkConfigurationManager
      2. QNetworkConfiguration
      3. QNetworkSession
      4. QNetworkInterface
    4. Communicating between games
      1. Time for action – Realizing a simple chat program
      2. The server – QTcpServer
        1. Time for action – Setting up the server
        2. What just happened?
        3. Time for action – Reacting on a new pending connection
        4. What just happened?
        5. Time for action – Forwarding a new message
        6. Have a go hero – Using QSignalMapper
        7. Time for action – Detecting a disconnect
        8. What just happened?
      3. The client
        1. Time for action – Setting up the client
        2. What just happened?
        3. Time for action – Receiving text messages
        4. Time for action – Sending text messages
        5. Have a go hero – Extending the chat server and client
      4. Synchronous network operations
    5. Using UDP
      1. Time for action – Sending a text via UDP
      2. Have a go hero – Connecting players of the Benjamin game
    6. Pop quiz
    7. Summary
  14. Custom Widgets
    1. Raster and vector graphics
    2. Raster painting
      1. Painter attributes
      2. Coordinate systems
      3. Drawing operations
    3. Creating a custom widget
      1. Time for action – Custom-painted widgets
        1. What just happened?
      2. Time for action – Transforming the viewport
        1. What just happened?
      3. Time for action – Drawing an oscillogram
      4. Time for action – Making oscillograms selectable
      5. Have a go hero – Reacting only to the left mouse button
      6. Touch events
    4. Working with images
      1. Loading
      2. Modifying
      3. Painting
    5. Painting text
      1. Static text
    6. Optimizing widget painting
      1. Time for action – Optimizing oscillogram drawing
        1. What just happened?
      2. Have a go hero – Caching the oscillogram in a pixmap
    7. Implementing a chess game
      1. Time for action – Developing the game architecture
        1. What just happened?
      2. Time for action – Implementing the game board class
        1. What just happened?
      3. Time for action – Understanding the ChessView class
        1. What just happened?
      4. Time for action – Rendering the pieces
        1. What just happened?
      5. Time for action – Making the chess game interactive
        1. What just happened?
      6. Time for action – Connecting the game algorithm
        1. What just happened?
      7. Have a go hero – Implementing the UI around the chess board
      8. Have a go hero – Connecting a UCI-compliant chess engine
    8. Pop quiz
    9. Summary
  15. OpenGL and Vulkan in Qt applications
    1. Introduction to OpenGL with Qt
      1. OpenGL windows and contexts
      2. Accessing OpenGL functions
    2. Using OpenGL in immediate mode
      1. Time for action – Drawing a triangle using Qt and OpenGL
      2. Multisampling
      3. Time for action – Scene-based rendering
        1. What just happened?
      4. Time for action – Drawing a textured cube
      5. Have a go hero – Animating a cube
    3. Modern OpenGL with Qt
      1. Shaders
      2. Time for action – Shaded objects
      3. GL buffers
      4. Using multiple OpenGL versions
      5. Offscreen rendering
    4. Vulkan in Qt applications
      1. Preparing the developing environment
      2. Vulkan instance, window, and renderer
      3. Time for action – Creating the minimal Vulkan project
        1. What just happened?
      4. Using Vulkan types and functions
      5. Time for action – Drawing with a dynamic background color
      6. Logs and validation
    5. Combining OpenGL or Vulkan with Qt Widgets
    6. Pop quiz
    7. Summary
  16. Scripting
    1. Why script?
    2. Evaluating JavaScript expressions
      1. Time for action – Creating a JavaScript editor
        1. What just happened?
      2. Global object state
    3. Exposing C++ objects and functions to JavaScript code
      1. Accessing C++ object's properties and methods
      2. Data type conversions between C++ and JavaScript
      3. Accessing signals and slots in scripts
      4. Time for action – Using a button from JavaScript
      5. Restricting access to C++ classes from JavaScript
      6. Creating C++ objects from JavaScript
      7. Exposing C++ functions to JavaScript
    4. Creating a JavaScript scripting game
      1. Time for action – Implementing the game engine
      2. Time for action – Exposing the game state to the JS engine
        1. What just happened?
      3. Time for action – Loading scripts provided by users
      4. Time for action – Executing the strategy scripts
      5. Time for action – Writing a strategy script
        1. Have a go hero – Extending the game
    5. Python scripting
      1. Time for action – Writing a Qt wrapper for embedding Python
        1. What just happened?
      2. Time for action – Converting data between C++ and Python
        1. What just happened?
        2. Have a go hero – Implementing the remaining conversions
      3. Time for action – Calling functions and returning values
        1. What just happened?
        2. Have a go hero – Wrapping Qt objects into Python objects
    6. Pop quiz
    7. Summary
  17. Introduction to Qt Quick
    1. Declarative UI programming
      1. Time for action – Creating the first project
      2. Time for action – Editing QML
        1. What just happened?
      3. Property groups
      4. Anchors
      5. Time for action – Positioning items relative to each other
      6. QML types, components, and documents
      7. How does it work?
      8. Time for action – Property binding
      9. A limitation of automatic property updates
    2. Overview of QML types provided by Qt
    3. Qt Quick Designer
      1. Time for action – Adding a form to the project
      2. Form editor files
      3. Form editor interface
      4. Time for action – Adding an import
      5. Time for action – Adding items to the form
      6. Time for action – Editing anchors
      7. Time for action – Applying layouts to the items
      8. Time for action – Assigning an expression to the property
      9. Time for action – Exposing items as properties
        1. What just happened?
      10. Time for action – Creating an event handler
    4. Qt Quick and C++
      1. Accessing C++ objects from QML
      2. Accessing QML objects from C++
    5. Bringing life into static user interfaces
      1. Fluid user interfaces
      2. States and transitions
      3. Time for action – Adding states to the form
      4. Time for action – Adding smooth transition effect
        1. What just happened?
        2. Have a go hero – Adding an animation of the item's position
    6. Pop quiz
    7. Summary
  18. Customization in Qt Quick
    1. Creating a custom QML component
      1. Time for action – Creating a button component
        1. What just happened?
      2. Time for action – Adding button content
        1. What just happened?
      3. Time for action – Sizing the button properly
        1. What just happened?
      4. Time for action – Making the button a reusable component
        1. What just happened?
      5. Importing components
      6. QML and virtual resource paths
    2. Event handlers
      1. Time for action – Making the button clickable
        1. What just happened?
      2. Time for action – Visualizing button states
        1. What just happened?
      3. Time for action – Notifying the environment about button states
        1. What just happened?
      4. Touch input
      5. Time for action – Dragging an item around
        1. What just happened?
      6. Time for action – Rotating and scaling a picture by pinching
        1. What just happened?
        2. Have a go hero – Rotating and scaling with a mouse
      7. Keyboard input
        1. Have a go hero – Practicing key-event propagation
      8. Text input fields
      9. Gamepad input
        1. What just happened?
      10. Sensor input
      11. Detecting device location
    3. Creating advanced QML components
      1. Time for action – A simple analog clock application
        1. What just happened?
      2. Time for action – Adding needles to the clock
        1. What just happened?
      3. Time for action – Making the clock functional
        1. What just happened?
    4. Dynamic and lazy loading of QML objects
      1. Creating objects on request
      2. Delaying item creation
    5. Imperative painting on Canvas using JavaScript
      1. Time for action – Preparing Canvas for heartbeat visualization
        1. What just happened?
      2. Time for action - drawing a heartbeat
        1. What just happened?
      3. Time for action – Hiding properties
      4. Time for action – Making the diagram more colorful
        1. What just happened?
    6. Using C++ classes as QML components
      1. Time for action – Self-updating car dashboard
        1. What just happened?
      2. Time for action – Grouping engine properties
        1. What just happened?
      3. Time for action – Registering C++ class as QML type
      4. Time for action – Making CarInfo instantiable from QML
        1. What just happened?
    7. Pop quiz
    8. Summary
  19. Animations in Qt Quick Games
    1. Animation framework in Qt Quick
      1. Generic animations
      2. Time for action – Scene for an action game
        1. What just happened?
      3. Time for action – Animating the sun's horizontal movement
        1. What just happened?
      4. Composing animations
      5. Time for action – Making the sun rise and set
        1. What just happened?
      6. Non-linear animations
      7. Time for action – Improving the path of the sun
        1. What just happened?
      8. Property value sources
      9. Time for action – Adjusting the sun's color
        1. What just happened?
      10. Time for action – Furnishing sun animation
        1. What just happened?
        2. Have a go hero – Animating the sun's rays
      11. Behaviors
      12. Time for action – Animating the car dashboard
        1. What just happened?
      13. States
      14. Transitions
      15. More animation types
    2. Quick game programming
      1. Game loops
      2. Input processing
      3. Time for action – Character navigation
        1. What just happened?
      4. Time for action – Another approach to character navigation
        1. What just happened?
        2. Have a go hero – Polishing the animation
      5. Time for action – Generating coins
        1. What just happened?
      6. Sprite animation
      7. Time for action – Implementing simple character animation
        1. What just happened?
      8. Time for action – Animating characters using sprites
        1. What just happened?
      9. Time for action – Adding jumping with sprite transitions
        1. What just happened?
        2. Have a go hero – Making Benjamin wiggle his tail in anticipation
      10. Time for action – Revisiting parallax scrolling
        1. What just happened?
        2. Have a go hero – Vertical parallax sliding
      11. Collision detection
      12. Time for action – Collecting coins
        1. What just happened?
      13. Have a go hero – Extending the game
    3. Pop quiz
    4. Summary
  20. Advanced Visual Effects in Qt Quick
    1. Making the game more attractive
      1. Auto-scaling user interfaces
      2. Graphical effects
        1. Have a go hero – The blur parallax scrolled game view
      3. Particle systems
        1. Tuning the emitter
        2. Rendering particles
        3. Making particles move
      4. Time for action – Vanishing coins spawning particles
        1. What just happened?
    2. Custom OpenGL-based Qt Quick items
      1. The scene graph
      2. Time for action – Creating a regular polygon item
        1. What just happened?
        2. Have a go hero – Creating a supporting border for RegularPolygon
    3. Using QPainter interface in Qt Quick
      1. Time for action – Creating an item for drawing outlined text
        1. What just happened?
    4. Pop quiz
    5. Summary
  21. 3D Graphics with Qt
    1. Qt 3D overview
      1. Entities and components
      2. Qt 3D modules
        1. Stable modules
        2. Experimental modules
      3. Using modules
    2. Rendering 3D objects
      1. Mesh, material, and transform
      2. Lighting
      3. Time for action – creating a 3D scene
        1. What just happened?
      4. Time for action – constructing the Tower of Hanoi scene
      5. Time for action – repeating 3D objects
        1. What just happened?
      6. Time for action – creating disks
    3. Handling user input
      1. Devices
      2. Keyboard and mouse buttons
      3. Input chords
      4. Analog (axis) input
      5. Object picker
      6. Frame-based input handling
      7. Time for action – receiving mouse input
        1. What just happened?
    4. Performing animations
      1. Time for action – animating disk movements
        1. What just happened?
      2. Time for action – implementing game logic
        1. Have a go hero – improving the game
    5. Integration with 3D modeling software
      1. Time for action – using OBJ files for the disks
      2. Loading a 3D scene
    6. Working with Qt 3D using C++
      1. Time for action – creating a 3D scene using C++
    7. Integration with Qt Widgets and Qt Quick
      1. Embedding Qt Quick UI into a 3D scene
      2. Embedding a Qt 3D scene into a Qt Quick form
    8. Pop quiz
    9. Summary
  22. Pop quiz answers
  23. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Game Programming using Qt 5 Beginner's Guide - Second Edition
  • Author(s): Pavel Strakhov, Witold Wysota, Lorenz Haas
  • Release date: April 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788399999