Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML2 deriving the Drawable class  (Read 1515 times)

0 Members and 1 Guest are viewing this topic.

Andy

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML2 deriving the Drawable class
« on: April 19, 2012, 05:02:59 am »
I'm probably going to get shot down for this (given my level of ignorance), but I'll ask away.

I'm trying to derive the drawable class with an object that makes calls to OpenGL directly.
And I'm trying to render a real basic sprite (for starters).

draw function definition
Code: [Select]
void BaseSprite::draw(sf::RenderTarget & target, sf::RenderStates states) const
{
glEnable(GL_TEXTURE_2D);
// Set the primitive color to white
glColor4f(1.f, 1.f, 1.f, 1.f);
// Bind the texture
glBindTexture(GL_TEXTURE_2D, myTexture->id());

glPushMatrix();  // Save modelview matrix

acl::PointF center = myBounds.center();

glTranslatef(center.x, center.y, 0.f);
glRotatef(0.f, 0.f, 0.f, 0.f);
glTranslatef(myBounds.x - center.x, myBounds.y - center.y, 0.f);

glBegin(GL_QUADS);
glTexCoord2f(0.f              , myTextureBounds.h);
glVertex2f  (0.f              , 0.f              );
glTexCoord2f(myTextureBounds.w, myTextureBounds.h);
glVertex2f  (myBounds.w       , 0.f              );
glTexCoord2f(myTextureBounds.w, 0.f              );
glVertex2f  (myBounds.w       , myBounds.h       );
glTexCoord2f(0.f              , 0.f              );
glVertex2f  (0.f              , myBounds.h       );
glEnd();

glPopMatrix();
}

Start up, which just sets up OpenGL
Code: [Select]
Graphics::instance().enable2d();

Main Loop code:
Code: [Select]
app.draw(txt); // a test sf::Text object
app.pushGLStates();
app.draw(bs); // which is a BaseSprite object
app.popGLStates();

I'm not sure what I'm missing (do I need setup for rendering something this? I'm not sure what to do). I do see it (the BaseSprite) flashing but then disappearing afterwords.

Is anyone willing to suggest anything?
Please don't angry with me, if this post is undeserving just tell me and I'll move on (eventually, hopefully).

Thanks for tips

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML2 deriving the Drawable class
« Reply #1 on: April 19, 2012, 08:10:45 am »
pushGlStates and popGlStates must surround SFML draw calls, not your own calls that mix SFML and OpenGL.

This means that it is very hard to insert OpenGL stuff into a sf::Drawable. I don't even know if there's an easy way to do it with the new API -- why don't you use the new SFML 2 stuff, like sf::Vertex and sf::VertexArray?
Laurent Gomila - SFML developer

 

anything