Is making game with c possible?

Started by
16 comments, last by Ravyne 7 years, 11 months ago

why we always use c++? what is the advantanges of c++ over c in game development? and does making game with c makes sense at all?

Advertisement

Programming is the game of creating enough structure in a problem such that you can explain a solution for it to a computer.

Humans are very different from computers. We think in objects and relations, we think in higher order abstract notions. We know what "spoon" means. You can find one even if it has a different shape than you have seen before. You understand what to do when you are given the task "prepare the table for dinner".

Computers on the other hand are very good at numbers, but lack all these higher level notions. They are very fast at computing them, but that's about it.

The structuring problem called "programming" is thus about closing the gap between our ideas about the solution in our mind, and the level of understanding of a computer.

In general this is a difficult task for us. We have to convert our objects and relations to numbers, and we're bad in numbers.

If you compare C with C++, you'll notice that C++ has some concepts that are closer to us. It has class, and objects, and abstract class. While these concepts are not a exact match to our notion that we use in thinking about the world, they do help us in explaining. In a sense, C++ lifts the computer level of understanding a bit closer to us.

This reduces the gap, and speeds up explaining the solution that we have found to the computer.

In C, you don't have class as native concept. It is however not difficult to add such a notion, but it is more work. Basically, you would be doing the conversion to something close to C++ level, and then do a second translation from your C++-ish solution to real C.

Making a game in C only has sense imho if learning about C is your goal. In all other cases, you only bring more work on the table, without any benefit.

And for a different take...

You can write anything in C and many game engines have been created in C in the past. C++ is just more popular now but some still prefer C.

Quake was C not C++ and then in system code I believe Linus still has a blanket ban on C++ in the Linux kernel as he prefers the simplicity that C and procedural code brings

The structure of your code will be different but that is not better or worse, just different. Not everything has to be objects, data is data and functions that operate on data are just functions. When you cut to the chase, object methods are just syntactical sugar that hide the passing of the this pointer into the function along with some fancy slight of hand that brings vtables into play for virtual routing. C++ also brings generics and lambda which I believe have not made it back to C at all..

Even in C++ you can write C style code as C++ is a superset on C so that it is backwards compatible with C

All depends on the style of code you like.

If you like OO pick an OO language, if you like procedural pick a procedural language and if you like functional pick a functional language. All will let you reach your goal and perform well as long as the design is good :)

TL;DR; of the abvove posts:

The advantage of C++ is more options to structure your code in readable ways, while not really having any disadvantages over C.

So the question is more "why use C?", which it seems like more and more people answer "there is no reason", so they use C++.

or have you cake and eat it too.

i use a c++ compiler for the stronger compile time checking, but the code is largely C. if i want to use C++ syntax i can. if i don't, i don't have to. so if i need polymorphism, inheritance, lambdas, etc, they are available. but 99% of the time, plain old C without the sugar coated wrapper of C++ syntax works just fine, and is usually less typing.

in general when you ask this type of question you'll get two types of responses, those from folks who only know c++ (younger programmers), and those who know there were games before C++ existed (older programmers).

i wrote my first PC game in 1982. so i'm definitely in the "games came before c++" group. 99% of the original purpose of c++ syntax was to make extending existing code bases easier. unfortunately, extending exiting code bases is something that's rarely required in games. but of course, if you've got a new hammer, you've just got to frickin' use it. so wait! we can also use it as a design methodoloy! but again, unfortunately non-trivial games are about performance, not academic design methodologies / methods of source code organization. those raised on C++ alone are usually unaware of the original purpose behind c++ syntax, and the history of the rise of OO design methodology.

this also speaks to the continual trend of hardware and software manufacturers coming out with new stuff (other than graphics cards), but not directly targeting games. and then feeling left out, gamedevs try to find a way to use the new tech. or non-gamedevs espouse the tech as a game solution (anyone remember MMX instructions?). in many such cases the fit between games and tech is forced or kludged or only works up to a certain point.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

I use C quite a bit, personally. It doesn't really provide much benefit over C++. I like it because not having classes handy can push me to think about different ways to design code. I don't think I've figured out anything novel yet, though. There's only two reasons why I would recommend anyone else use C over C++:

1) You don't want to, or can't, link to the normal libraries.
2) You're considering working with a C purist.

The second reason might come up in game development. There are some C purists in the hobbyist crowd. Usually their reasons for being C purists are pretty pointless, but in my experience they're usually crazy good and worth working with just to learn a thing or two.

The first reason is not likely to come up in game development *anymore*. Even cheap low end computers have enough hard drive space and memory to make saving 1-2mb fairly pointless (this was not the case 15 years ago).

The first reason *might* come up if you're doing other sorts of programming. For example, the C runtime isn't available in an OS kernel, or on a custom OS. The C runtime might not be available in certain embedded environments. If you're writing web services using CGI (common gateway interface), cutting out some of the pre-main work might be worth it.

Like Norman, a lot of my code winds up looking like C code even when I'm programming in C++. The "C way" of doing things is usually (but not always) more efficient, but you can always do the "C way" in C++, and still get all the advantages of C++.

To play Devil's advocate, OOP isn't loved by everyone.

A lot of people think it is terrible. They will say don't use C++ because it has objects. But, as pointed out above, you can write C++ and not abuse OOP.

You need tons of games to find your style and figure out if it saves you time and energy and money, or it doesn't.

Don't worry about it. Go write games.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I work at a game developer that uses C for 99% of its ~10MLOC, only writing C++ when interfacing with C++ libraries.

Even as someone who loathes C, it works perfectly fine. Why we use C is probably historical, but probably the only reason IMO to justify using it on a new project/codebase is that "you're less likely to mess up using all those shiny C++ features". I don't think that's a terribly good argument, but it's an argument a lot of people make.

As far as I'm concerned, C++ just means you get to write C with

  • Function/Operator Overloading
  • Templates
  • Lambdas
  • The ability to declare variables after the beginning of the block*
  • Objects
  • Namespaces

* Mostly a function of writing C in Visual Studio, which uses an ancient version of C. However, I included it because VS has a significant marketshare in the games industry and being stuck using it is a probable reality when writing C.

A great book to read, old now but what it says is still valid and will teach you a huge amount about what and why in C++ "The design and evolution of C++" by the creator of the language. It goes into the choices that were made, why they were made and give you insights into the core language that will deepen your understanding of the language. The deeper your understanding the better you can use a language and back in the day this gave me insight that no other books gave.

Don't worry about it. Go write games.

QFE. C vs C++ is a bloodied and savage battlefield populated by extremely powerful idiots. Thar be dragons.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement