ok.
http://www.dev-ch.de/upload/files/De...lGL2_Demo2.zip (831.67 KB)
here is the complete source code. you might not be able to compile and run it, but the principe is clear.
the variables that are static are declared in matrix.cpp and want to be used in virtualGL.cpp
i hope you understand the code as it is, it is very long you know...
edit: removing the static keyword does not make a difference
Last edited by Devils Child; 01-29-2008 at 02:43 PM.
If you want to use those variables in another source code file, you must not declare them static in matrix.cpp. Declare them normally in matrix.cpp, and use the "extern" keyword in virtualGL.cpp.
Having said that, you really really really really really really want to make that matrix a structure, and maybe turn some other things into structures and classes as well. Doing so will (1) make all your computations much easier, and (2) allow you to have more than one matrix at any given time, and (3) make writing code much easier, since you can use somewhat-more-normal syntax. (For instance, look at vgliTriangleFill: instead of fifteen input parameters, you could pass in 3 Point objects, or better yet a vector/array of Points, and then all those nearly-identical lines of code could be done with a for-loop, and all those swaps could just be swap_Points, etc.)
i was thinking of putting it into a class later, but while we are at this right now: i tried once to build up a texture class, but that gave EXACTLY the same problem: i had problems with the declarations, so:
1. (how) do i have to declare a class in the header file?
2. when this is done, i should be able to access this class from _every_ cpp file that is included there.
/* the thing with the triangle fill routine: hm i think it's a lot easyer and a bit of my style since in c++ you can wrap lines between tokens this is giving me the oppertunity to do more than 20 parameters to a function LOL */
You'll need to put the class definition in an included header file, yes.
Objects are just variables, but of a fancier type (instead of int i, you have Point i); so the same scoping rules apply: static variables are only visible in the file in which they are declared; extern variables are defined elsewhere and used in this file, etc.
yeh, i know how to use classes but i dont know how to declare them in the header files *shame*
and when i write extern instead of static i get the same error
I don't see where the global variable is used nor defined.
Can you explain where?
Also, I suggest you avoid this:
Do it the right way:Code:void swap_int(int* x, int* y) { int temp; temp = *(int*)x; *(int*)x = *(int*)y; *(int*)y = temp; } void swap_int64(__int64* x, __int64* y) {
Code:void swap_int(int* x, int* y) { int temp; temp = *(int*)x; *(int*)x = *(int*)y; *(int*)y = temp; } void swap_int64(__int64* x, __int64* y) {
Perhaps a stupid (as in does-nothing), yet illustrative, example is in order:
point.h:
point1.cpp:Code:class Point { double x, y, z; }
point2.cpp:Code:#include "point.h" Point a, b, c; //global variables -- bad practice, but we're illustrating static Point d, e, f; //complete opposite of global variables -- can never be seen in point2, no matter how hard you try
Code:#include "point.h" extern Point a, b, c; //same variables as in point1, through the magic of linking // if we don't link the object file point1.o, we're in trouble
tabstop: your explanation works! i just have to declare them not static in the main file and extern in all other files. also it is a lil bit messy to declare all those values as extern in the other files, but well...
(elysia: i do that messy thingy only because these swap functions only differs by their datatypes)
so now the only question left is, how to do the same thing wth classes.
i will declare a class in the virtualGL.cpp and want to use it in other files, too,...
As messy as static?
And if you have a lot of functions that differ only in their prototypes, then why don't you use templates?
Um ... that was a class? I don't know what you mean by "declare a class"; if you mean write out code or whatever, you need to have the class definition (the bit that that's in the header file here, plus function prototypes) in a header file, no matter what anyway. You can then write out the actual functions in a .cpp file.so now the only question left is, how to do the same thing wth classes.
i will declare a class in the virtualGL.cpp and want to use it in other files, too,...
What's with the casts?Code:void swap_int(int* x, int* y) { int temp; temp = *(int*)x; *(int*)x = *(int*)y; *(int*)y = temp; }
And why not just use what's already there?
Code:using std::swap; swap(x, y);
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
hm that swap command really exists :P
i replaced my one with that one
now to the classes: all i need to do is write the class declaration including its' functions into the header file and all other files are able to use that class? (cause that ain't working ..)
No, you define the class in the header and implement the functions in a .cpp file (unless it's a template function or template class).
But this still probably leaves the problem that you're going to make global objects. I can just hope you won't do that.
Then include the header and it will magically work.
well, it magically gives me a stack overflowCode:class vglTex { private: public: int w, h, buf[513][513]; /*void LoadFromFile(char path[]) { SDL_Surface* image = SDL_LoadBMP(path); w = (*image).w; h = (*image).h; for (int x = 0; x <= w; x++) { for (int y = 0; y <= h; y++) { buf[x][y] = GetPixel(image, x, y); } } SDL_FreeSurface(image); }*/ };