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

Author Topic: Returning sprites through functions.  (Read 2968 times)

0 Members and 2 Guests are viewing this topic.

Bonediggerninja

  • Newbie
  • *
  • Posts: 10
    • View Profile
Returning sprites through functions.
« on: December 10, 2016, 06:23:26 am »
I have a header file that has a function. That loads my sprites since I have 35 of the same. However, I need to draw the sprites in my main file. Or, If there is a better way to do this?

How would I return my 35 sprites to my main file?

Code

Pathfind.h
#pragma once

#include<iostream>
#include<cmath>

//Sfml Includes
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>




void createMap(int) {

        int rowNumber = 1;
        sf::Texture mapSprites;
        int map[35] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
        int tileNumber = 1;

        if (!mapSprites.loadFromFile("LevelSprites.png")) {

                std::cout << "mapSprites not Loading" << std::endl;
        } //For loading the sprites.

        std::vector<sf::Sprite> sprites(35, sf::Sprite(mapSprites));


        for(tileNumber = 0; tileNumber < 35; tileNumber++) {
               
                if (map[tileNumber] == 0){
                       
                        for (int i = 0; i < sprites.size(); i++) {
                               
                               
                                sprites[i].setTextureRect(sf::IntRect(0, 0, 60, 60)); //First panel
                                sprites[i].setPosition( (tileNumber % 7 + 1) * 60, rowNumber * 60);
                                sprites[i].setScale(2, 2);
                        }
               
                }//end if statement
                if (tileNumber % 7 == 0)
                        rowNumber++;
        }//ends for statement

       
}

Main code
#include<iostream>
#include <cstdlib> //Random No
#include <ctime> //Time
//Sfml Includes
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#include"Pathfind.h"
int main() {


        std::cout << "Swords of Alleria Start" << std::endl;
        sf::RenderWindow window(sf::VideoMode(840, 600), "Swords of Alleria");
        sf::Event CloseCheck;  
        window.setFramerateLimit(60);

       
        sf::Font font;
        createMap(0);
        if (!font.loadFromFile("corbel.ttf")) {

                std::cout << "Error: Font not found." << std::endl;

        }
        else {

                std::cout << "Font corble Loaded" << std::endl;
        }

        //Random Seed
        srand(time(NULL));

        while (window.isOpen())
        {
               
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Return)) {

                }
                while (window.pollEvent(CloseCheck))
                {
                       
                        if (CloseCheck.type == sf::Event::Closed)
                                window.close();
                }
               
                window.clear(sf::Color::Black);
                //I need to draw them here

                window.display();
        }



        return 0;
}

thanks in advance.
« Last Edit: December 10, 2016, 08:40:14 am by Bonediggerninja »

EGYPTIAN CODER

  • Newbie
  • *
  • Posts: 32
    • View Profile
    • Email
Re: Returning sprites through functions.
« Reply #1 on: December 10, 2016, 06:11:58 pm »
if I were you I wouldn't post all my code because probably no one has a time for that ;
I really give up reading after some lines of code;
but to give some help  how about a for loop
window.clear();
for(int q=0;q<35;q++)
{
window.draw(sprite);
}
window.display();
 
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Returning sprites through functions.
« Reply #2 on: December 11, 2016, 11:29:20 am »
All of your "things" (sprites and texture) are created locally inside the function and cease to exist when that function is over. To overcome this, you can simply create the texture and sprites vector in the main function and then pass them (as a reference) to your createMap function to set them up:
void createMap(sf::Texture& texture, std::vector<sf::Sprite>& sprites)
{
}
void main()
{
    sf::Texture texture;
    std::vector<sf::Sprite> sprites;
    createMap(texture, sprites);
}

Note that that using multiple sprites for a tile map in inefficient. If your map is staying at this size, it should be fine, of course, but if this is a test and you intend to increase the size of the visible map, you should consider using a vertex array.
There is even an example of how to use a vertex array to create a static tile map.
To create a tile map that changes, the tiles would need to be updated so the code would need to be expanded.
Or you could just use Selba Ward's Tile Map ;)

Your sf::Event should really be reset at the beginning of each cycle/tick/frame/program loop otherwise you may run into problems later on. One easy way to do this is just construct the event each time just before the polling loop as in all of the examples.

if I were you I wouldn't post all my code because probably no one has a time for that ;
I really give up reading after some lines of code;
It's not that long :P

but to give some help  how about a for loop
window.clear();
for(int q=0;q<35;q++)
{
window.draw(sprite);
}
window.display();
 
 
It's an std::vector so I would definitely recommend this way instead:
for (auto& sprite : sprites)
    window.draw(sprite);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*