Allegro.cc - Online Community

Allegro.cc Forums » Game Design & Concepts » What are design principals for C language?

This thread is locked; no one can reply to it. rss feed Print
What are design principals for C language?
Doctor Cop
Member #16,833
April 2018
avatar

I know the design patterns of Object-oriented languages but I have difficulty in implementing them in procedural languages like C.

Can anybody give me a reference to any design pattern tutorial for C language?

Peter Hull
Member #1,136
March 2001

Doctor Cop
Member #16,833
April 2018
avatar

Thanks, Peter for the links.

What's the meaning of

Quote:

Although, in some cases, the boilerplate that this approach generates may not always be worthwhile, writing imperative C code in OOP style can illuminate how OOP works. The ability to write in this style is also valuable for creating APIs that are shared between OOP and non-OOP languages.

The second line, is this a bad thing?

The first link was more useful and concise, I will read the pdf when I get time.

bamccaig
Member #7,536
July 2006
avatar

At some pain level you can basically achieve object-oriented programming in C. At extremely high pain levels you can also reach full enlightenment, but you can save a lot of time/money/work by using somebody else' solution.

Ultimately, object-oriented design patterns that rely on object-oriented semantics only work in object-oriented languages. Understanding the primitive reasoning behind the pattern and working out how to apply it to a C program is an exercise for the reader. There is no perfect 1:1 mapping. Some things cannot be done in C without great pain, which defeats the purpose of the pattern (it's only useful if it's reliable, and it's only reliable if it's mostly automatic).

Fortunately, several people have already gone through this pain, and you can use their work by picking up an interpreted language such as Perl, Python, or Ruby; most of which are ultimately implemented in C. That's not really the same thing as you're requesting, but what you're requesting doesn't really make sense, and perhaps the answer you needed to hear to it was just this one.

torhu
Member #2,727
September 2002
avatar

There are some techniques used in Allegro 5 that are relevant.

  • Encapsulation (struct fields only accessible via functions)

  • Polymorphism, ie. structs with function pointers for the system drivers

  • Inheritance, check out the event structs

Doctor Cop
Member #16,833
April 2018
avatar

Thank you all for letting me know that it's not my fault that it seems to be difficult to write programs in C, especially bambam (if it's really what they call you).

I just wanted to write a simple software in C to teach it to some students, now I will make it and will try not to do the same again, especially when writing the same thing is easy in C++.

Neil Roy
Member #2,229
April 2002
avatar

It's not difficult to program in C, unless you're trying to create C++ classes in C, which I don't see the point of, why not just use C++ if you wish to do object orientated programming?

I really don't think books on how to do OOP in C will be much help.

My Deluxe Pacman 2 game was written entirely in 100% pure C, no C++, no OOP. The source code is available in the link at the bottom of my message.

What you feel is easy and what is difficult depends on what you want. if you want a simple C program, write a simple C program and forget about object orientated programming. Just write it. If you want OOP, use C++ and classes.

In my own games I create structs in C for the player and enemies. For example, in a more simple pacman game, you would make a struct which contains the player's position, animation frame number, a flag to indicate whether he is dead, the number of lives he has etc. It's not rocket science. You will still need to use the same loops and check for the same events and whatnot that you need to in C++.

You could write all the code to manage the player in it's own player.c and player.h file, much as you would with C++. You then write initialization functions, functions to handle moving the player, boundary checks and what not. Say, with names like: player_init(), player_move() etc... you place prototypes for the functions you wish to be available to the main game (public) in the player.h and you're good to go. You program your game, when you need to move the player, you might call player_move(x, y) etc... it's not difficult.

Sadly, my own game isn't quite that organized, I would do it differently if I were to rewrite it, but the fact that my mess of code works without OOP is probably a testament to C. I didn't separate my functions into different files like I probably should have, but you really don't have to if you don't want that. Especially for a simple program you want.

#SelectExpand
1typedef struct Player { 2 int x; 3 int y; 4 int lives; 5} 6 7player_init(Player *player) 8{ 9 // init player here 10} 11 12player_move(Player *player, int x, int y) 13{ 14 player->x += x; 15 player->y += y; 16 // do other stuff like boundary checks 17} 18 19int main() 20{ 21 Player player; 22 player_init(@player); 23 24 // etc 25}

A good website to learn C from (not C to OOP) is the following website, they have a PDF version on this page as well at the bottom...

https://www.tutorialspoint.com/cprogramming/index.htm

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

Doctor Cop
Member #16,833
April 2018
avatar

Nitehacker, it's not rocket science and I know that writing code in C saves some time because you don't have to write classes but writing classes mean less messy code and I won't have to take the hassle to restructure my code every time it gets bigger than I originally I thought it would get.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

raynebc
Member #11,908
May 2010

That more or less seems to be the case from what I remember about C++. If you're going to be writing all functions to accept a structure for a parameter, you're doing explicitly what C++ does implicitly with objects.

Audric
Member #907
January 2001

I don't think it's a good idea to write C code that painfully tries to imitate C++.
If you're coding within an existing codebase, your C++-lookalike will look horribly out of place. It will not make you practice usual C conventions either.

Doctor Cop
Member #16,833
April 2018
avatar

Audric, you are spot on.
That's what I was trying to say but said something else entirely.

Well, all is well if the end is well.

{"name":"611999","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/6\/f699b3048548b6702cb100e14bc4483a.png","w":1279,"h":684,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/6\/f699b3048548b6702cb100e14bc4483a"}611999

jmasterx
Member #11,410
October 2009

I don't get it though, how else can you code in C other than writing functions that take a struct or primitive as parameter?

Unless you mean that if that struct is mutated by ref, and the function returns void, then it is exactly C++.

I thought C style programming was to define operations (functions) on data (structs) in order to transform input A into output B.

Just about every winapi function has you pass a pointer to a struct or buffer and the function fills that buffer / struct.

Audric
Member #907
January 2001

@jmasterx : Even people who do use this pattern (correctly) may try to overengineer it. For example, "I want foo.c file to only contain files that take a FOO* argument, like the methods of a C++ class. - And now which file should hold the function transfer_item_from_room_to_backback(ITEM *itm, ROOM *room, BACKPACK * backpack)" ?

jmasterx
Member #11,410
October 2009

item_management.c ?

Audric
Member #907
January 2001

Yes, but you intend it to organize (and regroup) code by purpose. Someone who is only used to OOP may see the "composite pattern" of this specific combination of classes (item-room-backpack), and balk at the idea of using the same C file for functions that handle different combination of classes (item-room, item-inventory, item-item)...

People who have only learnt OOP don't realize that in a language such as C you often organize by process (verb) instead of data (noun) : display_player(PLAYER*) and persist_player(PLAYER*) in display.c and persistance.c respectively, rather than everything in player.c

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

jmasterx
Member #11,410
October 2009

I like to use both ways, even in OOP.
In OOP I might have an item manager that listens for collisions between items and the player and puts the item into the backpack. (well, it would probably be some kind of scripting practically but you get the idea).

I like the idea of having mediators to handle interactions between objects.

eg: PhysicsSolver outputs collision events, observed by the item manager which could consume certain collision events.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

NiteHackr basically showed you OOP in C, but I'll bet you he'll deny it...

No, you're probably right about that. But...

Audric said:

I don't think it's a good idea to write C code that painfully tries to imitate C++.

This was basically my main point. If you're going to use C, use C, don't try and imitate C++. If you want C++, use C++.

Personally, I prefer C, but I always say that the best programming language is the one you are comfortable using. Unless of course you're in a team, then you all have to agree on something.

There are a few aspects of C++ that I do like. Vectors are very nice, but rather than use C++, I created my own vector-like functions in C for my own use, mainly because I enjoy the challenge of programming them. Turned out it wasn't that difficult. You can see some of this in my Deluxe Pacman 2 source. I wanted Vector stack style functionality for my path finding, so I wrote my own that is fairly portable in C.

I like this discussion. Lately I've gotten turned on by Data Driven Design and have been moving away from pure OOP. So my code ends up being kind of a hybrid.

Interesting discussion anyhow. I am about the same, there's no reason why we can't have the best of both worlds.

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

jmasterx
Member #11,410
October 2009

Actually, I've also been trying to take a more functional approach when possible: lambdas, ZipWith, Map, Aggregate, Accumulate, etc.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Doctor Cop
Member #16,833
April 2018
avatar

NiteHackr said:

I created my own vector-like functions in C for my own use, mainly because I enjoy the challenge of programming them.

Nitehacker, can you share those valuable resources with the world (GitHub)?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

Nitehacker, can you share those valuable resources with the world (GitHub)?

The link to my homepage (which is hosted on Github) that contains my source (not in Github format, never done that before, just in a ZIP file available) is in my signature.

I also have links to the dependencies I compiled for use with my game and my own compiled version of Allegro 5. Mainly so it can be compiled for anyone that wishes to use what I did. The Code::Blocks project file is in there as well. There's a readme file in the zip that gives details.

The version of the vector stack code in my source is fairly portable, I designed it that way and you should be able to use it as is without any changes.

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

Go to: