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

Pages: [1]
1
General / texture.update not working correctly
« on: December 09, 2017, 06:56:35 am »
I'm trying to make a UI blur using a fragment shader. The current route I'm taking is a sf::Sprite with a constantly updating sf::Texture, then the sf::Sprite is drawn with the shader, but instead the screen is just stuck on a single frame when using the shader.

The constructor:
BackgroundBlur::BackgroundBlur(sf::Vector2f _Position, sf::Vector2f _Size, float _Strength)
        : UIElement(_Position, true)
{
        sf::Texture _Texture;

        Textures.push_back(_Texture);

        sf::Sprite _Sprite;
        _Sprite.setTexture(Textures[0]);
        _Sprite.setTextureRect(sf::IntRect(_Position.x, _Position.y, _Size.x, _Size.y));

        Sprites.push_back(_Sprite);

        Blur.loadFromFile("Resource/Data/Shaders/Blur.frag", sf::Shader::Fragment);

        Blur.setUniform("blur_radius", sf::Glsl::Vec2(Strength, Strength));
        Blur.setUniform("texture", Textures[0]);
}

The sf::Texture is created here:
void BackgroundBlur::BeginPlay()
{
        Textures[0].create
        (
                CurrentApp->GetWindow().getSize().x,
                CurrentApp->GetWindow().getSize().y
        );
}

And the problem resides here, not updating, it works, but no blur:
void BackgroundBlur::Tick(float _DeltaTime)
{
        Textures[0].update(CurrentApp->GetWindow());
}

This is the draw function, if I remove the sf::Shader from the draw arguments, it works again, but there isn't blur:
void BackgroundBlur::Draw(sf::RenderTarget & _Target, sf::RenderStates _States) const
{
        _Target.draw(Sprites[0], &Blur);
}

I've tried adding
Blur.setUniform("texture", Textures[0]);
to the Tick function, however it seems to do nothing.

Also, the fragment shader I am using is
uniform sampler2D texture;
uniform vec2 blur_radius;

void main() {
    vec2 textureCoordinates = gl_TexCoord[0].xy;
    vec4 color = vec4(0.0);
    color += texture2D(texture, textureCoordinates - 10.0 * blur_radius) * 0.0012;
    color += texture2D(texture, textureCoordinates - 9.0 * blur_radius) * 0.0015;
    color += texture2D(texture, textureCoordinates - 8.0 * blur_radius) * 0.0038;
    color += texture2D(texture, textureCoordinates - 7.0 * blur_radius) * 0.0087;
    color += texture2D(texture, textureCoordinates - 6.0 * blur_radius) * 0.0180;
    color += texture2D(texture, textureCoordinates - 5.0 * blur_radius) * 0.0332;
    color += texture2D(texture, textureCoordinates - 4.0 * blur_radius) * 0.0547;
    color += texture2D(texture, textureCoordinates - 3.0 * blur_radius) * 0.0807;
    color += texture2D(texture, textureCoordinates - 2.0 * blur_radius) * 0.1065;
    color += texture2D(texture, textureCoordinates - blur_radius) * 0.1258;
    color += texture2D(texture, textureCoordinates) * 0.1330;
    color += texture2D(texture, textureCoordinates + blur_radius) * 0.1258;
    color += texture2D(texture, textureCoordinates + 2.0 * blur_radius) * 0.1065;
    color += texture2D(texture, textureCoordinates + 3.0 * blur_radius) * 0.0807;
    color += texture2D(texture, textureCoordinates + 4.0 * blur_radius) * 0.0547;
    color += texture2D(texture, textureCoordinates + 5.0 * blur_radius) * 0.0332;
    color += texture2D(texture, textureCoordinates + 6.0 * blur_radius) * 0.0180;
    color += texture2D(texture, textureCoordinates - 7.0 * blur_radius) * 0.0087;
    color += texture2D(texture, textureCoordinates - 8.0 * blur_radius) * 0.0038;
    color += texture2D(texture, textureCoordinates - 9.0 * blur_radius) * 0.0015;
    color += texture2D(texture, textureCoordinates - 10.0 * blur_radius) * 0.0012;
    gl_FragColor = color;
}
, taken from https://en.sfml-dev.org/forums/index.php?topic=20155.0

EDIT: I'm so stupid, instead of
Blur.setUniform("texture", Textures[0]);
it should be
Blur.setUniform("texture", sf::Shader::CurrentTexture);

2
General / How should I avoid calling draw every frame?
« on: September 09, 2017, 06:02:53 pm »
I've recently found out drawing 6000+ Sprites/Shapes is very heavy on performance, and I require advice on how I should begin handling this.

I intend to have an upwards of 1 million Sprites/Shapes in the future, and this can only worsen as I am actually drawing these 6000 sprites/shapes twice (one for the minimap, one for the main view). Obviously ~12000 draw calls every frame is going to slow down any computer, but I'm interested in how VertexArray handled this issue.

The first, most obvious route to take is chunking, but with the way I have things set up right now, implementing chunking would go against the framework of my program. Are there any other ways I can cut down the unnecessary draw calls per frame, or draw them in a more optimized way?

Thanks.

3
Hello,

So, I have a simple problem (or at least it seems). Let's say I have a sf::Sprite, and a sf::IntRect. The sf::IntRect covers a certain region of the sf::Sprite, and is relative to the sf::Sprite, but that's in local space of the sf::Sprite, what would I do if I wanted to find out where this sf::IntRect was in global space? I think I'm missing something big here.

4
General / Erasing AnimatedSprite from vector triggers crash
« on: April 22, 2017, 10:08:44 am »
Hello,
I've been looking for an answer for the past few days, and it's been frustrating.

I have a vector array of Enemy classes:
vector<Enemy> enemies;

while (window.isOpen()) {

                        if (spawned == false) {
                               
                                for (int i = 0; i < 5; i++) {
                                        Enemy enemyNew(enemy);
                                        enemyNew.setPosition(sf::Vector2f(100 + (i * 160), 100));
                                        enemyNew.setHealth(enemyHealth);
                                        enemies.push_back(enemyNew);
                                };

                                spawned = true;
                        }
}
 


 The Enemy class has a private AnimatedSprite variable from https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite, now, once a bullet hits an Enemy, the enemy loses health, blah blah, dies. Once the enemy is dead it sets a boolean to false.

In the main window loop, it erases the Enemy from the vector if that boolean is false.
                        enemies.erase(std::remove_if(enemies.begin(), enemies.end(), predEnemy), enemies.end());

However, once I kill an enemy, and it is erased from the vector, it returns
vector subscript out of range
and leads me to the following function in Animation.cpp (from the AnimatedSprite wiki):
const sf::IntRect& Animation::getFrame(std::size_t n) const
{
        return m_frames[n];
}

So I have identified it isn't a fault in my code. Also, this happens at random times, so if I launched into the game, waited a couple seconds, killed the enemy, then slowly killed all the other enemies aswell, there would be no crash. But as soon as I start firing rapidly it triggers the error. I assume this has to do with the frames of the Animation etc.

Also, no, the bullet is not animated.

Thanks in advance.

5
Graphics / Sprite is drawn as solid white
« on: April 15, 2017, 02:21:38 pm »
Hello!

I've come a across an issue which I cannot seem to understand.
So I'm creating a header file to handle all my tile creation, which can be passed onto main.cpp to be drawn (sort of). But I'm having some trouble.

tiles.h
#pragma once

#include <SFML/Graphics.hpp>

sf::Sprite createWallSide(sf::Vector2f position,int side) {
        // Create texture and load image
        sf::Texture wallSide_texture;
        wallSide_texture.loadFromFile("Tiles/Walls/wallSide.png");
        wallSide_texture.setSmooth(false);

        // Create sprite and set texture
        sf::Sprite wallSide;
        wallSide.setTexture(wallSide_texture);
        wallSide.setPosition(position);
        wallSide.setRotation(90);
        switch (side) {
                case 0:
                        wallSide.setScale(5, 5);
                        break;
                case 1:
                        wallSide.setScale(-5, 5);
                        break;
        };
        // Return sprite
        return wallSide;
}

main.cpp
Only the relevant parts:
Variable declaration
sf::Sprite wall = createWallSide(sf::Vector2f(100, 100), 0);
Variable later being drawn
window.draw(wall);

Now I first suspected it was the image itself, so instead of making it a transparent background, I made it a black background, but it made no difference. So then I thought it was smoothing, and since the image is small, it was maybe being blurred to hell, but setSmooth made no difference.

Any help is appreciated!

6
EDIT: I just solved it by playing the music right after it is loaded in.


7
General / How to switch between Menu and Game screens efficiently?
« on: January 01, 2017, 04:19:56 pm »
Hello.

I'm not sure if I'm posting on the right forums or not but I'm having trouble understanding how to switch between game and menu screens.

I have already created my main menu, and it is not a small amount of code. But now I need a way to move between the other screens such as settings and the game itself.

I've already looked at https://github.com/SFML/SFML/wiki/Tutorial:-Manage-different-Screens but I found it very confusing. I'm not very familiar with using different files to get things done and have only stuck to main.cpp when learning C++.

Is there a built in function that will manage this for me, and do I have to edit parts of my code that reference the RenderWindow?

Pages: [1]
anything