ebook img

Learn Lua for iOS game development PDF

410 Pages·2013·10.705 MB·English
by  VarmaJayant
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn Lua for iOS game development

www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author ��������������������������������������������������������������������������������������������������������������xxv About the Technical Reviewers ��������������������������������������������������������������������������������������xxvii Acknowledgments �����������������������������������������������������������������������������������������������������������xxix ■ Chapter 1: Introduction to Lua �������������������������������������������������������������������������������������������1 ■ Chapter 2: Lua Libraries ��������������������������������������������������������������������������������������������������13 ■ Chapter 3: File Operations �����������������������������������������������������������������������������������������������29 ■ Chapter 4: Math with Lua ������������������������������������������������������������������������������������������������41 ■ Chapter 5: Strings������������������������������������������������������������������������������������������������������������65 ■ Chapter 6: Threading �������������������������������������������������������������������������������������������������������77 ■ Chapter 7: Tips and Tricks �����������������������������������������������������������������������������������������������97 ■ Chapter 8: Corona SDK ��������������������������������������������������������������������������������������������������127 ■ Chapter 9: Gideros Studio ����������������������������������������������������������������������������������������������167 ■ Chapter 10: Moai �����������������������������������������������������������������������������������������������������������203 ■ Chapter 11: LÖVE �����������������������������������������������������������������������������������������������������������245 ■ Chapter 12: Codea ���������������������������������������������������������������������������������������������������������279 v www.it-ebooks.info vi Contents at a Glance ■ Chapter 13: Libraries �����������������������������������������������������������������������������������������������������303 ■ Chapter 14: Third-Party Applications ����������������������������������������������������������������������������317 ■ Chapter 15: Sample Source Code ����������������������������������������������������������������������������������349 Index ���������������������������������������������������������������������������������������������������������������������������������377 www.it-ebooks.info 1 Chapter Introduction to Lua Apple has been issuing checks to developers, and the 2012 figures indicate that it has so far been to the tune of $5 billion. In the past, it used to be desktops with Microsoft-based products that were raking in money for developers, with Visual Basic, or earlier with database products such as dBase and FoxPro. While the major share of this revenue goes to larger companies such as EA, Chillingo, Gameloft, Rovio and even Disney, a lot of indie developers and smaller companies vie for a share of that big pie. Who knows what idea might just become the money-spinner for a developer. Robert Nay, a 14-year-old, made the game Bubble Ball while he was learning to code, and it went viral, with over 8 million downloads. And no one knows what the next top game will be. As a person that has an interest in development, you have made the first step in this journey. You could be a student that has never developed before, or you could be a guru developer who can whip up an enterprise app in minutes. The point is that whatever your background, you are for some reason drawn to this strange-sounding language, Lua (pronounced LOO-ah.). What Is Lua? Lua is a programming language that has a small footprint, works across several platforms, and is quite flexible and extensible. Further, Lua is a game changer for developers that want to write apps for the mobile devices. It has powered a lot of apps and games in the Apple App Store, and it has been spoken about by the late Steve Jobs. It has even been linked with one of the most advanced self-replicating and mutating viruses, the Flame. Despite all of that, Lua remains to be a language that seems more like plain English than a cryptic programmer’s language, making it a language with a smaller learning curve. The History of Lua While knowing the history of Lua will not change anything in terms of making anyone a better programmer, it important to get an idea of why you’d want to use Lua. Lua was created at the Pontifical Catholic University of Rio de Janeiro, Brazil, by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, who were members of the 1 www.it-ebooks.info 2 CHAPTER 1: Introduction to Lua Computer Graphics Technology Group (TeCGraf). Generally, funding for university research is provided by industry, which also expects solutions to some problems that they face. Petrobas, a Brazilian oil company, one of the clients this group was helping them resolve issues related to data entry. Operations of a petroleum company are large, and a large number of data transactions were to be processed on a daily basis. They approached the group to devise a graphical front end that would help eliminate errors in data entry, especially when working with legacy code from fixed-format files. TeCGraf looked at the whole series of screens that were provided to them and tried to find some form of uniformity to help find a solution. For this they came up with a simple and unified data entry language (DEL), which was used to describe the data in each data entry task. I think a good parallel would be that of XML, but without the multitude of tags that make it confusing to understand. That Sol also means sun in Portuguese.) than that of an IDE-type application. The API was implemented as a C library and linked to the main program. Each type could have a callback function that functioned as the constructor (i.e., the function is called when an object of a particular type is created). In 1993, the creators realized that DEL and SOL could both be combined into a single more powerful language. This led to a proper programming language that would have it all: assignment, control structures, subroutines, functions, and so on. However, it would also work with the basic requirement to be able to offer data-description facilities like those of DEL or SOL. They wanted it to be an easy–to-use language without cryptic syntax and semantics, as the end users weren’t expected to be professional programmers. Lastly, they wanted it to be portable to run on any and every platform (if required). Because it was a modified version of SOL, the creators called this new program Lua (which mean moon in Portuguese). Lua Timeline As of this writing, Lua is in version 5.2.1. It has undergone quite a few changes and has been used extensively in many projects for enterprise, entertainment, games, and apps. For many of us, it might come as a surprise that Lua is used in South American homes practically on a daily basis. It powers their interactive televisions. Universities use Lua in their research to allow for quick processing and results. In 1996, Lua got exposure internationally after an article in Dr. Dobbs. Following the article, there were e-mails from developers. In a paper, Roberto narrates about how Bret Mogilefsky, who was www.it-ebooks.info CHAPTER 1: Introduction to Lua 3 the lead programmer at LucasArts (of Grim Fandango fame), wanted to replace their scripting language SCUMM with Lua. This drew interest from other developers, and Lua started to appear on newsgroups. Starting with Lua One important point that I’ll make throughout the book is that, with Lua, it doesn’t matter what framework you use; what’s important is the glue that holds it all together: Lua. In the ’90s, Microsoft was pushing client-server and three-tier architecture (similar to what Apple has been pushing as MVC [Model-View-Controller]). The idea behind MVC is that it involves three distinct portions: a model, which takes care of the data; a view, which displays the data from the model and provides interaction with the user; and a controller, which communicates between the model and the view, as these two do not have any idea of each other’s existence. The controller is the broker that helps the two talk to each other. The most usual way to work with MVC is to use a framework, which takes care of a lot of the details for you. In this book, I shall cover a few frameworks: CoronaSDK, Gideros Studio, Moai, Codea, and LÖVE. All of these except LÖVE help develop and run apps on the iOS platform. The architecture of a Lua-based mobile app is simple, as the diagram in Figure 1-1 shows. Figure 1-1. The architecture of an app using Lua on a mobile device platform An application is made up of all the resources graphics, music, text, etc.), which are compiled into the application, with the Lua code along with the framework stub. When run, the framework or the engine creates an OpenGL surface on which all of the graphics are displayed. This is how all of the frameworks work, and this is how they can offer cross-platform compatibility. The limitations on the framework are imposed mainly due to either the limitations of OpenGL or the framework engine. www.it-ebooks.info 4 CHAPTER 1: Introduction to Lua The MVC pattern discussed earlier holds true in this architecture. If we write our code with this in mind, we can create not only cross-platform applications, but cross-framework ones. The controller code that we write in Lua will change to cater for the other framework, but the rest of the code will be the same. In the next section, we shall focus on Lua and how to use it. After that, we’ll look specifically at the frameworks and integrate some of the things that you’ve learned. Setting Up Lua If we need to work with Lua, we need to have Lua. Since most of the frameworks use Lua 5.1.4, we shall use that version for compatibility. Lua can be obtained as a precompiled binary or source code www.lua.org/demo.html. . You can choose lua5_1_4_Win32_bin.zip or lua_5_1_4_Win64_bin.zip as per your version of Windows. For the Mac, there are versions for Tiger, Leopard, Snow Leopard, and Lion. For *nix, they are based on the kernel version; in this case, it’s easier to download Lua from the app catalog in each of the distributions. iOS There are two apps that I know of that allow for running Lua code interactively. Unlike the other Lua offerings mentioned, these are not free.  iLuaBox: This app uses the newer version of Lua 5.2 and costs about $2.99 in the App Store.  Lua Console: This app works with Lua 5.1.4 and costs $1.99 in the App Store. Of the two, iLuaBox has some advanced functionality in terms of managing files and directories. Features of Lua Lua is implemented as a library written in C. It does not have a main program, as there is no need to invoke something automatically; it works as in embedded mode and calls the embedding program. This code can invoke other functions, assign variables, and read and write the data. Having been written in C, it can also be extended; however, the official release will only add features that have been approved by a committee. www.it-ebooks.info CHAPTER 1: Introduction to Lua 5 Variables In simple computing terminology, a variable is a location that holds a value and can be accessed by giving it a name. Think of it as a filing cabinet in a company’s HR department. It can hold many files with details on the employees. When you need to access data from one of them, you can look it up via the file’s name tag. If you have two employees with the same first and last names, there need to be two files and the name tag would require some form of identification to distinguish between the two. Just as you cannot have two files with the same tag, you cannot have two variables with the same name. There has to be some point of differentiation (e.g., tag1 and tag2 instead of just tag). The names given to the variables can be a series of letters, digits, and underscores; however, they cannot begin with a digit. The names are case sensitive, so there is a difference between T and t. In addition to variables, Lua also uses keywords, which cannot be used for variable names, as Lua identifies them as commands for the code, not variable names. Here’s the list of system keywords, which cannot be used as variable names): and break do else elseif end false for function if in local nil not or repeat return then true until while www.it-ebooks.info 6 CHAPTER 1: Introduction to Lua Hello World, the Variable Way To begin with, we need to start Lua in interactive mode for the purpose of running all of our code. The way it works is to open a terminal in Mac OS X or *nix, type lua, and press Enter. After doing this, you should see the screen shown in Figure 1-2. Under Windows, you can start the Lua console from the Start menu. Note In this book, most of the screenshots and references will be for the Mac OS X version. Figure 1-2. The Lua interactive shell running in the terminal on a Mac The > on the line is the prompt where you can type the Lua code you want to run. We shall start with writing a simple Hello World example. Type the following at the prompt: print ("Hello World") You should see the text “Hello World” printed on the next line. The print function is used to display the text in the terminal. Let's take this a step further: www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.