Why won't this OpenGL program open a window? - c++

I am currently trying to learn both C++ and OpenGL, and I am beginning with the following code example:
simple.cpp
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}
It says here that this code can be compiled on MacOS using the following command:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
So I adapt the command in the following way:
gcc -Wno-deprecated-declarations -o simple simple.cpp -framework GLUT -framework OpenGL -framework Carbon
This seems to work and creates an executable called simple.
I am told that this code should generate a white square on a black background as follows:
However, when I try to run this file from the terminal using ./simple, the program runs continuously but nothing happens (that is, no window is generated at all), and so I have to terminate the program from the terminal.
Did I do something wrong here, or is this expected for the given code on MacOS?
EDIT1
To see what would happen, I tried to use the code presented in the aforementioned guide:
hello.c
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void display()
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutDisplayFunc(display);
glutMainLoop();
}
As the guide says, this is "a simple OpenGL program that does nothing".
I compile it as follows:
gcc -Wno-deprecated-declarations -o hello hello.c -framework GLUT -framework OpenGL -framework Carbon
This compiles fine. However, when I try to run the executable, I get the following error:
GLUT Fatal API Usage: main loop entered with no windows created.
zsh: abort ./hello
According to this error message, the program was terminated because no windows were created. However, as I said, my simple.cpp program does not terminate (I have to forcefully terminate it), and it is written to create windows. So I'm guessing this means that windows are created in simple.cpp, but for some reason they just don't show up on MacOS? Can anyone else confirm this? Is the issue perhaps that blank windows don't show up on MacOS, and you need to include other graphical elements in order to make the window show?
EDIT2
The problem is that my understanding is that glutCreateWindow("simple"); in simple.cpp is supposed to create a window with "simple" as its title, so I don't understand why it isn't working.
EDIT3
Thanks to derhass's answer, I was able to make it work:
#define GL_SILENCE_DEPRECATION
#include <GLUT/glut.h>
void init() {
// code to be inserted here
}
void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT);
// need to fill in this part
// and add in shaders
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutCreateWindow("simple");
init();
glutDisplayFunc(mydisplay);
glutMainLoop();
}

YOu did not include the call to glutInit() into your simple.cpp example. You can't call any other GLUT commands before this intialization, and doing so results in undefined behavior.

Related

Why can't I embed a Xephyr instance into a Qt window?

I am trying to get a Xephyr window opened within a Qt window of mine, but can't get it to work properly.
I am running a blank Qt application, for testing purpose, like this:
#include <QApplication>
#include <QWidget>
#include <iostream>
int main(int argc, char** argv) {
QApplication a(argc, argv);
QWidget window;
std::cout << window.winId() << std::endl;
window.resize(100, 100);
window.show();
return a.exec();
}
and compiling with g++ $(pkg-config --cflags --libs Qt5Widgets) test.cc
Then I get some window id after running the program. This time I got 104857606
Next, I use the Xephyr command Xephyr -parent 104857606 -screen 100x100 :1.
Here is the error I get when using it:
The XKEYBOARD keymap compiler (xkbcomp) reports:
> Warning: Could not resolve keysym XF86EmojiPicker
Errors from xkbcomp are not fatal to the X server
(EE)
Fatal server error:
(EE) X11 error
Error code: 8
Sequence number: 5
Major code: 1 Minor code: 0
Error value: 104857606
(EE)
I know this is an example of a BadMatch error, but I can't find out how to fix this. Even without the -screen 100x100, I can't get this program to work.
How can I get a Xephyr window into Qt if the -parent option doesn't work-- or how can I get this option to work?
I am using Arch Linux if that changes anything.

OpenGL program compiles but doesn't start correctly

I'm trying out OpenGL and C++, and I followed this video tutorial on writing my program (my code is exactly the same as his). I also followed the instructions on the freeglut website here to set up freeglut, compile, and link my program. The source code compiles with no problem, but when I try running the exe I get an error. The only reason I could think of is that I'm not using an IDE, so I'm probably missing some compilation steps or missing some command line arguments when running the exe, which the IDE would have done automatically. Can someone tell me what I need to do to run my program correctly?
Here's my code:
#include <GL/glut.h>
void init() {
glClearColor(1.0, 1.0, 0.0, 1.0);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glFlush();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowPosition(200, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("Window 1");
glutDisplayFunc(display);
init();
glutMainLoop();
}
When I compile I run
gcc -c -o hello.o hello.cpp -I"C:\MinGW\include"
gcc -o hello.exe hello.o -L"C:\MinGW\lib" -lfreeglut -lopengl32 -Wl,--subsystem,windows
Then I try to run hello.exe but I only get an error message "The application was unable to start correctly (0xc000007b)".
BTW I saw this duplicate question but I've tried putting the dll in the same directory (it was there from the start) but that didn't change anything.
Using the 32 bit freeglut dll (instead of the 64 bit dll) in my project fixed the problem.

Making a minimalistic build file for a Qt-using application from scratch

For pure educational purposes, I'd like to learn how to create my own QT Makefile from scratch.
I've used QMake successfully, but I still would like to learn how to make an extremely simple Makefile to run a basic QT application.
Here is my code that I want to compile with the Makefile:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.show();
return a.exec();
}
I don't have any special requirements and do not need any special native packaging.
Yes! That was a heck of a learning experience. :)
Here is the minimal Makefile I created.
# In the first two lines, I define the source (.cpp) file and the desired output file to be created (./hello)
SOURCE = hello.cpp
OUTPUT = hello
# This variable is used as a quick reference to the QT install path. This is my custom install directory on Mac OS X.
QT_INSTALL_PATH = /usr/local/qt/5.9.1/clang_64
# This is where you would provide the proper includes depending on your project's needs
INCLUDES = -I$(QT_INSTALL_PATH)/lib/QtWidgets.framework/Headers
#This is the RPATH. To be honest, I am not completely sure of what it does, but it is neccesary.
RPATH = -Wl,-rpath,$(QT_INSTALL_PATH)/lib
#Here is where you link your frameworks
FRAMEWORKS = -F$(QT_INSTALL_PATH)/lib -framework QtCore -framework QtWidgets
# The main action that happens when you type "make"
all:
# We run the g++ command and make sure we use c++11 with the second argument. Then after that, we simply plugin in our previous variables.
g++ -std=c++11 $(SOURCE) $(INCLUDES) $(FRAMEWORKS) $(RPATH) -o $(OUTPUT)
And in case you are wondering, here is my hello.cpp file. It's a very basic program that only imports QtWidgets.
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(QApplication::translate("toplevel", "Welcome to QT!"));
return app.exec();
}
This was a lot of fun and I definitely learned a lot about how QT programs are compiled. However, in the future I definitely plan on sticking to qmake, as it's a really great tool.

IMG_load and TTF_OpenFont cause a segmentation fault using SDL2 on Code::Blocks

Recently I've been working on a project that uses SDL2 on Code::Blocks 10.03 on Debian. I need to load some PNG images and a font to write some things to screen. However, when I try to load the files, both IMG_load and TTF_OpenFont create a segmentation fault.
I am 100% sure the files are on the current path, as using incorrect filenames such as buckt.png instead of bucket.png result in both SDL_GetError and IMG_GetError returning "Couldn't open buckt.png", the same with TTF files; whereas if I use the correct filename, it causes the segfault. Interestingly enough, if I compile the project from the command line, using g++ main.cpp -std=c++11 -lSDL2 -lSDL2_image -lSDL2_ttf it compiles and runs perfectly, but since I'm forced to use Code::Blocks, the terminal is not an option
For showcase, this snippet is located on the topmost part of the main function. As you may have guessed, on Code::Blocks, it won't work.
#include <iostream>
#include <limits>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
using namespace std;
int main( int argc, char* args[] ) {
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
TTF_Init();
// The segfault is caused here, using Code::Blocks
TTF_Font *gFont = TTF_OpenFont("LiberationMono-Bold.ttf",20);
if (gFont == NULL) {
// This is never reached
cout << "gFont is empty!" << endl;
}
TTF_Quit();
SDL_Quit();
}
And the execution working directory is set to ., which is the main folder for the Code::Blocks project, and also where the fonts and images are located.
If anyone has found a workaround to this situation, please let me know.

GTKmm3 (GTK+ 3 C++) compiles but throws GLib-GIO-CRITICAL error when run

I've just started trying to get GTK+ 3 working with C++ and have tried to compile the simplest GTKmm Hello-World example.
#include <gtkmm.h>
int main(int argc, char *argv[])
{
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(200, 200);
return app->run(window);
}
After compiling with:
g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`
There are no errors, however when I run the newly compiled application with:
./simple
I get the following error:
GLib-GIO-CRITICAL **: g_application_list_actions: assertion `application->priv->is_registered' failed
Segmentation fault (core dumped)
The code has been copied, unchanged from https://developer.gnome.org/gtkmm-tutorial/stable/sec-basics-simple-example.html.en so presumably it's likely to be a configuration issue, however I've frequently used this machine for PyGtk and CommandLine C++ without any issues.
Any help would be greatly appreciated.
This was a bug in gtkmm (probably triggered by a change in GTK+) that has been fixed in gtkmm:
https://git.gnome.org/browse/gtkmm/commit/?id=506cba376c0a0e09217ad7f5d597b6723d7f91a6
Maybe you can update your gtkmm version.