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

Author Topic: RenderTexture and editting pixels Help  (Read 2269 times)

0 Members and 1 Guest are viewing this topic.

SFMLNewGuy

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
RenderTexture and editting pixels Help
« on: November 22, 2020, 12:37:32 am »
Hello,

So I've been translating projects from a great YouTuber named Javidx9, to SFML. This project
"Line Of Sight" is the project I'm translating to SFML. At the end of the video, he uses a light_cast.png sprite (cool effect) that follows the mouse, and pixels are used to put the shadow over it.

He defines the variables like this (directly from the source), and this has been the most difficult part figuring out what is a sf::Image, sf::Sprite, sf::Texture, sf::RenderTexture.

olc::Sprite *sprLightCast;
olc::Sprite *buffLightRay;
olc::Sprite *buffLightTex;

On initialization:
sprLightCast = new olc::Sprite("light_cast.png");

// Create some screen-sized off-screen buffers for lighting effect
buffLightTex = new olc::Sprite(ScreenWidth(), ScreenHeight());
buffLightRay = new olc::Sprite(ScreenWidth(), ScreenHeight());
return true;

On userUpdate, which also draws. https://github.com/OneLoneCoder/olcPixelGameEngine/blob/master/Videos/OneLoneCoder_PGE_ShadowCasting2D.cpp#L434   (lines 434 to 444)
         
...
// Clear offscreen buffer for sprite
SetDrawTarget(buffLightTex);
Clear(olc::BLACK);

// Draw "Radial Light" sprite to offscreen buffer, centered around
// source location (the mouse coordinates, buffer is 512x512)
DrawSprite(fSourceX - 255, fSourceY - 255, sprLightCast);

// Clear offsecreen buffer for rays
SetDrawTarget(buffLightRay);
Clear(olc::BLANK);

On userUpdate, which also draws. https://github.com/OneLoneCoder/olcPixelGameEngine/blob/master/Videos/OneLoneCoder_PGE_ShadowCasting2D.cpp#L473 (473-477)
        ...
// Wherever rays exist in ray sprite, copy over radial light sprite pixels
SetDrawTarget(nullptr);
for (int x = 0; x < ScreenWidth(); x++)
        for (int y = 0; y < ScreenHeight(); y++)
                if (buffLightRay->GetPixel(x, y).r > 0)
                        Draw(x, y, buffLightTex->GetPixel(x, y));

So defining these variables correctly has been my problem. It looks like buffLightRay would be a sf::Image and buffLightText, but I'm getting hung up on the last portion where I need to change the pixel. I believe his draw() function is drawing a single pixel, which I have an equivalent function. The reason I thought I needed a sf::RenderTexture was that he mentioned an offscreen drawing. But, I don't think I need that?

Hope that makes sense, thank you have a great day.
« Last Edit: November 22, 2020, 12:39:56 am by SFMLNewGuy »

splavacado100

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: RenderTexture and editting pixels Help
« Reply #1 on: December 04, 2020, 06:15:20 pm »
The use of sf::RenderTexture is for when you have a complex image to display that doesn't need to be updated every frame. In the case of the line-of-sight lighting system, it really only needs to change when you move your mouse, otherwise, it would calculate to be the same image. The offscreen drawing would be used to render the scene, but only when there is a mouse moved event. To draw it on screen, you would package it up into a sprite using the offscreen drawing as the texture, so you can update the image onscreen every frame. Even though it would sometimes be the same image, it's a lot less taxing to draw a single prerendered sprite than it is to rerender the scene every frame, as opposed to every mouse move. I hope this answers your question! :D

 

anything