Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » Why I write games in C

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Why I write games in C
AceBlkwell
Member #13,038
July 2011
avatar

I agree with Neil, You've got to go with what works for you. Familiarity is always a factor.

I remember writing in QBasic Interpreter years ago. I installed Quick C 2-3 times before making myself use it. It didn't make much sense at the time to go though all the setup with includes and main() and {} etc when I could just say PRINT and BAM there it was. I did learn over time though that with complexity can come options.

I guess the trick is to determine what you are looking to get accomplished and how much effort you are willing to put in to getting the desired results. How much do you want to know about the details, or how much you'd rather use a black box approach with language commands/ macros.

I also agree with Neil in that global scope taboos are often overstated. I have wasted a lot of time trying to "be a good programmer" just to realize there is nothing wrong with a global variable on occasion. ;D. It's much like I don't agree with never signing on as Root in Linux either. I ONLY sign on as Root. See I live on the edge like that..

;D
Ace

Neil Roy
Member #2,229
April 2002
avatar

I used to program in QBasic and QuickBasic 4.5 (compiled version). What helped me move to C was a great book I found called QBasic to C. It got bad reviews as they stated that it didn't teach proper C, which as usual, was nonsense. It done what it said, it showed you QBasic and then showed you how to do the same in C. Hardly useful these days as nobody starts out with BASIC anymore, but great at the time. From there I used DJGPP under DOS and started to program my own library for graphics by reading various resources I found that helped me learn how to access video RAM etc... when I found Allegro I abandoned writing it myself. I wish I had never found Allegro now as I would have continued programming my own library and probably gotten a lot further.

I still have some of my original library code and QuickBasic code. Good times, I miss those days honestly (in the'90s).

Some of my DOS/DJGPP code from my VGA library...

#SelectExpand
1void set_video_mode(unsigned char mode) 2{ 3 __dpmi_regs r; 4 r.h.ah = 0; 5 r.h.al = mode; 6 __dpmi_int (0x10, &r); 7} 8 9void plot_pixel(int x, int y, unsigned char color) 10{ 11 _farpokeb(_dos_ds, VIDEO_BUFFER+((y<<8) + (y<<6)) + x, color); 12} 13 14unsigned char get_pixel(int x, int y) 15{ 16 return _farpeekb(_dos_ds, VIDEO_BUFFER+((y<<8) + (y<<6)) + x); 17}

---
“I love you too.” - last words of Wanda Roy

princeofspace
Member #11,874
April 2010
avatar

The first C I ever wrote was based on stuff Shawn had for the old Allegro 1 examples. Those WERE good days...

Neil Roy
Member #2,229
April 2002
avatar

I still have the DOS source for my first Allegro game I ever completed. I was just playing it today (an Artillery Duel game I called "Deluxe Artillery Duel") which I completed in 1999. I am thinking of resurrecting it and remaking it just for fun. It's funny to look at my code from back then. I still had many bad habits. While I don't mind globals, I had WAY too many of them back then!!! LMAO, my coding style wasn't set either, fascinating to look back at. Glad I'm a packrat and keep all these things. I still have Deluxe Paint 2e I used to make my graphics with.

---
“I love you too.” - last words of Wanda Roy

Specter Phoenix
Member #1,425
July 2001
avatar

Deluxe Artillery Duel
Deluxe Pac-Man 2

Starting to see a pattern here.

I went from BASIC in middle school (7th & 8th) to C++ in high school. I dabbled in other languages, but can't say I've made anything of substance with other languages.

The oddest thing I've ever seen has to be when a guy declared all his functions at the start of the program and then defined them a few lines down.

Neil Roy
Member #2,229
April 2002
avatar

Quote:

Deluxe Pac-Man 2

Nope. It's Deluxe Pacman. "Pac-Man" is a trademark of Namco or whomever (I researched it) so I avoid "Pac-Man" like the plague. You may be surprised to learn that "Pacman" however is not trademarked to any game company (there is a "Pacman" hat maker in Korea though, but that isn't related to games, so no problem). :)

The oddest thing I've ever seen has to be when a guy declared all his functions at the start of the program and then defined them a few lines down.

WHen I first used C, the common teaching was to declare all your functions at the start, your prototypes. The idea was that main was always the first function you seen, and any others could easily access your functions once they have been prototyped first.

These days I just declare the functions in a logical order so there are no problems, but that is the way C was originally meant to be done. My Deluxe Artillery Duel game has them all prototyped like that. And there is a certain bit of tidiness to it all I guess.

I still have much of my old BASIC code too, I had it all backed up and was browsing it today. Found some code I wrote for an artillery duel style game in QuickBASIC 4.5. The reason why I moved to C was that BASIC just wasn't fast enough for what I wanted at the time, not even compiled BASIC. It is now. I ran the original BASIC version and it was really fast. Also found some of my first experiments in C with artillery duel and it made me laugh as I had a tank shooting randomly all over and some of the shots were aimed straight up so they hit the player's tank again. I should make a video of them all, showing the progression just for fun. The original C code is scary to look at, the globals I used at the time will give you a heart attack! ;D

Oh, if you want to relive the BASIC nostalgia, there is a modern version of QuickBASIC for windows. Runs old code and new, compiles etc. I should load up some of my old code on it and see how it runs now.

This site has lots of game and such for it... http://www.qb45.org/
You can download the latest version here... http://www.qb64.net/

---
“I love you too.” - last words of Wanda Roy

Specter Phoenix
Member #1,425
July 2001
avatar

Neil Roy said:

It's Deluxe Pacman.

My mistake.

Quote:

WHen I first used C, the common teaching was to declare all your functions at the start, your prototypes. The idea was that main was always the first function you seen, and any others could easily access your functions once they have been prototyped first.

I understand having a file of code like this, in fact this is how I prefer to do it when learning a new library so everything is in one file so I can see the code just by scrolling instead of jumping through files:

#SelectExpand
1// includes 2 3void funct(); 4int funct2(); 5 6int main() 7{ 8 return 0; 9} 10 11void funct() 12{ 13 // code 14} 15 16int funct2() 17{ 18 // code 19 return num; 20}

The odd code I'm referring to is when I see C programmers do this:

#SelectExpand
1// includes 2void funct(); 3int funct2(); 4// global variables and constants 5void funct() 6{ 7} 8int funct2() 9{ 10 return num; 11} 12 13int main() 14{ 15 return 0; 16}

Neil Roy
Member #2,229
April 2002
avatar

Ah, I see what you mean now. Looks like the code from a new C programmer who thinks they need to declare prototypes no matter what.

Also, notice the comment at the top that says //includes.

---
“I love you too.” - last words of Wanda Roy

Specter Phoenix
Member #1,425
July 2001
avatar

Well that was my code to stress my example. A series of active examples of my point is Lazyfoo.net's SDL2 tutorials. I have the code from lessons 12 and 17 and both have that similar format.

bamccaig
Member #7,536
July 2006
avatar

Always declaring takes the thinking out of it though. Especially as code changes and restructures. If you pre-declare all functions you never run into that "compile -> error -> predeclare -> compile" cycle. Modern languages take this guessing out of the equation for you. It can be a nuisance to run into it in C or C++.

Neil Roy
Member #2,229
April 2002
avatar

I never had that problem. If I know a function will call one I am writing, I make certain it comes before it. I don't know, I just always done it that way and it's never been a problem. But I do understand the concept and would recommend new programmers do it properly.

---
“I love you too.” - last words of Wanda Roy

bamccaig
Member #7,536
July 2006
avatar

It makes me wonder why they can't just release a new C and C++ standard that makes forward declarations only necessary for backwards compatibility.

Chris Katko
Member #1,881
January 2002
avatar

Ever since I switched to D, I've basked in (and taken for granted) that there is no such thing as a forward declaration. Put any function, any class, anywhere you want. The order doesn't matter because compilers shouldn't be the dumbest thing in the toolchain.

Well, maybe forward declarations work for something else.

And the compile times. Good lord they're amazing once you hit a small-to-medium size project. No preprocessor pass. No includes. (Read: Modules. This thing they invented in what... the 80's?)

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

princeofspace
Member #11,874
April 2010
avatar

There's this school of thought (that I don't completely agree with) that putting all the functions into little table before they're defined it's like a table of contents in a book.

To be honest, I really like headers, though. It creates a well defined interface to a module. One of the things I don't like about, say, python, is that you can access everything once the module is imported.

Audric
Member #907
January 2001

There are a lots of things that I like in C in matters of mental discipline, but forcing the coder to sort his functions in the order "callee then caller" is really only for the compiler's benefit. An early processing of the C file would be enough for the compiler to determine which functions and variables the coder has ACTUALLY defined, and more accurately than guessing "Oh I don't know this function, it will probably return an integer".

Chris Katko
Member #1,881
January 2002
avatar

FYI D supports interfaces. (Especially because they intentionally DON'T support multiple inheritance.) So if you want more stuff, you use composition.

Every year I look around the D forums and I see some really smart people working toward cool stuff and making real progress.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

princeofspace
Member #11,874
April 2010
avatar

I suppose it's time for me to check out D. I did back in like 2012, and the general consensus was that while the language was solid, the toolchain was a bit weak.

But that was 5 internet centuries ago.

Neil Roy
Member #2,229
April 2002
avatar

While D sounds intriguing, sorry, I finally got fairly good at C. Not starting from scratch again. I'll be dead of old age by the time I get decent at D and then they'll come out with E. ;)

---
“I love you too.” - last words of Wanda Roy

Eric Johnson
Member #14,841
January 2013
avatar

Neil Roy said:

'll be dead of old age by the time I get decent at D and then they'll come out with E. ;)

You'd better get your will squared away, because E already exists. ;)

Chris Katko
Member #1,881
January 2002
avatar

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Neil Roy
Member #2,229
April 2002
avatar

Okay then, E++. LMAO

---
“I love you too.” - last words of Wanda Roy

princeofspace
Member #11,874
April 2010
avatar

Neil had a pretty good point: it makes a lot of sense to work in a language you're most comfortable in. When I get an idea for one of my games, I can implement it in c without learning some new language feature.

Neil Roy
Member #2,229
April 2002
avatar

Funny thing is, I realized recently that I am addicted to learning. It's nice, but I tend to spend a lot of time learning and not as much as I should actually implementing what I have learned. ;)

---
“I love you too.” - last words of Wanda Roy

Eric Johnson
Member #14,841
January 2013
avatar

I started off with C when I was 13 or 14. I quickly jumped ship to C++ though, because the book I was reading at the time told me C++ was newer and offered more features than C. I have stuck with C++ ever since.

A lot of people bash C++ for being bloated; for sporting features which result in hard-to-read code; and for tacking on classes and polymorphism, which can often lead to further difficulties in readability and scope of code.

Personally, I think that you can accomplish the same things in either language. C++ is only bad if you abuse it and write bad code. In other words, I do not think that C++ is inherently bad, but it does lend itself to sloppier code. So I would agree that C forces you to be a better programmer, but it is ultimately up to the programmer whether or not their code is difficult to follow.

I have not done much in C or C++ recently. Instead I have focused mostly on JavaScript. JavaScript is incredibly flexible and convenient. I absolutely love it. It is much more forgiving than C or C++, which certainly can lead to sloppy code, but again, a good programmer will structure their code to be well organized in any language. As far as making games goes, I think I will stick with JavaScript, because with it I can share games without having to make binaries, which is super convenient. JavaScript is not as quick as C or C++, and it has its limitations, but for my purposes, it gets the job done better than the alternatives.

bamccaig
Member #7,536
July 2006
avatar

C doesn't force you to be a better programmer... C just forces you to be more verbose with your code (which is arguably worse). Of course, C++ has a lot of tricky features that are difficult to do right. Code that compiles will not work or won't even run for reasons that are completely oblivious to beginners and even some intermediates. C++ is a very difficult language to master. That makes it a poor language. C has an edge there because there's less to remember, however, C offers so few features that you tend to have to be very careful to write proper code. C++ can automate some of that. There's no real winner between them.

C's major advantage over C++ is probably unmangled symbols. This makes it much easier to invoke C code from almost any other language in the world. Whereas interoperability between C++ and other languages is more difficult (if not impossible) to achieve.

C++'s major advantage over C is probably RAII.

Dynamic, high-level languages are typically way ahead of both in terms of functionality and tersity. However, they typically are slower to some degree (which often doesn't matter unless you're writing a game that pushes the limits of today's hardware).

 1   2   3 


Go to: