expand tile library, consolidate rom features, work on overworld tile16 selection
This commit is contained in:
@@ -69,6 +69,7 @@ void Controller::onInput() {
|
||||
case SDL_WINDOWEVENT:
|
||||
switch (event.window.event) {
|
||||
case SDL_WINDOWEVENT_CLOSE:
|
||||
editor_.Shutdown();
|
||||
quit();
|
||||
break;
|
||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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
|
||||
@@ -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
70
src/app/gfx/tile16.cc
Normal 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
0
src/app/gfx/tile32.cc
Normal file
@@ -23,7 +23,7 @@ int AddressFromBytes(uchar addr1, uchar addr2, uchar addr3) {
|
||||
return (addr1 << 16) | (addr2 << 8) | addr3;
|
||||
}
|
||||
|
||||
ROM::~ROM() {
|
||||
void ROM::Close() {
|
||||
if (loaded) {
|
||||
delete[] current_rom_;
|
||||
for (auto &each : decompressed_graphic_sheets_) {
|
||||
@@ -97,33 +97,14 @@ std::vector<tile8> ROM::ExtractTiles(gfx::TilePreset &preset) {
|
||||
return rawTiles;
|
||||
}
|
||||
|
||||
gfx::SNESPalette ROM::ExtractPalette(gfx::TilePreset &preset) {
|
||||
uint filePos =
|
||||
GetRomPosition(preset.pc_palette_location_, preset.SNESPaletteLocation);
|
||||
std::cout << "Palette pos : " << filePos << std::endl; // TODO: make this hex
|
||||
uint palette_size = pow(2, preset.bits_per_pixel_); // - 1;
|
||||
|
||||
auto palette_data = std::make_unique<unsigned char[]>(palette_size * 2);
|
||||
memcpy(palette_data.get(), current_rom_ + filePos, palette_size * 2);
|
||||
|
||||
// char *ab = (char *)malloc(sizeof(char) * (palette_size * 2));
|
||||
// memcpy(ab, current_rom_ + filePos, palette_size * 2);
|
||||
|
||||
for (int i = 0; i < palette_size; i++) {
|
||||
std::cout << palette_data[i];
|
||||
}
|
||||
gfx::SNESPalette ROM::ExtractPalette(uint addr, int bpp) {
|
||||
uint filePos = addr;
|
||||
uint palette_size = pow(2, bpp);
|
||||
char *palette_data = (char *)malloc(sizeof(char) * (palette_size * 2));
|
||||
memcpy(palette_data, current_rom_ + filePos, palette_size * 2);
|
||||
for (int i = 0; i < palette_size; i++) std::cout << palette_data[i];
|
||||
std::cout << std::endl;
|
||||
|
||||
const unsigned char *data = palette_data.get();
|
||||
gfx::SNESPalette pal(data);
|
||||
if (preset.no_zero_color_) {
|
||||
gfx::SNESColor col;
|
||||
|
||||
col.setRgb(ImVec4(153, 153, 153, 255));
|
||||
pal.colors.push_back(col);
|
||||
pal.colors.erase(pal.colors.begin(),
|
||||
pal.colors.begin() + pal.colors.size() - 1);
|
||||
}
|
||||
gfx::SNESPalette pal(palette_data);
|
||||
return pal;
|
||||
}
|
||||
|
||||
@@ -270,7 +251,7 @@ uchar *ROM::SNES3bppTo8bppSheet(uchar *buffer_in,
|
||||
return sheet_buffer_out;
|
||||
}
|
||||
|
||||
SDL_Texture *ROM::DrawgfxSheet(int offset) {
|
||||
SDL_Texture *ROM::DrawGraphicsSheet(int offset) {
|
||||
SDL_Surface *surface =
|
||||
SDL_CreateRGBSurfaceWithFormat(0, 128, 32, 8, SDL_PIXELFORMAT_INDEX8);
|
||||
std::cout << "Drawing surface #" << offset << std::endl;
|
||||
|
||||
@@ -24,22 +24,17 @@ int AddressFromBytes(uchar addr1, uchar addr2, uchar addr3);
|
||||
|
||||
class ROM {
|
||||
public:
|
||||
~ROM();
|
||||
void Close();
|
||||
|
||||
void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer);
|
||||
void LoadFromFile(const std::string& path);
|
||||
std::vector<tile8> ExtractTiles(gfx::TilePreset& preset);
|
||||
gfx::SNESPalette ExtractPalette(gfx::TilePreset& preset);
|
||||
gfx::SNESPalette ExtractPalette(uint addr, int bpp);
|
||||
uint32_t GetRomPosition(int direct_addr, uint snes_addr) const;
|
||||
char* Decompress(int pos, int size = 0x800, bool reversed = false);
|
||||
uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0);
|
||||
SDL_Texture* DrawgfxSheet(int offset);
|
||||
SDL_Texture* DrawGraphicsSheet(int offset);
|
||||
|
||||
inline uchar* GetRawData() { return current_rom_; }
|
||||
const uchar* getTitle() const { return title; }
|
||||
long int getSize() const { return size_; }
|
||||
char getVersion() const { return version_; }
|
||||
bool isLoaded() const { return loaded; }
|
||||
|
||||
unsigned int SnesToPc(unsigned int addr) {
|
||||
if (addr >= 0x808000) {
|
||||
@@ -48,6 +43,12 @@ class ROM {
|
||||
unsigned int temp = (addr & 0x7FFF) + ((addr / 2) & 0xFF8000);
|
||||
return (temp + 0x0);
|
||||
}
|
||||
inline uchar* GetRawData() { return current_rom_; }
|
||||
inline auto Renderer() { return sdl_renderer_; }
|
||||
const uchar* getTitle() const { return title; }
|
||||
long int getSize() const { return size_; }
|
||||
char getVersion() const { return version_; }
|
||||
bool isLoaded() const { return loaded; }
|
||||
|
||||
private:
|
||||
bool loaded = false;
|
||||
|
||||
@@ -22,7 +22,6 @@ Overworld::~Overworld() {
|
||||
free(allmapsTilesSP);
|
||||
|
||||
delete[] overworldMapPointer;
|
||||
delete[] owactualMapPointer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,17 +54,17 @@ void Overworld::Load(app::rom::ROM& rom) {
|
||||
DecompressAllMapTiles();
|
||||
|
||||
// Map Initialization :
|
||||
for (int i = 0; i < 160; i++) {
|
||||
allmaps.push_back(OverworldMap(rom_, tiles16, (uchar)i));
|
||||
}
|
||||
FetchLargeMaps();
|
||||
LoadOverworldMap();
|
||||
// for (int i = 0; i < 160; i++) {
|
||||
// allmaps.push_back(OverworldMap(rom_, tiles16, (uchar)i));
|
||||
// }
|
||||
// FetchLargeMaps();
|
||||
// LoadOverworldMap();
|
||||
|
||||
auto size = tiles16.size();
|
||||
for (int i = 0; i < 160; i++) {
|
||||
allmaps[i].BuildMap(mapParent, size, gameState, allmapsTilesLW,
|
||||
allmapsTilesDW, allmapsTilesSP);
|
||||
}
|
||||
// auto size = tiles16.size();
|
||||
// for (int i = 0; i < 160; i++) {
|
||||
// allmaps[i].BuildMap(mapParent, size, gameState, allmapsTilesLW,
|
||||
// allmapsTilesDW, allmapsTilesSP);
|
||||
// }
|
||||
|
||||
isLoaded = true;
|
||||
}
|
||||
@@ -298,10 +297,8 @@ void Overworld::FetchLargeMaps() {
|
||||
}
|
||||
|
||||
void Overworld::LoadOverworldMap() {
|
||||
overworldMapBitmap = new Bitmap(128, 128, overworldMapPointer);
|
||||
owactualMapBitmap = new Bitmap(512, 512, owactualMapPointer);
|
||||
overworldMapBitmap = new Bitmap(128, 128, 8, overworldMapPointer);
|
||||
|
||||
// Mode 7
|
||||
char* ptr = overworldMapPointer;
|
||||
|
||||
int pos = 0;
|
||||
@@ -317,30 +314,7 @@ void Overworld::LoadOverworldMap() {
|
||||
}
|
||||
}
|
||||
|
||||
// ColorPalette cp = overworldMapBitmap.Palette;
|
||||
// for (int i = 0; i < 256; i += 2)
|
||||
// {
|
||||
// //55B27 = US LW
|
||||
// //55C27 = US DW
|
||||
// cp.Entries[i / 2] = getColor((short)((ROM.DATA[0x55B27 + i + 1] << 8) +
|
||||
// ROM.DATA[0x55B27 + i]));
|
||||
|
||||
// int k = 0;
|
||||
// int j = 0;
|
||||
// for (int y = 10; y < 14; y++)
|
||||
// {
|
||||
// for (int x = 0; x < 15; x++)
|
||||
// {
|
||||
// cp.Entries[145 + k] = Palettes.globalSprite_Palettes[0][j];
|
||||
// k++;
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// overworldMapBitmap.Palette = cp;
|
||||
// owactualMapBitmap.Palette = cp;
|
||||
overworld_map_texture = overworldMapBitmap->CreateTexture(rom_.Renderer());
|
||||
}
|
||||
|
||||
} // namespace zelda3
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef YAZE_APP_DATA_OVERWORLD_H
|
||||
#define YAZE_APP_DATA_OVERWORLD_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <rommapping.h>
|
||||
|
||||
#include <memory>
|
||||
@@ -26,9 +27,6 @@ class Overworld {
|
||||
char* overworldMapPointer = new char[0x40000];
|
||||
gfx::Bitmap* overworldMapBitmap;
|
||||
|
||||
char* owactualMapPointer = new char[0x40000];
|
||||
gfx::Bitmap* owactualMapBitmap;
|
||||
|
||||
private:
|
||||
app::rom::ROM rom_;
|
||||
int gameState = 1;
|
||||
@@ -48,6 +46,13 @@ class Overworld {
|
||||
std::vector<ushort> tileLeftEntrance;
|
||||
std::vector<ushort> tileRightEntrance;
|
||||
|
||||
std::shared_ptr<uchar> mapblockset16;
|
||||
std::shared_ptr<uchar> currentOWgfx16Ptr;
|
||||
|
||||
gfx::Bitmap mapblockset16Bitmap;
|
||||
|
||||
SDL_Texture* overworld_map_texture;
|
||||
|
||||
int map32address[4] = {
|
||||
core::constants::map32TilesTL, core::constants::map32TilesTR,
|
||||
core::constants::map32TilesBL, core::constants::map32TilesBR};
|
||||
|
||||
Reference in New Issue
Block a user