C++ Tutorial Intermediate Recipes (SFML) [Shared Resource Management] [w/ subs]



00:00:00 – neither guys chew here coming at you
00:00:02 – with another sfml tutorial well I mean
00:00:05 – this one is in really sfml specific
00:00:08 – we're going to be using sfml gonna be
00:00:10 – building off of the sprite animation
00:00:12 – tutorial but this stuff here that we're
00:00:15 – talking about is really applicable to
00:00:17 – any kind of programming the topic for
00:00:19 – today is how to safely easily and
00:00:23 – elegantly implement resource sharing
00:00:26 – between entities in your system
00:00:28 – generally in your game and we're going
00:00:32 – to be again like I said are going to be
00:00:33 – building off of the sprite animation
00:00:35 – code that i started with in a previous
00:00:38 – tutorial but this tutorial isn't geared
00:00:41 – at the beginner level it's geared more
00:00:43 – at intermediate level so we're gonna be
00:00:46 – using a little bit more advanced stuff
00:00:47 – just keep that in mind if you like whole
00:00:49 – chili this isn't what you're not
00:00:50 – explaining anything well i'm sorry but
00:00:52 – this tutorial probably using so you a
00:00:54 – lot of this stuff I'm talking about its
00:00:56 – way over your head you can still watch
00:00:57 – you can still ask questions and
00:00:59 – everything but you don't get p***** off
00:01:01 – if I don't explain all this stuff that
00:01:04 – I'm using it's just assumed that you're
00:01:06 – going to know this stuff and again I the
00:01:10 – general target for this is my
00:01:12 – intermediate series maybe tutorial eight
00:01:16 – ish I don't know I've made those videos
00:01:18 – yet so I can't tell but general guess
00:01:20 – that's probably the type of people on
00:01:22 – targeting with this video so like I said
00:01:25 – I'm starting off with the code from the
00:01:30 – sprite animation tutorial but I've
00:01:32 – changed some things I've made it so that
00:01:35 – the sprite now has a standing still
00:01:38 – animation so you can walk you can stand
00:01:41 – still and how I implemented that I've
00:01:45 – kind of hinted it at it at the end of
00:01:47 – the sprite animation video what you do
00:01:50 – what I did was I replace this static
00:01:53 – array of rectangles with a vector of
00:01:58 – rectangles and that allows us to have a
00:02:00 – different amount of frames for different
00:02:04 – animations because as you can tell
00:02:06 – standing still animation this one here
00:02:08 – it only requires one frame whereas the
00:02:10 – walking random ations require eight
00:02:12 – dreams so by making a vector now I can
00:02:14 – have different lengths of animations and
00:02:17 – you know Bob's your uncle you create
00:02:20 – more animation states indexes you got to
00:02:24 – create more here yeah i'm gonna put this
00:02:26 – code up on github and you can check it
00:02:30 – out for yourself but like i said i'm
00:02:31 – assuming that your intermediate level
00:02:33 – and you'll be able to understand this
00:02:34 – stuff so i'm not going to go over it
00:02:36 – that's not the point of this tutorial
00:02:37 – the point of this tutorial is to create
00:02:40 – a system that can share resources so
00:02:44 – first off let's let's define the problem
00:02:46 – what is what is chilly dysfunction here
00:02:48 – what did he want to solve well the way
00:02:51 – we have things right now every character
00:02:54 – wee sprite basically has a number of
00:02:57 – animations mmm for our character right
00:03:00 – now he's got eight animations every
00:03:03 – animation has a texture so every
00:03:06 – animation load the texture and the
00:03:08 – texture that it's going to load is that
00:03:12 – sprite sheet that I just closed because
00:03:14 – I'm a f****** idiot here it is this one
00:03:17 – guy here so every animation load this
00:03:20 – texture but the texture contains the the
00:03:23 – flames for every animation so there's a
00:03:26 – lot of redundancy here we're loading the
00:03:29 – same image eight times when we only need
00:03:31 – to load it once and that's eight times
00:03:33 – if we only have one character if we've
00:03:35 – got ten character that's loading the
00:03:37 – same image 80 g****** tens when we could
00:03:41 – have only loaded at once saved ourselves
00:03:43 – a lot of video memory so what are we got
00:03:46 – to do well what we want is we want a
00:03:50 – system where all of the characters or
00:03:54 – all of the animations they can share the
00:03:57 – same texture if they're only using the
00:03:59 – same texture and they don't all load it
00:04:01 – individually now there are a bunch of
00:04:05 – ways we could implement that we could go
00:04:07 – into main here and we could load the
00:04:09 – texture and then pass it into character
00:04:11 – and in character each animation would
00:04:14 – maybe hold like a reference or a pointer
00:04:16 – to the texture that was loaded in Maine
00:04:19 – but that's annoying you know think of
00:04:21 – the case where you've got like
00:04:23 – maybe a few tens of characters hundreds
00:04:27 – of animations and they're all loading
00:04:29 – different textures when you add a new
00:04:31 – character now you've got to add a
00:04:33 – texture loading code and domain and when
00:04:36 – you remove one you should probably
00:04:37 – remove it from me and you gotta keep
00:04:39 – track of all that s*** yourself that's a
00:04:41 – pain in the a** what we want is we want
00:04:43 – every character to know which which
00:04:48 – textures and needs basically by every
00:04:50 – animation knowing which textures it
00:04:52 – needs to load and then the animations
00:04:55 – will load the textures themselves but
00:04:57 – again now we've come full circle now
00:04:59 – we've got the problem all the animations
00:05:00 – are loading the textures separately so
00:05:03 – they're duplicating them in memory to
00:05:05 – wasting a lot of time loading the
00:05:07 – wasting a lot of video memory so how do
00:05:09 – we do this well let's create a class
00:05:12 – that is going to keep track of the
00:05:16 – textures that we've loaded and is going
00:05:19 – to make sure that there aren't any
00:05:21 – duplicates loaded so what we're going to
00:05:24 – do is we're going to create a class and
00:05:25 – let's just call this class and will call
00:05:28 – the texture codecs and the way I the
00:05:34 – reason why I'm calling it codex I mean I
00:05:36 – might call it texture manager but that
00:05:38 – just triggers some people who you know
00:05:40 – small-minded so public and private now
00:05:46 – we only really need a single texture
00:05:49 – codecs for entire game you know you
00:05:52 – could probably dream up situations where
00:05:54 – you might want to have multiple separate
00:05:55 – ones but again you design for the level
00:06:00 – that you're going to be working at and
00:06:01 – we're not going to be doing anything
00:06:03 – that complicated so what I'm going to do
00:06:05 – is I'm going to make just that extra
00:06:08 – critic basically I'll static so first
00:06:10 – off we need some things that we're gonna
00:06:13 – be using one of the things we're going
00:06:14 – to need is stuff from memory and another
00:06:18 – one we're going to need is stuff from
00:06:22 – i'm going to say map maybe it's i don't
00:06:29 – know if it's called unordered map on or
00:06:31 – your unordered map there we go
00:06:34 – and well they probably don't need it but
00:06:37 – let's include string as well because
00:06:40 – we're gonna be using that so what we've
00:06:43 – got here now is we want a a map and that
00:06:48 – is a content addressable container
00:06:51 – associate of a container that will
00:06:54 – associate the name of a texture with the
00:06:57 – texture so what we do is we go did on
00:07:02 – all on the ordered map and we want the
00:07:06 – key name that's the thing that is going
00:07:08 – to be used to look up the textures and
00:07:09 – that is going to be stood string and
00:07:12 – then the value name is going to be the
00:07:16 – texture and we're going to store the
00:07:17 – textures as a smart pointer did shared
00:07:23 – pointer to SF texture now I don't use
00:07:31 – sometimes you see online you see people
00:07:33 – using a shared pointer a lot when they
00:07:36 – shouldn't be generally your go-to smart
00:07:40 – pointer and let's just call this one
00:07:42 – textures by the way your go-to smart
00:07:44 – pointer should be a unique pointer if
00:07:47 – you need to manage some dynamically
00:07:50 – allocated object you should be using a
00:07:52 – unique pointer the only time use a
00:07:54 – shared pointer is if you've got multiple
00:07:57 – things that own that object together
00:08:00 – because well I mean if you use a unique
00:08:04 – pointer and you had a GX let me just
00:08:06 – bring out the diagram here so if you use
00:08:09 – a unique pointer and you've got objects
00:08:12 – a and B and they both have a reference
00:08:17 – to some resource are ok and so the
00:08:22 – unique pointer is owned by a and then be
00:08:27 – just as a normal pointer well what if a
00:08:30 – dies if he dies is unique pointer dies
00:08:33 – and that means is unique pointer will
00:08:36 – free this and now be will be pointing to
00:08:39 – garbo and it's going to f*** you up so
00:08:43 – you don't do that you
00:08:46 – have a unique pointer in the case where
00:08:48 – two or more objects can own another
00:08:51 – canola resource right you use a shared
00:08:56 – pointer and these guys both have these
00:08:59 – are shared pointers now I don't know how
00:09:01 – to I'm to do share pointers believe me
00:09:03 – shared all right you you believe in life
00:09:08 – after love all that good stuff so if the
00:09:12 – bulk shared and one of these guys dies
00:09:15 – it won't automatically kill this guy
00:09:19 – this only dies when the last shared
00:09:22 – pointer dies then it cleans up after the
00:09:24 – resource that's the difference between
00:09:25 – shared pointer and unique pointer and
00:09:28 – usually you're fine with just one main
00:09:30 – object owning the resource and other
00:09:33 – objects can just maybe have references
00:09:35 – to them to the other resource if they
00:09:37 – really need it but you've got one main
00:09:39 – object and he determines when the
00:09:41 – resource lives and dies but with our
00:09:44 – without characters here we want our
00:09:50 – characters to share a texture and if one
00:09:52 – of those characters is erased but the
00:09:55 – other character isn't we don't want to
00:09:57 – get rid of that resource we want to keep
00:09:59 – it alive so we use a shared pointer to
00:10:02 – the texture long story short and this is
00:10:05 – going to be static and then what we want
00:10:10 – is we want a couple static functions
00:10:12 – well first off we want a static function
00:10:14 – will call this a choir and what is going
00:10:18 – to do is it's going to get a constant
00:10:19 – stayed string reference oh no in search
00:10:24 – actually at my workplace I actually tore
00:10:28 – the insert key right out of my keyboard
00:10:30 – no lie cuz I f****** hate that s*** and
00:10:33 – we'll just call this name of this this
00:10:37 – probably needs a return value it's going
00:10:39 – to be void so what are we going to do
00:10:43 – here well what we do is we check the
00:10:50 – unordered map first and we see if the
00:10:54 – texture has already been loaded
00:10:57 – so how do we do that well I mean we
00:10:59 – could go constant auto I is equal to
00:11:06 – texture should be texture though
00:11:10 – textures dot find name and this will
00:11:16 – give us an iterator into our our map of
00:11:22 – extra names two textures and then we
00:11:25 – want to check to see if that iterator is
00:11:27 – valid and the way we do that is of
00:11:29 – course we go if I is not equal to
00:11:32 – texture Carew's that end that means that
00:11:39 – we've got a valid way why is this not
00:11:44 – happy textures little textures though
00:11:49 – there we go so we States valid that
00:11:51 – means that this texture was already
00:11:52 – loaded it's already in the map and so
00:11:55 – all we gots to do is just returned a
00:11:57 – copy of the shared pointer to that thing
00:12:00 – so then we go what do we do I guess we
00:12:04 – just go return I point or two and the
00:12:10 – iterator is going to give us a pair the
00:12:12 – first part of the pair is the name it's
00:12:14 – the key at the string the second one is
00:12:17 – going to be the the shared pointer to
00:12:20 – the texture and we just return this now
00:12:22 – I was dumb obviously the acquired
00:12:24 – function is not going to return void it
00:12:26 – is going to return a skid shared pointer
00:12:29 – to SF texture like this and that will
00:12:37 – return a new shared pointer and now the
00:12:41 – resource is going to have a new owner
00:12:43 – now if the iterator is equal to end that
00:12:50 – means that we have not loaded the
00:12:52 – texture yet so all we gots to do in that
00:12:54 – case is we go textures not insert and we
00:13:01 – go steady make underscore shared
00:13:06 – SF texture and let me think here so when
00:13:16 – we insert we have to insert and I
00:13:21 – believe we have to insert a pair right
00:13:24 – and I'm not exactly sure we insert the a
00:13:30 – pair we insert the key and then let's
00:13:32 – try key and then value insert key and
00:13:36 – value so FF texture create a new texture
00:13:39 – and we insert that how's that that's no
00:13:43 – good well ball sack so we got to insert
00:13:47 – a pair but actually I'm thinking right
00:13:49 – now we have to we have to insert the
00:13:51 – created texture object but we also have
00:13:53 – to load the texture we have to load the
00:13:57 – image into the texture so what we should
00:13:59 – do is we should go something like auto
00:14:02 – text I'll call this one p tax is equal
00:14:09 – to and them all create a new texture
00:14:15 – with a shared pointer owned by a shared
00:14:17 – pointer then we go ptex pointer to load
00:14:22 – from file and we call with the the name
00:14:26 – that we're past in and then after we
00:14:29 – loaded from the file then we actually
00:14:32 – put it into our map so we can just use
00:14:37 – what you call that uniform initializer
00:14:42 – whatever and we call name and then ptex
00:14:46 – and now we'll put that shizzle into our
00:14:48 – map and then we return p text and there
00:14:55 – we go now whoever call this either way
00:14:59 – they're going to get a texture sometimes
00:15:01 – it's going to trigger a texture to be
00:15:03 – loaded sometimes they're just going to
00:15:05 – get a another handle on the texture that
00:15:08 – was already loaded but there you have it
00:15:10 – and then all you gots to do is go
00:15:13 – wherever you were loading
00:15:16 – you were calling texture load from file
00:15:18 – and instead what we're going to do well
00:15:20 – instead of having a texture now we're
00:15:23 – going to have a shared pointed to a
00:15:24 – texture so I'm gonna go stood shared
00:15:28 – pointer to our texture and let's rename
00:15:33 – this PTX now because it's a pointer and
00:15:36 – that's how I do that's how I do with
00:15:38 – pointers now here we're not going to go
00:15:42 – texture download some foul we're gonna
00:15:44 – go jeet x is equal to text your codex
00:15:50 – acquire and then we're going to acquire
00:15:53 – this texture by fair means or foul and
00:15:58 – then all we got to do is we go and when
00:15:59 – we go to apply to sprite instead of
00:16:01 – passing in texture we're gonna pass in
00:16:03 – you know the reference pointer pdx p
00:16:07 – textile it's like this and this should
00:16:11 – work and it should know it doesn't work
00:16:15 – i did something stupid all right well
00:16:17 – now we get to and have the fun of my
00:16:20 – link unresolved external symbol alright
00:16:26 – well this is static that basically means
00:16:29 – that we're gonna have to do some
00:16:30 – bullshit here we gotta really declare it
00:16:32 – here outside of the class this is the
00:16:35 – way static members work so we go static
00:16:40 – we basically just copy this as what
00:16:44 – would you type name then we go texture
00:16:48 – codex and then we go textures and this
00:16:53 – will now actually make the thing i
00:16:57 – believe let's try it yeah now it works
00:16:59 – it's just some dumb bullshit you have to
00:17:01 – do with static members and if we run it
00:17:06 – mmm we shouldn't see any difference it
00:17:09 – should still work the same but it should
00:17:10 – be using approximately eight times less
00:17:13 – memory and if we had you know ten
00:17:16 – textures or ten characters then it will
00:17:19 – be using 80 times less memory
00:17:21 – and that's that now this is great but it
00:17:27 – has one one fatal flaw and that is let's
00:17:33 – imagine we've got a game here and the
00:17:34 – game has three levels and in those
00:17:37 – levels you've got characters so in the
00:17:39 – first level you've got to read type
00:17:42 – characters in one yellow character
00:17:43 – second level 2 yellow one green third
00:17:46 – level 2 green and one blue and so what
00:17:51 – we're doing here is when we load level
00:17:54 – one we're going to load we're going to
00:17:56 – create these characters first time we
00:17:58 – create this one it's going to create
00:18:01 – going to load the texture in the memory
00:18:03 – the second one we create it's going to
00:18:05 – get a reference or a pointer to the
00:18:08 – already loaded texture and then when we
00:18:11 – load this one it's going to load the
00:18:12 – yellow dude texture right and that's
00:18:15 – great now when we end level one and
00:18:18 – enter level two we're going to delete
00:18:21 – these guys but guess what these guys are
00:18:27 – going to stay in memory so now we're
00:18:29 – going to load and it's going to get
00:18:32 – pointer to this one and then we're going
00:18:35 – to load this one and it's going to load
00:18:37 – a new texture but the red texture is
00:18:40 – going to be chilling out in memory and
00:18:42 – just waste in space now why is this well
00:18:46 – can you figure it out before chilly
00:18:50 – tells you that's the real question the
00:18:53 – thing is is that we're missing we're
00:18:59 – missing something in this picture and
00:19:01 – there's something that we're missing is
00:19:05 – the Codex because the codex has a map
00:19:11 – and the map has smart pointers to
00:19:15 – everything that you load so even though
00:19:17 – the original characters were destroyed
00:19:20 – and they're smart pointers were
00:19:21 – destroyed there's still the smart
00:19:23 – pointer in the Codex that's keeping
00:19:25 – these objects alive even after they're
00:19:28 – not being used anymore and they're just
00:19:30 – going to clutter up a memory you're
00:19:31 – going to basically have a memory leak
00:19:33 – and you don't want a memory leak so what
00:19:35 – we do is we add an extra functioning
00:19:39 – here and we're going to call this
00:19:41 – function this one is void for sure we're
00:19:44 – going to call this one murder orphans
00:19:48 – because that sounds nicer than you'll
00:19:50 – collect garbage what this guy is going
00:19:54 – to do is it's quite simple all we gots
00:19:57 – to do is go through our textures here
00:20:03 – and basically destroy remove all the
00:20:09 – entries in the map that only have one
00:20:14 – corner of the texture so for every entry
00:20:18 – in the map we check the shared pointer
00:20:20 – we check how many people are sharing the
00:20:24 – resource and if there's only one that
00:20:26 – means that and there's no nothing but
00:20:29 – the map is accessing the resource and
00:20:32 – then we're free to remove that from the
00:20:34 – map I will destroy the shared pointer
00:20:36 – that will destroy the resource now
00:20:39 – there's a problem normally what i like
00:20:40 – to use for this bullshit as i like you
00:20:42 – something called Stan STD remove yeah
00:20:46 – and this is my go-to for removing a
00:20:50 – bunch of s*** from a container but the
00:20:52 – problem is is that remove if don't work
00:20:54 – for associative containers like the map
00:20:57 – actually I just I just realized one
00:21:00 – thing I don't like the name that I gave
00:21:02 – this variable normally when I have a
00:21:04 – container of pointers i like going to
00:21:06 – take that in the container so I'm gonna
00:21:08 – do is I'm going to go for textures and
00:21:11 – I'm gonna replace it with texture
00:21:14 – pointers and i'm just going to replace
00:21:16 – all in selection there we go so these
00:21:19 – are texture pointers anyways so what we
00:21:22 – got to do is I got to do some iterator
00:21:24 – bullshit so for auto I is equal to
00:21:30 – texture pointers that begin and as long
00:21:33 – as I is not equal to X 2 pointers that
00:21:37 – end and that's it we're not going to
00:21:41 – have the third part and you'll see why
00:21:42 – in a second so now we want to check if
00:21:46 – I pointer to mmm no I'm not sure yes if
00:21:55 – I pointed a second which gives us the
00:21:58 – smart pointer dot unique there we go so
00:22:06 – this returns true if there is only one
00:22:09 – owner for the resource so if there's
00:22:11 – only one real owner that owner must be
00:22:14 – us they get the codex and so we're free
00:22:18 – to erase that entry from the map now
00:22:21 – erasing from the map is complicated
00:22:25 – erasing does not invalidate any
00:22:27 – iterators except for the iterator
00:22:29 – pointing to the thing that is erased so
00:22:33 – what you've got to do one of the things
00:22:36 – about erasing is it gives you the it
00:22:39 – returns an iterator to the next thing in
00:22:42 – the container so all I gotta do is set i
00:22:45 – equal to textures texture pointers dotty
00:22:48 – race and we're passing the iterator and
00:22:52 – that will give us iterator to the next
00:22:55 – thing which will still be valid
00:22:57 – everything will be fine but if you don't
00:23:00 – do this and you just erase your I will
00:23:02 – become invalid and then anything you try
00:23:04 – to do with it will result in an error so
00:23:06 – you've got to set I equal to the return
00:23:09 – value of a race and that's why I'm not
00:23:11 – doing this bullshit here because you've
00:23:13 – gotta update your I inside the loop like
00:23:15 – this otherwise if the resource had
00:23:22 – actual character owners or actual
00:23:26 – animation owners then you just integrate
00:23:28 – iterate increment that's the word I'm
00:23:32 – looking for I words you increment I and
00:23:35 – you keep going searching for all unique
00:23:37 – and erasing them from this guy murdering
00:23:40 – those orphans and now this will allow us
00:23:44 – to get rid of s*** from our memory so
00:23:52 – let me just tell now let me just show
00:23:56 – you what it looks like so let's create
00:23:58 – care
00:23:59 – f***** and so will create a character
00:24:05 – f***** and then we'll create a character
00:24:09 – will be will be original here and we'll
00:24:13 – call them call them f*** you d*** a
00:24:18 – different position on the screen and
00:24:20 – then we'll destroy them and then we'll
00:24:26 – call and uh we'll create another
00:24:28 – character and we will also call him
00:24:30 – f***** the second incarnation of the
00:24:34 – first stalker anyways so what happens
00:24:37 – when we step through this bullshit well
00:24:39 – let's try and find out so we enter we
00:24:46 – enter the f***** never mind this is not
00:24:49 – my code step in here and we're going to
00:24:53 – create a bunch of animations so let's
00:24:55 – let's step into these animation
00:24:58 – creations and will step into this one
00:25:01 – and will step into yeah good will step
00:25:07 – into the acquire step out of that that's
00:25:09 – creating a string and in here we're
00:25:13 – going to search for the texture we're
00:25:16 – not going to find it so we're going to
00:25:17 – create a new texture we're going to load
00:25:19 – it we're gonna insert it into the map
00:25:21 – and then we're going to return that very
00:25:23 – thing this and we're out let's step into
00:25:26 – the next animation let's create an
00:25:27 – animation here and we'll go through all
00:25:30 – the bullshit and will step in and step
00:25:32 – out and step in now we're going to
00:25:34 – search for it we're going to find it so
00:25:36 – we just return the one that we found and
00:25:40 – there you go now we're not making dumb
00:25:42 – copies of s*** now we're only keeping
00:25:44 – one copy of the texture in memory
00:25:47 – let me go back down here and again if we
00:25:52 – go down here the f***** too let's set a
00:25:57 – breakpoint and let's continue into here
00:25:59 – we are out and in and again when we
00:26:03 – acquire we get the thing that was in the
00:26:06 – g****** now right so we're not loading
00:26:10 – extra copies for other sprites only one
00:26:14 – copy now when we go out of here we are
00:26:18 – going to destroy those characters so now
00:26:21 – when we step into here normally what we
00:26:24 – would assume is we would assume that
00:26:26 – characters were destroyed so the
00:26:29 – textures were unloaded from memory so
00:26:31 – now we've got a load the texture again
00:26:33 – and but if we step into here step out
00:26:38 – step in we know will notice that it's
00:26:41 – still in memory and that's because we
00:26:43 – haven't called a good old signed murder
00:26:47 – orphans so now we're going to here and
00:26:52 – we call extra codex murder orphans now
00:27:01 – and I'll just skip this point here we
00:27:03 – know that we're going to load these
00:27:04 – textures we're going to load s*** from
00:27:07 – I'm going to load s*** into our text to
00:27:12 – codex it will be in there now we're
00:27:15 – going to step over here we're going to
00:27:16 – murder those orphans now we step into
00:27:18 – f***** step out step in and when we go
00:27:24 – to step in no don't step out and we go
00:27:30 – to acquire this texture what's going to
00:27:33 – happen is it's not going to find the
00:27:37 – picture because we have freed it and so
00:27:39 – can I have to load it over again and
00:27:43 – that's how s*** works so here is how you
00:27:46 – use that now and do a bunch of bullshit
00:27:50 – here
00:27:51 – so your load level one it loads these
00:27:58 – guys in now here's what you don't do you
00:28:02 – don't destroy will you destroy these
00:28:05 – guys for sure when you're in the level
00:28:07 – one you destroy all of your your
00:28:10 – characters what you don't do is you
00:28:16 – don't call murder orphans at this point
00:28:20 – because you call murder or things you
00:28:22 – can unload both of these guys and then
00:28:24 – when you comes down to load level two
00:28:25 – you're gonna have to reload the guys
00:28:28 – that you already had in memory so what
00:28:30 – you do is you destroy your characters in
00:28:35 – here then you create the characters for
00:28:38 – level two creating the characters to
00:28:41 – level two will create more shared
00:28:44 – pointers to the one that are needed and
00:28:47 – will also load new textures that you
00:28:50 – didn't have before then after you've
00:28:52 – created your entities for level two then
00:28:55 – you call murder orphans and that's going
00:28:57 – to clear this guy out now all you have
00:29:00 – are the textures that you actually need
00:29:02 – and then at the end of level two you
00:29:05 – destroy your entities that destroys
00:29:07 – these smart pointers so now the only
00:29:09 – smart pointers pointing to these f******
00:29:11 – it's going to be the ones from the Codex
00:29:13 – then you load your entities for level
00:29:17 – three now we'll create smart pointers
00:29:21 – what is necessary load the new s***
00:29:25 – that's necessary and then again you call
00:29:27 – murder or sins and that is how you live
00:29:31 – your life destroy entities load new
00:29:35 – entities then murder orphans lather
00:29:39 – rinse repeat this is what I recommend
00:29:42 – for managing resources now this doesn't
00:29:45 – have to just be as i said the porters
00:29:48 – have to be SS textures it can be
00:29:52 – anything doesn't have to be sfml it can
00:29:54 – be anything can be sounds mp3 files
00:29:56 – textures any other large resource that's
00:30:00 – going to be shared by multiple entities
00:30:02 – in your game
00:30:04 – and yeah that's that's pretty much it
00:30:06 – just a choir and murder orphans use a
00:30:10 – nice unordered map I don't use a normal
00:30:12 – map because unordered map is more
00:30:15 – efficient to look up I doesn't matter
00:30:18 – super that much though because you're
00:30:19 – not going to be you know looking these
00:30:21 – things up all the time y'all doing a
00:30:22 – look up basically when you when you call
00:30:25 – a choir which is mostly at the beginning
00:30:28 – of a level but you know just just
00:30:32 – because I like to use the unordered map
00:30:34 – uses a yeah uses a hashmap instead of at
00:30:37 – me but yeah that's resource management
00:30:39 – with shared pointer and map and it's
00:30:43 – great i love it never fails i'm just
00:30:47 – joking probably know there's no
00:30:49 – one-size-fits-all solution but i think
00:30:51 – it works in a lot of situations and it's
00:30:52 – very simple as you can see just one
00:30:55 – thing that again what I'm saying don't
00:30:57 – go ahead and you shared pointers for
00:30:59 – every g****** thing think first and
00:31:01 – always prefer unique pointer first only
00:31:05 – you shared pointer when you've actually
00:31:06 – got a situation of a resource being
00:31:09 – shared my different entities where
00:31:11 – either of the entities could die first
00:31:14 – that's the situation where you have a
00:31:16 – shared pointer if you've got one entity
00:31:18 – that's going to be alive from the
00:31:21 – beginning to the end of the resource you
00:31:23 – don't need a shared pointer you just
00:31:24 – make that entity the owner of the
00:31:26 – resource and he will keep it alive you
00:31:27 – will manage it for all of the other
00:31:29 – things that also maybe depend on that
00:31:31 – resource so shared pointer great but it
00:31:36 – doesn't see a lot of use in Michaels
00:31:38 – what I'm saying I use it when it's when
00:31:41 – it's warranted but I don't use it that
00:31:43 – often because usually you don't f******
00:31:45 – need it it has overhead compared to a
00:31:48 – unique pointer and it's not something
00:31:51 – you should worry about but again if you
00:31:52 – don't need it why f****** use it anyways
00:31:55 – hope you enjoyed this video if you did
00:31:56 – please click the like button oh s***
00:31:58 – this is how long has this been displaced
00:32:03 – office eyes anyways if you like the
00:32:05 – video if you liked my displaced IDE
00:32:07 – window please click the like button it
00:32:09 – helps a lot and I will see you soon with
00:32:12 – some more C++


Video Url:
http://youtu.be/IdY6pvVY0C0

Comments

comments