Creating a Level Loader - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Creating a Level Loader

Description:

From now on, anything that 'draws' will extend this class. All drawable things in the game have a standard API. BoundableEntity ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 22
Provided by: bdug1
Category:

less

Transcript and Presenter's Notes

Title: Creating a Level Loader


1
Creating a Level Loader
  • Bryan Duggan
  • Hugh McAtamney

2
What is a level loader
  • Loads up the geometry and textures for each level
  • We now have enough information to create a Dalek
    World level loader
  • Vectors
  • Matrices
  • The rendering pipeline
  • Vertex buffers
  • Texturing
  • File I/O using stdio
  • OO Framework

3
New framework classes
4
DrawableEntity
  • An interface in Java terms
  • Does nothing, but has an API with virtual member
    functions
  • setup
  • update
  • draw
  • cleanup
  • From now on, anything that draws will extend
    this class
  • All drawable things in the game have a standard
    API

5
BoundableEntity
  • Anything that has lower and upper bounds extends
    this class
  • We can use it later for things like bounding box
    collision detection

6
World
  • Extends DrawableEntity and BoundableEntity
  • Encapsulates Dalek World and everything in it
  • Draws the SkyBox, floor
  • Contains the camera
  • Has Vectors of
  • Internal walls
  • Daleks
  • Ammunition

7
Wall
  • Extends DrawableEntity and BoundableEntity
  • Draws an internal wall

8
Hang on..
  • I thought a vector was like a point in 3d space???

9
C Vectors
  • Consider the following C function
  • int add(int a, int b)
  • return a b
  • This works for ints, but if we want to use
    floats, we have to write another function
  • float add(float a, float b)
  • return a b
  • This is bad. What if we could pass the type as a
    parameter

10
C Vectors
  • To get around this problem, C introduces
    templates
  • A template is like passing a type as a parameter.
    Example
  • template lttypename tgt
  • t add(t a, t b)
  • return a b

t is a type. The compiler will create a function
for any type that the program uses
11
To use a template
  • You simply call the method as normal!
  • ints
  • int a, b
  • a 10 b 20
  • printf("a d b d a b d\n", a, b,
    add(a, b))
  • floats
  • float c, d
  • c 60.8 d 39.2
  • printf("c f d f c d f\n", c, d,
    add(c, d))
  • Even strings!
  • string f ("Hello"), g (" World")
  • string h add(f, g)
  • cout ltlt h

12
The STL
  • In fact C now has a huge library of template
    based classes that can simplify the writing of
    your programs

13
C Vector
  • Is like a dynamic array
  • We do not have to worry about the size of the
    array
  • We can just add and remove elements as necessary
  • The vector class keeps track of memory use and
    allocates more or less memory as necessary

14
To use a vector
Without the .h!
  • include ltvectorgt
  • void main()
  • vectorltintgt ages
  • ages.push_back(21)
  • ages.push_back(24)
  • ages.push_back(33)
  • ages.push_back(27)

Our vector will hold ints
Add elements to the end of the vector
15
Vector iteration
The size of the vector
  • for (int i 0 i lt ages.size() i )
  • printf("Age of student d is d\n", i, agesi)
  • You can also use
  • vectorltintgtiterator it
  • it ages.begin()
  • i 0
  • while (it ! ages.end())
  • printf("Age of student d is d\n", i, it )
  • it
  • i

Get the ith element
Create an iterator
Make the iterator point to the 1st element
The contents of the iterator is the element
Increment the iterator To make it point to the
next element
16
World class
  • Uses vectors to keep references to
  • walls
  • daleks
  • projectiles
  • E.g.
  • stdvectorltWallgt _walls
  • Wall wall
  • wall new Wall()
  • wall-gtsetBounds(x0, y0, z0, x1, y1, z1)
  • _walls.push_back(wall)
  • for(int i 0 i lt _walls.size() i)
  • _wallsi-gtsetup(device)

17
Loading assets
  • In a game an asset is
  • An audio file
  • A texture
  • A mesh (collection of vertices)
  • We only want to load each asset once
  • For example
  • If our game had 5000 walls
  • And each wall loaded a 1Mb texture
  • Then we would be using 5Mb, just to store the
    same texture over and over again!

18
The solution is
  • The singleton design pattern
  • A singleton is
  • A class that only allows you to create one
    instance of it.
  • The singleton takes care of lazy loading assets
    (loading the asset only once and on demand).

19
To implement a singleton in C
  • Make the constructor private
  • private
  • AssetLoader()
  • Create a static pointer to an instance of the
    class. This will be the single instance of the
    class created
  • static AssetLoader _theInstance
  • Create a static accessor member function that
    constructs the instance if it does not already
    exist
  • AssetLoader AssetLoaderinstance()
  • if (AssetLoader_theInstance NULL)
  • AssetLoader_theInstance new AssetLoader
  • return AssetLoader_theInstance

20
Finally
  • In C, the static member will not be created
    until its assigned a value, so you must include
  • AssetLoader AssetLoader_theInstance NULL
  • At some point in your code

21
Lazy loading
  • Now you can lazy load the assets in a similar
    manner. E.g.
  • IDirect3DTexture9 AssetLoadergetTexture(const
    char fileName, IDirect3DDevice9 device)
  • IDirect3DTexture9 texture NULL
  • texture _texturesfileName
  • if (texture NULL)
  • D3DXCreateTextureFromFile(device, fileName,
    texture)
  • _texturesfileName texture
  • return texture
Write a Comment
User Comments (0)
About PowerShow.com