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

Author Topic: Drawing only part of a string?  (Read 3368 times)

0 Members and 1 Guest are viewing this topic.

Recruit0

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Drawing only part of a string?
« on: July 01, 2010, 05:04:16 am »
What would be the best way to draw only part of a string? Basically I want to draw whatever part of a string fits into a box.

I'm guessing I could use a sf::View to clip the string within a region on the screen. Anyone have example code? I'm writing from scratch atm.

EDIT

So far I have:

Code: [Select]
   String draw_text( text );
    const View clip_text( FloatRect( x, y, x + width, y + height ) );
    window.SetView( clip_text );
    window.Draw( draw_text );
    window.SetView( window.GetDefaultView() );

But this isn't the type of view I was thinking about (brief description: it "looks" like it's "scaling" the image). Simply modifying the string length doesn't cut it (the string could span multiple lines of variable width). I need to use a clipping function.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing only part of a string?
« Reply #1 on: July 01, 2010, 08:13:36 am »
This is not directly supported. In SFML 1.x, the simplest solution is to use OpenGL and its glScissor function (I think you can find examples on this forum).
Laurent Gomila - SFML developer

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
Drawing only part of a string?
« Reply #2 on: July 01, 2010, 11:29:12 am »
Hm.. perhaps on SFML 2.x draw it into a RenderImage and clip it manually?
Pluma - Plug-in Management Framework

Recruit0

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Drawing only part of a string?
« Reply #3 on: July 01, 2010, 11:04:52 pm »
I'm (re)writing a GUI library (cpGUI) for SFML-1.6. Development for SFML 2 won't start until we've finished the new API and SFML2 is released.

Is this functionality (directly) supported in SFML2? If not, I request it.

Quote
In SFML 1.x, the simplest solution is to use OpenGL and its glScissor function
I'm not sure this is feasible in my situation. cpGUI is designed to overlay on top of whatever the user sets up (i.e. the user has control over the window's creation/destruction) Unless it's safe to mix OpenGL commands with a SFML 2D window (i.e. without making it specifically an OpenGL context), which I'm assuming it's not.

Just glancing at the cpGUI reference code, it looks like he manually handled each line to make it draw the whole text box correctly... Seems to work but not an optimal solution.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing only part of a string?
« Reply #4 on: July 01, 2010, 11:13:08 pm »
Quote
Is this functionality (directly) supported in SFML2? If not, I request it.

It is planned for SFML 2.

Quote
I'm not sure this is feasible in my situation. cpGUI is designed to overlay on top of whatever the user sets up (i.e. the user has control over the window's creation/destruction) Unless it's safe to mix OpenGL commands with a SFML 2D window (i.e. without making it specifically an OpenGL context), which I'm assuming it's not.

Using glScissor on a SFML window is safe ;)
All SFML windows have an OpenGL context, by the way.
Laurent Gomila - SFML developer

Recruit0

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Drawing only part of a string?
« Reply #5 on: July 01, 2010, 11:20:27 pm »
Quote
All SFML windows have an OpenGL context, by the way.
I know but I thought it might have some special code inside that will break it if we start throwing OpenGL commands around without specifically making it an sf::Context (i.e. sf::RenderWindow was a cut down OpenGL context with certain limitations).

EDIT

^_^ It works. Thanks,

Code: [Select]
   glEnable( GL_SCISSOR_TEST );
    glScissor( x, window.GetHeight() - ( y + height ), width, height );
    window.Draw( draw_text );
    glDisable(GL_SCISSOR_TEST);


In case anyone else tries this, it requires linking against libGL (add -lGL to linker options).