<p>Have you considered using <a href="https://www.libsdl.org" rel="nofollow noreferrer">SDL 2</a>?</p>
<p>My use-case is basically identical to yours, and I've found this approach to work really well. They have a really nice C (and C++) API for drawing framebuffers to a window. It's also platform-agnostic, so you can share your implementation fairly easily between macOS, Linux and Windows.
If you want to see an example, you can go check out <a href="https://github.com/vkoskiv/c-ray/blob/master/src/utils/ui.c" rel="nofollow noreferrer">this</a> module here on GitHub. I do warn you though, while this implementation <em>works</em>, and I can easily run it at 60-120FPS, it likely does use more CPU than a fully optimised implementation.
Specifically observe the <code>initDisplay()</code> and <code>drawWindow()</code> functions.</p>

CC BY-SA 4.0
2020-07-02T00:07:50.250Z
Sorry for the late answer but I just could not find the time to try your suggestion.
LittlePilgrim
CC BY-SA 4.0
2020-07-02T00:22:03.047Z
I've never used SDL2 before, but as @PaulHK has also pointed out it's very easy to put together a prototype.
With streaming textures the results were quite good. I've tried both C++ and a C# wrapper and the difference is insignificant. I could to reach 1000fps at 640x400 by generating a simple pattern pixel by pixel. Creating a complex image is slower of course, but SDL and texture streaming won't be a bottleneck for sure.
Your code helped me a lot, thanks! (BTW, awesome project!)
LittlePilgrim
CC BY-SA 4.0
2020-06-19T11:38:22.677Z
2020-06-19T11:38:22.677Z
vkoskiv
<p>I'm planning to write a small software rendering engine (before anyone asks, it's kind of a learning/scientific experiment for me).</p>

<p>Before writing any code, I'm already stuck at the very first step: how to display my framebuffer on the screen? What I'm planning to do is to render into an in-memory buffer e.g. 320x200 24-bit RGB format. Then of course I need to display the results on the screen; full-screen or in a window.</p>

<p>I know that similar questions already exists here: </p>

<ul>
<li><a href="https://computergraphics.stackexchange.com/questions/4648/how-can-i-send-custom-bitmap-to-frame-buffer">How can I send custom bitmap to frame buffer?</a></li>
<li><a href="https://computergraphics.stackexchange.com/questions/9923/how-are-digital-painting-canvas-rendered">How are digital painting canvas rendered?</a></li>
</ul>

<p>The difference is that I'd like to update the image frequently, with a decent framerate like 50FPS.</p>

<p>It seems one possibility would be OpenGL framebuffer objects and/or buffer streaming. I'm not sure I should go that way or is it an overkill for this use case?
I've also considered displaying a simple rectangle with a texture I would update each frame, but I'm not sure about the performance (I guess transferring a few megabytes of data each frame shouldn't be a problem though).
Several years earlier I've experimented with GDI+ and WPF, but the performance was nowhere near what I wanted to achieve. Unfortunately I don't have the source code any more, but I'd admit that my approach was maybe completely wrong back then.</p>

<p>I'm not expecting source code or a ready solution, but rather some directions: what functionality to use, I'd be happy to read articles or a tutorial about the topic.</p>

<p>I was considering OpenGL first, but I'm fine with DirectX too. Other suggestions are welcome. I'm planning to use C/C++ or C#, maybe Python for the implementation.</p>

<p>I'm sure there must be an easy solution, but I admit I'm a complete noob when it comes to graphics programming. I guess it's just about finding an effective way to copy data from RAM to GPU memory. I was wondering how retro emulators (e.g. Vice) or fantasy consoles (e.g. Pico-8) are solving this problem?</p>

CC BY-SA 4.0
2020-06-18T04:59:09.610Z
I would suggest using an OpenGL texture and using glTexImage2D to update the contents. Then you can render it as a single full screen quad. That would be straight forward enough to implement and performance should be good. If you don't already have it covered I would recommend SDL to get up an running quickly, that will take care of window setup for you.
PaulHK
<rendering><real-time><frame-buffer>
How to display an in-memory framebuffer
LittlePilgrim
# How to display an in-memory framebuffer

(Asked by: username_0 on 2020-06-17)

I'm planning to write a small software rendering engine (before anyone asks, it's kind of a learning/scientific experiment for me).

Before writing any code, I'm already stuck at the very first step: how to display my framebuffer on the screen? What I'm planning to do is to render into an in-memory buffer e.g. 320x200 24-bit RGB format. Then of course I need to display the results on the screen; full-screen or in a window.

I know that similar questions already exists here:

* [How can I send custom bitmap to frame buffer?](https://computergraphics.stackexchange.com/questions/4648/how-can-i-send-custom-bitmap-to-frame-buffer)
* [How are digital painting canvas rendered?](https://computergraphics.stackexchange.com/questions/9923/how-are-digital-painting-canvas-rendered)

The difference is that I'd like to update the image frequently, with a decent framerate like 50FPS.

It seems one possibility would be OpenGL framebuffer objects and/or buffer streaming. I'm not sure I should go that way or is it an overkill for this use case?
I've also considered displaying a simple rectangle with a texture I would update each frame, but I'm not sure about the performance (I guess transferring a few megabytes of data each frame shouldn't be a problem though).
Several years earlier I've experimented with GDI+ and WPF, but the performance was nowhere near what I wanted to achieve. Unfortunately I don't have the source code any more, but I'd admit that my approach was maybe completely wrong back then.

I'm not expecting source code or a ready solution, but rather some directions: what functionality to use, I'd be happy to read articles or a tutorial about the topic.

I was considering OpenGL first, but I'm fine with DirectX too. Other suggestions are welcome. I'm planning to use C/C++ or C#, maybe Python for the implementation.

I'm sure there must be an easy solution, but I admit I'm a complete noob when it comes to graphics programming. I guess it's just about finding an effective way to copy data from RAM to GPU memory. I was wondering how retro emulators (e.g. Vice) or fantasy consoles (e.g. Pico-8) are solving this problem?

**Tags:** rendering, real-time, frame-buffer

> **Comments:**
> **username_1**: I would suggest using an OpenGL texture and using glTexImage2D to update the contents. Then you can render it as a single full screen quad. That would be straight forward enough to implement and performance should be good. If you don't already have it covered I would recommend SDL to get up an running quickly, that will take care of window setup for you.  

---

## 1 answers

### Answer by username_2 on 2020-06-19. Score: 0 | Accepted answer

Have you considered using [SDL 2](https://www.libsdl.org)?

My use-case is basically identical to yours, and I've found this approach to work really well. They have a really nice C (and C++) API for drawing framebuffers to a window. It's also platform-agnostic, so you can share your implementation fairly easily between macOS, Linux and Windows.
If you want to see an example, you can go check out [this](https://github.com/username_2/c-ray/blob/master/src/utils/ui.c) module here on GitHub. I do warn you though, while this implementation *works*, and I can easily run it at 60-120FPS, it likely does use more CPU than a fully optimised implementation.
Specifically observe the `initDisplay()` and `drawWindow()` functions.

> **Comments:**
> **username_0**: Sorry for the late answer but I just could not find the time to try your suggestion.  
> **username_0**: I've never used SDL2 before, but as @username_1 has also pointed out it's very easy to put together a prototype. With streaming textures the results were quite good. I've tried both C++ and a C# wrapper and the difference is insignificant. I could to reach 1000fps at 640x400 by generating a simple pattern pixel by pixel. Creating a complex image is slower of course, but SDL and texture streaming won't be a bottleneck for sure. Your code helped me a lot, thanks! (BTW, awesome project!)  

---

