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

Pages: [1]
1
There are two books I'm reading atm:
- SFML Game Development By Example by Raimondas Pupius.
-SFML Game Development by* Jan Haller, Henrik Vogelius Hansson, Artur Moreira

I'm currently having a hard time following SGDbyExample because he's handling player inputs event by reading a text file which feels so unnatural and his variable naming is weird like "l_name" in parameters.

I've already read the SFML-GD by Jan and in both books they walkthrough different implementations of a State Manager.
Which books' code has better code implementation for their state manager and their games in general?

Should I bother learning SFML Game Development by Example's implementation of his State manager? To me, it feels more inferior to the other book. 
Someone on reddit told me to skip to Chapter 9, where they explain Entity-Component-Systems.

2
it seems to be removed from Packt for some reason. maybe you can still find some printed copies around...

Sadly none. I checked on [redacted] and other free book sites and nothing came up.

I tried to add the author on Linkedin to ask him for a copy off the book, but Linkedin is not letting me "connect" with him for some reason.

3
I found this book: C++ 17 Game Development Projects: Create 3 fully functional games with C++ using SFML, OpenGL, and Vulkan by Artur Moreira.

There is literally no copy of this book online. No stores sell it and no one mentions it here. Was this book made redundant on purpose?

He is one of the authors off SFML Game Development

4
I found this book: C++ 17 Game Development Projects: Create 3 fully functional games with C++ using SFML, OpenGL, and Vulkan by Artur Moreira.

There is literally no copy of this book online. No stores sell it and no one mentions it here. Was this book made redundant on purpose?

He is one of the authors off SFML Game Development by Example.

5
General / Re: How to draw a cubemap on SFML with OpenGL
« on: November 29, 2020, 11:34:09 am »
Thanks!
But I read somewhere that  I had to download GLEW from http://glew.sourceforge.net/, add the include and lib files to the additional file settings respectively for the project settings on Visual Studio and then add #include <GL/glew.h> to my file.

6
General / How to draw a cubemap on SFML with OpenGL
« on: November 28, 2020, 09:06:18 am »
I really couldn't find much about the implementation on doing this with SFML. Alot of the syntax like GL_TEXTURE_CUBE_MAP, samplerCube etc are undefined. I read somewhere SFML isn't great for cubemaps but I'm not sure if that's true.

Say I wanted to draw the cubemap for
how would I implement it on SFML.

7
General / Flickering movement from drawing rectangleshape for snake game
« on: November 14, 2020, 01:19:25 am »
I keep getting weird flickering when it's draws the movement for the snake.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <deque>
#include <cassert>
#include <random>
#include <ctime>

constexpr int PIXEL_WIDTH{ 32 };
constexpr int PIXEL_HEIGHT{ 32 };
constexpr int SCREEN_WIDTH{ 800 };
constexpr int SCREEN_HEIGHT{ 600 };
constexpr int MAP_WIDTH{ SCREEN_WIDTH / PIXEL_WIDTH };
constexpr int MAP_HEIGHT{ SCREEN_HEIGHT / PIXEL_HEIGHT };


class Snake
{
private:
        std::deque<sf::Vector2f> m_snake{ sf::Vector2f(10, 10), sf::Vector2f(9, 10), sf::Vector2f(8, 10) };

        sf::RectangleShape m_cell{ sf::Vector2f(MAP_WIDTH, MAP_HEIGHT) };

        bool growing{ false };
public:
        Snake()
        {
                m_cell.setFillColor(sf::Color::Green);
        }

        Snake(const Snake& snake) = delete;

        ~Snake()
        {
        }
       
        void move(sf::Vector2f goToPos)
        {
                if (!growing)
                        m_snake.pop_back();

                growing = false;

                m_snake.emplace_front(goToPos);
        }

        void draw(sf::RenderWindow& window)
        {
                for (auto& body : m_snake)
                {
                        m_cell.setPosition(body.x * MAP_WIDTH, body.y * MAP_HEIGHT);
                        window.draw(m_cell);
                }
        }

        sf::Vector2f& operator[](int index)
        {
                assert(index >= 0 && index < m_snake.size());
                return m_snake[index];
        }

        std::deque<sf::Vector2f> getSnake() { return m_snake; }
};


void changeDirections(Snake& snake, const Directions& d)
{
        switch (d)
        {
        case Directions::UP:
                snake.move(sf::Vector2f(snake[0].x, snake[0].y - 1));
                break;
        case Directions::DOWN:
                snake.move(sf::Vector2f(snake[0].x, snake[0].y + 1));
                break;
        case Directions::LEFT:
                snake.move(sf::Vector2f(snake[0].x - 1, snake[0].y));
                break;
        case Directions::RIGHT:
                snake.move(sf::Vector2f(snake[0].x + 1, snake[0].y));
                break;
        default:
                break;
        }
}


int main()
{
        sf::RenderWindow window{ sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Snake" };

        sf::Event event{};

        float timeTaken{ 0.0f };
        const float TIME_PER_UPDATE{ 0.5f };
        // Timer starts here
        sf::Clock clock;

        std::vector<int> grid(MAP_WIDTH * MAP_HEIGHT);
        bool GameStart{ true };
        Snake snake{};

        for (int i{ 0 }; i < 10; ++i)
        {
                int value = getRandomNumber(grid.size() - 1);
                std::cout << value << '\n';
                grid[value] = -1;
        }

        Directions state{ Directions::RIGHT };
        while (window.isOpen())
        {
                timeTaken += clock.getElapsedTime().asSeconds();
                clock.restart();

                while (TIME_PER_UPDATE <= timeTaken)
                {
                        timeTaken -= TIME_PER_UPDATE;
                        while (window.pollEvent(event))
                        {
                                switch (event.type)
                                {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                case sf::Event::KeyPressed:
                                        switch (event.key.code)
                                        {
                                        case sf::Keyboard::Up:
                                                state = Directions::UP;
                                                break;
                                        case sf::Keyboard::Down:
                                                state = Directions::DOWN;
                                                break;
                                        case sf::Keyboard::Left:
                                                state = Directions::UP;
                                                break;
                                        case sf::Keyboard::Right:
                                                state = Directions::RIGHT;
                                                break;
                                        default:
                                                break;
                                        }
                                        break;
                                }
                        }

                        window.clear(sf::Color::Black);
                        changeDirections(snake, state);
                        snake.draw(window);
                }
                window.display();

        }
        return 0;
}
 

I also tried implementing sprite with IntRect but nothing was shown. What could I use or improve to remove the flickering? If you run the code, you'll see what I mean.

8
Quote
Both have led to the screen crashing and I have been trying to debug and fix it for some hours
So, after some hours, you should know at least where it is crashing, and possibly have more details about the problem? Can you share that with us?

Its running fine actually, however the screen is completely black. I can adjust the frequency, octaves and the scaling period but it still remains completely black. If I only use this if statements:

                        if (x % 2 == 0 && y % 2 == 0)
                        {
                                pixel_w = noise[(y * TERRAIN_SIZE + x) / 2];
                               
                        }
                        color = FloatToTerrainColor(pixel_w);
                        cell.setFillColor(color);
                        cell.setPosition(x * (PIXEL_WIDTH / 2), y * (PIXEL_HEIGHT / 2));
                        window.draw(cell);
 

It will be able to draw the squares with half the filled with color and the rest black like the image like I showed on the top.

However the other else if statements involve creating a rectangleShape, that takes a texture from an sf::Image, which has every pixel set to a color, to fill in the black squares.

9
Hello, i'm a new programmer. I've been trying to implement bi-linear interpolation for image scaling. For starters this is what it looks like with nearest neighbour:

(I haven't spent much time in getting the colours right).

So I scaled a 32x32 pixels by 2 to 64x64 like so:

This prints every perlin noise value for every even x,y coordinate pixel and just a black one for anything else. It uses sf::RectangleShape to draw the pixels. I'm trying to replace the black ones using bilinear interpolation.

For bi-linear I had a read of the wikipedia page https://en.wikipedia.org/wiki/Bilinear_interpolation:


And implemented this using float values:

void getSquarePoints(sf::Vector2i& bLPos, sf::Vector2i& bRPos, sf::Vector2i& tLPos, sf::Vector2i& tRPos, int x, int y)
{
                bLPos.x, tLPos.x = (x * (PIXEL_WIDTH / 2));
                bRPos.x, tRPos.x = tLPos.x + (PIXEL_WIDTH / 2);
                tLPos.y, tRPos.y = (x * (PIXEL_HEIGHT / 2));
                bLPos.y, bRPos.y = tLPos.y + (PIXEL_HEIGHT / 2);
}

void bilinearInterpolate(sf::RectangleShape& cell, float tL, float tR, float bL, float bR, int x, int y, sf::Texture& texture, sf::RenderWindow& window)
{
        sf::Image image{};

        sf::Vector2i tLPos{}, tRPos{}, bLPos{}, bRPos{}; // Position for each corner. tL = top Left
// bR = bottom Right

        getSquarePoints(bLPos, bRPos, tLPos, tRPos, x, y);

        int totalDistanceX{ TERRAIN_SIZE / 2 }; // Size of rectangleShape in X direction
        int totalDistanceY{ TERRAIN_SIZE / 2 }; // Size of rectangleShape in Y direction

        image.create(totalDistanceX, totalDistanceY, sf::Color(0, 0, 0));

        int differenceToLeft{}; // distance to left hand side
        int differenceToRight{}; // distance to right hand side
        float currentPixel{};
        for (int pixelY{ tLPos.y }; pixelY < bLPos.y; ++pixelY)
        {
                for (int pixelX{ tLPos.x }; pixelX < tRPos.x; ++pixelX)
                {
                        differenceToLeft = pixelX - tLPos.x;
                        differenceToRight = tRPos.x - pixelX;

                        //vertical
                        int differenceToTop{ tLPos.y - pixelY };
                        int differenceToBottom{ pixelY - bLPos.y };

                        float rTop = (float)(tL * differenceToRight + (tR * differenceToLeft)) / totalDistanceX;

                        float rBot = (float)(bL * differenceToRight + (bR * differenceToLeft)) / totalDistanceX;
               
                        currentPixel = (float)(rBot * differenceToTop + rTop * differenceToBottom) / totalDistanceY;
                        image.setPixel(pixelX, pixelY, FloatToTerrainColor(currentPixel));
                }
        }

        texture.update(image);
        cell.setTexture(&texture);
}

void drawNoise2d(float noise[], sf::RenderWindow& window, sf::Texture& texture)
{
        sf::Color color{};

        sf::RectangleShape cell{ sf::Vector2f(TERRAIN_SIZE / 2, TERRAIN_SIZE / 2) };

        float pixel_w{ 0 };
        for (int y{ 0 }; y < TERRAIN_SIZE * 2; ++y)
        {
                for (int x{ 0 }; x < TERRAIN_SIZE * 2; ++x)
                {
                         //Even x,y coords(already colored)
                        if (x % 2 == 0 && y % 2 == 0)
                        {
                                pixel_w = noise[(y * TERRAIN_SIZE + x) / 32];
                                color = FloatToTerrainColor(pixel_w);
                                cell.setFillColor(color);
                        }
                        // These in between the even x,y coords
                        else if (x % 2 == 1 && y % 2 == 0)
                        {
                                float left{noise[(y * TERRAIN_SIZE + x - 1) / 2] };
                                float right{ (noise[(y * TERRAIN_SIZE + x + 1) / 2]) };
                                bilinearInterpolate(cell, left, right, left, right, x, y, texture, window);
                        }
                        //These are in between top and bottom of even , y coords
                        else if (x % 2 == 0 && y % 2 == 1)
                        {
                                float top{ noise[(y * TERRAIN_SIZE + x - TERRAIN_SIZE) / 2] };
                                float bot{ noise[(y * TERRAIN_SIZE + x + TERRAIN_SIZE) / 2] };
                                bilinearInterpolate(cell, top, top, bot, bot, x, y, texture, window);
                        }
                        // These are inbetween to the sides of even,y coords
                        else if (x % 2 == 1 && y % 2 == 1)
                        {
                                float topLeft{ noise[(y * TERRAIN_SIZE + x - TERRAIN_SIZE - 1) / 2] };
                                float topRight{ noise[(y * TERRAIN_SIZE + x - TERRAIN_SIZE + 1) / 2] };
                                float bottomLeft{ noise[(y * TERRAIN_SIZE + x + TERRAIN_SIZE - 1) / 2] };
                                float bottomRight{ noise[(y * TERRAIN_SIZE + x + TERRAIN_SIZE + 1) / 2] };
                                bilinearInterpolate(cell, topLeft, topRight, bottomLeft, bottomRight, x, y, texture, window);
                        }
                       
                       
                       
                        cell.setPosition(x * (PIXEL_WIDTH / 2), y * (PIXEL_HEIGHT / 2));
                        window.draw(cell);
                }
        }
}

// CONSTANTS
constexpr int TERRAIN_SIZE{ 32 };
constexpr int SCREEN_WIDTH{ 1040 };    
constexpr int SCREEN_HEIGHT{ 1040 };
constexpr int PIXEL_WIDTH{ SCREEN_WIDTH / TERRAIN_SIZE };
constexpr int PIXEL_HEIGHT{ SCREEN_HEIGHT / TERRAIN_SIZE };

// in int main()

float *noise = new float[TERRAIN_SIZE * TERRAIN_SIZE]{};
generateNoise2d(nOctaves, noise, terrain, scalingBias); // method to generate noise values
sf::Texture texture{};
texture.create(TERRAIN_SIZE / 2, TERRAIN_SIZE / 2);
 
I've done the same bilinear function using the RGB values of the noise values unlike the one above which only uses the noise values. Both have led to the screen crashing and I have been trying to debug and fix it for some hours now and at this point, I've given up. I've also tried using sf::Image which led to no success either. Any suggestions?

10
General / Re: Turning values into pixels for perlin noise
« on: October 25, 2020, 02:22:01 am »
Thanks for the help guys anyway. I've been able to fix it. The problem was the size of the pixels compared to the screen size. I've adjusted them and I can get that greyscale perlin noise image and produce nice looking maps without the weird bits.

11
General / Re: Turning values into pixels for perlin noise
« on: October 23, 2020, 08:04:34 pm »
Thanks I can get really nice terrain
probably ought to be

int pixel_w = (int)(noise[y * SCREEN_WIDTH + x] * 12.0f);

Also it's more memory access friendly if you loop like this:
for (int y{ 0 }; y < SCREEN_HEIGHT; ++y)
{
    for (int x{ 0 }; x < SCREEN_WIDTH; ++x)
    {

    }
}
 

as you're not skipping SCREEN_WIDTH values at a time, you'll be accessing your array sequentially. If you print pixel_w to the console you'll see what I mean  ;D

Thanks I'm getting some nice looking terrain however I'm still unable to produce the "tv" set noise. Sometimes i get really obscure looking images like for example at the 5th octave and scaling at: 0.4f :



I'm using
std::mt19937 mersenne{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
the to produce the seeds.

I'm not sure how to fix it.  :-\

12
General / Re: Turning values into pixels for perlin noise
« on: October 23, 2020, 04:20:35 pm »
Thanks. I changed it to
void drawNoise2d(int mapWidth, int mapHeight, float noise[], sf::RenderWindow& window, sf::Texture& texture);

int main()
{
        // do stuff here

        sf::Texture texture{};
        texture.create(SCREEN_WIDTH, SCREEN_HEIGHT);

        // do stuff here
}
 
Note how I added sf::Texture &texture to the parameter. However the images that are generated look incredibly ugly. I configured button presses to change the scale and number of octaves. I'm using the same algorithm used in OneLoneCoder's video on perlin noise.

Here  are some of the results:





They all are very similar, like it produces a "shark-head" shape and I am never able to achieve the "television" noise and they produces a thin line of the colours.

void drawNoise2d(int mapWidth, int mapHeight, float noise[], sf::RenderWindow& window, sf::Texture& texture)
{
        sf::Image image{};
        sf::Sprite sprite{};

        image.create(SCREEN_WIDTH, SCREEN_HEIGHT, sf::Color(0, 0, 0));
        sf::Color color{};
        for (int x{ 0 }; x < SCREEN_WIDTH; ++x)
        {
                for (int y{ 0 }; y < SCREEN_HEIGHT; ++y)
                {
                        int pixel_w = (int)(noise[y * SCREEN_HEIGHT + x] * 12.0f);

                        switch (pixel_w)
                        {
                        case 0: // GREY was black before
                                color.r = 128;
                                color.g = 128;
                                color.b = 128;
                                break;
                        case 1: // DIM GREY
                                color.r = 105;
                                color.g = 105;
                                color.b = 128;
                                break;
                        case 2: // GREY
                                color.r = 128;
                                color.g = 128;
                                color.b = 128;
                                break;
                        case 3: // DARK GREY
                                color.r = 169;
                                color.g = 169;
                                color.b = 169;
                                break;

                        case 4: // SILVER
                                color.r = 192;
                                color.g = 192;
                                color.b = 192;
                                break;
                        case 5:
                                color.r = 0;
                                color.g = 0;
                                color.b = 255;
                                break;
                        case 6: // Lawn green
                                color.r = 124;
                                color.g = 252;
                                color.b = 0;
                                break;
                        case 7:
                                color.r = 32;
                                color.g = 178;
                                color.b = 170;
                                break;
                        case 8:  // Forest Green
                                color.r = 34;
                                color.g = 139;
                                color.b = 34;

                                break;
                        case 9:
                                color.r = 0;
                                color.g = 0;
                                color.b = 128;
                                break;
                        case 10: // SNOW
                                color.r = 255;
                                color.g = 250;
                                color.b = 250;
                                break;
                        case 11: // LAVENDER
                                color.r = 255;
                                color.g = 240;
                                color.b = 245;
                                break;
                        case 12: // White
                                color.r = 255;
                                color.g = 255;
                                color.b = 255;
                                break;
                        }

                        image.setPixel(x, y, color);
                }
        }
        texture.update(image);
        sprite.setTexture(texture);
        window.draw(sprite);
}
 

I'm not sure as to why.

13
General / Turning values into pixels for perlin noise
« on: October 23, 2020, 02:21:15 am »
Hello, I'm working on a Perlin noise generator.
The following just draws a completely black window.

void drawNoise2d(int mapWidth, int mapHeight, float noise[], sf::RenderWindow& window)
{
        sf::Image image{};
        sf::Texture texture{};
        sf::Sprite sprite{};

        image.create(SCREEN_WIDTH, SCREEN_HEIGHT, sf::Color(0, 0, 0));
        sf::Color color{};

        for (int x{ 0 }; x < SCREEN_WIDTH; ++x)
        {
                for (int y{ 0 }; y < SCREEN_HEIGHT; ++y)
                {
                        int pixel_w = std::floor(noise[y * SCREEN_HEIGHT + x] * 4.0f);

                        switch (pixel_w)
                        {
                        case 0:
                                color.r = 0;
                                color.g = 0;
                                color.b = 0;
                                break;
                        case 1:
                                color.r = 105;
                                color.g = 105;
                                color.b = 105;
                                break;
                        case 2:
                                color.r = 128;
                                color.g = 128;
                                color.b = 128;
                                break;
                        case 3:
                                color.r = 169;
                                color.g = 169;
                                color.b = 169;
                                break;

                        case 4:
                                color.r = 192;
                                color.g = 192;
                                color.b = 192;
                                break;
                        }

                        image.setPixel(x, y, color);
                }
        }
        texture.update(image);
        sprite.setTexture(texture);
        window.draw(sprite);
}
 

The noise[] holds an array of values. For example:
std::cout << "Noise # " << y * mapWidth + x << ": " << noise[y * mapWidth + x] << '\n';
 

Noise # 0: 0.786747
Noise # 100: 0.770166
Noise # 200: 0.753585
Noise # 300: 0.737004
Noise # 400: 0.720423
Noise # 500: 0.703842
Noise # 600: 0.687262
Noise # 700: 0.67491
Noise # 800: 0.662559
Noise # 900: 0.650208
Noise # 1000: 0.637857
Noise # 1100: 0.625506
Noise # 1200: 0.613155
Noise # 1300: 0.605034
Noise # 1400: 0.596913
Noise # 1500: 0.588791
Noise # 1600: 0.58067
Noise # 1700: 0.572549
Noise # 1800: 0.564428
Noise # 1900: 0.556306
Noise # 2000: 0.548185
Noise # 2100: 0.540064
Noise # 2200: 0.531942
Noise # 2300: 0.523821
Noise # 2400: 0.5157
Noise # 2500: 0.507579
Noise # 2600: 0.503518
Noise # 2700: 0.499457
Noise # 2800: 0.495397
Noise # 2900: 0.491336
Noise # 3000: 0.487276
Noise # 3100: 0.483215
Noise # 3200: 0.479154
Noise # 3300: 0.475094
Noise # 3400: 0.471033
Noise # 3500: 0.466972
Noise # 3600: 0.462912
Noise # 3700: 0.458851
Noise # 3800: 0.454791
Noise # 3900: 0.45073
Noise # 4000: 0.446669
Noise # 4100: 0.442609
Noise # 4200: 0.438548
Noise # 4300: 0.434487
Noise # 4400: 0.430427
Noise # 4500: 0.426366
Noise # 4600: 0.422305
Noise # 4700: 0.418245
Noise # 4800: 0.414184
Noise # 4900: 0.410124
Noise # 5000: 0.406063
Noise # 5100: 0.410124
Noise # 5200: 0.414184
Noise # 5300: 0.418245
Noise # 5400: 0.422305
Noise # 5500: 0.426366
Noise # 5600: 0.430427
Noise # 5700: 0.434487
Noise # 5800: 0.438548
Noise # 5900: 0.442609
Noise # 6000: 0.446669
Noise # 6100: 0.45073
Noise # 6200: 0.454791
Noise # 6300: 0.458851
Noise # 6400: 0.462912
Noise # 6500: 0.466972
Noise # 6600: 0.471033
Noise # 6700: 0.475094
Noise # 6800: 0.479154
Noise # 6900: 0.483215
Noise # 7000: 0.487276
Noise # 7100: 0.491336
Noise # 7200: 0.495397
Noise # 7300: 0.499457
Noise # 7400: 0.503518
Noise # 7500: 0.507579
Noise # 7600: 0.5157
Noise # 7700: 0.523821
Noise # 7800: 0.531942
Noise # 7900: 0.540064
Noise # 8000: 0.548185
Noise # 8100: 0.556306
Noise # 8200: 0.564428
Noise # 8300: 0.572549
Noise # 8400: 0.58067
Noise # 8500: 0.588791
Noise # 8600: 0.596913
Noise # 8700: 0.605034
Noise # 8800: 0.613155
Noise # 8900: 0.621276
Noise # 9000: 0.629398
Noise # 9100: 0.637519
Noise # 9200: 0.64564
Noise # 9300: 0.653761
Noise # 9400: 0.661883
Noise # 9500: 0.670004
Noise # 9600: 0.678125
Noise # 9700: 0.686246
Noise # 9800: 0.694368
Noise # 9900: 0.702489
Noise # 1: 0.777325
Noise # 101: 0.761523
Noise # 201: 0.745722
Noise # 301: 0.72992
Noise # 401: 0.714118
Noise # 501: 0.698317
Noise # 601: 0.682515
Noise # 701: 0.670458
Noise # 801: 0.6584
Noise # 901: 0.646343
Noise # 1001: 0.634286
Noise # 1101: 0.622229
Noise # 1201: 0.610171
...CONTINUED...
 

The window screen is completely black but running. Where is it wrong or could it be a poor implementation elsewhere in the code. I don't think its the latter, because before this function, I was able to draw randomly picked sample of values of noise[] on a 1D plane, but translating that into 2D isn't working. So I think the error is due to this function. 

I'm really new to SFML so I'd really appreciate some help. :-[
 

14
General / Re: Use console and the game window at the same time
« on: October 20, 2020, 06:02:11 pm »
Ok I looked into threads and this the change I made
//in main()
        bool threadRunning{ true };
        sf::Thread thread([&game, &threadRunning]() {
                while (threadRunning)
                        threadRunning = game.playGame();
        }
        );

        thread.launch();


        while (window.isOpen())
        {

                if (!threadRunning)
                {
                        thread.wait();
                        break;
                }

                DrawHangman(skipper, window, clock, deltaTime, game.get_TotalWrongLetters());
        }

        return 0;
 

It works as how I intended it to be. However it feels incredibly messy. What could I improve on it?

15
General / Use console and the game window at the same time
« on: October 19, 2020, 10:58:27 pm »
Hi, I'm a new programmer and I have made this hangman game. I am trying to make the user input at the console and the animation to move on the game window simultaneously.

However, when the user is needed to make an input for the console, the game window only shows the first still sprite image and animate through the row.

void DrawHangman(Animation& animation, sf::RenderWindow& window, sf::Clock& clock, float& deltaTime, int totalWrong)
{
        deltaTime += clock.getElapsedTime().asSeconds();
        clock.restart();

        if (deltaTime >= 0.25f)
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }

                animation.update(deltaTime, totalWrong);

                window.clear();
                animation.draw(window);
                window.display();

                deltaTime = 0.0f;
        }
}

int main()
{
        unsigned int videoWidth{ 900 };
        unsigned int videoHeight{ 500 };
       
        sf::RenderWindow window(sf::VideoMode(videoWidth, videoHeight), "Skipping");

        Animation skipper(std::string("Sprites/blueskipper.png"), 4, 4, sf::Vector2f(200, 220));

        float deltaTime{ 0.0f };

        sf::Clock clock;
       
        // Picks a random word from wordlist.txt
        WordMap word;
        const std::string wordToGuess = word.getRandomWord();
        Game game(wordToGuess);


        while (window.isOpen())
        {
                //Draw sprite
                DrawHangman(skipper, window, clock, deltaTime, game.get_CurrentWrongLetters());
               
                // Main game that happens in console
                if (!game.playGame())
                        break;
        }
        return 0;
}

This is the playGame() function.
bool Game::playGame()
{
    system("cls");
    std::cout << "Guessed Letters: " << guessedLetters << '\n';
    std::cout << progress;

    char guess = getGuess();
    IsContains(guess);
    return ((wrongLetters < maxWrongGuesses) && (progress != word));
}

update function
void Animation::update(float deltaTime, int totalWrong)
{
        m_timeTaken += deltaTime;
        if (m_timeTaken >= 0)
        {
                m_source.x++;
               
                if (m_source.x >= m_column)
                {
                        m_source.y++;
                        m_source.x = 0;
                }
                if (m_source.y >= totalWrong)
                        m_source.y = 0;
        }
}
 

Example of what happens:
It will show this on console
[console]
Guessed Letters: a12
___a____
Enter letter:

[game window]
Still image of the first sprite.

However when I tried
while (game.playGame())
        {
                while (window.isOpen())
                        DrawHangman( skipper, window, clock, deltaTime, game.get_CurrentWrongLetters() );
        }
 

It does the complete opposite. The game window will have the sprite animated, but won't allow inputs for the console.

I understand why they both don't work but I just can't figure how animate in the game window while the console is active too.

Pages: [1]