|  | 
 
Introduction 
 The Blitz Version First Ok, let's start with the Blitz source. This was written in Blitz3d, but will work perfectly in Blitz3d and BlitzPlus and should work without modification in the original Blitz2d also. Have a good look through the code and make sure you understand what's happening. This is far from the fastest or best Pong clone around, but it [i]should[/i] be one of the easiest to understand. I've broken everything up into small steps to make it very obvious what's going on at each step of the way. If you can't see what's going on, either my code needs some work, or perhaps you're not ready to learn C++ just yet. Some parts may seem a little unnatural, but I've just organised the code to most resemble the C++ version which comes later. The key thing here is how similar the code will be and how C++ really shouldn't be intimidating. Now Let's Rewrite That in C++ Now we come to the C++ code for the same game. If you're following along with the tutorial yourself, make sure you set up your compiler to point to the HGE headers and the HGE library for your compiler ( it comes with libraries for Borland, Visual and Dev C++. ) and you're ready to go. There is a tutorial which explains how to set up your compiler here : http://hge.relishgames.com/doc/index.html but the tutorial is really for Visual C++ Fortunately, Dev C++ is not too much different, and you will you probably be able to do this yourself. If you need help, here's a step by step : Firstly, go to "Tools\Compiler Options" and click on the "Directories" Tab and then the "Libraries" sub-tab. Add an entry here and point it to HGE14\lib\GCC ( including the path to wherever you installed HGE, obviously ) Next click on the "C++ Includes" sub-tab, add an entry and point it to "\HGE14\include" ( Plus the path to wherever you installed HGE ) That's all you need to do for my game, because I have set up the project to include the library for you. If you're starting from scratch, create a new folder on your hard drive and copy the HGE.dll and the Bass.dll files to this folder. I won't be using any sound commands, but it's good practice to get into the habit. Now start a new project file in your C++ compiler and make sure you include the HGE library and the HGE Helper library so that we can use the HGE functions in our program. Start a new .cpp file, call it anything you like, but main.cpp is what I chose. Add this to your project and paste the following code into your main.cpp file. If you're using my Project File, you can just open it up, as all this has been done for you. Not as Bad as you Thought? Now hopefully this won't seem too intimidating. It shouldn't be, as many of the commands are practically identical to how you would do things in Blitz. Let's pull out some differences and explain them. Include files include is used to include code from an external file, just as you would with Blitz. # is used for preprocessor directives, but you need not be concerned with this. It's just to tell the compiler that this is an instruction for it to deal with ( in this case to add in some extra code here ) rather than an actual instruction to compile. As the comments state, math.h and stdlib.h are standard libraries that come with all C++ compilers and the three HGE includes are necessary because we want to use the HGE library instead of writing our own graphics and input code from scratch. Declaring Variables These are just variable definitions. In Blitz, you can use variables without declaring them first, but this is not the case with C++. Your compiler will throw up an error if you try to address a variable without declaring it. In Blitz, we would declare these variables with % and # for integer and floating point, but in C++ we simply place the keywords int and float in front of the variable name. As with Blitz, we can initialize the variable with a value, and separate multiple variable declarations with commas. As a side note, there are no global and local keywords in C++. If you declare a variable outside of all functions, it's global. If you declare it in a function or other block of code, it's local and can only be used within that function or block of code. Delta Timing HGE has inbuilt delta timing. In order to get your game running at the same speed on all machines, all you need to do is retrieve the delta multiplier, as in the code above, then multiply all movement by this variable. Don't worry too much about the -> at this stage. If you decide to learn C++ you'll learn this in good time, but for now, just think of the whole thing as one long function name. Input and Function Returns This is a great line of code, as it shows several things. Firstly, it shows that you return a value from a function just as you do in Blitz. Secondly, it shows you how to do keyboard input. HGE has a function called Input_GetKeyState() which tells you if a key has been pressed. HGEK_ESCAPE is simply a constant for the escape key. You may well have done something similar in Blitz to avoid typing scancodes every time. Dead simple, right? Conditional Statements Ok, here's an If.. Else.. condition structure. It's almost identical to Blitz. The only difference is that you use a curly brace after the if, and one at the end instead of an endif. All blocks of code are marked this way in C++, so you get used to it very quickly. The other slight anomaly you might have noticed here is the += This is simply shorthand for adding something to a variable and putting it back into the same variable. Instead of saying a=a+12, you simply say a+=12. It doesn't seem a lot better initially, but it sure saves a lot of typing when you have variables called player_one_first_series_score. The Graphics Part Ok, here's the meaty bit. This is the graphics stuff. You know it's the bit you're interested in, you eye candy whores :P Gfx_BeginScene() is simply a function to tell HGE we're going to start drawing now. Gfx_Clear(0) clears the screen, exactly as you would at the beginning of a Blitz2d drawing loop. Now the next bit might be a little confusing, but it's really very simple and very useful. Instead of having a function called DrawImage to which you pass the image handle, each sprite in HGE is an object, and has a method called Render. Don't worry too much about objects and methods. All this means is, you write it the other way around. If Blitz did things this way, it would just be Image>Draw rather than DrawImage(image). It's just the same thing written another way. The printF bit is exactly the same. Instead of calling a function and passing a font to draw with, you just access the font's own print function. It's the same thing the other way around. HGE uses bitmap fonts automatically, so it's always superfast and you never have to worry about loading ttf fonts. Finally, Gfx_EndScene() is the function we call to tell HGE that we're finished drawing things. Setting Things Up hge_Create is simply a function to initialize the entire HGE engine. The following calls to System_SetState, are simply setting a few important system variables. Ignore the first two for now, as they're really not important and don't need changing. HGE_Title is the text you want on the title bar of your window. It's the same as AppTitle in Blitz. The following four are simply the parameters you would normally feed to Graphics when you set it. The code above is the Blitz equivalent of Graphics 800,600,16,2. Creating New Sprites Don't worry too much about the syntax here, but basically, we're just create a new sprite. Battexture is the texture we want it to show, and the numerical parameters are the pixel coordinates you want to use. 0,0 is the pixel coordinate of the top left pixel on the texture you want to be on your sprite. You could, for example, put four 16x16 sprites on one texture and in that case, you would create subsequent sprites with x and y offsets of 16,0 0,16 and 16,16 respectively. The final two numerical parameters are the width of the texture, or part of the texture you want to use for this sprite. The next line simply sets blend modes. They're just constants that HGE has built in, and you can look them up in the HGE documentation if you want more information. SetHotSpot is exactly what it sounds like. It's a function to set at which part of your sprite you want the handle to be. In this case, I've set the hotspot at 32,8 on a 64x16 texture, or in other words, I've set the image handle right in the centre. Summary Hopefully the rest of the code is pretty self explanatory once you've had a little read through it. Much of the code is just calls to certain HGE functions. Those are much like Blitz functions in that you end up memorizing them parrot fashion, and don't really need to know much about them. The names are very self-explanatory too, of course. Feel free to delve into the code and change a few things if you want to. It's not going to break anything. If you really, really do something stupid, you might get an illegal operation. Big deal. Most of the time, the worst you're going to get is a compiler error. Change a few simple things, and build up from there, and you'll probably avoid errors altogether. Now hopefully this little tour through a Blitz game and how the same game looks in C++ has made you realise that C++ isn't intimidating at all. In fact, at it's most basic level, it's hardly any different from Blitz. Hopefully you've now got an interest in learning more, and I highly recommend learning C++, if only because it will help you learn new things to use in Blitz. I should end with a warning that this is only easy because you've replaced Blitz2d with another library. If you were going to get down and dirty with DX8 yourself, or even with OpenGL, Bass, etc, it would be considerably more complicated. But, as you can see, you really don't need to do that. You can make fun little games, and even big games, without ever getting into DX and OpenGL directly, if that's what you want. HGE is just my prefernce. If you want to look at other similar libraries, SDL and Allegro offer similarly easy interfaces to graphics. PTK is very similar to HGE, but uses OpenGL instead of Direct3d. If you hanker to play with 3d, I highly recommend TrueVision3d, which has been designed to be very easy to use, as well as very powerful. All of these libraries are free for non-commercial use. Some Links and Suggested Reading on the Subject Game Libraries for use with C++ 
 Books and Tutorials on C++ The book I'm using to learn C++ is called "C++ Nuts and Bolts for Experienced Programmers" by Herbert Schildt. I believe it's out of print now, so you may have trouble tracking it down. If so, I understand he has more recent books which cover the same ground. This is an excellent book, and MUCH more beginner friendly than it sounds. I've tried to read a number of other books, including some intended for beginners and found they overcomplicated some things considerably, at the same time as explaining other things as though they were talking to a chimp. This book assumes you already know how to program in another language and just tells you what you need to know in order to learn C++. It doesn't insist on making things sound more complex than they really are, but equally it doesn't gloss over anything. There are, of course, a number of great online resources for learning C++. If, like me, you're starting with Dev CPP because it's free, you can find a list of recommended reading here : http://www.bloodshed.net/dev/doc/index.html Remember, all supporting files including, source, exes, project file ( Dev Cpp only ) and all required media have been uploaded here Click Here Any errors, ommissions, etc, please let me know and I'll take care of them. You can either email me at sybixsus@gmail.com or you can post on the article discussion thread on the forums. For a printable copy of this article, please click HERE. 
 This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.   |