libtcod
Loading...
Searching...
No Matches
zip.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 TCOD_ZIP_HPP_
36#define TCOD_ZIP_HPP_
37#ifndef TCOD_NO_ZLIB
38
39#include <filesystem>
40#include <optional>
41#include <string>
42
43#include "color.hpp"
44#include "config.h"
45#include "console.hpp"
46#include "image.hpp"
47#include "mersenne.hpp"
48#include "zip.h"
49
50// clang-format off
62class TCODLIB_API TCODZip {
63public :
64
73 [[deprecated("This method of serialization is not cross-platform. An alternative such as C++ Cereal should be used instead.")]]
75
102
113 void putChar(char val);
114
123 void putInt(int val);
124
133 void putFloat(float val);
134
143 void putString(const char *val);
144
153 void putColor(const TCODColor *val);
154
163 void putImage(const TCODImage *val);
164
173 [[deprecated("This function will corrupt console characters greater than 255.")]]
174 void putConsole(const TCODConsole *val);
175
184 void putRandom(const TCODRandom *val);
185
195 void putData(int nbBytes, const void *data);
196
204 uint32_t getCurrentBytes() const;
205
232 int saveToFile(const char *filename);
233
234
243 void saveToFile(const std::filesystem::path& path) { saveToFile(path.string().c_str()); }
244
257 int loadFromFile(const char *filename);
258
259
270 void loadFromFile(const std::filesystem::path& path) {
271 if (loadFromFile(path.string().c_str()) == 0) {
272 throw std::runtime_error{"Failed to load file: " + path.string()};
273 }
274 }
275
283 char getChar();
284
292 int getInt();
293
301 float getFloat();
302
311 const char *getString();
312
321
330
339
348
376 int getData(int nbBytes, void *data);
377
385 uint32_t getRemainingBytes() const;
386
395 void skipBytes(uint32_t nbBytes);
396 // clang-format on
397
398
405 void put(char value) { TCOD_zip_put_char(data, value); }
406
413 void put(int value) { TCOD_zip_put_int(data, value); }
414
421 void put(float value) { TCOD_zip_put_float(data, value); }
422
429 void put(const char* value) { TCOD_zip_put_string(data, value); }
430
437 void put(const std::string& value) { TCOD_zip_put_string(data, value.c_str()); }
438
445 void put(const std::optional<std::string>& value) {
446 if (value) return put(value.value());
447 TCOD_zip_put_string(data, nullptr);
448 }
449
456 void put(const tcod::ColorRGB& value) { TCOD_zip_put_color(data, value); }
457
464 void put(const TCODColor& value) { put(tcod::ColorRGB{value}); }
465
472 void put(const TCODImage& value) { TCOD_zip_put_image(data, value.get_data()); }
473 TCODLIB_BEGIN_IGNORE_DEPRECATIONS
474
481 [[deprecated("This function will corrupt console characters greater than 255.")]] void put(const TCODConsole& value) {
482 TCOD_zip_put_console(data, value.get_data());
483 }
484
491 [[deprecated("This function will corrupt console characters greater than 255.")]] void put(
492 const tcod::Console& value) {
493 TCOD_zip_put_console(data, value.get());
494 }
495 TCODLIB_END_IGNORE_DEPRECATIONS
496
503 void put(const TCODRandom& value) { TCOD_zip_put_random(data, value.get_data()); }
504
505
515 template <typename T>
516 T get() {
517 T out{};
518 get(out);
519 return out;
520 }
521
522
529 void get(char& out) { out = TCOD_zip_get_char(data); }
530
537 void get(int& out) { out = TCOD_zip_get_int(data); }
538
545 void get(float& out) { out = TCOD_zip_get_float(data); }
546
553 void get(std::optional<std::string>& out) {
554 out = {};
555 const char* string = TCOD_zip_get_string(data);
556 if (string) out = string;
557 }
558
565 void get(std::string& out) { out = get<std::optional<std::string>>().value(); }
566
573 void get(tcod::ColorRGB& out) { out = tcod::ColorRGB{TCOD_zip_get_color(data)}; }
574
581 void get(TCODColor& out) { out = TCODColor{TCOD_zip_get_color(data)}; }
582
589 void get(tcod::ImagePtr& out) { out = tcod::ImagePtr{TCOD_zip_get_image(data)}; }
590
598
605 void get(tcod::ConsolePtr& out) { out = tcod::ConsolePtr{TCOD_zip_get_console(data)}; }
606
614
622
629 void get(TCODRandom& out) { out = TCODRandom{TCOD_zip_get_random(data)}; }
630
631 protected:
632 TCOD_zip_t data{};
633};
634
635#endif // TCOD_NO_ZLIB
636#endif // TCOD_ZIP_HPP_
Definition color.hpp:221
Classic turn by turn game loop:TCODConsole::initRoot(80,50,"my game",false); while (!...
Definition console.hpp:137
auto get_data() noexcept -> TCOD_Console *
Return a pointer to the underlying TCOD_Console struct.
Definition console.hpp:1772
Definition image.hpp:60
TCOD_Image * get_data() noexcept
Return the pointer to this objects TCOD_Image data.
Definition image.hpp:535
Definition mersenne.hpp:94
TCOD_Random * get_data() noexcept
Return this objects TCOD_Random* pointer.
Definition mersenne.hpp:427
void get(TCODImage &out)
Extract an image to out.
Definition zip.hpp:597
void get(char &out)
Extract a char to out.
Definition zip.hpp:529
uint32_t getRemainingBytes() const
void get(int &out)
Extract an int to out.
Definition zip.hpp:537
TCODRandom * getRandom()
void get(TCODConsole &out)
Extract a console to out.
Definition zip.hpp:621
void saveToFile(const std::filesystem::path &path)
Save this objects buffered objects to the file at path.
Definition zip.hpp:243
TCODColor getColor()
TCODImage * getImage()
void putConsole(const TCODConsole *val)
void put(const tcod::ColorRGB &value)
Save a color to this zip.
Definition zip.hpp:456
int getData(int nbBytes, void *data)
Note that the getData length must match the length of the data when the file was created (with putDat...
int saveToFile(const char *filename)
Once you have finished adding data in the buffer, you can compress it and save it in a file.
TCODLIB_END_IGNORE_DEPRECATIONS void put(const TCODRandom &value)
Save an RNG state to this zip.
Definition zip.hpp:503
int loadFromFile(const char *filename)
You can read data from a file (compressed or not) into the buffer.
void get(tcod::ConsolePtr &out)
Extract a console pointer to out.
Definition zip.hpp:605
int getInt()
void put(const char *value)
Save a string to this zip.
Definition zip.hpp:429
void get(std::optional< std::string > &out)
Extract an optional string to out.
Definition zip.hpp:553
void get(tcod::Console &out)
Extract a console to out.
Definition zip.hpp:613
void putRandom(const TCODRandom *val)
void putInt(int val)
void put(const std::optional< std::string > &value)
Save an optional string to this zip.
Definition zip.hpp:445
void put(float value)
Save a float to this zip.
Definition zip.hpp:421
void put(const std::string &value)
Save a string to this zip.
Definition zip.hpp:437
const char * getString()
The address returned is in the buffer.
void get(TCODColor &out)
Extract a color to out.
Definition zip.hpp:581
void putFloat(float val)
TCODZip()
This function initializes a compression buffer.
void putColor(const TCODColor *val)
uint32_t getCurrentBytes() const
void put(char value)
Save a char to this zip.
Definition zip.hpp:405
void put(const TCODColor &value)
Save a color to this zip.
Definition zip.hpp:464
T get()
Return a value of T from this zip object.
Definition zip.hpp:516
float getFloat()
void putChar(char val)
TCODConsole * getConsole()
void putData(int nbBytes, const void *data)
~TCODZip()
Once you don't need the buffer anymore, you can release resources.
void skipBytes(uint32_t nbBytes)
void get(tcod::ColorRGB &out)
Extract a color to out.
Definition zip.hpp:573
void put(int value)
Save an int to this zip.
Definition zip.hpp:413
void putImage(const TCODImage *val)
char getChar()
void put(const TCODImage &value)
Save an image to this zip.
Definition zip.hpp:472
void get(float &out)
Extract a float to out.
Definition zip.hpp:545
void put(const tcod::Console &value)
Save a console to this zip.
Definition zip.hpp:491
TCODLIB_BEGIN_IGNORE_DEPRECATIONS void put(const TCODConsole &value)
Save a console to this zip.
Definition zip.hpp:481
void get(tcod::ImagePtr &out)
Extract an image pointer to out.
Definition zip.hpp:589
void loadFromFile(const std::filesystem::path &path)
Load objects from the file at path.
Definition zip.hpp:270
void get(std::string &out)
Extract a string to out.
Definition zip.hpp:565
void get(TCODRandom &out)
Extract an RNG state to out.
Definition zip.hpp:629
void putString(const char *val)
A managed libtcod console containing a grid of tiles with {ch, fg, bg} information.
Definition console_types.hpp:80
auto get() noexcept -> TCOD_Console *
Return a pointer to the internal TCOD_Console struct.
Definition console_types.hpp:167
Color handling module.
Libtcod config header.
C++ console module.
Image handling module.
Random number generator C++ module.
std::unique_ptr< struct TCOD_Console, ConsoleDeleter > ConsolePtr
A unique pointer to a TCOD_Console.
Definition console_types.hpp:56
std::unique_ptr< TCOD_Image, ImageDeleter > ImagePtr
A unique pointer to a TCOD_Image.
Definition image.hpp:56
A C++ RGB color, used to handle conversions between color types.
Definition color.hpp:53
Deprecated serialization and compression module.
TCOD_Random * TCOD_zip_get_random(TCOD_zip_t zip)
Read a TCOD_Random* object.
void TCOD_zip_put_random(TCOD_zip_t zip, const TCOD_Random *val)
Write a TCOD_Random* object.