Skip to content

Commit

Permalink
Palette shifting works for most maps! Still need to config for Tail's…
Browse files Browse the repository at this point in the history
… Lab map.
  • Loading branch information
Ohmnivore committed Sep 26, 2019
1 parent 3579881 commit 2d2f823
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 10 deletions.
8 changes: 6 additions & 2 deletions assets/battle_highway.map
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ tex assets:battle_highway/walls/5.png 32 32
tex assets:battle_highway/walls/6.png 32 32
tex assets:battle_highway/walls/7.png 8 32
tex assets:battle_highway/walls/8.png 8 32
tex assets:battle_highway/battle_highway.png 512 512
tex assets:battle_highway/battle_highway_palette.png 256 1


# Format: wall direction x y gfx
Expand Down Expand Up @@ -53,9 +55,11 @@ tex assets:battle_highway/walls/8.png 8 32


# Tilemap
tilemap 4 0 0 0
# tilemap 4 0 0 0
tilemap 14 0 0 0 palette 15
tilemap 5 0 0 46

paletteshift 15 208 223 32
paletteshift 15 224 239 32

# Opts
opt wall_gfx_height 32
Expand Down
Binary file added assets/battle_highway/battle_highway.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/battle_highway/battle_highway_palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion assets/emerald_beach.map
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ tex assets:emerald_beach/walls/3.png 32 32
tex assets:emerald_beach/walls/4.png 32 32
tex assets:emerald_beach/walls/5.png 32 32
tex assets:emerald_beach/walls/6.png 32 32
tex assets:emerald_beach/emerald_beach.png 512 512
tex assets:emerald_beach/emerald_beach_palette.png 256 1


# Format: wall direction x y gfx
Expand Down Expand Up @@ -51,8 +53,11 @@ tex assets:emerald_beach/walls/6.png 32 32


# Tilemap
tilemap 4 0 0 0
# tilemap 4 0 0 0
tilemap 12 0 0 0 palette 13
tilemap 5 0 0 46
paletteshift 13 209 221 -8
paletteshift 13 225 237 -8


# Opts
Expand Down
Binary file added assets/emerald_beach/emerald_beach.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/emerald_beach/emerald_beach_palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 84 additions & 5 deletions code/BattleApp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,75 @@ void BattleApp::DrawRenderable(Renderer::Renderable& rend) {
// (It's really a mat3 but we pass it as a mat4)
vsGBAParams.model = glm::mat4(rend.transform);

// Update parameters
MainDrawState.Mesh[0] = UnitMesh;
MainDrawState.FSTexture[MainShader::tex] = Res.Tex[rend.texIdx];
Gfx::ApplyDrawState(MainDrawState);
if (rend.paletteTexIdx == -1) // No palette
{
// Update parameters
MainDrawState.Mesh[0] = UnitMesh;
MainDrawState.FSTexture[MainShader::tex] = Res.Tex[rend.texIdx];
Gfx::ApplyDrawState(MainDrawState);
}
else
{
// Update parameters
PaletteDrawState.Mesh[0] = UnitMesh;
PaletteDrawState.FSTexture[PaletteShader::tex] = Res.Tex[rend.texIdx];
PaletteDrawState.FSTexture[PaletteShader::paletteTex] = Res.Tex[rend.paletteTexIdx];
Gfx::ApplyDrawState(PaletteDrawState);

// Update palette shifts
float ToUVCoords = Res.Lvl.texSizes[rend.paletteTexIdx].x;
const Renderer::PaletteShift* paletteShifts[Renderer::MAX_SHIFTS_PER_PALETTE];
for (int i = 0; i < Renderer::MAX_SHIFTS_PER_PALETTE; ++i)
{
paletteShifts[i] = nullptr;
}

int numFound = 0;
for (int i = 0; i < Res.Lvl.paletteShifts.Size(); ++i)
{
const Renderer::PaletteShift& paletteShift = Res.Lvl.paletteShifts[i];

if (paletteShift.paletteTexIdx == rend.paletteTexIdx)
{
paletteShifts[numFound] = &paletteShift;
numFound++;
if (numFound >= Renderer::MAX_SHIFTS_PER_PALETTE)
{
break;
}
}
}

for (int i = 0; i < Renderer::MAX_SHIFTS_PER_PALETTE; ++i)
{
float start = -1.0f;
float end = -1.0f;
float offset = -1.0f;

if (paletteShifts[i] != nullptr)
{
const Renderer::PaletteShift& paletteShift = *paletteShifts[i];
start = static_cast<float>(paletteShift.startIdx) / ToUVCoords;
end = static_cast<float>(paletteShift.endIdx) / ToUVCoords;
offset = glm::floor(paletteShift.currentIdxOffset) / ToUVCoords;
}

if (i == 0)
{
fsPallete1Params.start1 = start;
fsPallete1Params.end1 = end;
fsPallete1Params.offset1 = offset;
Gfx::ApplyUniformBlock(fsPallete1Params);
}
else if (i == 1)
{
fsPallete2Params.start2 = start;
fsPallete2Params.end2 = end;
fsPallete2Params.offset2 = offset;
Gfx::ApplyUniformBlock(fsPallete2Params);
}
}
}
Gfx::ApplyUniformBlock(vsGLParams);
Gfx::ApplyUniformBlock(vsGBAParams);

Expand All @@ -43,7 +108,8 @@ AppState::Code BattleApp::OnRunning() {
vsGLParams.viewProj = ViewProj;

Cam.UpdateTransforms();
Renderer.Update(Cam, Res.Lvl);
Renderer.Update(Clock::Since(LastFrameTimePoint).AsSeconds(), Cam, Res.Lvl);
LastFrameTimePoint = Clock::Now();

// Draw
Renderer::TilemapList& tilemaps = Renderer.UpdateTilemaps(Cam, Res.Lvl);
Expand Down Expand Up @@ -142,6 +208,19 @@ AppState::Code BattleApp::OnInit() {
mainPipSetup.BlendState.DstFactorRGB = BlendFactor::OneMinusSrcAlpha;
MainDrawState.Pipeline = Gfx::CreateResource(mainPipSetup);

// Setup pipeline for offscreen rendering (with palette shader)
Id paletteShader = Gfx::CreateResource(PaletteShader::Setup());
auto palettePipSetup = PipelineSetup::FromLayoutAndShader(shapeBuilder.Layout, paletteShader);
palettePipSetup.BlendState.ColorFormat = rtSetup.ColorFormat;
palettePipSetup.BlendState.DepthFormat = rtSetup.DepthFormat;
palettePipSetup.RasterizerState.SampleCount = rtSetup.SampleCount;
palettePipSetup.DepthStencilState.DepthWriteEnabled = false;
palettePipSetup.BlendState.BlendEnabled = true;
palettePipSetup.BlendState.ColorWriteMask = PixelChannel::RGB;
palettePipSetup.BlendState.SrcFactorRGB = BlendFactor::SrcAlpha;
palettePipSetup.BlendState.DstFactorRGB = BlendFactor::OneMinusSrcAlpha;
PaletteDrawState.Pipeline = Gfx::CreateResource(palettePipSetup);

// Setup pipeline for screen quad
ShapeBuilder shapeBuilderScreenQuad;
shapeBuilderScreenQuad.Layout
Expand Down
5 changes: 5 additions & 0 deletions code/BattleApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "glm/mat4x4.hpp"

#include "Core/Main.h"
#include "Core/Time/Clock.h"
#include "Gfx/Gfx.h"

#include "Camera.h"
Expand Down Expand Up @@ -31,14 +32,18 @@ class BattleApp : public App {

Id MainRenderPass;
DrawState MainDrawState; // Renders to texture at native GBA resolution
DrawState PaletteDrawState; // Renders to texture at native GBA resolution using palette and indexed image
MainShader::gl vsGLParams;
MainShader::gba vsGBAParams;
PaletteShader::palette1 fsPallete1Params;
PaletteShader::palette2 fsPallete2Params;
glm::mat4 ViewProj;

DrawState ScreenQuadDrawState; // Displays render texture to screen, upscaled

Id UnitMesh;

TimePoint LastFrameTimePoint;
Camera Cam;
Renderer Renderer;
Controls Controls;
Expand Down
20 changes: 20 additions & 0 deletions code/MapFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ void MapFile::ProcessLine(Renderer::LvlData& lvl, const std::string& line) {
tilemap.pos.y = static_cast<float>(values[2]);
tilemap.pos.z = static_cast<float>(values[3]);

if (words.size() > 6 && words[5] == "palette")
{
tilemap.paletteTexIdx = ReadInt(words, 6);
}
else
{
tilemap.paletteTexIdx = -1; // No palette
}

lvl.tilemaps[NumTilemapsLoaded] = tilemap;
NumTilemapsLoaded++;
}
Expand Down Expand Up @@ -208,6 +217,17 @@ void MapFile::ProcessLine(Renderer::LvlData& lvl, const std::string& line) {

lvl.boxColliders.Add(box);
}
if (type == "paletteshift") {
Renderer::PaletteShift paletteShift;

paletteShift.paletteTexIdx = ReadInt(words, 1);
paletteShift.startIdx = ReadInt(words, 2);
paletteShift.endIdx = ReadInt(words, 3);
paletteShift.shiftSpeed = static_cast<float>(ReadInt(words, 4));
paletteShift.currentIdxOffset = 0.0f;

lvl.paletteShifts.Add(paletteShift);
}
else if (type == "opt") {
if (words[1] == "wall_gfx_height") {
lvl.wallGfxHeight = static_cast<float>(ReadInt(words, 2));
Expand Down
23 changes: 22 additions & 1 deletion code/Renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Renderer::Renderable::Renderable()

Renderer::Renderable::Renderable(const Wall& wall, const glm::vec3& viewSpacePos, const glm::mat3& transform) :
texIdx(wall.texIdx),
paletteTexIdx(-1),
pos(wall.pos),
viewSpacePos(viewSpacePos),
transform(transform)
Expand All @@ -27,6 +28,7 @@ Renderer::Renderable::Renderable(const Wall& wall, const glm::vec3& viewSpacePos

Renderer::Renderable::Renderable(const Sprite& sprite, const glm::vec3& viewSpacePos, const glm::mat3& transform) :
texIdx(sprite.texIdx),
paletteTexIdx(-1),
pos(sprite.pos),
viewSpacePos(viewSpacePos),
transform(transform)
Expand All @@ -35,6 +37,7 @@ Renderer::Renderable::Renderable(const Sprite& sprite, const glm::vec3& viewSpac

Renderer::Renderable::Renderable(const DropShadow& dropShadow, const glm::mat3& transform, int texIdx) :
texIdx(texIdx),
paletteTexIdx(-1),
transform(transform)
{
}
Expand All @@ -45,6 +48,7 @@ void Renderer::LvlData::Reset() {
texPaths.Clear();
texSizes.Clear();

paletteShifts.Clear();
for (int dir = 0; dir < MAX_WALL_DIRECTIONS; ++dir) {
walls.walls[dir].Clear();
}
Expand All @@ -68,13 +72,14 @@ void Renderer::Setup(LvlData& lvl) {
}


void Renderer::Update(Camera& cam, LvlData& lvl) {
void Renderer::Update(float delta, Camera& cam, LvlData& lvl) {
glm::mat3 scale = glm::scale(glm::mat3(), glm::vec2(1.0, glm::cos(cam.Pitch)) * MAP_AND_WALL_SCALE);
glm::mat3 rotate = glm::rotate(scale, -cam.Heading);
TileMapAffine = rotate;

UpdateWallsVisibility(cam);
UpdateWallsAffine(cam.GetTransformInverse(), lvl);
UpdatePalettes(delta, lvl);
}


Expand Down Expand Up @@ -149,6 +154,7 @@ Renderer::TilemapList& Renderer::UpdateTilemaps(Camera& cam, LvlData& lvl) {
Renderable& dst = Tilemaps[layer];
dst.transform = modelTranslate * TileMapAffine;
dst.texIdx = map.texIdx;
dst.paletteTexIdx = map.paletteTexIdx;
}

return Tilemaps;
Expand Down Expand Up @@ -375,3 +381,18 @@ bool Renderer::CollideCircleBox2D(glm::vec2 circlePos, float circleRadius, BoxCo

return false;
}

void Renderer::UpdatePalettes(float delta, LvlData& lvl)
{
for (int i = 0; i < lvl.paletteShifts.Size(); ++i) {
PaletteShift& paletteShift = lvl.paletteShifts[i];

paletteShift.currentIdxOffset += paletteShift.shiftSpeed * delta;

float width = static_cast<float>(paletteShift.endIdx - paletteShift.startIdx);
if (paletteShift.currentIdxOffset >= width)
{
paletteShift.currentIdxOffset -= width;
}
}
}
19 changes: 18 additions & 1 deletion code/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,21 @@ class Renderer {
struct Tilemap {
glm::vec3 pos;
int texIdx;
int paletteTexIdx;
};

struct PaletteShift {
int paletteTexIdx;
int startIdx;
int endIdx; // Inclusive
float shiftSpeed; // In idx-per-second
float currentIdxOffset;
};

static const int MAX_SHIFTS_PER_PALETTE = 2;

typedef Oryol::Array<PaletteShift> PaletteShifts;


struct Sprite {
glm::vec3 pos;
Expand Down Expand Up @@ -89,6 +102,7 @@ class Renderer {
Renderable(const DropShadow& dropShadow, const glm::mat3& transform, int texIdx);

int texIdx;
int paletteTexIdx;
glm::vec3 pos;
glm::vec3 viewSpacePos;
glm::mat3 transform;
Expand All @@ -109,6 +123,7 @@ class Renderer {
TexSizes texSizes;

Tilemap tilemaps[MAX_TILEMAP_LAYERS];
PaletteShifts paletteShifts;
AllWalls walls;
Sprites sprites;
DropShadows dropShadows;
Expand All @@ -127,7 +142,7 @@ class Renderer {

void Setup(LvlData& lvl);

void Update(Camera& cam, LvlData& lvl);
void Update(float delta, Camera& cam, LvlData& lvl);

TilemapList& UpdateTilemaps(Camera& cam, LvlData& lvl);

Expand All @@ -147,6 +162,8 @@ class Renderer {

static bool CollideCircleBox2D(glm::vec2 circlePos, float circleRadius, BoxCollider box);

void UpdatePalettes(float delta, LvlData& lvl);

glm::mat3 TileMapAffine;
glm::mat3 WallAffine[WallDirection::MAX_WALL_DIRECTIONS];
bool WallVisible[WallDirection::MAX_WALL_DIRECTIONS];
Expand Down
4 changes: 4 additions & 0 deletions code/files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ files: [

# Emerald Beach
'assets/emerald_beach.map',
'assets/emerald_beach/emerald_beach.png',
'assets/emerald_beach/emerald_beach_palette.png',
'assets/emerald_beach/bg2.png',
'assets/emerald_beach/bg3.png',
'assets/emerald_beach/walls/1.png',
Expand All @@ -42,6 +44,8 @@ files: [

# Battle Highway
'assets/battle_highway.map',
'assets/battle_highway/battle_highway.png',
'assets/battle_highway/battle_highway_palette.png',
'assets/battle_highway/bg2.png',
'assets/battle_highway/bg3.png',
'assets/battle_highway/walls/1.png',
Expand Down
Loading

0 comments on commit 2d2f823

Please sign in to comment.