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

Pages: [1]
1
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.

2
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.

3
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?

4
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