-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
84 lines (72 loc) · 2.37 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif // __EMSCRIPTEN__
#include <SDL.h>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <libtcod.hpp>
#if defined(_MSC_VER)
#pragma warning(disable : 4297) // Allow "throw" in main(). Letting the compiler handle termination.
#endif
/// Return the data directory.
auto get_data_dir() -> std::filesystem::path {
static auto root_directory = std::filesystem::path{"."}; // Begin at the working directory.
while (!std::filesystem::exists(root_directory / "data")) {
// If the current working directory is missing the data dir then it will assume it exists in any parent directory.
root_directory /= "..";
if (!std::filesystem::exists(root_directory)) {
throw std::runtime_error("Could not find the data directory.");
}
}
return root_directory / "data";
};
static constexpr auto WHITE = tcod::ColorRGB{255, 255, 255};
static tcod::Console g_console; // The global console object.
static tcod::Context g_context; // The global libtcod context.
/// Game loop.
void main_loop() {
// Rendering.
g_console.clear();
tcod::print(g_console, {0, 0}, "Hello World", WHITE, std::nullopt);
g_context.present(g_console);
// Handle input.
SDL_Event event;
#ifndef __EMSCRIPTEN__
// Block until events exist. This conserves resources well but isn't compatible with animations or Emscripten.
SDL_WaitEvent(nullptr);
#endif
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
std::exit(EXIT_SUCCESS);
break;
}
}
}
/// Main program entry point.
int main(int argc, char** argv) {
try {
auto params = TCOD_ContextParams{};
params.tcod_version = TCOD_COMPILEDVERSION;
params.argc = argc;
params.argv = argv;
params.renderer_type = TCOD_RENDERER_SDL2;
params.vsync = 1;
params.sdl_window_flags = SDL_WINDOW_RESIZABLE;
params.window_title = "Libtcod Template Project";
auto tileset = tcod::load_tilesheet(get_data_dir() / "dejavu16x16_gs_tc.png", {32, 8}, tcod::CHARMAP_TCOD);
params.tileset = tileset.get();
g_console = tcod::Console{80, 40};
params.console = g_console.get();
g_context = tcod::Context(params);
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(main_loop, 0, 0);
#else
while (true) main_loop();
#endif
} catch (const std::exception& exc) {
std::cerr << exc.what() << "\n";
throw;
}
}