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

Pages: [1]
1
SFML projects / SFMLnake - Snake clone
« on: June 24, 2018, 07:09:06 am »
Hello Community members,

As a part of my learning process I’ve created yet another snake clone… their pretty popular around here recently
What makes this project special?. As I’ve created some basic “clones” in the past like Pong, Arkanoid and Tetris but this one as only went all the way to actual final 1.0v :).
Yes, it’s my first fully finished project.

Any thoughts and feedback really appreciated.

Github project:
https://github.com/PawelWorwa/SFMLnake

Windows binaries:
https://github.com/PawelWorwa/SFMLnake/releases/tag/v1.0.0

Linux/Mac binaries:
Well I’m afraid it’s required to compile project using included cmake file.

Controls:
Arrows – movement
m key – turn music on/off

Screenshots from game:
(click to show/hide)

2
SFML projects / Simplex Noise generator
« on: July 20, 2017, 02:44:36 pm »
Hello Community Members,

I know that there were project like this in past... nevertheless I wanted to create own or to be more precisely - port existing algorithm from other language with couple of additional functionality. Mostly for further project, that required random terrain generation.

Putting it all together there it is: Simplex Noise generator for 2D plane. I’ve used SFML to visualise output, it looks like this (greyscale):



If anyone is interested, here’s the code: https://github.com/PawelWorwa/SimplexNoise

Any feedback appreciated  :)


3
General / White square issue after accesing resource from std::map
« on: October 05, 2016, 04:55:59 pm »
Hello Community,

I’m facing "strange" (in my opinion) issue – infamous white square problem, in which I can’t find its source.
I’ m well aware of official tutorial (http://www.sfml-dev.org/tutorials/2.3/graphics-sprite.php#the-white-square-problem) – used it couple of times, but this time I just don’t see what might be causing an error.
Also, I’ve searched forum for similar issue but as it seems each case is different and uique in its own way…

Describing my issue in couple words: I’m trying to create title manager – class that’s storing titles inside std::map container, so I could access them when necessary. The issue is, when I’m creating “plain” Title object – it’s drawn without problem. Once I store it inside container and try to access it, all I’m receiving is white square.

I’m a little bit stuck at this moment, so any suggestion would be very appreciated.

Minimal code provided below:

#include <SFML/Graphics.hpp>

enum class LocalTitleType {
    Grass
};

class LocalTitle {
    public:
        static const int WIDTH = 64;
        static const int HEIGHT = 64;

        void setTexture( sf::Texture &texture ) {
            title.setTexture( &texture );
        }

        sf::RectangleShape getTitle( void ) {
            return title;
        }

        void create( void ) {
            title.setSize( sf::Vector2f( WIDTH, HEIGHT ) );
        }

    private:
        sf::RectangleShape title;
};

class LocalTitleManager {
    public:
        void create( void ) {
            createGrassTitle();
        }

        LocalTitle& getTitleRef( LocalTitleType titleType ) {
            return titles.at( titleType );
        }

    private:
        std::map< LocalTitleType, LocalTitle > titles;

        void createGrassTitle( void ) {
            sf::Texture texture;
            texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity

            LocalTitle title;
            title.create();
            title.setTexture( texture );

            titles[LocalTitleType::Grass] = title;
        }
};

int main() {
    LocalTitleManager manager;
    manager.create();

    sf::RenderWindow window( sf::VideoMode( 200, 200 ), "Title Manager Test" );
    while ( window.isOpen() ) {
        sf::Event event;
        while ( window.pollEvent( event ) ) {
            if ( event.type == sf::Event::Closed )
                window.close();
        }// while

        window.clear();

        LocalTitle title = manager.getTitleRef( LocalTitleType::Grass );
        window.draw( title.getTitle() ); /** here's the issue */

        /** white square problem is not existent using below:
        sf::Texture texture;
        texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity
        LocalTitle localTitle;
        localTitle.create();
        localTitle.setTexture( texture );
        window.draw( localTitle.getTitle() );
        */


        window.display();
    }// while

    return EXIT_SUCCESS;
}


I know that issue lies inside “LocalTitleManager” Class, in this method precisely:
void createGrassTitle( void ) {
   sf::Texture texture;
   texture.loadFromFile("media/textures/titles.png"); //one texture for simplicity

   LocalTitle title;
   title.create();
   title.setTexture( texture );

   titles[LocalTitleType::Grass] = title;
}

...but according to my knowledge, once I’ve created desired texture, attach it to LocalTitle class and finally store it in Container, it should work.

What am I missing?

Thank You for You’re help in advance

4
General / Sprite manager issue
« on: February 15, 2016, 10:03:30 pm »
Hello,

I’ve been trying to create resource (sprites) manager, but ended up having problem. Idea was like this: image and texture are being loaded into memory only once (those files might be big), Sprites that are being created should point to those resources instead.

That’s for the vision, below something I’ve got so far:
Hpp class file
#ifndef OBJECTS_H
#define OBJECTS_H

#include <SFML/Graphics.hpp>
#include <memory>

class Objects {
    public:
        Objects();
        virtual ~Objects();
        void draw ( sf::RenderWindow *window );

    protected:
        void create ( void );
        sf::Sprite foo ( void );

    private:
        sf::Image image;
        sf::Texture texture;
        //std::vector < sf::Sprite > sprites;         // First approach -> Without pointers, it's working
        std::vector < sf::Sprite* > sprites;
        //std::vector < std::unique_ptr < sf::Sprite > > sprites; // Third approach - smart ptr's
};

#endif // OBJECTS_H

Cpp Class file:
#include "Objects.hpp"

Objects::Objects() {
    image.loadFromFile ( "image.png" ); // just some red 20x20 px square
    texture.loadFromImage( image );
    create();
}

Objects::~Objects() {
    //dtor
}

// Objects::create
void Objects::create ( void ) {
    sf::Sprite sprite;
    sprite.setTexture( texture );

    // sprites.push_back ( sprite ); // First approach - works, but we have unnecesary textures per sprite resisting in memory?
     sprites.push_back( &sprite ); // Second approach - not working
    // sprites.push_back( std::unique_ptr < sf::Sprite > ( &sprite ) ); // Third approach, not working as well

}// Objects::create

// Objects::draw
void Objects::draw ( sf::RenderWindow *window ) {
    //window->draw ( ( sprites[0] ) ); // First working approach
    window->draw ( ( *sprites[0] ) );  // Second and Third approach - not working
}// Objects::draw

In main.cpp, I’m, just creating class object and invoking draw function:
#include <SFML/Graphics.hpp>
#include "Objects.hpp"

int main() {
    Objects o;

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "Texture Loading Test!" );
    while ( window.isOpen() ) {
        sf::Event event;
        while ( window.pollEvent( event ) ) {
            if ( event.type == sf::Event::Closed ) {
                window.close();
            }
        }// while

        window.clear();
        o.draw( &window );
        window.display();

    }// while

    return 0;
}

As for Me... it should work  :), but (surprise)… it’s not. I believe we have some kind of memory leek here. I assume, the problem lies here (passing reference to object, pointing to null texture):
void Objects::create ( void ) {
    sf::Sprite sprite;
    sprite.setTexture( texture );
     sprites.push_back( &sprite );


I know I can ignore the pointers approach, and just stick to “first approach” mentioned above, but to my knowledge it will create unnecessary textures, resisting in memory (for example, creating 100 of sprites will create 100 of additional textures) – is my reasoning correct?

I must say, I’ve got no clue how to fix this... :( Any suggestions/hints/leads are most welcomed.

 
Thank You for help in advance.

5
General / draw vector class - problem
« on: June 12, 2015, 04:22:28 pm »

Hello,

I have some issue with drawing vectror of class containing shape objects.
Below sample code:

#include <SFML/Graphics.hpp>
#include <iostream>

class Block
{
    private:
        sf::RectangleShape rectangle;
    public:
        sf::RectangleShape create ( int xPos, int yPos );
};

sf::RectangleShape Block::create ( int xPos, int yPos )
{
    sf::RectangleShape object( sf::Vector2f ( 120, 50 ) );
    object.setPosition( xPos, yPos );
    return object;
}

int main()
{
   sf::RenderWindow window;
   window.create(sf::VideoMode(800, 600), "Sample application");
   std::vector < Block > blocks;
   Block *ptr;
   for (int i = 0; i < 10; i++)
   {
        ptr = new Block;
        ptr->create( i*2, i*2 );
        blocks.push_back(*ptr);
   }
   delete ptr;
   ptr = NULL;
   //Windows
   while(window.isOpen())
   {
       //Event Handler
        sf::Event event;
        while(window.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                window.close();
            }//if
        }//while

        window.clear();

        for ( int i = 0; i < blocks.size(); i++ ) {
            window.draw( blocks[i] ); /** ERROR */
        }

        window.display();
   }//while
}//main

Qustion is why am I getting an error?

In advance, thank You for answer   :)

Pages: [1]