Title: PDA programming Computer Games Software Engineering
1PDA programming Computer Games Software
Engineering
- A large part of this module will be based on
programming PDAs running the Pocket PC Operating
System. - Well be using Microsofts eMbedded Visual C to
do this. For many, C itself is a new concept.
Be aware that Visual C programming is in itself
a tricky skill. - It is a skill though that is in high demand by
industry. - Today well start looking at C programming in
general and at Pocket PC programming with Visual
C in particular. - In the first tutorial (practical) for the module
you will develop (well, type in) and build a
Hello World application for the PDA using eVC.
2C and JavaComputer Games Software Engineering
- C was developed from its predecessor C, by
Bjarne Stroustrup, of Bell Labs, in the 1980s. - For a long time, C was the most widely taught
computer programming language in Universities. - This was probably due to the fact that it is
object-oriented as well as being widely used in
industry. The problem with C is that is
arguably the most complex language ever to be
widely used. - In the late 1990s Java came along. Java seems to
have all the good parts of C and yet it is a
relatively easy language to learn and develop
applications with. - However, many areas of industry have been slow to
take up Java and many still demand C skills.
3Problems with C/C Computer Games Software
Engineering
- There are many that advocate that C (and
certainly C) has no place in modern software
development. - Mostly, this is on grounds of security and
reliability. - Many security vulnerabilities in server based
systems are only possible in languages such as C
and C. - Reliability is also a problem. Pointers are hard
to get right and can lead to buggy code. Error
handling is not easy to do, especially in C. The
manual freeing of memory when not done right can
lead to problematic memory leaks. - Modern languages such as Java fix many of these
problems with techniques such as exception
handling, garbage collection, no pointers,
sandboxing and automatic type checking.
4Hang on! What's a pointer? Computer Games
Software Engineering
- Much of the criticism levelled at C/C regards
its use of pointers. Pointers dont exist in Java
so you may have never encountered them before. - A pointer is simply a variable that holds the
address of a memory location. - Pointers enable the programmer to access and
manipulate data, data-structures, raw memory, and
hardware devices in ways not possible in other
languages. - They are also useful for passing parameters into
functions in a manner that allows a function to
modify and return values to the calling routine
(well use this!). - When used incorrectly, they also are a frequent
source of both bugs and programmer frustration.
5Why havent C/C been killed off Computer Games
Software Engineering
- If C and C are so nasty why havent they died
a death yet? Lots of people (including some in
the School of Computing at Napier) want to see
C die. - This is the wrong attitude. So long as Microsoft
continue to develop their OSs in C they will
expect third party developers to develop in C
as well. - If you hate Microsoft, well thats no excuse
either, because all UNIX implementations
(including Linux, MacOS) are still written in C
(and largely even in C) as well. - You wont find many OSs written in Java. And you
wont find many large, safety critical
(aerospace, defence, transport) applications
written in Java either. And neither will you find
many Java-based embedded systems (yet).
6Developing in C Computer Games Software
Engineering
- Java is an interpreted language kind of.
- You take your source code and compile it (using
javac) to what Sun call Java bytecode. This
bytecode is then interpreted by the Java Virtual
Machine at runtime. - A C compiler takes your source code (which is
syntactically similar to Java source code) and
produces native code which can only run on a
particular CPU. - Interestingly the C language specification
provides no guidance on what this native code
must look like hence the same piece of C
source code compiled on the same machine with two
different compilers might actually produce code
that behaves differently. - And there are literally hundreds of compilers
around.
7Hello World in C/C (hello.cpp) Computer Games
Software Engineering
include ltstdio.hgt int main()
printf(Hello World\n) return
0 include ltiostreamgt int main()
cout ltlt "Hello World\n" return 0
(i) The C way to do it this would still compile
and run using a C compiler.
(ii) The C way to do it seems more complicated
to me plus the C version is much more closer in
syntax to the Java version.
8Hello World in Java Computer Games Software
Engineering
include ltstdio.hgt int main()
printf(Hello World\n) return 0
public class hello public static void
main(String args)
System.out.println("Hello world.\n")
return
The C version.
The Java version.
9Classes and OOP in C Computer Games Software
Engineering
- C is object oriented. It therefore uses
classes. - However you can mix OO with procedural code in
C. In the hello world examples, neither used
objects or classes. - OO programming is full of mumbo-jumbo and
academic guff about encapsulation, inheritance,
polymorphism etc. - An object is an instance of a class. A class is
simply a type of object. Classes in OOP attempt
to model real world entities. Just as an entity
has properties and abilities, a class has member
variables (data) and member functions (or methods
in Java-speak). - The basic idea is that interfaces to objects
(which define how you, and other objects, use an
object) are separated (or hidden) from how the
object does its stuff.
10Class declaration and data hiding Computer Games
Software Engineering
- A class is declared by using the keyword class
followed by a programmer-specified name
followed by the class definition in braces. The
class definition contains the class members, its
data, and its functions.
class Dog public    void setAge(int
n)Â Â Â Â int getAge()Â Â Â Â void speak()privateÂ
   int age
- In this example, the keyword private indicates
that the member variable age cannot be directly
accessed from outside of the class. - The keyword public indicates that all functions
can be called from code outside of the class.
That is, they may be called other parts of a
program using objects of this class.
11Class implementations Computer Games Software
Engineering
- The actual code that defines the declaration is
called the class implementation. - If methods are implemented outside of the class
definition, they must be identified as belonging
to that class. - This is done with the scope resolution operator
- It identifies each method, for example, getAge(),
as belonging to the class Dog.
void DogsetAge(int n) age n int
DoggetAge() return age void
Dogspeak() printf(Bark!\n)
12Constructors/Destructors Computer Games Software
Engineering
class Dog public Dog() //Constructor
Dog() // Destructor void setAge(int age)
int getAge() void speak() private
int age DogDog() age
0 DogDog() printf("Deleting dog")
- Every class has a special method, the
constructor, that is called when an object of the
class is instantiated (created). - The constructor can be used to initialize
variables, dynamically allocate memory or setup
any other needed resources. - Another special method, the destructor, is called
when an object is destroyed. The destructor is
used to free any memory that was allocated and
possible release other resources.
13Developing in C Computer Games Software
Engineering
- As we have already discussed, C uses a compiler
to create native code. Its not quite as simple as
this. - A typical set of C development tools will first
use a compiler to compile your own code to object
code. This is still native machine code but you
cant run it. - Usually your object code first needs to be linked
to other existing pieces of object code,
libraries, and other resources. This, if you want
it to, will result in an executable file which
can be run. - Some compiler/linkers (well refer to them
usually as just compilers) can create object and
native code for processors of a different type to
the one on which you are doing your development
this is termed cross-compilation.
14Header files Computer Games Software Engineering
- C/C use header (.h) files. A header file is a
file containing the external declarations for a
library. In reality this means all the names of
your classes, class operations, variables and
functions etc. - To use a header file you use a line of form-
include ltiostream.hgt , or include
myheader.h - The first example uses some kind of search path
to the find the file. The second uses a path
relative to the implementation (or .cpp) file. - Using an include file is actually nothing too
exciting all the include statement conceptually
does is take the contents of the header file and
paste it into your source file. This process is
done by a pre-processor.
15The C Preprocessor Computer Games Software
Engineering
- The preprocessor is executed automatically by the
compiler makes the first pass of the program's
code. - Preprocessor directives are orders within
programs that are not instructions for the
application itself but for the compiler (well,
for the preprocessor anyway). - Such directives must be specified in a single
line of code and do not have to an end in a
semicolon. - Directives can be used, for instance, define
constants to be used later on in the application
e.g. define MAX_WIDTH 100 if
(X_posgtMAX_WIDTH) printf(oops!) - Or, as we have seen already, be used to include
header files include ltiostream.hgt
16More examples Computer Games Software
Engineering
-
- A common use of directives is to tell the
compiler whether or not to compile sections of
code or whether to use certain options during the
compilation. For example - All C compilers have their own special
preprocessor directives as well that can be
initiated using pragma. E.g. pragma
comment(lib, "gd205bnr.lib") // use the GapiDraw
lib
define MY_CODE ifdef MY_CODE // do some code
else // do
something else instead endif
17Static libraries Computer Games Software
Engineering
- C/C use library (.lib) files to implement
functionality that might be used by more than one
application. - Hence if you wish to make use of some-one elses
C/C code they might supply you with a .lib file
which you can link to during your compile/link
(or build) stage. - Anyone can create a .lib file by telling their
compiler/linker to create one instead of creating
an .exe. - Most .lib files are static libraries in that they
never change in content and you only ever go near
them during compilation. - Any code that is needed by your application from
the .lib file is included in your final
executable.
18Import Dynamic libraries Computer Games
Software Engineering
- Using static library means all your application
is neatly bound up in one bundle (the .exe). - However this might mean you create a huge
executable. - Also, it means that if a lib file changes then
you have to rebuild your entire application to
use these changes. - Windows, and its variants (including the Pocket
PC OS), extensively use DLLs, or Dynamic Link
Libraries, to get around these problems. - A DLL is similar to a static library in that it
contains lots of nice object code that might be
used by anyone else. However, your linker doesnt
include this code in your executable.
19Import Dynamic libraries Computer Games
Software Engineering
- To use a DLL your linker must put a place-marker
in your object code (called a stub) which acts,
in a way, like a GOTO statement to get to the
correct place in the DLL at runtime. It finds
this information from a special .lib file called
an import library. - Hence the import .lib file is only used during
compilation whilst the DLL file is only used
during runtime. This allows you (and Microsoft,
for instance) to update DLLs whenever you (and
they) like. - It also means that you quite often get complaints
from Windows executables saying that DLLs are
missing. You would never get this if only static
libraries were used. - The DLL must be present in the runtime
environment.
20Developing C for WindowsComputer Games
Software Engineering
- Windows (and things like Pocket PC which are
based on Windows) are big, nasty, graphical
operating systems. - Anyone can develop C applications for Windows
if they have the right tools. You can get free
C compilers (e.g. Open Watcom, GNU C/C) and
commercial ones (Codewarrior, Borland C,
Microsoft Visual C). - The industry standard is, of course, Microsofts
own products now wrapped up as Visual Studio
.NET which includes C, C and Visual Basic. - Visual Studio is a complex, graphical integrated
development environment (IDE) which allows you to
create very sophisticated (or very trivial)
Windows applications.
21Developing for PDAsComputer Games Software
Engineering
- A PDA is an example of a target device. Its not
the kind of device on which it is easy to develop
code. The screen is too small and its not got a
real keyboard for starters. - It is really an example of an embedded system.
- Usually, embedded systems developers will develop
on one type of platform (something nice like a
desktop PC), and use a cross-compiler to create
code for the target. - Well be doing this in this module. We will
develop on the Windows platform using a special
set of tools that can create executables that
will run on an ARM/Pocket PC 2003 system. If you
try and run such an executable on the PC it
simply wont work.
22Developing for PPC 2003Computer Games Software
Engineering
- Microsoft used to supply free versions of both
Visual Basic and C for developing for Pocket PC
2002 wrapped up as a product called eMbedded
Visual Tools 3.0. - For Pocket PC 2003 only C is available for free
(named eMbedded Visual C 4.0). This seems to be
because MS want us to buy full blown copies of
Windows Visual Studio .NET which allow developers
to create applications in C, C and VB for
devices running all manner of Windows OSs
(including XP, Pocket PC and SmartPhone). - Pocket PC is in fact an example of a special
purpose OS based on the generic Windows CE
software platform. SmartPhone is another example
of this. Pocket PC 2003 is based on Windows CE
.NET (Windows CE 4.2).
23Developing using eVC4.0Computer Games Software
Engineering
- To the developer, the IDE for eVC looks pretty
much like Visual C. You must create projects
(and, by default, workspaces) in order to create
applications. - To create a new application for this module you
must run eVC, then create a new, empty, WCE
Pocket PC 2003 application from the new project
wizard. This creates all the configuration files
necessary for the project. - The main project file has .vcw extension.
- You then add your C source file(s), link to any
resources (images etc.), point the
compiler/linker to any extra APIs (header files,
library files etc) that you might be using and
then build your application.
24Hello World for PPC2003 Computer Games Software
Engineering
include ltwindows.hgt int WINAPI
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
printf("Hello World.\n") // print a message
char cgetchar() // wait for ltRETURNgt
keypress return 0
25Hello World for PPC2003 Computer Games Software
Engineering
- Including the windows.h (windows header) file
allows your program to use of a vast array of
other include files (and therefore other
libraries and classes) that define Windows CE as
well as the structures and constants they use. - The line beginning int WINAPI is termed the entry
point to the program. A Windows CE application
requires an entry point of a standard type i.e.
exactly like the one shown. - The application waits for someone to press return
on the keyboard this is to stop the program
running in a console window then instantly
exiting. - Although this is the first program you will use
for the module it bears little resemblance to the
kinds of programs you will develop games with.
26Using the GapiDraw library Computer Games
Software Engineering
- We will develop games applications during this
module using the third party library GapiDraw
developed by the Future Applications Lab at the
Viktoria Institute in Sweden. - You must begin to take a look at the website for
this software at www.gapidraw.com. - GapiDraw defines its own base class for a games
application called CGapiApplication. All your
programs will be derived from this. - Practically, to use to the Gapidraw classes in
your own programs you need to include some header
and C files, link to an import library, and
make sure that the required GapiDraw DLL is
available at runtime.
27Inheritance Computer Games Software Engineering
- Your games applications will be derived from a
Class already defined for you by the GapiDraw
library. - This is an example of inheritance.
- We say that CMyApplication is a kind of
CGapiApplication, or is derived from
CGapiApplication, or is even a subclass of the
base class CGapiApplication.
class CMyApplication public CGapiApplication Â
//construction //attributes
//implementation
28Inheritance Computer Games Software Engineering
- Inheritance, of course, is a fundamental part of
OO programming and of C. - Away from all the academic guff relating to OO,
the practical reason for having inheritance is
because it would be a pity to go to all the
trouble of creating a class and then have to
create a brand new one that has similar
functionality.
- It would be much nicer if we could take an
existing class (perhaps one that has been created
by someone else), clone it, and then make
additions, or modifications, to the clone.
29Inheritance Computer Games Software Engineering
- You have two ways of making your new derived
class different to the original base class. - Firstly, you can add new functions to the derived
class (if the base class didnt do everything you
want it to). - Secondly, you can alter the behaviour of an
existing base-class function. This, as you should
know from Java, is called overriding that
function.
30Next Week and Further Reading Computer Games
Software Engineering
- Next week we will look in detail at creating some
simple graphics applications by deriving a new
class from the CGapiApplication base class. - In the meantime you should (if you havent
already) have a read of the GapiDraw website and
its documentation a lot of this will be OO
mumbo-jumbo at present dont worry its not as
complex as the OOP people will have you believe. - The SoC homepage lists some possible books on
C. You should also have a look at some of the
C books in the library choosing a programming
language book is often a very personal thing (I
hate the DeitelDeitel books for instance) and at
this level you should, by now, have strong
opinions about what helps you and what doesnt.