libtcod
Loading...
Searching...
No Matches
context.h
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_H_
36#define LIBTCOD_CONTEXT_H_
37
38#ifdef __cplusplus
39#include <algorithm>
40#include <array>
41#include <memory>
42#include <stdexcept>
43
44#include "console_types.hpp"
45#include "error.hpp"
46#endif // __cplusplus
47
48#include "config.h"
49#include "console.h"
50#include "context_viewport.h"
51#include "error.h"
52#include "mouse_types.h"
53#include "tileset.h"
54
55struct SDL_Window;
56struct SDL_Renderer;
57struct SDL_Rect;
58union SDL_Event;
59
60struct TCOD_Context; // Defined in this header later.
61typedef struct TCOD_Context TCOD_Context;
62
68typedef struct TCOD_ContextParams {
73
82 int window_x, window_y;
87 int pixel_width, pixel_height;
94 int columns, rows;
110 int vsync;
124 const char* window_title;
128 int argc;
135 const char* const* argv;
144 void (*cli_output)(void* userdata, const char* output);
154
164
165#ifdef __cplusplus
166extern "C" {
167#endif // __cplusplus
174TCOD_PUBLIC void TCOD_context_delete(struct TCOD_Context* renderer);
180TCOD_NODISCARD struct TCOD_Context* TCOD_context_new_(void);
194 struct TCOD_Context* context, const struct TCOD_Console* console, const struct TCOD_ViewportOptions* viewport);
206TCOD_PUBLIC TCOD_Error TCOD_context_screen_pixel_to_tile_d(struct TCOD_Context* context, double* x, double* y);
215TCOD_PUBLIC TCOD_Error TCOD_context_screen_pixel_to_tile_i(struct TCOD_Context* context, int* x, int* y);
221TCOD_PUBLIC TCOD_Error TCOD_context_convert_event_coordinates(struct TCOD_Context* context, union SDL_Event* event);
227TCOD_PUBLIC TCOD_Error TCOD_context_save_screenshot(struct TCOD_Context* context, const char* filename);
233TCOD_PUBLIC struct SDL_Window* TCOD_context_get_sdl_window(struct TCOD_Context* context);
239TCOD_PUBLIC struct SDL_Renderer* TCOD_context_get_sdl_renderer(struct TCOD_Context* context);
253TCOD_PUBLIC int TCOD_context_get_renderer_type(struct TCOD_Context* context);
263 struct TCOD_Context* context, float magnification, int* __restrict columns, int* __restrict rows);
264
277 struct TCOD_Context* __restrict context,
278 TCOD_ColorRGBA* __restrict out_pixels,
279 int* __restrict width,
280 int* __restrict height);
281
291TCOD_NODISCARD
293 struct TCOD_Context* __restrict context, int* __restrict width, int* __restrict height);
294
304 struct TCOD_Context* __restrict context, const TCOD_MouseTransform* __restrict transform);
305#ifdef __cplusplus
306} // extern "C"
307#endif // __cplusplus
313#ifdef __cplusplus
318 [[deprecated(
319 "TCOD_Context methods have been moved to tcod::Context,"
320 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
321 get_renderer_type() noexcept -> int {
323 }
324
334 [[deprecated(
335 "TCOD_Context methods have been moved to tcod::Context,"
336 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
337 present(const TCOD_Console& console, const TCOD_ViewportOptions& viewport) {
338 tcod::check_throw_error(TCOD_context_present(this, &console, &viewport));
339 }
340
349 [[deprecated(
350 "TCOD_Context methods have been moved to tcod::Context,"
351 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
352 present(const TCOD_Console& console) {
353 tcod::check_throw_error(TCOD_context_present(this, &console, nullptr));
354 }
355
364 [[deprecated(
365 "TCOD_Context methods have been moved to tcod::Context,"
366 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
367 get_sdl_window() noexcept -> struct SDL_Window* {
368 return TCOD_context_get_sdl_window(this);
369 }
370
379 [[deprecated(
380 "TCOD_Context methods have been moved to tcod::Context,"
381 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
382 get_sdl_renderer() noexcept -> struct SDL_Renderer* {
384 }
385
392 [[deprecated(
393 "TCOD_Context methods have been moved to tcod::Context,"
394 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
395 pixel_to_tile_coordinates(const std::array<int, 2>& xy) -> std::array<int, 2> {
396 std::array<int, 2> out{xy[0], xy[1]};
398 return out;
399 }
400
407 [[deprecated(
408 "TCOD_Context methods have been moved to tcod::Context,"
409 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
410 pixel_to_tile_coordinates(const std::array<double, 2>& xy) -> std::array<double, 2> {
411 std::array<double, 2> out{xy[0], xy[1]};
413 return out;
414 }
415
427 [[deprecated(
428 "TCOD_Context methods have been moved to tcod::Context,"
429 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
433
443 [[deprecated(
444 "TCOD_Context methods have been moved to tcod::Context,"
445 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
446 save_screenshot(const char* filepath) {
448 }
449
458 [[deprecated(
459 "TCOD_Context methods have been moved to tcod::Context,"
460 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
461 save_screenshot(const std::string& filepath) {
463 }
464
480 auto new_console(int min_columns = 1, int min_rows = 1, float magnification = 1.0f) -> tcod::Console {
481 int columns;
482 int rows;
483 if (magnification <= 0.0f) {
484 throw std::invalid_argument(
485 std::string("Magnification must be greater than zero. Got ") + std::to_string(magnification));
486 }
487 tcod::check_throw_error(TCOD_context_recommended_console_size(this, magnification, &columns, &rows));
488 return tcod::Console{std::max(columns, min_columns), std::max(rows, min_rows)};
489 }
490#endif // __cplusplus
491 // All remaining members are private.
495 int type;
499 void* __restrict contextdata_;
500 // Context C callback are prefixed with 'c_', always check if see if these are NULL.
504 void (*c_destructor_)(struct TCOD_Context* __restrict self);
512 TCOD_Error (*c_present_)(
513 struct TCOD_Context* __restrict self,
514 const struct TCOD_Console* __restrict console,
515 const struct TCOD_ViewportOptions* __restrict viewport);
519 void (*c_pixel_to_tile_)(struct TCOD_Context* __restrict self, double* __restrict x, double* __restrict y);
523 TCOD_Error (*c_save_screenshot_)(struct TCOD_Context* __restrict self, const char* __restrict filename);
527 struct SDL_Window* (*c_get_sdl_window_)(struct TCOD_Context* __restrict self);
531 struct SDL_Renderer* (*c_get_sdl_renderer_)(struct TCOD_Context* __restrict self);
536 TCOD_Error (*c_accumulate_)(
537 struct TCOD_Context* __restrict self,
538 const struct TCOD_Console* __restrict console,
539 const struct TCOD_ViewportOptions* __restrict viewport);
543 TCOD_Error (*c_set_tileset_)(struct TCOD_Context* __restrict self, TCOD_Tileset* __restrict tileset);
550 TCOD_Error (*c_recommended_console_size_)(
551 struct TCOD_Context* __restrict self, float magnification, int* __restrict columns, int* __restrict rows);
552
560 TCOD_Error (*c_screen_capture_)(
561 struct TCOD_Context* __restrict self,
562 TCOD_ColorRGBA* __restrict out_pixels,
563 int* __restrict width,
564 int* __restrict height);
565
571 TCOD_Error (*c_set_mouse_transform_)(
572 struct TCOD_Context* __restrict self, const TCOD_MouseTransform* __restrict transform);
573};
574#ifdef __cplusplus
575namespace tcod {
577 void operator()(TCOD_Context* console) const { TCOD_context_delete(console); }
578};
579typedef std::unique_ptr<TCOD_Context, ContextDeleter> ContextPtr;
580typedef std::shared_ptr<TCOD_Context> ContextSharedPtr;
581} // namespace tcod
582#endif // __cplusplus
583#endif // LIBTCOD_CONTEXT_H_
Libtcod config header.
Various console functions.
C++ types for console functions.
TCOD_ColorRGBA * TCOD_context_screen_capture_alloc(struct TCOD_Context *context, int *width, int *height)
Allocate and return a screen capture.
TCOD_Error TCOD_context_screen_capture(struct TCOD_Context *context, TCOD_ColorRGBA *out_pixels, int *width, int *height)
Fill out_pixels with a screen capture.
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.
void TCOD_context_delete(struct TCOD_Context *renderer)
Delete a rendering context.
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 viewport options.
Error handling module.
TCOD_Error
An enum of libtcod error codes.
Definition error.h:48
Error handling module.
Mouse state provided by the libtcod event system.
The libtcod namespace.
Definition color.hpp:45
int check_throw_error(int error)
Check and throw error messages.
Definition error.hpp:57
A 4-channel RGBA color struct.
Definition color.h:89
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:121
A rendering context for libtcod.
Definition context.h:312
void save_screenshot(const std::string &filepath)
Save a screenshot to filepath.
Definition context.h:461
void present(const TCOD_Console &console)
Present a console to the display.
Definition context.h:352
auto get_sdl_renderer() noexcept -> struct SDL_Renderer *
Return a non-owning pointer to the SDL_Renderer used by this context.
Definition context.h:382
auto pixel_to_tile_coordinates(const std::array< int, 2 > &xy) -> std::array< int, 2 >
Convert pixel coordinates to this contexts integer tile coordinates.
Definition context.h:395
void convert_event_coordinates(SDL_Event &event)
Convert the pixel coordinates of SDL mouse events to the tile coordinates of the current context.
Definition context.h:430
void save_screenshot(const char *filepath)
Save a screenshot to filepath.
Definition context.h:446
auto get_sdl_window() noexcept -> struct SDL_Window *
Return a non-owning pointer to the SDL_Window used by this context.
Definition context.h:367
auto pixel_to_tile_coordinates(const std::array< double, 2 > &xy) -> std::array< double, 2 >
Convert pixel coordinates to this contexts sub-tile coordinates.
Definition context.h:410
void present(const TCOD_Console &console, const TCOD_ViewportOptions &viewport)
Present a console to the display with the provided viewport options.
Definition context.h:337
int type
The TCOD_renderer_t value of this context.
Definition context.h:495
auto get_renderer_type() noexcept -> int
Return the TCOD_renderer_t value of this context which may be different than the one requested.
Definition context.h:321
auto new_console(int min_columns=1, int min_rows=1, float magnification=1.0f) -> tcod::Console
Return a new console with a size automatically determined by the context.
Definition context.h:480
A struct of parameters used to create a new context with TCOD_context_new.
Definition context.h:68
TCOD_Tileset * tileset
tileset is an optional pointer to a tileset object.
Definition context.h:105
int columns
columns and rows are the desired size of the terminal window.
Definition context.h:94
int window_x
window_x and window_y are the starting position of the window.
Definition context.h:82
int tcod_version
Compiled libtcod version for ABI compatiblity with older versions of libtcod.
Definition context.h:72
void(* cli_output)(void *userdata, const char *output)
If user attention is required for the given CLI parameters then cli_output will be called with cli_us...
Definition context.h:144
bool window_xy_defined
If this is false then window_x/window_y parameters of zero are assumed to be undefined and will be ch...
Definition context.h:153
int vsync
If vsync is true, then vertical sync will be enabled whenever possible.
Definition context.h:110
int pixel_width
pixel_width and pixel_height are the desired size of the window in pixels.
Definition context.h:87
const char * window_title
window_title will be the title of the opened window.
Definition context.h:124
const char *const * argv
argc and argv are optional CLI parameters.
Definition context.h:135
int renderer_type
renderer_type is one of the TCOD_renderer_t values.
Definition context.h:98
void * cli_userdata
This is passed to the userdata parameter of cli_output if called.
Definition context.h:148
TCOD_Console * console
A console to be used as a reference for the desired window size.
Definition context.h:162
int sdl_window_flags
sdl_window_flags is a bitfield of SDL_WindowFlags flags.
Definition context.h:118
int argc
The number of items in argv.
Definition context.h:128
Info needed to convert between mouse pixel and tile coordinates.
Definition mouse_types.h:117
A container for libtcod tileset graphics.
Definition tileset.h:60
Viewport options for the rendering context.
Definition context_viewport.h:46
Definition context.h:576
Tileset module.