Games Development 2 Tools Programming: C - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Games Development 2 Tools Programming: C

Description:

We can divide games programming into 3 areas: Game Engine ... And jagged arrays an array of different length arrays. C#: Other. C# is more typesafe than C ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 33
Provided by: lauren80
Category:

less

Transcript and Presenter's Notes

Title: Games Development 2 Tools Programming: C


1
Games Development 2Tools Programming C
  • CO3301
  • Week 16-17

2
Contents
  • Tools Programming
  • Tool Chains
  • Introducing C
  • C Differences from C
  • C Language Overview

3
Tools Programming
  • We can divide games programming into 3 areas
  • Game Engine - non-game-specific code
  • Renderer
  • Sound, input, mass storage libraries and similar
  • Generic scripting, data import, AI code
  • Physics engine
  • Game Code
  • Specific game logic, camera work, entity updates
    etc.
  • Game Scripts
  • Tools
  • Game editors
  • Asset / data preparation manipulation
  • Offline optimisation / pre-calculation

4
Tools Programming
  • Tools programming is often overlooked, but a very
    important area
  • Games are massively data driven and that data
    usually needs considerable processing
  • Tool types
  • Level Editor
  • Major tool that allows the viewing and editing of
    the game world. Often game independent
  • Mesh / material viewers, editors
  • Post-modelling, engine specific work with 3D
    assets
  • Texture manipulation
  • UV atlasing, normal map creation, etc
  • Sound editors, data packing, testing systems etc.

5
Tools Development
  • Where possible, tools are written as plug-ins for
    existing software
  • Maya MEL / Python scripts, C API plug-ins
  • Similarly Max Script
  • Photoshop plug-ins, etc.
  • Extending existing programs is preferable
  • Dont need to write everything from scratch
  • Building on robust, documented platform
  • Also allows the asset creators to work seamlessly
    with the game tools

6
Tools Development
  • However, many specialised tools are required that
    do not naturally fit existing packages
  • E.g. Particle system editor, AI editor
  • Such tools are usually written in C or, more
    frequently now, C
  • Rapid to develop GUI applications
  • Interface easily with the native C code
  • Some tools dont need a GUI
  • Asset copying / optimising / packaging, testing
    systems
  • These may be console applications or scripts

7
Tool Chains
  • A Tool Chain is the sequence of tools needed to
    take a raw assets through to usable game data
  • A chain might be
  • Artist uses normal map plug-in in Photoshop,
    exports textures using plug-in to compressed
    format
  • Export script for Maya outputs models in custom
    format
  • Mesh / texture optimised, custom tool called by
    plug-ins
  • Level editor imports art assets, allows viewing /
    manipulation. Also supports game entity layout,
    attribute setup, trigger and script attachment
  • Level editor contains BSP and waypoint generation
    and lighting pre-calculation. Exports custom
    level data
  • Level data assembled into package using a console
    app

8
Introducing C
  • C is becoming more frequently used for tools
    development
  • Used to be mostly C
  • C is a language developed by Microsoft as part
    of their .NET initiative.
  • Based on C, with some ideas from Java and
    Delphi
  • Simpler than C and Java
  • Attractive for rapid development due to
  • Focus on simplification
  • Features enhancing program robustness
  • Fairly good performance
  • Good development environment

9
CLI / CLR
  • C is usually understood in the context of the
    Common Language Infrastructure
  • An open specification developed by Microsoft
  • Describes the executable code and runtime
    environment at the core of the Microsoft .NET
    framework
  • The Common Language Runtime is Microsofts
    implementation of CLI for Windows machines
  • Other implementations, on other platforms are
    possible
  • C does not have to target CLI
  • But it was designed to

10
Comparing C and C
  • No global variables
  • All variables must be members of a class (called
    fields)
  • No global functions either
  • All functions must be methods of a class
  • Multiple inheritance is not supported
  • Although a class can implement any number of
    interfaces
  • Supports properties, methods that appear as
    public member variables
  • Simplified form of getter and setter
    functionality

11
C Pointers
  • All classes are accessed by reference
  • Not by value
  • Rarely need for pointers
  • Only simple types and structs accessed by value
  • Structs are not classes, but optimised data
    collections
  • Pointers can only be used within code sections
    specifically marked as unsafe
  • Programs with unsafe code need appropriate
    permissions to run
  • C programs tend to avoid pointers

12
C Memory
  • Memory can be explicitly allocated
  • But cannot be explicity freed
  • It is instead automatically garbage collected
  • Potential performance issues here
  • Especially for games developers
  • Support for ArrayList - variable length array
  • And jagged arrays an array of different length
    arrays

13
C Other
  • C is more typesafe than C
  • Only a few implicit conversions allowed
  • E.g. cannot covert from enum to int or vice-versa
  • All types derived from System.Object
  • So primitive types can have methods
  • 42.ToString()

14
C Language Overview
  • A simple program
  • using System // Access System namespace
  • class Hello // Everything must be in a class
  • static void Main() // Entry point must be Main
  • Console.WriteLine("Hello World")
  • // No need for a closing on classes
  • The System namespace contains a wide range of
    classes for commonly-used language features
  • Used for Console here, almost always needed

15
C Language Overview
  • A two class program
  • using System
  • class Counter
  • int val 0
  • public void Add(int x) val x
  • public int GetVal() return val
  • class Entry
  • static void Main()
  • Counter c new Counter()
  • c.Add( 5 )
  • c.Add( 6 )
  • Console.WriteLine( c.GetVal() )

16
C Types
  • Simple types always stored by value
  • bool, char, byte, sbyte, short, ushort, int,
    uint, long, ulong
  • (s- prefix indicates a signed type, u- is
    unsigned)
  • float, double and decimal (accurate, limited
    range)
  • structs and enums also always stored by value
  • Arrays, classes, interfaces and delegates (like a
    function pointer) are always stored by reference
  • All of these must be allocated with new
  • Pointer types available with restrictions
  • All types compatible with / derived from the
    object type

17
C Values, References
  • Types stored by value work as expected
  • int x 5
  • int y x // 5 is copied
  • x 6 // Now x 6 and y 5
  • Careful with types stored by reference
  • string s1 bob
  • string s2 s1 // s2 is a copy of the reference
    to bob
  • s1 jim // s1 s2 jim

18
C Arrays
  • Array syntax differs from C
  • int a new int3 // Uninitialised
  • int b new int 3, 4, 5 // Initialised
  • int c 3, 4, 5 // --
  • SomeClass d new SomeClass10 // Array of
    references
  • SomeStruct e new SomeStruct10// Array of
    values
  • int len a.Length // number of elements in a
  • Individual arrays are fixed length, but array
    types are flexible
  • public Output(int arr) ... // Any length
    array

19
C Arrays
  • Jagged array (array of different length arrays)
  • int a new int2
  • a0 new int3
  • a1 new int4
  • int x a01
  • int len a.Length // 2 number of arrays
  • len a0.Length // 3 length of first array
  •  
  • Rectangular array (like C 2D array)
  • int, a new int2, 3
  • int x a0, 1
  • int len a.Length // 6 total elements in 2D
    array
  • len a.GetLength(0) // 2 size of first
    dimension
  • len a.GetLength(1) // 3 size of second
    dimension

20
C String Type
  • The System namespace contains a string class
  • string s1 jim
  • Console.WriteLine( s11 ) // Output i
  • Console.WriteLine( s1.Length ) // Output 3
  • string s2 s1 bob // Concatenation
    jimbob
  • s12 b // ERROR strings cant be
    modified
  • // Use StringBuilder for
    modification
  • Class String defines many useful operations
  • CompareTo, IndexOf, StartsWith, Substring, etc.

21
C Enumerations
  • Enumerations very similar to C
  • enum Color red, blue, green // values 0, 1, 2
  • // Specify storage type and values
  • enum Flags byte Dynamic1, ReadOnly2,
    WriteOnly4
  • Usage
  • Color c Color.blue // Must explicitly indicate
    enum
  • Flags VertexBufferFlags Flags.Dynamic
    Flags.WriteOnly
  • Although enums use integer values, need to cast
    into an integer type

22
C Structs
  • Structures look like classes
  • struct Point
  • public int x, y // Member variables called
    fields
  • public Point(int nx, int ny) x nx y ny
  • public void MoveTo(int a, int b) x a y b
  • Usage
  • Point p new Point(3, 4)
  • p.MoveTo(10, 20)
  • However
  • Structs cannot have destructors
  • Structs cannot inherit other classes
  • But Structs can implement interfaces

23
C Classes
  • C classes behave like C
  • class Rectangle
  • Point origin
  • public int width, height
  • public Rectangle()
  • origin new Point(0,0) width height
    0
  • public Rectangle (Point p, int w, int h)
  • origin p width w height h
  • public void MoveTo (Point p) origin p
  • Usage
  • Rectangle r new Rectangle(new Point(10, 20), 5,
    5)
  • int area r.width r.height // Note use of .
    not -gt
  • r.MoveTo(new Point(3, 3))

24
C Boxing
  • Boxing is the conversion of a value type to a
    reference type, often the object type
  • Allows use of methods on simple types
  • int i 3 object obj i // Boxing, int to
    object
  • Console.WriteLine( obj.ToString() ) // Std
    object method
  • Console.WriteLine( 3.ToString() ) // Implicit
    conversion
  • Unboxing is the opposite conversion
  • int i (int)obj
  • Allows for generic arrays / container classes
  • object arr new object3
  • arr0 3 // 3 is boxed into an
    object
  • arr1 new Point(3, 5) // struct is also boxed
  • arr2 new Rectangle() // Class - no boxing
    required,
  • // all classes inherit
    from object

25
C foreach
  • Extension of for loops
  • int a 3, 17, 4, 8, 2, 29
  • foreach (int x in a)
  • sum x
  • string s "Hello"
  • foreach (char ch in s)
  • Console.WriteLine(ch)
  • Can also be used to iterate over container classes

26
C Switch with Goto
  • Can goto different cases in switch statements
  • switch (state)
  • case 0
  • if (count 0) ...
  • else if (count 1) goto case 1 // Can goto
    other cases
  • else goto default
  • case 1
  • ...
  • default
  • ...

27
C Class Constants
  • Two kinds of constant for classes
  • class MyClass
  • const int c1 5 // Computed at compile time
  • readonly int c2 // Computed once at object
    creation
  • public MyClass( int init )
  • c1 init // ERROR cant initialise constant
  • c2 init // OK in constructor only
  • public void MyFunc()
  • c2 // ERROR cant change value

28
C Parameters
  • Three kinds of parameters in C
  • Value (only applicable to value types see
    earlier)
  • void Inc(int x) x x 1 // Operates on
    copy of int
  • int y 3 Inc(y) // Doesnt change y
  • Reference (default for reference types see
    earlier)
  • void Inc(ref int x) x x 1 // Affects
    original int
  • int y 3 Inc(ref y) // y becomes 4
  • Output (by reference without initialisation)
  • void Read(out int x)
  • string s x.ToString() // ERROR initialise x
    first
  • x Console.Read() // OK
  • int y Read(out y) // Dont initialise y - gets
    result

29
C Constructors
  • Structs always get a default constructor
  • Initialises everything to 0
  • Cannot write own default constructor for structs
  • Classes also get default constructor if no
    other constructors defined
  • Can write own default constructor for classes
  • Constructors can call other constructors
  • struct Complex
  • double re, im
  • public Complex(double re, double im) ...
  • public Complex(double re) this (re, 0)

30
C Constructors / Destructors
  • C has static constructors
  • Called once only, before class is used
  • class Rect
  • static Rect () Console.WriteLine("Rectangle
    Init)
  • ...
  • C has destructors for classes like C
  • But used much more sparingly
  • Automatic garbage collection reduces need
    considerably
  • Considered unsafe

31
C Properties
  • Extension of fields (known as smart fields)
  • Just another way to write a field (member
    variable) with a getter / setter
  • class Data
  • private int mX
  • public int X
  • get return mX // When reading X
  • set mX value // When writing X
  • // Can provide just one get or set if
    required
  • Data d
  • d.X 5 // Invokes set for X
  • Console.WriteLine( d.X ) // Invokes get for X

32
C Indexers
  • A kind of operator overload for classes/structs
  • Allows a class to be accessed like an array
  • class TwoArray
  • private int Arr1 new int100
  • private int Arr2 new int100
  • public int thisint i
  • get return Arr1i Arr2i
  • // Can also provide set
  • TwoArray a
  • ...
  • Console.WriteLine( a50 ) // Arr150 Arr250
Write a Comment
User Comments (0)
About PowerShow.com