Chose the renderer driver instead of passing -1 (default).

This commit is contained in:
King_DuckZ 2014-08-06 17:44:28 +02:00
parent d5d75b034e
commit 5941e0af56
4 changed files with 58 additions and 2 deletions

View file

@ -6,6 +6,11 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -Wall -Wextra -pe
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -Wall -Wextra -pedantic -Wconversion")
option(WITH_BUILTIN_PHYSFS "Force using the version of PhysFS accompanying the code even if a system library is available" OFF)
option(FORCE_OPENGLES "Try to chose the openGL ES renderer if available. Enable this on Raspberry Pi" OFF)
if (FORCE_OPENGLES)
add_definitions(-DFORCE_OPENGLES)
endif (FORCE_OPENGLES)
include(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
@ -26,7 +31,7 @@ endif(PHYSFS_FOUND)
add_definitions(
${PNG_DEFINITIONS}
-DWITH_VERBOSE_OBS_MANAGER
# -DWITH_VERBOSE_OBS_MANAGER
-DWITH_VERBOSE_COLLIDER
)

View file

@ -58,6 +58,7 @@ int main (int, char* parArgv[]) {
physfs.Append(GAME_BASE_PATH "/resources/", "resources");
sdlmain.Init();
std::cout << "Using renderer \"" << sdlmain.GetRendererName() << "\"\n";
cloonel::GameplaySceneClassic game(&sdlmain);
RunMainLoop(game);

View file

@ -24,8 +24,50 @@
#include <SDL2/SDL.h>
#include <stdexcept>
#include <sstream>
#include <ciso646>
#include <cstring>
#include <utility>
namespace cloonel {
namespace {
///----------------------------------------------------------------------
///----------------------------------------------------------------------
std::pair<int, std::string> GetRenderingDriver() {
typedef std::pair<int, std::string> RetPairType;
const int count = SDL_GetNumRenderDrivers();
int opengles = -1;
int opengles2 = -1;
int opengl = -1;
SDL_RendererInfo info;
for (int z = 0; z < count; ++z) {
const int ret = SDL_GetRenderDriverInfo(z, &info);
if (0 == ret) {
if (std::strcmp("opengles", info.name) == 0)
opengles = z;
else if (std::strcmp("opengles2", info.name) == 0)
opengles2 = z;
else if (std::strcmp("opengl", info.name) == 0)
opengl = z;
}
}
#if !defined(FORCE_OPENGLES)
if (opengl > -1)
return RetPairType(opengl, "opengl");
#endif
if (opengles2 > -1)
return RetPairType(opengles2, "opengles2");
if (opengles > -1)
return RetPairType(opengles, "opengles");
#if defined(FORCE_OPENGLES)
if (opengl > -1)
return RetPairType(opengl, "opengl");
#endif
return RetPairType(-1, "default");
}
} //unnamed namespace
struct SDLMain::LocalData {
SDL_Window* window;
SDL_Renderer* renderer;
@ -67,13 +109,19 @@ namespace cloonel {
throw std::runtime_error(SDL_GetError());
parInitSDL.initialized = true;
#if defined(FORCE_OPENGLES)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
#endif
const float2 wh(m_localData->sizeratio.Resolution());
SDL_Window* const win = SDL_CreateWindow(m_gameName.c_str(), 100, 100, static_cast<int>(wh.x()), static_cast<int>(wh.y()), SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if (!win)
throw std::runtime_error(SDL_GetError());
parInitSDL.window = win;
SDL_Renderer* const renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
const auto rendererDriver = GetRenderingDriver();
m_rendererName = rendererDriver.second;
SDL_Renderer* const renderer = SDL_CreateRenderer(win, rendererDriver.first, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer)
throw std::runtime_error(SDL_GetError());
parInitSDL.renderer = renderer;

View file

@ -42,6 +42,7 @@ namespace cloonel {
size_t RegisterForResChange ( SizeNotifiableBase* parNotif );
void UnregisterForResChange ( size_t parID ) noexcept;
void SwapRegisteredForResChange ( size_t parID, SizeNotifiableBase* parNotif );
const std::string& GetRendererName ( void ) const { return m_rendererName; }
private:
struct LocalData;
@ -50,6 +51,7 @@ namespace cloonel {
void ClearIFN ( LocalData& parData ) noexcept;
const std::string m_gameName;
std::string m_rendererName;
std::unique_ptr<LocalData> m_localData;
};
} //namespace cloonel