DaBooda Turbo v1.4 - Tutorial C |
A Little Bit of Everything (Mouse, ViewPort, Counters, SoundBuffers) |
Andrew "DaBooda" Stickney |
August 10, 2004 |
Welcome to the third tutorial in this series. In this one we will cover a little bit of everything, alot of new stuff in v1.4 and I am trying to cover it all. In this tutorial I will only cover what is new, I have said this before, but if you are not yet comfortable, go back until you are. It only gets more intense from here on out... well not really but you should be comfortable at least.
Lets jump into this thing.
Ahoy! Matey! I See Land Through That ViewPort! |
Besides the fact that is the most stupid title I have ever come up with, you might have noticed these two new commands.
'clear screen Engine.ClearScreen 'Set up view port Engine.SetUpViewPort 64, 16, 512, 448 |
Lets concentrate on the last one first. This does what it says, it sets up a view port. What this means is the engine will only clear and render to this portion of the screen. This is very handy, especially for me because I will be translating alot of console type games to this engine, and the resolution on consoles is nowhere close to computer monitor. One thing to keep in mind, just because your view port is set up, doesn't mean your map view is, so it is important to set that up to corrospond to this, same with over view and text view, for you will not see anything outside of the view port.
The first command is there for good reason. Go ahead comment it out and run the program. Ok, lets hope none of you were eptileptic or suffered flash backs to the 70's... because that strobe light show was annoying. This is because in fullscreen DirectX8 is set up to use a flip method to flip the screen. When we have a viewport set, it will not clear around the edges of it, becuase the device will only clear what is going to be drawn. This is why I used the clear screen command before setting up the viewport, this clears both screens in the flipping chain to the back color... in other words take that strobe light out.
Please note that this does not cause a problem in windowed mode, but it is good practice to get into. Also this command is a render type command, so please have your FPS set up or you will recieve an error. This is my fault for not putting a default FPS into it, sorry about that.
Love Grid, Baby Love Grid! |
Ok, Lets talk about that handy little frame that seems to be locking to a grid, this takes two different codes to do this, but it is easy trust me. And yes thes titles are getting worse, its late guys and the human body shouldn't drink 4 pots of coffee in one day.
'this is for the frame .SetGridSize 64 'Update Frame With Engine .DBSprite.SetXPosition 66, CSng(.DBMouse.GetGridX) .DBSprite.SetYPosition 66, CSng(.DBMouse.GetGridY) End With |
Lets look at the first command found within the mouse init routine. Well this simply gives the mouse class a number to be used later by the next two commands. This basically lets the mouse class know you wish to divide the mouse view into 64x64 units. Why would this be handy, can anybody say EDITOR!!
Well just because we told the class to divide it into that, we have to put this into use so take a look at the last two commands. What this does is finds the mouse's current X and Y and figurs which grid it would be on... see that is easy right. You might be wondering about the CSNG, well the mouse class uses nothing but integers, this is because I wanted to keep out any decimal values, well the x and y position of a sprite are singles, so conversions are necessary, vb is full of them, check them out, makes programming easy.
I Am Suffering Multiple Sound Buffer Disorder... |
Upon running this program you might have noticed that annoying ding a ding sound when you moved over a block. Well I hope you realized that that sound was being played multiple times at the same time, and this is just one sound. This is done with one simple little command.
'set up secondary buffers to play multiple instances of one sound Engine.DBMath.SoundSetUpSecondaryBuffers 5 |
What this does is lets the SoundSpritePlay command in the math class know that hey I want to hear that sound alot and it declares a set of blank buffers to do this. What this really does is it each time this command is then called it finds the next blank buffer copies the original sound into it, and plays it. This was really created for your shootes, because there will more than likely be alot of explosions. Sounds alot better now doesn't it, but this will only work with that one command, and really what other command would you use, for that is the only command to play directional sound with anyways.
One thing to keep in mind is that I have a 100$ sound card in my system, but a lot of people are using integrated sound, which means that the buffers are then part of the system memory. Well you can either wait for everybody to become serious about gaming or you can keep the above number low, for I can run about 100 of those secondary if not more, but some may suffer at over 5... so either keep it low, or give them the option to change it... I like the latter choice.
The State Of Sprites To Come... |
Ok, lets talk a little bit about advanced menu toggling... this method is very handy if you wish to change menu items as the mouse hovers and leaves an item. If you notice in the program that the blocks turn bright blue and stay blue until you leave it. Well this is done through the simple use of three totally different concepts, Sprite State, Counters and Hover commands.
If Engine.DBMath.MouseHoverSpriteA(I) = True And Engine.DBSprite.GetState(I) = 0 Then 'sprites first time to hover Engine.DBCounter.ResetCounter I Engine.DBSprite.SetState I, 10 Engine.DBMath.SoundSpritePlay 1, 65 ElseIf Engine.DBMath.MouseHoverSpriteA(I) = True And Engine.DBSprite.GetState(I) = 5 Then 'sprites not first time so flip Engine.DBCounter.QMFlipMinMax1 I, True Engine.DBSprite.SetState I, 10 Engine.DBMath.SoundSpritePlay 1, 65 ElseIf Engine.DBMath.MouseHoverSpriteA(I) = False And Engine.DBSprite.GetState(I) = 10 Then 'sprite is no longer being hovered, so flip to decrease specular Engine.DBCounter.QMFlipMinMax1 I, True Engine.DBSprite.SetState I, 5 End If |
Looking at the above code you may be thinking to yourself, wow, what a mess, but trust me there isn't that much going on up there. Lets break that down even more:
If Engine.DBMath.MouseHoverSpriteA(I) = True And Engine.DBSprite.GetState(I) = 0 Then 'sprites first time to hover Engine.DBCounter.ResetCounter I Engine.DBSprite.SetState I, 10 Engine.DBMath.SoundSpritePlay 1, 65 |
Lets look at the first set of commands. This basically only runs one time and that is the first time. When a sprite is first added its state is 0, state can be anything you wish, for the engine ignores this. The hover returns true and the state is 0, so therefore this runs, what it does is set the state to 10, which basically just says hey wait for me unhover the sprite. Now also the counter since it was never ran, you have to reset it, basically turning it on, this is what starts the blue to glow on the button. Also notice how I play the sound on this one. Ok Lets look at the last if statement for when the state is 10.
ElseIf Engine.DBMath.MouseHoverSpriteA(I) = False And Engine.DBSprite.GetState(I) = 10 Then 'sprite is no longer being hovered, so flip to decrease specular Engine.DBCounter.QMFlipMinMax1 I, True Engine.DBSprite.SetState I, 5 End If |
And yes I do know that I skipped the middle one and there is good reason for this. This last if statement, basically just waits until the mouse leaves the sprite. The state declares that the sprite already has the mouse over it, so we wait. When this finally does happen, notice it flips the min and max, this makes the counter decrease, which in turn makes the block go back to normal color. Notice how we set the state back to 5 instead of 0, this is for the counters sake, this way we don't reset it next time we hover we only flip it again... like so:
ElseIf Engine.DBMath.MouseHoverSpriteA(I) = True And Engine.DBSprite.GetState(I) = 5 Then 'sprites not first time so flip Engine.DBCounter.QMFlipMinMax1 I, True Engine.DBSprite.SetState I, 10 Engine.DBMath.SoundSpritePlay 1, 65 |
This if statement is real similar to State 0 or the first one, except we flip the counter now. This one still plays the sound and the state is still set to 10. This may seem like alot of effort for a little bit, but for multiple menu items it really isn't that much work. If this method wasn't being used the sound would play constantly, the counter would never flip, so the block would always be blue... see the reasoning behind this method.
I implore you to use this method when making menus, I do not want to see cheesy menus in any game programmed with this engine...lol... please...
Tying Up Loose Ends |
A few things I wish to cover before ending this tutorial.
First when running this program you may have noticed that sometimes blocks adjacent to eachother will both glow, this is because rects overlap by one pixel. This is a good reason not to just things together that you wish to hover.
Second there are two commands for mouse hovering that I haven't covered and really couldn't figure out a tutorial for them. But they are easy, and can come in handy for some instances. Whenever you use hover type A commands for Sprites or Overlays, two variables get filled with the distance from the upper left corner of the object. These get filled regardles if the hover check was true or not. You can find out what these values are through the MouseReturnXOffset or MouseReturnYOffset commands in the math class. These may come in handy... for maybe image maps, or value bars or anything you wish.
Thirdly and lastly, do not feel limited for any reason, I feel this engine has grown to a point where anything you wish to do, can be done, just be creative with the mixture of commands, find the best way to do things. Also if anyone has any ingenious ideas or has found pleasant side effects please report them to us, so we can share with the DaBooda community.
This is it for this tutorial, this one really had no purpose but to wrap up a few little odds and ends, from here on out we will be getting into some pretty cool stuff. Some of the stuff we have covered I might have rushed, or not explained right, if so email me and of course I will answer you. masterbooda@yahoo.com .
And yes I want to start seeing some games, so go forth and experirment, and keep doing so, and read on master programmer! I know go to bed right, going trust me...