libtcod
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2026, Jice and the libtcod contributors.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
34#pragma once
35#ifndef LIBTCOD_CONTEXT_HPP_
36#define LIBTCOD_CONTEXT_HPP_
37#include <array>
38#include <filesystem>
39#include <stdexcept>
40
41#include "config.h"
42#include "console.h"
43#include "console_types.h"
44#include "context.h"
45#include "context_init.h"
46#include "context_viewport.h"
47#include "error.hpp"
48#include "mouse_types.h"
49#include "tileset.hpp"
50
51namespace tcod {
52
71class Context {
72 public:
73 Context() = default;
74
79 explicit Context(const TCOD_ContextParams& params) {
80#ifndef NO_SDL
81 struct TCOD_Context* context = nullptr;
82 check_throw_error(TCOD_context_new(&params, &context));
83 context_ = ContextPtr{context};
84#else
85 throw std::logic_error("Libtcod not compiled with SDL support, so it can not create its own context.");
86#endif // NO_SDL
87 };
89 explicit Context(ContextPtr&& ptr) : context_{std::move(ptr)} {}
91 explicit Context(TCOD_Context* ptr) : context_{ptr} {}
92
93 // Copy disabled, move allowed.
94 Context(const Context&) = delete;
95 Context& operator=(const Context&) = delete;
96 Context(Context&&) = default;
97 Context& operator=(Context&&) = default;
98
99
102 [[nodiscard]] auto get_renderer_type() noexcept -> TCOD_renderer_t {
103 return static_cast<TCOD_renderer_t>(TCOD_context_get_renderer_type(context_.get()));
104 }
105
132 void present(const TCOD_Console& console, const TCOD_ViewportOptions& viewport) {
133 tcod::check_throw_error(TCOD_context_present(context_.get(), &console, &viewport));
134 }
135
140 void present(const TCOD_Console& console) {
141 tcod::check_throw_error(TCOD_context_present(context_.get(), &console, nullptr));
142 }
143
157 [[nodiscard]] auto get_sdl_window() noexcept -> struct SDL_Window* {
158 return TCOD_context_get_sdl_window(context_.get());
159 }
160
176 [[nodiscard]] auto get_sdl_renderer() noexcept -> struct SDL_Renderer* {
177 return TCOD_context_get_sdl_renderer(context_.get());
178 }
179
182 [[nodiscard]] auto pixel_to_tile_coordinates(const std::array<int, 2>& xy) -> std::array<int, 2> {
183 std::array<int, 2> out{xy[0], xy[1]};
184 tcod::check_throw_error(TCOD_context_screen_pixel_to_tile_i(context_.get(), &out[0], &out[1]));
185 return out;
186 }
187
190 [[nodiscard]] auto pixel_to_tile_coordinates(const std::array<double, 2>& xy) -> std::array<double, 2> {
191 std::array<double, 2> out{xy[0], xy[1]};
192 tcod::check_throw_error(TCOD_context_screen_pixel_to_tile_d(context_.get(), &out[0], &out[1]));
193 return out;
194 }
195
224 void convert_event_coordinates(SDL_Event& event) {
226 }
227
232 void save_screenshot(const std::filesystem::path& path) {
233 tcod::check_throw_error(TCOD_context_save_screenshot(context_.get(), path.string().c_str()));
234 }
235
238 void save_screenshot() { tcod::check_throw_error(TCOD_context_save_screenshot(context_.get(), nullptr)); }
239
265 [[nodiscard]] auto new_console(int min_columns = 1, int min_rows = 1, float magnification = 1.0f) -> tcod::Console {
266 int columns;
267 int rows;
268 if (magnification <= 0.0f) {
269 throw std::invalid_argument(
270 std::string("Magnification must be greater than zero. Got ") + std::to_string(magnification));
271 }
272 tcod::check_throw_error(TCOD_context_recommended_console_size(context_.get(), magnification, &columns, &rows));
273 return tcod::Console{std::max(columns, min_columns), std::max(rows, min_rows)};
274 }
275
278 auto change_tileset(tcod::Tileset& new_tileset) -> void {
279 check_throw_error(TCOD_context_change_tileset(context_.get(), new_tileset.get()));
280 }
281
288 auto set_mouse_transform(const TCOD_MouseTransform& transform) -> void {
289 check_throw_error(TCOD_context_set_mouse_transform(context_.get(), &transform));
290 }
291
292
295 [[nodiscard]] auto get_ptr() noexcept -> ContextPtr& { return context_; }
296
299 [[nodiscard]] auto get_ptr() const noexcept -> const ContextPtr& { return context_; }
300
303 auto close() -> void { *this = Context(); }
304
305 private:
306 ContextPtr context_ = nullptr;
307};
308} // namespace tcod
309#endif // LIBTCOD_CONTEXT_HPP_
Libtcod config header.
Various console functions.
C types for console functions.
TCOD_renderer_t
Libtcod rendering modes.
Definition console_types.h:491
Libtcod display management context.
struct TCOD_ContextParams TCOD_ContextParams
A struct of parameters used to create a new context with TCOD_context_new.
TCOD_Error TCOD_context_present(struct TCOD_Context *context, const struct TCOD_Console *console, const struct TCOD_ViewportOptions *viewport)
Present a console to the screen, using a rendering context.
int TCOD_context_get_renderer_type(struct TCOD_Context *context)
Return the TCOD_renderer_t renderer type for this context.
TCOD_Error TCOD_context_recommended_console_size(struct TCOD_Context *context, float magnification, int *columns, int *rows)
Set columns and rows to the recommended console size for this context.
TCOD_Error TCOD_context_screen_pixel_to_tile_i(struct TCOD_Context *context, int *x, int *y)
Convert the screen coordinates to integer tile coordinates for this context.
TCOD_Error TCOD_context_set_mouse_transform(struct TCOD_Context *context, const TCOD_MouseTransform *transform)
Manually set the pixel-to-tile mouse position transformation.
struct SDL_Window * TCOD_context_get_sdl_window(struct TCOD_Context *context)
Return a pointer the SDL_Window for this context if it uses one.
TCOD_Error TCOD_context_change_tileset(struct TCOD_Context *self, TCOD_Tileset *tileset)
Change the active tileset for this context.
struct SDL_Renderer * TCOD_context_get_sdl_renderer(struct TCOD_Context *context)
Return a pointer the SDL_Renderer for this context if it uses one.
TCOD_Error TCOD_context_convert_event_coordinates(struct TCOD_Context *context, union SDL_Event *event)
Convert the pixel coordinates of SDL mouse events to the tile coordinates of the current context.
TCOD_Error TCOD_context_save_screenshot(struct TCOD_Context *context, const char *filename)
Save the last presented console to a PNG file.
TCOD_Error TCOD_context_screen_pixel_to_tile_d(struct TCOD_Context *context, double *x, double *y)
Convert the screen coordinates to tile coordinates for this context.
Context initialization module.
TCOD_Error TCOD_context_new(const TCOD_ContextParams *params, TCOD_Context **out)
Create a new context with the given parameters.
Context viewport options.
Error handling module.
Mouse state provided by the libtcod event system.
struct TCOD_MouseTransform TCOD_MouseTransform
Info needed to convert between mouse pixel and tile coordinates.
The libtcod namespace.
Definition color.hpp:45
int check_throw_error(int error)
Check and throw error messages.
Definition error.hpp:57
Tileset module.