kuru
Laptop und Trachtenjanker
Tutorials say you should prescind code and put it in a file of its' own, encapsulate it according to its' function.
So the noob looks at their main.cpp and tries to move everything SDL-specific to another file like I did below.
But you will find SDL_Window* in main.cpp. I guess this works because WindowManager.h and .cpp both include SDL.h.
SDL_Window* should not be in main.cpp, correct?
What is the proper thing to return from WindowManager::Create() ?
I tried getting a memory address which to then be able to feed to WindowManager:: Destroy() but got completely lost in details. Would I have to know the size of the memory being pointed at to do this properly?
or
Am I overthinking? Is there a more elegant way? Maybe just returning TRUE from WindowManager::Create() and having some invisible variable inside the WindowManager object store the pointer-to-surface? What's the 'proper' way to do that?
Do I even need to include WindowManager in main, or can this be done with a forward declaration?
Please excuse my throwing around big words paired with a tremendous lack of insight! The truth seems within arm's reach...
main.cpp
the pretentiously named WindowManager.cpp
Edit:
I've changed my main.cpp as follows:
WindowHandle is a public variable of type SDL_Window* inside WindowManager
Is that what abstraction is all about? Explicitely not having written down the SDL_Window type in this file? Or am I violating the principle just by having the type occur in this file?
Edit2:
Meh! I don't need to pass the WindowHandle to the destroy method within main.cpp.
It's better to let the handle be a private variable in WindowManager.cpp that I can simply reference from there.
Whaddayasay? I think I got it now. Writing down some thoughts helped.
So the noob looks at their main.cpp and tries to move everything SDL-specific to another file like I did below.
But you will find SDL_Window* in main.cpp. I guess this works because WindowManager.h and .cpp both include SDL.h.
SDL_Window* should not be in main.cpp, correct?
What is the proper thing to return from WindowManager::Create() ?
I tried getting a memory address which to then be able to feed to WindowManager:: Destroy() but got completely lost in details. Would I have to know the size of the memory being pointed at to do this properly?
or
Am I overthinking? Is there a more elegant way? Maybe just returning TRUE from WindowManager::Create() and having some invisible variable inside the WindowManager object store the pointer-to-surface? What's the 'proper' way to do that?
Do I even need to include WindowManager in main, or can this be done with a forward declaration?
Please excuse my throwing around big words paired with a tremendous lack of insight! The truth seems within arm's reach...
main.cpp
Code:
#include "WindowManager.h"
int main( int argc, char* args[] )
{
WindowManager MyWindowManager;
SDL_Window* GameWindow = MyWindowManager.Create();
MyWindowManager.Destroy(GameWindow);
return 0;
}
Code:
SDL_Window* WindowManager::Create()
{
//The window we'll be rendering to
SDL_Window* window = NULL;
//The surface contained by the window
SDL_Surface* screenSurface = NULL;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Create window
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( window == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
}
else
{
//Get window surface
screenSurface = SDL_GetWindowSurface( window );
//Fill the surface white
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
//Update the surface
SDL_UpdateWindowSurface( window );
//Wait two seconds
SDL_Delay( 2000 );
return window;
}
}
printf( "WindowManager problems");
return NULL;
}
void WindowManager::Destroy(SDL_Window* window)
{
//Destroy window
SDL_DestroyWindow( window );
printf( "Window has been destroyed\n");
//Kill SDL
SDL_Quit();
printf( "SDL has been quit\n");
}
Edit:
I've changed my main.cpp as follows:
Code:
int main( int argc, char* args[] )
{
WindowManager MyWindow;
MyWindow.Create();
MyWindow.Destroy(MyWindow.WindowHandle);
return 0;
}
Is that what abstraction is all about? Explicitely not having written down the SDL_Window type in this file? Or am I violating the principle just by having the type occur in this file?
Edit2:
Meh! I don't need to pass the WindowHandle to the destroy method within main.cpp.
Code:
int main( int argc, char* args[] )
{
WindowManager MyWindow;
MyWindow.Create();
MyWindow.Destroy();
return 0;
}
It's better to let the handle be a private variable in WindowManager.cpp that I can simply reference from there.
Code:
private:
SDL_Window* WindowHandle;
//and
void WindowManager::Destroy()
{
//Destroy window
SDL_DestroyWindow( WindowHandle );
printf( "Window has been destroyed\n");
//Kill SDL
SDL_Quit();
printf( "SDL has been quit\n");
}
Last edited: