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

Author Topic: A Thread and RenderWindow issue - My poor Super Mario  (Read 2919 times)

0 Members and 1 Guest are viewing this topic.

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
A Thread and RenderWindow issue - My poor Super Mario
« on: November 08, 2013, 01:48:10 pm »
Hello Ladies and Guys from SFML

I'm extending my Super Mario game, and decided to introduce multithreading to increase its performance

Before, I had something like this in my main loop:

while (true)
{
     DrawBackground();

     DrawLevelPlace();

     DrawSuperMario();

     DrawEnemies();

     DrawForeground();

     delay();
}
 

Now I'm changing each function into the loop by

void DrawLevelPlace()
{
      while()
      {
           the same than before
      }
}

and remove it from the main thread main loop and


Thread thread1 = new Thread(new ThreadStart(DrawLevelPlace));

thread1.Start();

 


********
It produces a console message: "Failed to activate window's context" repeated several times

I admit I'm not an expert in multithreading, just know the basics, but I learn fast

Would you help me?

Thanks

this is the last version of the program that I upload to dropbox

https://www.dropbox.com/s/2v13d30js73bhsb/My%20New%20Super%20Mario%20Bros.rar



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #1 on: November 08, 2013, 01:57:34 pm »
Two things:

1. The answer is written in the tutorials (see "OpenGL" and/or "Drawing").

2. I think you have no idea of what you're doing. You want to improve your program but you're making it slower and much less maintainable. Forget about multithreading, or read/practice more to know how to use it to actually improve things ;)
Laurent Gomila - SFML developer

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #2 on: November 08, 2013, 02:31:05 pm »
Thank you, Laurent

Could you give me the link, web address, or how to reach the place where the answer is written?
I googled "sfml tutorals" and didn't find it

Actually, not only I want to improve my game, also I want to practise and learn multithreading, I think it is the greatest thing of .NET

I don't know if you saw the game in the link, it isn't multithreaded, it's the last update I uploaded ... maybe it's slow cos it isn't multithreaded ...
Don't worry, I'll learn well, I love programming and games, and my brain is almost the only thing I have

Thanks again

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #3 on: November 08, 2013, 04:17:55 pm »
Quote
Could you give me the link, web address, or how to reach the place where the answer is written?
I googled "sfml tutorals" and didn't find it
SFML tutorials are there: http://www.sfml-dev.org/tutorials/2.1/

Quote
Actually, not only I want to improve my game, also I want to practise and learn multithreading, I think it is the greatest thing of .NET
Fair enough :P
But keep in mind that you won't improve anything. You only have one physical graphics card, so what do you think you'll gain from drawing from multiple threads? And you'll also discover that drawing to the same target from multiple threads is not easy, because of lower-level constraints.

Quote
I don't know if you saw the game in the link, it isn't multithreaded, it's the last update I uploaded ... maybe it's slow cos it isn't multithreaded ...
Don't guess. Profile your code.
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #4 on: November 08, 2013, 04:26:02 pm »
I must get really lucky when using Google then... ::)
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

TheRabbit

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #5 on: November 08, 2013, 06:04:46 pm »
It's probably only because you spelled tutorial wrong  ;D

Tigre, if you do multi-threading the way you explained it in your main post, you'll end up with several race conditions and probably some bad synchronization problems. I'd strongly suggest reading up some more on multi-threading once you get it running (specifically on the two topics I just mentioned).

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #6 on: November 08, 2013, 08:35:30 pm »
Thank you all, Guys (and Ladies)

I was lucky to find the answer considering I'm very inept on searching, plus I'm lazy

.... Not so lucky as binary1248, but I stand .... (1048576 hi hi hi) ... even that I spell correctly, in general

Laurent, I can't compare with your knowledges, but I think that what you gain with multithreading concerns more to processing code, that is, not when the cpu draws to the screen, but when it thinks and does calculations, ... and in my poor Super Mario that is quite important part (trajectories, movements, attacks, scrolls, etc ...)

TheRabbit, you're right, troubles exist ... I'll get through them, ... and these are not the worst, in any way or another I'll learn this interesting issue

And, actually I applied what I read from the tutorial and now the application already runs vissibly faster, though there appeared a few problems, I'm working on that ... but it works, now I see again the graphics

Thanks to you  :)  ;)

Before you remember me, I'll post the link to my poor Mario very very improved  ;D  8)
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #7 on: November 08, 2013, 09:55:25 pm »
Quote
I think that what you gain with multithreading concerns more to processing code, that is, not when the cpu draws to the screen, but when it thinks and does calculations, ... and in my poor Super Mario that is quite important part (trajectories, movements, attacks, scrolls, etc ...)
If trajectories, movements, attacks, scrolls, etc ... are all done in the Draw functions, then they are very badly named. And I still assume that they contain the drawing code, which should not be scattered in multiple threads.
Laurent Gomila - SFML developer

cuddlyogre

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: A Thread and RenderWindow issue - My poor Super Mario
« Reply #8 on: November 08, 2013, 10:46:38 pm »
I learned recently just how much of a fool's errand multithreading really is, unless you really know what you're doing. I would recommend looking into how to implement a quadtree to limit how much your game has to do. It is far less maddening to deal with.