Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Lua Game Development Cookbook
Lua Game Development Cookbook

Lua Game Development Cookbook: Over 70 recipes that will help you master the elements and best practices required to build a modern game engine using Lua

eBook
₹799.99 ₹3276.99
Paperback
₹4096.99
Subscription
Free Trial
Renews at ₹800p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Lua Game Development Cookbook

Chapter 2. Events

In this chapter, we will cover the following recipes:

  • Processing input events with LuaSDL
  • Using the keyboard input
  • Using the relative mouse position
  • Using the absolute mouse position
  • Using timers

Introduction

Event-driven design offers a cheap and efficient way to detect user input without the need to check for input device status in each frame. A naïve approach to this is to query all input devices for changes in their state. There are many types of input devices such as keyboard, mouse, joystick, keypad controller, tablet, touch screen, and so on. LuaSDL relies on the library libSDL 1.2, which supports only basic input devices such as keyboard, mouse, and joystick. This version doesn't support the use of multiple devices of the same kind. This limitation has been removed with libSDL 2.x, which is used in LuaSDL 2. LuaSDL 2 is the successor of a former LuaSDL library and it's in the stage of early development at the time of writing. This chapter will cover the use of an older LuaSDL library, as the LuaSDL 2 interface is not so different.

Processing input events with LuaSDL

LuaSDL offers a form of platform-independent abstraction layer to these devices with an inner event pool. You only have to query the event pool for unprocessed events and, if there are any, check for the event type.

Getting ready

Before doing any event processing, your application must initialize internal event pools. This can be achieved with the SDL.SDL_Init function, where the only parameter is a bitmask representing which parts of LuaSDL you want to initialize. You can use the bitlib library for the Lua language. Another option would be to use the bit32 internal library if you are using the newer version of the Lua interpreter. The default value here is SDL.SDL_INIT_EVERYTHING, which is fine as it starts the event pool automatically. Specifically, you can use the SDL.SDL_INIT_EVENTTHREAD or SDL.SDL_INIT_VIDEO values to initialize the event pool.

A code sample can be used to initialize the LuaSDL library. It should be used right at the start of the application...

Using the keyboard input

LuaSDL offers a simple way of determining what key was pressed or released on your keyboard. Events with event types SDL.SDL_KEYDOWN and SDL.SDL_KEYUP can react to one keystroke at the time. This is usually fine during a game play. However, if you want to use keyboard shortcuts in a text input field, the previous approach would not be very efficient.

This recipe will show you how to manage keyboard input in a robust way that can be used for both situations—to control a game or to write text into an input field.

Getting ready

Let's say you need your game character to run when a Shift key is pressed. There are three key problems. The common PC keyboard has left and right Shift keys. These are two different keys with two different key symbol codes. The next thing is that you use these keys with another keyboard key which may or may not be the modifier key. The last problem is putting key states together, so you'll know if the player has pressed the Shift...

Using the relative mouse position

The relative mouse position is often used when you need unconstrained mouse movement. A typical example of such a use is a first person shooter game in a 3D environment. The relative mouse position represents how much the mouse pointer position changed in comparison with the previous state in all the axes.

Getting ready

The biggest problem with the relative mouse position is that the mouse pointer is constrained to the application window or the screen. You can solve this by centering the mouse cursor in the center of the application window after computing the relative cursor position or by using direct values from the mouse driver.

The relative mouse position has the big advantage of versatility because you can apply the mouse cursor speed modifier simply by multiplying the relative mouse position with a number. If that number is greater than 1, the mouse cursor will move faster. Multiplying by a number lesser than 1, will slow down the mouse cursor.

How to...

Using the absolute mouse position

The absolute mouse position is used primarily in window applications, where the mouse position constraints are desirable. The mouse position (0,0) corresponds to the upper-left corner of your application window. The maximum mouse position depends always on the size of the window. Take special care when the mouse cursor is outside the application window. The behavior of LuaSDL in this situation is highly dependent on the currently used operating system! In most cases, you won't get any events related to the mouse cursor motion.

The main advantage of this is that you can use the mouse cursor position reported directly by LuaSDL to precisely manipulate GUI elements inside of the application window. This approach is used also with tablet touch input devices, where you always get absolute positions.

How to do it…

The following mouse movement handler function shows a simple way to get the absolute mouse cursor position:

[SDL.SDL_MOUSEMOTION] = function...

Using timers

LuaSDL offers support for timer objects. The problematic part is the use of timers. The LibSDL library uses callback functions to call event functions. These callbacks run in another thread and the naïve approach, where you put the Lua function in the position of callback function, would lead to Lua state corruption. There is a better way to accomplish this by using the internal LuaSDL callback function that invokes a special user event.

Timers aren't very precise and they are mostly used in GUI updates. If you need more precision, you'll need to use High Precision Event Timer (HPET), which is out of the scope of this book.

Getting ready

Each timer object uses a user-defined event that contains unique timer function identifiers represented by integer values. LuaSDL offers a slightly modified version of the SDL.SDL_AddTimer function, where it accepts two parameters instead of three. The first parameter is an interval value in milliseconds. The second is the user...

Left arrow icon Right arrow icon

Description

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.

Who is this book 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.

What you will learn

  • Set up OpenGL graphics along with GLSL shaders
  • Use lighting and graphical effects
  • Create animated game characters using Box2D library
  • Load and use textures, fonts, and 3D models
  • Design and implement a graphical user interface
  • Integrate simple Artificial Intelligence for pathfinding
  • Implement networking support
  • Use data structures in programming
Estimated delivery fee Deliver to India

Premium delivery 5 - 8 business days

₹630.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jul 28, 2015
Length: 360 pages
Edition : 1st
Language : English
ISBN-13 : 9781849515504
Languages :
Concepts :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to India

Premium delivery 5 - 8 business days

₹630.95
(Includes tracking information)

Product Details

Publication date : Jul 28, 2015
Length: 360 pages
Edition : 1st
Language : English
ISBN-13 : 9781849515504
Languages :
Concepts :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
₹800 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
₹4500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts
₹5000 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just ₹400 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 10,204.97
L÷VE for Lua Game Programming
₹2457.99
Learning game AI programming with Lua
₹3649.99
Lua Game Development Cookbook
₹4096.99
Total 10,204.97 Stars icon

Table of Contents

10 Chapters
1. Basics of the Game Engine Chevron down icon Chevron up icon
2. Events Chevron down icon Chevron up icon
3. Graphics – Common Methods Chevron down icon Chevron up icon
4. Graphics – Legacy Method with OpenGL 1.x–2.1 Chevron down icon Chevron up icon
5. Graphics – Modern Method with OpenGL 3.0+ Chevron down icon Chevron up icon
6. The User Interface Chevron down icon Chevron up icon
7. Physics and Game Mechanics Chevron down icon Chevron up icon
8. Artificial Intelligence Chevron down icon Chevron up icon
9. Sounds and Networking Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.3
(6 Ratings)
5 star 83.3%
4 star 0%
3 star 0%
2 star 0%
1 star 16.7%
Filter icon Filter
Top Reviews

Filter reviews by




CPallini Oct 01, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book covers a very appealing topic: game development with Lua.Leveraging on libSDL, OpenGL and several other libraries, illustrates, in the formof recipes, many techniques required to build yourself a game, game engine orprototype.Since the topic is wide and deep, it is a condensed book, rich of content inrelatively few pages: from directory organization, module and error handling, Luainteraction with C++, to game UI, physics, AI, sound and network communication,passing for the event driven SDL game loop, OpenGL (both the older immediate mode and the moremodern shading language).I found most of the content interesting, some recipes a bit difficult tounderstand at first (in my opinion delaying the explanation in 'how it works'section is a bit confusing), neverhtless worthy to be studied.A relatively good knowledge of Lua is required to fully understand the recipes(while proficience with modern C++ would help for actual game development).The approach is pragmatic and you have to use the companion code in order toappreciate the book and start experimenting (I should say compiling myself therequired libraries was not exactly 'a breeze' like the author claims in the book).All in all it is a solid, pragmatic, fairly advanced book: enough to make youappreciate the efforts required in game development and possibly desire to deepenyour knowledge about the related problems and techniques.
Amazon Verified review Amazon
Julie Blanchard Feb 07, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Bought for my sons Christmas presentHe loves it
Amazon Verified review Amazon
Dan Kosko Nov 27, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
quick delivery. just as described. good job
Amazon Verified review Amazon
Kiffin Gish Oct 27, 2015
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you've ever fantasized about creating your own fancy game but never was able to get beyond the basics, then this is the right place to begin. Starting with the first chapters, you become more familiar with the Lua programming language and what goes into a powerful gaming engine. Slowly but surely, your learn how Lua provides the ideal playground to build up a solid platform of modules and components that can be combined nicely. It's not just about the complex concepts that go into designing games but also the choice of the right programming language and how to use it effectively. By the end of the book you start mastering the more difficult concepts and can deal with all the fancy algorithms that go into good game making.
Amazon Verified review Amazon
Joel A. Sundquist Apr 19, 2016
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great lua book. Even if you are not developing games it has some helpful performance tips
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela