diff --git a/ion/src/simulator/assets/horizontal_arrow.jpg b/ion/src/simulator/assets/horizontal_arrow.jpg new file mode 100644 index 000000000..248143985 Binary files /dev/null and b/ion/src/simulator/assets/horizontal_arrow.jpg differ diff --git a/ion/src/simulator/assets/key_layouts/horizontal_arrow.png b/ion/src/simulator/assets/key_layouts/horizontal_arrow.png deleted file mode 100644 index 4a75fabee..000000000 Binary files a/ion/src/simulator/assets/key_layouts/horizontal_arrow.png and /dev/null differ diff --git a/ion/src/simulator/assets/key_layouts/large_squircle.png b/ion/src/simulator/assets/key_layouts/large_squircle.png deleted file mode 100644 index bd223f189..000000000 Binary files a/ion/src/simulator/assets/key_layouts/large_squircle.png and /dev/null differ diff --git a/ion/src/simulator/assets/key_layouts/round.png b/ion/src/simulator/assets/key_layouts/round.png deleted file mode 100644 index cb94b177e..000000000 Binary files a/ion/src/simulator/assets/key_layouts/round.png and /dev/null differ diff --git a/ion/src/simulator/assets/key_layouts/small_squircle.png b/ion/src/simulator/assets/key_layouts/small_squircle.png deleted file mode 100644 index 32dd991a1..000000000 Binary files a/ion/src/simulator/assets/key_layouts/small_squircle.png and /dev/null differ diff --git a/ion/src/simulator/assets/key_layouts/vertical_arrow.png b/ion/src/simulator/assets/key_layouts/vertical_arrow.png deleted file mode 100644 index ef86bfaf2..000000000 Binary files a/ion/src/simulator/assets/key_layouts/vertical_arrow.png and /dev/null differ diff --git a/ion/src/simulator/assets/large_squircle.jpg b/ion/src/simulator/assets/large_squircle.jpg new file mode 100644 index 000000000..cb729b6b9 Binary files /dev/null and b/ion/src/simulator/assets/large_squircle.jpg differ diff --git a/ion/src/simulator/assets/round.jpg b/ion/src/simulator/assets/round.jpg new file mode 100644 index 000000000..9311aacee Binary files /dev/null and b/ion/src/simulator/assets/round.jpg differ diff --git a/ion/src/simulator/assets/small_squircle.jpg b/ion/src/simulator/assets/small_squircle.jpg new file mode 100644 index 000000000..9673e9851 Binary files /dev/null and b/ion/src/simulator/assets/small_squircle.jpg differ diff --git a/ion/src/simulator/assets/vertical_arrow.jpg b/ion/src/simulator/assets/vertical_arrow.jpg new file mode 100644 index 000000000..b6e34cf1d Binary files /dev/null and b/ion/src/simulator/assets/vertical_arrow.jpg differ diff --git a/ion/src/simulator/shared/layout.cpp b/ion/src/simulator/shared/layout.cpp index bbdf97d10..038362fa5 100644 --- a/ion/src/simulator/shared/layout.cpp +++ b/ion/src/simulator/shared/layout.cpp @@ -4,11 +4,6 @@ #include #include #include -#include "../assets/key_layouts/horizontal_arrow.h" -#include "../assets/key_layouts/large_squircle.h" -#include "../assets/key_layouts/round.h" -#include "../assets/key_layouts/small_squircle.h" -#include "../assets/key_layouts/vertical_arrow.h" namespace Ion { namespace Simulator { @@ -98,13 +93,23 @@ void getBackgroundRect(SDL_Rect * rect) { class KeyLayout { public: - enum class Shape { + enum class Shape : uint8_t { HorizontalArrow, VerticalArrow, Round, SmallSquircle, - LargeSquircle + LargeSquircle, + NumberOfShapes }; + static constexpr size_t NumberOfShapes = (size_t)Shape::NumberOfShapes; + static constexpr const char * imagePathForKey[KeyLayout::NumberOfShapes] = { + "horizontal_arrow.jpg", + "vertical_arrow.jpg", + "round.jpg", + "small_squircle.jpg", + "large_squircle.jpg" + }; + constexpr KeyLayout(float x, float y, Shape shape) : m_center{X(x), Y(y)}, m_shape(shape) {} @@ -116,6 +121,8 @@ private: Shape m_shape; }; +constexpr const char * const KeyLayout::imagePathForKey[KeyLayout::NumberOfShapes]; + static constexpr KeyLayout sKeyLayouts[Keyboard::NumberOfValidKeys] = { KeyLayout(191, 1029, KeyLayout::Shape::HorizontalArrow), // A1, Left KeyLayout(273, 945, KeyLayout::Shape::VerticalArrow), // A2, Up @@ -173,44 +180,38 @@ static constexpr KeyLayout sKeyLayouts[Keyboard::NumberOfValidKeys] = { KeyLayout(950, 2040, KeyLayout::Shape::LargeSquircle), // I5, EXE }; -const Image * imageForKey(int keyIndex) { - if (keyIndex == -1) { - return nullptr; - } - assert(keyIndex >= 0 && keyIndex < Keyboard::NumberOfValidKeys); - switch (sKeyLayouts[keyIndex].shape()) { - case KeyLayout::Shape::Round: - return ImageStore::Round; - case KeyLayout::Shape::LargeSquircle: - return ImageStore::LargeSquircle; - case KeyLayout::Shape::SmallSquircle: - return ImageStore::SmallSquircle; - case KeyLayout::Shape::VerticalArrow: - return ImageStore::VerticalArrow; - default: - assert(sKeyLayouts[keyIndex].shape() == KeyLayout::Shape::HorizontalArrow); - return ImageStore::HorizontalArrow; - } -} - static void getKeyCenter(int validKeyIndex, SDL_Point * point) { assert(validKeyIndex >= 0 && validKeyIndex < Keyboard::NumberOfValidKeys); makeAbsolute(sKeyLayouts[validKeyIndex].center(), point); } -static void getKeyRectangle(int validKeyIndex, SDL_Rect * rect) { +static void getKeyRectangle(int validKeyIndex, SDL_Texture * texture, SDL_Rect * rect) { assert(validKeyIndex >= 0 && validKeyIndex < Keyboard::NumberOfValidKeys); SDL_FPoint point = sKeyLayouts[validKeyIndex].center(); - const Image * img = imageForKey(validKeyIndex); + int w, h; + SDL_QueryTexture(texture, NULL, NULL, &w, &h); SDL_FRect fRect; - fRect.w = X(img->width()); - fRect.h = Y(img->height()); + fRect.w = X(w); + fRect.h = Y(h); fRect.x = point.x - fRect.w/2.0f; fRect.y = point.y - fRect.h/2.0f; makeAbsolute(fRect, rect); } -int sHighlightedKeyIndex; +static constexpr uint8_t k_blendingRatio = 0x44; +static SDL_Texture * sBackgroundTexture = nullptr; +static SDL_Texture * sKeyLayoutTextures[KeyLayout::NumberOfShapes]; + +void init(SDL_Renderer * renderer) { + sBackgroundTexture = IonSimulatorLoadImage(renderer, "background.jpg"); + for (size_t i = 0; i < KeyLayout::NumberOfShapes; i++) { + sKeyLayoutTextures[i] = IonSimulatorLoadImage(renderer, KeyLayout::imagePathForKey[i]); + SDL_SetTextureBlendMode(sKeyLayoutTextures[i], SDL_BLENDMODE_BLEND); + SDL_SetTextureAlphaMod(sKeyLayoutTextures[i], k_blendingRatio); + } +} + +static int sHighlightedKeyIndex; Keyboard::Key highlightKeyAt(SDL_Point * p) { int newHighlightedKeyIndex = -1; @@ -240,65 +241,24 @@ Keyboard::Key highlightKeyAt(SDL_Point * p) { return nearestKey; } -SDL_PixelFormat * sRgbaPixelFormat = SDL_AllocFormat(SDL_PIXELFORMAT_RGBA32); - -// round.png file is 130 x 130 and is the largest key layout -static constexpr size_t k_maxKeyLayoutSize = 130*130; -static constexpr uint8_t k_blendingRatio = 0x44; - -// TODO: use a "native" image decompressor instead of LZ4 -void fillRGBABufferWithImage(Uint32 * buffer, const Image * img) { - KDColor pixelBuffer[k_maxKeyLayoutSize]; - Ion::decompress( - img->compressedPixelData(), - reinterpret_cast(pixelBuffer), - img->compressedPixelDataSize(), - img->width() * img->height() * sizeof(KDColor) - ); - for (int i = 0; i < img->width() * img->height(); i++) { - buffer[i] = SDL_MapRGBA(sRgbaPixelFormat, pixelBuffer[i].red(), pixelBuffer[i].green(), pixelBuffer[i].blue(), k_blendingRatio); - } +void unhighlightKey() { + sHighlightedKeyIndex = -1; + Main::setNeedsRefresh(); } void drawHighlightedKey(SDL_Renderer * renderer) { if (sHighlightedKeyIndex < 0) { return; } - const Image * img = imageForKey(sHighlightedKeyIndex); - SDL_Texture * framebufferTexture = SDL_CreateTexture( - renderer, - SDL_PIXELFORMAT_RGBA32, - SDL_TEXTUREACCESS_STREAMING, - img->width(), - img->height() - ); - SDL_SetTextureBlendMode(framebufferTexture, SDL_BLENDMODE_BLEND); - int pitch = 0; - void * pixels = nullptr; - SDL_LockTexture(framebufferTexture, nullptr, &pixels, &pitch); - assert(pitch == sizeof(Uint32) * img->width()); - fillRGBABufferWithImage(static_cast(pixels), img); - SDL_UnlockTexture(framebufferTexture); + int shape = static_cast(sKeyLayouts[sHighlightedKeyIndex].shape()); + SDL_Texture * keyTexture = sKeyLayoutTextures[shape]; SDL_Rect rect; - getKeyRectangle(sHighlightedKeyIndex, &rect); - SDL_RenderCopy(renderer, framebufferTexture, nullptr, &rect); - SDL_DestroyTexture(framebufferTexture); - + getKeyRectangle(sHighlightedKeyIndex, keyTexture, &rect); + SDL_RenderCopy(renderer, keyTexture, nullptr, &rect); // Reset highlighted key unhighlightKey(); } -void unhighlightKey() { - sHighlightedKeyIndex = -1; - Main::setNeedsRefresh(); -} - -static SDL_Texture * sBackgroundTexture = nullptr; - -void init(SDL_Renderer * renderer) { - sBackgroundTexture = IonSimulatorLoadImage(renderer, "background.jpg"); -} - void draw(SDL_Renderer * renderer) { SDL_Rect backgroundRect; getBackgroundRect(&backgroundRect);