expand tile library, consolidate rom features, work on overworld tile16 selection

This commit is contained in:
Justin Scofield
2022-06-20 17:54:14 -04:00
parent f7d793ecc2
commit f11e8f2aac
17 changed files with 222 additions and 239 deletions

View File

@@ -10,6 +10,23 @@ namespace yaze {
namespace app {
namespace gfx {
Bitmap::Bitmap(int width, int height, int depth, char *data)
: width_(width), height_(height), depth_(depth), pixel_data_(data) {
surface_ = SDL_CreateRGBSurfaceWithFormat(0, width, height, depth,
SDL_PIXELFORMAT_INDEX8);
// Default grayscale palette
for (int i = 0; i < 8; i++) {
surface_->format->palette->colors[i].r = i * 31;
surface_->format->palette->colors[i].g = i * 31;
surface_->format->palette->colors[i].b = i * 31;
}
surface_->pixels = data;
}
SDL_Texture *Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
texture_ = SDL_CreateTextureFromSurface(renderer.get(), surface_);
}
int GetPCGfxAddress(char *romData, char id) {
char **info1 = new char *[255];
int gfxPointer1 =
@@ -30,7 +47,8 @@ int GetPCGfxAddress(char *romData, char id) {
char gfxGamePointer3 = romData[gfxPointer3 + id];
return lorom_snes_to_pc(
yaze::app::rom::AddressFromBytes(gfxGamePointer1, gfxGamePointer2, gfxGamePointer3),
yaze::app::rom::AddressFromBytes(gfxGamePointer1, gfxGamePointer2,
gfxGamePointer3),
info1);
}

View File

@@ -14,16 +14,19 @@ namespace gfx {
class Bitmap {
public:
Bitmap() = default;
Bitmap(int width, int height, char *data)
: width_(width), height_(height), pixel_data_(data) {}
Bitmap(int width, int height, int depth, char *data);
int GetWidth() const { return width_; }
int GetHeight() const { return height_; }
SDL_Texture *CreateTexture(std::shared_ptr<SDL_Renderer> renderer);
private:
int width_;
int height_;
int depth_;
char *pixel_data_;
SDL_Surface *surface_;
SDL_Texture *texture_;
};
static bool isbpp3[core::constants::NumberOfSheets];

View File

@@ -1,121 +0,0 @@
#include "tile.h"
#include <tile.h>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace app {
namespace gfx {
TilesPattern::TilesPattern() {
tiles_per_row_ = 16;
number_of_tiles_ = 16;
// transform_vector_.push_back(std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
// 8,
// 9, 10, 11, 12, 13, 14, 15});
// transform_vector_.push_back(std::vector<int>{});
// transform_vector_.push_back(std::vector<int>{});
// transform_vector_.push_back(std::vector<int>{});
transform_vector_.push_back(std::vector<int>{0, 1, 2, 3});
transform_vector_.push_back(std::vector<int>{4, 5, 6, 7});
transform_vector_.push_back(std::vector<int>{8, 9, 10, 11});
transform_vector_.push_back(std::vector<int>{12, 13, 14, 15});
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const std::vector<tile8> &tiles) const {
uint repeatOffsetY = 0;
uint repeatOffsetX = 0;
uint tVectHeight = transform_vector_.size();
uint tVectWidth = transform_vector_[0].size();
uint repeat = 0;
std::vector<std::vector<tile8> > toret;
uint transPerRow = tiles_per_row_ / tVectWidth;
uint nbTransform = tiles.size() / number_of_tiles_;
printf("Tiles size : %d\nnbtransform : %d\npattern number of tiles : %d\n",
tiles.size(), nbTransform, number_of_tiles_);
if (transPerRow > nbTransform)
toret.resize(tVectHeight);
else
toret.resize(((uint)(((double)nbTransform / (double)transPerRow) + 0.5)) *
tVectHeight);
for (auto &each : toret) {
each.resize(tiles_per_row_);
}
std::cout << toret[0].size() << " x " << toret.size();
while (repeat != nbTransform) {
std::cout << "repeat" << repeat;
for (uint j = 0; j < tVectHeight; j++) {
for (uint i = 0; i < tVectWidth; i++) {
uint posTile = transform_vector_[j][i] + number_of_tiles_ * repeat;
uint posX = i + repeatOffsetX;
uint posY = j + repeatOffsetY;
printf("X: %d - Y: %d - posTile : %d \n", posX, posY, posTile);
toret.at(posY).at(posX) = tiles[posTile];
}
}
if (repeatOffsetX + tVectWidth == tiles_per_row_) {
repeatOffsetX = 0;
repeatOffsetY += tVectHeight;
} else
repeatOffsetX += tVectWidth;
repeat++;
}
std::cout << "End of transform" << std::endl;
return toret;
}
std::vector<tile8> TilesPattern::reverse(
const std::vector<tile8> &tiles) const {
uint repeatOffsetY = 0;
uint repeatOffsetX = 0;
uint tVectHeight = transform_vector_.size();
uint tVectWidth = transform_vector_[0].size();
uint repeat = 0;
uint nbTransPerRow = tiles_per_row_ / tVectWidth;
uint nbTiles = tiles.size();
std::vector<tile8> toretVec(tiles.size());
for (uint i = 0; i < nbTiles; i++) {
uint lineNb = i / tiles_per_row_;
uint lineInTab = lineNb % tVectHeight;
uint colInTab = i % tVectWidth;
uint tileNb = transform_vector_[lineInTab][colInTab];
uint lineBlock = i / (nbTransPerRow * number_of_tiles_);
uint blockNB =
(i % (nbTransPerRow * number_of_tiles_) % tiles_per_row_) / tVectWidth;
std::cout << colInTab << lineInTab << " = " << tileNb;
uint pos = tileNb + (lineBlock + blockNB) * number_of_tiles_;
std::cout << i << "Goes to : " << pos;
toretVec[pos] = tiles[i];
}
return toretVec;
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const TilesPattern &pattern, const std::vector<tile8> &tiles) {
return pattern.transform(tiles);
}
} // namespace gfx
} // namespace app
} // namespace yaze

View File

@@ -9,6 +9,7 @@
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <regex>
#include <string>
#include <unordered_map>
@@ -16,6 +17,7 @@
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace app {
namespace gfx {
@@ -55,6 +57,10 @@ class Tile32 {
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
};
void BuildTiles16Gfx();
void CopyTile16(int x, int y, int xx, int yy, int offset, TileInfo tile,
uchar* gfx16Pointer, uchar* gfx8Pointer);
class Tile16 {
public:
TileInfo tile0_;
@@ -72,31 +78,9 @@ class Tile16 {
}
};
class TilesPattern {
public:
TilesPattern();
std::string name;
std::string description;
unsigned int tiles_per_row_;
unsigned int number_of_tiles_;
static TilesPattern pattern(std::string name);
static std::vector<std::vector<tile8>> transform(
const TilesPattern& pattern, const std::vector<tile8>& tiles);
protected:
std::vector<std::vector<tile8>> transform(
const std::vector<tile8>& tiles) const;
std::vector<tile8> reverse(const std::vector<tile8>& tiles) const;
private:
std::vector<std::vector<int>> transform_vector_;
};
class TilePreset {
public:
TilePreset() = default;
TilesPattern tilesPattern;
bool no_zero_color_ = false;
int pc_tiles_location_ = 0;
int pc_palette_location_ = 0;

70
src/app/gfx/tile16.cc Normal file
View File

@@ -0,0 +1,70 @@
#include <memory>
#include <vector>
#include "app/core/constants.h"
#include "tile.h"
namespace yaze {
namespace app {
namespace gfx {
void BuildTiles16Gfx(std::shared_ptr<uchar> mapblockset16,
std::shared_ptr<uchar> currentOWgfx16Ptr,
std::vector<Tile16>& allTiles) {
auto gfx16Data = mapblockset16.get();
auto gfx8Data = currentOWgfx16Ptr.get();
const int offsets[4] = {0, 8, 1024, 1032};
auto yy = 0;
auto xx = 0;
// Number of tiles16 3748? // its 3752
for (auto i = 0; i < core::constants::NumberOfMap16; i++) {
// 8x8 tile draw
// gfx8 = 4bpp so everyting is /2
auto tiles = allTiles[i];
for (auto tile = 0; tile < 4; tile++) {
TileInfo info = tiles.tiles_info[tile];
int offset = offsets[tile];
for (auto y = 0; y < 8; y++) {
for (auto x = 0; x < 4; x++) {
CopyTile16(x, y, xx, yy, offset, info, gfx16Data, gfx8Data);
}
}
}
xx += 16;
if (xx >= 128) {
yy += 2048;
xx = 0;
}
}
}
void CopyTile16(int x, int y, int xx, int yy, int offset, TileInfo tile,
uchar* gfx16Pointer, uchar* gfx8Pointer) // map,current
{
int mx = x;
int my = y;
uchar r = 0;
if (tile.horizontal_mirror_) {
mx = 3 - x;
r = 1;
}
if (tile.vertical_mirror_) {
my = 7 - y;
}
int tx = ((tile.id_ / 16) * 512) + ((tile.id_ - ((tile.id_ / 16) * 16)) * 4);
auto index = xx + yy + offset + (mx * 2) + (my * 128);
auto pixel = gfx8Pointer[tx + (y * 64) + x];
gfx16Pointer[index + r ^ 1] = (uchar)((pixel & 0x0F) + tile.palette_ * 16);
gfx16Pointer[index + r] = (uchar)(((pixel >> 4) & 0x0F) + tile.palette_ * 16);
}
} // namespace gfx
} // namespace app
} // namespace yaze

0
src/app/gfx/tile32.cc Normal file
View File