Program Listing for File console.hpp

Return to documentation for file (libtcod/console.hpp)

/* BSD 3-Clause License
 *
 * Copyright © 2008-2023, Jice and the libtcod contributors.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef _TCOD_CONSOLE_HPP
#define _TCOD_CONSOLE_HPP

#include <stdexcept>
#include <string>
#include <utility>

#include "color.hpp"
#include "console.h"
#include "console_drawing.h"
#include "console_etc.h"
#include "console_init.h"
#include "console_printing.h"
#include "console_rexpaint.h"
#include "console_types.h"
#include "image.hpp"

namespace tcod {

inline void blit(
    TCOD_Console& dest,
    const TCOD_Console& source,
    const std::array<int, 2>& dest_xy = {0, 0},
    std::array<int, 4> source_rect = {0, 0, 0, 0},
    float foreground_alpha = 1.0f,
    float background_alpha = 1.0f) {
  TCOD_console_blit(
      &source,
      source_rect.at(0),
      source_rect.at(1),
      source_rect.at(2),
      source_rect.at(3),
      &dest,
      dest_xy.at(0),
      dest_xy.at(1),
      foreground_alpha,
      background_alpha);
}
}  // namespace tcod
// clang-format off

class TCODImage;
// clang-format on
class TCODLIB_API TCODConsole {
 public:

  TCODConsole();
  // clang-format off
  static TCODConsole *root;

  [[deprecated("This way of initializing libtcod is deprecated.  See the documentation for how to use tcod::new_context.")]]
  static void initRoot(int w, int h, const char * title, bool fullscreen = false, TCOD_renderer_t renderer=TCOD_RENDERER_SDL);

  // clang-format on
  [[deprecated(
      "This function is not compatible with contexts."
      "  Consider using tcod::load_tilesheet or TCOD_tileset_load instead."
      "  https://libtcod.readthedocs.io/en/latest/upgrading.html")]] static void
  setCustomFont(
      const char* fontFile, int flags = TCOD_FONT_LAYOUT_ASCII_INCOL, int nbCharHoriz = 0, int nbCharVertic = 0);
  // clang-format off
  [[deprecated("This function is not compatible with contexts.")]]
  static void mapAsciiCodeToFont(int asciiCode, int fontCharX, int fontCharY);

  [[deprecated("This function is not compatible with contexts.")]]
  static void mapAsciiCodesToFont(int firstAsciiCode, int nbCodes, int fontCharX, int fontCharY);

  [[deprecated("This function is not compatible with contexts.")]]
  static void mapStringToFont(const char *s, int fontCharX, int fontCharY);

  [[deprecated("This function is not compatible with contexts. Use SDL_GetWindowFlags to check this.")]]
  static bool isFullscreen();
  [[deprecated("This function is not compatible with contexts. Use SDL_SetWindowFullscreen to set this state.")]]
  static void setFullscreen(bool fullscreen);

  [[deprecated("This function is not compatible with contexts. Use SDL_SetWindowTitle to change the window title.")]]
  static void setWindowTitle(const char *title);

  [[deprecated("This function is not compatible with contexts. Use SDL for events and check for SDL_QUIT.")]]
  static bool isWindowClosed();

  [[deprecated("This function is not compatible with contexts. Use SDL_GetWindowFlags to check this.")]]
  static bool hasMouseFocus();

  [[deprecated("This function is not compatible with contexts. Use SDL_GetWindowFlags to check this.")]]
  static bool isActive();

  [[deprecated("This function is not compatible with contexts. Consider using `TCOD_console_credits_render_ex`.")]]
  static void credits();

  [[deprecated("This function is not compatible with contexts. Consider using `TCOD_console_credits_render_ex`.")]]
  static bool renderCredits(int x, int y, bool alpha);

  [[deprecated]]
  static void resetCredits();

  [[deprecated("Default console parameters are deprecated.")]]
  void setDefaultBackground(TCODColor back);

  [[deprecated("Default console parameters are deprecated.")]]
  void setDefaultForeground(TCODColor fore);

  void clear();

  [[deprecated]]
  void setCharBackground(int x, int y, const TCODColor &col, TCOD_bkgnd_flag_t flag = TCOD_BKGND_SET);
  [[deprecated]]
  void setCharForeground(int x, int y, const TCODColor &col);

  [[deprecated]]
  void setChar(int x, int y, int c);

  [[deprecated]]
  void putChar(int x, int y, int c, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);

  [[deprecated]]
  void putCharEx(int x, int y, int c, const TCODColor &fore, const TCODColor &back);

  [[deprecated("Default console parameters are deprecated.")]]
  void setBackgroundFlag(TCOD_bkgnd_flag_t flag);

  [[deprecated("Default console parameters are deprecated.")]]
  TCOD_bkgnd_flag_t getBackgroundFlag() const;

  [[deprecated("Default console parameters are deprecated.")]]
  void setAlignment(TCOD_alignment_t alignment);

  [[deprecated("Default console parameters are deprecated.")]]
  TCOD_alignment_t getAlignment() const;
  [[deprecated("Use TCODConsole::printf or the std::string overload for this function."
               "\nNote that you'll need to use UTF-8 encoded strings for those functions.")]]
  void print(int x, int y, const char *fmt, ...);
#ifndef TCOD_NO_UNICODE
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print.")]]
  void print(int x, int y, const std::string &str);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print.")]]
  void print(int x, int y, const std::string &str,
             TCOD_alignment_t alignment, TCOD_bkgnd_flag_t flag);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print and tcod::stringf.")]]
  TCODLIB_FORMAT(4, 5)
  void printf(int x, int y, const char *fmt, ...);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print and tcod::stringf.")]]
  TCODLIB_FORMAT(6, 7)
  void printf(int x, int y, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment,
              const char *fmt, ...);
#endif // TCOD_NO_UNICODE
  [[deprecated(
    "Use TCODConsole::print (std::string overload) or TCODConsole::printf"
    "instead of this function."
    "\nNote that you'll need to use UTF-8 encoded strings for those functions.")]]
  void printEx(int x, int y, TCOD_bkgnd_flag_t flag,
               TCOD_alignment_t alignment, const char *fmt, ...);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print_rect and tcod::stringf.")]]
  TCODLIB_FORMAT(6, 7)
  int printRect(int x, int y, int w, int h, const char *fmt, ...);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print_rect and tcod::stringf.")]]
  TCODLIB_FORMAT(8, 9)
  int printRectEx(int x, int y, int w, int h, TCOD_bkgnd_flag_t flag,
                  TCOD_alignment_t alignment, const char *fmt, ...);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::get_height_rect.")]]
  TCODLIB_FORMAT(6, 7)
  int getHeightRect(int x, int y, int w, int h, const char *fmt, ...);

  static void setColorControl(TCOD_colctrl_t con, const TCODColor &fore, const TCODColor &back);

#ifndef NO_UNICODE
  static void mapStringToFont(const wchar_t *s, int fontCharX, int fontCharY);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print and tcod::stringf.")]]
  void print(int x, int y, const wchar_t *fmt, ...);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print and tcod::stringf.")]]
  void printEx(int x, int y, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, const wchar_t *fmt, ...);
  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print_rect and tcod::stringf.")]]
  int printRect(int x, int y, int w, int h, const wchar_t *fmt, ...);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::print_rect and tcod::stringf.")]]
  int printRectEx(int x, int y, int w, int h, TCOD_bkgnd_flag_t flag, TCOD_alignment_t alignment, const wchar_t *fmt, ...);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::get_height_rect.")]]
  int getHeightRect(int x, int y, int w, int h, const wchar_t *fmt, ...);
#endif

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::draw_rect.")]]
  void rect(int x, int y, int w, int h, bool clear, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::draw_rect.")]]
  void hline(int x,int y, int l, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::draw_rect.")]]
  void vline(int x,int y, int l, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT);

  [[deprecated("TCODConsole methods are deprecated, this call should be replaced with tcod::draw_frame.")]]
  TCODLIB_FORMAT(8, 9)
  void printFrame(int x,int y,int w,int h, bool clear=true, TCOD_bkgnd_flag_t flag = TCOD_BKGND_DEFAULT, const char *fmt=NULL, ...);

  int getWidth() const;

  int getHeight() const;

  [[deprecated("Default console parameters are deprecated.")]]
  TCODColor getDefaultBackground() const;

  [[deprecated("Default console parameters are deprecated.")]]
  TCODColor getDefaultForeground() const;

  [[deprecated]]
  TCODColor getCharBackground(int x, int y) const;

  [[deprecated]]
  TCODColor getCharForeground(int x, int y) const;

  [[deprecated]]
  int getChar(int x, int y) const;

  [[deprecated("This function does not support contexts.")]]
  static void setFade(uint8_t fade, const TCODColor &fadingColor);

  static uint8_t getFade();

  static TCODColor getFadingColor();

  [[deprecated("This function does not support contexts.")]]
  static void flush();

  /* deprecated as of 1.5.1 */
  [[deprecated("This API is deprecated, use SDL_WaitEvent instead.")]]
  static TCOD_key_t waitForKeypress(bool flush);
  /* deprecated as of 1.5.1 */
  [[deprecated("This API is deprecated, use SDL_PollEvent instead.")]]
  static TCOD_key_t checkForKeypress(int flags=TCOD_KEY_RELEASED);

  static bool isKeyPressed(TCOD_keycode_t key);

  TCODConsole(int w, int h);

  TCODConsole(const char *filename);

  bool loadAsc(const char *filename);
  bool loadApf(const char *filename);

  bool saveAsc(const char *filename) const;

  bool saveApf(const char *filename) const;

#ifndef TCOD_NO_ZLIB
  bool loadXp(const char *filename) {
    return TCOD_console_load_xp(data, filename) != 0;
  }
  bool saveXp(const char *filename, int compress_level) {
    return TCOD_console_save_xp(data, filename, compress_level) != 0;
  }
#endif // TCOD_NO_ZLIB

  [[deprecated("This function has been replaced by tcod::blit.")]]
  static void blit(const TCODConsole *src,int xSrc, int ySrc, int wSrc, int hSrc, TCODConsole *dst, int xDst, int yDst, float foreground_alpha=1.0f, float background_alpha=1.0f);
  void setKeyColor(const TCODColor &col);
  [[deprecated("This function is a stub and will do nothing.")]]
  static void setKeyboardRepeat(int initialDelay,int interval);
  [[deprecated("This function is a stub and will do nothing.")]]
  static void disableKeyboardRepeat();
  // clang-format on
  // Delete copy constructors.
  TCODConsole(const TCODConsole&) = delete;
  TCODConsole& operator=(const TCODConsole&) = delete;
  // Supports move since version 1.19
  TCODConsole(TCODConsole&& rhs) : data{rhs.data} { rhs.data = nullptr; }
  TCODConsole& operator=(TCODConsole&& rhs) {
    std::swap(data, rhs.data);
    return *this;
  }

  virtual ~TCODConsole();

  [[deprecated("This function does nothing.")]] void setDirty(int x, int y, int w, int h);

  // This conversion may be unsafe.
  TCODConsole(TCOD_Console* console) : data{console} {}

  explicit TCODConsole(tcod::ConsolePtr console) : data{console.release()} {}

  // ctrl = TCOD_COLCTRL_1...TCOD_COLCTRL_5 or TCOD_COLCTRL_STOP
  static const char* getColorControlString(TCOD_colctrl_t ctrl);
  // ctrl = TCOD_COLCTRL_FORE_RGB or TCOD_COLCTRL_BACK_RGB
  static const char* getRGBColorControlString(TCOD_colctrl_t ctrl, const TCODColor& col);

  [[nodiscard]] auto get_data() noexcept -> TCOD_Console* { return get(); }
  [[nodiscard]] auto get_data() const noexcept -> const TCOD_Console* { return get(); }

  [[nodiscard]] auto get() noexcept -> TCOD_Console* {
    if (!data) return TCOD_sys_get_internal_console();
    return data;
  }
  [[nodiscard]] auto get() const noexcept -> const TCOD_Console* {
    if (!data) return TCOD_sys_get_internal_console();
    return data;
  }

  [[nodiscard]] operator TCOD_Console&() {
    TCOD_Console* out = data;
    if (!out) out = TCOD_sys_get_internal_console();
    if (!out) throw std::logic_error("Tried to get a reference to nullptr.");
    return *out;
  };

  [[nodiscard]] operator const TCOD_Console&() const {
    const TCOD_Console* out = data;
    if (!out) out = TCOD_sys_get_internal_console();
    if (!out) throw std::logic_error("Tried to get a reference to nullptr.");
    return *out;
  };

  [[nodiscard]] explicit operator TCOD_Console*() noexcept { return get_data(); };

  [[nodiscard]] explicit operator const TCOD_Console*() const noexcept { return get_data(); };

 protected:
  TCOD_Console* data = nullptr;  // This should be a unique_ptr, but fixing it will break the ABI.
};
#endif /* _TCOD_CONSOLE_HPP */