SFML : drawing from different files - c++

I'm working on a SFML project that contains several different files and classes. The problem is : in every file, there is some drawing, and the sf::RenderWindow* defined in the main file seems to be out of scope here, therefore I can't link these drawing with the main window. I've tried to redefine sf::RenderWindow in every header files, and it doesn't work as well (a black screen appear and disappear promptly). The error was : Access violation reading location. But when I group everything in one file, things work well but it looks really messy.
I also found this question asked on another forum, and the answers were "using reference and external method", which is a bit vague to me. Any help on this linking problem would be appreciated :(

I think that answer meant something like:
void draw_in_class (sf::RenderWindow* window){
window->draw(...);
}
in class, and then you can use 'draw_in_class' in main:
#include"class.h"
... // Define sf::RenderWindow here
draw_in_class(&window);

Related

How can I find out what causes undefined behaviour in my program?

My Qt application consists of four screens that can be switched. These screens are added to a QStackedLayout Class with addWidget(). The switching between screens happens with setCurrentWidget().
Adding some variables in the header file in one of the screens(/classes) always causes the program to crash. Debugging gives me a segfault at adwidget() where I try to add that screen to the Layout.
Now if I clean all before the build it always works. Also if just change something in the header file of some other class (can be any header file) and I rebuild only that one header file it works aswell.
It only happens with that one screen(/class). It doesn't matter what type of variable I add. Doesn't matter if they are public or private. The class is almost identical with others that work without any problem. When I compile I get no warnings or errors. It just crashes at addwidget().
Is there any way to find out whats going on?
edit:
This is the result of valgrind memcheck:
Line 18 in compassdebugscreen.cpp is: QString const strCalib3DStartBtn = "Start Calib 3D";
Line 14 in main.cpp is: QApplication a(argc, argv)

Try to rewrite engine from SDL to SFML, wrong scope of sf::RenderWindow?

I have problem with display image (I tried to rewrite engine from SDL to SFML 2.0; engine downloaded from:
http://gamedevgeek.com/tutorials/managing-game-states-in-c/
)
I have problem with especially that part of code that is in introstate.cpp.
The program compiles and create window (just for one sec) and then vanishes with no reaction and no render anything (it should display image).
I think it has to do with range of object sf::RenderWindow MarioClone. I mean it was declared in few headers and used in variety methods, so I think there's misunderstanding with pointing to the specific window that is created. Should I use "extern" keyword somwhere or what?
I leave link to github because code is in many files and even one file contains a lot of code and don't want to paste it here (it would be hard to read).
https://github.com/shahar23/MarioClone
(And yes - the code has previous original SDL commented to understand easily what should be put in methods instead)
In your gameengine.cpp file, in your init method, you create a local variable of the same name as the variable declared in your header file. That's not what you want. You want to change the existing variable:
void CGameEngine::Init(const char* title, int width, int height, bool fullscreen)
{
// This line creates a NEW LOCAL variable of the same name.
// Your instance level variable remains unchanged:
// sf::RenderWindow MarioClone(sf::VideoMode(width, height), title, sf::Style::Default);
// instead, change your class level variable:
MarioClone.create(sf::VideoMode(width, height), title, sf::Style::Default);

DevIL reading an invalid extension

So I've been using the DevIL library for a while now (along with ILU and ILUT) and have had few problems with it after I got it working. That is, until now. All of a sudden it does not seem to work with any image of any format. I'm using this code:
GLuint myTex;
void Init() {
myTex = ilutGLLoadImage(L"img.tga");
}
void Draw() { // 2D Drawing
glBindTexture(GL_TEXTURE_2D, myTex);
draw::TwoD::Textured::Rectangle(100, 100, 200, 200);
}
The rectangle appears blank white. I checked the function and it seems like it should work fine, so I tried calling ilGetError(). It returned IL_INVALID_EXTENSION for nearly every different image I tried. I've tried putting the whole directory and even made sure that I was using the right version of DevIL (unicode) and still it returns this. Any idea what's going on?
Thanks in advance!
Edit:
I've also notices that DevIL returns this even if the file specified does not exist. This makes me think something is wrong with DevIL itself instead of my code.
Edit 2:
So I found out that I placed the unicode DLLs in the project directory (as necessary) but it turns out I linked to the library directories for multi-byte chars. Even after fixing it, the problem continued. I'm stumped by this one.
Edit 3:
Okay sorry for so many edits but I also just found out that the DevIL setup I had in my previous projects still work perfectly. It seems to only be the project I'm using now. All the relevant code is the same (copy/pasted), so I think it has to do with the project properties, but I can't find any differences. Do you know of any way to copy the properties of one project to another in VS2015?

Qt Designer undefined symbol with Custom Widget plugin

I am using Qt 4.7.2 under Fedora 14.
I have a working library of custom widgets which I am trying to integrate into Qt Designer.
There is a simple widget base class, then some more complex widgets inheriting from it.
The simple widget integrates fine, but I have a problem with the more complex ones.
The problem comes from the fact that that the more complex ones require a special structure to be passed to them on creation. So my code for creating a complex widget in the plugin is:
QWidget *myPlugin::createWidget(QWidget *parent)
{
my_struct *xyz = new my_struct;
return new myWidget("MyWidget",xyz, parent);
}
my_struct is just a simple C style struct with ints, doubles and arrays.
Then when I start Qt Designer with this plugin I get:
/usr/lib64/qt4/bin/designer: symbol lookup error: /usr/lib64/qt4/plugins/designer/libMyPlugin.so: undefined symbol: _ZN8MyWidgetC1E7QStringP17my_structP7QWidget
I am building a release version of the plugin, not a debug version.
I also tried defining xyz as a static class variable in the plugin and just passing its address to the constructor, but I still get the same error.
I also tried adding xyx as an unused static class variable to my simple widget and that does not give the error, so the error seems to be coming from the call to the myWidget constuctor.
Can anyone help me resolve this?
Thanks Arne,
You were right.
I added my library explicitly to the plugin's .pro file and then it worked.
What puzzles me is how could I build the plugin with no errors without the library?
And why did my simple widget work without the library?
The error message looks as if the linker did not find a definition for the constructor myWidget(QString, my_struct*, QWidget*). Have you added the relevant source file to your release build? Do you actually have a definition for this constructor? It is also possible that you have to exchange the object file linking order. I have seen this with larger projects in the past, so I wouldn't be too surprised. The object file containing the definition needs to be stated before the one using the definition.

QtCreator build returns collect2: ld returned exit status 1

While building several different projects in QtCreator, I have run across the following build error:
collect2: ld returned 1 exit status
After only changing a few things (that should not change anything significant in the build), it will go away if it has already appeared, or it will appear if it's not there.
In my current program for a school project, I am trying to compile rock03.cpp. It's the only file in the build, and has the main() method. I had just run it successfully, and went back to change the order of some if()s, now, I get only two relevant warnings:
overriding commands for target 'rock03.o'
and
ignoring old commands for target 'rock03.o'
along with the error in question.
Does anyone know why this would happen? I cannot seem to reproduce the error with any reasonable certainty, and QtCreator is not complaining about any thing before I build.
Thanks
If the only message error is this one concerning linker, the reason can be that your program is still running and linker can not access to the binary file. Be sure your application was stopped or kill it if still running.
Qtcreator never checks if previous run was stopped before compiling.
This happens to me because I make a declaration in the header file, then delete the function in the cpp file and I forget to delete the decleration in the header. For example...
//header file
class CLASS : public Q_OBJECT
{
...
protected:
void mouseMoveEvent(QMouseEvent*);
}
//source file
void CLASS::mouseMoveEvent(QMouseEvent*e)
{
...
}
//I'll delete this, then forget to delete "void mouseMoveEvent(QMouseEvent*);" in the header file
The compiler output is really helpful if you're just getting this as an error, but the first candidate is probably that you've still got the output program open, and it can't write to the file, because that'll give you a solitary collect2 error like this
This error may also occur because of problems with linkage, for example, you forgot to declare some static variables from header file using 'extern' directive.
In my case, folder permissions were the problem. Checking the "Compile Output" window is crucial for finding out what exactly the problem is. (QtCreator is the opposite of Visual Studio in that regard, so it takes some getting used to.) I tried setting the permissions properly, but after that didn't seem to work, in the end I deactivated shadow build and then I went to "Tools/Options/Build&Run/General/Projects Directory" and set "Directory" to ".". Then it finally compiled. "It" being the kmap2qmap project in Qt 5.11.
Just my 2 cents in case anyone might find them useful.
This happens when you do not close your main app (so the output executable is still running, but without any visible window). An example:
int main() {
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
This app ends fine when you close the main window, but this code
int main() {
QApplication a(argc, argv);
QDialog w;
w.exec();
return a.exec();
}
doesn't close the app when you close the dialog (at least for me).
A solution is to create always your main window and make sure you close it.
In my case it was declaring the clear virtual function.
void virtual Func(MouseEvent*); // Error.
void virtual Func(MouseEvent*) = 0; // Well!
There could be many more reasons for the error. But for me, on removing of unused SLOTS from the class the problem was solved.
I had the same problem. My resolution is - implement all virtual functions and all slots declarations.
Checking the "Compile Output" pane reveals that the .pro file was trying to link the same .cpp file twice.