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 - the__cows

Pages: [1]
1
Network / Re: Anyway to get download progress using SFML?
« on: February 06, 2016, 10:55:16 am »
With the HTTP class directly you can't do it. One way that might work is, if the server supports it, to do partial downloads (google it), which you also could use to make two threads downloading stuff. It's not trivial but should be doable.

Otherwise you'll have to fall back to TCP sockets.

Ah, thanks, makes sense. Honestly, I'd rather do more complicated programming with SFML then use a different library like libcurl & cpp-netlib; tried for days to get them working and nada.

Thanks :)

2
Network / Anyway to get download progress using SFML?
« on: February 05, 2016, 09:48:04 pm »
Hi,

I've been playing around with SFML's HTTP Client (downloading files, getting file sizes using HEAD etc), and I was wondering if there's anyway to get the amount of bytes that have currently been downloaded.

At first I just thought "well just stick the download in a new thread and keep checking the size of the file." However, I realised that you aren't writing to the file until everything has already downloaded... bit of an oversight for me haha.

Any ideas anyone?

Edit:
Download code:
int download(std::string host, std::string uri, std::string filename) {
        sf::Http http;
        http.setHost(host);

        sf::Http::Request request;
        request.setUri(uri);

        sf::Http::Response response = http.sendRequest(request);

        if (response.getStatus() == sf::Http::Response::Ok) {
                std::ofstream file(filename, std::ios::out | std::ios::binary);
                file << response.getBody();
                file.close();
                //std::cout << "Downloaded " << host << uri << " to " << filename << "!" << std::endl;
                return jb::ReturnValues::DOWNLOAD_SUCCESSFUL;
        }
        else{
                std::cout << "Error: " << response.getStatus() << std::endl;
                return jb::ReturnValues::DOWNLOAD_FAILED;
        }
}

Getting remote file size code:
int getRemoteFileSize(std::string host, std::string uri) {
        sf::Http http;
        http.setHost(host);

        sf::Http::Request request;
        request.setMethod(sf::Http::Request::Head);
        request.setUri(uri);

        sf::Http::Response response = http.sendRequest(request);

        if (response.getStatus() == sf::Http::Response::Ok) {
                return stoi(response.getField("Content-Length"));
        }
        else{
                return jb::ReturnValues::GET_REMOTE_FILE_SIZE_FAILED;
        }
       
}

(Mostly SFML's example code rn I know :P)

3
Just do the clear/draw/display dance every frame.
See also the big red box in http://www.sfml-dev.org/tutorials/2.3/graphics-draw.php
  • Don't count on your loop running exactly at 60 FPS
  • Framerate limits and VSync will not guarantee exact timing
  • Redraw everything every frame - clear, draw, and display each time
  • Implement a fixed time step to update your game objects
  • Read: http://gafferongames.com/game-physics/fix-your-timestep/
Draw every frame. Frame limit or syncronise using v-sync if required.
You can update as much as necessary (from zero to multiple times) each frame.

You should be drawing each frame for a number of reason but one to keep in mind is that the window is interacting with the OS as well and the OS may change some of what is currently visible.

p.s. Jesper and zsbzsb beat me to it  ;D

Thanks all! Much more helpful than the GameDev StackExchange haha. I was confused at first; drawing it when needed seemed fine in my current project, but when I was looking at different code samples involving many different libraries and languages, they all had the one thing in common - drawing every interval. This, and the realisation that making things in this way would make the game asynchronous (you can, for example, move a texture up slowly on the screen without, for example, affecting the player input (Think how the town names show up in the Pokemon games)) made me question whether my way was the best way to do things. Also, the three of you all saying the same thing proved it for sure, if that makes sense haha.

p.s. Jesper and zsbzsb beat me to it  ;D
Not a problem! Like I said before, the fact that you all said it solidifies it for me :)

Had a quick scan over it; complicated but doable. Thanks for the link - I'll implement it into my projects for sure :)

Thanks all! :)

4
So what's better?

In case the title is a bit confusing I mean:

1) Drawing every window update interval. For example, for a 60FPS window, every 17 milliseconds. For example:

int main() {
sf::RenderWindow window(sf::VideoMode(800, 608), "Example Window For Forums");
window.setFramerateLimit(60); //Update every 17 milliseconds for 60FPS
while(window.isOpen()) {
window.clear();
window.draw(sf::Sprite()); //Imagine we're drawing a RenderTexture here
window.display();
}
}
Or

2) Drawing when you need to (for example, when a sprite moves) and displaying it straight after. For example:

void func(sf::RenderWindow& window) {
window.clear();
window.draw(sf::Sprite()); //Whatever you're drawing
window.display();
}
Or is there a much better way?

5
Graphics / Animation for player sprite?
« on: July 10, 2015, 07:56:27 pm »
Hi,

So basically I'm making a small platformer game, and I have the textures etc setup for the sprite animation. What I want to know is, is there a better way of doing this? The code I am currently using is:


case RIGHT:
                if (globals.ggs == IN_GAME) {
                        sf::Clock animationClock;
                        sf::Time clockTimeTemp;
                        playerSprite.setPosition(playerSprite.getPosition().x + 10, playerSprite.getPosition().y);
                        while (true) {
                                clockTimeTemp = animationClock.getElapsedTime();
                                if (clockTimeTemp.asMilliseconds() >= 7.f) {
                                        break;
                                }
                        }

                        animationClock.restart();

                        playerSprite.setTexture(p1);
                        initGame();

                        while (true) {
                                clockTimeTemp = animationClock.getElapsedTime();
                                if (clockTimeTemp.asMilliseconds() >= 7.f) {
                                        break;
                                }
                        }

                        animationClock.restart();

                        playerSprite.setTexture(p2);
                        initGame();

                        while (true) {
                                clockTimeTemp = animationClock.getElapsedTime();
                                if (clockTimeTemp.asMilliseconds() >= 7.f) {
                                        break;
                                }
                        }

                        animationClock.restart();

                        playerSprite.setTexture(p3);
                        initGame();

                        while (true) {
                                clockTimeTemp = animationClock.getElapsedTime();
                                if (clockTimeTemp.asMilliseconds() >= 10.f) {
                                        break;
                                }
                        }

                        animationClock.restart();
                        initGame();
                }

                break;
 

Even though this works, other mechanics such as jumping haven't been implemented yet, and because of the while(true)'s, I'm worried this will cause things such as delays.

Any better ways?

Thanks,
Jay :)

6
Audio / Re: Best way to keep a music track running inside a thread?
« on: July 10, 2015, 07:54:27 pm »
Ahhhh thank you! :D

7
Audio / Best way to keep a music track running inside a thread?
« on: July 08, 2015, 09:33:07 pm »
Hi,

So I'm basically creating a thread for music with the code;

void startMusic() {
        sf::Music music;
        music.openFromFile("upandaway.ogg");
        music.setLoop(true);
        music.play();

        while (music.getStatus() == sf::Music::Status::Playing) {}
}

sf::Thread thread(&startMusic)

Obviously a while loop is not a viable way due to very high memory usage (tried it, and it was using 50% of my CPU. 50% for music!)

What would be an alternative to this, to keep the music running while keeping CPU Usage relatively low?

Pages: [1]
anything