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

Author Topic: Sprite Container/ Combiner?  (Read 4577 times)

0 Members and 1 Guest are viewing this topic.

SethGandy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Sprite Container/ Combiner?
« on: January 27, 2013, 12:27:49 pm »
So I'm new to the wonderful world of SFML.
I'm making an isometric tile engine as my first project.
Problem I'm having is I want to combine all the tiles I draw into one object.
That way the map moves all as one piece once rendered.
How would I go about making this said "container"?

My current map rendering system looks like this:
Code: [Select]
// image already loaded in to textureVector[x], mapVector[x][y] is the map in zeros and ones
Sprite tileSprite;
for(int i = 0; i < MapVector.size(); i++)
{
for(int j = 0; j < MapVector[i].size(); j++)
{
tileSprite.setTexture(textureVector[MapVector[i][j]]);
tileSprite.setPosition((j * BLOCKSIZE / 2)-(i * BLOCKSIZE /2)+40, (j * BLOCKSIZE / 4)+(i * BLOCKSIZE /4)+40);
tileSprite.setRotation(45);
Window.draw(tileSprite);
}
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Container/ Combiner?
« Reply #1 on: January 27, 2013, 12:55:34 pm »
class Tilemap : public sf::Drawable, public sf::Transformable
{
private:
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states)
    {
        states.transform *= getTransform(); // sf::Transformable::getTransform()
        target.draw(member, states);
    }

    sf::Sprite member; // can be a container of tiles or whatever
}

The class Map inherits all the public methods like setPosition() etc. If you call them from the outside, the transform changes, which influences sf::RenderStates.

Take a look at the corresponding parts of the SFML 2 documentation to understand the mechanics in detail.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SethGandy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite Container/ Combiner?
« Reply #2 on: January 28, 2013, 01:50:14 am »
Apologies, I'm confused as to how to implement this?
I'm pretty rusty on inheritance classes and virtuals.
I can't create a new class object such as Tilemap newOb; , so I'm kinda at a loss as how to use this lol  ???
« Last Edit: January 28, 2013, 03:47:35 am by SethGandy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Sprite Container/ Combiner?
« Reply #3 on: January 28, 2013, 02:19:45 am »
I'm pretty rusty on inheritance classes and virtuals.
Then you should read up on it, it's one of the most essential subjects in C++. ;)

I can't create a new class object such as Tilemap newOb;
Why not?
Where are you exactly stuck? What don't you understand? :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

SethGandy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite Container/ Combiner?
« Reply #4 on: January 28, 2013, 03:22:28 am »
When I try to create the new object it errors out 'Tilemap': cannot instantiate abstract class.
pure virtual function "sF::Drawable::draw" has no overrider.

Also I'm actually reading up on it, from C++ PROGRAMMING: PROGRAM DESIGN INCLUDING DATA STRUCTURES, 5TH EDITION. It's for class.

This whole isometric thing is just my own infatuation getting the best of me.
« Last Edit: January 28, 2013, 03:48:20 am by SethGandy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Sprite Container/ Combiner?
« Reply #5 on: January 28, 2013, 03:26:14 am »
'Tilemap': cannot instantiate abstract class.
pure virtual function "sF::Drawable::draw" has no overrider.
This means you haven't defined the draw function for the class.
Maybe you forgot to add Tilemap:: infront of draw(), when defining it in the source file?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Sprite Container/ Combiner?
« Reply #6 on: January 28, 2013, 03:52:21 am »
Nexus forgot const after his draw (which is/should be there because Evil Laurent™ makes things const correct).
Back to C++ gamedev with SFML in May 2023

SethGandy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite Container/ Combiner?
« Reply #7 on: January 28, 2013, 03:55:10 am »
Attached is my project in it's entirety.
So far, I've only written to the main.cpp, I haven't bothered splitting up my functions and creating headers and implementation files yet, not until I have the base of the program done... bad habit of mine.

But here's the code thus far:
#include<SFML\Graphics.hpp>
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;
using namespace sf;

#define ScreenWidth 800
#define ScreenHeight 600

#define BLOCKSIZE 40
int currentMapX;
int currentMapY;

vector <vector <int> > MapVector;
vector <Texture> textureVector;

class Tilemap : public Drawable, public Transformable
{
private:
        virtual void draw(RenderTarget& target, RenderStates states)
        {
                states.transform *= getTransform(); // sf::Transformable::getTransform()
                target.draw(member, states);
        }
        Sprite member; // can be a container of tiles or whatever
};

void LoadMap(const char *filename)
{
        ifstream openfile(filename);
        vector <int> tempVector;

        string line;

        if(openfile.is_open())
        {
                while(!openfile.eof())
                {
                        getline(openfile, line);

                        string tempLine = line.erase(line.find_last_not_of(" ") + 1);
                        int space = tempLine.find_first_of(" ");
                        if(space == 0)
                                tempLine = tempLine.erase(0, space + 1);
                        stringstream str(tempLine);

                        while(!str.eof())
                        {
                                string value;
                                getline(str, value, ' ');
                                if(value.length() > 0)
                                        tempVector.push_back(atoi(value.c_str()));
                        }
                        MapVector.push_back(tempVector);
                        tempVector.clear();
                }
        }
        cout<<"[------- MAP -------]\n";

        //------Ouput Map---------
        int mapY=0;
        int mapXtemp=0;
        int mapX=0;
        for (int r=0; r < MapVector.size(); r++)
        {
                mapY++;
                for (int c=0; c < MapVector[r].size(); c++)
                {
                        cout << MapVector[r][c] << " ";
                        mapXtemp++;
                }
                if(mapXtemp > mapX)
                {
                        mapX=mapXtemp;
                }
                mapXtemp=0;
                cout << endl;
        }
        currentMapX = mapX;
        currentMapY = mapY;
        cout<<endl<<mapY<<"x"<<mapX<<endl;
}
void LoadTextures(const char *filename)
{
        cout << "#####################\n";
        string line;
        Texture inText;
        ifstream openfile(filename);

        if(openfile.is_open())
        {
                while(!openfile.eof())
                {
                        getline(openfile, line);
                        string tempLine = line.erase(line.find_last_not_of(" ") +1);
                        cout<<"Loaded : ["<<tempLine<<"]"<<endl;

                        if(tempLine.length() > 0)
                        {
                                if(!inText.loadFromFile(tempLine))
                                {
                                        cout << "Could not load" << line << endl;
                                }
                                textureVector.push_back(inText);
                        }
                }
        }
        cout << "#####################\n";
}
void DrawMap(RenderWindow &Window)
{
        RectangleShape mapCont;
        mapCont.setFillColor(Color::Green);

        mapCont.setSize(Vector2f((currentMapX)*BLOCKSIZE,(currentMapY)*BLOCKSIZE));
        mapCont.setPosition(20,40);
        mapCont.setRotation(45);
        mapCont.setPosition(mapCont.getPosition().x, mapCont.getPosition().y * 2);

        View v = Window.getDefaultView();
        v.setSize(v.getSize().x, v.getSize().y * 2);
        v.setCenter(v.getSize() *.5f);
        Window.setView(v);
        Window.draw(mapCont);
        Window.setView(Window.getDefaultView());
        //Window.setView(v);

        Sprite tileSprite;
        for(int i = 0; i < MapVector.size(); i++)
        {
                for(int j = 0; j < MapVector[i].size(); j++)
                {
                        tileSprite.setTexture(textureVector[MapVector[i][j]]);
                        tileSprite.setPosition((j * BLOCKSIZE)+40,(i * BLOCKSIZE)+40);
                        //tileSprite.setRotation(45);
                        Window.draw(tileSprite);
                }
        }
        Window.setView(Window.getDefaultView());
}

int main()
{
        RenderWindow Window(VideoMode(ScreenWidth,ScreenHeight,32), "isometric engine", Style::Close);
        LoadTextures("Textures.txt");
        LoadMap("Map1.txt");

        Font Font;
        if (!Font.loadFromFile("font.ttf"))
                return 1;

        Text Text("Seth Gandy | iso v0.01", Font);
        //Text.setStyle(Text::Style::Bold);
        Text.setCharacterSize(25U);
        Text.setPosition(510,565);

        while(Window.isOpen())
        {
                Event Event;
                while(Window.pollEvent(Event))
                {
                        if(Event.type == Event::Closed || Event.key.code == Keyboard::Escape)
                                Window.close();
                }

                Window.clear(Color(0, 125, 255));
                DrawMap(Window);
                Window.draw(Text);
                Window.display();
        }
        return 0;
}
 

[attachment deleted by admin]

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Sprite Container/ Combiner?
« Reply #8 on: January 28, 2013, 04:15:56 am »
virtual void draw(RenderTarget& target, RenderStates states)
Add const at the end of this line.
Also, globals, macros(#define xxx yyy) and using directives(using namespace xxx) are considered bad practice in c++, use / for paths, it works perfecly normal on windows and visual supports that in its advanced options for intelisense.
« Last Edit: January 28, 2013, 04:31:16 am by FRex »
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Container/ Combiner?
« Reply #9 on: January 28, 2013, 06:47:08 am »
So far, I've only written to the main.cpp, I haven't bothered splitting up my functions and creating headers and implementation files yet, not until I have the base of the program done... bad habit of mine.
There are a lot of bad habits in your code ;)

Apart from the ones FRex mentioned, don't use using namespace everywhere. It defeats the purpose of namespaces, and leads to constructs like Font Font;. Don't put sf::Texture into std::vector (unless you pre-allocate).

By the way, you were not supposed to copy my code 1:1, it was meant as an example. I also wrote "Take a look at the corresponding parts of the SFML 2 documentation to understand the mechanics in detail." And if you have a problem with understanding of classes, you should really read about them first, because C++ basics are mandatory to use SFML.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

SethGandy

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite Container/ Combiner?
« Reply #10 on: January 29, 2013, 03:55:38 am »
Hitting the books, putting the project off for a bit.
Been reading for the past 5 hours, to help wash away the feeling of shame and incompetence.