Search code examples
javalwjgl

Best way to render and edit large images each frame?


I am running into a little snag here. Me and a friend are making a 2D game for fun. We are trying to implement a "fog of war" that will restrict what the play sees so they can't see around corners. Though, I am trying to figure out the best way to make a "shadow map" that pixels can be changed each frame as the player moves. The shadow map at times is a large as 1024x1024. We are using LWJGL and Slick.

Is there any efficient method of editing and rendering a large image each frame?

I have looked into other Fog of war questions on this site but none seem to mention how to do the actual rendering and editing. I have tried using Slick's ImageBuffer, though, the only way I can find to get the image for rendering creates a new Image object each frame causing huge memory leaks even when the old Image object is disposed.


Solution

  • I wanted to give this old question a kick and hopefully solve the question for the OP and anyone else that happens across this topic. Having recently come to the same problem I want to outline my solution. Please note, although I am using a 2D game engine (that I created) I did not use OpenGL or JOGL, so my drawing methods were fairly specific and not generic in this case. Your specific answer would have to be adapted to match the system you are using.

    In my case I have tiled maps, generally X by Y in size. With a visibility set on the character (e.g. visibilityRange = visionLength).

    I should note as well - all my "vision lines" go from the center of the character.

    Initial setup was easy, simply create a fog of war outside the range of the range of the character. This created a nice circle around my character. From there I would locate all "vision blockers" within the vision radius. Map two lines (one to the left-most vision side, and the right-most) and then obscure everything behind it. Anything that was partially hit I would do a "half-vision" fog of war. Here is a rough example...

    p = player, X = vision blocker, L = low vision, N = no vision

    P     X N N N
          L L L N
    X X X X L L N
    

    Hope this helps any game developers that stumble across this post.