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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Antonio9227

Pages: [1]
1
DotNet / Memmory leak if not disposing assets
« on: December 26, 2023, 11:29:20 pm »
This is totally my fault but I decided to write a post in here just in case somebody comes across this in the future.

I needed a small app that loads in a bunch of pictures (like..thousands of them) and does some very basic editing (such as rescaling, adding text, stuff like that). Decided to use SFML since I've used it a lot in the past.

Guess I got used to the Garbage Collection of .NET and didn't even think about this until my PC froze.

BEWARE! GarbageCollection does NOT work automatically on Textures (and I assume other stuff as well) unless you call the Dispose method. It doesn't seem to matter that you're technically no longer using the texture and you no longer have any reference to it.


// Will cause a memmory leak
public void DoStuff(string[] texturePaths) {
  foreach(var texturePath in texturePaths) {
    var texture = new Texture(texturePath);
    DoOtherStuff(texture);
  }
}


// memmory gets cleared on GC.Collect()
public void DoStuffSafely(string[] texturePaths) {
  foreach(var texturePath in texturePaths) {
    var texture = new Texture(texturePath);
    DoOtherStuff(texture);
    texture.Dispose(); // this. do this when you no longer need the texture
  }
}
 

2
SFML projects / Some lighting stuff (+ how to implement it yourself)
« on: June 30, 2018, 12:01:18 am »
Hello there! So a couple of days ago I got really curious about how I could simulate some 2D lights in SFML. After a bit of work, this is what I came up with.


Click here for a video(I didn't realize fraps recorded my background music too)
Bonus video of early testing

I kinda cheated because the scene itself was not made by me, it's just some screenshot I found on the internet. (which you can see by clicking here)
This is far from finished but I think it looks kinda cool. I know there are lots of better projects, such as Let there be light, that are probably far more optimized and work better overall.


Here you can see the walls I set for this specific scene.

I'm pretty sure there are things that could be done better so let me know if you have any suggestions/criticisms.

So how does it work? There are three steps to generate a lightmap (which is basically more or less a texture generated depending on the lights), then we take the lightmap and draw it over our scene using the multiply blending mode. By using this blending mode each pixel that is already draw will be multiplied with the pixel from the lightmap that is directly above it. You can try using Photoshop or some other picture editing software to get a feeling of how this blendmode works.

To generate the lightmap we have to:

1. Generate/calculate the light mesh
First of all we need some kind of vertex array that describes the span of our light. To do this we have a couple of options. This is the hardest part you'll have to think about, but don't worry, it's not nearly as hard as you might think.
We could for example take a circle with a radius of how far we want the light to reach and do something called "polygon clipping" to clip away any object that is not supposed to be illuminated. 
My approach however uses something called raytracing. In a nutshell, we imagine firing a ray in a certain direction to see if it hits anything on the way. I imagine this could get resource intensive really fast but I was too lay to figure out polygon clipping and I ways already familiarized with raytracing.
To learn how you can use raytracing to generate your mesh you can follow THIS awesome interactive article that shows you everything you need to know, step by step, with interactive animations!

At the end, we're left with something like this:

Kinda ugly, but it's the exact same lightmap you saw drawn in the video (except the light controlled by my cursor).

Notice that the lights have a maximum range after which they stop propagating.

To draw colored light all you have to do is draw the polygon a certain color. In a scene with multiple lights I recommend using the add blend mode when you're drawing the light meshes. You can see in the picture above that where 2 lights intersect the colors get blended rather than overlapping.

If you want, you can add a bit of ambient light by clearing the screen with something other than black when rendering the lightmap. If you leave it completely black only what's illuminated by your lights will be visible. Choosing a gray color will make the entire scene visible, but darker where the lights don't illuminate. Finally choosing a different color will give your scene a slight tint. You can use this to make a scene feel warmer or colder or whatever your creativity demands.

2. Adjust the intensity over distance

No light in the real world would look like that because the intensity (the color) of the mesh is the same everywhere. Naturally you would expect a light to be at maximum intensity at the source and then slowly lose intensity as it spreads out more and more into space. Since we're going to multiply the lightmap with the rest of the scene at the end, intensity is pretty much the same as color at this point.

To do this you can either set different color for each vertex in the mesh or you could use shaders and let your graphics card work a bit.
I chose to use a fragment shader just because it felt easier to write at the time. It's very simple and you can write it in less than 10 lines of code. Basically you have to slowly fade out the color of your light the further you get from the light source.

Just by doing this your lightmap should start looking like this:


It kinda looks a bit soft and fuzzy because of the way I coded it, this can be improved a lot.

3. Blur
Right now our light map has the right shape and colors but it still looks kinda rough so I blur them a bit with another shader that applies some gaussian blur.

Congratulations! Your lightmap texture is complete. Now all you have to do is draw it over your scene, using the multiply blending mode.


Penumbra
If you looked really carefully at the screenshots/video you may have noticed that the main light I'm controlling with my cursor produces penumbra. Usually a point light only produces an umbra (shadow) while something like the sun also produces penumbra. (if you have no ieda what the heck I'm rambling about, just google 'penumbra' and the first or second image should be self explanatory). I managed to get this effect by spawning a ring of point lights, this kinda approximates the way light would be emitted from the surface of an object.

Performance issues and considerations
The part that will stress your computer the most is generating the mesh for each light. To avoid this you can generate the mesh only when needed (when the light is created, moved, has changed colors, etc) and when some object that is in the rage of the light has changed (has moved, rotated, etc).
I would say updating lots of lights in the same frame is a bad idea with this method and should be avoided. One cheap way I got away with it was decreasing the number of light updates per frame and scheduling the rest of them for the next few frames.

In my experience, since my code is not really optimized and it's single threaded, updating 10 lights every frame in the scene you saw above caused a noticeable FPS dip and while restricting the number of light updates per frame helped a lot, I would say there's huge space for improvements.
I'm sure this is caused by my sloppy code and you can probably get away without any performance issues if you optimize it enough.

Multi threading also might solve this problem but I'll have to rewrite a bit of the code for that to work so maybe someday I'll do a followup.

The code
I'm not going to share the code I wrote because in my opinion it's not ready to be used. It's more or less just a demo I made to see what kind of effect I can get without much work or complicated maths. I'm not proud of the performance side either and I'm sure it can be improved a lot.

If you have any suggestions or any kid of comments about my approach feel free to leave a reply below and I'll check it out tomorrow.

3
Graphics / Soft Light blendmode
« on: March 18, 2015, 11:00:25 am »
Hello guys, now this might sound like a noobie problem, but I would like to ask how can I implement the Soft light blendmode (just like in photoshop) to a SFML application. I'm thinking of using a fragment shader but unfortunately my GLSL skills are not that good.

I found this formula on wikipedia, it's not quite the way photoshop does it but it should work just fine.


Thanks in advance ^_^.

4
Graphics / [SOLVED] Crash when draw() is used
« on: January 28, 2015, 08:49:01 am »
Hello, I am a object oriented programming newbie and I've encountered a weird problem while trying to make a level class. My application crashes every time I try to draw a level object, here is the code I'm using:
virtual void draw(sf::RenderTarget &target,sf::RenderStates states) const
{
    states.transform *=getTransform();
    states.texture = m_texture;
    target.draw(m_vertices,states); //Crashes here
}
 

During debug I receive an SIGSEGV segmentation fault signal when that specific line is executed. If I disable it the program runs as expected. m_vertices is a sf::VertexArray and m_texture is a sf::Texture pointer.
Also it doesn't matter if I constructed a mesh inside the vertex array or not, it still crashes. I've used the exact same piece of code for another class and it works just fine.

I really don't know what the problem could be and some help would be appreciated. 
Thanks in advance, regards Antonio.

5
Window / Keyboard question
« on: December 03, 2013, 03:55:44 pm »
Hello guys, I am still working on my first SFML game and I have another question for you today.This time I have a problem with the keyboard.

My code looks something like this:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) || sf::Keyboard::isKeyPressed(sf::Keyboard::A))
//Move Left
It works as supposed, the object moves to left, but it also works when I'm outside of the render window. For example if I leave the game open and go write some text somewhere, every time I use "ASDW" the object moves. It's like sensing the keys globally.

Well since I test if the key is pressed, this make total sense (the game sits back and tests if keys are pressed).

 Is there any better way of doing this?

6
Graphics / View ports
« on: November 12, 2013, 04:24:02 pm »
I am trying to add a minimap to my game. I already used views for moving cameras, but I can't figure out how to use viewports. I know what they are supposed to do, but I still can't figure it out.

Here is what I was trying to do. I have "view" which is what I'm using for the game and "minimap" which should be the minimap.
view.setViewport(sf::FloatRect(0,0,1,1));
sf::View minimap;
minimap.reset(sf::FloatRect(0,0,level.x*56,level.y*56));
minimap.setViewport(sf::FloatRect(0.75f, 0, 0.25f, 0.25f));
They are working separately, if I bind one of them to my window ( window.setView(view); ) it works, but I can't figure out how to display them both. I searched on google but I couldn't find anything usefull.

If you can help me, leave a reply below.

7
Graphics / Applying contextual settings at runtime
« on: October 13, 2013, 11:23:45 am »
I want to change the antialiasing level during runtime for a sf::RenderWindow.
How can I do it?

8
Graphics / [SOLVED]Check for sprite collision
« on: October 02, 2013, 03:51:29 pm »
Is there any way to check if 2 sprites are collided?

Pages: [1]
anything