logo资料库

Game Programming Using QT.pdf

第1页 / 共512页
第2页 / 共512页
第3页 / 共512页
第4页 / 共512页
第5页 / 共512页
第6页 / 共512页
第7页 / 共512页
第8页 / 共512页
资料共512页,剩余部分请下载后查看
Cover
Copyright
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Table of Contents
Preface
Chapter 1: Introduction to Qt
The cross-platform programming
Qt Platform Abstraction
Supported platforms
A journey through time
New in Qt 5
Restructured codebase
Qt Essentials
Qt Add-ons
Qt Quick 2.0
Meta-objects
C++11 support
Choosing the right license
An open source license
A commercial license
Summary
Chapter 2: Installation
Installing the Qt SDK
Time for action – installing Qt using an online installer
Setting up Qt Creator
Time for action – loading an example project
Time for action – running the Affine Transformations project
Building Qt from sources
Time for action – setting up Qt sources using Git
Time for action – configuring and building Qt
Summary
Chapter 3: Qt GUI Programming
Windows and dialogs
Creating a Qt project
Time for action – creating a Qt Desktop project
Adding child widgets to a window
Managing widget content
Time for action – implementing a tic-tac-toe game board
Qt meta-objects
Signals and slots
Time for action – functionality of a tic-tac-toe board
Properties
Declaring a property
Using a property
Time for action – adding properties to the board class
Designing GUIs
Time for action – designing the game configuration dialog
Time for action – polishing the dialog
Accelerators and label buddies
The tab order
Signals and slots
Using designer forms
Direct approach
The multiple-inheritance approach
The single inheritance approach
Time for action – the logic of the dialog
An application's main window
The Qt resource system
Time for action – the main window of the application
Time for action – adding a pull-down menu
Time for action – creating a toolbar
Time for action – filling in the central widget
Time for action – putting it all together
Summary
Chapter 4: Qt Core Essentials
Text handling
Manipulating strings
Encoding and decoding text
Basic string operations
The string search and lookup
Dissecting strings
Converting between numbers and strings
Using arguments in strings
Regular expressions
Time for action – a simple quiz game
Extracting information out of a string
Finding all pattern occurrences
Data storage
Files and devices
Traversing directories
Getting access to the basic file
Devices
Time for action – implementing a device to encrypt data
Text streams
Data serialization
Binary streams
Time for action – serialization of a custom structure
XML streams
Time for action – implementing an XML parser for player data
JSON files
Time for action – the player data JSON serializer
Time for action – implementing a JSON parser
QSettings
Summary
Chapter 5: Graphics with Qt
Raster painting
Painter attributes
Widget painting
Time for action – custom-painted widgets
Time for action – transforming the viewport
Time for action – drawing an oscillogram
Input events
Time for action – making oscillograms selectable
Working with images
Loading
Modifying
Painting
Painting text
Static text
Rich text
Optimized drawing
Time for action – optimizing oscillogram drawing
Time for action – developing the game architecture
Time for action – implementing the game board class
Time for action – understanding the ChessView class
Time for action – rendering the pieces
Time for action – making the chess game interactive
Time for action – connecting the game algorithm
OpenGL
Introduction to OpenGL with Qt
Time for action – drawing a triangle using Qt and OpenGL
Time for action – scene-based rendering
Time for action – drawing a textured cube
Modern OpenGL with Qt
Shaders
Time for action – shaded objects
GL buffers
Off-screen rendering
Summary
Chapter 6: Graphics View
Graphics View architecture
Items
Parent child relationship
Appearance
Time for action – creating a black, rectangular item
Time for action – reacting to an item's selection state
Time for action – making the item's size definable
Standard items
Coordinate system of the items
Time for action – creating items with different origins
Time for action – rotating an item
Scenes
Adding items to the scene
Time for action – adding an item to a scene
Interacting with items on the scene
Rendering
Time for action – rendering the scene's content to an image
Coordinate system of the scene
Time for action – transforming parent items and child items
View
Time for action – putting it all together!
Showing specific areas of the scene
Transforming the scene
Time for action – creating an item where transformations can easily be seen
Time for action – implementing the ability to scale the scene
Time for action – implementing the ability to move the scene
Time for action – taking the zoom level into account
Questions you should keep in mind
The jumping elephant or how to animate the scene
The game play
The player item
Time for action – creating an item for Benjamin
The playing field
The scene
Time for action – making Benjamin move
Parallax scrolling
Time for action – moving the background
QObject and items
Time for action – using properties, signals, and slots with items
Property animations
Time for action – using animations to move items smoothly
Time for action – keeping multiple animations in sync
Item collision detection
Time for action – making the coins explode
Setting up the playing field
A third way of animation
Widgets inside Graphics View
Optimization
A binary space partition tree
Caching the item's paint function
Optimizing the view
Summary
Chapter 7: Networking
QNetworkAccessManager
Downloading files over HTTP
Time for action – downloading a file
Error handling
Time for action – displaying a proper error message
Downloading files over FTP
Downloading files in parallel
The finished signal
Time for action – writing the OOP conform code using QSignalMapper
The error signal
The readyRead signal
The downloadProgress method
Time for action – showing the download progress
Using a proxy
Connecting to Google, Facebook, Twitter, and co.
Time for action – using Google's Distance Matrix API
Time for action – constructing the query
Time for action – parsing the server's reply
Controlling the connectivity state
QNetworkConfigurationManager
QNetworkConfiguration
QNetworkSession
QNetworkInterface
Communicating between games
Time for action – realizing a simple chat program
The server – QTcpServer
Time for action – setting up the server
Time for action – reacting on a new pending connection
Time for action – forwarding a new message
Time for action – detecting a disconnect
The client
Time for action – setting up the client
Time for action – receiving text messages
Time for action – sending text messages
Improvements
Using UDP
Time for action – sending a text via UDP
Summary
Chapter 8: Scripting
Why script?
The basics of Qt Script
Evaluating JavaScript expressions
Time for action – creating a Qt Script editor
Time for action – sandboxed script evaluation
Integrating Qt and Qt Script
Exposing objects
Time for action – employing scripting for npc AI
Exposing functions
Exposing C++ functions to scripts
Exposing script functions to C++
Time for action – storing the script
Time for action – providing an initialization function
Time for action – implementing the heartbeat event
Using signals and slots in scripts
Creating Qt objects in scripts
Error recovery and debugging
Extensions
The other Qt JavaScript environment
Alternatives to JavaScript
Python
Time for action – writing a Qt wrapper for embedding Python
Time for action – converting data between C++ and Python
Time for action – calling functions and returning values
Summary
Chapter 9: Qt Quick Basics
Fluid user interfaces
Declarative UI programming
Element properties
Group properties
Object hierarchies
Time for action – creating a button component
Time for action – adding button content
Time for action – sizing the button properly
Time for action – making the button a reusable component
Event handlers
Mouse input
Time for action – making the button clickable
Time for action – visualizing button states
Time for action – notifying the environment about button states
Touch input
Time for action – dragging an item around
Time for action – rotating and scaling a picture by pinching
Keyboard input
Using components in Qt Quick
Time for action – a simple analog clock application
Time for action – adding needles to the clock
Time for action – making the clock functional
Dynamic objects
Using components in detail
Creating objects on request
Delaying item creation
Accessing your item's component functionality
Imperative painting
Time for action – preparing Canvas for heartbeat visualization
Time for action – drawing a heartbeat
Time for action – making the diagram more colorful
Qt Quick and C++
Creating QML objects from C++
Pulling QML objects to C++
Pushing C++ objects to QML
Time for action – self-updating car dashboard
Time for action – grouping engine properties
Extending QML
Registering classes as QML elements
Time for action – making CarInfo instantiable from QML
Custom Qt Quick items
OpenGL items
Time for action – creating a regular polygon item
Painted items
Time for action – creating an item for drawing outlined text
Summary
Chapter 10: Qt Quick
Bringing life into static user interfaces
Animating elements
Generic animations
Time for action – scene for an action game
Time for action – animating the sun's horizontal movement
Composing animations
Time for action – making the sun rise and set
Non-linear animations
Time for action – improving the path of the sun
Property value sources
Time for action – adjusting the sun's color
Time for action – furnishing sun animation
Behaviors
Time for action – animating the car dashboard
States and transitions
More animation types
Quick game programming
Game loops
Time for action – character navigation
Time for action – another approach to character navigation
Time for action – generating coins
Sprite animation
Time for action – implementing simple character animation
Time for action – animating characters using sprites
Time for action – adding jumping with sprite transitions
Parallax scrolling
Time for action – revisiting parallax scrolling
Collision detection
Time for action – collecting coins
Notes on collision detection
Eye candy
Auto-scaling user interfaces
Graphical effects
Particle systems
Tuning the emitter
Rendering particles
Making particles move
Time for action – vanishing coins spawning particles
Summary
Appendix: Pop Quiz Answers
Chapter 3, Qt GUI Programming
Chapter 4, Qt Core Essentials
Chapter 6, Graphics View
Chapter 7, Networking
Chapter 8, Scripting
Chapter 11, Miscellaneous and Advanced Concepts
Index
[ 1 ] www.allitebooks.com
Game Programming Using Qt A complete guide to designing and building fun games with Qt and Qt Quick 2 using their associated toolsets Witold Wysota Lorenz Haas BIRMINGHAM - MUMBAI www.allitebooks.com
Game Programming Using Qt Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: January 2016 Production Reference: 1210116 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-887-4 www.packtpub.com www.allitebooks.com
Credits Project Coordinator Sanjeet Rao Proofreader Safis Editing Indexer Monica Ajmera Mehta Graphics Disha Haria Jason Monterio Production Coordinator Conidon Miranda Cover Work Conidon Miranda Authors Witold Wysota Lorenz Haas Reviewers Simone Angeloni Rahul De Pooya Eimandar Shaveen Kumar M. Cihan Özer Acquisition Editor Vinay Argekar Content Development Editor Pooja Mhapsekar Technical Editor Mrunmayee Patil Copy Editor Neha Vyas www.allitebooks.com
About the Authors Witold Wysota is a software architect and developer living in Poland. He started his adventure with Qt in 2004 when he joined QtForum.org and started answering questions about programming with Qt. Shortly afterwards, he became part of the moderator group of the forum. In 2006, together with the moderator team, he established QtCentre.org, which quickly became the largest community-driven support site devoted to Qt. For a number of years, he conducted commercial, academic, and public trainings and workshops and has been giving lectures on Qt, Qt Quick, and related technologies. He is a certified Qt developer and was a member of Qt Education Advisory Board with Nokia, where he helped prepare materials in order to use Qt in educational activities. Witold was also a technical reviewer for Foundations of Qt Development, Johan Thelin, Apress Publishing, a book about Qt 4, and an author of a couple of articles on programming with Qt. In real life, he is a passionate adept of Seven Star Praying Mantis, a traditional style of Chinese martial arts. I would like to thank all the people who have worked on Qt's development over the years for creating such a great programming framework, which was the main force that helped me to shape my programming career. I would also like to thank Lorenz for helping me with the book as well as the team at Packt Publishing for having a magnitude of patience for me during the process of the creation of this book. www.allitebooks.com
Lorenz Haas is a passionate programmer who started his Qt career with Qt 3. Thrilled by Qt's great community, especially the one at QtCentre.org, he immersed himself in this framework, became one of the first certified Qt developers and specialists, and turned his love for Qt into his profession. Lorenz is now working at a medium-sized IT company based in Erlangen, Germany, as a lead software architect. He mainly develops machine controls and their user interfaces as well as general solutions for the industry sector. Additionally, he runs his own small consultancy and programming business called Histomatics (http://www.histomatics.de). A few years ago, he started contributing to Qt Creator. He added a couple of refactoring options that you probably rely on a regular basis if you use Qt Creator. He is also the author of the Beautifier plugin. I would like to thank Witold who guided me through my first steps into the Qt world back in the days and who still assists me with any problems that arise. I am also very grateful to him for taking me on board for this book writing project. He's an excellent teacher and tutor! Secondly, I would like to thank the team at Packt Publishing, who helped and guided us through the entire process of writing this book. www.allitebooks.com
About the Reviewers Simone Angeloni is a software developer and consultant with over a decade of experience in C++ and scripting languages. He is a passionate gamer, but an even more passionate modder and game designer. He is currently working for Crytek GmbH and developing the UI of the free-to-play, award- winning video game Warface. Before this, he was realizing configuration systems for railway signaling and creating standalone applications with Qt. Later, he worked with universities and the National Institute of Nuclear Physics to realize fast data acquisition for particle accelerators. Recently, he founded Clockwise Interactive, a game company that is currently working on the production of its first title. Rahul De is a 23-year-old systems and server-side engineer from Kolkata, India. He recently graduated from the Vellore Institute of Technology with a bachelor of technology degree in computer science and now works with ThoughtWorks. Being a tech and open source enthusiast and a proper "geeks geek", Rahul took up programming at a very young age and quickly matured from developing small-time native applications for desktops to maintaining servers, writing compilers, building IDEs, and enhancing Qt. His latest pet projects involve Medusa—a JIT compiler for Python using Qt, which aims to provide up to a 1500 percent boost for Python projects. Being an avid gamer, he has already dabbled quite a bit with various engines such as Unreal and Cry. He likes to play and develop games in his spare time. www.allitebooks.com
Pooya Eimandar was born on 7th January 1986. He graduated in computer science and has a hardware engineering degree from Shomal University. He is also the author of DirectX 11.1 Game Programming, Packt Publishing. He began his career working on various 3D simulation applications. In 2010, he founded BaziPardaz Game Studio, and since then, he has been leading an open source game engine (https://persianengine.codeplex.com/) at Bazipardaz. He is currently working on a playout and CG editor tool for Alalam News Network. You can find more information about him at http://PooyaEimandar.com/. Shaveen Kumar is a computer scientist and engineer. He graduated from Carnegie Mellon University in 2013 with a master's degree in entertainment technology and is working at Google. He works there as a graphics engineer and technical artist. His main interests are in GPU programming, parallel computing, game engine development, robotics, and computer vision. More information about his work can be found at http://www.shaveenk.com. M. Cihan Özer is a game developer and researcher in computer graphics. He started his career in game development and worked for several mobile and game companies. He got his bachelor's degree from Ankara University, Turkey, and he is currently an MS student at Université de Montréal, Canada. Cihan's work focuses on realistic rendering and interactive techniques. I would like to thank the authors of this book. It will help a lot of people who want to learn Qt and work with it. Also, I would like to thank the great people at Packt Publishing for giving me the opportunity to review this book. www.allitebooks.com
分享到:
收藏