C
C#6d ago
Iarley

Flickering (/blink) bug effect on opentk

public class FrameBuffer2D
{

public FrameBuffer2D(IntPtr renderer, int width, int height)
{
sdlRenderer = renderer;
Resize(width, height);
}

public void Bind() => GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBO);
public void Unbind() => GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

public void ReadToSDLTexture()
{
byte[] pixels = new byte[Width * Height * 4];

// copia os pixels do OpenGL

GL.BindTexture(TextureTarget.Texture2D, ColorTex);
GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
GL.BindTexture(TextureTarget.Texture2D, 0);

// atualiza a SDL texture
unsafe
{
fixed (byte* p = pixels)
{
SDL.SDL_UpdateTexture(SDLTexture, IntPtr.Zero, (IntPtr)p, Width * 4);
}
}
}


public void DrawSDL(int x, int y, int w, int h)
{
SDL.SDL_Rect dst = new SDL.SDL_Rect { x = x, y = y, w = w, h = h };
SDL.SDL_RenderCopy(sdlRenderer, SDLTexture, IntPtr.Zero, ref dst);
}
}

}
public class FrameBuffer2D
{

public FrameBuffer2D(IntPtr renderer, int width, int height)
{
sdlRenderer = renderer;
Resize(width, height);
}

public void Bind() => GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBO);
public void Unbind() => GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

public void ReadToSDLTexture()
{
byte[] pixels = new byte[Width * Height * 4];

// copia os pixels do OpenGL

GL.BindTexture(TextureTarget.Texture2D, ColorTex);
GL.GetTexImage(TextureTarget.Texture2D, 0, PixelFormat.Rgba, PixelType.UnsignedByte, pixels);
GL.BindTexture(TextureTarget.Texture2D, 0);

// atualiza a SDL texture
unsafe
{
fixed (byte* p = pixels)
{
SDL.SDL_UpdateTexture(SDLTexture, IntPtr.Zero, (IntPtr)p, Width * 4);
}
}
}


public void DrawSDL(int x, int y, int w, int h)
{
SDL.SDL_Rect dst = new SDL.SDL_Rect { x = x, y = y, w = w, h = h };
SDL.SDL_RenderCopy(sdlRenderer, SDLTexture, IntPtr.Zero, ref dst);
}
}

}
Hello, I have this framebuffer class to render the game engine's scene and consequently copy it to an SDL texture (to be able to implement it within the UI that is made in SDL2), but I am experiencing flickering effects when adding a game object to the scene
58 Replies
Iarley
IarleyOP6d ago
Consequently, I also have the SpriteBatch2D class, which is responsible for rendering sprites.
Iarley
IarleyOP6d ago
public void Run()
{
while (IsRunning)
{
ProcessEvents();

// Atualiza delta/time no início do frame
Time.Update();

// Render Game World -> OpenGL FBO
framebuffer.Bind();
GL.Viewport(0, 0, framebuffer.Width, framebuffer.Height);

GL.ClearColor(0.0f, 0.0f, 0.0f, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit);

if (!SceneManager.CurrentScene.Paused)
{
SceneManager.CurrentScene.Update(Time.DeltaTime, framebuffer.Width, framebuffer.Height);
Logger.Log("Scene is Running - [" + (int)Time.FPS + "] FPS", Logger.LogLevel.Info);
}

framebuffer.Unbind();
GL.Finish();
framebuffer.ReadToSDLTexture();

// Render UI + Framebuffer (SDL)
SDL.SDL_SetRenderDrawColor(_renderer, 25, 25, 25, 255);
SDL.SDL_RenderClear(_renderer);

// primeiro update de UI (lógica), depois desenho
UIManager.Update();
UIManager.Draw(_renderer);

SDL.SDL_RenderPresent(_renderer);
SDL.SDL_RenderClear(_renderer);

}

Quit();
}
public void Run()
{
while (IsRunning)
{
ProcessEvents();

// Atualiza delta/time no início do frame
Time.Update();

// Render Game World -> OpenGL FBO
framebuffer.Bind();
GL.Viewport(0, 0, framebuffer.Width, framebuffer.Height);

GL.ClearColor(0.0f, 0.0f, 0.0f, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit);

if (!SceneManager.CurrentScene.Paused)
{
SceneManager.CurrentScene.Update(Time.DeltaTime, framebuffer.Width, framebuffer.Height);
Logger.Log("Scene is Running - [" + (int)Time.FPS + "] FPS", Logger.LogLevel.Info);
}

framebuffer.Unbind();
GL.Finish();
framebuffer.ReadToSDLTexture();

// Render UI + Framebuffer (SDL)
SDL.SDL_SetRenderDrawColor(_renderer, 25, 25, 25, 255);
SDL.SDL_RenderClear(_renderer);

// primeiro update de UI (lógica), depois desenho
UIManager.Update();
UIManager.Draw(_renderer);

SDL.SDL_RenderPresent(_renderer);
SDL.SDL_RenderClear(_renderer);

}

Quit();
}
my run method If someone can identify the error and help me fix the bug, I would be grateful. @CityPop
hugeman
hugeman6d ago
You unbind the framebuffer before reading the texture, not sure if that causes issues but maybe rearrange it? Is the flickering every other frame or is it random?
Iarley
IarleyOP6d ago
every other frame lemme try it
hugeman
hugeman6d ago
Every other frame sounds something to do with with swap buffers function but i could be wrong
Cattywampus
Cattywampus6d ago
most flickers in sdl mostly just the wrong ordering
Iarley
IarleyOP6d ago
It didn’t work, I’ve already tried several things, I’ve tried refactoring the rendering system, I’ve literally tried everything, I asked the AI for help and it didn’t work
Cattywampus
Cattywampus6d ago
the order should be : Clear Render Render present //Your timestep condition and repeat
Iarley
IarleyOP6d ago
// Atualiza delta/time no início do frame
Time.Update();

// Render Game World -> OpenGL FBO
framebuffer.Bind();
GL.Viewport(0, 0, framebuffer.Width, framebuffer.Height);

GL.ClearColor(0.0f, 0.0f, 0.0f, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit);

if (!SceneManager.CurrentScene.Paused)
{
SceneManager.CurrentScene.Update(Time.DeltaTime, framebuffer.Width, framebuffer.Height);
Logger.Log("Scene is Running - [" + (int)Time.FPS + "] FPS", Logger.LogLevel.Info);
}

framebuffer.Unbind();
GL.Finish();
framebuffer.ReadToSDLTexture();

// Render UI + Framebuffer (SDL)
SDL.SDL_SetRenderDrawColor(_renderer, 25, 25, 25, 255);
SDL.SDL_RenderClear(_renderer);

// primeiro update de UI (lógica), depois desenho
UIManager.Update();
UIManager.Draw(_renderer);

SDL.SDL_RenderPresent(_renderer);
SDL.SDL_RenderClear(_renderer);
// Atualiza delta/time no início do frame
Time.Update();

// Render Game World -> OpenGL FBO
framebuffer.Bind();
GL.Viewport(0, 0, framebuffer.Width, framebuffer.Height);

GL.ClearColor(0.0f, 0.0f, 0.0f, 1f);
GL.Clear(ClearBufferMask.ColorBufferBit);

if (!SceneManager.CurrentScene.Paused)
{
SceneManager.CurrentScene.Update(Time.DeltaTime, framebuffer.Width, framebuffer.Height);
Logger.Log("Scene is Running - [" + (int)Time.FPS + "] FPS", Logger.LogLevel.Info);
}

framebuffer.Unbind();
GL.Finish();
framebuffer.ReadToSDLTexture();

// Render UI + Framebuffer (SDL)
SDL.SDL_SetRenderDrawColor(_renderer, 25, 25, 25, 255);
SDL.SDL_RenderClear(_renderer);

// primeiro update de UI (lógica), depois desenho
UIManager.Update();
UIManager.Draw(_renderer);

SDL.SDL_RenderPresent(_renderer);
SDL.SDL_RenderClear(_renderer);
Iarley
IarleyOP6d ago
Cattywampus
Cattywampus5d ago
did you fix this already? did you check the order?
Iarley
IarleyOP5d ago
yeah, i checked the order, this still has the same error, not just for animated sprites, but also for static sprites
Iarley
IarleyOP5d ago
GitHub
Severe flickering and unstable rendering on animated sprites using ...
📄 Description I am experiencing serious rendering issues in the Luna game engine involving animated sprites and the viewport system. Observed Problems Animated sprites constantly flicker / blink Sp...
Iarley
IarleyOP5d ago
GitHub
GitHub - iarleyyyxz/Luna2D: Free and open source 2D Game Engine in C#
Free and open source 2D Game Engine in C#. Contribute to iarleyyyxz/Luna2D development by creating an account on GitHub.
Cattywampus
Cattywampus5d ago
that either render order issue or vsync.. the latter is very unlikely try toing around with the order, nothing much to do really other than debugging on your end also .. why GL.Finish there tho?
Iarley
IarleyOP5d ago
i removed it I already tried several orders, before I made a mistake, I was rendering two scenes (one in the run() method, and another in the UIViewport class) I removed it, it turned out that wasn’t the reason
Cattywampus
Cattywampus5d ago
you're using matrices or vector for transform? how's your camera setup?
Iarley
IarleyOP5d ago
Vector not set up yet just the screen coordinates for now
Cattywampus
Cattywampus5d ago
remove that UI render or whatever it is and just plain texture2d to debug things
Iarley
IarleyOP5d ago
ok I really wanted to try creating a UI in SDL, but these integration errors are driving me crazy
Cattywampus
Cattywampus5d ago
UI is a massive task just so you know, I personally if its just a fun project I'd just use Imgui.net instead
Cattywampus
Cattywampus5d ago
this is mine I made it two years ago iirc
No description
No description
Cattywampus
Cattywampus5d ago
thats SDL + ogl + Imgui.net
Iarley
IarleyOP5d ago
sdl for the window?
Cattywampus
Cattywampus5d ago
yes... the scene editor there Imgui.net
Iarley
IarleyOP5d ago
I really don't like imgui...
Cattywampus
Cattywampus5d ago
so just passing the framebuffer of OGL to imgui then back to sdl... quite simple actually
Iarley
IarleyOP5d ago
I feel trapped oh gotcha you have the source code?
Cattywampus
Cattywampus5d ago
the default is ugly, but you can do customs.. and there are tons of addons for that you can use on github
Iarley
IarleyOP5d ago
Can you name a few for me?
Cattywampus
Cattywampus5d ago
there are couple of challenges, the most painful one is the Text rendering SDL_TTF is just bad and basic
Iarley
IarleyOP5d ago
for sure
Cattywampus
Cattywampus5d ago
your other options is to use FreeType binding I prefer the latter, SDL_TTF is just garbage :when:
Iarley
IarleyOP5d ago
lol
Cattywampus
Cattywampus5d ago
or do it the easy way, use imgui.net
Iarley
IarleyOP5d ago
I will take it into consideration .? ?
Cattywampus
Cattywampus5d ago
its private for now, I'm not working on it any further yet, proably later in the future
Iarley
IarleyOP5d ago
oh okay where can i find these addons?
Cattywampus
Cattywampus5d ago
just google it :when: .. or just make your own custom in imgui.net, not that hard
Iarley
IarleyOP5d ago
The problem with ImGui is the issue of rendering custom icons.
Cattywampus
Cattywampus5d ago
hows is that a problem? as you can see in the gif above posted, I used heck tons of icons
Iarley
IarleyOP5d ago
i mean like this:
No description
Cattywampus
Cattywampus5d ago
you can do that in imgui no issue
Iarley
IarleyOP5d ago
I don't know how to do it, I've always looked into it and it seems pretty complicated .
Cattywampus
Cattywampus5d ago
well, everything is complicated when making your own game engine :when:
Iarley
IarleyOP5d ago
lol fair
Cattywampus
Cattywampus5d ago
strip out anything unnecessary here, and just render plain texture2d and see how it goes
Iarley
IarleyOP5d ago
i think i find the error... I think I'm calculating the frame size of the spritesheets wrong... the image moves as if it were rendering the entire spritesheet on the screen, and then moves until it renders the last frame
Cattywampus
Cattywampus5d ago
there you go happy debbugging :blobthumbsup: a little tip, use renderTarget so you're rendering all your texture2ds into a single texture instead of individual rendering like that, so you can have dynamic viewport for cases when detecting different screen resolutions but thats for later, now just fix your current bug
Iarley
IarleyOP5d ago
i have a spritebatch for render multiple sprites in one draw call
Cattywampus
Cattywampus5d ago
spritebatch is unrelated to rendertexture
Iarley
IarleyOP5d ago
Okay, I had misunderstood I’m going to refactor spritesheet class
Cattywampus
Cattywampus5d ago
bth how you spritebatch? sounds to me you're using SDL 2d api instead of OGL here yeah very likely, if so, your ogl is useless there mind you sdl 2d api cant do z-order by default
Iarley
IarleyOP5d ago
no no, im just using sdl to window and input
Cattywampus
Cattywampus5d ago
yeah my question was how you spritebatch, show me the snippet if you'r like or just the ogl shader
Iarley
IarleyOP5d ago
you mean the shader class or the shader src? I'm not using SDL 2D for game rendering. SDL is only used for window creation, input and (for now) the editor UI. All sprites are rendered with OpenGL through my own Renderer2D / QuadRenderer system. I currently have a simple SpriteBatch that queues sprites and draws them via OpenGL, but it's still being improved (no full batching or instancing yet). The flickering issue was caused by mixing SDL_Renderer with OpenGL and some UV problems in my SpriteSheet system. I'm now moving to a fully OpenGL-based pipeline to avoid these conflicts and implement proper batching and z-order.
using System.Numerics;

namespace Luna.g2d.Renderer
{
public static class Renderer2D
{
private static SpriteBatch2D batch = new SpriteBatch2D();

public static void Begin()
{
batch.Begin();
}

public static void Draw(Sprite2D sprite)
{
batch.DrawSprite(
sprite.Texture,
sprite.Position,
sprite.Size,
sprite.Rotation,
sprite.Color,
sprite.UV0,
sprite.UV1,
sprite.FlipX,
sprite.FlipY,
sprite.Layer
);
}

public static void End(int screenW, int screenH)
{
batch.End(screenW, screenH);
}
}
}
using System.Numerics;

namespace Luna.g2d.Renderer
{
public static class Renderer2D
{
private static SpriteBatch2D batch = new SpriteBatch2D();

public static void Begin()
{
batch.Begin();
}

public static void Draw(Sprite2D sprite)
{
batch.DrawSprite(
sprite.Texture,
sprite.Position,
sprite.Size,
sprite.Rotation,
sprite.Color,
sprite.UV0,
sprite.UV1,
sprite.FlipX,
sprite.FlipY,
sprite.Layer
);
}

public static void End(int screenW, int screenH)
{
batch.End(screenW, screenH);
}
}
}
Cattywampus
Cattywampus5d ago
ah nice then 👍
Iarley
IarleyOP5d ago
is it okay? GL.TexImage2D( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, image.Width, image.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, image.Data );

Did you find this page helpful?