- Lua Game Development Cookbook
- Mário Ka?uba
- 1746字
- 2021-07-16 13:23:05
Preface
Game development is one of the most complex processes in the world as it requires a wide set of skills such as programming, math, physics, art, sound engineering, management, marketing, and many more. Even with modern technologies, it may take from a few hours to several years to create a game. This depends on the game complexity and tools available.
Computer games are usually based on a mix of simple concepts, which are turned into an enjoyable experience. The first step in making a good game is a game prototype. These can be made with the help of various game engines. However, learning how to use a game engine to the full extent may require you to study how it actually works. This way you have to rely on the available documentation and features that the game engine provides. Many game engines today provide a scripting language as a tool to implement certain game mechanics or to extend the game engine itself with new features.
The Lua programming language is gaining popularity in the game industry mainly due to its simplicity and efficiency. Most of the time, it's used only for simple tasks such as NPC dialogs, user interface, or custom game events. However, with additional Lua modules, you can create your own full-fledged game engine that can use almost all the capabilities of the modern computer hardware.
In this book, you'll find a set of recipes with solutions to the most common problems you may encounter while creating games with the Lua language.
The best way to learn something is to play with it. Therefore, each recipe is paired with simple demo applications that will help you understand the topic covered. You may even use these demo samples to create your own game prototype in no time.
All sample applications are available in the digital content of this book.
What this book covers
Chapter 1, Basics of the Game Engine, covers important algorithms and the basic design of a game engine written in the Lua programming language, as well as LuaSDL multimedia module preparation, which is the main part of all the recipes in this book.
Chapter 2, Events, deals with handling input events that are an important part of any game engine.
Chapter 3, Graphics – Common Methods, contains basic concepts used in computer graphics. You'll learn how to initialize the graphics mode, use basic OpenGL functions, load images, create textures, and draw text on the screen.
Chapter 4, Graphics – Legacy Method with OpenGL 1.x-2.1, explains how to use the intermediate mode of OpenGL, which is intended for use on older GPUs. Even when this mode is currently deprecated, it holds important information that is vital when using modern versions of OpenGL. It may be used as a precursor to more advanced topics in Chapter 5, Graphics – Modern Method with OpenGL 3.0+.
Chapter 5, Graphics – Modern Method with OpenGL 3.0+, covers the basics of using the GLSL shading language with the Lua language to draw various scenes. You'll also learn how to use per-pixel lighting, render into textures and apply surface effects with normal maps.
Chapter 6, The User Interface, covers the implementation of the custom user interface from simple windows to window controls.
Chapter 7, Physics and Game Mechanics, explains how to prepare and use the LuaBox2D module with the Lua language for physics simulation. The Box2D library is quite popular in modern side-scrolling games mainly because it offers great flexibility.
Chapter 8, Artificial Intelligence, deals with pathfinding algorithms and fuzzy logic. You'll learn how pathfinding works in games with simple maze or even tiled environments. More advanced topics cover decision making with fuzzy logic. In combination with pathfinding algorithms, you can create intelligent game opponents that won't jump into a lava lake at the first opportunity.
Chapter 9, Sounds and Networking, covers how to initialize the sound card, play sounds, and music. The second part covers network communication with the high-performance ZeroMQ library. It contains many improvements over traditional socket communication and it's used by companies such as AT&T, Cisco, EA, Zynga, Spotify, NASA, Microsoft, and CERN.
What you need for this book
Sample demonstrations for recipes require the Microsofts Windows or Linux operating systems. Unfortunately, Mac OS is not currently supported.
If you intend to build binary Lua modules from this book, you'll need the C/C++ compiler along with the recent version of the CMake building system. Additionally, Linux users will need to install development packages for the XOrg display server in order to include the graphical output.
However, it's not necessary to do so as binary Lua modules are included in code files for this book.
The recipes in Chapter 5, Graphics – Modern Method with OpenGL 3.0+, require a graphic card released after 2010 with support for OpenGL 3.3.
Who this book is for
This book is for all programmers and game enthusiasts who want to stop dreaming about creating a game, and actually create one from scratch.
The reader should know the basics of programming and using the Lua language. Knowledge of the C/C++ programming language is not necessary, but it's strongly recommended in order to write custom Lua modules extending game engine capabilities or to rewrite parts of the Lua code into a more efficient form.
Algebra and matrix operations are required in order to understand advanced topics in Chapter 4, Graphics – Legacy Method with OpenGL 1.x-2.1 and Chapter 5, Graphics – Modern Method with OpenGL 3.0+.
Sample demonstrations are coupled with binary libraries for Windows and Linux operating systems for convenience.
Sections
In this book, you will find several headings that appear frequently (Getting ready, How to do it, How it works, There's more, and See also).
To give clear instructions on how to complete a recipe, we use these sections as follows:
Getting ready
This section tells you what to expect in the recipe, and describes how to set up any software or any preliminary settings required for the recipe.
How to do it…
This section contains the steps required to follow the recipe.
How it works…
This section usually consists of a detailed explanation of what happened in the previous section.
There's more…
This section consists of additional information about the recipe in order to make the reader more knowledgeable about the recipe.
See also
This section provides helpful links to other useful information for the recipe.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include
directive."
A block of code is set as follows:
local sum_of_numbers = 0 local iterations = 3 for i=1,iterations do sum_of_numbers = sum_of_numbers + 1/iterations print(("%f"):format(sum_of_numbers)) end -- is the result equal to 1? print("Sum equals to 1?", sum_of_numbers == 1)
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
gl.Begin(gl_enum.GL_LINES)
-- A
gl.Color4f(1, 0, 0, 1)
gl.Vertex3f(-0.5, -0.5, 0)
-- B
gl.Color4f(0, 1, 0, 1)
gl.Vertex3f(0.5, -0.5, 0)
-- C
gl.Color4f(0, 0, 1, 1)
gl.Vertex3f(0.5, 0.5, 0)
-- D
gl.Color4f(1, 1, 0, 1)
gl.Vertex3f(-0.5, 0.5, 0)
gl.End()
Any command-line input or output is written as follows:
mkdir luagl/build cd luagl/build cmake ..
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "You can validate settings by pressing the Configure button"
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>
, and we will do our best to address the problem.