Use SDL to detect screen dimensions

Supports macOS, remove dependency on X11
This commit is contained in:
Cong 2018-04-28 15:05:56 +10:00
parent d77166b3fe
commit 6896308451
3 changed files with 15 additions and 38 deletions

View File

@ -19,11 +19,6 @@ include(cmake/FindSDL2_mixer.cmake)
include(cmake/FindCCache.cmake) include(cmake/FindCCache.cmake)
include(cmake/Findcppcheck.cmake) include(cmake/Findcppcheck.cmake)
if (NOT WIN32)
include(FindX11)
include(cmake/FindCheck.cmake)
endif (NOT WIN32)
configure_file( configure_file(
"${PROJECT_SOURCE_DIR}/src/config.h.in" "${PROJECT_SOURCE_DIR}/src/config.h.in"
"${PROJECT_BINARY_DIR}/config.h" "${PROJECT_BINARY_DIR}/config.h"
@ -73,11 +68,6 @@ include_directories(
${SDL2_MIXER_INCLUDE_DIR} ${SDL2_MIXER_INCLUDE_DIR}
) )
if (NOT WIN32)
include_directories(
${X11_INCLUDE_DIR}
)
endif (NOT WIN32)
if (CHECK_FOUND) if (CHECK_FOUND)
include_directories( include_directories(
${CHECK_INCLUDE_DIR} ${CHECK_INCLUDE_DIR}
@ -163,12 +153,6 @@ else (NOT PHYSFS_FOUND)
) )
endif (NOT PHYSFS_FOUND) endif (NOT PHYSFS_FOUND)
if (NOT WIN32)
target_link_libraries(breakhack
${X11_LIBRARIES}
)
endif (NOT WIN32)
if (MSVC) if (MSVC)
set_target_properties(breakhack PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE") set_target_properties(breakhack PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:CONSOLE")
set_target_properties(breakhack PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE") set_target_properties(breakhack PROPERTIES COMPILE_DEFINITIONS_DEBUG "_CONSOLE")

View File

@ -92,6 +92,13 @@ static
bool initSDL(void) bool initSDL(void)
{ {
int imgFlags = IMG_INIT_PNG; int imgFlags = IMG_INIT_PNG;
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
error("Could not initiate SDL2: %s", SDL_GetError());
return false;
}
Dimension dim = getScreenDimensions(); Dimension dim = getScreenDimensions();
if (dim.height > 1080) { if (dim.height > 1080) {
@ -104,12 +111,6 @@ bool initSDL(void)
info("Scaling by %f", renderScale); info("Scaling by %f", renderScale);
} }
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
{
error("Could not initiate SDL2: %s", SDL_GetError());
return false;
}
if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) { if ( (IMG_Init(imgFlags) & imgFlags) == 0 ) {
error("Unable to initiate img loading: %s", error("Unable to initiate img loading: %s",
IMG_GetError()); IMG_GetError());

View File

@ -17,28 +17,20 @@
*/ */
#include "defines.h" #include "defines.h"
#include "util.h"
#ifndef _WIN32 #include <SDL_video.h>
#include <X11/Xlib.h>
#else // _WIN32
#include <windows.h>
#endif // _WIN32
#include <stdlib.h>
#include "screenresolution.h" #include "screenresolution.h"
Dimension getScreenDimensions(void) Dimension getScreenDimensions(void)
{ {
#ifndef _WIN32 SDL_DisplayMode dm;
Display *d = XOpenDisplay(NULL); if (SDL_GetCurrentDisplayMode(0, &dm) != 0)
Screen *s = DefaultScreenOfDisplay(d); {
Dimension dim = (Dimension) { s->width, s->height }; error("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError());
}
free(d); Dimension dim = (Dimension) { dm.w, dm.h };
free(s);
#else // _WIN32
Dimension dim = (Dimension) { GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN) };
#endif // _WIN32
return dim; return dim;
} }