static namespace members assignment SDL/OpenGL.

Hello, i am working on my first semiserious project in c++ with SDL/OpenGL.

I have written a function that is supposed to load images using SDL_Image and convert them into usable gl textures.

I have a namespace which have some static members which are supposed to store the ID:s of the loaded textures.

When i am trying to assign any value to those unsigned ints nothing happends, they just stay zero for some reason. I cant even assign values using,
textures::TEXTURE_GRASS = 1;

What i find really strange is that they actually have the value 0, they should be unassigned.

The console output is:

The image: "grass.png" loaded succesfully and have the ID: 1
The image: "road.png" loaded succesfully and have the ID: 2
The image: "towerArrow.png" loaded succesfully and have the ID: 3
0
0
0


I will post the texture namespace and the function that loads images.


Here is the init function which initializes the whole game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool game::Init()
{
	/*
            Some unrelated code that i removed for this topic.
        */

	//Initialize textures
	textures::TEXTURE_GRASS = functions::Textures::LoadTexture("grass.png");
	textures::TEXTURE_ROAD = functions::Textures::LoadTexture("road.png");
	textures::TEXTURE_TOWER_ARROW = functions::Textures::LoadTexture("towerArrow.png");
        

        //Just cout:ing the variables to check if they have a value
	std::cout << textures::TEXTURE_GRASS << std::endl;
	std::cout << textures::TEXTURE_ROAD << std::endl;
	std::cout << textures::TEXTURE_TOWER_ARROW << std::endl;

	return true;
}


Here is the LoadTexture function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
GLuint functions::Textures::LoadTexture( std::string filename)
{
	SDL_Surface * tempImage = IMG_Load(filename.c_str());
	SDL_Surface * image;

	if(tempImage != NULL)
	{
		image = SDL_DisplayFormatAlpha(tempImage);
		SDL_FreeSurface(tempImage);
	}else{
		std::cout << "Error: could not load " << filename << std::endl;
		return 0;
	}

	GLuint TextureID(0);

	glGenTextures(1,&TextureID);

	glBindTexture(GL_TEXTURE_2D,TextureID);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	SDL_FreeSurface(image);

	std::cout << "The image: \"" << filename << "\" loaded succesfully and have the ID: " << TextureID << std::endl;
	return TextureID;
}


Here is the textures namespace

1
2
3
4
5
6
namespace textures
{
	static unsigned int TEXTURE_GRASS;
	static unsigned int TEXTURE_ROAD;
	static unsigned int TEXTURE_TOWER_ARROW;
}

static here means that they will have internal linkage and each translation unit (source file together with all includes) will have it's own copy of the variables. What I think you actually want is to use extern declaration in the header
1
2
3
4
5
6
namespace textures
{
	extern unsigned int TEXTURE_GRASS;
	extern unsigned int TEXTURE_ROAD;
	extern unsigned int TEXTURE_TOWER_ARROW;
}
and define them in a source file
1
2
3
unsigned int textures::TEXTURE_GRASS;
unsigned int textures::TEXTURE_ROAD;
unsigned int textures::TEXTURE_TOWER_ARROW;
Last edited on
I dont really understand how you mean But i will take a look at the extern keyword. will they still act global if i define them in a source file like that?

I have learned c++ totally on my own so i have missed alot of those basic stuffs and i have no teachers to ask if i hit a problem.

They teach us VB.NET i my school... And even non OOP VB.NET. so it's really bad.

My point was. It looks pretty dumb to use OpenGL when i dont even know the basics. Just wanted to point out why.

Thank you for your help!
Last edited on
will they still act global if i define them in a source file like that?
Yes. If you define them in the header you will get "multiple definition errors" so you don't have much of a choice.
okay. Does it matter where i define them?
No. As long as they are defined once somewhere in any of the source files it should work.
I am so happy right now when i saw it working i cant describe it. Thank you for you kindness again!
Hmm, i ask this questing right here instead of starting a new thread. How can i acces all the pixels in the texture and raise them a bit? so that the texture will get more light?

EDIT: Or maybe is it better to just make some other textures. Feels like an ugly solution. But accessing pixels like that is very performance dropping right?
Last edited on
Topic archived. No new replies allowed.