Skip to content

Commit

Permalink
Use option to enable/disable SDL_TTF
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Oct 6, 2024
1 parent 7c12281 commit ce13dce
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 48 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ ENDIF(ENABLE_OPENGL AND OPENGL_FOUND)

# The Guisan SDL extension library
OPTION(ENABLE_SDL "Enable the Guisan SDL extension" ON)
OPTION(ENABLE_SDL_TTF "Enable the Guisan SDL TTF extension" ON)
OPTION(BUILD_GUISAN_SDL_SHARED "Build the Guisan SDL extension library as a shared library." ON)

IF(ENABLE_SDL)
FIND_PACKAGE(SDL2 REQUIRED)
FIND_PACKAGE(SDL2_image REQUIRED)
FIND_PACKAGE(SDL2_ttf REQUIRED)

INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIR})
INCLUDE_DIRECTORIES(SYSTEM ${SDL2_INCLUDE_DIR})

# The Guichan SDL extension source
FILE(GLOB GUISAN_SDL_HEADER include/guisan/sdl.hpp)
Expand Down Expand Up @@ -188,7 +188,13 @@ IF(ENABLE_SDL)
MESSAGE(STATUS "MINGW32_LIBRARY: ${MINGW32_LIBRARY}")
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_sdl ${MINGW32_LIBRARY})
ENDIF(MINGW)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_sdl PRIVATE SDL2::SDL2 SDL2_image::SDL2_image SDL2_ttf::SDL2_ttf ${PROJECT_NAME})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_sdl PRIVATE SDL2::SDL2 SDL2_image::SDL2_image ${PROJECT_NAME})
IF(ENABLE_SDL_TTF)
FIND_PACKAGE(SDL2_ttf REQUIRED)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_sdl PRIVATE SDL2_ttf::SDL2_ttf)
TARGET_COMPILE_DEFINITIONS(${PROJECT_NAME}_sdl PUBLIC USE_SDL2_TTF)
ENDIF(ENABLE_SDL_TTF)
TARGET_LINK_LIBRARIES(${PROJECT_NAME}_sdl PRIVATE ${PROJECT_NAME})

SET_TARGET_PROPERTIES(${PROJECT_NAME}_sdl PROPERTIES
VERSION ${${PROJECT_NAME}_VERSION}
Expand Down
7 changes: 4 additions & 3 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ if not conf.CheckPKGConfig('0.15.0'):

env['HAVE_OPENGL'] = conf.CheckPKG('gl')
env['HAVE_SDL2'] = conf.CheckPKG('sdl2')
env['HAVE_SDL2_TTF'] = conf.CheckPKG('SDL2_ttf')

if env['HAVE_SDL2']:
if not conf.CheckPKG('SDL2_image'):
print('SDL2_image not found. Disabling SDL2 support.')
env['HAVE_SDL2'] = 0
if not conf.CheckPKG('SDL2_ttf'):
print('SDL2_ttf not found. Disabling SDL2 support.')
env['HAVE_SDL2'] = 0

if env['HAVE_SDL2_TTF']:
env.Append(CPPDEFINES = ['USE_SDL2_TTF'])

env = conf.Finish()

Expand Down
4 changes: 4 additions & 0 deletions demo/ff/SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ if not conf.CheckPKGConfig('0.15.0'):
Exit(1)

env['HAVE_SDL2'] = conf.CheckPKG('sdl2')
env['HAVE_SDL2_TTF'] = conf.CheckPKG('SDL2_ttf')

if env['HAVE_SDL2']:
if not conf.CheckPKG('SDL2_image'):
Expand All @@ -41,6 +42,9 @@ env.Append(CPPPATH = ['#../../include'])
env.Append(CFLAGS = ['-g'])
env.Append(CPPFLAGS = ['-g'])

if env['HAVE_SDL2_TTF']:
env.Append(CPPDEFINES = ['USE_SDL2_TTF'])

Export("env")

# Main program
Expand Down
7 changes: 5 additions & 2 deletions include/guisan/sdl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@
#ifndef GCN_SDL_HPP
#define GCN_SDL_HPP

#include <guisan/sdl/sdlgraphics.hpp>
#include <guisan/sdl/sdl2graphics.hpp>
#include <guisan/sdl/sdlgraphics.hpp>
#include <guisan/sdl/sdlimage.hpp>
#include <guisan/sdl/sdlimageloader.hpp>
#include <guisan/sdl/sdlinput.hpp>
#include <guisan/sdl/sdltruetypefont.hpp>

#if USE_SDL2_TTF
# include <guisan/sdl/sdltruetypefont.hpp>
#endif

#include "platform.hpp"

Expand Down
11 changes: 7 additions & 4 deletions include/guisan/sdl/sdltruetypefont.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,18 @@
#ifndef GCN_CONTRIB_SDLTRUETYPEFONT_HPP
#define GCN_CONTRIB_SDLTRUETYPEFONT_HPP

#include <map>
#include <string>
#if !USE_SDL2_TTF
# error "USE_SDL2_TTF not activated"
#endif

#include "SDL_ttf.h"
#include "guisan/color.hpp"

#include "guisan/font.hpp"
#include "guisan/platform.hpp"

#include <SDL_ttf.h>
#include <map>
#include <string>

namespace gcn
{
class Graphics;
Expand Down
52 changes: 30 additions & 22 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ newoption {
description = "Set the output location for the generated files"
}

newoption {
trigger = "use-sdl_ttf",
value = "bool",
description = "Use SDL_TTF (for font)",
default = true
}

if (_ACTION == nil) then
return
end
Expand All @@ -13,14 +20,20 @@ local locationDir = _OPTIONS["to"] or path.join("solution", _ACTION, "stratagus"
local nugetPackages = {
"sdl2.nuget:2.28.0", "sdl2.nuget.redist:2.28.0",
"sdl2_image.nuget:2.6.3", "sdl2_image.nuget.redist:2.6.3",
"sdl2_mixer.nuget:2.6.3", "sdl2_mixer.nuget.redist:2.6.3",
"sdl2_net.nuget:2.2.0", "sdl2_net.nuget.redist:2.2.0",
"sdl2_ttf.nuget:2.20.2", "sdl2_ttf.nuget.redist:2.20.2"
"sdl2_mixer.nuget:2.6.3", "sdl2_mixer.nuget.redist:2.6.3" -- Sound for demo
}

if _OPTIONS["use-sdl_ttf"] then
nugetPackages = table.join(nugetPackages, {"sdl2_ttf.nuget:2.20.2", "sdl2_ttf.nuget.redist:2.20.2"})
end

function useGuisan()
includedirs { "include/" }
links { "guisan" }

if _OPTIONS["use-sdl_ttf"] then
defines { "USE_SDL2_TTF" }
end
end

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -81,6 +94,10 @@ project "guisan"
files { "src/SConscript" }
files { "*.*"}

if _OPTIONS["use-sdl_ttf"] then
defines { "USE_SDL2_TTF" }
end

includedirs { "include" }

-- ----------------------------------------------------------------------------
Expand All @@ -92,9 +109,8 @@ project "opengl_helloworld"

files { "examples/openglhelloworld.cpp", "examples/opengl_helper.hpp", "examples/helloworld_example.hpp" }

includedirs { "include" }
links { "guisan" }
links {"opengl32"}
useGuisan()
links { "opengl32" }

debugdir "examples"

Expand All @@ -105,9 +121,8 @@ project "opengl_widgets"

files { "examples/openglwidgets.cpp", "examples/opengl_helper.hpp", "examples/widgets_example.hpp" }

includedirs { "include" }
links { "guisan" }
links {"opengl32"}
useGuisan()
links { "opengl32" }

debugdir "examples"

Expand All @@ -118,8 +133,7 @@ project "sdl_action"

files { "examples/sdlaction.cpp", "examples/sdl_helper.hpp", "examples/action_example.hpp" }

includedirs { "include" }
links { "guisan" }
useGuisan()

debugdir "examples"

Expand All @@ -130,8 +144,7 @@ project "sdl_hello"

files { "examples/sdlhelloworld.cpp", "examples/sdl_helper.hpp", "examples/helloworld_example.hpp" }

includedirs { "include" }
links { "guisan" }
useGuisan()

debugdir "examples"

Expand All @@ -142,8 +155,7 @@ project "sdl_rickroll"

files { "examples/sdlrickroll.cpp", "examples/sdl_helper.hpp", "examples/rickroll_example.hpp" }

includedirs { "include" }
links { "guisan" }
useGuisan()

debugdir "examples"

Expand All @@ -154,8 +166,7 @@ project "sdl_widgets"

files { "examples/sdlwidgets.cpp", "examples/sdl_helper.hpp", "examples/widgets_example.hpp" }

includedirs { "include" }
links { "guisan" }
useGuisan()

debugdir "examples"

Expand All @@ -166,8 +177,7 @@ project "sdl2_widgets"

files { "examples/sdl2widgets.cpp", "examples/sdl2_helper.hpp", "examples/widgets_example.hpp" }

includedirs { "include" }
links { "guisan" }
useGuisan()

debugdir "examples"

Expand All @@ -181,9 +191,7 @@ project "Demo_ff"
files { "Demo/ff/**.*" }
includedirs { "Demo/ff/include" }

includedirs { "include" }
links { "guisan" }

useGuisan()
links {"opengl32"}

debugdir "Demo/ff"
2 changes: 2 additions & 0 deletions src/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ guisan = env.Clone()
if env['HAVE_SDL2']:
guisan.ParseConfig('pkg-config --cflags --libs sdl2')
guisan.ParseConfig('pkg-config --cflags --libs SDL2_image')
if env['HAVE_SDL2_TTF']:
guisan.ParseConfig('pkg-config --cflags --libs SDL2_ttf')

if env['HAVE_OPENGL']:
Expand All @@ -101,6 +102,7 @@ print("=========================================")
print(" Guisan build")
print("")
print(" SDL 2.0 support.............. " + str(env['HAVE_SDL2']))
print(" SDL 2.0 TTF support..........." + str(env['HAVE_SDL2_TTF']))
print(" OpenGL support............... " + str(env['HAVE_OPENGL']))
print("-----------------------------------------")
print(" Installation target")
Expand Down
30 changes: 16 additions & 14 deletions src/sdl/sdltruetypefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@
*/

/*
* For comments regarding functions please see the header file.
* For comments regarding functions please see the header file.
*/

#include "guisan/sdl/sdltruetypefont.hpp"
#if USE_SDL2_TTF
# include "guisan/sdl/sdltruetypefont.hpp"

#include "guisan/exception.hpp"
#include "guisan/image.hpp"
#include "guisan/graphics.hpp"
#include "guisan/sdl/sdlgraphics.hpp"
#include "guisan/sdl/sdl2graphics.hpp"
# include "guisan/exception.hpp"
# include "guisan/graphics.hpp"
# include "guisan/image.hpp"
# include "guisan/sdl/sdl2graphics.hpp"
# include "guisan/sdl/sdlgraphics.hpp"

namespace gcn
{
Expand All @@ -61,7 +62,7 @@ namespace gcn
{
if (mFont == nullptr)
{
throw GCN_EXCEPTION("SDLTrueTypeFont::SDLTrueTypeFont. "+std::string(TTF_GetError()));
throw GCN_EXCEPTION("SDLTrueTypeFont::SDLTrueTypeFont. " + std::string(TTF_GetError()));
}
}

Expand All @@ -83,8 +84,8 @@ namespace gcn
return TTF_FontHeight(mFont) + mRowSpacing;
}

void SDLTrueTypeFont::drawString(Graphics* graphics, const std::string& text, const int x, const int y,
bool enabled)
void SDLTrueTypeFont::drawString(
Graphics* graphics, const std::string& text, const int x, const int y, bool enabled)
{
if (text.empty())
{
Expand All @@ -94,11 +95,10 @@ namespace gcn
auto sdlGraphics = dynamic_cast<SDLGraphics*>(graphics);
auto sdl2Graphics = dynamic_cast<SDL2Graphics*>(graphics);


if (sdlGraphics == nullptr && sdl2Graphics == nullptr)
{
throw GCN_EXCEPTION("SDLTrueTypeFont::drawString. Graphics object not an SDL graphics object!");
return;
throw GCN_EXCEPTION(
"SDLTrueTypeFont::drawString. Graphics object not an SDL graphics object!");
}

// This is needed for drawing the Glyph in the middle if we have spacing
Expand Down Expand Up @@ -185,4 +185,6 @@ namespace gcn
{
mColor = color;
}
}
} // namespace gcn

#endif

0 comments on commit ce13dce

Please sign in to comment.