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

Pages: [1]
1
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 10, 2014, 08:59:07 pm »
...

actually when I wrote that it wasn't true that the map loader performed culling of vertex arrays which weren't in view. I've updated the source on github so now it does.

Does it do it automatically?

I decided to test it out on a 512 x 512 tile map @ 64 x 64px tile size, where the map is just 4 layers, each one representing each quarter of the map.



I load the map and run the program in release, and get around 10-12 FPS no matter where the view currently is. It is the same if the view is right in a corner of the map, or on top of where all the layers meet.

Am I doing something very wrong?

(Yes I've updated to the most recent github version of the maploader.)

2
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 10, 2014, 01:15:03 pm »
Uhhh... That is pretty much what I am proposing.

The thing is that the whole map gets drawn, then the player.
I need to be able to slot my player in between the different layers to achieve the effect of the bottom being in front of the player.

3
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 10, 2014, 01:03:22 pm »
Can the order in which the layers are drawn be changed?

I want to have my player sprite be able to appear to be fully/partially behind objects if an object is in front of them, i.e. further down on the screen than the player.

Like this:


I was thinking something along the lines of using the quad-tree system used for collision to find anything that is close enough to be considered for reordering, then draw anything that is further up on the Y than where the player's sprite is first, then draw anything below the player on top.

Is this feasible?

4
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 08, 2014, 02:19:12 pm »
Ok, so I got this working after a bit of trouble setting up zlib.

Thx for this awesome tool!
Much easier to use than my own convoluted map making system.

Is there a way to only draw what is currently within the view to improve performance on big maps?
I loaded a 1024 x 1024 tile map where each tile is 64 x 64 pixels, and I get around 11 FPS.

I know if I'm having FPS issues as a result of map size then I should just break the map into smaller maps that get loaded individually as the player enters that area, but I would like to be able to implement this feature for if I need to in the future.

5
SFML projects / Re: 'Tiled' Tile-map Loader
« on: April 04, 2014, 07:34:19 pm »
Is a compiler that supports C++11 really necessary?

I'm using VC2010 and would hate to have to move to one of the more recent and somewhat unintuitive versions.  :-\

6
General / Re: Find right vector element to draw with indexed culling?
« on: March 30, 2014, 12:26:29 am »


Instead it should be more something like: y*NumberOfColumns+x

Yay, that is exactly what I was looking for. :D

I knew the problem was something to do with going from 2D to 1D. I face-palmed at how simple the solution is.
I was over-complicating things for myself, trying to formulate a way of turning my 1D index of sprites into 2D, thinking that it might work that way. :-\

7
General / Find right vector element to draw with indexed culling?
« on: March 28, 2014, 11:18:12 pm »
OK, here goes.

In my game I have a vector of sf::Sprites that contains all the sprites used to draw a map, with their positions on the map already set. My current method for drawing the map just loops through all of the elements in the vector and calls draw() for each one.

This draws everything, even stuff outside of the current view, which drags the FPS down to single digits for some of my large maps.

What I'm trying to do is something like what this person has done with indexed culling.
http://en.sfml-dev.org/forums/index.php?topic=7933.0

I understand how this works but the way my map is stored means I have to figure out how to find which sprite should be drawn based on where the current view is.

CreateMap.h
#pragma once
#include "SFML\Graphics.hpp"
#include <vector>

class CreateMap : public sf::Drawable, public sf::Transformable
{
public:
        CreateMap(const std::string fileName, float spriteSize, int gameScale, sf::Image& spriteSheet)
        {
                mapScale = gameScale;
                mapSpriteSize = spriteSize;
                mapImage.loadFromFile(fileName);
                textures.loadFromImage(spriteSheet, sf::IntRect(0, 144, 256, 48));

                TILE_WIDTH = 16 * gameScale;
                TILE_HEIGHT = 16 * gameScale;

                MAP_WIDTH = 64 ;
                MAP_HEIGHT = 64 ;

                loadMap();
        };

        // Loads the map
        void loadMap();

        // Used to update the position of the view, which is used by the draw function to find out which spirtes to draw.
        void indexedCulling(sf::View& view);

private:
        // The map comes from an image file.
        sf::Image mapImage;

        // CreateMap.cpp assigns each colour found in the above image file to a particular sprite found on a spritesheet image file that has been loaded elsewhere in the program.
        std::vector <sf::Color> mapPixels;

        // Each sprite by now has been assigned a texture and a position on the map.
        std::vector <sf::Sprite> mapSprites;

        // The size of each sprite. Currently 16x16 pixels.
        float mapSpriteSize;

        // How big the map
        int mapScale;

        // This holds the image that each sprite is getting its texture from.
        sf::Texture textures;

        int TILE_WIDTH, TILE_HEIGHT;
        int MAP_WIDTH, MAP_HEIGHT;
        int startX, endX;
        int startY, endY;

        // This is what I want to use to draw the map
        void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                states.transform *= getTransform();

                for(int y = startY; y < endY; y++)
                {
                        for(int x = startX; x < endX; x++)
                        {
                                target.draw(mapSprites[y + x]);
                        }
                }
        };
};

CreateMap.cpp
#include "CreateMap.h"

void CreateMap::loadMap()
{
        int mapSize = mapImage.getSize().x * mapImage.getSize().y;
        mapPixels.resize(mapSize);

        // Sets the size of the vector to the size of the map image
        mapSprites.resize(mapSize);

        // Add new tiles here
        sf::Color Grass                 ( 34, 177,  76, 255);
        sf::Color Water                 (  0, 162, 232, 255);
        sf::Color Sand                  (255, 242,   0, 255);
        sf::Color Tree                  (185, 122,  87, 255);
        sf::Color Waves                 ( 63,  72, 204, 255);
        sf::Color Bush                  (146, 195,  98, 255);
        sf::Color Brown_Brick           ( 53,  51,  45, 255);
        sf::Color Snow                  (206, 214, 205, 255);
        sf::Color Marble                        (159, 172, 149, 255);
        sf::Color BLACK                 (  0,   0,   0, 255);


        sf::Color VOID                  (255,   0, 255, 255);

        int i = 0;

        for(int y = 0; y < mapImage.getSize().y; y++)
        {
                for(int x = 0; x < mapImage.getSize().x; x++)
                {
                        mapPixels[i] = mapImage.getPixel(x, y);

                        mapSprites[i].setTexture(textures, false);
                        // Check to see what texture the pixel on the map should be
                        if              (mapPixels[i] == Grass) mapSprites[i].setTextureRect(sf::IntRect( 64,  16,  16,  16));
                        else if (mapPixels[i] == Water) mapSprites[i].setTextureRect(sf::IntRect( 80,  32,  16,  16));
                        else if (mapPixels[i] == Waves) mapSprites[i].setTextureRect(sf::IntRect( 96,  32,  16,  16));
                        else if (mapPixels[i] == Sand)  mapSprites[i].setTextureRect(sf::IntRect( 16,   0,  16,  16));
                        else if (mapPixels[i] == Tree)  mapSprites[i].setTextureRect(sf::IntRect( 48,   0,  16,  16));
                        else if (mapPixels[i] == Bush)  mapSprites[i].setTextureRect(sf::IntRect( 48,   16,  16,  16));
                        else if (mapPixels[i] == Brown_Brick)   mapSprites[i].setTextureRect(sf::IntRect( 0,   32,  16,  16));
                        else if (mapPixels[i] == Snow)  mapSprites[i].setTextureRect(sf::IntRect( 112,  32,  16,  16));
                        else if (mapPixels[i] == Marble)        mapSprites[i].setTextureRect(sf::IntRect( 48,   32,  16,  16));
                        else if (mapPixels[i] == BLACK) mapSprites[i].setTextureRect(sf::IntRect(  64,  0,  16,  16));

                        // Displays pink if it can't find a texture to use
                        else mapSprites[i].setTextureRect(sf::IntRect(96, 0, 16, 16));
                       
                        mapSprites[i].setScale(mapScale,mapScale);
                        mapSprites[i].setPosition(x * (mapSpriteSize * mapScale), y * (mapSpriteSize * mapScale));

                        i++;
                }
        }
}

// Should only render what is inside the view. Gets called in the game update function.
void CreateMap::indexedCulling(sf::View& view)
{
        startX = (view.getCenter().x - ( 6 * TILE_WIDTH))/TILE_WIDTH;
        endX = (view.getCenter().x + ( 6 * TILE_WIDTH))/TILE_WIDTH;
        startY = (view.getCenter().y - ( 6 * TILE_HEIGHT))/TILE_HEIGHT;
        endY = (view.getCenter().y + ( 6 * TILE_HEIGHT))/TILE_HEIGHT;

        if(startX > MAP_WIDTH)
                startX = MAP_WIDTH;
        if(startX < 0)
                startX = 0;

        if(startY > MAP_HEIGHT)
                startY = MAP_HEIGHT;
        if(startY < 0)
                startY = 0;

        if(endY > MAP_HEIGHT)
                endY = MAP_HEIGHT;
        if(endY < 0)
                endY = 0;

        if(endX > MAP_WIDTH)
                endX = MAP_WIDTH;
        if(endX < 0)
                endX = 0;
}

I'm stumped on how to get this to only render what is inside the view without having to change a lot of my existing code and would appreciate if anyone would point me in the right direction.

I know this is something I should figure out for myself, but I have been stuck on this for a few weeks and I want to move onto other things.

Sorry if my code looks messy. I'm going to clean it up when this is working.  :(
Let me know if I need to explain anything more.

8
General / Re: Vector of projectile sprites problem
« on: February 13, 2014, 11:59:06 pm »
Yep, my loop looks like this now:
        if(clock.getElapsedTime().asMilliseconds() > updateRate)
                {
                        clock.restart();
//      *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       Update logic stuff here
                        fb.updateFireballs(keyboardRef);
                }
                window.clear(sf::Color::Black);
//      *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       Draw stuff here
                window.draw(fb);

                window.display();
        }

But the thing is, none of that was the problem (though I'm sure it would have gave me problems in the future).

After an hour of just messing about trying random stuff, I realised it was something to do with the locations of my exe. and the Spritesheet that it needed.
The flickering/warped sprites problem only happens when I run the program from the exe. in my Release folder (I'm using VC2010, Release mode, static SFML config btw) which also has the Spritesheet in it that the exe. is looking for.

When I run from VC itself there are no problems at all.

9
General / Re: Vector of projectile sprites problem
« on: February 13, 2014, 10:56:35 pm »
Ok, so I've done that and I've gave to documentation a good seeing to but none of it seems to have remedied the visual artifacts seen in the second screenshot.

10
General / Re: Vector of projectile sprites problem
« on: February 13, 2014, 09:02:56 pm »
What do you mean?

I'm only calling window.display() once every update as it seems pointless to call it if nothing has changed.



Am I going to have to redo a lot of code to make it fall into the 'Update' and 'Draw' paradigm?

11
General / Re: Vector of projectile sprites problem
« on: February 13, 2014, 08:38:36 pm »
...

Ok, so after commiting suicide I added
if(!fireball[i].spriteIsSet)
                {
                        fireball[i] = sprite;
                        fireball[i].spriteIsSet = true;
                }
to Fireball.cpp and put 'bool spriteIsSet' into the Sprite SFML header file and set it to false, but now...


What the...? O_o
Pressing space adds new elements to the vector and they show up and start moving, but if quickly tapping on the space key some of the sprites go like that. It happens randomly upon key presses.
I have no idea where to begin with this.

12
General / Vector of projectile sprites problem
« on: February 13, 2014, 07:21:54 pm »
Ok, so I'm having major problems with creating and updating a vector of sprites that are meant to represent projectiles. Everything compiles fine, but my sprite, or should I say sprite, has other plans it seems...

The fireball is meant to be moving across the screen, but it doesn't.


All relevant code, will compile to the above exe.:

Entity.h
#pragma once
#include "SFML/Graphics.hpp"
#include <Windows.h>

class Entity : public sf::Drawable, public sf::Transformable
{
public:
        void createTexture(const sf::Image& imageRef, int xTexturePosition, int yTexturePosition, int xTextureSize, int yTextureSize);
        void createSprite(int width, int height, int xPosition, int yPosition, float scale);

protected:
        sf::Texture texture;
        sf::Sprite sprite;

private:
        void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                states.transform *= getTransform();

                target.draw(sprite, states);
        };
       
};

Entity.cpp
#include "Entity.h"

void Entity::createTexture(const sf::Image& imageRef, int xTexturePosition, int yTexturePosition, int xTextureSize, int yTextureSize)
{
        texture.loadFromImage(imageRef, sf::IntRect((xTexturePosition * xTextureSize), (yTexturePosition * yTextureSize), xTextureSize, yTextureSize));
}

void Entity::createSprite(int width, int height, int xPosition, int yPosition, float scale)
{
        sprite.setTexture(texture);
        sprite.setTextureRect(sf::IntRect(0, 0, width, height));
        sprite.setOrigin((width / 2), (height / 2));
        sprite.setScale(scale, scale);
        sprite.setPosition(xPosition, yPosition);
}

Fireball.h
#pragma once
#include "Entity.h"
#include <vector>

class Fireball : public Entity
{
public:
        void createFireball(sf::Keyboard& keyboard);

private:
        void draw(sf::RenderTarget& target, sf::RenderStates states) const
        {
                states.transform *= getTransform();

                for(unsigned int i = 0; i < fireball.size(); i++)
                {
                        target.draw(fireball[i], states);
                }
        };

        std::vector <sf::Sprite> fireball; // A vector of sprites called fireball.
};

Fireball.cpp
#include "Fireball.h"

void Fireball::createFireball(sf::Keyboard& keyboard)
{
        if( keyboard.isKeyPressed(keyboard.Space))
        {
                fireball.resize(fireball.size() + 1);
        }

        for (unsigned int i = 0; i < fireball.size(); i++)
        {
                fireball[i] = sprite; // Gives the fireball at [i] the properties of the protected sprite in Entity.

                fireball[i].move(1,1); // This seems to do nothing.

                if(fireball[i].getPosition().x > 700)
                {
                        fireball.erase(fireball.begin() + i);
                }

                fireball.shrink_to_fit();
        }
}

Game.h
#pragma once
#include "SFML/Graphics.hpp"

class Game
{
public:
        Game();
        void runGame();

private:
        int screenHeight;
        int screenWidth;

        sf::Clock clock;
        long float updateRate;
};

Game.cpp
#include "Game.h"
#include "Fireball.h"

Game::Game()
{
        screenWidth = 1600;
        screenHeight = screenWidth / 16 * 9;
}

void Game::runGame()
{
        sf::Keyboard keyboard;
        sf::Keyboard& keyboardRef = keyboard;

        sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Y U NO WORK");
        sf::RenderWindow& windowRef = window;
       
        sf::Image spriteSheet;
        spriteSheet.loadFromFile("Main sprite sheet.png");
        sf::Image& imageRef = spriteSheet;


        Fireball fb;
        fb.createTexture(imageRef, 0, 8, 16, 16);
        fb.createSprite(16, 16, 150, 150, 4);

        long float updateRate = 1000.0f / 60.0f;       

    while (window.isOpen())
    {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed || keyboard.isKeyPressed(keyboard.Escape))
                                window.close();
                }
                window.clear(sf::Color(0,0,0));
               
                if(clock.getElapsedTime().asMilliseconds() >= updateRate)
                {
                        clock.restart();
//      *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       *       Game logic goes here
                        fb.createFireball(keyboardRef);

                        window.draw(fb);
                        window.display();
                }
        }
}

int main()
{
        Game game;
        game.runGame();
    return 0;
}

I've been stuck on this problem for what seems like an age. I think the problem is something to do with the draw function that is being used. Is window.draw(fb) using the draw function from Entity or the one in Fireball?
Also, shouldn't the fireball.move() in Fireball be doing something? The sprite just sits there.

I'm no C++ pro so plz help a noob out.

13
General / Re: setFramerateLimit doesn't work
« on: February 09, 2014, 07:27:29 pm »
Frame rate and update rate are different. Your computer will try and run the program as fast as possible, so you need a way of regulating the speed, otherwise your program will actually run at different speeds on different computers, which is generally bad.

Apart from that there could be a bunch of reasons. 'large' is relative, the program might be running just fine but could appear to be intensive if you have a weak CPU.

14
General / Creating a tilemap from image pixel data?
« on: February 09, 2014, 06:53:19 pm »
Ok so my game is coming along nicely and I'm at the point were I want to make an effective way of creating maps.

I know there is the read from a text file or section of code way that could have a bunch of numbers and/or letters to represent different tiles that I currently have loaded from a sprite sheet.
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
But that is visually unappealing and cumbersome to work with if I or a team member wanted to alter/create a map.


Is there a way of loading an image file and checking each pixel of that image line by line and fill in my array of tiles based on the colour value of each pixel?

Example


I want it to be able to read and identify the color of each pixel and draw a tile based on what the colour code is. I.e. if colour = #bb00bb then draw water, etc.

Does SFML have anything that might help with this? Anyone know of any good methods of approaching this or know of someone else who has implemented this?

15
Greetings.

So I'm a bit of a C++ noob and using SFML 2.1 to make a little game project to get better at programming.

I'm having problems when creating an inheritance hierarchy for the entities I plan on using for my game.
I made an Entity class in a separate header to use as a base class to derive other stuff like a player class and enemy class from.

What would the best way of having it hold a 'draw' function that the derived classes can use to draw themselves to the RenderWindow that I have in my main() be?
The tutorial on 'Creating a SFML-like entity' was confusing, and the example code gives me 'object of abstract class type "Entity" is not allowed;' errors.

I'm not going to be using vertex arrays. Just simple shapes (rectangles, circles, sprites) for now.

Quote
Entity.h

class MyEntity : public sf::Drawable, public sf::Transformable
{
public:
   //   Some stuff

private:

   //   pure virtual function
    virtual void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const = 0;


};

Quote
Player.h
#include "Entity.h"

class Player : public MyEntity
{
public:
   // Player stuff

private:

   void drawStuff(sf::RenderTarget& target, sf::RenderStates states) const
    {
        states.transform *= getTransform();

        target.draw(playerIcon, states);
    };

int playerXPosition;
int playerYPosition;
// Rectangle to represent player (placeholder)
sf::RectangleShape playerIcon;

};

Quote
Output
1>Game.cpp(22): error C2259: 'MyEntity' : cannot instantiate abstract class
1>          due to following members:
1>          'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'
1>          'void MyEntity::drawStuff(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          c:\users\user\documents\programming\crappygame\crappygame\Entity.h(13) : see declaration of 'MyEntity::drawStuff'
1>Game.cpp(23): error C2259: 'Player' : cannot instantiate abstract class
1>          due to following members:
1>          'void sf::Drawable::draw(sf::RenderTarget &,sf::RenderStates) const' : is abstract
1>          C:\Users\User\Documents\Programming\SFML-2.1-windows-vc10-32bits\SFML-2.1\include\SFML/Graphics/Drawable.hpp(69) : see declaration of 'sf::Drawable::draw'

Why can't I instantiate an abstract class?

To my knowledge the pure virtual drawStuff function is what makes Entity abstract, but I've tried removing it and still get the errors.

Also, what is the 'sf::RenderStates states' parameter meant to do and how would 'states' be passed in as an argument when calling the function?? I've looked at the documentation but it didn't make any sense to me.

Also, I tried searching the forums but the search thing appears broken. I tried it a few times and it told me to inform an admin.

I know I've made a rat's ass of this whole thing but please don't hate me. :(

Windows 7, Visual C++ 2010, Release config.

Pages: [1]
anything