SDL2 port: Tile

This commit is contained in:
Christophe Meneboeuf 2022-06-18 01:01:47 +02:00
parent a33c95d6c0
commit 7239fff361
6 changed files with 106 additions and 40 deletions

View File

@ -56,23 +56,20 @@ set_property(TARGET Picture PROPERTY
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG})
## Application Tile
#add_executable(Tile src/App_Tile.cpp
# src/Tile.h
# src/Tile.cpp
# )
### custom definitions
#target_compile_definitions(Tile PRIVATE cimg_use_png)
### dependencies
#if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "9")
# target_link_libraries(Tile stdc++fs) # filesystem lib not included in stdc++ for gcc < 9
#endif()
#conan_set_find_library_paths(Tile)
#conan_target_link_libraries(Tile)
#target_link_libraries(Picture ${PROJECT_NAME})
### output
#set_property(TARGET Tile PROPERTY
# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}
#)
# Application Tile
add_executable(Tile src/App_Tile.cpp)
## custom definitions
target_compile_definitions(Tile PRIVATE cimg_use_png)
## dependencies
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND "${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "9")
target_link_libraries(Tile stdc++fs) # filesystem lib not included in stdc++ for gcc < 9
endif()
conan_set_find_library_paths(Tile)
conan_target_link_libraries(Tile)
target_link_libraries(Tile ${PROJECT_NAME})
## output
set_property(TARGET Tile PROPERTY
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}
)

View File

@ -23,16 +23,18 @@
#include <string>
#include <sys/stat.h>
#include <Magick++.h>
#include <SDL2/SDL_image.h>
#include <tclap/CmdLine.h>
#include "ImageQuantized.h"
#include "Picture.h"
#include "Tile.h"
using namespace std;
using namespace RgbToHires;
/// @brief Returns true if a file exists
inline bool exists(const std::string& path)
{
@ -40,10 +42,25 @@ inline bool exists(const std::string& path)
return (stat(path.c_str(), &buffer) == 0);
}
void ExitOnError(const std::string& message,
std::vector<SDL_Surface*> surfaces = {})
{
std::cout << "Error\n" << message << '\n';
for (auto surface : surfaces)
{
SDL_FreeSurface(surface);
}
SDL_Quit();
IMG_Quit();
exit(-1);
}
/// @brief Program entry point
int main( int argc, char *argv[] )
{
Magick::InitializeMagick(*argv);
//Parsing command line
TCLAP::CmdLine cmd("Tile - by Christophe Meneboeuf <christophe@xtof.info>", ' ', "0");
@ -69,28 +86,34 @@ int main( int argc, char *argv[] )
std::cout << "Line number shall be < 11" << std::endl;
return -1;
}
try
{
const auto filepath = imagePath.getValue();
if (!exists(filepath)) {
throw runtime_error("Cannot read " + filepath);
}
const auto imageRgb = Magick::Image{ filepath };
auto imageQuantized = ImageQuantized{ imageRgb };
const auto tileHiRes = Tile{ imageQuantized, column.getValue(), line.getValue()};
const auto filepath = imagePath.getValue();
if (!exists(filepath)) {
throw runtime_error("Cannot read " + filepath);
}
std::vector<SDL_Surface*> surfaces;
SDL_Surface* surfaceRgb = IMG_Load(filepath.c_str());
surfaces.push_back(surfaceRgb);
if (surfaceRgb == nullptr)
{
ExitOnError("Cannot decode " + filepath, surfaces);
}
const ImageQuantized imageHiRes{ surfaceRgb };
const auto tileHiRes = imageHiRes.getTile(column.getValue(), line.getValue());
// Always output in asm
ofstream output(outputPath.getValue());
output << tileHiRes.getHiresAsm();
// Always output in asm
ofstream output(outputPath.getValue());
output << tileHiRes;
for (auto surface : surfaces)
{
SDL_FreeSurface(surface);
}
//Fatal error
catch (const exception& e) {
cout << e.what();
return -1;
}
SDL_Quit();
IMG_Quit();
return 0;
}

View File

@ -18,6 +18,8 @@
#include <stdexcept>
#include <iterator>
#include <iomanip>
#include <sstream>
#include "ImageQuantized.h"
@ -119,6 +121,43 @@ namespace RgbToHires {
std::string ImageQuantized::getTile(const unsigned col, const unsigned line) const
{
constexpr int TILE_W = 14;
constexpr int TILE_H = 16;
std::string assembly;
for (auto lineNr = line * TILE_H;
lineNr < TILE_H * (line + 1); ++lineNr)
{
std::stringstream stream;
stream << ".byte ";
auto& line = _blobHr[lineNr];
const unsigned nbBlockPerTile = TILE_W / NB_PIXEL_PER_BLOCK;
for (unsigned blockHrNr = col * nbBlockPerTile;
blockHrNr < nbBlockPerTile * (col + 1); ++blockHrNr)
{
const auto& block = line[blockHrNr];
for (const auto byte : block)
{
stream << '$' << std::setw(2) << std::setfill('0') << std::uppercase
<< std::hex << static_cast<int>(byte) << ", ";
}
}
assembly += stream.str();
assembly.pop_back(); //removing the last coma
assembly.pop_back();
assembly += "\n";
}
return assembly;
}
double ImageQuantized::Distance(const ColorRgb& color1, const ColorRgb& color2)
{

View File

@ -48,11 +48,18 @@ namespace RgbToHires {
ImageQuantized(SDL_Surface* const source);
~ImageQuantized() = default;
// Buffer
/// @brief Returns the binary hires picture
std::unique_ptr <std::array<uint8_t, FRAME_SIZE>> getHiresBuffer() const;
/// @brief Returns asm code corresponding to the image in memory (CA65 format)
std::string getHiresAsm() const;
/// @brief Returns an HIRES block
// Tile
/// @brief Returns an HIRES tile. The line are not interleaved as in an HIRES framebuffer
/// @param col Position: column number of the tile, from 0 to 9
/// @param col Position: line number of the tile, from 0 to 11
std::string getTile(const unsigned col, const unsigned line) const;
private:
ColorRgb Quantize(const ColorRgb& color);

View File

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
Rgb2Hires_PC/testTile.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB