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.


Messages - Antonio9227

Pages: [1] 2
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
Graphics / Re: Rotation convex shape (ver 2.2).
« on: October 17, 2018, 04:13:33 pm »
Solution 1:
Code: [Select]
sf::ConvexShape polygon;
polygon.setPointCount(3);
polygon.setPoint(0, sf::Vector2f(0, 0);
polygon.setPoint(1, sf::Vector2f(-5,-5));
polygon.setPoint(2, sf::Vector2f(+5,-5));

polygon.setPosition(400,400);
polygon.rotate(10);

Solution 2:
Code: [Select]
x = 400;
y = 400;
sf::ConvexShape polygon;
polygon.setPointCount(3);
polygon.setPoint(0, sf::Vector2f(x, y);
polygon.setPoint(1, sf::Vector2f(x-5, y-5));
polygon.setPoint(2, sf::Vector2f(x+5, y-5));

polygon.setOrigin(x,y);

The way you're doing it your convex shape has the position (0,0) and when you apply rotation, it pivots around that origin point.
You can either reset the origin point (2nd example) or construct the shape in local space and move it to (400,400) like in the first example (which I prefer).



Longer explanation:
When you build your polygon with setPoint you're building it in local space. In local space you should always define how your object looks without any rotation or translation applied; in this case you should not set the points . When the polygon is being drawn (moved to world space), it is translated and rotated according to it's transform.
I know this is a pretty crappy explanation so I googled a better one for you. This one is for unity but it's pretty much the same thing in SFML too

Edit: basically what Laurent just said

3
Graphics / Re: How to reload a texture without adding a driver overhead?
« on: October 16, 2018, 09:00:43 pm »
I can think of 2 ways of dealing with this; the first one is having an array of textures and switching between them, the second one is having just 1 texture containing multiple sprites.

[Array/Dictionary]
One way is to load all the textures you need when your program starts (or when you load a specific scene), then when you need to change the texture for a specific sprite just call the setTexture method with the new texture as a parameter. In this way your program won't have to access the HDD to load stuff.

[Sprite atlas]
Another way is to construct a sprite atlas. A sprite atlas is a pretty big image file that contains all the sprites you need. Then you can call the setTetureRect method of your sprite to change what part of the image it will use when drawn. While it's much easier to do this when all your sprites are the same size, it is 100% possible to do this with sprites that have different sizes.
Alongside with your atlas.png you will need a file that tells your program the texture rect of each sprite you want to use. You can hardcode this but I recommend not to because it's easier to debug and add new stuff later on like this.

Here's a sprite atlas I randomly found on the internet. As you can see it has sprites of all sizes.
(click to show/hide)

There are also programs that input individual image files and generate an optimal atlas (and also output all texture rects)

4
Audio / Re: Latency when starting recording
« on: October 16, 2018, 04:13:41 pm »
This is just a guess but do you have any power saving options enabled on your device?

I had a similar problem with an old laptop where really short sounds (0.1 seconds) would not play because it had it's sound card disabled or in a low powered state. By the time my PC woke up the sound card, the actual sound I was trying to play was long over and it would seem as the sound itself was not playing.
By playing something on really low volume in the background my sound had no problems in playing on time and as intended.

You could test this by using your microphone to record something in audacity (to keep the device awake) and then run your program and see if anything changes.

If it works, you can disable the power saving stuff from DeviceManager > your device > uncheck 'allow the computer to turn off this device to save power'

5
DotNet / Re: Text render issue
« on: October 11, 2018, 03:53:16 pm »
Can you post a snippet of your code please? I haven't experienced anything like this when working with text objects and it seems interesting.

6
General / Re: resolution independant movement?
« on: October 10, 2018, 02:01:30 pm »
First make sure you either set vsync on or set a framerate limit such as 60fps.
Code: [Select]
window.setFramerateLimit(60);

If the window is smaller (and thus the resolution) it will render much faster because overall your GPU has less pixels to deal with. (this is just a hunch, I haven't tested it)

You should also think about using views and view ports. This SFML tutorial will explain better than I ever could in a short post.

7
Audio / Re: Trouble with openFromfile
« on: October 04, 2018, 01:10:06 pm »
One thing that I noticed right away is that you call the play method each frame. It's enough to call it once before the game loop and the music will continue playing.

I also recommend using window.setFramerateLimit(60); because right now your program will try to run as fast as possible, getting you hundreds of FPS but also consuming all of the available processing power.

Code: [Select]
sf::Music mymusic;
mymusic.openFromFile("somefile.ogg");
mymusic.play();

window.setFramerateLimit(60);

while(window.isOpen())
{
    //draw and update stuff
}

Otherwise make sure your environment is set up properly and you copied over the necessary dll files (in the same folder as your exe). I got similar problems in the past by improperly setting up Visual Studio and I kept getting similar errors

8
SFML projects / Re: Isometric tile map
« on: July 15, 2018, 01:41:40 am »
Good job, man.
It looks really good, perhaps you can use it for some kind of game.

9
Something REALLY weird happened yesterday.

I had some free time so I decided to compile this for linux and I noticed it was running smoother than usual. So I looked over the FPS and for some reason it had HUGE improvements.

I use the same machine in a dual boot configuration and I compiled exactly the same code, yet somehow it somehow manages to work way faster.

Usually on windows this got an average of 1700FPS when no light updates occurred, which doesn't sound that bad but when updating all the lights in the scene it dropped to about 30FPS (I have around 12 lights). On linux on the other hand, the average FPS when not updating was around 3000FPS and even when updating all the lights in the same frame it still managed to only drop to 2300FPS (which would be expected when you only update such a small amount of lights)

It only dropped to 30FPS when updating 300 lights at the same time. This blows my mind and I have no clue what might have caused it.

Maybe it's in the way the code got compiled or something... Both debug and release versions improved their FPS.

Any idea why or how this might have happened?

10
SFML projects / Re: Squatbot: A Mobile Platformer
« on: June 30, 2018, 09:42:21 pm »
The character movement looks really smooth, kinda reminds me of Super Meat Boy. Good job!

11
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.


(I didn't realize fraps recorded my background music too)


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.

12
General / Re: Exporting a final project
« on: May 04, 2018, 02:39:12 am »
Probably the first step is to organize your resources in folders (eg: Data\Graphics\mySprite.png, Data\Sounds\pop.wa, etc). This is probably the easiest and straight forward way of managing your resource files.

Here are step by step instructions on how to link to the static version of SFML:
Instructions for CodeBlocks
Instructions for VisualStudio

The short answer is you have to pre-define "SFML_STATIC" and add the "-s" suffix to your linked libraries ("sfml-graphics" will become "sfml-graphics-s"), but you can just follow the instructions in the links above.

If you use the audio module from SFML you will still have to copy "openal32.dll" (which you can find in your SFML folder\bin) in the same folder as your exe file.

That's pretty much it. Your final folder should contain the exe file, openal32.dll if you use sound and your resource folders. It should run perfectly on other PCs too.

13
Graphics / Re: texture.update() problem
« on: May 27, 2015, 02:22:24 pm »
Hi JollyGent!
I've looked over your code and I think I know what's causing the problem. Remember that the code you write inside while(window.isOpen()) is repeated until the window is no longer opened. Basically your program loads the image file over and over again while the window is opened.
Usually it's a good idea to load all the textures you need (textures/sounds/etc) at the beginging of your program. Once loaded, the textures are kept in the memory and you don't need to load them again every time you need to use them. In this case you should load the textures before the while loop.

What I would also recommend is loading the file in a sf::Texture object like this:
sf::Texture texture;
texture.loadFromFile("Stones.png");
You don't really need to load it into an image and then transfer it to a texture. It looks much more simpler to just load them directly into a sf::Texture ^_^. If you later need to use just a part from your image as a texture, you can use the setTextureRect() method for your sprite (if you decide to use a sprite).

I hope this helps, if you need further help use the SFML documentation or come back and ask on the forum. Have a nice day!

14
Graphics / Re: Soft Light blendmode
« on: March 18, 2015, 11:08:42 am »
That was really fast, wow. Thanks ! I will look over the native blend modes and try to understand the whole concept better.

15
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 ^_^.

Pages: [1] 2