Game Programming Using Qt

Book description

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

About This Book

  • Learn to create simple 2D to complex 3D graphics and games using all possible tools and widgets available for game development in Qt
  • Understand technologies such as QML, Qt Quick, OpenGL, and Qt Creator, and learn the best practices to use them to design games
  • Learn Qt with the help of many sample games introduced step-by-step in each chapter

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. Any previous knowledge of Qt is not required, however knowledge of C++ is mandatory.

What You Will Learn

  • Install 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's Network module
  • Script your game with Qt Script
  • Program resolution-independent and fluid UI using QML and Qt Quick
  • Control your game flow as per the sensors of a mobile device
  • See how to 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 more popular by the day, especially on mobile and embedded devices. Despite its simplicity, it's a powerful tool that perfectly fits game developers' needs. Using Qt and Qt Quick, it is easy to build fun games or shiny user interfaces. You only need to create your game once and deploy it on all major platforms like iOS, Android, and WinRT without changing a single source file.

The book begins with a brief introduction to creating an application and preparing a working environment for both desktop and mobile platforms. It then dives deeper into the basics of creating graphical interfaces and Qt core concepts of data processing and display before you try creating a game. As you progress through the chapters, you'll learn to enrich your games by implementing network connectivity and employing scripting. We then delve into Qt Quick, OpenGL, and various other tools to add game logic, design animation, add game physics, and build astonishing UI for the games. Towards the final chapters, you'll learn to exploit mobile device features such as accelerators and sensors to build engaging user experiences. If you are planning to learn about Qt and its associated toolsets to build apps and games, this book is a must have.

Style and approach

This is an easy-to-follow, example-based, comprehensive introduction to all the major features in Qt. The content of each chapter is explained and organized around one or multiple simple game examples to learn Qt in a fun way.

Table of contents

  1. Game Programming Using Qt
    1. Table of Contents
    2. Game Programming Using Qt
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Sections
      5. Time for action – heading
        1. What just happened?
        2. Pop quiz – heading
        3. Have a go hero – heading
      6. Conventions
      7. Reader feedback
      8. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Introduction to Qt
      1. The cross-platform programming
        1. What just happened?
        2. Qt Platform Abstraction
        3. Supported platforms
      2. A journey through time
      3. New in Qt 5
        1. Restructured codebase
          1. Qt Essentials
          2. Qt Add-ons
        2. Qt Quick 2.0
        3. Meta-objects
        4. C++11 support
      4. Choosing the right license
        1. An open source license
        2. A commercial license
      5. Summary
    9. 2. Installation
      1. Installing the Qt SDK
      2. Time for action – installing Qt using an online installer
        1. What just happened?
      3. Setting up Qt Creator
      4. Time for action – loading an example project
        1. What just happened?
      5. Time for action – running the Affine Transformations project
        1. What just happened?
      6. Building Qt from sources
      7. Time for action – setting up Qt sources using Git
        1. What just happened?
      8. Time for action – configuring and building Qt
        1. What just happened?
      9. Summary
    10. 3. Qt GUI Programming
      1. Windows and dialogs
        1. Creating a Qt project
      2. Time for action – creating a Qt Desktop project
        1. What just happened?
        2. Adding child widgets to a window
          1. Managing widget content
      3. Time for action – implementing a tic-tac-toe game board
        1. What just happened?
      4. Qt meta-objects
        1. Signals and slots
        2. Pop quiz – making signal-slot connections
      5. Time for action – functionality of a tic-tac-toe board
        1. Properties
          1. Declaring a property
          2. Using a property
      6. Time for action – adding properties to the board class
        1. What just happened?
      7. Designing GUIs
      8. Time for action – designing the game configuration dialog
        1. What just happened?
      9. Time for action – polishing the dialog
        1. Accelerators and label buddies
        2. The tab order
        3. Signals and slots
        4. What just happened?
        5. Using designer forms
          1. Direct approach
          2. The multiple-inheritance approach
          3. The single inheritance approach
      10. Time for action – the logic of the dialog
        1. An application's main window
          1. The Qt resource system
      11. Time for action – the main window of the application
        1. What just happened?
      12. Time for action – adding a pull-down menu
        1. What just happened?
      13. Time for action – creating a toolbar
        1. What just happened?
      14. Time for action – filling in the central widget
        1. What just happened?
      15. Time for action – putting it all together
        1. What just happened?
        2. Have a go hero – extending the game
        3. Pop quiz – using widgets
      16. Summary
    11. 4. Qt Core Essentials
      1. Text handling
        1. Manipulating strings
          1. Encoding and decoding text
          2. Basic string operations
          3. The string search and lookup
          4. Dissecting strings
          5. Converting between numbers and strings
          6. Using arguments in strings
        2. Regular expressions
      2. Time for action – a simple quiz game
        1. What just happened?
        2. Extracting information out of a string
        3. Finding all pattern occurrences
      3. Data storage
        1. Files and devices
          1. Traversing directories
          2. Getting access to the basic file
          3. Devices
      4. Time for action – implementing a device to encrypt data
        1. What just happened?
        2. Have a go hero – a GUI for the Caesar cipher
        3. Text streams
        4. Data serialization
          1. Binary streams
      5. Time for action – serialization of a custom structure
        1. What just happened?
        2. XML streams
      6. Time for action – implementing an XML parser for player data
        1. What just happened?
        2. Have a go hero – an XML serializer for player data
        3. JSON files
      7. Time for action – the player data JSON serializer
      8. Time for action – implementing a JSON parser
        1. What just happened?
        2. QSettings
        3. Pop quiz – Qt core essentials
      9. Summary
    12. 5. Graphics with Qt
      1. Raster painting
        1. Painter attributes
        2. Widget painting
      2. Time for action – custom-painted widgets
        1. What just happened?
      3. Time for action – transforming the viewport
        1. What just happened?
      4. Time for action – drawing an oscillogram
        1. Input events
      5. Time for action – making oscillograms selectable
        1. Have a go hero – reacting only to the left mouse button
        2. Working with images
          1. Loading
          2. Modifying
          3. Painting
        3. Painting text
          1. Static text
          2. Rich text
        4. Optimized drawing
      6. Time for action – optimizing oscillogram drawing
        1. What just happened?
        2. Have a go hero – implementing a double-buffered oscillogram
      7. Time for action – developing the game architecture
        1. What just happened?
      8. Time for action – implementing the game board class
        1. What just happened?
      9. Time for action – understanding the ChessView class
        1. What just happened?
      10. Time for action – rendering the pieces
        1. What just happened?
      11. Time for action – making the chess game interactive
        1. What just happened?
      12. Time for action – connecting the game algorithm
        1. What just happened?
        2. Have a go hero – implementing the UI around the chess board
        3. Have a go hero – connecting a UCI-compliant chess engine
      13. OpenGL
        1. Introduction to OpenGL with Qt
      14. Time for action – drawing a triangle using Qt and OpenGL
      15. Time for action – scene-based rendering
        1. What just happened?
      16. Time for action – drawing a textured cube
        1. Have a go hero – animating a cube
        2. Modern OpenGL with Qt
          1. Shaders
      17. Time for action – shaded objects
        1. GL buffers
        2. Off-screen rendering
      18. Summary
    13. 6. Graphics View
      1. Graphics View architecture
        1. Items
          1. Parent child relationship
          2. Appearance
      2. Time for action – creating a black, rectangular item
        1. What just happened?
      3. Time for action – reacting to an item's selection state
        1. What just happened?
      4. Time for action – making the item's size definable
        1. What just happened?
        2. Have a go hero – customizing the item
        3. Standard items
        4. Coordinate system of the items
      5. Time for action – creating items with different origins
        1. What just happened?
      6. Time for action – rotating an item
        1. What just happened?
        2. Have a go hero – applying multiple transformations
        3. Scenes
          1. Adding items to the scene
      7. Time for action – adding an item to a scene
        1. What just happened?
        2. Interacting with items on the scene
        3. Rendering
      8. Time for action – rendering the scene's content to an image
        1. What just happened?
        2. Have a go hero – rendering only specific parts of a scene
        3. Coordinate system of the scene
      9. Time for action – transforming parent items and child items
        1. What just happened?
        2. Have a go hero – playing with the z value
        3. View
      10. Time for action – putting it all together!
        1. What just happened?
        2. Showing specific areas of the scene
        3. Transforming the scene
      11. Time for action – creating an item where transformations can easily be seen
        1. What just happened?
      12. Time for action – implementing the ability to scale the scene
        1. What just happened?
      13. Time for action – implementing the ability to move the scene
        1. What just happened?
      14. Time for action – taking the zoom level into account
        1. What just happened?
        2. Questions you should keep in mind
      15. The jumping elephant or how to animate the scene
        1. The game play
        2. The player item
      16. Time for action – creating an item for Benjamin
        1. What just happened?
        2. The playing field
        3. The scene
      17. Time for action – making Benjamin move
        1. What just happened?
        2. Parallax scrolling
      18. Time for action – moving the background
        1. What just happened?
        2. Have a go hero – adding new background layers
        3. QObject and items
      19. Time for action – using properties, signals, and slots with items
        1. Property animations
      20. Time for action – using animations to move items smoothly
        1. What just happened?
        2. Have a go hero – letting the scene handle Benjamin's jump
      21. Time for action – keeping multiple animations in sync
        1. What just happened?
        2. Item collision detection
      22. Time for action – making the coins explode
        1. What just happened?
        2. Setting up the playing field
        3. Have a go hero – extending the game
        4. A third way of animation
      23. Widgets inside Graphics View
      24. Optimization
        1. A binary space partition tree
        2. Caching the item's paint function
        3. Optimizing the view
        4. Pop quiz – mastering Graphics View
      25. Summary
    14. 7. Networking
      1. QNetworkAccessManager
        1. Downloading files over HTTP
      2. Time for action – downloading a file
        1. Have a go hero – extending the basic file downloader
        2. Error handling
      3. Time for action – displaying a proper error message
        1. Downloading files over FTP
        2. Downloading files in parallel
          1. The finished signal
      4. Time for action – writing the OOP conform code using QSignalMapper
        1. What just happened?
        2. The error signal
        3. The readyRead signal
        4. The downloadProgress method
      5. Time for action – showing the download progress
        1. What just happened?
        2. Using a proxy
      6. Connecting to Google, Facebook, Twitter, and co.
      7. Time for action – using Google's Distance Matrix API
      8. Time for action – constructing the query
      9. Time for action – parsing the server's reply
        1. Have a go hero – choosing XML as the reply's format
      10. Controlling the connectivity state
        1. QNetworkConfigurationManager
        2. QNetworkConfiguration
        3. QNetworkSession
        4. QNetworkInterface
      11. Communicating between games
      12. Time for action – realizing a simple chat program
      13. The server – QTcpServer
      14. Time for action – setting up the server
        1. What just happened?
      15. Time for action – reacting on a new pending connection
        1. What just happened?
      16. Time for action – forwarding a new message
        1. Have a go hero – using QSignalMapper
      17. Time for action – detecting a disconnect
        1. What just happened?
      18. The client
      19. Time for action – setting up the client
        1. What just happened?
      20. Time for action – receiving text messages
      21. Time for action – sending text messages
        1. Have a go hero – extending the chat with a user list
      22. Improvements
      23. Using UDP
      24. Time for action – sending a text via UDP
        1. Have a go hero – connecting players of the Benjamin game
        2. Pop quiz – test your knowledge
      25. Summary
    15. 8. Scripting
      1. Why script?
      2. The basics of Qt Script
        1. Evaluating JavaScript expressions
      3. Time for action – creating a Qt Script editor
        1. What just happened?
      4. Time for action – sandboxed script evaluation
        1. What just happened?
      5. Integrating Qt and Qt Script
        1. Exposing objects
      6. Time for action – employing scripting for npc AI
        1. What just happened?
        2. Have a go hero – extending the Dungeons & Dragons game
        3. Exposing functions
          1. Exposing C++ functions to scripts
          2. Exposing script functions to C++
      7. Time for action – storing the script
        1. What just happened?
      8. Time for action – providing an initialization function
        1. What just happened?
      9. Time for action – implementing the heartbeat event
        1. What just happened?
        2. Have a go hero – defending against attacks
        3. Using signals and slots in scripts
        4. Have a go hero – triggering defense using signals and slots
        5. Creating Qt objects in scripts
        6. Error recovery and debugging
        7. Extensions
        8. The other Qt JavaScript environment
      10. Alternatives to JavaScript
        1. Python
      11. Time for action – writing a Qt wrapper for embedding Python
        1. What just happened?
      12. Time for action – converting data between C++ and Python
        1. What just happened?
        2. Have a go hero – implementing the remaining conversions
      13. Time for action – calling functions and returning values
        1. What just happened?
        2. Have a go hero – wrapping Qt objects into Python objects
        3. Pop quiz – scripting
      14. Summary
    16. 9. Qt Quick Basics
      1. Fluid user interfaces
      2. Declarative UI programming
        1. Element properties
        2. Group properties
        3. Object hierarchies
      3. Time for action – creating a button component
        1. What just happened?
      4. Time for action – adding button content
        1. What just happened?
      5. Time for action – sizing the button properly
        1. What just happened?
      6. Time for action – making the button a reusable component
        1. What just happened?
      7. Event handlers
        1. Mouse input
      8. Time for action – making the button clickable
        1. What just happened?
      9. Time for action – visualizing button states
        1. What just happened?
      10. Time for action – notifying the environment about button states
        1. What just happened?
        2. Touch input
      11. Time for action – dragging an item around
        1. What just happened?
      12. 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
        3. Keyboard input
        4. Have a go hero – practicing key-event propagation
      13. Using components in Qt Quick
      14. Time for action – a simple analog clock application
        1. What just happened?
      15. Time for action – adding needles to the clock
        1. What just happened?
      16. Time for action – making the clock functional
        1. What just happened?
        2. Dynamic objects
        3. Using components in detail
        4. Creating objects on request
        5. Delaying item creation
        6. Accessing your item's component functionality
        7. Imperative painting
      17. Time for action – preparing Canvas for heartbeat visualization
        1. What just happened?
      18. Time for action – drawing a heartbeat
        1. What just happened?
      19. Time for action – making the diagram more colorful
        1. What just happened?
      20. Qt Quick and C++
        1. Creating QML objects from C++
        2. Pulling QML objects to C++
        3. Pushing C++ objects to QML
      21. Time for action – self-updating car dashboard
        1. What just happened?
      22. Time for action – grouping engine properties
        1. What just happened?
      23. Extending QML
        1. Registering classes as QML elements
      24. Time for action – making CarInfo instantiable from QML
        1. What just happened?
        2. Custom Qt Quick items
          1. OpenGL items
      25. Time for action – creating a regular polygon item
        1. What just happened?
        2. Have a go hero – creating a supporting border for RegularPolygon
        3. Painted items
      26. Time for action – creating an item for drawing outlined text
        1. What just happened?
      27. Summary
    17. 10. Qt Quick
      1. Bringing life into static user interfaces
        1. Animating elements
          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?
        2. Composing animations
      4. Time for action – making the sun rise and set
        1. What just happened?
        2. Non-linear animations
      5. Time for action – improving the path of the sun
        1. What just happened?
        2. Property value sources
      6. Time for action – adjusting the sun's color
        1. What just happened?
      7. Time for action – furnishing sun animation
        1. What just happened?
        2. Have a go hero – animating the sun's rays
        3. Behaviors
      8. Time for action – animating the car dashboard
        1. What just happened?
        2. States and transitions
          1. More animation types
      9. Quick game programming
        1. Game loops
      10. Time for action – character navigation
        1. What just happened?
      11. Time for action – another approach to character navigation
        1. What just happened?
        2. Have a go hero – polishing the animation
      12. Time for action – generating coins
        1. What just happened?
        2. Sprite animation
      13. Time for action – implementing simple character animation
        1. What just happened?
      14. Time for action – animating characters using sprites
        1. What just happened?
      15. Time for action – adding jumping with sprite transitions
        1. What just happened?
        2. Have a go hero – making Benjamin wiggle his tail in anticipation
        3. Parallax scrolling
      16. Time for action – revisiting parallax scrolling
        1. What just happened?
        2. Have a go hero – vertical parallax sliding
        3. Collision detection
      17. Time for action – collecting coins
        1. What just happened?
        2. Notes on collision detection
        3. Eye candy
          1. Auto-scaling user interfaces
          2. Graphical effects
        4. Have a go hero – the blur parallax scrolled game view
          1. Particle systems
          2. Tuning the emitter
          3. Rendering particles
          4. Making particles move
      18. Time for action – vanishing coins spawning particles
        1. What just happened?
      19. Summary
    18. A. Pop Quiz Answers
      1. Chapter 3, Qt GUI Programming
        1. Pop quiz – making signal-slot connections
        2. Pop quiz – using widgets
      2. Chapter 4, Qt Core Essentials
        1. Pop quiz – Qt core essentials
      3. Chapter 6, Graphics View
        1. Pop quiz – mastering Graphics View
      4. Chapter 7, Networking
        1. Pop quiz – testing your knowledge
      5. Chapter 8, Scripting
        1. Pop quiz – scripting
      6. Chapter 11, Miscellaneous and Advanced Concepts
        1. Pop quiz – testing your knowledge
    19. Index

Product information

  • Title: Game Programming Using Qt
  • Author(s): Witold Wysota, Lorenz Haas
  • Release date: January 2016
  • Publisher(s): Packt Publishing
  • ISBN: 9781782168874