I've been as busy as ever learning all I can while attending my CS degree, and I'd love to share what I learn for free with people that want to better themselves. Don't know programming much, but interested? Anyone can learn, it's not some difficult or far away unacheivable goal that only genius can reach (see below section for absolute beginners). I know of three different ways to program video games for new developers that I can recommend*: C, Flash, and Python.
*Can't recommend anything I haven't tried! There are definitely other options out there, these are just what I have personal experience in.
Python
Lets face it, Python has some very clean syntax. It also has some extremely newcomer friendly features such as dynamic typecasting -this means all variables are typeless to the programmer. You can assign a string to a variable, then an integer without compiler errors. There are wonderful resources like PyGame for developing games within Python. All around programming with Python is always extremely fun for me, and so I recommend it to new programmers looking to try out different things.
Flash
Flash doesn't have as awesome of a syntax as Python, in my opinion, but it's also very easy to use and learn to develop with. ActionScript is the name of the language used to develop Flash applications. ActionScript 3 is what is currently released to develop in.
There are immense amounts of documentation out there for anyone to get going on developing games in Flash, and due to the easy access of such resources and easy-going nature to get projects up pretty quick I highly recommend flash to anyone wanting to learn to program video games as a hobby.
There are even awesome places like Kongregate, or the iPhone app store that allows Flash games to have immense potential in generating revenue!
C
Now for C. Why C? Why not Java, or C++? Well I recommend C for those who have a desire to eventually work in a professional setting for game development in C++. Grounding yourself in C first gets you accustomed to working in a language that gives you complete access to everything you want. Langauges like Python hide a lot of low-level interaction from the developer to make things easier or more time-efficient. However, in real-time applications that are demanding on hardware require a much more fine-tuned and optimized structure to function well. C++ lets professionals do this, and it is the industry standard. However, I'm a strong proponent of learning C before C++. Learning C first lets you understand the limitations of C on an intimate level, and by learning of these limitations you learn to make highly effective use of the features presented in C++.
I'm currently at that stage of transitioning from C to C++, and so I'll be writing about this subject next. There will be a detailed post about object orientation within C, with regards to inheritance and especially to polymorphism, although I've written a short version here.
+ Show Spoiler [Class-like Objects in C] +
In C you can make use of structs and function pointers in order to emulate the class system within C++. If you've read my state manager post and understand the benefits it provides, then you can easily incorporate class-like objects in your C code. Each object is simply a structure. Each object holds the following function pointers:
The object struct also has a tag inside of it (just an integer will work, preferably an enum value) to determine what type of object your struct is. Then when the object is allocated through your memory management system, you initialize these function pointers to point to a set of functions corresponding to the type of object you are creating. For example I could have a file called Bat.c to create a bat object. Inside of Bat.c I have functions BatInit, BatDraw, BatUpdate, etc. When I allocate my object structure and want to create a new bat object, I initialize the function pointers within this structure to the ones in Bat.c. This then allows you to easily loop through a list of all live objects during your update/draw states, and make appropriate function calls to all your different types of objects!
The way this works is by making use of the void pointer. By using a void pointer as your function arguments within your objects, you can create classes, and even emulate inheritance with those classes. If all you do when you inherit from one class to another is extend the structure, then you can make use of simple inheritance. For example say you have a Structure.c file with the corresponding Structure functions to Construc, Destruct, Init, etc. Then, you copy/paste the Structure struct definition into a new header file called GoblinStructure. You then add in same data members to the struct. You can still pass your GoblinStructure to the Structure functions and all will be well. This is because the same data members will be accessed at the same points in memory with the Structure functions no matter if a GoblinStructure is passed or a Structure is passed. If this is confusing, wait for the better-documented post!
- Construct - Allocation and loading of any required space or data to attach onto the object via pointers. This would include AI data, animation sequence, etc.
- Initialize - Initialize allocated space from the Construct function, and initialize all data to prepare for use.
- Update - Uses delta time to update all of the object's data and perform all behavior logic.
- Draw - Render object and other effects from object to screen.
- Destruct - Deallocate all space allocated involving the object.
The object struct also has a tag inside of it (just an integer will work, preferably an enum value) to determine what type of object your struct is. Then when the object is allocated through your memory management system, you initialize these function pointers to point to a set of functions corresponding to the type of object you are creating. For example I could have a file called Bat.c to create a bat object. Inside of Bat.c I have functions BatInit, BatDraw, BatUpdate, etc. When I allocate my object structure and want to create a new bat object, I initialize the function pointers within this structure to the ones in Bat.c. This then allows you to easily loop through a list of all live objects during your update/draw states, and make appropriate function calls to all your different types of objects!
The way this works is by making use of the void pointer. By using a void pointer as your function arguments within your objects, you can create classes, and even emulate inheritance with those classes. If all you do when you inherit from one class to another is extend the structure, then you can make use of simple inheritance. For example say you have a Structure.c file with the corresponding Structure functions to Construc, Destruct, Init, etc. Then, you copy/paste the Structure struct definition into a new header file called GoblinStructure. You then add in same data members to the struct. You can still pass your GoblinStructure to the Structure functions and all will be well. This is because the same data members will be accessed at the same points in memory with the Structure functions no matter if a GoblinStructure is passed or a Structure is passed. If this is confusing, wait for the better-documented post!
I've been keeping a personal blog chronicling the steps taken to become a professional game programmer, as well as attempting to provide a free resource for anyone wanting to learn to program games.
As such, I'd love to share some excerpts from my recent posts with you all! Lets start with a continuation from where my last TL post left off. The last post was about the painter's algorithm, though the more recent ones were more generalized to apply to any coding project.
Excerpt: Variable Sized Struct:
In the previous post I had talked a little bit about image ordering, and image transparency. The way the image data was held in memory was very quirky! We had to create an entire header file for each image, and each image had to be an entirely different structure definition due to arrays of data being different from image to image.
I'd like to show you a method that makes use of some clever macros in order to achieve an interesting variable-sized image structure! The idea was actually brought to me by a friend of mine named Malcolm Smith. Here's the structure design for an object to be dynamically sized during run-time, in our case for images:
In the above structure we have two pointers, one to chars and one to colors. These pointers will point to arrays of unsigned characters, which is the datatype that represents both colors and characters for the Window's console. In order to access these arrays, you should recall that the name of an array can be treated the same as a pointer to the first element in the array. So after we allocate space for our arrays we'll need to initialize chars and colors properly by setting them to point to the first element in our arrays.
In order to go about allocating our space properly we need to allocate the space of the Image_ structure, space for the width * the size of our character array * height, and finally space for our color array * width * height. Here's a call to malloc to this in one single swoop:
The nice thing about this sort of allocation is that it isn't broken up into separate allocation calls, which speeds up the process since allocation is slow. This also keeps all the parts of memory in the same location in memory all adjacent to one another, as opposed to who knows where in memory when malloc is called multiple times, thus lessening memory fragmentation.
In the previous post I had talked a little bit about image ordering, and image transparency. The way the image data was held in memory was very quirky! We had to create an entire header file for each image, and each image had to be an entirely different structure definition due to arrays of data being different from image to image.
I'd like to show you a method that makes use of some clever macros in order to achieve an interesting variable-sized image structure! The idea was actually brought to me by a friend of mine named Malcolm Smith. Here's the structure design for an object to be dynamically sized during run-time, in our case for images:
typedef struct _Image
{
int width;
int height;
unsigned char *chars;
unsigned char *colors;
} Image_;
In the above structure we have two pointers, one to chars and one to colors. These pointers will point to arrays of unsigned characters, which is the datatype that represents both colors and characters for the Window's console. In order to access these arrays, you should recall that the name of an array can be treated the same as a pointer to the first element in the array. So after we allocate space for our arrays we'll need to initialize chars and colors properly by setting them to point to the first element in our arrays.
In order to go about allocating our space properly we need to allocate the space of the Image_ structure, space for the width * the size of our character array * height, and finally space for our color array * width * height. Here's a call to malloc to this in one single swoop:
Image_ *image = (Image_ *)malloc( sizeof( Image_ ) +
sizeof( width * height * sizeof( unsigned char ) +
sizeof( width * height * sizeof( unsigned char ) );
The nice thing about this sort of allocation is that it isn't broken up into separate allocation calls, which speeds up the process since allocation is slow. This also keeps all the parts of memory in the same location in memory all adjacent to one another, as opposed to who knows where in memory when malloc is called multiple times, thus lessening memory fragmentation.
...
I plan to continue on my series of creating Windows console game to the point where someone can pick up programming without any experience, and finish a fine product in C from scratch! I can remember back in highschool when I first decided I wanted to program for a living and had no idea where to start. Especially for game programming. Back then I wished I could stumble onto someone's blog and find good, concise explanations and answers, as I had nobody I could directly talk to about the subject. Hopefully I can help to solve that predicament for anyone here interesting in learning to program!
Recently I've had the chance to apply some of the concepts I've written about, such as Vector Physics and Program Structure/Design, in a game of Asteroids! Check out a video I made showing some ridiculous particles flying all over.
http://www.youtube.com/watch?v=zDdKNJnv_VQ
If you're the straightforward type, you can take a look at the pseudo code involved in the physics of the video above. It's actually extremely simple, and anyone with an understanding of basic algebra can achieve some very cool physics effects from very simple code:
// Find current direction vector
dirVect.x = cos( radianOrientation );
dirVect.y = sin( radianOrientation );
// Apply forward acceleration
if(keypress( UP ))
vel.x += ACCELERATION_FORWARD * dirVect.x * dt;
vel.y += ACCELERATION_FORWARD * dirVect.y * dt;
// Simulate friction
vel.x *= .99
vel.y *= .99
// Apply backward acceleration (negative forward)
if(keypress( DOWN ))
vel.x += ACCELERATION_BACKWARD * dirVect.x * dt;
vel.y += ACCELERATION_BACKWARD * dirVect.y * dt;
// Simulate friction
vel.x *= .99
vel.y *= .99
// Add a value scaled by dt to rotate orientation
if(keypress( LEFT ))
radianOrientation += ROTATION_SPEED * dt;
// Bound checking for pi and -pi
if radianOrientation > PI
radianOrientation = -PI
else if radianOrientation < -PI
radianOrientation = PI
// Subtract a value scaled by dt to rotate orientation
if(keypress( RIGHT ))
radianOrientation -= ROTATION_SPEED * dt;
// Bound checking for pi and -pi
if radianOrientation > PI
radianOrientation = -PI
else if radianOrientation < -PI
radianOrientation = PI
// Update position with our new calculated values
pos.x += vel.x * dt
pos.y += vel.y * dt
However, what if you're completely new to programming and know really just about nothing? Well, I've written a nice post about getting started with the C language, and I don't assume you know anything! In about an hour you can be well on your way to writing programs that deal with simple mathematical statements, and even print output of those statements onto the screen! Here's an excerpt from the blog post I wrote called I Want to Learn Programming, but I Know Nothing!:
+ Show Spoiler [Blog Excerpt] +
CecilSunkure wrote on his Blog:
So you want to learn to program. Great! So you're reading this article, and here you'll learn to start with C on a Windows Operating System. Starting with C is an excellent place to start, as it grounds you with a very solid base of understanding for programming in general, that of which I feel is easily transferable to other languages! I even feel that once you learn the basics of C and transfer to another language (I recommend Python!) you'll be better off in the long run than if you started with another language and then moved to C (perhaps start with Java and move to C). I won't get into deep details about this point as that's out of the scope of this article, but you should definitely take my word on the matter!
...
You can write your C code in any text editor you like. You can just simply use notepad (to open hit windows key+r and type notepad, then hit enter), though I recommend using the IDE apart of Visual Studio. Once you write some C code, save your file as a .c file and then use a compiler to turn it into a .exe file.
Now what about this compiler business. You might have already googled for a C compiler by now and noticed that there is a multitude of compilers out there. Luckily I have one to recommend to you! Download and install Visual Studio 2010 Express.
When installing you'll be ask if you have a CD. Just say no and download the installation from online. Once you see this screen (mine is reinstalling however):
Be prepared to restart your computer in the middle of the installation. For some reason this installation took a long time, so be patient! After you restart, at the end of the installation it says: requires a key. Go ahead and hit the button to acquire one; you'll need a microsoft address from something like live.com. Once you fill out this lame questionnaire with fake answers copy/paste your key into the box and continue! You're now ready to code your C programs, and compile them! ...
So you want to learn to program. Great! So you're reading this article, and here you'll learn to start with C on a Windows Operating System. Starting with C is an excellent place to start, as it grounds you with a very solid base of understanding for programming in general, that of which I feel is easily transferable to other languages! I even feel that once you learn the basics of C and transfer to another language (I recommend Python!) you'll be better off in the long run than if you started with another language and then moved to C (perhaps start with Java and move to C). I won't get into deep details about this point as that's out of the scope of this article, but you should definitely take my word on the matter!
...
You can write your C code in any text editor you like. You can just simply use notepad (to open hit windows key+r and type notepad, then hit enter), though I recommend using the IDE apart of Visual Studio. Once you write some C code, save your file as a .c file and then use a compiler to turn it into a .exe file.
Now what about this compiler business. You might have already googled for a C compiler by now and noticed that there is a multitude of compilers out there. Luckily I have one to recommend to you! Download and install Visual Studio 2010 Express.
When installing you'll be ask if you have a CD. Just say no and download the installation from online. Once you see this screen (mine is reinstalling however):
Be prepared to restart your computer in the middle of the installation. For some reason this installation took a long time, so be patient! After you restart, at the end of the installation it says: requires a key. Go ahead and hit the button to acquire one; you'll need a microsoft address from something like live.com. Once you fill out this lame questionnaire with fake answers copy/paste your key into the box and continue! You're now ready to code your C programs, and compile them! ...
There's also a sneak peak preview of a game I'm constructing with a team of 3 other members for our second semester project... Prepare to be in awe of our awesome pixel art madness! The idea of the game is you play as a Forest fighting against a faction of Grumpy Monsters who are corrupting the land. Game concept was inspired by this unfinished Flash game I found one day: link. The cool thing about the project is that it applies most of all the topics I've been writing about lately, especially the soon-to-come topic of Object Oriented C. How else would you make an RTS without object orientation?
Sneak peak screenshot of semester project!
I know you'll all feel at home playing an RTS game, which is what the above screenshot will be. I'll likely make a blog post about it when the time comes and hopefully have all you RTS pros playtest the game ^^
Future topics to write about and share with TL:
- Binary collision maps
- File in/out
- Object oriented C
- Simple 2D static collision
- Image z ordering via linked list
- Basic Tile-Based Pathfinding
- And more! Taking suggestions
+ Show Spoiler [Interesting Blog Responses] +
On February 28 2012 15:14 Rayeth wrote:
I think you have some good idea here, but I want to put forward that the C++ vs C argument changes when you don't want to work in a games setting. For example, I work in embedded systems and use C almost exclusively for various reasons (very low level access to things like timers, registers, or other hardware; need to save cycles due to low power processors; and the compilers aren't standard and therefore limit our language choice). So the choice you make on what language to use is really dependent on what situation you're in. Gaming on a modern PC is one thing, but there are TONS of systems out there that don't have gigabytes (or even megabytes) of memory for you to use, and in these cases C (or sometimes even assembly) are pretty much all you've got.
This changes the game completely, of course. Everything becomes about how to optimize memory usage and how to do more with less. Making things work in confining situation is an altogether different type of skill from the creative impulse (IMO) needed to develop games. There is often time very little visual feedback when things are broken, which makes for fun times figuring out bugs. I like to think it makes me very good at spotting and fixing memory leaks, but I can't say that for certain
Anyhow, that isn't to say C++ or things like Python, etc don't have their places in embedded designs too. There are LOTS of different systems out there. Most can be done in many languages and are relatively fast and easy to use. For anyone interested in doing (or even trying out) embedded stuff, I find the MSP430 from Texas Instruments a great starter. Plus at only $4.30 its pretty damn cheap and has a wide array of hardware for you to mess around with. Plus there is a great community that uses that processor family if you need basic help.
Anyhow. Good blog! And remember there are lots of things to do with programming besides gaming. Computers really are EVERYWHERE these days.
I think you have some good idea here, but I want to put forward that the C++ vs C argument changes when you don't want to work in a games setting. For example, I work in embedded systems and use C almost exclusively for various reasons (very low level access to things like timers, registers, or other hardware; need to save cycles due to low power processors; and the compilers aren't standard and therefore limit our language choice). So the choice you make on what language to use is really dependent on what situation you're in. Gaming on a modern PC is one thing, but there are TONS of systems out there that don't have gigabytes (or even megabytes) of memory for you to use, and in these cases C (or sometimes even assembly) are pretty much all you've got.
This changes the game completely, of course. Everything becomes about how to optimize memory usage and how to do more with less. Making things work in confining situation is an altogether different type of skill from the creative impulse (IMO) needed to develop games. There is often time very little visual feedback when things are broken, which makes for fun times figuring out bugs. I like to think it makes me very good at spotting and fixing memory leaks, but I can't say that for certain
Anyhow, that isn't to say C++ or things like Python, etc don't have their places in embedded designs too. There are LOTS of different systems out there. Most can be done in many languages and are relatively fast and easy to use. For anyone interested in doing (or even trying out) embedded stuff, I find the MSP430 from Texas Instruments a great starter. Plus at only $4.30 its pretty damn cheap and has a wide array of hardware for you to mess around with. Plus there is a great community that uses that processor family if you need basic help.
Anyhow. Good blog! And remember there are lots of things to do with programming besides gaming. Computers really are EVERYWHERE these days.
Interesting links to get started:
http://pygame.org/ - The PyGame engine! Free engine that's very easy to develope games very quickly with! Extremely fun.
Invent your own games with Python - Very cool book on making simple games with Python!
http://kongregate.com/labs/ - Wonderful website for Flash game development.
http://forums.tigsource.com/index.php?topic=14588.0 - Awesome thread on ascii art, with tools to create
http://labs.bp.io/2011/ascii-paint-bpio-fork.zip - My favorite tool to create ascii art
http://www2.warwick.ac.uk/fac/sci/moac/students/peter_cock/cygwin/part1/ - How to install Cygwin to use the GCC compiler
http://www.crimsoneditor.com/ - Favorite text editor for writing code
http://cecilsunkure.blogspot.com/ - My personal resource to start programming, especially games