2

I'm kind of a beginner, and I have gone off written a 10000 line program which mostly uses globals and void functions, in c++.

Anyways, my program doesn't have a GUI, so I am making one for it using Clutter. So in clutter, you use a signal handling function to connect button clicks, motion events, etc.

The signal handling function can only accept one user data parameter. However, many components of the GUI, hundreds need to be accessed by different functions. So I'm putting all my GUI objects in a single struct, and passing it from every signal handling function.

So my program as it is now, (console program) prints press some letter to do something. If you press that letter, launch a certain function. If I eliminate the use of global, I would need to pass as parameters some of these variables.

If I directly insert my code into the GUI, then the signal handler function will launch the appropriate functions, but can only pass a single user data parameter, which as it is now, is already used as a struct with hundreds of GUI members.

Sorry If this all sounds crazy. I'm just trying to re-write my code to use better practices, but with the 10000 long code, and my lack of understanding of some things, I feel pretty overwhelmed.

I'm just looking for some advice about where to start and how to deal with this issue I am perceiving with connecting to the GUI.

And for my question about structs. I am interested in knowing if there is a maximum number of elements that can be inside a struct. If you have an array inside struct, is the access time for that array going to be slower? Is there a difference in how memory is handled for a struct.

Thanks.

MVTC
  • 845
  • 11
  • 28
  • 4
    If you really have 10000 lines of code written this way, it might be easier to start over with proper techniques than to modify it – Seth Carnegie Feb 04 '12 at 22:52
  • 1
    *"many components of the GUI, hundreds need to be accessed by different functions"* - sounds like you should try to modularize your code. – Georg Fritzsche Feb 04 '12 at 22:53
  • Have you looked at classes? They should help you structure the code better. That way you could keep the variables close to the thing they belong to. It's a bit hard help without knowing the problem. I haven't worked with clutter, but I guess you'd pass a pointer to the specific class. Or the handler it self could be a method of a specific class. – Maiku Mori Feb 04 '12 at 22:59
  • Also if you want plain old GUI (buttons, lists, labels, etc.) I don't think Clutter is the best choice, Qt or GTK+ should be a lot easier and they're more mainstream. – Maiku Mori Feb 04 '12 at 23:05

2 Answers2

1

I think your question has a hidden question: How to write GUI application/framework?

Clutter seems very low level from what I quickly Googled. If you want skip a lot coding just use something like Qt Framework which also renders most stuff using OpenGL (or GTK+, depends what kind of license you want).

There's no simple answer if you want to write it more or less from ground up and I'll try to just go over it at a very high level and just highlight the very very basic idea how it usually works.

Since you tagged it with C++ tag, then proper C++ way is to use classes.

You would have classes for things like Sound (MP3), with methods such a decode, encode, etc. Then you would have classes for GUI elements, with methods such as onPaint, onClick, etc.

Let's say you wanted a button. It would be a class with variables such as x, y, width, height, text, clickHandler, enabled and so on. It would have some low level event handlers such as onPaint, onClick (this one would check if the mouse is over the button and if it's enabled, and if it all those checks pass would it would call the clickHandler). It might also have some methods like isEnabled(), disable(), enable(), etc.

Now you're free to make as many button instances as you want, each one has it's own state (enabled or disabled, text, width, height, all of those). You would set a clickHandler for each button.

There are going to be some globals, but most of internal stuff is inside the classes. If you have some sort of hierarchy such a window has many buttons then your Window class would just have a list of buttons. And don't forget classes should each be in a new file unless there are multiple very closely related.

But really you don't want to write anything of this as there are huge libraries already doing it.

As for how to glue it all together you'd probably want to follow the convention used by the specific framework you choose. If you're on Windows and want to make Windows only application then use WPF (or Winforms). Qt is nice for cross-platform stuff and it's quite modern, but the license is a bit constraining if you're making a commercial app.

Maiku Mori
  • 7,419
  • 2
  • 40
  • 52
  • Thanks. I already have GTK as part of the clutter package, and the api is very similar to clutter, so I'm going to give it a try. There is actually a library, clutter-gtk, which will let me embed GTK widgets into clutter. Already found out that I can use some things like GTK file chooser dialog in clutter, connected to a clutter stage. I had planned to make my own file chooser dialog using boost filesystems, and had been a little stressed just thinking about it. – MVTC Feb 05 '12 at 01:44
  • @MVTCplusplus, ah I see, it actually makes sense that it has it as dependency. In that case you could browse some open source (for example https://github.com/search?q=gtk&type=Everything&repo=&langOverride=&start_value=1 ) GTK+ based projects and check how the applications are structured there, you'll probably get some good ideas from those. As for file open dialogs I think it's best to use the one provided by the OS (or GTK in your case which should just use the current OS file open dialog) because most users are used to it and it also contains history and favorites set by the user. – Maiku Mori Feb 05 '12 at 04:23
0

Accessing an element inside a struct is always slower by the cost of having an additional reference to perform, and thus an extra read to RAM. Try grouping objects into structs so that they will be used together so that these memory requests can be cached in faster RAM/onboard cache.

There isn't really a limit to struct members and they are often useful when you exceed the maximum number of parameters a function should take, I was taught (in 2002) the magic number is 7.

Ultimately, when grouping members into structs you should be able to identify which functions use them and instead of passing the struct you can make the function a member and do away with it having to dereference the struct to get at that parameter's members. I think at this point you can probably award the structs the equivalent C++ label, class.

John
  • 6,433
  • 7
  • 47
  • 82
  • "Accessing an element inside a struct is always slower by the cost of having an additional reference to perform, and thus an extra read to RAM." This is not true in most cases. 7 arguments to a function is an extremely large number. Consolidating concepts to structs would simplify this. – Captain Giraffe Feb 04 '12 at 23:16
  • @CaptainGiraffe 7 is the upper limit, some automated tools produce 100's. Ok some cpu's offer indirection as part of their opcode but if you are dereferencing pointers also, then that is used up. The same deal may apply with classes but it is a good argument for an Object Oriented approach. – John Feb 04 '12 at 23:24