Lets take apart a .c file for knowledge

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Lets take apart a .c file for knowledge

Post by Money_YaY! »

Hello, I am reading my C and C++ books diligently. But when I go to he blender code I am still lost. The code just seems to flow into one another and gets lost. So I am in questions of how Blender uses a .c file to build a feature.

Also I would love to know how blender picks and chooses files to add as a file for compile. Like I want to build a Test Blender version that has NOTHING at all in it just the menus blank and basic windows with nothing else. That way I can figure out how to see how things are built into it.

I know that the interface stuff is in two files and buttons placement is not nice and clean. I could be wrong with all of these of course.

The file I have choosen is a messy one.
http://aprilcolo.com/oh/natch/simplefor ... armature.c

I would like to find out how most of it works as a feature set.. Now my log of what I know is this:

This is a comment /* * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. */
/* */

This is a load header file: #include <stdlib.h>
And this is a Blender header file that is loaded into it to: #include "DNA_action_types.h"

This is a declared variable: #define B_YELLOW 0x77FFFF and it make whatever B_YELLOW is into a color.
Now in that file it says " /* >>>>> FIXME: ARG! Colours should be defined in a header somewhere! */
/* Note, these came from drawobject.c They really should be in a nice header file somewhere */ "
Does this mean that there are more than one areas of data that contain color information ? Say I wanted to change the color of the armature to green will I have to edit more than one file ?

extern int tottrans; /* Originally defined in editobject.c */ <---- I guess that means in extern folder int file tottrans....

#define TEST_EDITARMATURE {if(G.obedit==0) return; if( (G.vd->lay & G.obedit->lay)==0 ) return;} <----- Code I do not know yet
== means equal, so lay)==0 is zero. ... The rest I do not know.

static void editbones_to_armature (ListBase *bones, Object *ob); <----- More I have no clue of


Blah, it goes on from there. Now, what 'file' or code piece calls for the editarmature.c file and how can I find it ? And what does this editarmature.c file need to complete itself or is it a standalone file that could theoretically be removed ?

I know these questions are to many but I need to know how "blender" works, as that is mostly the only reason I will try to learn C and I just want to learn the exact methods I need to fight with it. I have a few books but it only helps so much.

^v^ More later...

gabio
Posts: 0
Joined: Thu Jan 15, 2004 6:41 am
Location: Canada - Québec - Sherbrooke
Contact:

Post by gabio »

I completely agree.
Do you mind if I use this threat too to learn a bit about the code. I'm a still a beginner in c, let say about 2 years now that i'm in computer science. the coding knowledge is not a real problem(actualy just some very fancy function and standard lib. i don't know the use or meaning) but the real problem is, all this... mess. I'm reading this file, editview.c for gesture, and got the way it see gesture and switch... but it's all this function it's calling i'm never done trying to read it all.
anyway, i'll stop complaining as i know i must first understand some base, like the way blender is coded or the way all this structure is assembled... I looked over the doc of doxygen but it's damn empty.
don't worry, i'm reading all the dev. doc on the left of this screen too ;-).

So Money_YaY!, I can't realy help you be i'm gonna look at this thread closely.

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Re: Lets take apart a .c file for knowledge

Post by theeth »

Money_YaY! wrote:This is a declared variable: #define B_YELLOW 0x77FFFF and it make whatever B_YELLOW is into a color.
This is NOT a variable. Anything that begins with # in C/C++ is an instructions for the preprocessor part of the compiler. This one here is the most basic definition. It replaces all instances of a word in the code by the value that follows (replaces all B_YELLOW by 0x77FFFF which is a value in hexadecimal for the color yellow).
#define TEST_EDITARMATURE {if(G.obedit==0) return; if( (G.vd->lay & G.obedit->lay)==0 ) return;} <----- Code I do not know yet
== means equal, so lay)==0 is zero. ... The rest I do not know.
Same thing as before but this replaces the word with the block of code that follows.

BTW, == test if two values are equal. Not to mix with = which is an assignation operator (and always evaluates as 1 when the assignation works). But you probably knew that for the first couple of pages of the book.
static void editbones_to_armature (ListBase *bones, Object *ob); <----- More I have no clue of
This is a function prototype. It tells the compiler what to expect for calls of this functions. You tell the compiler what to expect for a function either by declaring the function before it is called or by putting a prototype before all the calls and then declaring the function later. There's a lot of function prototypes in the .h files too.
Blah, it goes on from there. Now, what 'file' or code piece calls for the editarmature.c file and how can I find it ? And what does this editarmature.c file need to complete itself or is it a standalone file that could theoretically be removed ?
It is not a standalone file. The functions in there are called whenever an action is used to act on an armature.

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Post by Money_YaY! »

Thank you theeth. :D

This part
It is not a standalone file. The functions in there are called whenever an action is used to act on an armature.

Martin
What is that in detail? As in during compiling it will call for it a number of times or during the programs oparation. If it is during compiling then how could I remove it from compiling ? As in how can I find what file is asking for it , like keywords to look after and such...

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Post by theeth »

C files are not used when running the program, they are compiled in the executables.

As to where the functions can be called from, that's pretty much anywhere, but mostly the menu files, the toolbox, the buttons window and the hotkey file (space.c).
To know when it is called, look for the functions name from editarmature in other files.

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Post by Money_YaY! »

theeth wrote:C files are not used when running the program, they are compiled in the executables.

As to where the functions can be called from, that's pretty much anywhere, but mostly the menu files, the toolbox, the buttons window and the hotkey file (space.c).
To know when it is called, look for the functions name from editarmature in other files.

Martin
Hum, then in your honest opinion, is this method a bad thing or a good thing ? Should Blenders menu system build the contents of it's menus during compile as a dynamic find and seek ? Or is it better to hard code each one in as it is currently? I think it would be nice if it did it dynamically, as that would help in getting features as a per file basis instead of a seek and find mission...

Just observing. ^v^

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Post by theeth »

Features are in a per file basis already. If you want to link a feature with a menu or whatever, you have to call the function, it doesn't magically know what to do.

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Post by Money_YaY! »

theeth wrote:Features are in a per file basis already. If you want to link a feature with a menu or whatever, you have to call the function, it doesn't magically know what to do.

Martin
Well it should, Boy! Computers are dumb :P :D

SirDude
Posts: 233
Joined: Sun Oct 13, 2002 7:37 pm
Location: University of Minnesota (USA)
Contact:

Post by SirDude »

Just a couple of other side comments.
Lets start with this question:

>> Also I would love to know how blender picks and chooses files to add as a file for compile.

It depends on the build system. If your using scons
there are these files SConscript that specify what
files make up the program.

If your using the Nan Makefiles, the makefiles specify directorys and include all of the files in a given directory that are a *.c or a .cpp and use those.

Moving on, the next question I'll deal with is:
>> Blah, it goes on from there. Now, what 'file' or code piece calls for the editarmature.c file and how can I find it ?

As you know from what youve learned so far. The first fuction that gets called is main() after that you can just follow the code and work your way through things to see what gets called when. You will have to go through a bunch of different files, tools to help you do this would be grep/cscope/ctags and I'm sure there are others.

Going backwards is similar. If your in the
do_it_to_it function and you want to see what calls that function you just search all of the other files for do_it_to_it.

For an example of doing this look at:
http://www.blender3d.org/cms/Adding_com ... 184.0.html

Ok now to take a step back and answer this question:

>>Also I would love to know how blender picks and chooses files to add as a file for compile. Like I want to build a Test Blender version that has NOTHING at all in it just the menus blank and basic windows with nothing else. That way I can figure out how to see how things are built into it.

This is not an easy task and would not be straightforward. Lots of times functions are grouped by the tasks they preform not where they are in the program. This makes it somewhat complicated to comment out large sections of code. What would be a better exercise is to pick a button in the interface and back trace through the blender code to find the function that button uses to do its work. Another thing would be find the main function and go through it and try to see how blender starts up. Where does it parse the command line arguments, where does it draw the gui, etc....

Then start with little changes to the code. How do I update the tooltip for blah... How do I add a button to the render panel that has a different output size preset.
Then just work your way up...

gabio
Posts: 0
Joined: Thu Jan 15, 2004 6:41 am
Location: Canada - Québec - Sherbrooke
Contact:

Post by gabio »

The first fuction that gets called is main() after that you can just follow the code and work your way through things to see what gets called when.
Just a note, after some search, it appear there is more than 1 main() function. Though if i understand the story of blender right. the first one is in there:
\blender\source\creator\creator.c(195) because creator was the heart of blender.
But what about this:
\blender\source\gameengine\Rasterizer\RAS_texmatrix.cpp(93):int main()

I can see this is not the the good one but i didn't know it was possible to have more than one main in c, i suppose that this is a different system so.
Then start with little changes to the code. How do I update the tooltip for blah... How do I add a button to the render panel that has a different output size preset.
Then just work your way up...
Good idea, and i'm at this stage, experience.

For an example of doing this look at:
http://www.blender3d.org/cms/Adding_com ... 184.0.html
very good ref. damn! this section of the website has got some new stuff. I'm on for some read...

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Post by Money_YaY! »

SirDude: that is a lot to comment on. So I will just poke at other things right now.

Question: Is Blender built messy? If so would it help to just make things cleaner ?

Q: What is a module? My knowledge of a mdule is like a plug in, it lives in a dir or file and it gets parsed during compile or as others have it during load time. I for one am against "during load time" as I want a super fast open and close Blender with multiple Blenders open if needed.

Q: Is there some way to get others to edit basic code ... eh lets hold off on this one...

Q: How is code structured ? I know it is a bunch of knowledgeable thoughts and database structure, but when I go to read the code it flows nothing like that at all. In fact it is just like a long boring novel, not anything like CSS HTML editing and clean style sheets. Forgive my example on that, I just love how CSS has a real 'system' to it's self.

Q: ummm... I have more, but I need to learn how Blenders C and C++ works before I can do anything, as right now a lot of it sounds redundant and journey keyed instead of : Features, GUI, Logic, New Maths, comments-docs, Functions, and so one more blocks ...

Q: Oh and how much in a header is a major area, as in could everything be put in Header files so to speak ? I might be confused about headers.

Building a clean blender with no features in pratice should be easy, but since it is not, there seems to be a problem there. That is still a goal. But it seems it is only avalible if everything is clean up as modules or freedom files...

Please forgive any foolishness I seak of but I must learn it somehow, but I want it where 'others' can add to it freely in some way...

theeth
Posts: 500
Joined: Wed Oct 16, 2002 5:47 am
Location: Montreal
Contact:

Post by theeth »

Money_YaY! wrote:Question: Is Blender built messy? If so would it help to just make things cleaner ?
Some parts are. And yes, it would help to make things cleaner. Code cleaning is planned and in order.
Q: Is there some way to get others to edit basic code ... eh lets hold off on this one...
Yes, lets do that :)
Q: How is code structured ? I know it is a bunch of knowledgeable thoughts and database structure, but when I go to read the code it flows nothing like that at all.
The code is structured by functionnality. For the record, a database doesn't necesarely flows linearly, you can have a lot of tables crosslinked together. And that wouldn't necesarely be a bad design.
In fact it is just like a long boring novel, not anything like CSS HTML editing and clean style sheets. Forgive my example on that, I just love how CSS has a real 'system' to it's self.
Both HTML and CSS are markup languages, by definition, they have to flow linearly (unless you add dynamic content, mind you).
Q: ummm... I have more, but I need to learn how Blenders C and C++ works before I can do anything, as right now a lot of it sounds redundant and journey keyed instead of : Features, GUI, Logic, New Maths, comments-docs, Functions, and so one more blocks ...
That's exactly how it is already. All the GUI is neatly tucked in separate files and then reseparated by section. Same for features which are separated regarding the data they manipulate.
Q: Oh and how much in a header is a major area, as in could everything be put in Header files so to speak ? I might be confused about headers.
Headers are only definition, they cannot countain functionnal code.
Building a clean blender with no features in pratice should be easy
And you say this with your big coding experience? :roll:

Martin
Life is what happens to you when you're busy making other plans.
- John Lennon

gabio
Posts: 0
Joined: Thu Jan 15, 2004 6:41 am
Location: Canada - Québec - Sherbrooke
Contact:

Post by gabio »

here, i have found some valuable information that may help you to read the code. Look for "#define *STUFF*" stuff. This set some variable the compiler is using to replace word by number or function as explaned over. But it's very valuable to know what mean all these MAJ word, for exemple a lot of event are defined by number, keys are too.
ex: #define B_SHOWSPLASH 70
this define that if some one ever call for this even like in \blender\source\blender\src\headerbuttons.c where this function: "void do_global_buttons(unsigned short event)" is waiting to be called with an even, then the guy would juste have to write: do_global_buttons(B_SHOWSPLASH) so migicaly a splash would appear because this function, the do_global_buttons, will switch to try all the event possible and will finaly test this winning solution:
case B_SHOWSPLASH:
show_splash();
break;
you may want to peak at \blender\source\blender\makesdna\ where a lot of definition take place for what is apearing to be a prototype of a standard blend file. Also this file \blender\source\blender\include\blendef.h is full of interesting variable handling different even number and value.
And of course by a quick search you can alway find some related definition or use in others file so it's not so much of a problem for now.

i'm personaly using msvc wich is a IDE(integrated developement environement), and search function is very easy with such a tool. there is some free tools out there : one I found to be very good and compatible with msvc6 project is devc(http://sourceforge.net/projects/dev-cpp/). anyway it's just a sugestion, as i tried it myself before copy-past the link, its very good but lack the ability to open more than one project at a time. (Each module in blender is a project)Just played with it for almost a minute but for coding it's a nice parser nonetheless. working on windows and X11, can handle compiler and debug.

hannibar
Posts: 50
Joined: Wed Oct 16, 2002 3:02 pm

Post by hannibar »

I hope nowbody minds if I ask a question in this thread too.

I would like to know if there's basic documentation on (all) the structures found in makesdna (or other directories).

For example :

Code: Select all

typedef struct EditEdge
{
	struct EditEdge *next, *prev;
	struct EditVert *v1, *v2, *vn;
	short f,h;
	short f1, dir;
} EditEdge;
I understand what this structure is used for, and when it is used, but I don't have a clue what the variables f, h, f1 and dir mean in this example.

So is there a place where all these structures are documented? Or is there a way to find out? (I already looked in the code (the extrude code to be specific), but I couldn't understand the use of these variables in the code for this example)

Money_YaY!
Posts: 442
Joined: Wed Oct 23, 2002 2:47 pm

Post by Money_YaY! »

theeth wrote:
Building a clean blender with no features in pratice should be easy
And you say this with your big coding experience? :roll:

Martin
I don't proof read enough :P I meant that the bare basics of Building Blender 'should' be easy to build a basic Blender , But that is obvisouly not the case at all. I know it is not possible at all from me at this point, and have heard others say that it is not easy at all.

Post Reply