Jump to content
  • Advertisement
Sign in to follow this  
JapanFred

Is this the best way? (Textured Objects, C++ SDL/OGL)

This topic is 3546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, Been a member for a while, lost my account though, so here i am re-incarnated! Anyway, just a couple of questions on best practises really. I have 4 3D objects that i render to the screen (Running in glOrtho mode), 2 Paddles, 1 Ball (even though it's a square) and the background. Can you tell what i'm writing...? Now, only the background has a texture, but the other objects just have colour. I have a base class (Object is it's name), which supports Texture or Colour. And renders everything fine, i have just a texture on the Background, and everything else renders with colour. I'm just wanting to know if what i'm trying to achieve is the best way.
/*Class Declaration*/
class Object
{
protected:
	float _x, _y, _z;
	float _width, _height;
	GLuint _texture;
	GLfloat _r, _g, _b;

public:

	void Render()
	{
		_x += this->xVel;
		_y += this->yVel;

		if (_texture != 0)
		{
			//Enable Texturing
			glEnable(GL_TEXTURE_2D);
			//Bind the Texture to this object
			glBindTexture(GL_TEXTURE_2D, _texture);
		}

		//Stack the current settings for colour, so we can set the colour for this object, then pop the settings back when we are done
		glPushAttrib(GL_CURRENT_BIT);

		//Draw this object the correct colour
		glColor3f(_r, _g, _b);

		//Move the object to the correct location
		glTranslatef(_x, _y, 0);

		glBegin(GL_QUADS);
			if (_texture != 0)
				glTexCoord2i(0, 0);
			glVertex3f(0, 0, 0);
			if (_texture != 0)
				glTexCoord2i(0, 1);
			glVertex3f(0, _height, 0);
			if (_texture != 0)
				glTexCoord2i(1, 1);
			glVertex3f(_width, _height, 0);
			if (_texture != 0)
				glTexCoord2i(1, 0);
			glVertex3f(_width, 0, 0);
		glEnd();

		//Pop the colour settings back from the stack
		glPopAttrib();

		//Disable Texturing again
		if (_texture != 0)
			glDisable(GL_TEXTURE_2D);
	}
}


I am loading the texture up using a SDL_Surface to Texture method, and like i said, is working fine, it's displaying correctly, just want to see if this is the best way. The next question, is when i'm rendering all of this, i am pushing/poping the matrix 4 times, and i'm not sure if this is the optimal way either! This is what i'm doing...
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT);

	/*Render the Background*/
	glPushMatrix();
	background.Render();
	glPopMatrix();

	/*Render Player 1s Paddle*/
	glPushMatrix();
	player1.Render();
	glPopMatrix();

	/*Render Player 2s Paddle*/
	glPushMatrix();
	player2.Render();
	glPopMatrix();

	/*Render the Ball*/
	glPushMatrix();
	theBall.Render();
	glPopMatrix();

	SDL_GL_SwapBuffers();


Which again, works, just checking. Any more questions about how stuff is setup just ask, i can post the entire source if required, doesn't bother me! Many thanks guys. Dean Edit - Sorry, code tags didn't work, using source tags :)

Share this post


Link to post
Share on other sites
Advertisement
Afaik there is nothing to improve to your code. Personally I wouldn't choose "Object" as a class name, since an object is really an instance of a class, or in Java, it's the root class. I would go with "Entity" or "Element" or something like that. It's just good practice, might you ever want to make something open source.

Other than that, your code looks very clean.

[edit]

You only need to call glClearColor once, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bear777
Afaik there is nothing to improve to your code. Personally I wouldn't choose "Object" as a class name, since an object is really an instance of a class, or in Java, it's the root class. I would go with "Entity" or "Element" or something like that. It's just good practice, might you ever want to make something open source.

Other than that, your code looks very clean.

[edit]

You only need to call glClearColor once, though.


Brilliant, thanks for that.

The Object name did stick with me for a little while, coming from 3 years of C#, but i figured this was only a little demo application.

Share this post


Link to post
Share on other sites
Also, consider moving your translation code (pos += velocity) into a seperate Update() function or similar :)
Imagine the case where you'd want to render the same 'object' more than once.

Share this post


Link to post
Share on other sites
Quote:
Original post by stein
Also, consider moving your translation code (pos += velocity) into a seperate Update() function or similar :)
Imagine the case where you'd want to render the same 'object' more than once.


Might be me being silly, but why would i want to render 1 object in 2 places?

Sorry if i'm missing something very simple!

Share this post


Link to post
Share on other sites
Quote:
Original post by JapanFred
Quote:
Original post by stein
Also, consider moving your translation code (pos += velocity) into a seperate Update() function or similar :)
Imagine the case where you'd want to render the same 'object' more than once.


Might be me being silly, but why would i want to render 1 object in 2 places?

Sorry if i'm missing something very simple!


Technically, you don't (shouldn't) always render when all the game logic updates. Think framedrops , or framerates above your screen refresh rate (60fps). It isn't a problem with a Pong game, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by JapanFred
Quote:
Original post by stein
Also, consider moving your translation code (pos += velocity) into a seperate Update() function or similar :)
Imagine the case where you'd want to render the same 'object' more than once.


Might be me being silly, but why would i want to render 1 object in 2 places?

Sorry if i'm missing something very simple!


Its common to separate the texture and geometry of a model from the logical existence of an entity/object.

So for example, you have two paddles called player1 and player2. In your case you might be using the same geometry and textures but loading them twice. This isn't much overhead for a small sample/demo like yours, but if you've got 256 of them you don't want to be loading 256 copies of the texture and geometry into memory. Instead you create just one "object" and load its texture once. Then your entities reference that single "object" either via pointer, or an index into the array of loaded "objects" or whatever you want to do to manage them.

You save on having to load things repeatedly and you save on memory usage by just doing things once and referring to that instance.

Andy

Share this post


Link to post
Share on other sites
Yea, you'll probably do just fine with this pong thing... It was ment for good practice :)
One scenario may be a game with more than one viewport (both showing your object) - i.e. a splitscreen game etc.

Share this post


Link to post
Share on other sites
Quote:
Original post by NineYearCycle
Its common to separate the texture and geometry of a model from the logical existence of an entity/object.

So for example, you have two paddles called player1 and player2. In your case you might be using the same geometry and textures but loading them twice. This isn't much overhead for a small sample/demo like yours, but if you've got 256 of them you don't want to be loading 256 copies of the texture and geometry into memory. Instead you create just one "object" and load its texture once. Then your entities reference that single "object" either via pointer, or an index into the array of loaded "objects" or whatever you want to do to manage them.

You save on having to load things repeatedly and you save on memory usage by just doing things once and referring to that instance.

Andy


Quote:
Original post by stein
Yea, you'll probably do just fine with this pong thing... It was ment for good practice :)
One scenario may be a game with more than one viewport (both showing your object) - i.e. a splitscreen game etc.


Both explained that perfectly, made much more sense!

Thanks guys!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!