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

Pages: [1]
1
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)

2
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?

3
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 :)

4
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