SDL does not show its window if I use a logging library - c++

I'm trying to use log4cplus in conjunction with SDL to make a little graphic application.
I'm using minGW and Eclipse CDT on windows.
My problem is that whenever I use the library, my SDL window is not shown.
Instead I get this on the console [New Thread 2624.0x1270] and that is it. No error message, no compilation/linking problem, nothing (see edit for precisions).
If I don't use the library, a similar message appears on the console and then disappears, and my SDL window is shown properly.
Below is an example of this behavior. If I comment the two lines starting with "Logger ", then everything is fine. If I do not, SDL window is not shown.
Edit: I've tried using just the logging library and commenting all the other code but this fails as well somehow. I cannot put a breakpoint on the logger code, eclipse tells me: Breakpoint attribute problem: installation failed and the code don't seem to be executed.
*Edit2:*I was on the wrong track, see my post below. Problem kind of solved.
Any Idea?
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_endian.h> /* Used for the endian-dependent 24 bpp mode */
#include "Screen.h"
#include <log4cplus/logger.h>
#include <iomanip>
using namespace log4cplus;
int main(int argc, char **argv) {
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Impossible d'initialiser SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_Surface *screen;
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if ( screen == NULL ) {
fprintf(stderr, "Impossible de passer en 640x480 en 16 bpp: %s\n", SDL_GetError());
exit(1);
}
Logger root = Logger::getRoot();
Logger log_1 = Logger::getInstance(LOG4CPLUS_TEXT("test.log_1"));
while(true)
{
SDL_Event event;
SDL_WaitEvent(&event);
switch (event.type) {
case SDL_KEYDOWN:
printf("La touche %s a été préssée!\n",
SDL_GetKeyName(event.key.keysym.sym));
//SDL_Quit();
break;
case SDL_QUIT:
exit(0);
}
Screen::DrawPixel(screen,20,20,200,10,10);
}
}

I have no experience with log4cplus, but I do know that you should be aware that SDL does some obnoxious rewiring of stdout and stderr.
I was going to say that you should move your Logger setup to after your SDL_Init call, but I just noticed that you don't even have one. Try calling SDL_Init before setting up your display.

I found the problem. I hadn't noticed the error gdb gave me when running the program: "
gdb: unknown target exception 0xc0000135". In fact the program didn't start at all.
This means it could not load a dll because of some path problems. I tried putting the dll I use for log4cplus in the system path, tried other workarounds like starting eclipse from but to no avail. One way I found to make it work is simply to put the dll in the Debug folder.
The problem with this gdb error is quite widespread (see google). This is not a proper fix, but I will live with it for the time being, so I consider the problem solved.

Related

Application error despite console displaying no errors?

I’ve been watching this tutorial and I’ve run into an error. For some reason even though I fixed the unresolved external symbols errors, the application error is still available. https://imgur.com/a/ppngHxL I don't know whether it's due to my computer because it says it's a 64 bit processing system, but nothing seems to work.
I tried using one tutorial in that I extract the sdl2.dll file then copy pasting it into the windows32 file, and that still didn't work, and the properties panels don't have a compatibility tab on my computer. I'm not sure what to try next. Here's the code
#include "SDL.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN );
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
return 0;
}
The application does not start properly (0xc000007b)
The root cause is the lack of the required DLL, and the wrong version of the dll is also a possible reason, the report error of the two are exactly the same.
I noticed the following question. Is it possible that different versions caused your problem. But I tested and found nothing, it work.
Have you performed this step? Copy SDL2.dll to the root directory of your project. This may also cause some problems, although the compilation is successful.
Of course, it is more likely that you are missing other system files or dlls. It is not easy to guess which file missing. If this is the case, I am sorry that I have no better suggestions.

Loading and displaying image using opencv in vs2012

Thanks for all the replies!
I've added the ..\wavFile.wav in the command argument.
But I still cant use the command window.
It still pops up and close immediately.
Maybe its because I use the console application to run this program?
Or are there other reasons?
I am new to opencv and I tried the following code to load and display an image
(using visual studio 2012)
I ran it using the debug mode, but I always get a window shows that
Usage: display_image ImageToLoadAndDisplay,and the window close immediately
(seems like argc is always equal to 2?)
The window wont stay there and wait for a command to load my image.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return 0;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
cvNamedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
Might be a really stupid question but I really cant figure it out for a long time.
Hope someone can help me! THANKS A LOT!
Right click your project in Solution Explorer and select Properties from the menu
Go to Configuration Properties -> Debugging
Set the Command Arguments in the property list.
source : https://stackoverflow.com/a/3697320/4499919
OR
The Mozilla.org FAQ on debugging Mozilla on Windows is of interest here.
In short, the Visual Studio debugger can be invoked on a program from the command line, allowing one to specify the command line arguments when invoking a command line program, directly on the command line.
This looks like the following for Visual Studio 8 or 9
devenv /debugexe 'program name' 'program arguments'
It is also possible to have an explorer action to start a program in the Visual Studio debugger.
source : Debugging with command-line parameters in Visual Studio
OR
https://stackoverflow.com/questions/24202291/opencv-imread-from-command-line-argv1

How to start BGI window of c++ program in maximized mode?

I am making a c++ project for my High School. I am using Dev c++ with graphics. What I want is when BGI window opens it should start in maximized mode instead of normal window.
following is my code but it doesn't works :(
#include<iostream>
#include<conio.h>
#include<graphics.h>
#include<windows.h>
using namespace std;
void loading() {
int x=170,i,gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"");
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(170,180,"LOADING,PLEASE WAIT");
for(i=0;i<300;++i)
{
delay(30);
line(x,200,x,220);
x++;
}
getch();
}
main() {
ShowWindow( GetConsoleWindow(), SW_HIDE );
loading();
ShowWindow(FindWindow(NULL,"Windows BGI"),SW_MAXIMIZE);
}
console window gets hide according to my need but BGI window don't get Maximized. I am newbie to c++ so i don't know how to handle windows with c++. Please help with some useful code or solution.
I don't have an immediate answer, but I can suggest a direction.
You need to break this down to see where it is failing (an approach that helps with many different kinds of bugs).
Are you getting to the second "ShowWindow" line of code? You can check with a breakpoint on that line.
What is FindWindow returning? My guess would be NULL, but you can confirm that (such as by pulling it out to its own line:
HWND hwndBGI = FindWindow(NULL,"Windows BGI");
ShowWindow(hwndBGI,SW_MAXIMIZE);
Then you can check what FindWindow returns. If NULL, you know where to look for the problem; in the call to FindWindow.
If FindWindow fails, a call to GetLastError after the call to FindWindow may give you more information.
FindWindow does not always succeed. Potential problems could be an inaccurate window name, issues finding a window in a different process, a window which is not a top-level window....
Wishing you success!
Open a maximum window using getmaxwidth(), getmaxheight()
/* getmaxwidth and getmaxheight example */
#include <graphics.h>
int main(void)
{
/* Make a window, as big as possible */
initwindow(getmaxwidth( ), getmaxheight( ));
/* clean up */
getch();
closegraph();
return 0;
}

Linux, can I redirect the debug output of a external library (.so)

I'm developing an app in qt c++, on linux (and then others platforms).
I'm using a library of third part in my project. During the program execution the library writes debug messages on my debug console and I need to keep clean it for debug others part of the program.
What can I do to redirect (or hide) only the debug messages from this library?
Thanks
Regards
Andrea
If you have the code of the *.so you can try compile it in release mode, that way you avoid debug messages from being compiled/executed.
You can still use the "release mode" library with your "debug mode" project. Note you don´t be able to see any symbols from the library neither.
In reponse to your comment:
... I've already tryed but the library keeps to print debug messages also in release build
It seems that messages you see aren´t really "debug" messages. It is possible the messages are printed by cout or printf? Is that the case, I'm afraid you can't redirect just the library's messages.
You can redirect messages of a process, and when you load a library, every function you execute is executed by the loader process, hence, it would be as redirect/hide just part of the output of the process.
But, since you have the code, why don't add the
#ifdef DEBUG
...
#endif
blocks your self?
You can use qInstallMsgHandler and create a custom message handler.
Unfortunately this way means that all qDebug messages get handled the same way, regardless from where. It does, however give you the flexibility to create your own debug message type and use that in your application, whereas the shared library uses the plain qDebug type.
This is modified code from a Qt example:
#include <qapplication.h>
#include <stdio.h>
#include <stdlib.h>
const unsigned int MyMsgType = QtFatalMsg + 1;
void myMessageOutput(QtMsgType type, const char *msg)
{
switch (type) {
case QtDebugMsg:
// do nothing.
break;
case QtWarningMsg:
fprintf(stderr, "Warning: %s\n", msg);
break;
case QtCriticalMsg:
fprintf(stderr, "Critical: %s\n", msg);
break;
case QtFatalMsg:
fprintf(stderr, "Fatal: %s\n", msg);
abort();
case MyMsgType:
fprintf(stderr, "MyMsg: %s\n", msg);
break;
}
}
int main(int argc, char **argv)
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
return app.exec();
}
Elsewhere to print your custom debug message use QDebug(MyMsgType) << "Message.";

Launch console application which uses environment variables from QT Gui application

I am currently making a GUI using QT4.8 which basically needs to launch a console application. However, because this console application tries to fetch some environment variables, I can't seem to manage to make this work.
I am using QProcess obviously and have tried several solutions :
process->start("./yarpbridge", QStringList() << "--from" << "tmp.ini");
This solution does not spawn a console window and besides, by redirecting the output to qDebug(), it prints the erros corresponding to the lack of environment variables.
process->start("gnome-terminal", QStringList() << "-e" << "zsh" << "-c" << "\"./yarpbridge --from tmp.ini"\");
This solution does launch a console window but it nevertheless displays the error messages because somehow .zshrc was probably not consulted when opening the console window.
Would you have a solution that would allow me to do this, and even better that would not only work with "gnome-terminal" and "zsh" users ?
Thanks a lot,
Can you post the error you are getting?
It is very strange because you don't need to start a terminal in order to run a CLI program, maybe after posting your error message I might get an idea what the problem is.
Also you can try this as well:
#include <stdio.h>
char buffer[1024];
FILE* fd = popen("/path/to/yarpbridge", "r");
if (fd == NULL) {
// Error: do something
}
while(NULL != fgets(buffer, sizeof(buffer), fd)) {
QString s(buffer);
s = s.stripWhiteSpace();
// s contains the output, pretty much as readAllStandardOutput() in QProcess
}
// don't forget to close file.
close (fd);