Skip to content

Commit

Permalink
Fixed broken window setting code with SDL2. Closes #40.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanemagnenat committed Feb 17, 2021
1 parent f661401 commit f03d62f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
2 changes: 1 addition & 1 deletion libgag/include/SDLGraphicContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ namespace GAGCore
//! the pointer for iterating through mode list
SDL_DisplayMode **modesitr;
int totalmodes;
SDL_Window *window;
SDL_Window *window = nullptr;
SDL_Renderer *sdlrenderer;
friend class DrawableSurface;
//! option flags
Expand Down
60 changes: 27 additions & 33 deletions libgag/src/GraphicContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,32 +2020,23 @@ namespace GAGCore
}

TTF_Init();
Uint32 sdlflags = 0;
if (flags & USEGPU)
sdlflags |= SDL_WINDOW_OPENGL;
if (flags & FULLSCREEN)
sdlflags |= SDL_WINDOW_FULLSCREEN;

SDL_CreateWindowAndRenderer(w, h, sdlflags, &window, &sdlrenderer);
if (flags & USEGPU)
///If setting the given resolution fails, default to 800x600
if(!setRes(w, h, flags))
{
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
fprintf(stderr, "Toolkit : Can't set screen resolution, resetting to default of 800x600\n");
if (!setRes(800,600,flags)) {
fprintf(stderr, "Toolkit : Initial window could not be created, quitting.\n");
exit(1);
}
}
if (!title.empty() && !icon.empty())
{
SDL_SetWindowTitle(window, title.c_str());
SDL_Surface *iconsurface = IMG_Load(icon.c_str());
SDL_SetWindowIcon(window, iconsurface);
SDL_FreeSurface(iconsurface);
}
///If setting the given resolution fails, default to 800x600
if(!setRes(w, h, flags))
{
fprintf(stderr, "Toolkit : Can't set screen resolution, resetting to default of 800x600\n");
setRes(800,600,flags);
SDL_Surface *iconSurface = IMG_Load(icon.c_str());
SDL_SetWindowIcon(window, iconSurface);
SDL_FreeSurface(iconSurface);
}

}

GraphicContext::~GraphicContext(void)
Expand Down Expand Up @@ -2078,36 +2069,34 @@ namespace GAGCore
fprintf(stderr, "Toolkit : Screen height %d is too small, set to min %d\n", h, minH);
h = minH;
}

SDL_SetWindowSize(window, w, h);
sdlsurface = SDL_GetWindowSurface(window);

// set flags
optionFlags = flags;
Uint32 sdlFlags = 0;
if (flags & FULLSCREEN)
sdlFlags |= SDL_WINDOW_FULLSCREEN;
if (flags & FULLSCREEN)
sdlFlags |= SDL_WINDOW_RESIZABLE;
// if (flags & FULLSCREEN)
// sdlFlags |= SDL_WINDOW_RESIZABLE;
#ifdef HAVE_OPENGL
if (flags & USEGPU)
{
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
sdlFlags |= SDL_WINDOW_OPENGL;
}
else
{

SDL_DestroyRenderer(sdlrenderer);
sdlrenderer = SDL_CreateSoftwareRenderer(sdlsurface);
}
#else
// remove GL from options
optionFlags &= ~USEGPU;
SDL_DestroyRenderer(sdlrenderer);
sdlrenderer = SDL_CreateSoftwareRenderer(sdlsurface);
#endif

// if window exists, delete it
if (window) {
SDL_DestroyWindow(window);
window = nullptr;
}
// create the new window and the surface
window = SDL_CreateWindow("unnamed", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, sdlFlags);
sdlsurface = window != nullptr ? SDL_GetWindowSurface(window) : nullptr;

// check surface
if (!sdlsurface)
{
Expand All @@ -2118,7 +2107,12 @@ namespace GAGCore
else
{
_gc = this;

// enable GL context
if (flags & USEGPU)
{
SDL_GLContext context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
}
// set _glFormat
if ((optionFlags & USEGPU) && (_gc->sdlsurface->format->BitsPerPixel != 32))
{
Expand Down

0 comments on commit f03d62f

Please sign in to comment.