[gens-sdl] Make use of the bpp command line parameter.
authorDavid Korth <gerbilsoft@gerbilsoft.com>
Mon, 7 Sep 2015 15:52:09 +0000 (11:52 -0400)
committerDavid Korth <gerbilsoft@gerbilsoft.com>
Mon, 7 Sep 2015 15:52:09 +0000 (11:52 -0400)
Options::bpp() now returns an MdFb::ColorDepth value.

TODO: Remove the bpp value from VBackend's constructor.
This will require re-creating the texture whenever the MdFb's
color depth changes.

14 files changed:
src/gens-sdl/CrazyEffectLoop.cpp
src/gens-sdl/EmuLoop.cpp
src/gens-sdl/GLBackend.cpp
src/gens-sdl/GLBackend.hpp
src/gens-sdl/Options.cpp
src/gens-sdl/Options.hpp
src/gens-sdl/SdlGLBackend.cpp
src/gens-sdl/SdlGLBackend.hpp
src/gens-sdl/SdlHandler.cpp
src/gens-sdl/SdlHandler.hpp
src/gens-sdl/SdlSWBackend.cpp
src/gens-sdl/SdlSWBackend.hpp
src/gens-sdl/VBackend.cpp
src/gens-sdl/VBackend.hpp

index ee751e0..60964a9 100644 (file)
@@ -107,7 +107,7 @@ int CrazyEffectLoop::run(const Options *options)
 
        // Initialize the SDL handlers.
        d->sdlHandler = new SdlHandler();
-       if (d->sdlHandler->init_video() < 0)
+       if (d->sdlHandler->init_video(options->bpp()) < 0)
                return EXIT_FAILURE;
        // No audio here.
        //if (d->sdlHandler->init_audio() < 0)
@@ -131,7 +131,7 @@ int CrazyEffectLoop::run(const Options *options)
        // Image size defaults to the full framebuffer,
        // so we don't have to worry about "stretch modes".
        d->crazyFb = new MdFb();
-       d->crazyFb->setBpp(MdFb::BPP_32);
+       d->crazyFb->setBpp(options->bpp());
        // Set the SDL video source.
        d->sdlHandler->set_video_source(d->crazyFb);
 
index af5ab02..f69b6c2 100644 (file)
@@ -587,7 +587,7 @@ int EmuLoop::run(const Options *options)
 
        // Initialize the SDL handlers.
        d->sdlHandler = new SdlHandler();
-       if (d->sdlHandler->init_video() < 0)
+       if (d->sdlHandler->init_video(options->bpp()) < 0)
                return EXIT_FAILURE;
        if (d->sdlHandler->init_audio() < 0)
                return EXIT_FAILURE;
@@ -607,9 +607,8 @@ int EmuLoop::run(const Options *options)
        // TODO: Close the ROM, or let EmuContext do it?
 
        // Set the color depth.
-       // TODO: Command line option?
        MdFb *fb = d->emuContext->m_vdp->MD_Screen->ref();
-       fb->setBpp(MdFb::BPP_32);
+       fb->setBpp(options->bpp());
 
        // Set the SDL video source.
        d->sdlHandler->set_video_source(fb);
index 72538bc..b7eeef6 100644 (file)
@@ -20,6 +20,7 @@
  ***************************************************************************/
 
 #include "GLBackend.hpp"
+
 #include "libgens/Util/MdFb.hpp"
 using LibGens::MdFb;
 
@@ -375,12 +376,17 @@ void GLBackendPrivate::stopShaderEffects(void)
 
 /** GLBackend **/
 
-GLBackend::GLBackend()
-       : d(new GLBackendPrivate(this))
+GLBackend::GLBackend(MdFb::ColorDepth bpp)
+       : super(bpp)
+       , d(new GLBackendPrivate(this))
        , m_winW(640), m_winH(480)
 {
        // Default window size is 640x480.
        // GL must be initialized by the subclass.
+
+       // TODO: Remove bpp parameter;
+       // update other classes' bpp when the
+       // MdFb's bpp changes?
 }
 
 GLBackend::~GLBackend()
index 8d67700..c42440b 100644 (file)
 
 // Video Backend.
 #include "VBackend.hpp"
-// LibGens includes.
-#include "libgens/Util/MdFb.hpp"
 
 namespace GensSdl {
 
 class GLBackendPrivate;
 class GLBackend : public VBackend {
        public:
-               GLBackend();
+               GLBackend(LibGens::MdFb::ColorDepth bpp);
                virtual ~GLBackend();
 
        private:
index 52251f6..cd91454 100644 (file)
 
 #include "Options.hpp"
 
+// MdFb
+#include "libgens/Util/MdFb.hpp"
+using LibGens::MdFb;
+
 // C includes. (C++ namespace)
 #include <cstring>
 #include <cerrno>
@@ -79,7 +83,7 @@ class OptionsPrivate
                int fps_counter;                // Enable FPS counter?
                int auto_pause;                 // Auto pause?
                int paused_effect;              // Paused effect?
-               int bpp;                        // Color depth. (15, 16, 32)
+               MdFb::ColorDepth bpp;           // Color depth. (15, 16, 32)
 
                // Special run modes.
                int run_crazy_effect;           // Run the Crazy Effect
@@ -116,7 +120,7 @@ void OptionsPrivate::reset(void)
        fps_counter = true;
        auto_pause = false;
        paused_effect = true;
-       bpp = 32;
+       bpp = MdFb::BPP_32;
 
        // Special run modes.
        run_crazy_effect = false;
@@ -201,8 +205,10 @@ int Options::parse(int argc, const char *argv[])
        struct {
                const char *rom_filename;
                const char *tmss_rom_filename;
+               int bpp;
        } tmp;
        memset(&tmp, 0, sizeof(tmp));
+       tmp.bpp = 32;
 
        // NOTE: rom_filename is provided as a non-option parameter.
        // It will get parsed later.
@@ -253,7 +259,7 @@ int Options::parse(int argc, const char *argv[])
                        "* Tint the window when paused.", NULL},
                {"no-paused-effect", 0, POPT_ARG_VAL, &d->paused_effect, 0,
                        "  Don't tint the window when paused.", NULL},
-               {"bpp", 0, POPT_ARG_INT, &d->bpp, 0,
+               {"bpp", 0, POPT_ARG_INT, &tmp.bpp, 0,
                        "  Set the internal color depth. (15, 16, 32)", "BPP"},
                POPT_TABLEEND
        };
@@ -385,12 +391,13 @@ int Options::parse(int argc, const char *argv[])
        }
 
        // Verify certain options.
-       if (d->bpp != 15 && d->bpp != 16 && d->bpp != 32) {
+       d->bpp = MdFb::bppToColorDepth(tmp.bpp);
+       if (d->bpp < 0 || d->bpp >= MdFb::BPP_MAX) {
                // Invalid color depth.
                fprintf(stderr, "%s: '--bpp=%d': invalid color depth\n"
                        "Valid options are 15, 16, and 32.\n"
                        "Try `%s --help` for more information.\n",
-                       argv[0], d->bpp, argv[0]);
+                       argv[0], tmp.bpp, argv[0]);
                poptFreeContext(optCon);
                return -EINVAL;
        }
@@ -460,7 +467,7 @@ ACCESSOR_BOOL(auto_fix_checksum)
 ACCESSOR_BOOL(fps_counter)
 ACCESSOR_BOOL(auto_pause)
 ACCESSOR_BOOL(paused_effect)
-ACCESSOR(uint8_t, bpp)
+ACCESSOR(MdFb::ColorDepth, bpp)
 
 /** Special run modes. **/
 ACCESSOR_BOOL(run_crazy_effect)
index 26a12b0..f259814 100644 (file)
@@ -22,6 +22,9 @@
 #ifndef __GENS_SDL_OPTIONS_HPP__
 #define __GENS_SDL_OPTIONS_HPP__
 
+// MdFb
+#include "libgens/Util/MdFb.hpp"
+
 // C++ includes.
 #include <string>
 
@@ -141,10 +144,10 @@ class Options
                bool paused_effect(void) const;
 
                /**
-                * Color depth to use. (15, 16, 32)
+                * Color depth to use.
                 * @return Color depth.
                 */
-               uint8_t bpp(void) const;
+               LibGens::MdFb::ColorDepth bpp(void) const;
 
                /** Special run modes. **/
 
index 17328f8..2691b2f 100644 (file)
@@ -20,6 +20,7 @@
  ***************************************************************************/
 
 #include "SdlGLBackend.hpp"
+
 #include "libgens/Util/MdFb.hpp"
 using LibGens::MdFb;
 
@@ -48,8 +49,8 @@ using LibGens::MdFb;
 
 namespace GensSdl {
 
-SdlGLBackend::SdlGLBackend()
-       : super()
+SdlGLBackend::SdlGLBackend(MdFb::ColorDepth bpp)
+       : super(bpp)
        , m_window(nullptr)
        , m_glContext(nullptr)
 {
@@ -58,11 +59,31 @@ SdlGLBackend::SdlGLBackend()
        // window isn't ridiculously tiny.
        // FIXME: SDL window resizing is broken.
        m_winW = 640; m_winH = 480;
-       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
-       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
-       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+
+       // Set the color depth.
+       switch (bpp) {
+               case MdFb::BPP_15:
+                       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
+                       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
+                       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
+                       break;
+               case MdFb::BPP_16:
+                       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
+                       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
+                       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
+                       break;
+               case MdFb::BPP_32:
+               default:
+                       SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
+                       SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
+                       SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
+                       break;
+       }
+
+       // Other GL parameters.
        SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, true);
+
        // TODO: Make sure m_window, m_glContext, etc. were created successfully.
        // TODO: Rename m_window to m_window?
        m_window = SDL_CreateWindow("Gens/GS II [SDL]",
index 5f34779..e8ba503 100644 (file)
 #include <SDL.h>
 
 // Video Backend.
-#include "VBackend.hpp"
 #include "GLBackend.hpp"
-// LibGens includes.
-#include "libgens/Util/MdFb.hpp"
 
 namespace GensSdl {
 
 class SdlGLBackend : public GLBackend {
        public:
-               SdlGLBackend();
+               SdlGLBackend(LibGens::MdFb::ColorDepth bpp);
                virtual ~SdlGLBackend();
 
        private:
index 6774a5b..fa07024 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "SdlHandler.hpp"
 #include "libgens/Util/MdFb.hpp"
+using LibGens::MdFb;
 
 #include "libgens/sound/SoundMgr.hpp"
 using LibGens::SoundMgr;
@@ -60,9 +61,10 @@ SdlHandler::~SdlHandler()
 /**
  * Initialize SDL video.
  * TODO: Parameter for GL rendering.
+ * @param bpp Color depth.
  * @return 0 on success; non-zero on error.
  */
-int SdlHandler::init_video(void)
+int SdlHandler::init_video(MdFb::ColorDepth bpp)
 {
        if (m_vBackend) {
                // Video is already initialized.
@@ -80,7 +82,7 @@ int SdlHandler::init_video(void)
 
        // Initialize the video backend.
        // TODO: Fullscreen; GL vs. SW selection; VSync.
-       m_vBackend = new SdlGLBackend();
+       m_vBackend = new SdlSWBackend(bpp);
        return 0;
 }
 
@@ -112,7 +114,7 @@ void SdlHandler::set_window_title(const char *title)
  * If nullptr, removes the SDL video source.
  * @param fb MdFb.
  */
-void SdlHandler::set_video_source(LibGens::MdFb *fb)
+void SdlHandler::set_video_source(MdFb *fb)
 {
        if (m_vBackend) {
                m_vBackend->set_video_source(fb);
index 181c9ad..ad9c91e 100644 (file)
 // SDL
 #include <SDL.h>
 
+#include "libgens/Util/MdFb.hpp"
 #include "libgenskeys/GensKey_t.h"
-namespace LibGens {
-       class MdFb;
-}
 
 // TODO: Minimum gcc version, other compilers?
 // TODO: Move to libgens/macros/common.h?
@@ -65,9 +63,10 @@ class SdlHandler {
                /**
                 * Initialize SDL video.
                 * TODO: Parameter for GL rendering.
+                * @param bpp Color depth.
                 * @return 0 on success; non-zero on error.
                 */
-               int init_video(void);
+               int init_video(LibGens::MdFb::ColorDepth bpp);
 
                /**
                 * Shut down SDL video.
index 6cba8ee..15d133d 100644 (file)
@@ -20,7 +20,9 @@
  ***************************************************************************/
 
 #include "SdlSWBackend.hpp"
+
 #include "libgens/Util/MdFb.hpp"
+using LibGens::MdFb;
 
 // C includes. (C++ namespace)
 #include <cassert>
@@ -36,8 +38,9 @@
 
 namespace GensSdl {
 
-SdlSWBackend::SdlSWBackend()
-       : m_window(nullptr)
+SdlSWBackend::SdlSWBackend(MdFb::ColorDepth bpp)
+       : super(bpp)
+       , m_window(nullptr)
        , m_renderer(nullptr)
        , m_texture(nullptr)
 {
@@ -72,8 +75,20 @@ SdlSWBackend::SdlSWBackend()
        SDL_RenderPresent(m_renderer);
 
        // Create a texture.
-       m_texture = SDL_CreateTexture(m_renderer,
-                       SDL_PIXELFORMAT_ARGB8888,
+       uint32_t format;
+       switch (bpp) {
+               case MdFb::BPP_15:
+                       format = SDL_PIXELFORMAT_RGB555;
+                       break;
+               case MdFb::BPP_16:
+                       format = SDL_PIXELFORMAT_RGB565;
+                       break;
+               case MdFb::BPP_32:
+               default:
+                       format = SDL_PIXELFORMAT_ARGB8888;
+                       break;
+       }
+       m_texture = SDL_CreateTexture(m_renderer, format,
                        SDL_TEXTUREACCESS_STREAMING,
                        320, 240);
 }
@@ -133,7 +148,12 @@ void SdlSWBackend::update(bool fb_dirty)
        SDL_RenderClear(m_renderer);
        if (m_fb) {
                // Source surface is available.
-               SDL_UpdateTexture(m_texture, nullptr, m_fb->fb32(), m_fb->pxPitch() * sizeof(uint32_t));
+               // TODO: Verify color depth.
+               if (m_bpp == MdFb::BPP_32) {
+                       SDL_UpdateTexture(m_texture, nullptr, m_fb->fb32(), m_fb->pxPitch() * sizeof(uint32_t));
+               } else {
+                       SDL_UpdateTexture(m_texture, nullptr, m_fb->fb16(), m_fb->pxPitch() * sizeof(uint16_t));
+               }
                SDL_RenderCopy(m_renderer, m_texture, nullptr, nullptr);
        }
 
index 3da94bb..8bc4b6f 100644 (file)
@@ -38,7 +38,7 @@ namespace GensSdl {
 
 class SdlSWBackend : public VBackend {
        public:
-               SdlSWBackend();
+               SdlSWBackend(LibGens::MdFb::ColorDepth bpp);
                virtual ~SdlSWBackend();
 
        private:
index 96cf416..9128ebe 100644 (file)
  ***************************************************************************/
 
 #include "VBackend.hpp"
+
 #include "libgens/Util/MdFb.hpp"
+using LibGens::MdFb;
 
 // C includes. (C++ namespace)
 #include <cstdio>
 
 namespace GensSdl {
 
-VBackend::VBackend()
+VBackend::VBackend(MdFb::ColorDepth bpp)
        : m_dirty(true)
        , m_fullscreen(false)
        , m_fb(nullptr)
        , m_int_fb(nullptr)
+       , m_bpp(bpp)
        , m_stretchMode(STRETCH_H)
        , m_aspectRatioConstraint(true)
        , m_pausedEffect(false)
index 02f2664..a120d9f 100644 (file)
@@ -40,8 +40,9 @@
 // ZOMG image data.
 struct _Zomg_Img_Data_t;
 
+// LibGens includes.
+#include "libgens/Util/MdFb.hpp"
 namespace LibGens {
-       class MdFb;
        class EmuContext;
 }
 
@@ -49,7 +50,7 @@ namespace GensSdl {
 
 class VBackend {
        public:
-               VBackend();
+               VBackend(LibGens::MdFb::ColorDepth bpp);
                virtual ~VBackend();
 
        private:
@@ -197,6 +198,9 @@ class VBackend {
                // Internal MdFb for effects.
                LibGens::MdFb *m_int_fb;
 
+               // Internal color depth.
+               LibGens::MdFb::ColorDepth m_bpp;
+
                // Dirty flag functions.
                void setDirty(void);
                void clearDirty(void);