Hi guys,
so I've decided to start trying to making a simple game using SDL2. I'd found a ( what seems to be good ) Tutorial by Lazy Foo... Problem is....
Either I'm a complete noob, or something just isn't working (note: I've not used C++ in about a year, and came over from Java).
This is what I have (also, this is my OOP approach to his tutorials, and I'll pick out the "problem" code):
Gui.h:
[spoiler]
class Gui {
private:
...
SDL_Window* window;
SDL_Surface* screenSurface;
public:
SDL_Surface* keyPressSurfaces[];
SDL_Surface* currentSurface;
public:
Gui();
void initGui();
bool loadMedia();
SDL_Surface* loadSurface ( std::string path );
...
};
[/spoiler]
Gui.cpp:
[spoiler]
Gui::Gui() {
window = NULL;
screenSurface = NULL;
currentSurface = NULL;
keyPressSurface[Input::KEY_PRESS_SURFACE_TOTAL]; // There is a compiler warning here saying "statement has no effect"????
}
void Gui::initGui() {
window = SDL_CreateWindow ( .... ) // Creating the master window
if ( window == NULL ) {
}
else {
screenSurface = SDL_GetWindowSurface ( window ); // Get the window surface
// set the default surface
currentSurface = keyPressSurfaces[Input::KEY_PRESS_SURFACE_DEFAULT];
// Apply the current surface
SDL_BlitSurface ( currentSurface, NULL, screenSurface, NULL );
// Update the surface
SDL_UpdateWindowSurface ( window );
}
}
bool Gui::loadMedia() {
Input input;
bool success = true;
// Load default surface
keyPressSurfaces[input.KEY_PRESS_SURFACE_DEFAULT] = loadSurface ( "assets/surfaces/press.jpg" );
if ( keyPressSurfaces[input.KEY_PRESS_SURFACE_DEFAULT] == NULL ) {
printf ( "Failed to load default image!\n" );
success = false;
}
// I'll omit the code for the up, down, left and right key presses as they are all identical to the "load default surface" one above ( except for
// the path )
...
...
return success;
}
SDL_Surface* Gui::loadSurface ( std::string path ) {
// Load image at specified path
SDL_Surface* loadedSurface = SDL_LoadBMP ( path.c_str() );
return loadedSurface;
}
[/spoiler]
Input.h:
[spoiler]
class Input {
public:
enum Key_Press_Surfaces {
KEY_PRESS_SURFACE_DEFAULT,
...
...
KEY_PRESS_SURFACE_RIGHT,
KEY_PRESS_SURFACE_TOTAL
};
public:
Input();
};
[/spoiler]
Event.cpp:
[spoiler]
void Initialize::event ( SDL_Event* event ) {
Gui gui;
Input input;
// User presses a key
else if ( Event -> type == SDL_KEYDOWN ) {
// Select surface based on key press
switch ( Event -> key.keysym.sym ) {
case SDLK_w :
gui.currentSurface = gui.keyPressSurfaces[input.KEY_PRESS_SURFACE_UP];
break;
case SDLK_s :
gui.currentSurface = gui.keyPressSurfaces[input.KEY_PRESS_SURFACE_DOWN];
break;
case SDLK_a :
gui.currentSurface = gui.keyPressSurfaces[input.KEY_PRESS_SURFACE_LEFT];
break;
case SDLK_d :
gui.currentSurface = gui.keyPressSurfaces[input.KEY_PRESS_SURFACE_RIGHT];
break;
default:
gui.currentSurface = gui.keyPressSurfaces[input.KEY_PRESS_SURFACE_DEFAULT];
break;
}
}
[/spoiler]
In my Initialize.cpp I've got the initGui function just before my event loop.
Oh and my dev tree is as follows:
[spoiler]
Tutorial ( root )
assets/
surfaces/
press.bmp
down.bmp
left.bmp
right.bmp
up.bmp
bin/
includes/
control/
Input.h
gui/
Gui.h
init/
Initialize.h
includes.h
obj/
src/
control/
Input.cpp
gui/
Gui.cpp
init/
Initialize.cpp
main/
main.cpp
term/
CleanUp.cpp
[/spoiler]
I don't understand what's going wrong, or I'm completely overlooking something, but that being said, when it's built and run, there are no error, except that one warning, and the surface goes black, so it's certainly doing something.
Any help would be much appreciated.
Thanks!
Jamie.