Programming tips

Anything that doesn't fit into the other forums.
Post Reply
gyboth
Site Admin
Posts: 1421
Joined: Sat Dec 18, 2004 8:42 am
Location: Passau, Germany
Contact:

Programming tips

Post by gyboth »

Oki asked me to give some hints on C++ and GTK programming, and i thought i might as well make it publicly, so here are the things i learned while making Bygfoot.
  1. C++
    Unfortunately i can't tell you anything about C++. i'm programming in C. what i can tell you is that probably it's a good idea to learn C++ instead of C because after all C++ is the successor of C in a way. nevertheless it should be easy to find some decent C++-tutorials on the net.
    here are some general rules one could follow to make the code well-structured and clear:
    • functions begin with filename prefix (e.g. functions from 'file.c' begin with 'file_': 'file_find_support_file')
    • structs are capitalized: LiveGame
    • functions that return a value end with '_get': player_cskill_get
    • functions that change a value end with '_set': player_cskill_set
    • macros are uppercase LIG(i)
    • functions returning gboolean (TRUE or FALSE) start with 'query_' and then the filename prefix, e.g. query_team_is_in_league(Team *tm)'
    i'm not following all of these in 1.9 because i only realised the usefulness of some while working on this version.
  2. GTK and Glade
    i don't write any part of the Bygfoot interface myself. anything you see, be it a window, a button or else, is made with glade. using glade has a few disadvantages, but it spares a lot of programming time if you can make the interface by clicking around with your mouse. here i found a nice tutorial for glade 2.

    of course glade only generates the interface. you still have to define what happens when the user interacts with it. this is then a mixture of normal C(++) functions and GTK stuff.
    after gathering some experience with GTK, it turns out to be fairly easy to use. of course the best way to learn it is by making some small project.
    here is the official and rather nicely made GTK tutorial. this is a good way to start learning.
    the next step is to use the API reference, which can be found here. after you've learned the basics you won't need anything else for most of the things you want to do.
  3. GLib
    GLib is the low-level library that's being used a lot in GTK. it contains a lot of things that make your life easier, so if you have to incorporate some low-level functionality in your program (e.g. a dynamically growing and shrinking list) you should first check whether there are functions for it in GLib. the two things i use most in GLib are GStrings (dynamically growing and shrinking strings) and GArrays (dynamically growing and shrinking arrays). the biggest benefit of such GLib data types is that you don't have to take care of allocating memory; it's all done automatically.
    here's the reference manual for GLib
i hope this post is useful for some of you; if you have questions, don't hesitate to post them.

gyözö
Press any key to continue or any other key to quit.
scristian
Posts: 10
Joined: Mon Dec 27, 2004 1:25 pm
Location: Romania
Contact:

Post by scristian »

using glade has a few disadvantages,
What are those disadvantages ?
Romanian translator of Bygfoot.
gyboth
Site Admin
Posts: 1421
Joined: Sat Dec 18, 2004 8:42 am
Location: Passau, Germany
Contact:

Post by gyboth »

scristian wrote:What are those disadvantages ?
only minor ones. glade doesn't always support all the new functionality and widgets in the latest versions of GTK. and as far as i could see, there isn't a way to spread a window over numerous glade files, which results in the source code for the window to land in one .c file; if the window contains a lot of widgets, that file can be rather big.

but as i said, these are minor problems and they are absolutely outweighed by the advantage of not having to program the interface yourself.

gyözö
Press any key to continue or any other key to quit.
Lomion
Posts: 58
Joined: Thu Jun 23, 2005 10:14 am
Location: Grenoble, France

Post by Lomion »

My (silly) programming tips:

1) use Perl
2) When switching a value from 0 to 1 or 1 to 0, use: var = 1 - var

I think it's enough for today's lesson. Tomorrow, in our history of programming class, we will study how the downfall of assembler made the poor GOTO disappear off the face of the planet.
Post Reply