static ofTrueTypeFont

hi,

i have a class definition that will be used for a bunch of objects which draw text to the screen and i would like to use a static ofTrueTypeFont member since they will all write in the same font+size.

i tried:

  
  
class MyClass {  
	public:   
		static ofTrueTypeFont font;  
};  
  
MyClass::font.loadFont("Georgia.ttf", 12);  
  

but that’s not compiling.

the docs i read only mention static methods, so i then tried this:

  
  
class MyClass {  
	public:   
		static void setFont(const char* name, int size);  
	private:   
		static ofTrueTypeFont font;  
};  
  
// ...  
  
void testApp::setup() {	   
	MyClass::setFont("Georgia.ttf", 12);  
}  
  

and that compiles properly but fails at linking.

any ideas? thanks

you have to define your static class variables like this:

  
ofTrueTypeFont MyClass::font;  

insert this before

  
void testApp::setup() {     
   MyClass::setFont("Georgia.ttf", 12);  
}   
  

now it should link.

best
joerg

hey joerg,
could you be a little more specific? i’m trying a few things and it won’t work.
does that first line go inside the class brackets or outside?

  
  
class MyClass {   
   public:   
      static void setFont(const char* name, int size);   
   private:   
      ofTrueTypeFont MyClass::font;  
};   
  
void MyClass::setFont(const char* name, int size) {  
   font.loadFont(name, size);  
}  
  

or

  
  
class MyClass {   
   public:   
      static void setFont(const char* name, int size);   
};   
  
ofTrueTypeFont MyClass::font;  
  
void MyClass::setFont(const char* name, int size) {  
   font.loadFont(name, size);  
}  
  

neither of these work…

thanks

it’s like i wrote just ADD the line i suggested. don’t replace the existing definition with the declaration. like you define the method:

  
class MyClass {  
   public:  
      static void setFont(const char* name, int size);  
   private:  
      ofTrueTypeFont font;   
};  
  
ofTrueTypeFont MyClass::font;  
  
void MyClass::setFont(const char* name, int size) {  
   font.loadFont(name, size);  
}   

that’s one of the reasons why i am not a big fan of c++…

best
joerg

you could also try:

  
  
class MyClass {  
   public:  
      static ofTrueTypeFont font;  
};   
  
  
ofTrueTypeFont MyClass::font;  
  

and load your font in main for example,

  
  
int main()  
{  
  
    ofSetupOpenGL(1024, 768, OF_WINDOW);  
  
    MyClass::font.loadFont("Georgia.ttf", 12);  
  
    ...  
}  
  

thanks for the replies, i finally got it to work.

my problem was that i was putting everything in the .h file and kept getting duplicate variable/method errors.

it worked when i placed the

  
ofTrueTypeFont MyClass::font;  

statement in the .cpp

the separation of the code into .h and .cpp-files is the root of all evil in c++.

best
joerg

not if you know what you do :wink:
no seriously, it’s actually a good old and working concept

not if you know what you do :wink:
no seriously, it’s actually a good old and working concept[/quote]

old: yes.
good: what’s good about it?
working: yes, i have to type everything twice. more work.

honestly, it’s just because c is very old. in c it was necessary because of the implementation of the compiler.
java, python, c# (not sure, never programmed) can do without header files. so could i.

best
joerg

[quote author=“jroge”]
old: yes.
good: what’s good about it?
working: yes, i have to type everything twice. more work.

honestly, it’s just because c is very old. in c it was necessary because of the implementation of the compiler.
java, python, c# (not sure, never programmed) can do without header files. so could i.

best
joerg[/quote]

good:
it hides the implementation. so you easily can see the layout and structure of the class. most of the times you are not interested in the implementation anyway.

you can deliver the header (and therefore the function signatures) with your library as part of the documentation.

you can have different source files for the header for testing.
it accelerates the parsing of the compiler.

of course, if you don’t know what you do and are not experienced you’ll easily get into a big mess.

and pls don’t compare it to java, c#, actionscript …
c++ is not an interpreted language and it does not have so much rules where to put the class and source files.

and if you are a good programmer, you have small (template) classes which only consist of header files.

typing everything twice? come on… :roll:

modules in other languages provide the same functionality. see javadoc.

ok. that’s a point.

no it does not. i remember programming c++ before they invented precompiled header files. it was incredibly slow.
and precompiled header files sometimes get out of sync with the rest of the code. i hate it when that happens.

that’s how designers talk about customers: it’s the fault of the user! no it’s flawed design.

java & c# are not really interpreted languages. the difference to c++ is that they compile to virtual machine code (which can be compiled to machine code at runtime).

yes, similar to a module.

[quote author=“didito”]
typing everything twice? come on… :roll:[/quote]

i always feel like i have to do the work the computer can do much better: copying information.

and it can’t even remember that i have included the header file before. no, if to add stupid
#ifndef _BLABLA_H
#define _BLABLA_H

#endif

aaah, i could go on and rant about c++ forever. the bad thing is that there is no real alternative for c++.
c# is created and fucked up by microsoft.
java is slower and can’t access hardware that well on multiple platforms.
objective c is c# for macs.
what else?

best
jörg

i don’t know what you really mean by modules right now.

and i personally like to rely on the header (or exportslist, as close as i can get), and not some javadoc.

i don’t know too much about java programming/compiling, but from my experience the c++ compiler is a lot faster. i think this is because the package stuff is not existing, i have include guards, i watch my includes, i use forward decls.
but if you always include everything or much unused stuff of course it gets slower.
btw, reading tip for everyone: “large scale software design (lacos)”

personally i don’t use precompiled header files. they suck and just tend to screw things up. and my machine is fast enough to compile my fairly small codebase.

ok, got your point. you might be right in the domain of software or tool/application design.
and i don’t say everything is perfect in c++ world. it is not, it is far from perfect. it is an old language, but for me it has no real competitor and still i like the style.
in the end it is a tool you have to master (software engineering has nothing about engineering, it is a craft for me). i’m glad that not everyone can master it in 20 minutes. this would obliterate many of my studies, skills, work.

thx, i know. this was not the point. the point not to compare them is that c++ does have more freedom and not so much (or different) rules concerning source files and packaging.

i can see your point. i would like the computer to assist me too. but the thing is, the computer is not smart enough yet to do this in an appropriate way. it can help but still needs some kind of extra work to refine. maybe it is the fact we all still have work …

in the end, this is my personal professional opinion and everything has become very, very off topic. so maybe let’s leave it here :wink:
i just don’t like the idea to try to convince a novice that <i.e. c++> is not good. maybe try to tell them the reasons why something is that or that way and in the end they will have their own feeling about it.

jm2c
/didi

you are right we are very off topic by now.
it’s just that i feel if i had the right tools i could do much more complex work. now i have to care about memory leaks and pointers, segfaults etc. instead of just writing beautiful code that does amazing things :slight_smile:

i don’t want to convince anybody… it’s more that i’d like to be convinced.

and some of the mentioned points are the reason why i chose to include lua into my next project, so i can have both: speed and beauty. and all with OF.
which is the piece of software that should be praised more often!

all the best
joerg

Cool - I would be very curious about lua and OF.
Would they be separate apps communicating with each other or integrated together?

Keeping this off topic - I have to say the two files thing is a pain to me as well. My solution when I am bashing out code is to just work in the header file. This makes it quick to make changes - add params to my functions etc, change the functionality of my class and most importantly not get too attached to my class structure as it is always quite easy to change. When I feel I have figured it out and its the approach I want to take for that class I then move the implementation over to the cpp file.

I am sure this is ‘bad practice’ by a lot of c++ coders standards but for me this approach helps me writer cleaner, better organized code, faster.

Theo

the cool thing about lua is that it was designed as a scripting language. so it’s very easy to integrate in c.
i am working on a game-engine-like software to do interactive text/sound-mangling.

i was thinking of creating ofxLua but then decided not to as lua is an extension itself and there’s no real need of an extra layer between lua and OF. maybe some OF lua lib would be good to have. maybe i could do it once i have more time…

so if people are fed up with some difficulties with c++ i’d recommend to check out lua.
http://www.lua.org

best
joerg