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.


Messages - MrGarrison

Pages: [1]
1
Window / Re: waitEvent - loop.
« on: June 26, 2023, 01:00:35 pm »
I don't expect changing just pollEvent for waitEvent directly to actually have any noticeable effect in that code; it should perform - visually - the same.


That's what i have done at first - just made it "look" like it is working for basic render.

Quote
Note, of course, that the loop doesn't run unless events are present so updates in that loop would not change until an event happens.

Yes, i understand that, that's why i have whole conflict - it won't be good/complete solution because of that.

@eXpl0it3r

The first example that you have posted here is something that i have chosen in this version, ofcourse, without this coloring stuff that demonstrates whole problem - because that would expose problem (i hope it won't be visible at the first glance) ;D

The second one is something that is a bit familiar with codesnippet that i have posted in other topic ( but yours is more smooth, which can be useful for me if it needs :D ).

And you are right - it is just overcomplicated pollEvent that is not 100% safe, but as you said - we're using waitEvent, woohoo !  ;D

I hope that it won't be necessary to use second aproach, since it will be hard to embed that into basic tutorial/presentation about using SFML... We will see.

Thank you guys for fast reponses, time, and effort that you've put in helping me. I really appreciate it !

2
Window / waitEvent - loop.
« on: June 23, 2023, 10:55:14 am »
Is it possible to make simple example, that is something like :

#include <iostream>
#include <SFML\Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(300, 300), "SFML Works");

        sf::RectangleShape shape(sf::Vector2f(150,150));

        shape.setFillColor(sf::Color::Blue);

        while (window.isOpen()) {

                sf::Event event;
                while (window.pollEvent(event)) {

                        if (event.type == sf::Event::Closed) {
                                window.close();
                        }

                }

                window.clear();
                window.draw(shape);
                window.display();
        }

        return 0;
}

To be done using waitEvent in gameloop instead of pollEvent, and make this basic example work?

3
General / Re: Events - help.
« on: June 23, 2023, 10:20:14 am »
Thank you for fast and deep response.

Well, it was not mine desire to be thread running fully, but it was the only stuff that i came up with so i can make game loop to be without pollEvent - just waitEvent ...

I saw your snippet, so - simply i have stayed on mine own, but with that significant change - without locking during waitEvent, and animation works fine.

On the other side - i know how setFramerateLimit works, but it seems that request side is kind a hard to work with - so i'll see if i am able to stick with setFramerateLimit only, or i'll need to use sleep directly.

Quote
I'm not sure how thread-safe sf::Window is in itself. Maybe at least waitEvent is thread-safe; the documentation suggests this approach anyway.

I have found this old topic, so i dunno if some new problems can occur, since the finale of that topic from 2017 was:

Quote
I added some labels but I'm not really sure where we ended up with the discussion.

SFML isn't thread-safe
waitEvent() and close() in its current state can't be used in a multi-threaded environment
SFML uses a "custom" waitEvent loop
Do we do something about this or do we just mark it as "won't fix"?

Whole story is here:

https://github.com/SFML/SFML/issues/1184


4
General / Re: Events - help.
« on: June 23, 2023, 12:45:42 am »
So i'll update this topic.

It seems that request is to make game loop to be fully depend on WaitEvent, not PollEvent.

I dont know if it is the best approach, but mine presentation wouldn't even be considered for review if i don't "fix it", because it had been sad - that "busy wait pollEvent" method is not 21st century way of programming (wtf, i know but ... it is what it is ).

I saw some implementation to make it at least to "look like" some kind of WaitEvent driven code, so, it looks like this:

Quote
#include <chrono>
#include "pch.h"
#include <iostream>
#include <mutex>
#include <thread>
#include <memory>
#include <SFML/Graphics.hpp>

std::mutex m;
sf::Sprite sprite;
bool quit = false;

void draw(sf::RenderWindow *window) {
   unsigned i = 0;
   while (!quit) {
      sprite.setPosition((i % 10) * 30, (i / 10) * 30);
      i++;
      if (i > 100) {
         i = 0;
      }
      m.lock();
      std::cout << "Rendering." << std::endl;
      window->setActive(true);
      window->clear();
      window->draw(sprite);
      window->display();
      m.unlock();
      std::this_thread::sleep_for(std::chrono::milliseconds(100));
   }
}

int main() {
   sf::Texture texture;
   texture.loadFromFile("Data/kenny.png");
   sprite = sf::Sprite(texture);
   sf::RenderWindow window(sf::VideoMode(400, 200), "test");
   window.setFramerateLimit(300);

   std::thread rendering(draw, &window);
   window.setActive(false);
   
   sf::Event event;
   while (true) {
      m.lock();
      std::cout << "Waiting for an event…" << std::endl;
      bool ok = window.waitEvent(event);
      std::cout << "Event received." << std::endl;
      m.unlock();
      if (ok) {
         if (event.type == sf::Event::Closed)
            break;
      }
      else
         break;
   }
   quit = true;

   rendering.join();
}

So, outcome would be moving of little Kenny on canvas - problem is like in one of previous subjects - he is moving only when there are events (halted without it), and + it is moving slow, in not regular steps that are defined by sleep method.

I know that first halted stuff could be solved with better implementation of setActive(true/false), but where i should put it in this code to make it work ?

Can somebody help me to solve these problems?

5
General / Re: Events - help.
« on: June 16, 2023, 02:57:17 am »
Well, as i could understand, something that is not based on busy-wait loop, like normal gameloops.

Loop that is event driven ( even if you have actions that are automatic like enemies in game - everything should be event driven, not classic - poll event - update, render etc ... ).

SFML's API isn't event driven, even using WaitForEvent isn't really event driven.

Well i know that, but i don't have a right understanding from the other side about that...

Quote
But you can design an API around the SFML API, that pushes the SFML events into an event bus or similar, on top of which you then build your "event-driven" application.

Anybody knows how can i do that ?
I need simple example if it is possible.

6
General / Events - help.
« on: June 15, 2023, 03:44:34 pm »
Hi everybody,

First, i would love to say that i use to develop a lot of stuff using SFML a few years ago, all following docs from your page, but now i have a problem that i don't know how to resolve.

I was making kind of presentation/tutorial of SFML, and now i am in a bit of problem because i have request to make a one example of using SFML with full event driven game loop (not optimized "busy wait" loop with controlling number of FPS). As a saw, all stuff like - setFrameLimit, vsync, or manually customized with timers - are giving good performance with small CPU usage, but still, request is - make one example with full event driven loop ( wait for event - i know that there is - WaitForEvent function, but i was using it only in some situations in combination with PollEvent, like pauses etc ).

Is it possible for someone of you to help me, or make basic example with just drawing screen or something trivial on it like square ?

Thank you for reading.


Pages: [1]