Welcome, Guest. Please login or register. Did you miss your activation email?

Recent Posts

Pages: [1] 2 3 ... 10
1
General / I have a problem with background
« Last post by swthxbld3 on Today at 03:09:15 pm »
I need to make the background rise with the platforms when jumping, but I do not know how to do this. You also need the background to be drawn infinitely many times. Can you help me please
main.cpp
(#include <SFML/Graphics.hpp>
#include "hero.h"
#include "game.h"
#include <iostream>


int main() {
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");



    game(window);

   

    return 0;
})
hero.h
(#ifndef HERO_H
#define HERO_H

#include <SFML/Graphics.hpp>

class Hero {
private:
    sf::Sprite sprite;
    sf::Texture texture;
    float x, y;

public:
    Hero(std::string spritePath, float posX, float posY) {
        sprite.setTextureRect(sf::IntRect(0, 2048, 1024 ,1024));
        sprite.scale(sf::Vector2f(0.15, 0.15));
        if (!texture.loadFromFile(spritePath)) {
        }

        sprite.setTexture(texture);
        x = posX;
        y = posY;
        sprite.setPosition(x, y);
    }

    void Idle(sf::RenderWindow& window, float, float&);

    void draw(sf::RenderWindow& window);

    void move(sf::RenderWindow& window, float&, float&, float, float&, bool&);








};

#endif HERO_H)
hero.cpp
(#include <iostream>
#include "hero.h"
#include "update.h"



void Hero::Idle(sf::RenderWindow& window, float time, float& CurrentFrame)
{

}

void Hero::draw(sf::RenderWindow& window)
{
        window.draw(sprite);
}




void Hero::move(sf::RenderWindow& window, float&  playerX, float& playerY, float time, float& CurrentFrame, bool& Idle)
{

       

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
                playerX -= 0.1 * time;
                CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
                if (CurrentFrame != 0) {
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, 1024, 1024));
                }
                Idle = true;
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
                playerX += 0.1 * time;
                CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                if (CurrentFrame > 4) CurrentFrame -= 3; //если пришли к третьему кадру - откидываемся назад.
                sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 3072, -1024, 1024));
                Idle = false;
        }
        else {
                if (Idle == true) {
                        CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                        if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, 1024, 1024));
                }
                if (Idle == false) {
                        CurrentFrame += 0.005 * time; //служит для прохождения по "кадрам". переменная доходит до трех суммируя произведение времени и скорости. изменив 0.005 можно изменить скорость анимации
                        if (CurrentFrame > 3) CurrentFrame -= 2; //если пришли к третьему кадру - откидываемся назад.
                        sprite.setTextureRect(sf::IntRect(1024 * int(CurrentFrame), 2048, -1024, 1024));
                }
        }


    sprite.setPosition(playerX, playerY);

}






)
game.h
(#pragma once

#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>


float playerX = 250;
float playerY = 151;



void game(sf::RenderWindow& window) {

    window.setFramerateLimit(60);


    sf::Clock clock;

    while (window.isOpen()) {


        std::cout << time<< std::endl;
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }




        window.clear();
        scence(window, playerX, playerY);
        window.display();
    }
}
)
scene.h
(#pragma once

#include <SFML\Graphics.hpp>
#include <SFML\Audio.hpp>
#include <random>
#include <ctime>
#include <sstream>
#include "hero.h"



float CurrentFrame = 0;
bool Idle;
float backGroundY = 0;

void scence(sf::RenderWindow& window, float& playerX, float& playerY) {
        Hero hero("ALL/images/heroAnimation.png", 100, 100);



        sf::Texture backgroundTexture;
        sf::Texture platformTexture;
        backgroundTexture.loadFromFile("ALL/images//BG.jpg");
        platformTexture.loadFromFile("ALL/images/platform.png");
        sf::Sprite background(backgroundTexture);
        sf::Sprite platform(platformTexture);

        sf::RectangleShape gameoverBackground(sf::Vector2f(1920, 1080));
        gameoverBackground.setFillColor(sf::Color::White);
        sf::Clock clock;
        sf::Time t1 = sf::microseconds(10000);
        sf::Font font;
        font.loadFromFile("ALL/font/arial.ttf");
        sf::Text scoreText;
        scoreText.setFont(font);
        scoreText.setCharacterSize(50);
        scoreText.setFillColor(sf::Color::Red);
        sf::Text gameoverText;
        gameoverText.setFont(font);
        gameoverText.setString("Game Over!");
        gameoverText.setCharacterSize(80);
        gameoverText.setFillColor(sf::Color::Red);

        sf::SoundBuffer buffer;
        buffer.loadFromFile("ALL/sound/jump.wav");
        sf::Sound sound;
        sound.setBuffer(buffer);

        sf::Vector2u platformPosition[20];
        std::uniform_int_distribution<unsigned> x(0, 1920);       //-platformTexture.getSize().x
        std::uniform_int_distribution<unsigned> y(100, 1080);
        std::default_random_engine e(time(0));
        for (size_t i = 0; i < 20; ++i)
        {
                platformPosition[i].x = x(e);
                platformPosition[i].y = y(e);
        }

        float dy = 0;
        int height = 150;
        int score = 0;

        //const int PLAYER_LEFT_BOUNDING_BOX = 20;
        //const int PLAYER_RIGHT_BOUNDING_BOX = 60;
        //const int PLAYER_BOTTOM_BOUNDING_BOX = 70;

        const int PLAYER_LEFT_BOUNDING_BOX = 20;
        const int PLAYER_RIGHT_BOUNDING_BOX = 80;
        const int PLAYER_BOTTOM_BOUNDING_BOX = 100;


        while (window.isOpen())
        {
                float time = clock.restart().asMicroseconds();
                time = time / 800;
                clock.restart();
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        playerX -= 10;
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        playerX += 10;


                if (playerX > 1800)
                        playerX = 1800; //0
                if (playerX < -40)
                        playerX = -40; // 1920

                if (playerY == height && dy < (-1.62))
                {
                        score += 1;
                        scoreText.setString("Score: " + std::to_string(score));
                }

                // player&#39;s jump mechanism
                dy += 0.2;
                playerY += dy;
                backGroundY += dy;
               

                if (playerY < height)
                        for (size_t i = 0; i < 20; ++i)
                        {

                                playerY = height;
                                platformPosition[i].y -= dy;  // vertical translation
                                if (platformPosition[i].y > 1080) // set new platform on the top
                                {
                                        platformPosition[i].y = 0;
                                        platformPosition[i].x = x(e);
                                }

                        }
                // detect jump on the platform
                for (size_t i = 0; i < 20; ++i)
                {
                        if ((playerX + PLAYER_RIGHT_BOUNDING_BOX > platformPosition[i].x) && (playerX + PLAYER_LEFT_BOUNDING_BOX < platformPosition[i].x + platformTexture.getSize().x)        // player&#39;s horizontal range can touch the platform
                                && (playerY + PLAYER_BOTTOM_BOUNDING_BOX > platformPosition[i].y) && (playerY + PLAYER_BOTTOM_BOUNDING_BOX < platformPosition[i].y + platformTexture.getSize().y)  // player&#39;s vertical   range can touch the platform
                                && (dy > 0)) // player is falling
                        {
                                sound.play();
                                dy = -20;
                        }

                }
                hero.move(window, playerX, playerY, time, CurrentFrame, Idle);
                hero.Idle(window, time, CurrentFrame);

                window.draw(background);
                background.setPosition(0, backGroundY);
                hero.draw(window);

                // set and draw platforms
                for (size_t i = 0; i < 20; ++i)
                {
                        platform.setPosition(platformPosition[i].x, platformPosition[i].y);
                        window.draw(platform);
                }

                // game over
                if (playerY > 1080)
                {
                        gameoverText.setPosition(30, 200);
                        scoreText.setPosition(150, 400);
                        goto gameover;
                }
                window.draw(scoreText);
                window.display();
        }

        // Game Over
gameover:
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.draw(gameoverBackground);
                window.draw(gameoverText);
                window.draw(scoreText);
                window.display();
        }
       

})
2
Graphics / Re: How to correctly draw a triangle fan?
« Last post by Nortski on Today at 12:42:34 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).

Thank you for taking the time to help me with this.

I actually fixed the problem. As it turned out it was an off by one problem with my loop. A triangle fan needs to be 2 elements larger than the number of vertex points in my vector, 1 for the central position and 1 to close the gap between the last vertex and the first vertex.

// Make polygon 2 larger than vertex container
sf::VertexArray visibilityPolygon(sf::TriangleFan, vectorAngleContainer.size() + 2);
visibilityPolygon[0] = ray[0].position;
visibilityPolygon[0].color = sf::Color::Red;

for (int i = 1; i <= vectorAngleContainer.size(); i++)
{
    visibilityPolygon[i] = sf::Vertex(sf::Vector2f(vectorAngleContainer[i - 1].position.x, vectorAngleContainer[i - 1].position.y), sf::Color::Red);
}

// Set the last point == to the first vertex [1] (not [0] as that is the central point)
visibilityPolygon[vectorAngleContainer.size() + 1] = visibilityPolygon[1].position;
visibilityPolygon[vectorAngleContainer.size() + 1].color = sf::Color::Red;
3
General / Re: Can you integrate allegro into an sfml window?
« Last post by fallahn on Today at 11:57:24 am »
When you add the files to your project, make sure you're only compiling VideoTexture.cpp (not the header files)
4
Graphics / Re: How to correctly draw a triangle fan?
« Last post by Nortski on Today at 11:38:23 am »
Ok, I will try your suggestions.
It's weird though as it only happens on the right side of that shape and nowhere else.
5
General / Re: I have a problem
« Last post by kojack on Today at 09:08:51 am »
The biggest hit here is that you are loading the background jpg from disk every frame. It really should be loaded just once at the beginning then reused, like the hero sprite's texture.
6
General / Re: I have a problem
« Last post by swthxbld3 on Today at 06:17:40 am »
It turns out that this is because of the image itself. But now I have fps drawdowns. It is also unclear why. Could you look at the code and tell me what is better to redo.
7
General / I have a problem
« Last post by swthxbld3 on Today at 06:00:01 am »
When I run the program, the character moves in a small fps. I looked at how it could be solved and came across Clock and time. But the problem has not been solved. Please help me, I have to submit the project soon. The problem is that when I add the background, the character starts lagging
main.cpp
 (#include <SFML/Graphics.hpp>
#include "hero.h"
#include "game.h"
#include <iostream>


int main() {
    sf::RenderWindow window(sf::VideoMode(1920, 1080), "WayofDarkness");



    game(window);

   

    return 0;
})

hero.h
 (#ifndef HERO_H
#define HERO_H

#include <SFML/Graphics.hpp>

class Hero {
private:
    sf::Sprite sprite;
    sf::Texture texture;
    float x, y;

public:
    Hero(std::string spritePath, float posX, float posY) {
        if (!texture.loadFromFile(spritePath)) {
        }

        sprite.setTexture(texture);
        x = posX;
        y = posY;
        sprite.setPosition(x, y);
    }

    void draw(sf::RenderWindow& window);

    void move(sf::RenderWindow& window, float&, float&, float);








};

#endif HERO_H)

hero.cpp
 (#include <iostream>
#include "hero.h"
#include "update.h"



void Hero::draw(sf::RenderWindow& window)
{
        window.draw(sprite);
}

void Hero::move(sf::RenderWindow& window, float&  playerX, float& playerY, float time)
{

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                playerX -= 0.6 * time;
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                playerX += 0.6 * time;


    sprite.setPosition(playerX, playerY);

}






)

game.h
 (#pragma once

#include <SFML/Graphics.hpp>
#include "hero.h"
#include "scence.h"
#include <iostream>


float playerX = 250;
float playerY = 151;



void game(sf::RenderWindow& window) {

    //window.setFramerateLimit(60);


    Hero hero("HeroSprite/doodle.png", 100, 100);
    sf::Clock clock;

    while (window.isOpen()) {
        float time = clock.restart().asSeconds();
        clock.restart();

        std::cout << time<< std::endl;
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }


        window.clear();
        hero.move(window, playerX, playerY, time);
        scence(window);
        hero.draw(window);
        window.display();
    }
}
)

scene.h
 (#pragma once

#include <SFML/Graphics.hpp>





void scence(sf::RenderWindow& window) {
        sf::Texture backgroundTexture;
        backgroundTexture.loadFromFile("ALL/images/animationBG.jpg");
        sf::Sprite background(backgroundTexture);
        window.draw(background);


       

})

8
General / Re: Can you integrate allegro into an sfml window?
« Last post by Me-Myself-And-I on Today at 12:23:08 am »
Thankyou.
There's a problem though. 
216   3   ...\pl_mpeg.h   [Error] conflicting declaration 'typedef struct plm_plane_t plm_plane_t'
9
Graphics / Re: How to correctly draw a triangle fan?
« Last post by Hapax on May 18, 2024, 11:18:29 pm »
If the triangle solution works, you must check your formation of the triangle strip to see if it's correct, including infinitely thin or 'backwards' triangles (breaking the triangle strip shape).
It might be worth making the calculations using doubles - for more accuracy - and then only converting to floats when creating the graphics (triangle strip).
10
General / Re: Can you integrate allegro into an sfml window?
« Last post by fallahn on May 18, 2024, 12:05:47 pm »
This should get you off on the right foot:

https://github.com/fallahn/VideoTexture

Just add the files in the 'src' directory to your project, and check the example for usage :)
Pages: [1] 2 3 ... 10