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

Author Topic: Is there a way to take a capture of the map width and height?  (Read 2791 times)

0 Members and 1 Guest are viewing this topic.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Is there a way to take a capture of the map width and height?
« on: August 20, 2014, 03:17:46 pm »
Ok so what I want to do is save an image of the entire map to a file. The problem im having is it only saves what is shown in the window, I want it to save the entire height and width of the map not the window. I'm a bit unsure on how to go about doing this though, if it's even possible.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #1 on: August 20, 2014, 03:21:50 pm »
You can use a RenderTexture with the size of your entire map and save that as an image.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #2 on: August 20, 2014, 03:26:59 pm »
Don't forget that textures have a maximum size that depend on the graphics card (can be 512x512 on crappy GPUs), so if you map is bigger you'll have to split into multiple smaller parts.
Laurent Gomila - SFML developer

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #3 on: August 20, 2014, 03:33:54 pm »
Hmm, ok how would the code look? I'm having trouble getting it working. As for the size problem, I could write some code that captures the map in grids then puts them all together and outputs it, if that will work, not sure though.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #4 on: August 20, 2014, 03:40:30 pm »
Create a RenderTexture with the right size, clear it, draw your map, display the RenderTexture, extract the image information with copyToImage() and then save the image.
See the documentation/tutorials for detailed usage.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #5 on: August 20, 2014, 04:22:48 pm »
Ok, I've been reading the documentation and I'm stuck, this is the code I have so far.

void CaptureScreen(WinVars &winVars, GameVariables &gameVars, Player &player)
{

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
    {
        sf::RenderTexture RT;
        if(!RT.create(gameVars.x, gameVars.y))
        {
            cout << "Error creating Render Texture" << endl;
        }

        RT.draw(gameVars.tiles);

        sf::Texture getRTImage(RT.getTexture());
        getRTImage.copyToImage();

        sf::Image RTImage;
        RTImage.saveToFile();


        //sf::Image screenshot = winVars.window.capture();
        //screenshot.saveToFile("screenshot.png");
    }
}
 

I would like to just draw it to a texture then output it as a .jpg or a .png but i'll do whatever works.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #6 on: August 20, 2014, 04:27:38 pm »
void CaptureScreen(WinVars &winVars, GameVariables &gameVars, Player &player)
{

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::C))
    {
        sf::RenderTexture RT;
        if(!RT.create(gameVars.x, gameVars.y))
        {
            cout << "Error creating Render Texture" << endl;
        }

        RT.clear(); // Don't forget that bit!!!
        RT.draw(gameVars.tiles);
        RT.display(); // Don't forget that bit!!!

        // No need to explicitly declare a temp object
        sf::Image RTImage = RT.getTexture().copyToImage();
        RTImage.saveToFile("screenshot.png");


        //sf::Image screenshot = winVars.window.capture();
        //screenshot.saveToFile("screenshot.png");
    }
}
« Last Edit: August 20, 2014, 04:36:47 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #7 on: August 20, 2014, 04:35:23 pm »
Alright I see what I did wrong, thank you. But when i use it, the file is blank, why is that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #8 on: August 20, 2014, 04:40:43 pm »
Forgot to clear the render texture (really just think of it like a render window), but I'm not sure whether it will fix the issue.

You can also try to add the texture to a sprite and render it to the screen to see if the content you've drawn is visible there.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #9 on: August 20, 2014, 04:45:39 pm »
Ok well I did clear and a little black bar appears in the middle of the image, thats it. Also, the "tiles" is a sprite, should i use the texture that holds them?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #10 on: August 20, 2014, 05:22:30 pm »
you can't directly draw the texture, but you need it alive when drawing the sprite. maybe that's why the empty square showed up.

what's inside the GameVariables class?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #11 on: August 20, 2014, 05:57:46 pm »
Its just a struct holding variables, noting else really:

#ifndef VARIABLESTRUCT_H_INCLUDED
#define VARIABLESTRUCT_H_INCLUDED

#include <string>
#include <vector>
#include <fstream>
#include <SFML/Graphics.hpp>

struct GameVariables
{
    GameVariables(std::string MAPNAME,
                  std::string TILENAME,
                  std::string LINE,
                  std::vector<std::vector<sf::Vector2i> > MAP,
                  std::vector<sf::Vector2i> TEMPMAP,
                  sf::Texture TILETEXTURE,
                  sf::Sprite TILES,
                  std::vector<std::string> STORETILESHEETNAMES,
                  std::vector<std::string> STOREMAPNAMES);

    std::string mapName;
    std::string tileName;
    std::string audioFileName;
    std::string tempLineStorage; //Temporarily stores the line from MapList.txt
    std::string characterName;

    std::vector<std::vector<sf::Vector2i> > map;
    std::vector<sf::Vector2i> tempMap;

    std::vector<std::string> storeTileSheetNames;
    std::vector<std::string> storeMapNames;
    std::vector<std::string> storeAudioNames;
    std::vector<std::string> storeMusicNames;
    std::vector<std::vector<sf::Vector2f> > storeNPCLocations;
    std::vector<std::string> storeCharacters;
    sf::Texture tileTexture;
    sf::Sprite tiles;
    sf::Sprite playerSprite;
    sf::Texture playerTexture;
    sf::Vector2i source;
    sf::Clock clock;

    sf::Texture charTexture;
    sf::Sprite charSprite;
    sf::Mouse mouse;

    //AnimationSpeed
    float frameCounter = 0;
    float switchFrame = 64; //Speed to switch frame higher = slower, lower = faster
    float frameSpeed = 800;

    //Player Speed
    sf::Vector2f currPlayerPosition;
    sf::Vector2f playerVelocity;
    float maxSpeed = 1.5f;
    float accelSpeed = 1.5f;
    float decelSpeed = 0.01f;

    int x; //Map width
    int y; //Map height

    sf::View scrollScreen;
    sf::Vector2f screenPosition;

    bool isFirstStartup = true; //Check to see if the player has a save file

    enum Direction {Down, Left, Right, Up}; // Player Directions
};

#endif // VARIABLESTRUCT_H_INCLUDED
 

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Is there a way to take a capture of the map width and height?
« Reply #12 on: August 20, 2014, 06:05:53 pm »
Hmm, well i looked at the link you provided and I tried putting it everywhere i could think of, I even took out the if statement so it would just capture it and it still got the same problem.

 

anything