Input Polling | Game Engine series

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey little guys my name is trying to welcome back to my game engine series so last time we took a look at github pull requests and there's some of those in make sure that you check out that video if you haven't already today we're gonna finally get to something that I've been wanting to do for a really long time just for my own sanity really and that is polling input right so we set up this whole event system which is great because you're having an event system receiving events whenever we press a key or move the mouse or like the window resizes that's all fantastic and that's gonna form the basis for a lot of what we actually build in hazal but there is still the other side of input that is important and that is polling input so that's basically what I want instead of me being notified when something happens it's me being able to do at any point at any point in our program ask hey is this Kay down is this mouse button down you know am i holding the Alt key as I move my mouse because if we do that then something else might one we might want to change what that mouse move does based on if that's held down right for example it's a way in wave menteng a camera control all right and we want like a Maya style camera where we hold down alt and then like you know the middle mouse button a left mouse button to rotate you know and then we move the mouse I want to check the old keys down I want to check if the left mouse button is down and then if so when I move the mouse I might receive a mousemove event and on that Mouse move event if the alt and the left mouse button are down then I want to rotate the camera right stuff like that we want to be able to actually ask hazel if something is down now obviously this could all be implemented client-side from this event system that we've built it's very easy for us to simply be like okay well fine in my lair every time I get a mouse press to ban I'll just set the left mouse button to be pressed and then the mouse release tab and set it to false and that way I can check well yes you can but obviously that's a bit of a hassle having to do that for like everything and build your own system why build your own system to do that if that's something that hazel can actually provide so that's what we're gonna do today when you take a look at input we're gonna make a class called input like windows input because it's probably be platform dependent at least I mean the whole platform dependency King is a little bit shady right now because we are using gel of W which theoretically means that all desktop platforms will use gel W but I still as I mentioned and what I do with the windows window klass right I am splitting everything up per platform the reason I'm doing that is because in the future we might want to move away from Jeff W and we might want to use something else on Linux or Mac versus Windows and so because of that that'll be a huge refactor I don't want to do that I still want to keep all the platforms separate in case there's maybe something in those files that we end up making only for that platform so that's the reason for that let's jump in let's implement this input class okay so the first thing I'm going to start off by doing is over here in hazel I'm going to come over into source will explain platform and hazel and then just over here in hazel making sure that I've got show all files showing so that I'm not like creating stuff in filters I'm gonna add a new item we're gonna call it input on H and that's really all it's going to be we're just gonna have a header file that is our input this is very similar to what we had for the window header file but we just had a just this this window header file right and then from there it was just really an interface and then from there we had an implementation where we had windows window we're gonna kind of follow that same convention here we're gonna implement everything inside windows window and then this will just kind of be just that right but there's a bit of a difference because with window everything here they're just methods whereas with input input is going to be static in the sense that there's going to be one global input kind of manager and that's it whereas you can have potentially several windows so several things like that input I don't want to be I don't want that to be per window or anything like that it's going to be global now technically speaking you will probably have the like the separate windows thing is interesting because again I didn't want to make window completely static it is right now though because if you look at application and I put static you have one application per hazel that obviously has a single window so right now you can only have one window so whether or not window is static doesn't matter at this point but input for now at least will be static there are many ways that we could deal with that and they're kind of interesting complications to that because on one hand if you have multiple windows then maybe you want to pull input from a particular window on the other hand if it's static you can still do that you could just say that if you ask the input class if the mouse button is pressed it will be after the currently active window because obviously you can only have one window active at once anyway you can't have two windows focused at the same time on your computer so yeah there's a lot of things to consider I'm not gonna kind of spend too long kind of going through all of them I don't think that's necessary but anyway we're just gonna begin with the input class and see where that takes us so hazel API input I'm going to include I never know what it's cold anymore I think it's called something else in my reporting tree but hazel core dot H to get that hazel API thing and then we're just gonna have essentially just a bunch of static functions serve is keep rest it's kind of probably one of the most important ones we're just gonna take in a key card now right now this will end up being a GLW key card just because we don't have key codes we need to make our own key codes I've been stressing that for alot we might do that next episode but we need to have our own key codes because we can't tie ourselves to gel if W because of course that's going to end up being potentially different on other platforms as well as obviously like that's not gonna be a thing on like Android or other kind of devices once we kind of get into that space as well now here's the thing this is static however what I'm going to do is I'm actually going to make this as singleton so we're gonna have a single instance of this input class like that and then what we're going what we're going to end up doing is returning s instance and then probably the implementation of this function now this is where it gets interesting because whilst that is a thing what we're actually gonna end up doing is having protected virtual functions so essentially this is keypress we're gonna have a virtual version of that which is um not static we're going to call it is key pressed impulse sensor implementation and then that's going to take in a key code of course and that's going to be pure virtual so this is what we implement per platform in that actual like a realization I guess of this interface because this is just an interface we do have like a variable but it's static so it doesn't actually belong to this class class has absolutely no daughter in it this is just a static global kind of s instances does not belong to the class so that's essentially how this whole thing is gonna work and that's gonna call that and then there you go okay so is key pressed cause the implementation and then that obviously is something that will be kind of implemented by your interface so just to kind of show you show you an example before I add everything else in platform windows I'm going to make a new item and when we get header file it's gonna be called windows input dot H and then I'm also going to make a CPP file with the same name so windows input dot CPP I'm going to include my precompiled header of course every time you make new files by the way especially CBP files you need to rerun premi because as this stands I'm not sure whether or not this will actually be included in the program pilot headers it may be you can see it is set to use because that's the default for our project so actually maybe you don't have to rerun it I usually do anyway just for sanity but it's not something you have to do it so we'll include Windows input and dot H and then what kind of obviously just seven here I'm gonna copy pierce whoops and put it in here we're going to include hazel input and this will be glass hazel API well actually I don't probably don't want to actually export this into the DLL file so we'll probably just say like we don't I don't think we want it to be I mean we might yeah we probably won't have to we'll see how this works there so and then this will kind of just override that so protected this will kind of just override our function so our virtual bull is key pressed int key code okay so that's the override from input sorry this one obviously and then we'll kind of just implement that and then what this will actually do right is actually called G of W and then do everything that we need to kind of do that so this will be tied to jelf W which is fairly fine because it's inside windows it's inside our platform-specific CPP file so it's not gonna get compiled or in it anywhere where W might not be available what we're gonna do is we're gonna actually get the window from the application and then we actually need to get did you'll have W window so this is interesting because we do have an implementation for this I think but it might need to be changed because here's the thing right at the moment if you take a look at our window it's quite interesting actually I'm kind of glad that we're getting into this because it opens up a lot so at the moment we have no way to access this right we have a jail of W window however we can't access that at the moment now there's several things we could do we could make this public there'll be probably the worst thing you could do we could make windows input a friend of windows window that would make sense because input window kind of tied together could do that but because I want to kind of make this API somewhat powerful and potentially to people even in the client side who might want to for some reason do something that hazel doesn't do I actually want to function a public function in the API which exposes JFW window now I obviously want this to be somewhat portable and like I don't want this to be specific for windows window or anything like that I want to do too much of that so what we're actually gonna end up doing is inside a window we're actually going to add something here which is essentially going to be a virtual void pointer get native window okay and what this will do is we'll make that Const what this will do is actually return our gfw window though the reason it's a void pointer is because of course this doesn't have to be a djellaba window if we're using some other windowing library like this could potentially return like an H wind or something that could be anything right but in this case is returning just this native window okay just just this kind of gel W pointer in this case so we'll copy this we'll go into Windows window we'll paste this in we can make it inline and I'll probably keep it a special this is another question I get quite often why do I keep virtual in here right so I don't do it every way you can see I really don't I do sometimes I probably should do it more often but for example in here I kept this as virtual and people like well hang on do is something going to like extend this windows info class am I gonna have a child of this class and that's why I want this there the answer is nor it doesn't matter the thing is when you make something virtual over here in the base class everything down the hierarchy like everywhere down the hierarchy this function will be virtual so if I don't put virtual here doesn't matter it's still a virtual function why because it's obviously deriving from a virtual function so this is meaningless like just because you're inputted that does not mean this is suddenly not virtual okay so you might as well put it there because that's kind of a reminder of the fact that it is special it's kind of like why we use override although override actually will generate compile errors if you like you know miss clap something because you're not actually overriding anything whereas vegetable warrant but it's just a little reminder that this is in fact a virtual function again you might not need to have that because you have override there and that makes sense that obviously it's overriding a virtual function but usually like most people decide to actually leave this in here because it is in fact a virtual function so why not have just that extra readability in your code to kind just you know let you know that it is a virtual function so that's why I do that okay because it doesn't it doesn't make a difference whether you happen or not okay so um so there's now get native winter and this is just furniture to return our M window which is our gfw window fantastic now that we've got that back in windows input all we need to do to access our window and by the way the reason we need to access our window maybe they started with that is because we want to call GLW get cave okay that's going to tell us whether or not a key is actually down so the first thing that we need to cast into here is actually delighted to pass into here is our window pointer and then it's the key card so key card easy but window pointer something that we need to actually pass in that's what we're doing here we're getting window which is going to be equal to applications so we need to include hazel slash application we're doing application get and then get window okay this gives us back just a window reference once we have that window reference we can get the jail FW winter right by doing get native window so this is going to be we just leave this as order so this actually in this case if we look at this it's a void pointer that's fantastic but we actually need to be a chill of w window pointer right so we'll just start a cast it to that okay so static cast Jill have W window and then that so now this of course is the gel top your window pointer perfect we can pass that in to window and there we go a function is fine okay so this of course returns the state where it's kind of pressed on art so we'll say Auto state equals that and then here we just need to return whether or not it's pressed so return state equals jail toffee press right or Philip W what is it repeat right like that oops I don't know how I've had a static so there we go so now what we're doing here is actually we're asking GW if this key is pressed or repeat or whatever if it's held down basically then we'll return true otherwise if it's you know the other state which would be released then it's gonna return false so there we go there we have that pretty easy pretty simple and now we have a way to also get the native window which is really cool and really powerful okay so if we go back to input so hopefully this makes sense right we statically call input : : is key pressed we put in our key code which right now is the jelf w key code so obviously we need to include shelf top users which is a little bit unfortunate but that's just the way it is for now and then from here this calls the implementation of it which is a virtual function which in our case gets dispatched into this windows window sorry windows input class and calls this function which of course actually us gel W and that's how we get our keys so hopefully all of that makes sense okay cool so that is kind of how that works now let's go back and actually we'll go into input H and we'll do the rest of this right so the other things that we need to do for here is really just Mouse stuff and that's probably all will do for now so we'll have a static let's see this can all all be in line by the way because obviously it's implemented right yeah but it'll be a static flirt for example we'll get the outcomes in Mouse position building mask upon the first is mouse button pressed and then we'll do a la each button that will return oops is mouse button pressed button hopefully you guys can see that but if not then that's what it returns right and we'll make that function in a minute and then we'll do get Mouse X yeah mouse why and and you can like keep like filling this out obviously but for now I think that's probably it we don't know anything we need anything else for now that's all I can think of really oops and we'll just actually do this and that and that and these are flows okay so now we have everything we obviously need to implement this so back into windows window sorry windows import we go I always confuse the two oops all right so we can take this away and override I'll implement all of them just using visual assist and then we can just implement all of them really so again with mouse button exactly the same thing instead of Jeff whe we do gfw yeah mouse button window and button now there's no such thing as the repeats type the mouse button or so it's just going to be gelled up you press and then for this that's as well paste is in where we're actually going to use is Jeff W get as a physician and then we need window and then double X or Y pause as output parameters all right um and then we need to pass these in by their memory address so that's pointers and then we'll just return in this case s mother's Creek you could be like hey man that's look this is a waste of time like then you know this is you're doing both you're getting both here whereas you're only returning guest position and yeah I agree it's a bit annoying we could return both definitely as a tuple or as something like that in fact we might do that just now and I'll show you guys kind of how that works for fun so this is the same of Y put Y position so we'll go back to input and we'll do a quick little get Mouse position I'll put it up here get Mouse position just for turn 8s City pair of floor and flow it get Mouse position and cool let's copy this maybe I'm will just do that okay and then we'll obviously implement that in side windows input just like that there's Mouse stuff I'll create a function out of it and then this will kind of do the same but obviously over here it's just going to return the pair so x position Y position and the really cool thing about this by the way is that obviously with kind of simple cloth 17 if you're using c plus 17 which i think we are we can check quickly whereas this language yep civil has 17 what you can actually do as I'll show you here really quickly is if you get Mouse position like this which we might actually end up doing instead of doing all this you can actually do something like this right Auto XY equals get Mouse position and then we can just return X like that okay the reason this works is because you can basically orderable you can basically name these two floats that are being returned which is really really cool and it definitely makes tuples and pairs a lot more convenient because you basically can you know name this whatever you want instead of actually you know having to do pest City yeah zero you know this is my variable name feet like that to get like you know cuz that that's what it would have been like that's what you have to do in signals 14 or below but it's tables in C++ 17 you can just do this which is really cool alright so that is that and we don't have to cost or anything and we'll do the same for here as well so pretty cool stuff glad we out of that that's basically it right we've done absolutely everything in our info class the other thing that remains is actually creating this input thing right so we need to actually somewhere in our code we need to actually create this instance and of course since this actually has to be Windows like the windows instance we have to create it inside with a simple well we don't have to but we're gonna do it here so input as instance is going to equal a null pointer in this case but we're gonna just have it here and sorry this is a input pointer so it equals null pointer here and then we need to initialize our input as some in some way or another and that's what will actually set this up so a good place to do this would probably be Windows window and then all we really need to do is actually set it up here so this init function would for example be a really good place to do that however again because input is completely static it has no it's just data right it's just data on a collection of functions it does no initialization it's not like it has a constructor that relies on anything so what we can actually do here is just set it equal to new windows input right here in that static block and that's it right now we have this whole windows input thing which actually will statically initialize this so if we want to see this in our code maybe an application dot H or sorry applicational CPP all we need to do to actually use this is I will include inputs dot H and then over here maybe in the run loop or something like that I'm just going to do a check or trace we'll put this little thing in maybe we'll just do a zero zero one and then we'll put in I'll do order X Y as I did before input yet Mouse position X Y okay let's run this see what happens okay so we should see over here that you can see we have hazel and then the mouse position right it's also printing all the events that's a bit hard to see but there you have hazel and in the mouse position okay there your being printed so we can definitely see that that is in fact working anyway hope you guys enjoyed this video if you did you can hit that like button huge thank you to all the patrons on paper and upon 4/2 chatter that helped make this video possible it wouldn't be here without you if you want to support that you guys get the next video early so like you'll get it straight away basically it's music this public on youtube as well as the hazel development branch where I've already done all this stuff and way way way more so in kind of skip ahead and see hazel as it's actually being developed by me my spare time next time we'll probably implement those key codes and mouse button coins that I talked about um feel free to play around with this I own the tests of get math position but you guys should test get Mouse XY mouse buttons and key buttons as well and make sure all that's working so that's a little bit of homework for you guys if not if something if I did something wrong open upload a place on github last video will teach you how to do that and then we got home agitator and I'll fix it I hope you guys into this video I'll see you next time good bye [Music]
Info
Channel: The Cherno
Views: 31,841
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, game engine, how to make a game engine, game engine series, hazel, hazel engine, input, input polling, keyboard input, mouse input
Id: yuhNj8yGDJQ
Channel Id: undefined
Length: 25min 40sec (1540 seconds)
Published: Sun Feb 10 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.