libtcod
Loading...
Searching...
No Matches
context.h
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_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
70typedef struct TCOD_ContextParams {
75
84 int window_x, window_y;
89 int pixel_width, pixel_height;
96 int columns, rows;
112 int vsync;
126 const char* window_title;
130 int argc;
137 const char* const* argv;
146 void (*cli_output)(void* userdata, const char* output);
156
166
167#ifdef __cplusplus
168extern "C" {
169#endif // __cplusplus
176TCOD_PUBLIC void TCOD_context_delete(struct TCOD_Context* renderer);
182TCOD_NODISCARD struct TCOD_Context* TCOD_context_new_(void);
198 struct TCOD_Context* context, const struct TCOD_Console* console, const struct TCOD_ViewportOptions* viewport);
212TCOD_PUBLIC TCOD_Error TCOD_context_screen_pixel_to_tile_d(struct TCOD_Context* context, double* x, double* y);
223TCOD_PUBLIC TCOD_Error TCOD_context_screen_pixel_to_tile_i(struct TCOD_Context* context, int* x, int* y);
231TCOD_PUBLIC TCOD_Error TCOD_context_convert_event_coordinates(struct TCOD_Context* context, union SDL_Event* event);
239TCOD_PUBLIC TCOD_Error TCOD_context_save_screenshot(struct TCOD_Context* context, const char* filename);
247TCOD_PUBLIC struct SDL_Window* TCOD_context_get_sdl_window(struct TCOD_Context* context);
255TCOD_PUBLIC struct SDL_Renderer* TCOD_context_get_sdl_renderer(struct TCOD_Context* context);
273TCOD_PUBLIC int TCOD_context_get_renderer_type(struct TCOD_Context* context);
285 struct TCOD_Context* context, float magnification, int* __restrict columns, int* __restrict rows);
286
301 struct TCOD_Context* __restrict context,
302 TCOD_ColorRGBA* __restrict out_pixels,
303 int* __restrict width,
304 int* __restrict height);
305
317TCOD_NODISCARD
319 struct TCOD_Context* __restrict context, int* __restrict width, int* __restrict height);
320
330 struct TCOD_Context* __restrict context, const TCOD_MouseTransform* __restrict transform);
331#ifdef __cplusplus
332} // extern "C"
333#endif // __cplusplus
341#ifdef __cplusplus
346 [[deprecated(
347 "TCOD_Context methods have been moved to tcod::Context,"
348 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
349 get_renderer_type() noexcept -> int {
351 }
352
362 [[deprecated(
363 "TCOD_Context methods have been moved to tcod::Context,"
364 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
365 present(const TCOD_Console& console, const TCOD_ViewportOptions& viewport) {
366 tcod::check_throw_error(TCOD_context_present(this, &console, &viewport));
367 }
368
377 [[deprecated(
378 "TCOD_Context methods have been moved to tcod::Context,"
379 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
380 present(const TCOD_Console& console) {
381 tcod::check_throw_error(TCOD_context_present(this, &console, nullptr));
382 }
383
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 get_sdl_window() noexcept -> struct SDL_Window* {
396 return TCOD_context_get_sdl_window(this);
397 }
398
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 get_sdl_renderer() noexcept -> struct SDL_Renderer* {
412 }
413
420 [[deprecated(
421 "TCOD_Context methods have been moved to tcod::Context,"
422 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
423 pixel_to_tile_coordinates(const std::array<int, 2>& xy) -> std::array<int, 2> {
424 std::array<int, 2> out{xy[0], xy[1]};
426 return out;
427 }
428
435 [[deprecated(
436 "TCOD_Context methods have been moved to tcod::Context,"
437 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] auto
438 pixel_to_tile_coordinates(const std::array<double, 2>& xy) -> std::array<double, 2> {
439 std::array<double, 2> out{xy[0], xy[1]};
441 return out;
442 }
443
455 [[deprecated(
456 "TCOD_Context methods have been moved to tcod::Context,"
457 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
461
471 [[deprecated(
472 "TCOD_Context methods have been moved to tcod::Context,"
473 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
474 save_screenshot(const char* filepath) {
476 }
477
486 [[deprecated(
487 "TCOD_Context methods have been moved to tcod::Context,"
488 " use `auto context = tcod::Context(params);` to make a tcod context for C++.")]] void
489 save_screenshot(const std::string& filepath) {
491 }
492
508 auto new_console(int min_columns = 1, int min_rows = 1, float magnification = 1.0f) -> tcod::Console {
509 int columns;
510 int rows;
511 if (magnification <= 0.0f) {
512 throw std::invalid_argument(
513 std::string("Magnification must be greater than zero. Got ") + std::to_string(magnification));
514 }
515 tcod::check_throw_error(TCOD_context_recommended_console_size(this, magnification, &columns, &rows));
516 return tcod::Console{std::max(columns, min_columns), std::max(rows, min_rows)};
517 }
518#endif // __cplusplus
519 // All remaining members are private.
523 int type;
527 void* __restrict contextdata_;
528 // Context C callback are prefixed with 'c_', always check if see if these are NULL.
532 void (*c_destructor_)(struct TCOD_Context* __restrict self);
540 TCOD_Error (*c_present_)(
541 struct TCOD_Context* __restrict self,
542 const struct TCOD_Console* __restrict console,
543 const struct TCOD_ViewportOptions* __restrict viewport);
547 void (*c_pixel_to_tile_)(struct TCOD_Context* __restrict self, double* __restrict x, double* __restrict y);
551 TCOD_Error (*c_save_screenshot_)(struct TCOD_Context* __restrict self, const char* __restrict filename);
555 struct SDL_Window* (*c_get_sdl_window_)(struct TCOD_Context* __restrict self);
559 struct SDL_Renderer* (*c_get_sdl_renderer_)(struct TCOD_Context* __restrict self);
564 TCOD_Error (*c_accumulate_)(
565 struct TCOD_Context* __restrict self,
566 const struct TCOD_Console* __restrict console,
567 const struct TCOD_ViewportOptions* __restrict viewport);
571 TCOD_Error (*c_set_tileset_)(struct TCOD_Context* __restrict self, TCOD_Tileset* __restrict tileset);
578 TCOD_Error (*c_recommended_console_size_)(
579 struct TCOD_Context* __restrict self, float magnification, int* __restrict columns, int* __restrict rows);
580
588 TCOD_Error (*c_screen_capture_)(
589 struct TCOD_Context* __restrict self,
590 TCOD_ColorRGBA* __restrict out_pixels,
591 int* __restrict width,
592 int* __restrict height);
593
599 TCOD_Error (*c_set_mouse_transform_)(
600 struct TCOD_Context* __restrict self, const TCOD_MouseTransform* __restrict transform);
601};
602#ifdef __cplusplus
603namespace tcod {
605 void operator()(TCOD_Context* console) const { TCOD_context_delete(console); }
606};
607typedef std::unique_ptr<TCOD_Context, ContextDeleter> ContextPtr;
608typedef std::shared_ptr<TCOD_Context> ContextSharedPtr;
609} // namespace tcod
610#endif // __cplusplus
611#endif // LIBTCOD_CONTEXT_H_
A managed libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console_types.hpp:80
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 bresenham.hpp:157
int check_throw_error(int error)
Check and throw error messages.
Definition error.hpp:57
A 4-channel RGBA color struct.
Definition color.h:92
A libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console.h:125
A rendering context for libtcod.
Definition context.h:340
void save_screenshot(const std::string &filepath)
Save a screenshot to filepath.
Definition context.h:489
void present(const TCOD_Console &console)
Present a console to the display.
Definition context.h:380
auto get_sdl_renderer() noexcept -> struct SDL_Renderer *
Return a non-owning pointer to the SDL_Renderer used by this context.
Definition context.h:410
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:423
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:458
void save_screenshot(const char *filepath)
Save a screenshot to filepath.
Definition context.h:474
auto get_sdl_window() noexcept -> struct SDL_Window *
Return a non-owning pointer to the SDL_Window used by this context.
Definition context.h:395
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:438
void present(const TCOD_Console &console, const TCOD_ViewportOptions &viewport)
Present a console to the display with the provided viewport options.
Definition context.h:365
int type
The TCOD_renderer_t value of this context.
Definition context.h:523
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:349
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:508
A struct of parameters used to create a new context with TCOD_context_new.
Definition context.h:70
TCOD_Tileset * tileset
tileset is an optional pointer to a tileset object.
Definition context.h:107
int columns
columns and rows are the desired size of the terminal window.
Definition context.h:96
int window_x
window_x and window_y are the starting position of the window.
Definition context.h:84
int tcod_version
Compiled libtcod version for ABI compatiblity with older versions of libtcod.
Definition context.h:74
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:146
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:155
int vsync
If vsync is true, then vertical sync will be enabled whenever possible.
Definition context.h:112
int pixel_width
pixel_width and pixel_height are the desired size of the window in pixels.
Definition context.h:89
const char * window_title
window_title will be the title of the opened window.
Definition context.h:126
const char *const * argv
argc and argv are optional CLI parameters.
Definition context.h:137
int renderer_type
renderer_type is one of the TCOD_renderer_t values.
Definition context.h:100
void * cli_userdata
This is passed to the userdata parameter of cli_output if called.
Definition context.h:150
TCOD_Console * console
A console to be used as a reference for the desired window size.
Definition context.h:164
int sdl_window_flags
sdl_window_flags is a bitfield of SDL_WindowFlags flags.
Definition context.h:120
int argc
The number of items in argv.
Definition context.h:130
Info needed to convert between mouse pixel and tile coordinates.
Definition mouse_types.h:117
A container for libtcod tileset graphics.
Definition tileset.h:62
Viewport options for the rendering context.
Definition context_viewport.h:48
Definition context.h:604
Tileset module.