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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - T.T.H.

Pages: [1]
1
General / SFML 2.0 multi thread rendering demo
« on: September 11, 2012, 01:53:26 pm »
I've written a short, fully functional and well documented SFML 2.0 multi thread rendering demo (see attachment) and have a couple of questions now:

Am I doing everything "right", especially regarding thread-safety and performance?

There still is some visible stretching of the viewport while resizing the window - is there any way to prevent that?

The documentation says never to use setVerticalSyncEnabled and setFramerateLimit at the same time but my demo actually has a lower CPU load (about 60% of one core) when using both setVerticalSyncEnabled(true) and setFramerateLimit(60) compared to a higher CPU load (about 100% of one core) when using only setVerticalSyncEnabled(true) - do I trust the documentation or my own tests now?

P.S.: the demo was made with Visual Studio 2010 on Windows 7 and the used font file and image file can simply be exchanged with others.

[attachment deleted by admin]

2
Graphics / problem with render images and threads
« on: July 12, 2010, 03:54:07 pm »
I've ran into the following problem: when a render image is created in one thread it cannot be modified anymore in another thread. The render image is still there and it can e.x. be saved, but whatever modification (clear, draw, etc.) I throw at it the image won't change. Please note that I already do allocate an sf::Context in every thread.

The following minimalistic code example demonstrates the problem:
Code: [Select]
// include SFML
#include <SFML/Graphics.hpp>

// include STD
#include <iostream>
#include <fstream>
#include <sstream>

// include threads
#include <windows.h>
#include <process.h>

// global variable (for simplicity)
sf::RenderImage* g_pRenderImage = NULL;

// thread function
unsigned int _stdcall ThreadFunction(void* pParam)
{
  // create SFML context in every thread
  sf::Context MyContext;

  // get and output thread index
  int* pThreadIndex = (int*) pParam;
  std::cout << "thread " << *pThreadIndex << " started" << std::endl;

  // create or reuse render image
  if (g_pRenderImage == NULL) {
    std::cout << "  creating render image" << std::endl;
    g_pRenderImage = new sf::RenderImage;
    g_pRenderImage->Create(300, 100);
  } else {
    std::cout << "  reusing render image" << std::endl;
  }

  // loop
  int ImageIndex;
  for (ImageIndex = 0; ImageIndex < 2; ++ImageIndex) {

    // clear render image
    g_pRenderImage->Clear(sf::Color(200, 200, 200));

    // create label
    std::stringstream Label;
    Label << "thread" << *pThreadIndex << "image" << ImageIndex;

    // create SFML text from label
    sf::Text MyText(Label.str());
    MyText.SetColor(sf::Color(255, 0, 0));
    MyText.SetPosition(20.0f, 20.0f);

    // draw SFML text
    g_pRenderImage->Draw(MyText);

    // display (finish) render image
    g_pRenderImage->Display();

    // save render image
    std::cout << "  saving image '" << Label.str() << ".png'" << std::endl;
    g_pRenderImage->GetImage().SaveToFile(Label.str() + ".png");
  }

  std::cout << "  done" << std::endl;
  return 0;
}

// main function
int main(int argc, char** argv)
{
  // create SFML context in main thread
  sf::Context MyContext;

  // loop over threads
  int ThreadIndex;
  for (ThreadIndex = 0; ThreadIndex < 2; ++ThreadIndex) {

    // create thread
    unsigned int TempThreadID = 0;
    HANDLE TempThreadHandle = (HANDLE) _beginthreadex(
        NULL,           // no security attributes
        0,              // use default stack size
        ThreadFunction, // thread function
        &ThreadIndex,   // argument to thread function
        0,              // use default creation flags
        &TempThreadID); // returns the thread identifier

    // handle thread creation error
    if (TempThreadHandle == NULL) {
      std::cout << "failed to create thread " << ThreadIndex << std::endl;
      return 1;
    }

    // wait till thread finishes
    WaitForSingleObject(TempThreadHandle, INFINITE);
  }

  return 0;
}

This code was made with Visual C++ 2003 .NET on Windows XP 32 Bit using https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2 revision 1523.

After running this code there will be 4 image files:

"thread0image0.png" containing the text "thread0image0" which is correct.
"thread0image1.png" containing the text "thread0image1" which is correct.
"thread1image0.png" containing the text "thread0image1" which is wrong.
"thread1image1.png" containing the text "thread0image1" which is wrong.

Based on my actual, big project using multiple threads seems to work for render windows, but as the example above shows it does not work for render images.

I'd like to know whether I'm doing something wrong, whether this is intended/expected behaviour or whether this is a bug, thanks.

3
Graphics / sf::Image::Reset() private?
« on: February 11, 2010, 05:02:16 pm »
Is there any reason why sf::Image::Reset() is private?

I'd could make good use of it if it would be public and by having a (short) look at the code I did only see Reset() being called after an error.

4
Graphics / tiny piece of superfluous code in Shape.cpp
« on: February 09, 2010, 11:20:56 am »
Shape.cpp of SFML2 revision 1392:

Code: [Select]
Shape Shape::Rectangle(const Vector2f& p1, ...
{
    return Shape::Rectangle(p1.x, p1.y, ...
}

Shape Shape::Rectangle(float p1x, float p1y, ...
{
    // Create the shape's points
    Shape shape;
    shape.AddPoint(Vector2f(p1x, p1y), ...

Vector2f being converted to floats, floats being converted to Vector2f - seems a bit superfluous.

In the other functions (Shape::Line, Shape::Circle) it's different:
floats being converted to Vector2f, Vector2f being used - seems to make more sense.

Just a minor inconsistency I stumbled over...

5
Graphics / wobbling lines when render window size changes (SFML2)
« on: February 05, 2010, 03:05:13 pm »
I'm using branches/sfml2 revision 1369 and I noticed that when creating and drawing lines with sf::Shape::Line the final position of those lines seems to be dependent on the size of the render window. In addition there seem to be issues with the visibility of diagonal (angular/beveled/sloped?) lines when their thickness is too small.

When running my demo application (see next post for code) you'll notice the following:
- the window size randomly changes all the time (intended)
- the blue rectangle always remains on the very same spot (intended)
- the red horizontal and vertical lines are "wobbling" despite the fact that their positions are not changed in the code (bad)
- the red diagonal line is not visible at all (bad)

As I can't imagine any use for positions of lines being dependent on the render window size I'd really appreciate if at least the first issue will get fixed.

Please note that the second issue was already mentioned here:
http://www.sfml-dev.org/forum/viewtopic.php?t=1828

6
General / SFML2 and Visual C++ 2003 - possible!
« on: November 04, 2009, 11:41:49 am »
The year 2009, the whole world is using modern compilers. Well, not entirely? One small programmer still holds out against the adherence to standards, thanks to a pretty old compiler made by Microsoft.

At work I'm (still) using Visual C++ .NET 2003 (compiler version 7.1.6030, "Professional" edition) - for good reasons and with many headaches.

Nevertheless I think about using SFML at work, too, and today simply tried it out (updates of 11. June 2010 in red):
[list=1]
  • I got SFML2 ( https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2 revision 1258 )
  • I got Visual Studio Project Converter ( http://sourceforge.net/projects/vspc/ version 0.9.3 )
  •  I converted "build\vc2008\SMFL.sln" from VS2008 to VS2003 project format with that tool
  • I opened the converted file "SFML.sln" with Visual C++ 2003
  • I manually removed the compile option "/MP" from "Configuration Properties", "C++", "Command Line"
  • I added the compile option "/Zc:wchar_t" to "Configuration Properties", "C++", "Command Line" ( based on hints in this thread at the Ogre forums)
  • I added the following code into Image.cpp between the header includes and the first namespace::sf definition:
    Code: [Select]
    ////////////////////////////////////////////////////////////
    // HACK FOR VS2003
    // http://software.intel.com/en-us/forums/showthread.php?t=60003
    extern "C" {
    long _ftol( double );
    long _ftol2_sse( double dblSource ) { return _ftol( dblSource ); }
    }

  • I set the project "opengl" as startup project
  • I compiled the "opengl" project in the modes "Release static" and "Debug static" which then automatically built the dependencies "sfml-graphics", "sfml-window", "sfml-system" and "sfml-main"
  • I ignored a huge list of compiler warnings
  • I started "opengl.exe" and "opengl-d.exe" from the command line
  • I happily jumped around seeing the rotating cube in front of the green landscape[/list:o]
    I have no clue how this experiment will go on further but I just wanted to let you know that as of now it is still possible to compile SFML with Visual C++ 2003 (at least the graphics part which is of interest for me).

    As of 11. June 2010 this works with https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2 revision 1523 and was tested with "Release static" and "Debug static".

7
Window / [solved] Error "Failed to activate the window's context
« on: October 28, 2009, 09:00:49 pm »
Today I updated sfml/branches/sfml2 from revision 1197 to revision 1250 and now my application outputs "Failed to activate the window's context" to stderr every draw call. It comes from the following function in Window.cpp:

Code: [Select]
bool Window::SetActive(bool active) const
{
    if (myContext)
    {
        if (myContext->SetActive(active))
        {
            return true;
        }
        else
        {
            std::cerr << "Failed to activate the window's context" << std::endl;
            return false;
        }
    }
    else
    {
        return false;
    }
}


Call Stack is:
Code: [Select]
MyApp.exe!sf::Window::SetActive(bool active=false)  Line 330 C++
MyApp.exe!sf::RenderWindow::Activate(bool active=false)  Line 81 C++
MyApp.exe!sf::RenderTarget::Flush()  Line 106 + 0x11 bytes C++
MyApp.exe!sf::RenderWindow::OnDisplay()  Line 119 C++
MyApp.exe!sf::Window::Display()  Line 345 + 0xf bytes C++
MyApp.exe!CScreen::display()  Line 187 C++
MyApp.exe!CGraphicEngine::render(bool & rHalt=false, bool & rError=false)  Line 703 C++
MyApp.exe!CGraphicThread::operator()()  Line 69 C++
MyApp.exe!boost::detail::thread_data<CGraphicThread>::run()  Line 57 C++
MyApp.exe!boost::`anonymous namespace'::thread_start_function(void * param=0x00b65c30)  Line 168 C++
msvcr90d.dll!6815dfd3()
[Frames below may be incorrect and/or missing, no symbols loaded for msvcr90d.dll]
msvcr90d.dll!6815df69()
kernel32.dll!76aceccb()
ntdll.dll!7790d24d()
ntdll.dll!7790d45f()


Both SFML and my project are made with VC++ 2008 Express  and I'm using a statically linked version of SFML. Operating System is Windows Vista Home Premium 64 Bit. Graphics card is an ATI Radeon HD4870 with Catalyst 9.2.

Before I start debugging maybe somebody has a hint?!


UPDATE: solved, see here

8
Feature requests / better comment for sf::RenderWindow::SetView
« on: June 12, 2008, 06:38:45 pm »
I think the function sf::RenderWindow::SetView needs a better comment including a big, phat warning that the instance of the sf::View you pass as paramater must not be destroyed for the whole lifetime of the render window, because not the values of the passed view are stored but only a pointer to that view.

When I tinkered around the first time with SetView I got a big crash due to access of invalid memory because I used it like this...
Code: [Select]
MyRenderWindow(sf::View(0, 0, 800, 600));
...which was perfectly fine for all the other SFML functions I used so far.

9
SFML website / sourceforge and the forum
« on: February 07, 2008, 08:23:00 pm »
Since a couple of days (now it's 7. February 2008) when reading the forum...
...sometimes it loads veeeery slow
...sometimes I get a timeout
...often I only get a blank page (uber annoying)
...in that case I have to hit reload a couple of times to get something again

The SFML website ain't that fast either (subjective impression)

I am from Germany and my ping to sfml.sourceforge.net usually is around 190ms. I'm using Firefox.

Just for you to know. Relief my F5 key!

10
Graphics / strange behaviour when resizing window
« on: January 31, 2008, 09:50:43 pm »
Hi again

There is yet another thing which I don't understand yet: when I open a window and draw sprites into it everything looks fine. But when I resize the window the sprites get "scaled" somehow despite I didn't change their scale myself.

...and again in images:

Source image, 32 x 32 pixel, with colorful 1 pixel wide border and checkerboard within:



Screenshot after starting application, no resizing done:



Screenshot after resizing window, making it smaller:



Screenshot after resizing window, making it larger:



And finally my code:

Code: [Select]

// --- includes ---

// windows
#include <windows.h>

// SFML
#include "SFML/Graphics.hpp"

// STL
#include <iostream>

// --- defines ---

#define IMAGE_FILE "image_1x1.bmp"
const int SCREEN_WIDTH = 400;
const int SCREEN_HEIGHT = 400;
const int SPRITE_WIDTH_DEST = 32;
const int SPRITE_HEIGHT_DEST = 32;

// --- signal handler ---

BOOL WINAPI MyHandlerRoutine(DWORD dwCtrlType)
{
  std::cout << "exiting the hard way" << std::endl;
  exit(0);
  return TRUE;
}

// --- run ---

bool runSFML()
{
  // variables
  bool Looping(true);
  int X(0);
  int Y(0);
  sf::Event MyEvent;

  // render window
  sf::RenderWindow MyRenderWindow(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFML", sf::Style::Close | sf::Style::Resize);
  MyRenderWindow.OptimizeForNonOpenGL(true);
  MyRenderWindow.SetBackgroundColor(sf::Color(255, 0, 255));

  // image
  sf::Image MyImage;
  if (!MyImage.LoadFromFile(IMAGE_FILE))
  {
    std::cout << "failed to load image '" << IMAGE_FILE << "'" << std::endl;
    return false;
  }
  MyImage.SetSmooth(false);

  // sprite
  sf::Sprite MySprite(MyImage);
  MySprite.Scale((float) SPRITE_WIDTH_DEST / (float) MyImage.GetWidth(), (float) SPRITE_HEIGHT_DEST / (float) MyImage.GetHeight());

  // big loop
  while (Looping)
  {
    // loop over screen
    for (Y = 0; Y < (int) MyRenderWindow.GetHeight(); Y += SPRITE_HEIGHT_DEST)
    {
      for (X = 0; X < (int) MyRenderWindow.GetWidth(); X += SPRITE_WIDTH_DEST)
      {
        // draw sprite
        MySprite.SetPosition((float) X, (float) Y);
        MyRenderWindow.Draw(MySprite);
      }
    }

    // display screen
    MyRenderWindow.Display();

    // poll events
    while (MyRenderWindow.GetEvent(MyEvent))
    {
      // check whether window was closed
      if (MyEvent.Type == sf::Event::Closed)
      {
        Looping = false;
      }

      // check whether escape was pressed
      if ((MyEvent.Type == sf::Event::KeyReleased) && (MyEvent.Key.Code == sf::Key::Escape))
      {
        Looping = false;
      }

      // check whether resized
      if ((MyEvent.Type == sf::Event::Resized)) {

        // ### WHAT TO DO HERE ?!? ###
      }
    }
  }

  return true;
}

// --- main ---

int main(int argc, char *argv[])
{
  // add signal handler routine
  if (!SetConsoleCtrlHandler(MyHandlerRoutine, TRUE))
  {
    std::cout << "failed to add signal handler routine" << std::endl;
    exit(1);
  }

  // start
  std::cout << "started" << std::endl;

  // run SFML
  runSFML();

  // stopped
  std::cout << "stopped" << std::endl;

  return 0;
}


Using SFML revision 442 with Visual C++ 2005 Express under Windows 2000 including a Sapphire ATI Radeon 9800 Pro with ATI Catalyst 4.4.

11
Graphics / subpixel accuracy of sprite of image
« on: January 30, 2008, 10:57:17 pm »
Hi there

Since I am a nitpicker regarding pixel moving I stumbled upon a problem: when I create a sprite from an image and draw that sprite on the screen, the border pixel of the image are only half as big as the inner pixel of the image.

...and now with pictures:

Source image, 32 x 32 pixel, with colorful 1 pixel wide border and checkerboard within:



Screenshot, rendered with SFML, sprite scaled quite large and rendered four times around the center of the screen:



As you can see the colorful border pixel are only half as big as the black and white checkerboard pixel.

Relevant piece of code:
Code: [Select]

  // render window
  sf::RenderWindow MyRenderWindow(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "SFML", sf::Style::Close);
  MyRenderWindow.OptimizeForNonOpenGL(true);
  MyRenderWindow.SetBackgroundColor(sf::Color(255, 0, 255));

  // image
  sf::Image MyImage;
  MyImage.LoadFromFile(IMAGE_FILE);
  MyImage.SetSmooth(false);

  // sprite
  sf::Sprite MySprite(MyImage);
  MySprite.Scale(20.f, 20.f);

  // center of screen
  float CenterX = (float) MyRenderWindow.GetWidth()  / 2.0f;
  float CenterY = (float) MyRenderWindow.GetHeight() / 2.0f;

  // big loop
  while (Looping)
  {
    // draw top left sprite
    MySprite.SetPosition(CenterX - MySprite.GetWidth(), CenterY - MySprite.GetHeight());
    MyRenderWindow.Draw(MySprite);

    // draw top right sprite
    MySprite.SetPosition(CenterX                      , CenterY - MySprite.GetHeight());
    MyRenderWindow.Draw(MySprite);

    // draw bottom left sprite
    MySprite.SetPosition(CenterX - MySprite.GetWidth(), CenterY                       );
    MyRenderWindow.Draw(MySprite);

    // draw bottom right sprite
    MySprite.SetPosition(CenterX                      , CenterY                       );
    MyRenderWindow.Draw(MySprite);

    // display screen
    MyRenderWindow.Display();


I don't like that and I would like to change that. Unfortunately I'm not sure whether it's due to SFML or due to OpenGL or due to the fact that I am totally missing something. Please somebody enlighten me  :D

Pages: [1]