Game Design, Programming and running a one-man games business…

Going from BASIC to C++, in understanding game source code

I was chatting to someone about a year ago, who was embarking, for research purposes, on how to code a game in C++. They were familiar with the rudimentary concepts of coding, and with games, but had never looked at C++ before, or the source to any modern game. They had enormous problems going from the programming they leanred briefly as a kid which was this:

10 PRINT "I AM L33T!"
20 GOTO 10

And modern game source code. The main difference is that a C++ game is ‘object-oriented’ and the languages people start with are just simple lists of instructions, executed in order. This is a 30 second guide to helping newbie coders cope with the difference. The biggest problem you have, is the question “Yes but where does the code actually START?”. And the answer is, a function called WinMain. Basically, the structure of any game is this:

WinMain()
{
Create A Window();
InitialiseGameStuff()
while(true)
{
CheckGameNotQuit()
GameLoop()
}
}

That’s basically, Kudos, Democracy, Starship Tycoon, GSB… etc. It’s all there. that is the whole game. The real stuff happens inside GameLoop(). That is basically this:

GameLoop()
{
BeginFrame()
DoAIStuff()
DrawStuff()
EndFrame()
}

And that obviously splits out into a billion other functions. Now already, it might look scarily nothing like 10 PRINT SCREEN, but just imagine that GameLoop() is a GOTO that jumps to the start of GameLoop() and imagine a RETURN statement at the end of that section. The weird bit, for new coders, is that the code will jump back and forth all over the place, between different source files, in different orders, up and down and all over like a ping pong ball, but, ultimately, when the chips are down, it’s not really *that* different from old BASIC programs. Until you multithread it, anyway :D.


13 thoughts on Going from BASIC to C++, in understanding game source code

  1. People who teach new coders using basic should be shot :)

    Starting with a c#/java command line program would be far easier.

    At least today.

    Also that’s not really oop, it’s still procedural. But I’m nitpicking :)

  2. Now, that’s not really your point, but I can’t help but point out that WinMain() is Windows-specific, and that everywhere else the execution starts in a function called main(). Maybe Xboxes also have WinMain?

  3. Yeah, I was going to mention that there’s nothing OOP going on here. Sure, inside those functions there may be some objects. I may be wrong, but a more OOP approach here would be an event driven system.

    But anywho. I don’t think anyone actually teaches that style of basic anymore anyway. VB is much more like Java, and obviously so is C#. Oddly I found PHP one of the best gateways, for myself, when trying to sort out more advanced programming concepts and practices. It’s lacking in some ways but RealBasic is also quite nice.

  4. “imagine that GameLoop() is a GOTO that jumps to the start of GameLoop() and imagine a RETURN statement at the end of that section.”

    Surely you mean GOSUB, not GOTO.

    10 PRINT “DIXONS ARE SHIT!”
    20 GOTO 10

    ;)

  5. The stuff about teaching OOP first is nonsense, people insist on doing that because it deters people from programming and keeps the market in demand. It works, too.

    This is how I learned, and I think it’s really the only way, because every step consolidates and organises what you learned in the previous one.

    Sequential programming (basic with gotos)
    Procedural programming (some versions of basic, javascript, pascal etc)
    Object oriented programming (C# and actionscript 3 are my favourites)
    Then design patterns if you really want to show off, though I’ve yet to work on a project that really needed them.

  6. I tried to learn qbasic back in the day…and wasn’t even really good at that. then tried to learn visual basic and really wasn’t any good at that either. There’s probably no hope for me ever moving past the n00b stage in any coding language, and I think that it’s because I can never wrap my mind around the more advanced functions like arrays and coding designed to save you from typing the same 1000 line block of code 6 times with slight variations. When the real math kicks in, it’s game over.

  7. My high school taught me BASIC, but it was a bit of a joke. The teacher was using a 15 year old lesson plan and the computers were about as old.

    What really taught me the fundamentals were Drag and Drop programs. Specifically Game Maker. With its own language, I was slowly able to transition from Drag and Drop to GML. I.E. I was able to graduate from learning the concepts to learning how to write the concepts in a programming language. From there it’s only a matter of learning how to do the same things in a more respected language.

    Also, the Game Maker community is use to and willing to help novices.

  8. “a C++ game is ‘object-oriented’”
    Really? Where are these objects in your loop? C++ is a multi paradigm programming language!

  9. @Tim: Hmmm, I guess I haven’t really given game maker a serious shot. Might be just what breaks me into the coding world. . .1`

  10. For people who are trying to go from BASIC or some other now-ancient BASIC-like language to an object-oriented one, I HIGHLY recommend Game Maker and Alice (the Carnegie Mellon Java API). Both are designed to be extremely user-friendly, easy to learn, and 100% object-oriented from the start. Game Maker is a bit “stealthier” about which of it’s features are like curly-brace languages, but it’s slightly easier to learn how to use. Alice is basically a front-end for Java so it’s a bit more involved and more appropriate for a classroom environment than an independent study. But Alice is specifically designed to ease the learning curve, so it’s much less of a jump from Alice to Java or C++.

    Incidentally, both of these are so good at teaching you how to program, that I ran into problems when I took Structured Programming (the pre-CS1 C++ course) because I was trying techniques that were way ahead of the rest of the class but I didn’t properly understand the syntax yet. In one example, Visual Studio compiled the program fine but when I ran it, Windows kept threatening to report the program to Microsoft because it was “potentially dangerous”. My teacher was impressed at my attempt to try recursive function calls on my own, but gently suggested that I stick to ordinary loops until she actually taught me how to do recursive function calls.

    Re: Jim, obviously he doesn’t want to confuse the non-coders with prototype declarations and instances and classes and all that. It’s pesudo-code, not real code.

    (Oh, and I see someone else mentioned GM as well. Well, consider this another endorsement for it.)

  11. I feel everyone is nitpicking over programming syntax instead of addressing the real shift in gaming. The main reason new programmers/developers struggle is because what people expect out of a game has drastically changed.

    Anyone with average intelligence and literacy could make a basic, skeleton game in under 5 minutes with specialist toolkits like TWINE: http://gimcrackd.com/etc/src/

    The real problem is that new developers struggle with the next logical step: using specialist libraries/APIs/frameworks such as Direct X, OpenGL, XNA, Pushbutton Engine etc. Show them portions of the code and most people could puzzle out what’s going on. There might be the odd maths literacy or design pattern quirk that they wouldn’t immediately understand but the basic flow is pretty similar across all languages. What new developers don’t understand is which functions/tools already exist and how to get them to work together to make a game that’s recognizably from this decade.

    They assume that they have to write everything from scratch by themselves, mostly from memory, in a single pass. It’s like someone that’s only ever written 100 word stories in primary school trying to write a full blown screenplay because they’ve had some success with madlibs, all without the benefit of storyboards, understanding of dramatic arcs, stock characters, editors etc. It’s not utterly impossible but it is totally unnecessary and demoralizing.

Comments are currently closed.