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

Pages: [1]
Window / Re: Get rid of letterboxing
« on: November 01, 2017, 05:09:52 pm »
I feel like you're mixing multiple things here.

If you want to adjust the view depending on the window size, simply follow the official tutorial: https://www.sfml-dev.org/tutorials/2.4/graphics-view.php#showing-more-when-the-window-is-resized

If you get flickering, then this either a driver/display issue or your doing multiple window.display() calls.

Well I posted my window->display() code, I'm only calling it once.

Also I see what the tutorial is saying but if I do that, I get a lot of black space:

My code:
            sf::FloatRect visibleArea(0, 0, window->getSize().x, window->getSize().y);
            sf::View v(visibleArea);

Window size is 960x540. This is how it's technically supposed to look: (resized in photoshop manually)

I'm assuming I have to use sf::View::zoom()? Because I'm honestly confused how zoom works. Not sure what to set the factor to. Setting it to 2 makes the view smaller (no idea), setting it below 1 makes it larger. I set the factor to 0.333 (320/960) and it seems right (can barely tell), but the view is now not located at position(0, 0), so I don't know what it's doing:

Code I used for setting zoom:
    sf::FloatRect visibleArea(0, 0, window->getSize().x, window->getSize().y);
    sf::View v(visibleArea);
    v.zoom( 360.0 / 960.0 );

Window / Get rid of letterboxing
« on: November 01, 2017, 06:44:16 am »
So currently I've got code that is able to pick a correct logical width given a fixed logical height (180px) for the game resolution. It calculates the logical width based on the screen resolution you're on and adjusts the fixed logical height.

For example..

Say I want my logical height of the game to be at 180px. The ideal logical width would be: 320px  and the ideal logical height would stay at 180px since my display resolution is set to 1920 x 1080 and it's a perfect division.

If the display resolution is 1600 x 1024, the ideal logical size would be: 266.667 x 170.667 (integer truncation to 266 x 170).

I'm just not sure how to implement it correctly. So far it works on 1920 x 1080, but if I change the display to 1600 x 1024 and go fullscreen (borderless), the screen starts flickering. I can also see (past the flickering) that there is also still letterboxing. I believe that has to do with the posX and sizeX and posY and sizeY variables, but I'm not sure how to modify those correctly (code below).

My logical width and height calculation code:
sf::Vector2<int> GameWindow::getIdealSize() {

    sf::VideoMode desktop = sf::VideoMode::getDesktopMode();

    double display_width = desktop.width;//modes[0].width;
    double display_height = desktop.height;//modes[0].height;

    double ideal_width = 0; //we need to calculate this, we won't be using this fixed value anymore
    double ideal_height = 180; //fixed height value

    double aspect_ratio = display_width / display_height;

    ideal_width = round(ideal_height * aspect_ratio);

    //Portrait (comment this out as we're using landscape mode for this game, we have a fixed ideal_height set
    //ideal_height = round(ideal_width / aspect_ratio);

    //Perfect Pixel Scaling
    if((int)display_width % (int)ideal_width != 0){
        double d = round(display_width / ideal_width);
        ideal_width = display_width / d;

    if((int)display_height % (int)ideal_height != 0){
        double d = round(display_height / ideal_height);
        ideal_height = display_height / d;

    //ideal_width and ideal_height must be even numbers
    if((int)ideal_width & 1){
    if((int)ideal_height & 1){

    cout << "display_width: " << display_width << " display_height: " << display_height << " aspect_ratio: " << aspect_ratio << " ideal_width: " << ideal_width << " ideal_height: " << ideal_height << endl;

    sf::Vector2<int> v(ideal_width, ideal_height);

    return v;

So currently to implement scaling the view I'm using the letterboxing code provided by Hapax:

I'm honestly just not sure how to apply it here though. I don't know what to set sizeX and sizeY to (I'm pretty sure posX and posY would be 0). And I'm not sure why the screen flickers when I go on a lower resolution (1600x1024).

Current adjusted letterbox removal code:
sf::View GameWindow::getNoLetterboxView(sf::View view, int windowWidth, int windowHeight) {

    // Compares the aspect ratio of the window to the aspect ratio of the view,
    // and sets the view's viewport accordingly in order to archieve a letterbox effect.
    // A new view (with a new viewport set) is returned.

    float windowRatio = windowWidth / (float) windowHeight;
    float viewRatio = view.getSize().x / (float) view.getSize().y;
    float sizeX = 1;
    float sizeY = 1;
    float posX = 0;
    float posY = 0;

    bool horizontalSpacing = true;
    if (windowRatio < viewRatio)
        horizontalSpacing = false;

    // If horizontalSpacing is true, the black bars will appear on the left and right side.
    // Otherwise, the black bars will appear on the top and bottom.

    if (horizontalSpacing) {
        sizeX = viewRatio / windowRatio; //not sure what to set this to
        posX = 0;

    else {
        sizeY = windowRatio / viewRatio; //not sure what to set this to
        posY = 0;

    view.setViewport( sf::FloatRect(posX, posY, sizeX, sizeY) );

    return view;

And here is where I create the window and apply the view to the window:

//Multiply the logical size of the window by 3 so its not so tiny
int window_w = GameWindow::getIdealSize().x * 3;
int window_h = GameWindow::getIdealSize().y * 3;
//Create the window
window = GameWindow::createWindow(window_w, window_h);

sf::View view;
sf::Vector2<int> logicalSize = GameWindow::getIdealSize();
view.setSize( logicalSize.x, logicalSize.y );
view.setCenter( view.getSize().x / 2, view.getSize().y / 2 );
view = GameWindow::getNoLetterboxView( view, logicalSize.x, logicalSize.y );

while(running) {
    //Handle events here (toggle fullscreen by pressing F key)

    //Update logic code here

   //Rendering and applying view to window here


The game window looks fine. But when I go to fullscreen (borderless):
  • The screen flickers (I can barely even see the actual game)
  • I can still see letterboxing on the sides

I've got the correct logical size (unless I shouldn't be truncating it, should I be flooring/ceiling it?). What can I do to fix both the flickering and letterboxing still showing up?

Graphics / Re: Create sprite constantly?
« on: October 30, 2017, 05:44:21 am »
Ah thank you.

Graphics / Re: Create sprite constantly?
« on: October 29, 2017, 12:48:31 pm »
Whilst if it is only one sprite you're creating this way, the performance hit may not be noticeable, it is advised to only put the very necessary code into your render() function, since it is the function that is expected to be called most often.
Rephrased - no, sprite creation should be called only when it is absolutely need, optimally only once, preferably somewhere around the code where you load the textures.

And there's definitely no way to simply draw an image from a function like this then? A function that takes arguments such as x, y, angle, scale, etc. Without creating a new sprite instance?

And yeah this could potentially be called hundreds of times per frame.

Graphics / Create sprite constantly?
« on: October 29, 2017, 09:46:43 am »
I have a drawImage function where I give it an index to a list of already loaded textures and a few other arguments such as coordinates, scale, angle, etc.

To set these variables I make a Sprite object everytime this function is called and I do window->display(spriteInstance) ;

The thing is this is called every time I render a frame.

My question is, is it okay to constantly create a sprite instance like this or will it use up a lot of CPU?

Audio / Re: Theres an option for speed/tempo?
« on: August 07, 2017, 09:51:35 am »
Yes, a higher pitch has a higher frequency so increasing the pitch technically just increases the speed at which the samples are played back.

But how do programs like Audacity manage to up the pitch while maintaining the same speed of the sound?

Or is this delving into something too complicated to explain?

No, they're interleaved. Every two samples is a sample for each of the two channels.
For example, using five channels, the first five samples would be one sample per channel - all played at once. Then, the next set of five samples would be one samples per channel - all played at once. etc..

I'm confused, can you visualize it for me?

Is it like this, considering we're working with 5 samples per channel and 2 channels?
C# = Channel Number, S# = Sample Number
[C1S1, C1S2, C1S3, C1S4, C1S5, C2S1, C2S2, C2S3, C2S4, C2S5]


Audio / Re: Theres an option for speed/tempo?
« on: August 06, 2017, 12:52:59 pm »
The pitch is the only thing you can change that has an effect on the music speed:

For the current playback position:

Is there a reason why when you set the pitch higher the sound gets sped up?

You fixed it then, by sticking to the same number of channels?

Note that if you change the number of channels from one to two, you need twice as many samples. i.e. instead of one sample for the single channel, you need one sample for each of the two channels that are played at the same time.

At a guess, the reason it could sound 'silent' is that since the sample are playing at now twice the speed (two channels), it could be too high pitched for you to hear. Digitally, the actual back/forth waveform may be disrupted and may actually be incredibly low pitched, which may also be too low pitched for you to hear. This is all guesswork since you haven't provided the sample with which the situation occurs.

But how do you work with 2 channels though exactly if it's a 1D array? Shouldn't there be one array per channel?

So I'm loading a recorded file ("recording.ogg") which plays fine by default.

However what I'm doing now is I retrieve the samples and reverse them and try to put them in a new buffer.

After doing this and playing from the buffer, I hear nothing now. What's wrong?

// Headers
#include <SFML/Audio.hpp>
#include <iostream>
#include <string>

using namespace std;

//g++ test.cpp -o test -DSFML_STATIC -I C:\SFML\include -L C:\SFML\lib -lsfml-graphics-s -lsfml-window-s -lsfml-audio-s -lsfml-system-s -lopenal32 -lvorbisfile -lvorbisenc -lvorbis -logg -lFLAC -ljpeg -lfreetype -lgdi32 -lopengl32 -lwinmm

int main(int argc, char* argv[])
    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("recording.ogg")){
        return EXIT_FAILURE;


    const sf::Int16* samples = buffer.getSamples();

    size_t samples_count = buffer.getSampleCount();
    sf::Int16 newSamples[samples_count];

    //Reversing samples
    for(int i = 0; i <= samples_count; i++){
        newSamples[i] = samples[samples_count-i];

    sf::SoundBuffer newBuffer;

    if(!newBuffer.loadFromSamples(&newSamples[0], samples_count, 2, 44100)){
        cout << "Unable to load from new samples" << endl;

    sf::Sound snd(newBuffer);

    cout << "Done" << endl;
    return EXIT_SUCCESS;


EDIT: Apparently I had to use the same number of channels at the original buffer (original only had 1).
Anyone know why I can't just set it to 2? Why is it silent if I set the new buffer to 2 instead of the original (1) ?

Pages: [1]