Since the variables.cpp file was included in the main (template?) .cpp file, it would have created variables global to anything in that file and static would be fine (but totally unnecessary since it is in the same scope).
The idea behind using a .h/.cpp pair with 'extern' in the .h file is to make the variables in the .cpp file available to any other .cpp file, in the same project, that includes the header file. That is how to make something truly global within the compiled application.
'static' is very useful within functions and class method to make sure that a variable that is NOT a global one maintains its value between calls. If used in a function/class method, the scope for the variable is only that function/class method.
So, if you only really have one code file that uses a set of variables, declare them in that file. But by using a .cpp file in Visual Studio, it effectively gets compiled and linked in twice. The first time is as its own file and it gets added. Then again because it is included in another file. I'm actually surprised that this didn't cause a compile error, complaining about duplicate values.
As for dynamically creating and deleting stuff causing issues. You really only have an issue if you don't clean up after yourself. I did recognize that your example use of your class was using a constant string. If you assume that you will always use only a constant like that, there is absolutely no reason for your class to store the pointer. As a string constant it is taken care of by the standard stuff that hides behind everything (not AppGameKit, basic C/C++).
And, as long as you are consistent with your deletes, it does get cleaned up. Garbage collection is one of the things under the hood of every application. How effective it is depends on the platform. I have not seen frequent create/delete things causing issues in any well designed bit of code in all my years programming. And I've worked with some very complex systems. If you create a class that dynamically creates something, make sure it cleans it up in its constructor. Otherwise, you do get memory leaks.
I also just noticed that your header file ends with an "#endif". But there is no "#if<anything>" at the start. I assume that is just a copy/paste issue.
Now, I didn't mention it in my first post, but the way you are initialising txtno might not be a good idea, and it should not be defined as a const. This might be a better set of code for your class:
#ifndef _MY_TEXT_THING_
#define _MY_TEXT_THING_
#include<iostream>
using std::ostream;
#include "agk.h"
class Text
{
friend ostream& operator<<(ostream &output,Text & text)
{
agk::Print(text.GetTextString());
return output;
}
public:
Text(const char *any)
{
txt = (char *)any;
txtno = agk::CreateText(any);
agk::SetTextSize(txtno,25);
}
~Text()
{
agk::DeleteText(txtno);
}
void SetTextPosition(float posx, float posy)
{
agk::SetTextPosition(txtno,posx,posy);
}
const char * GetTextString()
{
return (const char*)txt;
}
private:
char *txt;
unsigned int txtno;
};
#endif
Note the following changes:
1. 'const int' changed to 'unsigned int'. The identifiers returned by all the create/load commands is of type 'UINT', which is unsigned int. And, while you are not changing the value, it is not a constant value and setting a constant value outside of its initial definition should not work.
2. changed 'char*' to 'const char*' in the constructor since that is what you are passing it and that is what you appear to want the input always to be. While compilers will allow mixing the types, they will also complain about it. Xcode always complains about type differences.
3. explicitly set txtno using the CreateText call instead of the inline initialisation (I don't know if that is how it is referred to, but I mean the ': txtno(agk::CreateText(any)' code. I suspect this would cause some issues. I wouldn't recommend using function calls as inline member initialisers. I'm not saying it can't be done, but I don't.
4. turned the input parameters for SetTextPosition to floats instead of ints. The actual agk::SetTextPosition command expects floating point values.
Cheers,
Ancient Lady
AGK Community Tester and AppGameKit Master