backend-infra-engineer: Pre-0.2.2 snapshot (2022)

This commit is contained in:
scawful
2023-01-01 17:52:09 -06:00
parent c86a241ee2
commit e7470bdfac
101 changed files with 13685 additions and 3 deletions

135
src/app/gfx/bitmap.cc Normal file
View File

@@ -0,0 +1,135 @@
#include "bitmap.h"
#include <SDL.h>
#include <cstdint>
#include <memory>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "app/core/constants.h"
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace app {
namespace gfx {
namespace {
void GrayscalePalette(SDL_Palette *palette) {
for (int i = 0; i < 8; i++) {
palette->colors[i].r = i * 31;
palette->colors[i].g = i * 31;
palette->colors[i].b = i * 31;
}
}
} // namespace
Bitmap::Bitmap(int width, int height, int depth, uchar *data) {
Create(width, height, depth, data);
}
Bitmap::Bitmap(int width, int height, int depth, int data_size) {
Create(width, height, depth, data_size);
}
Bitmap::Bitmap(int width, int height, int depth, uchar *data, int data_size) {
Create(width, height, depth, data, data_size);
}
// Pass raw pixel data directly to the surface
void Bitmap::Create(int width, int height, int depth, uchar *data) {
active_ = true;
width_ = width;
height_ = height;
depth_ = depth;
pixel_data_ = data;
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
GrayscalePalette(surface_->format->palette);
}
// Reserves data to later draw to surface via pointer
void Bitmap::Create(int width, int height, int depth, int size) {
active_ = true;
width_ = width;
height_ = height;
depth_ = depth;
data_size_ = size;
data_.reserve(size);
pixel_data_ = data_.data();
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width, height, depth,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
GrayscalePalette(surface_->format->palette);
}
// Pass raw pixel data directly to the surface
void Bitmap::Create(int width, int height, int depth, uchar *data, int size) {
active_ = true;
width_ = width;
height_ = height;
depth_ = depth;
pixel_data_ = data;
data_size_ = size;
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
GrayscalePalette(surface_->format->palette);
}
void Bitmap::Create(int width, int height, int depth, Bytes data) {
active_ = true;
width_ = width;
height_ = height;
depth_ = depth;
data_ = data;
pixel_data_ = data_.data();
surface_ = std::unique_ptr<SDL_Surface, SDL_Surface_Deleter>(
SDL_CreateRGBSurfaceWithFormat(0, width_, height_, depth_,
SDL_PIXELFORMAT_INDEX8),
SDL_Surface_Deleter());
surface_->pixels = pixel_data_;
GrayscalePalette(surface_->format->palette);
}
void Bitmap::Apply(Bytes data) {
pixel_data_ = data.data();
data_ = data;
}
// Creates the texture that will be displayed to the screen.
void Bitmap::CreateTexture(std::shared_ptr<SDL_Renderer> renderer) {
texture_ = std::shared_ptr<SDL_Texture>{
SDL_CreateTextureFromSurface(renderer.get(), surface_.get()),
SDL_Texture_Deleter{}};
}
// Convert SNESPalette to SDL_Palette for surface.
void Bitmap::ApplyPalette(const SNESPalette &palette) {
palette_ = palette;
for (int i = 0; i < palette.size_; ++i) {
if (palette.GetColor(i).transparent) {
surface_->format->palette->colors[i].r = 0;
surface_->format->palette->colors[i].g = 0;
surface_->format->palette->colors[i].b = 0;
surface_->format->palette->colors[i].a = 0;
} else {
surface_->format->palette->colors[i].r = palette.GetColor(i).rgb.x;
surface_->format->palette->colors[i].g = palette.GetColor(i).rgb.y;
surface_->format->palette->colors[i].b = palette.GetColor(i).rgb.z;
surface_->format->palette->colors[i].a = palette.GetColor(i).rgb.w;
}
}
}
} // namespace gfx
} // namespace app
} // namespace yaze

87
src/app/gfx/bitmap.h Normal file
View File

@@ -0,0 +1,87 @@
#ifndef YAZE_APP_GFX_BITMAP_H
#define YAZE_APP_GFX_BITMAP_H
#include <SDL.h>
#include <cstdint>
#include <memory>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "app/core/constants.h"
#include "app/gfx/snes_palette.h"
namespace yaze {
namespace app {
namespace gfx {
class Bitmap {
public:
Bitmap() = default;
Bitmap(int width, int height, int depth, uchar *data);
Bitmap(int width, int height, int depth, int data_size);
Bitmap(int width, int height, int depth, uchar *data, int data_size);
void Create(int width, int height, int depth, uchar *data);
void Create(int width, int height, int depth, int data_size);
void Create(int width, int height, int depth, uchar *data, int data_size);
void Create(int width, int height, int depth, Bytes data);
void Apply(Bytes data);
void CreateTexture(std::shared_ptr<SDL_Renderer> renderer);
void ApplyPalette(const SNESPalette &palette);
void WriteToPixel(int position, uchar value) {
this->pixel_data_[position] = value;
}
int GetWidth() const { return width_; }
int GetHeight() const { return height_; }
auto GetSize() const { return data_size_; }
auto GetData() const { return pixel_data_; }
auto GetByte(int i) const { return pixel_data_[i]; }
auto GetTexture() const { return texture_.get(); }
auto GetSurface() const { return surface_.get(); }
auto IsActive() const { return active_; }
private:
struct SDL_Texture_Deleter {
void operator()(SDL_Texture *p) const {
if (p != nullptr) {
SDL_DestroyTexture(p);
p = nullptr;
}
}
};
struct SDL_Surface_Deleter {
void operator()(SDL_Surface *p) const {
if (p != nullptr) {
p->pixels = nullptr;
SDL_FreeSurface(p);
p = nullptr;
}
}
};
int width_ = 0;
int height_ = 0;
int depth_ = 0;
int data_size_ = 0;
bool freed_ = false;
bool active_ = false;
uchar *pixel_data_;
Bytes data_;
gfx::SNESPalette palette_;
std::shared_ptr<SDL_Texture> texture_ = nullptr;
std::shared_ptr<SDL_Surface> surface_ = nullptr;
};
} // namespace gfx
} // namespace app
} // namespace yaze
#endif // YAZE_APP_GFX_BITMAP_H

197
src/app/gfx/snes_palette.cc Normal file
View File

@@ -0,0 +1,197 @@
#include "snes_palette.h"
#include <SDL.h>
#include <imgui/imgui.h>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <vector>
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace gfx {
ushort ConvertRGBtoSNES(const snes_color color) {
uchar red = color.red / 8;
uchar green = color.green / 8;
uchar blue = color.blue / 8;
return blue * 1024 + green * 32 + red;
}
snes_color ConvertSNEStoRGB(const ushort color) {
snes_color toret;
toret.red = ((color) % 32) * 8;
toret.green = ((color / 32) % 32) * 8;
toret.blue = ((color / 1024) % 32) * 8;
toret.red = toret.red + toret.red / 32;
toret.green = toret.green + toret.green / 32;
toret.blue = toret.blue + toret.blue / 32;
return toret;
}
snes_palette* Extract(const char* data, const unsigned int offset,
const unsigned int palette_size) {
snes_palette* toret = nullptr; // palette_create(palette_size, 0)
unsigned colnum = 0;
for (int i = 0; i < palette_size * 2; i += 2) {
unsigned short snes_color;
snes_color = ((uchar)data[offset + i + 1]) << 8;
snes_color = snes_color | ((uchar)data[offset + i]);
toret->colors[colnum] = ConvertSNEStoRGB(snes_color);
colnum++;
}
return toret;
}
char* Convert(const snes_palette pal) {
char* toret = (char*)malloc(pal.size * 2);
for (unsigned int i = 0; i < pal.size; i++) {
unsigned short snes_data = ConvertRGBtoSNES(pal.colors[i]);
toret[i * 2] = snes_data & 0xFF;
toret[i * 2 + 1] = snes_data >> 8;
}
return toret;
}
// ============================================================================
SNESColor::SNESColor() : rgb(ImVec4(0.f, 0.f, 0.f, 0.f)) {}
SNESColor::SNESColor(snes_color val) {
rgb.x = val.red;
rgb.y = val.green;
rgb.z = val.blue;
}
SNESColor::SNESColor(ImVec4 val) : rgb(val) {
snes_color col;
col.red = (uchar)val.x;
col.blue = (uchar)val.y;
col.green = (uchar)val.z;
snes = ConvertRGBtoSNES(col);
}
void SNESColor::setRgb(ImVec4 val) {
rgb = val;
snes_color col;
col.red = val.x;
col.blue = val.y;
col.green = val.z;
snes = ConvertRGBtoSNES(col);
}
void SNESColor::setSNES(snes_color val) {
rgb = ImVec4(val.red, val.green, val.blue, 255.f);
}
void SNESColor::setSNES(uint16_t val) {
snes = val;
snes_color col = ConvertSNEStoRGB(val);
rgb = ImVec4(col.red, col.green, col.blue, 0.f);
}
// ============================================================================
SNESPalette::SNESPalette(uint8_t mSize) : size_(mSize) {
for (unsigned int i = 0; i < mSize; i++) {
SNESColor col;
colors.push_back(col);
}
}
SNESPalette::SNESPalette(char* data) : size_(sizeof(data) / 2) {
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
for (unsigned i = 0; i < sizeof(data); i += 2) {
SNESColor col;
col.snes = static_cast<uchar>(data[i + 1]) << 8;
col.snes = col.snes | static_cast<uchar>(data[i]);
snes_color mColor = ConvertSNEStoRGB(col.snes);
col.rgb = ImVec4(mColor.red, mColor.green, mColor.blue, 1.f);
colors.push_back(col);
}
}
SNESPalette::SNESPalette(const unsigned char* snes_pal)
: size_(sizeof(snes_pal) / 2) {
assert((sizeof(snes_pal) % 4 == 0) && (sizeof(snes_pal) <= 32));
for (unsigned i = 0; i < sizeof(snes_pal); i += 2) {
SNESColor col;
col.snes = snes_pal[i + 1] << (uint16_t)8;
col.snes = col.snes | snes_pal[i];
snes_color mColor = ConvertSNEStoRGB(col.snes);
col.rgb = ImVec4(mColor.red, mColor.green, mColor.blue, 1.f);
colors.push_back(col);
}
}
SNESPalette::SNESPalette(const std::vector<ImVec4>& cols) {
for (const auto& each : cols) {
SNESColor scol;
scol.setRgb(each);
colors.push_back(scol);
}
size_ = cols.size();
}
SNESPalette::SNESPalette(const std::vector<snes_color>& cols) {
for (const auto& each : cols) {
SNESColor scol;
scol.setSNES(each);
colors.push_back(scol);
}
size_ = cols.size();
}
SNESPalette::SNESPalette(const std::vector<SNESColor>& cols) {
for (const auto& each : cols) {
colors.push_back(each);
}
size_ = cols.size();
}
void SNESPalette::Create(const std::vector<SNESColor>& cols) {
for (const auto each : cols) {
colors.push_back(each);
}
size_ = cols.size();
}
char* SNESPalette::encode() {
auto data = new char[size_ * 2];
for (unsigned int i = 0; i < size_; i++) {
std::cout << colors[i].snes << std::endl;
data[i * 2] = (char)(colors[i].snes & 0xFF);
data[i * 2 + 1] = (char)(colors[i].snes >> 8);
}
return data;
}
SDL_Palette* SNESPalette::GetSDL_Palette() {
auto sdl_palette = std::make_shared<SDL_Palette>();
sdl_palette->ncolors = size_;
auto color = std::vector<SDL_Color>(size_);
for (int i = 0; i < size_; i++) {
color[i].r = (uint8_t)colors[i].rgb.x * 100;
color[i].g = (uint8_t)colors[i].rgb.y * 100;
color[i].b = (uint8_t)colors[i].rgb.z * 100;
color[i].a = 0;
std::cout << "Color " << i << " added (R:" << color[i].r
<< " G:" << color[i].g << " B:" << color[i].b << ")" << std::endl;
}
sdl_palette->colors = color.data();
return sdl_palette.get();
}
PaletteGroup::PaletteGroup(uint8_t mSize) : size(mSize) {}
} // namespace gfx
} // namespace app
} // namespace yaze

117
src/app/gfx/snes_palette.h Normal file
View File

@@ -0,0 +1,117 @@
#ifndef YAZE_APP_GFX_PALETTE_H
#define YAZE_APP_GFX_PALETTE_H
#include <SDL.h>
#include <imgui/imgui.h>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <memory>
#include <vector>
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace gfx {
struct snes_color {
uchar red;
uchar blue;
uchar green;
};
using snes_color = struct snes_color;
struct snes_palette {
uint id;
uint size;
snes_color* colors;
};
using snes_palette = struct snes_palette;
ushort ConvertRGBtoSNES(const snes_color color);
snes_color ConvertSNEStoRGB(const ushort snes_color);
snes_palette* Extract(const char* data, const unsigned int offset,
const unsigned int palette_size);
char* Convert(const snes_palette pal);
struct SNESColor {
SNESColor();
explicit SNESColor(ImVec4);
explicit SNESColor(snes_color);
void setRgb(ImVec4);
void setSNES(snes_color);
void setSNES(uint16_t);
void setTransparent(bool t) { transparent = t; }
auto RGB() {
return ImVec4(rgb.x / 255, rgb.y / 255, rgb.z / 255, rgb.w);
}
bool transparent = false;
uint16_t snes = 0;
ImVec4 rgb;
};
class SNESPalette {
public:
SNESPalette() = default;
explicit SNESPalette(uint8_t mSize);
explicit SNESPalette(char* snesPal);
explicit SNESPalette(const unsigned char* snes_pal);
explicit SNESPalette(const std::vector<ImVec4>&);
explicit SNESPalette(const std::vector<snes_color>&);
explicit SNESPalette(const std::vector<SNESColor>&);
void Create(const std::vector<SNESColor>&);
void AddColor(SNESColor color) { colors.push_back(color); }
auto GetColor(int i) const { return colors[i]; }
SNESColor operator[](int i) {
if (i > size_) {
std::cout << "SNESPalette: Index out of bounds" << std::endl;
return colors[0];
}
return colors[i];
}
char* encode();
SDL_Palette* GetSDL_Palette();
int size_ = 0;
std::vector<SNESColor> colors;
};
struct PaletteGroup {
PaletteGroup() = default;
explicit PaletteGroup(uint8_t mSize);
void AddPalette(SNESPalette pal) {
palettes.emplace_back(pal);
size = palettes.size();
}
void AddColor(SNESColor color) {
if (size == 0) {
SNESPalette empty_pal;
palettes.emplace_back(empty_pal);
}
palettes[0].AddColor(color);
}
SNESPalette operator[](int i) {
if (i > size) {
std::cout << "PaletteGroup: Index out of bounds" << std::endl;
return palettes[0];
}
return palettes[i];
}
int size = 0;
std::vector<SNESPalette> palettes;
};
} // namespace gfx
} // namespace app
} // namespace yaze
#endif // YAZE_APP_GFX_PALETTE_H

29
src/app/gfx/snes_tile.cc Normal file
View File

@@ -0,0 +1,29 @@
#include "snes_tile.h"
#include <cstdint>
#include <vector>
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace gfx {
TileInfo GetTilesInfo(ushort tile) {
// vhopppcc cccccccc
bool o = false;
bool v = false;
bool h = false;
auto tid = (ushort)(tile & core::TileNameMask);
auto p = (uchar)((tile >> 10) & 0x07);
o = ((tile & core::TilePriorityBit) == core::TilePriorityBit);
h = ((tile & core::TileHFlipBit) == core::TileHFlipBit);
v = ((tile & core::TileVFlipBit) == core::TileVFlipBit);
return TileInfo(tid, p, v, h, o);
}
} // namespace gfx
} // namespace app
} // namespace yaze

94
src/app/gfx/snes_tile.h Normal file
View File

@@ -0,0 +1,94 @@
#ifndef YAZE_APP_GFX_SNES_TILE_H
#define YAZE_APP_GFX_SNES_TILE_H
#include <cstdint>
#include <vector>
#include "app/core/constants.h"
namespace yaze {
namespace app {
namespace gfx {
struct tile8 {
unsigned int id;
char data[64];
unsigned int palette_id;
};
using tile8 = struct tile8;
// vhopppcc cccccccc
// [0, 1]
// [2, 3]
class TileInfo {
public:
ushort id_;
bool over_;
bool vertical_mirror_;
bool horizontal_mirror_;
uchar palette_;
TileInfo() = default;
TileInfo(ushort id, uchar palette, bool v, bool h, bool o)
: id_(id),
over_(o),
vertical_mirror_(v),
horizontal_mirror_(h),
palette_(palette) {}
};
TileInfo GetTilesInfo(ushort tile);
class Tile32 {
public:
ushort tile0_;
ushort tile1_;
ushort tile2_;
ushort tile3_;
Tile32(ushort t0, ushort t1, ushort t2, ushort t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
};
class Tile16 {
public:
TileInfo tile0_;
TileInfo tile1_;
TileInfo tile2_;
TileInfo tile3_;
std::vector<TileInfo> tiles_info;
Tile16() = default;
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
tiles_info.push_back(tile0_);
tiles_info.push_back(tile1_);
tiles_info.push_back(tile2_);
tiles_info.push_back(tile3_);
}
};
class OAMTile {
public:
int x_;
int y_;
int mx_;
int my_;
int pal_;
ushort tile_;
OAMTile() = default;
OAMTile(int x, int y, ushort tile, int pal, bool upper = false, int mx = 0,
int my = 0)
: x_(x), y_(y), mx_(mx), my_(my), pal_(pal) {
if (upper) {
tile_ = (ushort)(tile + 512);
} else {
tile_ = (ushort)(tile + 256 + 512);
}
}
};
} // namespace gfx
} // namespace app
} // namespace yaze
#endif // YAZE_APP_GFX_SNES_TILE_H