Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO4301 – Advanced Games Development Week 1 Introduction

Similar presentations


Presentation on theme: "CO4301 – Advanced Games Development Week 1 Introduction"— Presentation transcript:

1 CO4301 – Advanced Games Development Week 1 Introduction
Gareth Bellaby

2 Introduction

3 Module contents Advanced Programming Concurrency
Concurrency for game logic. Parsers. Rapid prototyping Games programming patterns Optimising algorithms

4 Module contents Optimisation and efficiency. Binary search
Red/black trees (sets) Self-balancing trees Balance search

5 Questions I need to check the starting point for some of these topics.
What do you know about the various topics?

6 C11

7 C11 3 reasons to do this: Nice introduction to the module
Important for careers C11 has loads of cool stuff

8 C11 Overview: Types: auto, nullptr, new enums, new style functions, decltype Simplified initialisation, member defaults Range-based for loop Standard library: smart pointers, hash-tables, regex and much more Constructor delegation and deletion, “final” virtual functions Extensive support for multithreading Lambda expressions: (anonymous functions, Javascript-like) Constant expressions: code executed by the compiler Parameters & return values: rvalues and move semantics Template extensions such as variadic templates For more details see:

9 References

10 References Previously stated that references are useful but not default. Laurent has said that he’s now using references by default. Reading the C++ faq and comments from people such as Stroustrup it certainly seems to be the case that references are now the preferred form. Use const references. Use non-const references.

11 References - quote When should I use references, and when should I use pointers? Use references when you can, and pointers when you have to. References are usually preferred over pointers whenever you don’t need “reseating”. This usually means that references are most useful in a class’s public interface. References typically appear on the skin of an object, and pointers on the inside. The exception to the above is where a function’s parameter or return value needs a “sentinel” reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references must always alias objects, not a dereferenced null pointer).

12 References A reference always points to the same object.
A reference cannot be null. A pointer can point at multiple objects. A pointer can be null (or 0). Further: A reference cannot be uninitialised.

13 Smart Pointers

14 Smart pointers Smart pointers introduce two important things:
Automatic memory management: no need to worry about memory leaks. Delete gets called automatically when an object is killed. Ownership of an object. A single pointer owns a single object at any time.

15 C11 Include the <memory> library
A smart pointer variable is declared using unique_ptr. Need to supply the data type No need to use delete. The memory is managed for you. When you declare a unique_ptr, the compiler creates a raw pointer and wraps it around with extra code to manage the memory. unique_ptr <obj> ptr(new obj);

16 Ownership The pointer owns the block of memory being pointed to.
It owns it uniquely. An unique_ptr prevents copying of its contained address. It means that only one thing can own the memory block at a time and you have to explicitly transfer ownership to another pointer. Why this done?

17 Ownership The transferral of ownership is done using the keyword move.
ptr2 = move(ptr1);

18 Reuse of an unique_ptr You can reuse the same pointer to create a new object, or a whole series of them. The method reset is used to do this. unique_ptr <coords> tmp(new coords); tmp.reset(new coords);

19 Raw pointer A smart point is implemented using a standard pointer.
Terminology is that the standard pointer is called a “raw pointer”. You can obtain the raw pointer using get(). coords* t; t = ptr.get(); cout << t->x << endl;

20 Raw pointer For obvious reasons using a raw pointer is dangerous.
It allows you full access to the pointer and memory, exactly what the introduction of smart pointers was intended to stop. Raw pointers can be useful to observe the contents of a unique pointer. Do not use them to undermine or circumvent the (useful) limitations of a unique pointer.

21 Ranged-based for loop C11 introduces ranged-based loop.
Don’t need to declare an iterator A good place to use auto! Works for anything that has begin() and end() string str = "hello world"; for (auto i : str) cout << i << endl; deque<int> scores; for (auto value : scores) cout << value;

22 Functions Use move() to transfer ownership of a unique pointer to a function. And use it as well to ownership via the return statement. unique_ptr<int> Transfer(unique_ptr <int> t) { t = 2; return std::move(t); }

23 Reference Use of a reference works fine.
void foo(unique_ptr <coords>& p) { p->x = 5; }

24 Shared pointers You also have use of shared pointers.
This is meant for when multiple pointers share ownership of a block of memory and so (obviously) it only gets deleted when all of the owners disappear. Implemented using an internal count. As a rule do not use shared pointers. It undermines the intention of smart pointers. Think of an alternative approach if shared pointers look to be a solution.

25 Lambda Expressions

26 Lambda expressions Unnamed local function Describes a simple operation
Described at point of use, so makes it easier to read. A good example of use being sort.

27 Capture list a

28 Efficiency As a general statement there is no difference between a lambda function and a function in terms of efficiency. It does not magically make things faster. Indeed a compiler may build an equivalent function at compile time. However, it can be more efficient. If you pass a pointer to a function it may not be easy for the compiler to generate an inline version of the function. A lamdba function is an instance of a class.

29 Purpose It can help the compiler to optimise by providing a hint as to context. It allows you to use throw-away functions or functions that only have a local meaning or importance. This helps preserve localisation and supports safe coding.

30 Why “lambda” Lambda origins lie in the lambda calculus, which is a formal system in maths for describing computation. Uses the greek letter lambda (λ). lambda (λ) is used in lambda expressions to indicate binding a variable in a function. In the Lambda calculus you can write function anonymously, i.e. without giving them a name. So Lambda functions are the C++ implementation of anoymous functions.

31 Lambda functions Square(x) = x2 Can be written as: (x) |-> x2
In other words, as an anonymous function.


Download ppt "CO4301 – Advanced Games Development Week 1 Introduction"

Similar presentations


Ads by Google