Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


sunk

Member Since 02 Oct 2011
Offline Last Active Feb 16 2015 04:29 PM

Topics I've Started

How to correctly inject and share an object to multiple other objects

12 February 2015 - 12:42 PM

Hi, so I have a simple question in terms of dependency injection with c++. Say I have 3 classes A, B and C. If both B and C have a dependency on A, what is the best way to inject and share A with both B and C. I could have the following (pseudo code):

B(A& a) : uniqueAptr(&a){}

C(A& a) : uniqueAptr(&a){}

Where uniqueAptr is a unique_ptr (std::unique_ptr<A> uniqueAptr) member variable and then intialize B and C like so:

A aObject;
B bObject(aObject);
C cObject(aObject);

But wouldn't that cause both bObject and cObject object to delete the pointer to aObject effectively trying to delete aObject twice (which would cause an error). Would a shared_ptr make most sense in this case? I've read a few resources online saying that shared_ptr is not all that great to use for exampple http://programmers.stackexchange.com/questions/133302/stdshared-ptr-as-a-last-resort . So what's the preferred way of doing something like the above? This is a simple example, but in reality you would have other pointer member variable and so on.


SFML pushGLStates/popGLStates causing opengl texture to disappear

03 January 2015 - 08:35 AM

Hi, 

 

I'm trying to draw some opengl textures after some SFML draw calls. from what I understand I need to call  pushGLStates before and draw calls and then popGLStates after any draw calls. Then I should be safe to call my opengl code. However, in doing so, opengl stops drawing textures on the screen. I'm pretty confused about what might be causing the problems. I've attached an image showing the problem (the texutre is displayed on the left when I comment out the calls to  pushGLStates/popGLStates). Any ideas on what be causing the problem? Even if I comment out all sfml drawing code, I still see the same problem.

 

Here's my games init code which basically runs a bunch of opengl code.

void Game::init()
{
	glDisable(GL_LIGHTING);

	// Configure the viewport (the same size as the window)
	glViewport(0, 0, m_mainRenderWindow->getSize().x,
			m_mainRenderWindow->getSize().y);

	glMatrixMode(GL_PROJECTION);
	glOrtho(0, 1280, 800, 0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);

	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);                  // Depth Buffer Setup
	glEnable(GL_DEPTH_TEST);             // Enables Depth Testing
	glDepthFunc(GL_LEQUAL);

}

Here's my render function:

void Game::render() {
	m_mainRenderWindow->setActive(true);
	//TODO sfml stuff here
	m_mainRenderWindow->clear(sf::Color(50, 50, 50));
	m_mainRenderWindow->pushGLStates();
	m_mainRenderWindow->draw(m_cameraSystem);
	m_mainRenderWindow->draw(m_mapLoader);
	m_mainRenderWindow->draw(m_mouseSplitterSystem);
	m_fixedTimeStepSystem.drawDebug();
	m_mainRenderWindow->popGLStates();

	//TODO draw opengl stuff here
	glClear(GL_DEPTH_BUFFER_BIT);
	m_openglTextureRenderer.render();

	m_mainRenderWindow->display();

}

m_openglTextureRenderer is an instance of OpenGLTextureRenderer, which draws a bunch of textures to the screen, here's the render code for that:

void OpenGLTextureRenderer::OpenGLTextureRendererImpl::render(std::vector<anax::Entity>& entities) {
	const float M2P = 30.0f;
	for (auto entity : entities) {
		if(!entity.isActivated() || !entity.isValid()) continue;

		auto& texCoordsComp = entity.getComponent<Texcoords>();
		auto& physicsComp = entity.getComponent<PhysicsComponent>();
		auto& texCoordsVec = texCoordsComp.textCoords;

		if(texCoordsVec.size() <= 0)
			continue;

		b2Body* body = physicsComp.physicsBody;

		glBindTexture(GL_TEXTURE_2D, texCoordsComp.texture);
		b2PolygonShape* shape =
				((b2PolygonShape*) body->GetFixtureList()->GetShape());
		std::vector<b2Vec2> points(shape->GetVertexCount());
		for (int i = 0; i < shape->GetVertexCount(); i++) {
			points[i] = (shape)->GetVertex(i);
		}

		glPushMatrix();

	       b2Vec2 center = (body->GetPosition());

		float angle = body->GetAngle();
		glTranslatef(static_cast<float>(floor(center.x * M2P)), static_cast<float>(floor(center.y * M2P)), 0.0f);
		glRotatef(angle * 180.0 / M_PI, 0, 0, 1);

		glBegin(GL_POLYGON); //begin drawing of polygon
		for (int i = 0; i < shape->GetVertexCount(); i++) {
			glTexCoord2d(texCoordsVec[i].x, texCoordsVec[i].y);
			glVertex2f(floor(points[i].x * M2P), floor(points[i].y * M2P));
		}
		glEnd(); //end drawing of polygon
		glPopMatrix();
	}
}

Setting up a camera with SFML and raw opengl calls

30 September 2014 - 02:17 PM

Hi,
 
I'm having some trouble with using an sf::VIew as a camera when mixing raw opengl calls with SFML. For example, here in my code I'm mixing opengl calls with SFML: https://github.com/SundeepK/Clone/blob/wip/add_splittable_enviroment/src/Game.cpp#L58. Everything drawn by SFML is correct but the textures drawn manually using opengl calls follow the camera when the player moves, rather than staying put. Here's an example image: http://i.imgur.com/umME791.png?1 . When I move, you can see that the wooden texture follows rather than staying where it's box2d body is.
 
What would be the general way of handling a camera in this scenario? Would I need to write a custom camera class?

Opengl - How to texture polygons that may not be quads

29 June 2014 - 11:36 AM

Hi, I'm currently using Box2D, Opengl and SFML (still pretty new to this stuff) to build a splitting engine where the user can use their mouse to split shapes into smaller ones. I'm having a hard time actually figuring out how the split shapes would be textured after they have been sliced. For example, say I have a quad which has been textured correctly. I slice the square on the top right causing a new triangle and a 5 sided polygon to be created. How would the 2 new shapes be textured after this? I've only seen examples of quads and triangles being textured in opengl and subrecting a texture in SFML won't work since it only supports rects. Any thoughts? Thanks.


Fixed timestep with box2d and SFML

08 June 2014 - 09:22 AM

Hi, I've tried to implement a simple demo using box2d and SFML (a simple moving box). I've managed to implement a fixed timestep to the one described here http://www.unagames.com/blog/daniele/2010/06/fixed-time-step-implementation-box2d . The only problem is that moving my simple square around, it seems to become slightly  blurred and stretched. This maybe because its moving to fast (since it doesn't happen when moving slowly). I've ported this code to SDL as well and it seems to give similar results and I can't put my finger on why this is happening. I'm working on ubuntu but I've made a visual studios  project for windows as well showing the problem:

 

Linux (executable) https://www.dropbox.com/s/dzf4f8w6f6fl1o0/FixedTimeStepTest.rar

WIndows (visual studios project) https://www.dropbox.com/s/ghazj8tqyppk8v9/sfml%20test.zip

 

Should work, but let me know if you have issues (mostly likely need to have box2d and sfml install if your running on linux and running on windows you'll need to point to sfml directory for the include directories).

 

The actual implementation of the fixed timestep can be found below:

void B2DWorld::update(float dt, ActionController<std::string>& actionController){
    m_fixedTimestepAccumulator += dt;
    const int steps = floor(m_fixedTimestepAccumulator / FIXED_TIMESTEP);

    if (steps > 0)
	{
		m_fixedTimestepAccumulator -= steps * FIXED_TIMESTEP;
	}

	assertAccumilation();
    m_fixedTimestepAccumulatorRatio = m_fixedTimestepAccumulator / FIXED_TIMESTEP;

    const int clampedSteps = std::min(steps, MAX_STEPS);
	for (int i = 0; i < clampedSteps; ++ i)
	{
		resetStates();
		actionController.triggerCallbacks(m_fixedTimestepAccumulatorRatio);
		step(FIXED_TIMESTEP);
	}

	interpolateStates();
	m_world.DrawDebugData();

}

void B2DWorld::step(float dt){
    m_world.Step(dt, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
}

void B2DWorld::interpolateStates(){

	const float oneMinusRatio = 1.0f - m_fixedTimestepAccumulatorRatio;

	for (b2Body * b = m_world.GetBodyList (); b != NULL; b = b->GetNext ())
	{
		if (b->GetType () == b2_staticBody)
		{
			continue;
		}

		PhysicsComponent *c   = (PhysicsComponent*) b->GetUserData();
		c->smoothedPosition = m_fixedTimestepAccumulatorRatio * b->GetPosition () + oneMinusRatio * c->previousPosition;
		c->smoothedAngle = floor (m_fixedTimestepAccumulatorRatio * b->GetAngle () + oneMinusRatio * c->previousAngle);
	}

}

void B2DWorld::assertAccumilation(){
	assert (
		"Accumulator must have a value lesser than the fixed time step" &&
		m_fixedTimestepAccumulator < FIXED_TIMESTEP + FLT_EPSILON
	);
}

void B2DWorld::resetStates(){
for (b2Body * b = m_world.GetBodyList (); b != NULL; b = b->GetNext ())
	{
		if (b->GetType () == b2_staticBody)
		{
			continue;
		}
 		PhysicsComponent *c   = (PhysicsComponent*) b->GetUserData();
		c->smoothedPosition = c->previousPosition = b->GetPosition ();
		c->smoothedAngle = c->previousAngle = b->GetAngle ();
	}
}

The whole project is here: https://github.com/SundeepK/Clone/tree/wip/fix_time_step

 

After a bit of playing around, it looks like it maybe the way opengl renders things or the way its being setup. Any idea's on what maybe causing this issue? Thanks.


PARTNERS