Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework Palette handling #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(SOURCES
source/instructions/decoder.cpp
source/util/registers.cpp
source/util/flags.cpp
source/palette/palette.cpp
source/exception/state_exception.cpp
)

Expand Down Expand Up @@ -86,6 +87,7 @@ set(HEADERS
source/util/gpumode.h
source/util/return_codes.h
source/palette/dmg_palette.h
source/palette/palette.h
source/exception/state_exception.h
)

Expand Down
18 changes: 10 additions & 8 deletions core/source/palette/dmg_palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@
#ifndef FB_CORE_PALETTE_DMG_H
#define FB_CORE_PALETTE_DMG_H

#include <util/typedefs.h>
#include "palette.h"

namespace FunkyBoy::Palette::ARGB8888 {
namespace FunkyBoy::Palette {

const u8 DMG[4][3] = {
{255, 255, 255},
{192, 192, 192},
{96, 96, 96},
{0, 0, 0}
};
inline palette createDMGPalette() {
palette palette;
palette.setColor(0, 255, 255, 255);
palette.setColor(1, 192, 192, 192);
palette.setColor(2, 96, 96, 96);
palette.setColor(3, 0, 0, 0);
return palette;
}

}

Expand Down
33 changes: 33 additions & 0 deletions core/source/palette/palette.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2020 Michel Kremer (kremi151)
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "palette.h"

using namespace FunkyBoy::Palette;

color::color(): red(0), green(0), blue(0) {
}

color::color(FunkyBoy::u8 red, FunkyBoy::u8 green, FunkyBoy::u8 blue): red(red), green(green), blue(blue) {
}

void palette::setColor(int index, FunkyBoy::u8 red, FunkyBoy::u8 green, FunkyBoy::u8 blue) {
colors[index] = color(red, green, blue);
}

const color& palette::operator[](int index) const {
return colors[index];
}
57 changes: 57 additions & 0 deletions core/source/palette/palette.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Copyright 2020 Michel Kremer (kremi151)
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FB_SDL_PALETTE_H
#define FB_SDL_PALETTE_H

#include <util/typedefs.h>

namespace FunkyBoy::Palette {

class color {
private:
u8 red, green, blue;
public:
color();
color(u8 red, u8 green, u8 blue);

inline u8 getRed() const {
return red;
}

inline u8 getGreen() const {
return green;
}

inline u8 getBlue() const {
return blue;
}

};

class palette {
private:
color colors[4]{};
public:
palette() = default;

void setColor(int index, u8 red, u8 green, u8 blue);
const color &operator[] (int index) const;
};

}

#endif //FB_SDL_PALETTE_H
5 changes: 3 additions & 2 deletions platform-libretro/source/display_libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ using namespace FunkyBoy::Controller;
DisplayControllerLibretro::DisplayControllerLibretro()
: pixels(new uint32_t[FB_GB_DISPLAY_WIDTH * FB_GB_DISPLAY_HEIGHT]{})
, videoCb(nullptr)
, palette(Palette::createDMGPalette())
{
}

Expand All @@ -33,8 +34,8 @@ DisplayControllerLibretro::~DisplayControllerLibretro() {
void DisplayControllerLibretro::drawScanLine(FunkyBoy::u8 y, FunkyBoy::u8 *buffer) {
uint32_t pixel;
for (u8 x = 0 ; x < FB_GB_DISPLAY_WIDTH ; x++) {
auto &color = Palette::ARGB8888::DMG[*(buffer + x)];
pixel = (255u << 24u) | (color[0] << 16) | (color[1] << 8) | color[2];
auto &color = palette[*(buffer + x)];
pixel = (255u << 24u) | (color.getRed() << 16) | (color.getGreen() << 8) | color.getBlue();
pixels[(y * FB_GB_DISPLAY_WIDTH) + x] = pixel;
}
}
Expand Down
2 changes: 2 additions & 0 deletions platform-libretro/source/display_libretro.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define FB_LIBRETRO_DISPLAY_LIBRETRO_H

#include <controllers/display.h>
#include <palette/palette.h>

#include <libretro.h>

Expand All @@ -27,6 +28,7 @@ namespace FunkyBoy::Controller {
private:
uint32_t *pixels;
retro_video_refresh_t videoCb;
Palette::palette palette;
public:
DisplayControllerLibretro();
~DisplayControllerLibretro() override;
Expand Down
8 changes: 4 additions & 4 deletions platform-sdl/source/controllers/display_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

#include "display_sdl.h"
#include <util/typedefs.h>
#include <palette/dmg_palette.h>

using namespace FunkyBoy::Controller;

DisplayControllerSDL::DisplayControllerSDL(SDL_Renderer *renderer, SDL_Texture *frameBuffer)
DisplayControllerSDL::DisplayControllerSDL(SDL_Renderer *renderer, SDL_Texture *frameBuffer, const FunkyBoy::Palette::palette &palette)
: renderer(renderer)
, frameBuffer(frameBuffer)
, pixels(new uint32_t[FB_GB_DISPLAY_WIDTH * FB_GB_DISPLAY_HEIGHT])
, palette(palette)
{
}

Expand All @@ -34,8 +34,8 @@ DisplayControllerSDL::~DisplayControllerSDL() {
void DisplayControllerSDL::drawScanLine(FunkyBoy::u8 y, FunkyBoy::u8 *buffer) {
uint32_t pixel;
for (u8 x = 0 ; x < FB_GB_DISPLAY_WIDTH ; x++) {
auto &color = Palette::ARGB8888::DMG[*(buffer + x)];
pixel = (255u << 24u) | (color[0] << 16) | (color[1] << 8) | color[2];
auto &color = palette[*(buffer + x)];
pixel = (255u << 24u) | (color.getRed() << 16) | (color.getGreen() << 8) | color.getBlue();
pixels[(y * FB_GB_DISPLAY_WIDTH) + x] = pixel;
}
}
Expand Down
4 changes: 3 additions & 1 deletion platform-sdl/source/controllers/display_sdl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <SDL.h>
#include <controllers/display.h>
#include <palette/palette.h>

namespace FunkyBoy::Controller {

Expand All @@ -27,8 +28,9 @@ namespace FunkyBoy::Controller {
SDL_Renderer *renderer;
SDL_Texture *frameBuffer;
uint32_t *pixels;
Palette::palette palette;
public:
explicit DisplayControllerSDL(SDL_Renderer *renderer, SDL_Texture *frameBuffer);
DisplayControllerSDL(SDL_Renderer *renderer, SDL_Texture *frameBuffer, const Palette::palette &palette);
~DisplayControllerSDL() override;

void drawScanLine(u8 y, u8 *buffer) override;
Expand Down
10 changes: 9 additions & 1 deletion platform-sdl/source/window/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <controllers/display_sdl.h>
#include <ui/native_ui.h>
#include <fstream>
#include <palette/dmg_palette.h>

using namespace FunkyBoy::SDL;

Expand Down Expand Up @@ -49,7 +50,14 @@ Window::~Window() {
}
}

void Window::readConfigJson(FunkyBoy::Palette::palette &palette) {
// TODO: Implement
}

bool Window::init(int argc, char **argv, size_t width, size_t height) {
Palette::palette palette = Palette::createDMGPalette();
readConfigJson(palette);

window = SDL_CreateWindow(
FB_NAME,
SDL_WINDOWPOS_UNDEFINED,
Expand All @@ -70,7 +78,7 @@ bool Window::init(int argc, char **argv, size_t width, size_t height) {

controllers->setSerial(std::make_shared<Controller::SerialControllerSDL>());
controllers->setJoypad(std::make_shared<Controller::JoypadControllerSDL>());
controllers->setDisplay(std::make_shared<Controller::DisplayControllerSDL>(renderer, frameBuffer));
controllers->setDisplay(std::make_shared<Controller::DisplayControllerSDL>(renderer, frameBuffer, palette));

fs::path romPath;
if (argc <= 1) {
Expand Down
3 changes: 3 additions & 0 deletions platform-sdl/source/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <util/typedefs.h>
#include <emulator/emulator.h>
#include <util/fs.h>
#include <palette/palette.h>

namespace FunkyBoy::SDL {

Expand All @@ -46,6 +47,8 @@ namespace FunkyBoy::SDL {

void loadSave();
void writeSave();

void readConfigJson(Palette::palette &palette);
public:
explicit Window(GameBoyType gbType);
~Window();
Expand Down