libtcod
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1/* BSD 3-Clause License
2 *
3 * Copyright © 2008-2025, 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
73class Context {
74 public:
75 Context() = default;
76
81 explicit Context(const TCOD_ContextParams& params) {
82#ifndef NO_SDL
83 struct TCOD_Context* context = nullptr;
84 check_throw_error(TCOD_context_new(&params, &context));
85 context_ = ContextPtr{context};
86#else
87 throw std::logic_error("Libtcod not compiled with SDL support, so it can not create its own context.");
88#endif // NO_SDL
89 };
91 explicit Context(ContextPtr&& ptr) : context_{std::move(ptr)} {}
93 explicit Context(TCOD_Context* ptr) : context_{ptr} {}
94
95 // Copy disabled, move allowed.
96 Context(const Context&) = delete;
97 Context& operator=(const Context&) = delete;
98 Context(Context&&) = default;
99 Context& operator=(Context&&) = default;
100
101
104 [[nodiscard]] auto get_renderer_type() noexcept -> TCOD_renderer_t {
105 return static_cast<TCOD_renderer_t>(TCOD_context_get_renderer_type(context_.get()));
106 }
107
134 void present(const TCOD_Console& console, const TCOD_ViewportOptions& viewport) {
135 tcod::check_throw_error(TCOD_context_present(context_.get(), &console, &viewport));
136 }
137
142 void present(const TCOD_Console& console) {
143 tcod::check_throw_error(TCOD_context_present(context_.get(), &console, nullptr));
144 }
145
159 [[nodiscard]] auto get_sdl_window() noexcept -> struct SDL_Window* {
160 return TCOD_context_get_sdl_window(context_.get());
161 }
162
178 [[nodiscard]] auto get_sdl_renderer() noexcept -> struct SDL_Renderer* {
179 return TCOD_context_get_sdl_renderer(context_.get());
180 }
181
184 [[nodiscard]] auto pixel_to_tile_coordinates(const std::array<int, 2>& xy) -> std::array<int, 2> {
185 std::array<int, 2> out{xy[0], xy[1]};
186 tcod::check_throw_error(TCOD_context_screen_pixel_to_tile_i(context_.get(), &out[0], &out[1]));
187 return out;
188 }
189
192 [[nodiscard]] auto pixel_to_tile_coordinates(const std::array<double, 2>& xy) -> std::array<double, 2> {
193 std::array<double, 2> out{xy[0], xy[1]};
194 tcod::check_throw_error(TCOD_context_screen_pixel_to_tile_d(context_.get(), &out[0], &out[1]));
195 return out;
196 }
197
226 void convert_event_coordinates(SDL_Event& event) {
228 }
229
234 void save_screenshot(const std::filesystem::path& path) {
235 tcod::check_throw_error(TCOD_context_save_screenshot(context_.get(), path.string().c_str()));
236 }
237
240 void save_screenshot() { tcod::check_throw_error(TCOD_context_save_screenshot(context_.get(), nullptr)); }
241
267 [[nodiscard]] auto new_console(int min_columns = 1, int min_rows = 1, float magnification = 1.0f) -> tcod::Console {
268 int columns;
269 int rows;
270 if (magnification <= 0.0f) {
271 throw std::invalid_argument(
272 std::string("Magnification must be greater than zero. Got ") + std::to_string(magnification));
273 }
274 tcod::check_throw_error(TCOD_context_recommended_console_size(context_.get(), magnification, &columns, &rows));
275 return tcod::Console{std::max(columns, min_columns), std::max(rows, min_rows)};
276 }
277
280 auto change_tileset(tcod::Tileset& new_tileset) -> void {
281 check_throw_error(TCOD_context_change_tileset(context_.get(), new_tileset.get()));
282 }
283
290 auto set_mouse_transform(const TCOD_MouseTransform& transform) -> void {
291 check_throw_error(TCOD_context_set_mouse_transform(context_.get(), &transform));
292 }
293
294
297 [[nodiscard]] auto get_ptr() noexcept -> ContextPtr& { return context_; }
298
301 [[nodiscard]] auto get_ptr() const noexcept -> const ContextPtr& { return context_; }
302
305 auto close() -> void { *this = Context(); }
306
307 private:
308 ContextPtr context_ = nullptr;
309};
310} // namespace tcod
311#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 bresenham.hpp:157
int check_throw_error(int error)
Check and throw error messages.
Definition error.hpp:57
Tileset module.