floatybox

Float your way through perilous terrain in this endless side-scroller game.


floatybox

/

src

/

app

/

window.hh

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
/*
                                    88888888
                                  888888888888
                                 88888888888888
                                8888888888888888
                               888888888888888888
                              888888  8888  888888
                              88888    88    88888
                              888888  8888  888888
                              88888888888888888888
                              88888888888888888888
                             8888888888888888888888
                          8888888888888888888888888888
                        88888888888888888888888888888888
                              88888888888888888888
                            888888888888888888888888
                           888888  8888888888  888888
                           888     8888  8888     888
                                   888    888

                                   OCTOBANANA

Licensed under the MIT License

Copyright (c) 2020 Brett Robinson <https://octobanana.com/>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef WINDOW_HH
#define WINDOW_HH

#include "ob/text.hh"
#include "ob/term.hh"
#include "ob/prism.hh"

#include <cmath>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdlib>

#include <tuple>
#include <deque>
#include <regex>
#include <chrono>
#include <limits>
#include <memory>
#include <string>
#include <thread>
#include <vector>
#include <complex>
#include <utility>
#include <fstream>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <functional>
#include <unordered_map>

namespace aec = OB::Term::ANSI_Escape_Codes;

template<typename T>
struct Vec2n;

template<typename T>
Vec2n<T> operator*(Vec2n<T> lhs, Vec2n<T> const& rhs);

template<typename T>
Vec2n<T> operator/(Vec2n<T> lhs, Vec2n<T> const& rhs);

template<typename T>
Vec2n<T> operator+(Vec2n<T> lhs, Vec2n<T> const& rhs);

template<typename T>
Vec2n<T> operator-(Vec2n<T> lhs, Vec2n<T> const& rhs);

template<typename T>
std::ostream& operator<<(std::ostream& os, Vec2n<T> const& obj);

template<typename T>
bool operator<(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
bool operator<=(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
bool operator>(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
bool operator>=(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
bool operator==(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
bool operator!=(Vec2n<T> const& rhs, Vec2n<T> const& lhs);

template<typename T>
struct Vec2n {
  T x {0};
  T y {0};
  Vec2n(T const x, T const y) : x {x}, y {y} {}
  Vec2n() = default;
  Vec2n(Vec2n<T>&&) = default;
  Vec2n(Vec2n<T> const&) = default;
  template<typename N>
  Vec2n(Vec2n<N>&& obj) {
    x = static_cast<T>(obj.x);
    y = static_cast<T>(obj.y);
  }
  template<typename N>
  Vec2n(Vec2n<N> const& obj) {
    x = static_cast<T>(obj.x);
    y = static_cast<T>(obj.y);
  }

  ~Vec2n() = default;

  Vec2n<T>& operator=(Vec2n<T>&&) = default;
  Vec2n<T>& operator=(Vec2n<T> const&) = default;
  Vec2n<T>& operator*=(Vec2n<T> const& obj);
  Vec2n<T>& operator/=(Vec2n<T> const& obj);
  Vec2n<T>& operator+=(Vec2n<T> const& obj);
  Vec2n<T>& operator-=(Vec2n<T> const& obj);

  friend Vec2n<T> operator* <>(Vec2n<T> lhs, Vec2n<T> const& rhs);
  friend Vec2n<T> operator/ <>(Vec2n<T> lhs, Vec2n<T> const& rhs);
  friend Vec2n<T> operator+ <>(Vec2n<T> lhs, Vec2n<T> const& rhs);
  friend Vec2n<T> operator- <>(Vec2n<T> lhs, Vec2n<T> const& rhs);
  friend std::ostream& operator<< <>(std::ostream& os, Vec2n<T> const& obj);
  friend bool operator< <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
  friend bool operator<= <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
  friend bool operator> <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
  friend bool operator>= <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
  friend bool operator== <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
  friend bool operator!= <>(Vec2n<T> const& lhs, Vec2n<T> const& rhs);
};

template<typename T>
std::ostream& operator<<(std::ostream& os, Vec2n<T> const& obj) {
  os << obj.x << ":" << obj.y;
  return os;
}

template<typename T>
Vec2n<T>& Vec2n<T>::operator*=(Vec2n<T> const& obj) {
  x *= obj.x;
  y *= obj.y;
  return *this;
}

template<typename T>
Vec2n<T> operator*(Vec2n<T> lhs, Vec2n<T> const& rhs) {
  return lhs *= rhs;
}

template<typename T>
Vec2n<T>& Vec2n<T>::operator/=(Vec2n<T> const& obj) {
  x /= obj.x;
  y /= obj.y;
  return *this;
}

template<typename T>
Vec2n<T> operator/(Vec2n<T> lhs, Vec2n<T> const& rhs) {
  return lhs /= rhs;
}

template<typename T>
Vec2n<T>& Vec2n<T>::operator+=(Vec2n<T> const& obj) {
  x += obj.x;
  y += obj.y;
  return *this;
}

template<typename T>
Vec2n<T> operator+(Vec2n<T> lhs, Vec2n<T> const& rhs) {
  return lhs += rhs;
}

template<typename T>
Vec2n<T>& Vec2n<T>::operator-=(Vec2n<T> const& obj) {
  x -= obj.x;
  y -= obj.y;
  return *this;
}

template<typename T>
Vec2n<T> operator-(Vec2n<T> lhs, Vec2n<T> const& rhs) {
  return lhs -= rhs;
}

template<typename T>
bool operator<(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return (lhs.x < rhs.x) && (lhs.y < rhs.y);
}

template<typename T>
bool operator<=(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return (lhs.x <= rhs.x) && (lhs.y <= rhs.y);
}

template<typename T>
bool operator>=(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return (lhs.x >= rhs.x) && (lhs.y >= rhs.y);
}

template<typename T>
bool operator>(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return (lhs.x > rhs.x) && (lhs.y > rhs.y);
}

template<typename T>
bool operator==(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return (lhs.x == rhs.x) && (lhs.y == rhs.y);
}

template<typename T>
bool operator!=(Vec2n<T> const& lhs, Vec2n<T> const& rhs) {
  return !(lhs == rhs);
}

template<typename T>
std::optional<T> slope(Vec2n<T> const& p1, Vec2n<T> const& p2) {
  return p1.x == p2.x ? std::optional<T>() : (p2.y - p1.y) / (p2.x - p1.x);
}

template<typename T>
T y_intercept(Vec2n<T> const& p, T const& m) {
  return p.y - p.x * m;
}

template<typename T>
std::function<T(T const&)> line(Vec2n<T> const& p1, Vec2n<T> const& p2) {
  if (auto m = slope(p1, p2); m) {
    auto b = y_intercept(p1, m.value());
    return [m = m.value(), b](T const& x) -> T {
      return m * x + b;
    };
  }
  return [](T const& x) -> T {
    return x;
  };
}

template<typename T>
bool contains(Vec2n<T> const& point, Vec2n<T> const& min, Vec2n<T> const& max) {
  return
    (point.x >= min.x && point.x <= max.x) &&
    (point.y >= min.y && point.y <= max.y);
}

template<typename T>
bool intersect(Vec2n<T> const& amin, Vec2n<T> const& amax, Vec2n<T> const& bmin, Vec2n<T> const& bmax) {
  return
    (amin.x <= bmax.x && amax.x >= bmin.x) &&
    (amin.y <= bmax.y && amax.y >= bmin.y);
}

template<typename T>
Vec2n<T> trunc(Vec2n<T> obj) {
  obj.x = std::trunc(obj.x);
  obj.y = std::trunc(obj.y);
  return obj;
}

template<typename T>
Vec2n<T> floor(Vec2n<T> obj) {
  obj.x = std::floor(obj.x);
  obj.y = std::floor(obj.y);
  return obj;
}

template<typename T>
Vec2n<T> ceil(Vec2n<T> obj) {
  obj.x = std::ceil(obj.x);
  obj.y = std::ceil(obj.y);
  return obj;
}

template<typename T>
Vec2n<T> abs(Vec2n<T> obj) {
  obj.x = std::abs(obj.x);
  obj.y = std::abs(obj.y);
  return obj;
}

template<typename T>
Vec2n<T> round(Vec2n<T> obj) {
  obj.x = std::round(obj.x);
  obj.y = std::round(obj.y);
  return obj;
}

using Pos = Vec2n<std::size_t>;
using Size = Vec2n<std::size_t>;
using Vec2f = Vec2n<float>;

struct Style {
  friend std::ostream& operator<<(std::ostream& os, Style const& obj);
  enum Type : std::uint8_t {
    Clear = 0,
    Default,
    Bit_2,
    Bit_4,
    Bit_8,
    Bit_24,
    Bit_32,
  };
  enum Attr : std::uint8_t {
    Null = 0,
    Bold = 1 << 0,
    Reverse = 1 << 1,
    Underline = 1 << 2,
  };
  std::uint8_t type {Clear};
  std::uint8_t attr {Null};
  OB::Prism::RGBA fg;
  OB::Prism::RGBA bg;
};

inline std::ostream& operator<<(std::ostream& os, Style const& obj) {
  os
  << static_cast<int>(obj.attr) << " "
  << static_cast<int>(obj.type) << " "
  << obj.fg << " "
  << obj.bg;
  return os;
}

struct Cell {
  int zidx;
  Style style;
  std::string text {" "};
};

class Buffer {
public:
  Buffer(Size const size, Cell const& cell = {});
  Buffer() = default;
  Buffer(Buffer&&) = default;
  Buffer(Buffer const&) = default;
  ~Buffer() = default;
  Buffer& operator=(Buffer&&) = default;
  Buffer& operator=(Buffer const&) = default;
  void operator()(Pos const pos, Cell const& cell);
  void operator()(Cell const& cell);
  void put(Pos const pos, Cell const& cell);
  void put(Cell const& cell);
  Cell& at(Pos const pos);
  Cell const& at(Pos const pos) const;
  std::vector<Cell>& row(std::size_t const y);
  std::vector<Cell> const& row(std::size_t const y) const;
  Cell& col(Pos const pos);
  Cell const& col(Pos const pos) const;
  Pos cursor() const;
  void cursor(Pos const pos);
  Size size() const;
  void size(Size const size, Cell const& cell = {});
  bool empty() const;
  void clear();

private:
  Pos _pos;
  Size _size;
  std::vector<std::vector<Cell>> _value;
}; // class Buffer

class Window {
public:

// private:
  void winch();
  void refresh();
  void render();
  void write(std::string& str);
  void render_file(std::ofstream& file);
  void write_file(std::ofstream& file, std::string& str);
  void term_fg(std::string& str, OB::Prism::RGBA rgba);
  void term_bg(std::string& str, OB::Prism::RGBA rgba);

  Size size;
  std::size_t frames {0};
  std::size_t bsize {0};
  Style style_base;
  Style style;
  Buffer buf;
  Buffer buf_prev;
  Buffer buf_1;
  Buffer buf_2;
  std::string line;
  bool clear {true};
};

#endif // WINDOW_HH
Back to Top