SDL2 Not Loading/Showing Images?

Started by
16 comments, last by yellowsputnik 9 years, 11 months ago

Why do you declare a Gui variable in Initialize::event()?

I declared it because I thought it would be easier to write "gui.keyPressSurfaces[]" rather than "Gui::keyPressSurfaces" as I have at least 5 points in my code where I have to write that. And same goes for Input too in the Event class ( if that makes sense ).

But changing the event calls to "Gui::" instead just makes the compiler vomit the following error:


error: invalid use of non-static data member 'Gui::keyPressSurfaces'

changing the "Gui::" back to "gui." and then changing "input." to "Input::" causes no errors.

Edit:

I changed everything to read:

Event.cpp ( with changes to KEY_PRESS_SURFACE_... to match what I wanted it to look like ):

Gui::currentSurface = Gui::keyPressSurfaces[Input::KEY_PRESS_SURFACE_DEFAULT];

Gui.h:

SDL_Surface* keyPressSurfaces[];
SDL_Surface* currentScreen;
 
// To:
static SDL_Surface* keyPressSurfaces[];
static SDL_Surface* currentScreen;

the compiler then gives:

undefined reference to 'Gui::currentSurface'
Advertisement

You'll have to use the same Gui variable that you called initGui() on. Otherwise you're not using the window and surfaces that you created during initialization.

 

How do I go about doing that?

You are using a non-initialized Gui object on Event.cpp.

Every time you call the event( SDL_Event* ) function, you create a new Gui and use it without loading anything.

In the end of the function call, it is deleted.

So, as Yellowsputnik pointed out, you're not using the Gui object you think you are.

You could pass on the Gui object to the event function as in here:


void Initialize::event ( const Gui &gui, SDL_Event* event )

or even use a global variable for it.

What exactly is the Initialize class responsible for?


You are using a non-initialized Gui object on Event.cpp.
Every time you call the event( SDL_Event* ) function, you create a new Gui and use it without loading anything.
In the end of the function call, it is deleted.
So, as Yellowsputnik pointed out, you're not using the Gui object you think you are.

Right ok.


You could pass on the Gui object to the event function as in here:
void Initialize::event ( const Gui &gui, SDL_Event* event )
or even use a global variable for it.

I tried doing as you said and I think I did it wrong as it threw errors at me. Could you show me how it should be implemented?


What exactly is the Initialize class responsible for?

It's responsible for pretty much everything, it initializes the event loop, gui, render and finally the cleanup when the app is closed.

Could you show me how it should be implemented?


Take a look at the following code snippet:
#include <iostream>

class MyClass {
	std::string myName;
	
public:
	MyClass(std::string name): myName(name){
            //myName = name;
        }

	void PrintMyName() {
		std::cout << myName << std::endl;
	}
};

void CallPrintMyName (MyClass& correct_object) {
	correct_object.PrintMyName();
}

int main (){
        // Creates two objects with their names.
        MyClass Dejaime("Dejaime Neto"), JohnSmith("John Smith");

        //Call the function passing the first object
	CallPrintMyName (Dejaime);
        //Call again with the second one
	CallPrintMyName (JohnSmith);
}
You see, I pass on the object I want to use inside the CallPrintMyName function.
It outputs the following:
>> Dejaime Neto
>> John Smith

If you want some advice, it just seems you are biting a bit more than you can chew at the moment. I'd advise you to take a step back and study a bit more variable scopes, functions and object orientation in general, be it with Java or C++. Give it a week or so and then get back to this problem, it can only do good!

OK, I've decided to follow the tutorial a little closer to get the gist of how to make SDL work, but it's still not working properly. It builds and runs fine, but when I ask it to load a ".bmp" image, it just shows a white surface.

I've tried putting the image in with the "Initialize.cpp", "main.cpp", "Gui.cpp" and in the root directory, and even in the bin/debug folder and it still comes up with a just a white screen.

Make sure to check return values of functions to see if they actually did what you think they should do.

When debugging, I always like to use breakpoints and step through the code to know exactly what's going on. You can try that here as well.

 

This topic is closed to new replies.

Advertisement