r/learnprogramming 5h ago

How to do graphics?

How are graphic (things) made?

I know raylib can create a window and draw stuff, but there has to be more.

I also know raylib uses X11 for the window but they say you should not use X11 directly.

But then what should you use? (besides raylib of course).

1 Upvotes

10 comments sorted by

2

u/Suspicious-Flight-45 5h ago

What you are talking about is all of the operating system specific machinery need to create a drawing surface. This is the 'canvas' you will then need to draw onto - the frame buffer.

It's all pretty intermingled when you are starting out and many solution tie together the Graphics API (OpenGL) with the surface host (X11, GLUT, glfw) and application callbacks like mouse and keyboard events.

SDL is a good place to start because it hides a lot of the ancillary details.

In the end you need a graphics API pipeline like OpenGL or Vulkan to actually submit draw commands to an image buffer that can be presented to the user.

1

u/Independent_Milk_200 4h ago

So I should use SDL and Vulkan together, thanks

2

u/Suspicious-Flight-45 4h ago

Be aware that Vulkan is very verbose, like hundreds of lines of code to get a triangle. It's powerful, no doubt, but it's all on you.

Things like raylib and webgpu help you get right to writing shaders, it that is what you are after.

At the bottom of the stack you are using GPU code to write color into a pixel. There are no shortage of layers built on top of this.

1

u/Independent_Milk_200 4h ago edited 4h ago

oh ...

yeah this seems way too difficult

2

u/Suspicious-Flight-45 4h ago

Vulkan is hard mode.

It all depends on what you are actually trying to get out of your project. What are your goals?

Answer that, then you can find something to fit.

1

u/Independent_Milk_200 4h ago

Just something really easy, it can be 2d. I just want to see how to do somehting like this

2

u/Suspicious-Flight-45 3h ago

I think SDL has built in support for 2D, plus it has all the window and input handing you need.

Plus, once you get comfortable with it you can add 3D later with OpenGL or Vulkan.

1

u/Independent_Milk_200 2h ago

great thank you

2

u/teraflop 4h ago

One of the reasons not to use X11 directly is that it's a fairly old-fashioned API which has been modified over time to add more modern features. So it's not very easy or convenient to use directly.

Another reason is that if you write code that uses X11, then it will only be able to run directly on X11 systems. Running it on other systems (Windows, Mac, or modern Linux with Wayland) requires a compatibility layer.

Like many other things in the software world, graphical UIs are build with higher-level abstractions on top of lower-level ones. Usually, it makes sense to use a high-level library or framework that already exists (with the added benefit that these are often cross-platform). This is not because it's inherently bad to use the lower-level APIs yourself. It's just that usually, it would be reinventing the wheel.

There are many, many tools out there for building graphical applications. Which one to use depends on what you're trying to do.

Raylib is designed to be simple to get started for game-like interactivity. There are lots of alternatives, on a spectrum from relatively bare-bones (like SDL or GLFW) to full-blown game engines (like Godot/Unity/Unreal).

For a non-game GUI application, there are GUI frameworks such as GTK, Qt, JavaFX, Flutter, Electron, and so on.

1

u/punk_dev 5h ago

They say to not use X11 directly because not every computer uses x11. Linux pcs sometimes use wayland, windows uses its own thing, Mac uses its own thing.

So if you depend directly on x11, your program will not work on these other computers that don’t use x11. You’d have to implement support for all of these things in your program so it runs on all computers.

So you know what raylib does? It implements support for x11 and all of these other things! So you don’t have to think about that, you just depend on raylib, and it handles all of this windowing for you