where to start

where to start

Postby mixpro » 29 Aug 2011, 01:09

hi, this might take some time ...

the goal is a game developer , the problem is where to start .

here's the story :
i've been looking around on how to get start with developing games specifically for mobiles , i started reading opengl and animating + modeling with blender , but the problem is i don't know how to connect everything together , i mean :

1) animations with blender how to insert them with opengl in the code ? like characters running around .....

2) plus how the ai for the game works ? for instance if i'm developing a strategy games how can i build for example my own base and at the same time the computer is building his own , is it using threads ?

3)what exactly do i need to develop my own game engine , and what exactly is a game engine cause blender has one and i don't know how to use it in my code for writing the game .

please i'm a beginner , so if you could explain to me how everything works and if you could suggest books video tutorials ....

thnx a lot .

by the way i know c,c++,object oriented programing,some java + socket prog... ,
mixpro
 
Posts: 2
Joined: 29 Aug 2011, 01:07

Re: where to start

Postby charlie » 29 Aug 2011, 03:14

If you're happy to start with desktop game development and you know C/C++ then I'd suggest investigating Irrlicht or Ogre. These are good graphics engines that do the 'connecting' for you and reduce the amount of detail you have to dig in to with OpenGL. You'll be able to export models out of Blender in a format that the engines will like, and then just load them and indicate what animations to perform and the engines will do the leg work for you.

I'm not sure of a suitable mobile engine in C/C++.

If you are wishing to use Java (which may be more suitable for mobile dev) then you can look at Ardor3D which has Android support.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby farcodev » 29 Aug 2011, 04:06

2/ For the AI you could use finite-state machine, which is basically a structure of if..then and used in many games.
regarding using threads in a mobile OS, i don't know, maybe someone else will answer you better.
farcodev
 
Posts: 163
Joined: 09 Feb 2011, 02:52

Re: where to start

Postby sireus » 29 Aug 2011, 11:40

All I can tell you is this: don't try to write your own 3D engine. Of course it's fun and you'll learn a lot, but it might easily take you several years to get anywhere close to what you want to achieve.
Better try with one of the solutions Charlie suggested.
sireus
 
Posts: 109
Joined: 24 May 2011, 20:10

Re: where to start

Postby oln » 29 Aug 2011, 11:55

You could also consider trying to contribute to one or more FOSS games, and see how they are built. There are several which have forums here for instance.
I feel have learned a lot from working on a FOSS game project.
User avatar
oln
 
Posts: 1020
Joined: 26 Oct 2010, 22:16
Location: Norway

Re: where to start

Postby Knitter » 29 Aug 2011, 12:27

I recommend that you follow oln's suggestion. Join an existing project, there are several that should be interesting and will allow you to learn those things you are asking about since you'll have some help from the existing developers.

Tackling some of you questions: AI a very complex field, you can have a game where the computer looks a bit smart and you AI code is nothing more than a bunch of if/else lines but if your game starts to get complex so will your AI code. You should, at least, be familiar with the most used algorithms for decisions and path finding. There are several books and resources in those subjects.

Threading is important but is not a requirement. It will depend on the game and the libraries you are using, for example Slick2D (a 2D game library in Java) uses only one thread. Even if you think that two actions are done at the same time, like you example of building two bases, they can just be done one after the other and the user won't notice.

To build you own game engine you will need time, a lot of time. As a rough estimate, in a year's work you could be able to get the basis of a game engine. This was the time needed for a "one man" project to reach a usable state, and the developer already knew game development and 3D development.

Seeing you're a beginner, you will have to get a handle on many of the theoretical aspects of game development, maybe start with small 2D projects and develop small testing games. It will help you get a grasp on the usual problems and solutions and help ease the introduction of many of the concepts involved.

For mobile and Android development, I would recommend LibGdx instead of Ardor since this is a library specifically developed for Android platforms and one of the most, if not the most, optimized library with a great performance. Mobile games will through you back to counting frames and squeezing all the resources you can since a phone/smartphone has several limitations.

But really, join an existing project. You will be able to develop a game, help a project in need of more hands on the code and learn from those that are already working in game development.
Knitter
 
Posts: 237
Joined: 03 Jul 2011, 22:52
Location: Portugal

Re: where to start

Postby charlie » 29 Aug 2011, 20:13

Regarding strategy games, projects to look at:

www.megaglest.org
wildfiregames.com/0ad
www.boswars.org
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby qubodup » 29 Aug 2011, 22:53

i don't know how to connect everything together

how to insert them with opengl in the code

what exactly is a game engine

I think someone who knows what they're talking about should tell mixpro about
1) Game loop
2) Including libraries and importing files
3) Abstraction between input, output and game logic(?)
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: where to start

Postby mixpro » 29 Aug 2011, 23:28

Thank you guys i love the idea of FOSS , I'll try it definitly .
mixpro
 
Posts: 2
Joined: 29 Aug 2011, 01:07

Re: where to start

Postby MyEmail » 31 Aug 2011, 08:28

1. Learn C inside and out, repeat the process for C++, and repeat for every other language you encounter. Each time it gets easier and easier. The more languages you know the more marketable you are.
2. Join a small FOSS project and contribute what and where you can. More importantly pay attention to what and how the more experienced developers do throughout development.
3. Stay away from school--its a crutch only used by those who need it. Seriously, I am not joking. While it is possible to get good schooling in computers most people don't, and if you are smart enough you can teach yourself 10x faster than any school could. This allows you to develop your own view and style of programming unbiased by good and/or (more likely) bad habits pounded into your brain by school.
4. Get yourself a mentor. Someone you know and admire who you can ask the occasional question. If you have none, fallback to using forums (such as FreeGamer) to ask your questions.
5. Get a wide array of books. Applied Cryptography is a great one that will push both your mathematical and coding limits. Various books on Game Programming, in particular those covering OpenGL, artificial intillegence, vector math, number theory, etc. Business and general working-environment books are great too.
6. The #1 skill you need is problem solving, after that all you need is experience. If two programmers where competing for the same job (one with a Bachelors in computer science and the other with 4 years of on-the-job experience and having led a team of developers in creating a video game from start to finish, and knowing obscure programming languages they don't cover in school), most employers favor the experienced one over the educated one. HOWEVER, the key is to have a extremely impressive portfolio & resume to show just how experienced you are (I would even recommend creating a website all about you and your accomplishments).
7. Do freelance work until you can get a solid job, and keep track of everything you do by adding it to your portfolio. Things such as creating web interfaces for a local restaurant (e-z with perl/php), etc. Once you get a solid job stick with it (even if it sucks) for 6+ months if not a year, or longer. This shows future employers dedication--that you will start their project and work on it to the finish, and not quit halfway in between.
8. Know common programming operations by heart. In a interview someone may ask you something like: "In C, how would I swap the first 4bits and last 4bits of a unsigned char variable?". While this is easy for experienced programmers, many programmers crumble in this part of the interview. If you can do all this stuff on a whim in your head, you will destroy the competition.
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: where to start

Postby charlie » 31 Aug 2011, 09:30

MyEmail {l Wrote}:1. Learn C inside and out, repeat the process for C++, and repeat for every other language you encounter. Each time it gets easier and easier. The more languages you know the more marketable you are.

I disagree with this. I think at this point there is diminishing returns to being a good C/C++ programmer.

To get to a level where you are capable of creating a decent game with C/C++ will take several years. To get to the level where you will be of use to the commercial game industry will take a decade.

You're better off with Java or C#/.NET or HTML/JS or Python or Lua. Something higher level where you get up and running a lot more quickly and are worried about implementing your game logic and not surviving the madness of pointers, address allocation, and the many other complexities that come with the C/C++ territory.

Take inspiration from indie games. The likes of Minecraft and Revenge of the Titans are not done in C/C++ because it would take too long.

As computing power continues to increase, the need for runtime efficiency (the primary benefit of C/C++) declines. At some point it will become irrelevant - and in many ways it already is. Only the bleeding edge high-end graphical games - those at the forefront of the industry - require the power of C/C++ and even that has been reduced to mostly engine routines with the advent, and lately ubiquity, of scripting in games.

Try out various languages that have good libraries and tutorials and find one that makes sense to you. Python and PyGame (2D), Java and Slick (2D) or Ardor3D, C# and ?, Lua and LOVE, HTML5/JS and ?. Look around.

MyEmail {l Wrote}:In a interview someone may ask you something like: "In C, how would I swap the first 4bits and last 4bits of a unsigned char variable?"

That sums up C/C++ nicely for me. Pointless.

The rest of MyEmail's advice is good though I disagree with learning C/C++ inside-out. That's my opinion, and I'm not a game industry insider. Simply an observer.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby Zlodo » 31 Aug 2011, 09:57

The first step to learn C++ is not to write things such as "C/C++". C++ is a different language than C that happens to seamlessly integrate C as a subset and implying that you need to fight with pointers and memory allocation in C++ like you do in C is picking up C++ the wrong way.

I started to become effective at C++ and to actually like the language the day I decided to try to use C++ the way its creators intended (because, believe it or not, they weren't trying to make a complicated and unhelpful language) instead of as a souped up version of C. It turns out that everything became much easier and enjoyable when I did that.

I rarely ever use pointer arithmetic or even plain C pointers. Rarely, and mostly for things like storing data in opengl vertex buffers. If you have a hard time with C++ because of pointers and memory allocations, it means that what you're actually doing is C and yeah, C does definitely suck.

If you want to have fun with C++, pick up a modern compiler that implements most of C++11 (like gcc >= 4.6). Avoid using the C library (printf and co), and try not to use pointers anywhere. Use either shared_ptr, references or directly include objects by value on the stack or as object members. If you do that the only thing you ever have to be careful about is avoiding cyclic references between objects, and you'll never have memory leaks or overwriting random locations in memory because of dangling pointers.
Zlodo
 
Posts: 17
Joined: 14 Dec 2009, 12:36

Re: where to start

Postby mdwh » 31 Aug 2011, 12:08

charlie {l Wrote}:You're better off with Java or C#/.NET or HTML/JS or Python or Lua. Something higher level where you get up and running a lot more quickly and are worried about implementing your game logic and not surviving the madness of pointers, address allocation, and the many other complexities that come with the C/C++ territory.

Take inspiration from indie games. The likes of Minecraft and Revenge of the Titans are not done in C/C++ because it would take too long.

As computing power continues to increase, the need for runtime efficiency (the primary benefit of C/C++) declines. At some point it will become irrelevant - and in many ways it already is. Only the bleeding edge high-end graphical games - those at the forefront of the industry - require the power of C/C++ and even that has been reduced to mostly engine routines with the advent, and lately ubiquity, of scripting in games.

Whilst C++ isn't a good beginner language, I disagree it takes longer to write something in it once you know the language. I know Java and C++, but I don't see that Java would get me anywhere quicker.

C and C++ have the advantage of being widely cross-platform (C# isn't at all of course; Java was supposed to be, but is now fragmented into three different kinds of Java when you include mobiles). It also seems less straightforward to me to use established libraries/engines with Java? C++ will be the most supported, usually because that's what they're most commonly written in.

It also depends on what you already know. Whilst I've recently started using Lua, I wouldn't know how to build a large project in it, where as I can do that with C++.

Scripting languages are common yes, but that's a case of using C++ and something like Lua, not exchanging one for the other.

MyEmail {l Wrote}:In a interview someone may ask you something like: "In C, how would I swap the first 4bits and last 4bits of a unsigned char variable?"

That sums up C/C++ nicely for me. Pointless.

I don't follow your logic. Just because it's possible to do something that you think is pointless, doesn't make the language pointless. This is a criticism of the interview question, not the language.

Surely you can do bitwise operations in Java too? Is Java therefore pointless?
mdwh
 
Posts: 67
Joined: 13 Aug 2011, 01:53

Re: where to start

Postby charlie » 31 Aug 2011, 12:57

Good post mdbh!

I would dispute your cross platform point; very few FOSS projects are simple to compile cross-platform, implementing a cross-platform C++ build is an art form. You can use Eclipse on any desktop OS, whereas with C++ you need a different IDE for each OS - and Eclipse is arguably the #1 IDE.

C++ is an experts language. When you are an expert, you can do all sorts with it. Getting to that point will take you some time, which is what I was getting at.

mdwh {l Wrote}:
charlie {l Wrote}:
MyEmail {l Wrote}:In a interview someone may ask you something like: "In C, how would I swap the first 4bits and last 4bits of a unsigned char variable?"

That sums up C/C++ nicely for me. Pointless.

I don't follow your logic. Just because it's possible to do something that you think is pointless, doesn't make the language pointless. This is a criticism of the interview question, not the language.

Surely you can do bitwise operations in Java too? Is Java therefore pointless?

Right you are. I retract that cynical and incorrect remark.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby qubodup » 31 Aug 2011, 13:22

mdwh {l Wrote}:
MyEmail {l Wrote}:In a interview someone may ask you something like: "In C, how would I swap the first 4bits and last 4bits of a unsigned char variable?"

That sums up C/C++ nicely for me. Pointless.

I don't follow your logic. Just because it's possible to do something that you think is pointless, doesn't make the language pointless. This is a criticism of the interview question, not the language.

Surely you can do bitwise operations in Java too? Is Java therefore pointless?

I thought that was a joke about pointers.
User avatar
qubodup
Global Moderator
 
Posts: 1671
Joined: 08 Nov 2009, 22:52
Location: Berlin, Germany

Re: where to start

Postby farcodev » 31 Aug 2011, 15:22

use Object Pascal and that's it :D oh well it's not mainstream, shame on me :'(

Anyway whatever the language, the most important is what we do with it. Try one and if you like it stick w/ it, it's no more simpler than that, the end user doesn't care if it's made in c++/python or even objective-c (puke mode :P ).

On the other side i didn't follow much the thread when it's question of interview o.O Do we talk about FOSS or job ?
Because if it's for job, Java/C#/C++ are the mainstream industry language, for now.

Languages wars are as pointless as the hardware ones in my old 16/32 bits time (Amiga vs Atari for ex.)
farcodev
 
Posts: 163
Joined: 09 Feb 2011, 02:52

Re: where to start

Postby MyEmail » 31 Aug 2011, 18:35

Charlie: My following statement is not a debate and please do not interpret it as such.

From my experience learning alternative programming languages is vastly beneficial. Each language is different and requires different skills from the programmer. Skills you would not acquire from C you could get from perl, which then backwards-helps your C ability. This all compounds exponentially the more languages you learn and master. I dunno how many times my fellow developers have done dumb things like spent hours doing redundant C-code tasks, but it takes only a few moments to write a perl script to do it for me and I am done with the same that takes hours for them--all because they don't know perl and I do.

From my personal experience, I am a far more experienced and productive programmer than my coworkers, which is especially odd because I don't have the education and am considerably younger than most of them. I attribute this to the fact that they are stuck on conventional knowledge taught in schools (C++, DirectX, and basic vector math for the most part), whilst I am miles ahead with ANSI-C, C++, Perl, PHP, Scheme (Scheme is amazing!), Java, Lua, Bash, (soon to be python too), have vast experience with cross-platform development, have vast experience with Linux and its host of tools, have vast experience using various open-source libraries (OpenSSL, SFML, libCURL, etc etc), and the countless other advantages they don't cover in school.

Not to attribute anything to myself, but perhaps learning so many languages is a challenge for other people. I don't know. What I do know is that I mastered 8 languages in a little over 3 years. I personally think anyone can do it, and the impressive results are merely the vast benefits of learning multiple languages.

Also, while developing you should be creating your own code-base from which you can import and base projects off of. The dev time of projects is considerably shortened if you can import a code-base that takes care of most of the basic-work. Languages other than C++ shine in this regard, simply because they already have a gargantuan codebase. But if you develop your own you can produce better products in C++ with relatively the same development time.
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: where to start

Postby Zlodo » 31 Aug 2011, 19:36

MyEmail {l Wrote}:What I do know is that I mastered 8 languages in a little over 3 years.

Beware of the dunning kruger effect, it can impede your ability to learn quite a bit.
http://youarenotsosmart.com/2010/05/11/ ... er-effect/
Zlodo
 
Posts: 17
Joined: 14 Dec 2009, 12:36

Re: where to start

Postby charlie » 31 Aug 2011, 19:51

MyEmail {l Wrote}:Not to attribute anything to myself, but perhaps learning so many languages is a challenge for other people.

You are obviously a talented prorgammer. Not everybody finds it so easy. Some people take many years to master just 1 language. Some just aren't motivated enough or do not find it compelling enough. Most programmers will never learn 8 languages. Call it laziness, lack of ability, whatever, that's the reality.

We're both/all putting our personal experience forward as evidence for what X is good and Y is bad - me included. There are very few people in the world who are able to be truly objective about anything they are particularly knowledgeable about.

So I think the synthesis of the answers to the original question is to ask another question: how easy do you find programming? At what level are you capable of beginning and how soon do you start to find the ceiling in terms of difficulty?

Highly capable people could/should learn C then C++ then progress. Less highly capable, or those who want to specialize in only 1 language, go higher level (Python, Java, HTML5/JS) depending on your target.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby charlie » 31 Aug 2011, 19:56

I'll just add that another reason I was strongly advocating a higher level approach is the 3 requirements stated by the OP. 1) Animations in Blender 2) AI programming and 3) Game Engine requirements. If you go in at the C or C++ level, you're going to be a while in #3 before you get close to #1 or #2.

Anyway, here's some SDL tutorials (originating thread). It could be a good starting point:
http://lazyfoo.net/SDL_tutorials/index.php
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Re: where to start

Postby farcodev » 31 Aug 2011, 20:27

It depends also of what you do, especially when you aren't a pro the goals are differents, even if years after years you learn new ones.

Since the start i know basic (yeah 27 years ago)/assembler (z80 and x86)/ANSI C/C++/Object Pascal/C# and i currently use only one of them now. I dunno if i'm lack of super power but i don't spend 3 month for debugging. Learn the logic and be yourself structured is more important and harder than learn 8 languages IMHO, semantic and logic are two different parts.
farcodev
 
Posts: 163
Joined: 09 Feb 2011, 02:52

Re: where to start

Postby MyEmail » 01 Sep 2011, 01:05

Zlodo {l Wrote}:
MyEmail {l Wrote}:What I do know is that I mastered 8 languages in a little over 3 years.

Beware of the dunning kruger effect, it can impede your ability to learn quite a bit.
http://youarenotsosmart.com/2010/05/11/ ... er-effect/

I was speaking from experience, so in essence I have "gone to the chess tournament and won 1st place".

farcodev {l Wrote}:Learn the logic and be yourself structured is more important and harder than learn 8 languages IMHO

That's where you missed the point. Learning more languages gives you all that and more. I still wish C++ macro's weren't so gosh darn limited, and had more Scheme-like functionality. Oh snap, I can implement a pre-compile perl hook to add that functionality for me! Now I have the power and simplicity of C with the absurd power of Scheme-like macros. :O Its dumb things like that that people who don't know multiple languages can never do. They are stuck with limited functionality in the C-macros while I dance around them with a vast set of macro tools.

Most people probably don't even know they can do things like that--their stuck with the conventional knowledge provided by schooling and/or a single programming language. They don't go into the vast depths and potential programming really has and teach them the basics they need to skirt by in the industry.

Now that is just a really simple example of the vast benefits of knowing multiple programming languages. I mention this not to put myself above others or to brag, but to prove the point that knowing many languages is vastly beneficial.
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: where to start

Postby Zlodo » 01 Sep 2011, 01:39

MyEmail {l Wrote}:I was speaking from experience, so in essence I have "gone to the chess tournament and won 1st place".


You were judging you were an expert from your own clearly limited experience. 3 years is not enough to become an expert at any language and therefore to be able to judge yourself whether you are an expert, much less 8 languages.

MyEmail {l Wrote}:That's where you missed the point. Learning more languages gives you all that and more. I still wish C++ macro's weren't so gosh darn limited, and had more Scheme-like functionality. Oh snap, I can implement a pre-compile perl hook to add that functionality for me! Now I have the power and simplicity of C with the absurd power of Scheme-like macros. :O Its dumb things like that that people who don't know multiple languages can never do. They are stuck with limited functionality in the C-macros while I dance around them with a vast set of macro tools.


Or perhaps they prefer to avoid doing this kind of things because it frankly sound like an awful idea. It doesn't solve the biggest problems with the standard C preprocessor (macro arguments evaluated multiple times, no type safety, no knowledge of the type system, etc.), would make debugging confusing (as you'd be stepping through the macro-expanded version of the code you initially wrote), it makes your build system more complicated (because of the additional scheme preprocessing pass) and your code more messy (because of the scheme bits thrown in the middle).
Macros in C++ are kind of pointless anyway for the vast majority of situations when the language have much better alternatives such as inline functions and templates. (and constexpr in C++11)

There are situations where generating or preprocessing code has its place but it's a source of headache that shouldn't be done lightly.
Zlodo
 
Posts: 17
Joined: 14 Dec 2009, 12:36

Re: where to start

Postby MyEmail » 01 Sep 2011, 04:47

Zlodo {l Wrote}:You were judging you were an expert from your own clearly limited experience. 3 years is not enough to become an expert at any language and therefore to be able to judge yourself whether you are an expert, much less 8 languages.

I mastered them in 3 and have been applying them for well over 8 after that. I was also judging from the half dozen or so programming tournaments I have participated in since, not to mention my experience with other developers online, and in those from my current (and previous) employment(s).

Zlodo {l Wrote}:Or perhaps they prefer to avoid doing this kind of things because it frankly sound like an awful idea.

See, that's a prime example of how people are plagued by conventional knowledge. Their too stuck on what society teaches them instead of pushing the boundaries and expanding. Please don't make me list whats wrong with this (Bill Gates, Einstein, Galileo, etc).

Zlodo {l Wrote}:It doesn't solve the biggest problems with the standard C preprocessor

It doesn't? You could have fooled me, as I certainly don't have to deal with most of those issues (not even the debugging one). The features I have implemented are very useful, and actually fixes many of the issues the C macro system has. In fact, and just for an example, debugging is actually much easier than traditional C.

Zlodo {l Wrote}:but it's a source of headache that shouldn't be done lightly.

And you say I lack experience? Rofl, this stuff is the easiest crap on earth, which is why I do it on a whim. Why don't you try writing a ping-pong game in Scheme, then you will know what a REAL headache is. Oh, and anyone who has issues with C-macros are just too inexperienced to use them correctly ;).


I guarantee you have never seen code like mine before :). Take a look at a quick test-case scenario (which demonstrates the most basic features):
{l Code}: {l Select All Code}
// The extra functionality I have added allows the macros to literally write code in a Scheme-like fashion:
//      __PREPROCESSOR_OUTPUT__ is the output this macro produces that is substituted wherever this macro goes (if no reference to this is made the output of a macro behaves like a normal C-macro)
//      ##= is a operation like += instead performing the ## equivalent
//      #foreach will loop through each preprocessor variable and store it in the requested ident
//      #for works like a C for loop
//      #local_define defines a variable locally inside the macro (and sub-macros called within that macro).  It does not leave the scope of the parent macro.
//      #typeof is replaced by the type of of its argument (if it is already a type it returns the type)
//      NOTE: this is only the tip of the iceberg :D

// used to create unique identifiers for constructors, much the way C++ works under the hood.
#define OBJECT_CONSTRUCTOR_IDENT(objectType, ...) \
    __PREPROCESSOR_OUTPUT__ = objectType ## _constructor_;
    #foreach(__VA_ARGS__, arg) __PREPROCESSOR_OUTPUT__ ##= #typeof(arg) ## _;

// creates the unique identifier of a constructor for automaticaly calling while hidding in standard C syntax
// the object is passed via the first variable called object
#define defineConstructor(objectType, ...) OBJECT_CONSTRUCTOR_IDENT(objectType, __VA_ARGS__)(objectType * object, __VA_ARGS__)

// allocates a object by a perticular constructor (note: this version unrolls the loop--using sparingly and beware big executables!)
#define allocObject(objectType, count, ...) \
    objectType * objects = calloc(number, sizeof(objectType)); \
    #for(0, count, i) { \
        OBJECT_CONSTRUCTOR_IDENT(objectType, __VA_ARGS__)(&objects[i], ...); \
    } \
    __PREPROCESSOR_OUTPUT__ = objects;

// a wrapper to allocObject() to mimic C++
#define new(objectType, count, ...) allocObject(objectType, count, ...)


typedef struct someStruct_t {
    s32 someValue;
} someStruct;


void defineConstructor(someStruct, s32 someValue) {
    object->someValue = someValue;
}

s32 main(s32 argc, c8 ** argv) {
    // allocates 10 someStruct's, passing the value 5 to the appropriate constructor
    someStruct * test = new(someStruct, 10, 5);

    printf("%d\n", test[4].someValue);

    return 0;
}


That implements a memory allocation system similar to C++, but is much faster (performance AND dev-time wise). It is also far more versatile. For example, in a single new() call I could pass separate someValue arguments to each constructor, which is impossible to do in C++ without workarounds (allocate an array of pointers to objects, and manually allocate each object). I could even call different constructors AND use different parameters for each in a single new() call--try and do that in C++ :D. The unimaginably great potential found in this basic example alone shows just useful it is.

For another example consider debugging. After expansion the code looks like this: (see code just bellow). That is the code the compiler ends up with, as all the macros are handled in a pre-compile hook. Because everything is written in so simple terms debugging is a cinch--it in no way suffers from tradition C-macro issues with regards to bugs and/or debugging. Do note the debugging comments it adds to make things even easier.

{l Code}: {l Select All Code}
// Original code
typedef struct someStruct_t {
    s32 someValue;
} someStruct;





/*
    Expanded macro 'defineConstructor' called on line 46 in 'main.c'.
        Original:
            defineConstructor(someStruct, s32 someValue)
        Output:
            OBJECT_CONSTRUCTOR_IDENT(someStruct, s32 someValue)(someStruct * object, s32 someValue)

    Expanded macro 'OBJECT_CONSTRUCTOR_IDENT' called on line 1 of macro 'defineConstructor' in 'main.c'.
        Original:
            OBJECT_CONSTRUCTOR_IDENT(someStruct, s32 someValue)(someStruct * object, s32 someValue)
        Output:
            someStruct_constructor_s32(someStruct * object, s32 someValue)

*/

void someStruct_constructor_s32(someStruct * object, s32 someValue) {
    object->someValue = someValue;
}

s32 main(s32 argc, c8 ** argv) {
    /*
        Expanded macro 'new' called on line 52 in 'main.c'.
            Original:
                new(someStruct, 10, 5);
            Output:
                allocObject(someStruct, 10, 5)

        Expanded macro 'allocObject' called on line 1 of macro 'new' in 'main.c'.
            Original:
                allocObject(someStruct, 10, 5);
            Output:
                someStruct * objects = calloc(10, sizeof(someStruct));
                #for(0, 10, i) {
                    OBJECT_CONSTRUCTOR_IDENT(someStruct, __VA_ARGS__)(&objects[i], __VA_ARGS__);
                }
                __PREPROCESSOR_OUTPUT__ = objects;

        Expanded macro 'OBJECT_CONSTRUCTOR_IDENT' called on line 3 of macro 'allocObject' in 'main.c'.
            Original:
                OBJECT_CONSTRUCTOR_IDENT(someStruct, 5)(&objects[i], __VA_ARGS__);
            Output:
                someStruct_constructor_s32(&objects[i], __VA_ARGS__);
    */

    someStruct * test = NULL;
    {
        someStruct * objects = calloc(10, sizeof(someStruct));

        someStruct_constructor_s32(&objects[0], 5);
        someStruct_constructor_s32(&objects[1], 5);
        someStruct_constructor_s32(&objects[2], 5);
        someStruct_constructor_s32(&objects[3], 5);
        someStruct_constructor_s32(&objects[4], 5);
        someStruct_constructor_s32(&objects[5], 5);
        someStruct_constructor_s32(&objects[6], 5);
        someStruct_constructor_s32(&objects[7], 5);
        someStruct_constructor_s32(&objects[8], 5);
        someStruct_constructor_s32(&objects[9], 5);

        test = objects;
    }




    // Original code
    printf("%d\n", test[4].someValue);

    return 0;
}


In fact, I would argue it is easier to debug than C++ code. Consider this example: Each and every function (including constructors) have a unique identifier because its required in C. This means you know exactly what constructor/function is being called, whereas in C++ function mismatching can happen all the time because it does not require unique identifiers--you could accidentally be calling the wrong constructor because of a inline conversion operator, or the countless other situations where this could occur. On the flip side of the coin in my C+Scheme hybrid this will never happen.

Back to the original topic--this is just a very very very small taste of the amazing functionality wrought by a pre-compile hook. And even that is just an unimaginably small chunk of the vast benefits that could be gained by learning multiple languages (its just one example amongst dozens). IMHO every programmer would benefit astronomically from learning multiple languages. If not for the experience value, than the marketability it adds to your resume :).
MyEmail
 
Posts: 107
Joined: 06 Jul 2011, 08:58

Re: where to start

Postby charlie » 01 Sep 2011, 07:12

It's all well and good that you can create the ultimate code pipeline with a combination of several or all of 8 languages, but this is missing the point of the thread entirely.

Beginner | .....shortest path..... | Game programmer

Game programming is no longer about squeezing maximum performance out of hardware unless you are at the deep end of it. For 95% of people trying to make a game, something straightforward like Lua/Love or Python/PyGame is probably a good starting point.

Pre-compile hooks and bitwise operators... you're going places that a novice indie developer frankly does not need to be going.
Free Gamer - it's the dogz
Vexi - web UI platform
User avatar
charlie
Global Moderator
 
Posts: 2131
Joined: 02 Dec 2009, 11:56
Location: Manchester, UK

Who is online

Users browsing this forum: No registered users and 1 guest

cron