Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411429 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 19, 2024, 03:14:13 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Greetings! Newbie asking where to start with C++ game programming here!
Pages: [1] 2
Print
Author Topic: Greetings! Newbie asking where to start with C++ game programming here!  (Read 2701 times)
Baroque Moon
Level 0
*



View Profile WWW
« on: June 27, 2012, 03:48:40 AM »

After checking some topics, I must admit I'm feel like a kid in some sort of store as far as pondering where to start with C++ game programming. To maybe put it in some sort of perspective, I've done some C++ programming in school (very little game related as of yet), and I'm just wondering where I should start in order to get a certain sort of game started... lets say I want to make an insanely basic arcade style game and I want it to display in a nice little window, not some sort of DOS-shell screen. Can I get a pointer to a tutorial/chain of tutorials that will lead me to programming such a game in C++? Thank you for your patience and feedback!
Logged

Hey there.
Rusk
Level 1
*


View Profile
« Reply #1 on: June 27, 2012, 03:53:44 AM »

A game from scratch in C++ using SFML perhaps?
Logged
Sir Wolf
Level 0
***


A wolf cub growing up


View Profile
« Reply #2 on: June 27, 2012, 05:36:37 AM »

I myself started with LazyFoo's tutorials. I have a feeling the SFML one linked above by Rusk won't teach you as many bad habits you'll have to get rid of later.
Logged

"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn
Dacke
Level 10
*****



View Profile
« Reply #3 on: June 27, 2012, 05:46:52 AM »

Do you have a specific reason to use C++ rather than some other language? C++ is a very complex and powerful language and many people consider it extremely difficult to use efficiently. Most of the skills you have picked up using C/C++ can be easily transferred to a more accessible language, if you choose to make the transition.

Out of curiosity, have you used anything in C++ that isn't available in plain C? Have you done any object oriented programming (classes, objects, inheritance, polymorphism etc.)? I ask, because it is my experience that people say "C++" when they actually mean "C" (which is a much smaller and more elegant language)
« Last Edit: June 27, 2012, 06:14:11 AM by Dacke » Logged

programming • free software
animal liberation • veganism
anarcho-communism • intersectionality • feminism
rivon
Level 10
*****



View Profile
« Reply #4 on: June 27, 2012, 06:07:52 AM »

http://www.sdltutorials.com/tutorials
Logged
Fallsburg
Level 10
*****


Fear the CircleCat


View Profile
« Reply #5 on: June 27, 2012, 06:11:35 AM »

 I would urge avoiding C++ for your first game, unless you have a fair amount of C++ experience (by which I mean a couple of years).  You are going to be spending just as much, if not more, time wrestling with the language as you will be making a game.  

Now, if you are doing this to learn C++, then go for it.  But if you are doing it to make a game, I'd recommend going with a nicer language to start.  
Logged
Richard Kain
Level 10
*****



View Profile WWW
« Reply #6 on: June 27, 2012, 07:43:07 AM »

Now, if you are doing this to learn C++, then go for it.  But if you are doing it to make a game, I'd recommend going with a nicer language to start.

This is good advice. There are a lot of game-specific programming conventions and structures that aren't really language-specific. And C++ is not an easy language to start out with. If your objective is simply to produce a game, a different language would probably be a better choice.

C++ offers a lot more power than many other languages, mainly due to its fine control over memory management. With C++, you can meticulously design and manage where everything is stored, and reference any variable or structure at will. It is also one of the best languages for handling dynamically generated content at run-time. (via the free store) C++ is great for power users who want to get their hands dirty with the back-end of program management, and want to squeeze every ounce of performance that they can out of their software and hardware.

For more casual users who are more focused on general design and content, a memory-managed language like C#, Java, or even Actionscript 3 would be a better choice.
« Last Edit: June 27, 2012, 08:29:12 AM by Richard Kain » Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #7 on: June 27, 2012, 07:51:58 AM »

Quote
(which is a much smaller and more elegant language)

Althrought this is true, sometimes I doubt!
C's complexity is sometimes replaced by useful libraries from C++.

Here's how you read a file by a function in C (code taken from http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_02):

Code:
char* file_read(const char* filename)
{
  FILE* in = fopen(filename, "rb");
  if (in == NULL) return NULL;
 
  int res_size = BUFSIZ;
  char* res = (char*)malloc(res_size);
  int nb_read_total = 0;
 
  while (!feof(in) && !ferror(in)) {
    if (nb_read_total + BUFSIZ > res_size) {
      if (res_size > 10*1024*1024) break;
      res_size = res_size * 2;
      res = (char*)realloc(res, res_size);
    }
    char* p_res = res + nb_read_total;
    nb_read_total += fread(p_res, 1, BUFSIZ, in);
  }
 
  fclose(in);
  res = (char*)realloc(res, nb_read_total + 1);
  res[nb_read_total] = '\0';
  return res;
}

And here's how you do it with C++:

Code:
const char* file_read(const char* filename)
{
  std::string contents;
  std::ifstream file(filename,std::ios::in);
  if(!file.is_open())
  {
     std::cout << "Couldn't open file " << filename << ".\n";
     return "ERROR";
  }
  while(file.is_open())
  {
     std::string line;
     while(std::getline(file,line))
        contents += "\n" + line;
     file.close();
  }
  return contents.c_str();
}

Seems more elegant to me!


But yeah... If you pick C++ to make a simple game, go with a simple library.
Naming it: SFML, SDL or Allegro. I believe there are also many other nice and easy libraries but I don't want to risk and name them.

Cheers!
Logged

Dacke
Level 10
*****



View Profile
« Reply #8 on: June 27, 2012, 08:03:54 AM »

Fallsburg/Kain: Good advice.

Just note that "casual users" can include most indie developers.  With today's computers almost only AAA games need to push the limits of the hardware. For example, MineCraft is written in Java.  

Unless you have very optimized code, you are likely to get comparable speeds from Java and C++, anyway. Even with extremely optimized code you still get fairly comparable speeds.


kamac, absolutely, it was just an offhand remark Smiley
For example, Python contains much more complex language constructs than C. But Python code is generally both shorter and more readable.

Code: (Python)
def file_read(filename):
   file = open(filename, 'r')
   return file.read()

« Last Edit: June 27, 2012, 08:16:24 AM by Dacke » Logged

programming • free software
animal liberation • veganism
anarcho-communism • intersectionality • feminism
Klaim
Level 10
*****



View Profile WWW
« Reply #9 on: June 27, 2012, 08:19:08 AM »

ith today's computers almost only AAA games need to push the limits of the hardware. For example, MineCraft is written in Java.  

To be fair and clear: MineCraft is using Java for all the game logic and the rending is done by the graphic card. So as far as the game is mostly graphic consuming more than logic consuming, any language in game logic would do. However, if MineCraft wasn't in Java, I suspect it would take far less memory and allow more objects physics to be possible at the same time.

But then it might not be as fast success as it was.

Quote
Unless you have very optimized code, you are likely to get comparable speeds from Java and C++, anyway. Even with extremely optimized code you still get fairly comparable speeds.


Unfortunately, that is really not a good comparison. The problem being that little tests doesn't reflect bigger software complexity, so it is really almost impossible to make such a real comparison without building two times the same big application. Also, Java speed is not it's problem. The problem is predictability of it's performance. That is the same problem some people have with C# with games that have some strange hiccup on performance, that are almost impossible to fix because those languages are designed for the system to manage it, not you.

Also, Java, whatever the speed in tests, have to take more memory by design. This means that it is doom to be slower if data get's bigger, because memory size impact execution speed.

I think the answer to this question is a good read on the subject: http://programmers.stackexchange.com/questions/101649/a-modern-review-of-java

All that being said, I fully agree that C++ shouldn't be a language you choose just because you want to make games. My first games were in Basic and Visual Basic. I choose to learn C++ (mostly bad C with classes at the time) when I figured that the kind of game I wanted to make would require this kind of power anyway. I made a game in Python last year that I am sure I couldn't have made in two weeks of spare time like I did if I had used C++.

Anyway the question is more about what to do to learn than choosing the language.
For C++, I would say learn C++ first, as it is a big barrier to go on before tackling the complex matters of games... It should take you some rewarding years I guess.


EDIT> Wow I just checked some of the C++ code in the tests you linked, they are really not good code... some are using boost::pool that is not made at all for such fast benchmark!
« Last Edit: June 27, 2012, 08:29:20 AM by Klaim » Logged

Dacke
Level 10
*****



View Profile
« Reply #10 on: June 27, 2012, 08:31:34 AM »

My point was that C++ allows you to make awesome optimizations, but even with those optimizations it isn't that much faster.

From what I've understood, Java often wins out when program complexity increases for non-AAA titles. C++ memory management requiring much more effort to get right and optimized, while a pre-build garbage collector gives you something that is extremely good at what it does.

But yes, it's like comparing apples and oranges. Every language has strengths and weaknesses. But my general point was that non C++ languages (like Java) works both for big stuff (MineCraft) and for small stuff (super-optimized tasks). So you shouldn't shy away from using languages like that unless you have very specific needs for speed.
Logged

programming • free software
animal liberation • veganism
anarcho-communism • intersectionality • feminism
Richard Kain
Level 10
*****



View Profile WWW
« Reply #11 on: June 27, 2012, 08:37:22 AM »

Anyway the question is more about what to do to learn than choosing the language.
For C++, I would say learn C++ first, as it is a big barrier to go on before tackling the complex matters of games... It should take you some rewarding years I guess.

I would definitely agree with this point. Jumping right into C++ game programming is not a good idea. Far better to learn basic C++ syntax and coding structures before diving into game development with the language. C++ is NOT a scripting language. It doesn't run alongside an existing engine, providing a bit more power where needed. It is one of the most established languages currently in use in the game industry. When you can capably create and manipulate objects and object hierarchies, than you're probably ready to dip your toe into game development, but not before.
Logged
Baroque Moon
Level 0
*



View Profile WWW
« Reply #12 on: June 27, 2012, 02:39:48 PM »

Alright, thanks everyone for your input! I guess I honestly SHOULD just explore my options more before getting into the mindset of charging headfirst into C++, since that seems to be a major resonating theme with the responses.

Do you have a specific reason to use C++ rather than some other language? C++ is a very complex and powerful language and many people consider it extremely difficult to use efficiently. Most of the skills you have picked up using C/C++ can be easily transferred to a more accessible language, if you choose to make the transition.

Out of curiosity, have you used anything in C++ that isn't available in plain C? Have you done any object oriented programming (classes, objects, inheritance, polymorphism etc.)? I ask, because it is my experience that people say "C++" when they actually mean "C" (which is a much smaller and more elegant language)

My specific reason to use C++ is a bit of paranoia I suppose. I am under the impression that game devs need to start with C++ or otherwise they'll be seen as hacks. And lets see, as far as object oriented programming in C++ goes, that's essentially all I did with it. That's about all I can say really. Anyhow, I hope that answered your question!
Logged

Hey there.
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #13 on: June 27, 2012, 02:48:20 PM »

I am under the impression that game devs need to start with C++ or otherwise they'll be seen as hacks.

Definitely not true these days! Maybe there are people who feel like this, but they're elitists and you don't need to listen to their opinions.
Logged

Baroque Moon
Level 0
*



View Profile WWW
« Reply #14 on: June 27, 2012, 02:50:04 PM »

I am under the impression that game devs need to start with C++ or otherwise they'll be seen as hacks.

Definitely not true these days! Maybe there are people who feel like this, but they're elitists and you don't need to listen to their opinions.

OK, cool. Also, I can't believe I neglected to mention this, but I have some experience programming with XNA. I don't know how XNA is viewed by the indy-dev community, so I didn't bring it up.
Logged

Hey there.
randomnine
Level 1
*


View Profile WWW
« Reply #15 on: June 27, 2012, 03:33:43 PM »

Definitely not true these days! Maybe there are people who feel like this, but they're elitists and you don't need to listen to their opinions.

If you're interested in programming work for a major console game or PC game studio, it still makes a lot of sense to focus on working in C++.
Logged

Richard Kain
Level 10
*****



View Profile WWW
« Reply #16 on: June 27, 2012, 04:25:26 PM »

My specific reason to use C++ is a bit of paranoia I suppose. I am under the impression that game devs need to start with C++ or otherwise they'll be seen as hacks.

Understandable. Back in the day, there was definitely an "attitude" amongst C++ coders. They would look down their noses at the less capable "script-kiddies" and laugh whenever we came crying for their help. These days the need for low-level C++ coding is not nearly as dire. A lot of designers are going with systems like Unity, where scripting handles most of the game interactions. Java, Python, C#, Actionscript, all of these have become capable game development languages in their own right, often with cross-platform solutions.

Quote from: Baroque Moon
And lets see, as far as object oriented programming in C++ goes, that's essentially all I did with it. That's about all I can say really.

If you already know enough C++ syntax to get by, I would point you to SFML. This is a very capable and cross-platform C++ library that does a great job of focusing on object-oriented conventions. It will help you start in on C++ game development, while maintaining regular OOP structures.
Logged
_Tommo_
Level 8
***


frn frn frn


View Profile WWW
« Reply #17 on: June 27, 2012, 05:09:08 PM »

Unless you have very optimized code, you are likely to get comparable speeds from Java and C++, anyway. Even with extremely optimized code you still get fairly comparable speeds.

Actually of those tests, one is faster in Java; 5 are comparable and 5 are more than 2x slower. 4x slower means that something that takes one hour becomes 4 hour, and that in a 16 ms frame you can stuff 1/4 of the computations.

Also those are very small tests; they are easily optimizable by the JIT, and do not use the Java standard library. The Java library is provably slow as hell and causes lots of GC overwork (who the hell tought LinkedList was a good idea  Epileptic)...
so imo any bigger program (maybe one making a liberal use of the Java Library), will probably be much slower in comparison.

Memory consumption too goes from 2x to 51x (wtf), and from what I know the situation definitely doesn't get better with bigger programs.

So, if "comparable speed" for you is 2-10x slower with 10-20x the memory footprint, go ahead and use Java. If not, C++ is still the most efficient tool out there.
« Last Edit: June 27, 2012, 05:22:20 PM by _Tommo_ » Logged

Dacke
Level 10
*****



View Profile
« Reply #18 on: June 27, 2012, 05:46:50 PM »

I think that the link that Klaim supplied provided a reasonable approximation: C++ is probably around twice as fast as Java, with Java also running the risk of having temporary slowdowns. And sure, Java can't even theoretically be as as fast as C++, due to language limitations.

But I still call it "comparable speeds", because we have such extreme monster computers these days. Processors become approximately 2X as powerful every 2 years (according to Moore's law (although more cores isn't all that helpful for indie games)). But the the difference between C++ and Java remains fairly constant. So while Java certainly won't cut it for AAA titles, it should be enough for most indies who create games that are more like 10-30-year-old AAA games.

I'm not sure why Java gets such an extreme memory footprint in some of the tests*, but I assume it is due to extreme optimization for speed over memory. I think it's very unlikely that you'd run into something like that when using algorithms found in games.

It should also be noted that C is significantly faster than C++, if used properly. But it's harder to write complex things in C, so it's not worth it. Hand-written assembler also has the theoretical potential to be much faster than C/C++, but it's even more unusable. You trade raw speed for development speed when using more high-level languages. It's a difficult trade-off and there is no objectively "right choice". It all depends on what you want to do.

edit: the "it's only small tasks" argument is something I'd expect to hear from a Java-defender, as the JVM start-up times affect small problems more

edit 2: * I now think that the extreme memory difference is caused by how small the tests are. C++ has much less startup overhead and can use structs instead of objects. While Java will have more overhead in most cases, the proportional difference will be much less noticeable for larger projects. I think we're talking 2X the memory for a bigger project at worst, which again won't be a big problem as textures will take the same amount of memory.

edit 3: *The extreme memory difference seems to be due to the garbage collector not kicking in unnecessarily. The Java memory footprint in the n-body test is actually smaller than C++'s for 500k bodies. After that it grows to a larger amount, but it flats out at 14KB memory and actually drops after that. My guess is that the garbage collector doesn't bother to clean up a lousy 14 KBs of memory in this case.

edit 4: I ran the n-body test on my computer. I get a constant 479KiB memory usage. So while the site says that Java uses 43x the memory, my tests show only 1.5x the memory compared to C++. So the huge number (43x) on the site is not indicative of true memory usage but rather the nature of how garbage collectors work. Which is a problem for games that work on the limit of available memory, but isn't a problem when you've got plenty to spare.

Memory for n-body:
BodiesC++    Java             Java on my machine
500,000316KB216KB479KB
5,000,000320KB13,672KB479KB
50,000,000  320KB   13,664KB   479KB
« Last Edit: June 27, 2012, 07:13:35 PM by Dacke » Logged

programming • free software
animal liberation • veganism
anarcho-communism • intersectionality • feminism
Klaim
Level 10
*****



View Profile WWW
« Reply #19 on: June 27, 2012, 09:23:28 PM »

Dacke, I globally agree with you, I just wanted to precise some points. Smiley


From what I've understood, Java often wins out when program complexity increases for non-AAA titles. C++ memory management requiring much more effort to get right and optimized, while a pre-build garbage collector gives you something that is extremely good at what it does.


What we call "modern C++" is far easier to manage than Java. So yes you are right that most C++ devs don't know how to manage memory easily. In fact, you shouldn't manage memory most of the time. Here is an answer I gave about this very subject : http://programmers.stackexchange.com/questions/112098/in-c-how-much-programmer-time-is-spent-doing-memory-management

So, I would say that it is not correct. However, Java might win on long-term process application like web applications because the VM can take the time to balance and optimize the execution graph(s?). However, there is still problems (as describe in the link from my last post) related to memory layout (that you don't have to manage in C++, I'm talking about the layout defined by the language itself) that makes theorically impossible to beat a correct C++ program. On the other hand, understanding how to write correct C++ programs is the part that takes a long time. And Compilation time too.

All in all, it is always the same questions: "is this tool good enough?" and "can I affort the cost of using this tool?"


Quote from: Baroque Moon
My specific reason to use C++ is a bit of paranoia I suppose. I am under the impression that game devs need to start with C++ or otherwise they'll be seen as hacks. And lets see, as far as object oriented programming in C++ goes, that's essentially all I did with it. That's about all I can say really. Anyhow, I hope that answered your question!

It is necessary only if you want to work in the "classic" industry. Tons of games now don't require C++, still if you work on console games without managed frameworks (like XNA) you need to work with C++.


Quote from: Dacke
But I still call it "comparable speeds", because we have such extreme monster computers these days. Processors become approximately 2X as powerful every 2 years (according to Moore's law (although more cores isn't all that helpful for indie games)). But the the difference between C++ and Java remains fairly constant. So while Java certainly won't cut it for AAA titles, it should be enough for most indies who create games that are more like 10-30-year-old AAA games.

I fully agree. I know C++ better than Java so it wouldn't be a good choice for me, I know Python better for example. The knowledge at starting point of the project is important too.

Quote
I'm not sure why Java gets such an extreme memory footprint in some of the tests*, but I assume it is due to extreme optimization for speed over memory. I think it's very unlikely that you'd run into something like that when using algorithms found in games.

The most important reason is that the design imply garbage collection AND reflection, that imply that each type have a fairly big amount of information ready to be used at runtime. Also, garbage collection require memory manipulations that cannot be predictable so it have to work in a big allocated memory space to do it's job.
Also, some more infos: http://programmers.stackexchange.com/questions/113177/why-do-languages-such-as-c-and-c-not-have-garbage-collection-while-java-does
And see the disavantage part here: http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)

Quote
It should also be noted that C is significantly faster than C++, if used properly. But it's harder to write complex things in C, so it's not worth it. Hand-written assembler also has the theoretical potential to be much faster than C/C++, but it's even more unusable. You trade raw speed for development speed when using more high-level languages. It's a difficult trade-off and there is no objectively "right choice". It all depends on what you want to do.

Actually, that is not completely correct: depending on how you write you C++, you can beat C easily, simply by profitting of optimization that are not possible in C. For example, std::sort is faster than the same sort in C (I'm not even talking about qsort). The main reason being that template code allow the compiler to generate code inline, while in C you still have the indirection of pointers happening on runtime, so it have a cost, in particular in heavy process like "big" games.
C++ is "at least as fast" as C, util you use an abstraction that have a cost that is hard to compare with C directly (object orientation being possible but mostly more expensive to do in C, template having 0 overhead, etc).


I hope someone in the future implement a techically complex game in the future, that would have a high CPU load, and then write it in several languages, just for the fun of it.
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic