How do you create a window in Linux with C++? - c++

I was expecting a Linux API similar to the Windows API. All I see on Google is references to Qt and GTK. I really don't need anything more than a simple window to draw on with OpenGL, so these libraries appear bloated for my use. What do Qt and GTK use to create windows under Linux? Is there nothing more low-level?

The X window system generally does the drawing - you then use a toolkit such as Qt or GTK on top of raw Xlib to provide event loops, drag and drop, starting apps on mouseclicks and all the other 'desktop' stuff
It's fairly easy to work directly with Xlib and opengl or if you just want to learn opengl the glut provides the framework you need to display a window, handle mouse/keyboard events and so on.

For OpenGL, the easiest way to do it is by using GLUT or SDL. Here's an approximate example using GLUT:
#include <GL/glut.h>
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("My new window");
/* ... */
}
You really want to avoid using Xlib directly as it's extremely tedious to use. Furthermore, GLUT and SDL make it easier to port your OpenGL application to different platforms.

Updated answer for 2019. Unix like systems normally uses the X window system. You can work with it directly using Xlib this is the low level API. But you likely need a more welcoming and cross-platform solution. You can use:
OpenGL Utility Toolkit - GLUT
Simple and Fast Multimedia Library - SFML
Simple DirectMedia Layer - SDL
Graphics Library Framework - GLFW (my recommendation)
GLFW is written in C and has native support for Windows, macOS and many Unix-like systems using the X Window System, such as Linux and FreeBSD.
Once installed, create a window with :
#include <GLFW/glfw3.h>
.
. //Entry and glfwInit()
.
GLFWwindow* window = glfwCreateWindow(1000, 1000, "MyWindow", NULL, NULL);
glfwMakeContextCurrent(window);

Ax Martin said, X11 (or its fork XOrg these days) is the windowing system, but you can actually write X11 applications (i.e. clients) without using a toolkit, just using the X libraries. See here for documentation.
It is generally not the best idea to do so, as it is rather painful and will involve a lot of code for relatively simple applications to work as you expect them to.

I know this is an old post. But for people that have found this recently like me here is a useful diagram. It is just a matter of how far down do you want/need to go in abstraction; if you arent careful you'll end up trying to code in binary. XLib is a dependency for SFML and XLib has it's dependencies.
Diagram of GUI Layers

Assuming by simple window you mean the drawing should appear on the screen:
The Weston compositor uses EGLOutput/EGLDevice to display the composited Weston desktop or individual Wayland applications on a physical display device.
For drawing with other manufacturer's hardware studying GEM may give hints.

Related

Is there a performance difference in using an OpenGL window like GLFW or a surrounding window like GTK or SDL?

vlc has an impressive example showing how to integrate with gtk:
https://git.videolan.org/?p=vlc.git;a=blob;f=doc/libvlc/gtk_player.c
but we are using glfw and C++. If we were to create a wrapper window using a windowing toolkit like gtk, is there any performance penalty in terms of the OpenGL operating within it?
Is it possible to open a video window within the GTKGL component, or does OpenGL interfere with video even if it is not rendering?
If you pass the window handle to libvlc, I don't see why there would be performance differences.

Query about opengl, glut, glew

Is there not a simple sdk for opengl why there are glut or glew and what are the written using? Which one should I use? There are so many frameworks out there that it is so confusing? Why there isn't just one sdk for opengl?
why there are glut or glew and what are the written using?
GLUT, GLFW and similar frameworks exist, because the system level interfaces to create a window and a OpenGL context are extremely versatile in what they can do, but it also takes a lot of code, just to create a window and them some more to create the OpenGL context. …and then some lots more of code to also do things like UI event management and so on.
These system level APIs are that verbose, because on a system level, you don't want to pin down what you can and can not do with these APIs.
So the three calls
glutInitDisplayMode(…);
glutCreateWindow(…);
glutMainLoop();
encapsulate about 1.5k lines of code dealing with just the system. But then with GLUT you don't get nice buttons, menu bars and the likes. For that you'd use a different framework, like Qt, which has even more code, but uses the same system level APIs as GLUT does. Same goes for GLFW and so on.
GLEW we have, because OpenGL can be extended and the system level interface for extending it is again down for minimalism and flexibility.
Why there isn't just one sdk for opengl?
Because in the end OpenGL is a system level API and the "SDK" comes ready-to-use with your regular system default compiler.
If you want to, you can create OpenGL programs without GLEW, GLUT and so on. But it is tedious.

Do Libraries like GLUT & GLFW have the same functionality as SDL?

I was watching this video about OpenGL 3 - https://www.youtube.com/watch?v=XMgfddy7S7Q
And while talking about libraries to work with OpenGL (at 3:00) he mentions GLFW, freeGLUT and GLUT to use to create the window.
But can they handle input and sound as well like SDL?
And if so should I be using SDL? Or is GLFW more tuned for making games specifically in OpenGL?
No, GLUT and GLFW are frameworks that manage OpenGL context creation and windowing (which includes input) mostly. GLUT has a few components that are actually designed to draw things, but by in large it is there to setup GL only.
SDL, on the other hand, includes sound which neither GLFW nor GLUT do but also includes utilities to load resources such as image files. It is a much more end-to-end solution, whereas GLFW and GLUT are only designed to facilitate rendering/windowing. To do the same thing using GLFW, you would need to throw in some libraries such as SOIL (or work directly with libpng, libjpeg, etc.) and also find an audio library.
You do not need any of these things to make a game in truth. I interact directly with OpenGL (WGL/GLX/CGL) on Windows, Linux and OS X in my work but the extra time necessary to debug and maintain each of these platforms at such a direct level can be a real nuisance. If writing extra code specific to each platform you run on is unappealing then you should definitely consider GLFW, etc.

ncurses with graphics window

I have a bare-bones Linux distro running on a machine connected to a laser. I want to develop an interface which allows me to:
Configure settings for the laser (e.g. toolbars and buttons)
Display the current path of the laser (e.g. graphics window)
Since these are bare-bones machines, I don't have X11 installed. I figured that perhaps I could use ncurses to develop a cross-platform interface to configure the settings for the laser, and use SDL to draw arcs and lines to represent the path of the laser.
While I'm comfortable using ncurses and SDL independently, I'm having trouble figuring out how to embed the SDL graphics within an ncurses window.
Is it possible to embed a graphics window (not necessarily SDL) into an ncurses application? If not, is there a cross-platform alternative to ncurses which will do what I need without X11?
The Ncurses project appears focused on developing a library for the construction of text-based user interfaces. As such, I do not believe there currently is, nor is planned to be, support for embedding an SDL graphical context.
I would suggest looking into other options such as the AGAR library which enables the creation of graphical user interfaces within SDL.

What is difference between OpenGL programing with GLUT and Without GLUT

I want to start developing graphic programing using OpenGl
To kick start I am following OpenGL
I came across programing with GLUT and without GLUT but as being new to OpenGL in am even more confuse how to go with it?
GLUT is the OpenGL Utility Toolkit
It does stuff like this:
Multiple windows for OpenGL rendering
Callback driven event processing
Sophisticated input devices
An 'idle' routine and timers
A simple, cascading pop-up menu facility
Utility routines to generate various solid and wire frame objects
Support for bitmap and stroke fonts
Miscellaneous window management functions
You can find more about it here.
GLUT was designed as a lib for simple demos and tutorials. Maybe one cannot create full AAA game title using it... but for learning/teaching it is a great tool.
GLUT is very old right now, so look for FreeGlut which is an alternative that handles not only basic GLUT features but also gives some more advanced features: like fullscreen game mode, etc.
http://freeglut.sourceforge.net/