Skip to content

Commit

Permalink
Inline resource loading code into examples for self-containment
Browse files Browse the repository at this point in the history
  • Loading branch information
ltjax committed Nov 18, 2019
1 parent 6e9aa7a commit 4d6378e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 69 deletions.
20 changes: 7 additions & 13 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
add_custom_target(examples COMMENT "Build all examples")
add_dependencies(check examples)

# examples common code
# ====================

add_library(examples-common EXCLUDE_FROM_ALL
common/resources.hpp
common/resources.cpp)

target_compile_definitions(examples-common PRIVATE LAGER_PREFIX_PATH="\\"${CMAKE_INSTALL_PREFIX}\\"")
target_include_directories(examples-common INTERFACE common/)
# inject LAGER_PREFIX_PATH into examples
add_library(lager-prefix INTERFACE)
target_compile_definitions(lager-prefix INTERFACE LAGER_PREFIX_PATH="\\"${CMAKE_INSTALL_PREFIX}\\"")
# std examples
# ====================
Expand All @@ -34,7 +28,7 @@ set(counter_ncurses_include_directories
${CURSES_INCLUDE_DIR})
set(counter_ncurses_link_libraries
lager-debugger
examples-common
lager-prefix
${CURSES_LIBRARIES})
add_executable(counter-ncurses EXCLUDE_FROM_ALL ${counter_ncurses_sources})
Expand Down Expand Up @@ -69,7 +63,7 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(counter-sdl2
lager-debugger
examples-common
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples counter-sdl2)
Expand All @@ -82,7 +76,7 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(autopong
lager-debugger
examples-common
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples autopong)
Expand All @@ -96,7 +90,7 @@ if (SDL2_FOUND AND SDL2_ttf_FOUND)
${SDL2_TTF_INCLUDE_DIR})
target_link_libraries(autopong-debug
lager-debugger
examples-common
lager-prefix
${SDL2_LIBRARY}
${SDL2_TTF_LIBRARIES})
add_dependencies(examples autopong-debug)
Expand Down
16 changes: 13 additions & 3 deletions example/autopong/sdl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
//

#include "../autopong.hpp"
#include "resources.hpp"

#include <SDL.h>
#include <SDL_ttf.h>
Expand All @@ -26,12 +25,23 @@
#include <string>
#include <variant>

#ifndef LAGER_PREFIX_PATH
#error LAGER_PREFIX_PATH needs to be defined for examples
#endif

constexpr auto font_size = 32;

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

std::string font_path()
{
using namespace std::string_literals;
return example_common::resources_path() + "/SourceSansPro-Bold.ttf"s;
return resources_path() + "/SourceSansPro-Bold.ttf"s;
}

struct sdl_view
Expand Down Expand Up @@ -144,7 +154,7 @@ int main(int argc, const char** argv)
SDL_SetRelativeMouseMode(SDL_TRUE);

#ifdef DEBUGGER
auto debugger = lager::http_debug_server{argc, argv, 8080, example_common::resources_path()};
auto debugger = lager::http_debug_server{argc, argv, 8080, resources_path()};
#endif
auto view = sdl_view{};
auto loop = lager::sdl_event_loop{};
Expand Down
25 changes: 0 additions & 25 deletions example/common/resources.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions example/common/resources.hpp

This file was deleted.

16 changes: 13 additions & 3 deletions example/counter/ncurses/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

#include "../counter.hpp"
#include "terminal.hpp"
#include "resources.hpp"

#include <lager/debug/debugger.hpp>
#include <lager/debug/http_server.hpp>
Expand All @@ -22,8 +21,19 @@

#include <zug/compose.hpp>

#ifndef LAGER_PREFIX_PATH
#error LAGER_PREFIX_PATH needs to be defined for examples
#endif

using namespace std::string_literals;

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

void draw(const counter::model& c)
{
static const auto prelude = "current value is "s;
Expand Down Expand Up @@ -62,10 +72,10 @@ int main(int argc, const char** argv)
::init_pair(2, COLOR_WHITE, COLOR_RED);
::init_pair(3, COLOR_WHITE, COLOR_BLUE);
#if defined(DEBUGGER) || defined(TREE_DEBUGGER)
auto debugger = lager::http_debug_server{argc, argv, 8080, example_common::resources_path()};
auto debugger = lager::http_debug_server{argc, argv, 8080, resources_path()};
#endif
#ifdef META_DEBUGGER
auto meta_debugger = lager::http_debug_server{argc, argv, 8081, example_common::resources_path()};
auto meta_debugger = lager::http_debug_server{argc, argv, 8081, resources_path()};
#endif
auto store = lager::make_store<counter::action>(
counter::model{},
Expand Down
14 changes: 12 additions & 2 deletions example/counter/sdl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,27 @@
#include <SDL_ttf.h>

#include "../counter.hpp"
#include "resources.hpp"
#include <lager/event_loop/sdl.hpp>
#include <lager/store.hpp>

#include <iostream>
#include <string>

#ifndef LAGER_PREFIX_PATH
#error LAGER_PREFIX_PATH needs to be defined for examples
#endif

inline const char* resources_path()
{
auto env_resources_path = std::getenv("LAGER_RESOURCES_PATH");
return env_resources_path ? env_resources_path
: LAGER_PREFIX_PATH "/share/lager";
}

std::string font_path()
{
using namespace std::string_literals;
return example_common::resources_path() + "/SourceSansPro-Regular.ttf"s;
return resources_path() + "/SourceSansPro-Regular.ttf"s;
}

struct sdl_view
Expand Down

0 comments on commit 4d6378e

Please sign in to comment.