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

Author Topic: Store a made texture by the program for future use  (Read 3891 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Store a made texture by the program for future use
« on: December 11, 2013, 10:42:57 pm »
Hello there people,

I'm trying to implement a custom car maker, well basically the player is given a 32 by 32 grid, which they can place 4 by 4 pixel blocks on like so

now this will be of course stored in a 2D array, now for this example lets state that we have a 2D array which can hold 4 by 4

int carDesign[3][3]

the player then designs their car by placing blocks, at the end the player has something like this

0,1,1,0
1,0,0,1
1,0,0,1
0,1,1,0

now we first step through the 2D array and draw a block every time we reach a 1
so the first row would be like
no block, block, block, no block
each row element is X position 4 * element number
each column element is Y position 4 * element number

so at the end we have something like so draw on the screen



now this is awesome, this is something that I want and I can implement without a problem but here is the my problem, later on the game the player can create a 128 by 128 car, the 2D array would have a total of 1024 blocks to check, and it would have to do that for loop every frame to draw this custom car, now this would eat up tons of process as I plan to allow 8 players via network to play together, and draw 8 of these is 8192 blocks to draw every frame.

So i'm wondering if we had a 32 by 32 car design which is stored in a 2D array, could we not step though the 2D array elements once, save the outcome in a texture, then I can call stick that texture into a sprite and use that sprite, this then becomes a 1 block step compared to a large 1024 for loop to draw the same image, I am thinking I could allow the program to save the outcome as a image file and then load that up, but I would prefer if I could store the image output into a texture in the program for later use, does anyone have any ideas on who I could do that?

Thank you for reading.

Canvas.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Store a made texture by the program for future use
« Reply #1 on: December 11, 2013, 11:11:03 pm »
You can put a 1D array of unsigned char into sf::Image, or you can loop your structure once and set all pixels in image after making an image with correct size. Image you can then save to file or load into texture.
Back to C++ gamedev with SFML in May 2023

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Store a made texture by the program for future use
« Reply #2 on: December 12, 2013, 01:14:15 am »
any example code at all?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Store a made texture by the program for future use
« Reply #3 on: December 12, 2013, 08:30:44 am »
I'm not sure what the problem is, but you just need to kedp ths generated array/image/texture around, e.g. by saving it to an STL container. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

MarcusM

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Store a made texture by the program for future use
« Reply #4 on: December 12, 2013, 01:31:32 pm »
As eXpl0it3r is suggesting, you should only "draw" the car once over to a texture/rendertexture, leaving you with only one for-loop to draw the car rather than a for-loop in every single frame.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Store a made texture by the program for future use
« Reply #5 on: December 12, 2013, 06:12:40 pm »
You could also build a vertex array that contains all the blocks of the car. This is a little safer because:
- some very old (Intel) cards have poor support for render-textures
- texture size is always limited, and can be as low as 512x512 on old (Intel) cards)
Laurent Gomila - SFML developer

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: AW: Store a made texture by the program for future use
« Reply #6 on: December 12, 2013, 07:26:38 pm »
I'm not sure what the problem is, but you just need to kedp ths generated array/image/texture around, e.g. by saving it to an STL container. ;)

Ah ok that sounds perfect, ooohh to the Laurent don't worry about that, the biggest this image can be is 128 by 128 :), but may I ask eXpl0it3r how would I render it to s STL container an then re-use? A little example code would really help me out.

Once i have the array, how can i draw an image via the array and then store it to a texture? or image?
« Last Edit: December 12, 2013, 07:36:06 pm by Canvas »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Store a made texture by the program for future use
« Reply #7 on: December 12, 2013, 07:38:24 pm »
how would I render it to s STL container an then re-use? A little example code would really help me out.

You are not rendering to a STL container, you are simply storing the texture in a container. Probably something like this...

#include <vector>

std::vector<std::unique_ptr<sf::Texture>> my_textures;
// now here create, draw, and display the render texture
sf::RenderTexture rt;
// add the finished texture to our vector
my_textures.push_back(std::unique_ptr<sf::Texture>(new sf::Texture(rt.getTexture())));
// do whatever with the stored texture
 
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Store a made texture by the program for future use
« Reply #8 on: December 12, 2013, 07:47:10 pm »
the texture will be generated by the program, it won't be loaded at all via a file, it will use sprites to create the texture, and then i will store that texture

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Store a made texture by the program for future use
« Reply #9 on: December 12, 2013, 07:53:23 pm »
the texture will be generated by the program, it won't be loaded at all via a file, it will use sprites to create the texture, and then i will store that texture

The code above applies either way no matter if you load textures from the HDD or if you generate them at run-time  ;)
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Store a made texture by the program for future use
« Reply #10 on: December 13, 2013, 01:30:44 am »
Ok this is what I have

global variables declared
sf::Sprite spriteTile;
sf::Texture myTexture;
sf::Sprite myShiper;
sf::View view;
sf::RenderTexture rt;

I then set the tile image like so, and then do the part you spoke about Zsbzsb
spriteTile.setTexture(tile);
spriteTile.setPosition(100,100);
rt.draw(spriteTile);
myTexture = rt.getTexture();
myShiper.setTexture(myTexture);
myShiper.setPosition(100,100);
 

then in my draw function I call this

window.draw(myShiper);
 

and nothing is draw :(, what is up with that?

UPDATE ---

This is my whole code

#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
#include "SFML/System.hpp"
#include <iostream>

sf::Sprite spriteTile;
sf::Texture myTexture;
sf::Texture texture;
sf::Sprite myShiper;
sf::View view;
sf::RenderTexture rt;

class entity
{
public :
        float x;
        float y;
        sf::Sprite sprite;
};

int main()
{
        bool play = true;

        sf::RenderWindow window(sf::VideoMode(800,600), "window", sf::Style::Titlebar | sf::Style::Close);
        window.setFramerateLimit(60);
        window.setVerticalSyncEnabled(true);

        texture.loadFromFile("Images/tile1.png");
        spriteTile.setTexture(texture);
        spriteTile.setPosition(0,0);
        rt.draw(spriteTile);
        myTexture = rt.getTexture();
        myShiper.setTexture(myTexture);
        myShiper.setPosition(100,100);

        while( play )
        {
                sf::Event e;
                while(window.pollEvent(e))
                {
                        if(e.type == sf::Event::Closed)
                        {
                                window.close();
                                play = false;
                        }
                }

                window.clear(sf::Color(0,0,0));
                window.draw(spriteTile);
                window.draw(myShiper);
                window.display();
        }
}
 

Now in the top left corner I can see my sprite, but when I inform my myShiper to draw nothing is drawn, can anyone see why?
« Last Edit: December 13, 2013, 08:59:33 pm by Canvas »

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Store a made texture by the program for future use
« Reply #11 on: December 13, 2013, 09:09:47 pm »
I have now it drawing by using this code

        rt.create(100,100,false);
        rt.draw(spriteTile);
 

but the image seems to be flipped, like it has been rotated by a negative 90 degrees

this is the image output


Nvm I have fixed it using this code

        rt.create(100,100,false);
        rt.draw(spriteTile);
        rt.display();
 
« Last Edit: December 13, 2013, 09:14:18 pm by Canvas »