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

Author Topic: Images, Textures and Sprites - quick question  (Read 1106 times)

0 Members and 1 Guest are viewing this topic.

StickyCube

  • Newbie
  • *
  • Posts: 2
    • View Profile
Images, Textures and Sprites - quick question
« on: May 17, 2013, 11:06:45 pm »
Hi guys, I'm fairly new to c++ and I've been using the SFML 2.0 API for about a week now. The brilliant documentation has got me quite far and yesterday I started trying to build a little Mandelbrot Set explorer. All I have done so far is to get the algorithm to draw the set and send it to a renderwindow (I include a picture of the results). The next step is to start to package it up into a more object oriented framework and add zoom and scroll features, here I'm stumbling at the first hurdle. The code which sends the image to the renderwindow looks something like this at the moment:

Code: [Select]
sf::RenderWindow window( sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "MandelBrot Set!");
sf::Texture texture;
sf::Sprite sprite;
sf::image image(SCREEN_WIDTH, SCREEN_HEIGHT, sf::Color::White);

for(unsigned i=0; i<SCREEN_WIDTH; ++i)
{
  for(unsigned j=0; j<SCREEN_HEIGHT; ++i)
  {
    // calculate color for each pixel here using image.SetPixel(i,j,sf::Color)
  }
}

image.SaveToFile("some_directory.png");
texture.LoadFromFile("some_directory.png");
sprite.SetTexture(texture);

window.draw(sprite);
window.display();

//then some display loop

So my question is: Do I have to save the image to disk before i can access it with a texture? I cant see any way around it from just reading the stuff in the documentation. I never used 1.6 but I gather from various tutorials and such that this used to be possible by passing an image directly to a sprite, the image seems to be more abstract in 2.0.

Thanks.

Edit: Forgot the image..
http://postimg.org/image/w3azfoju3/
« Last Edit: May 17, 2013, 11:10:51 pm by StickyCube »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Images, Textures and Sprites - quick question
« Reply #1 on: May 18, 2013, 12:04:14 am »
With SFML 2.0 you can use tex.loadFromImage(img). :)
Also check out the nice tutorials on the graphics module.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything