OSDN Git Service

気になるところを少し整理。
authorMandhelingFreak <mandheling30-freak@yahoo.co.jp>
Wed, 5 Mar 2014 09:58:26 +0000 (18:58 +0900)
committerMandhelingFreak <mandheling30-freak@yahoo.co.jp>
Sat, 8 Mar 2014 13:04:10 +0000 (22:04 +0900)
GVONavish/GVONavish/GVOGameProcess.cpp
GVONavish/GVONavish/GVOImage.cpp
GVONavish/GVONavish/GVOImage.h
GVONavish/GVONavish/GVORenderer.cpp
GVONavish/GVONavish/GVORenderer.h
GVONavish/GVONavish/GVOTexture.cpp
GVONavish/GVONavish/GVOTexture.h

index 05766bf..b5e811a 100644 (file)
@@ -291,7 +291,7 @@ void GVOGameProcess::threadMain()
 void GVOGameProcess::grabImage( HDC hdc, const POINT& offset, const SIZE& size )
 {
        if ( !m_surveyCoordImage.bitmapHandle() ) {
-               m_surveyCoordImage.createDIBImage( k_surveyCoordSize );
+               m_surveyCoordImage.createImage( k_surveyCoordSize );
        }
 
        HDC hdcMem = ::CreateCompatibleDC( hdc );
@@ -351,7 +351,7 @@ void GVOGameProcess::extractGameIcon()
        const int height = bmp.bmHeight;
 
        ::EnterCriticalSection( &m_lock );
-       m_shipIconImage.createRGBAImage( width, height );
+       m_shipIconImage.createImage( width, height, k_GVOPixelFormat_RGBA );
 
        uint32_t * d = reinterpret_cast<uint32_t*>(m_shipIconImage.mutableImageBits());
        for ( int y = 0; y < height; ++y ) {
index 066a48f..8230e51 100644 (file)
@@ -39,7 +39,7 @@ namespace {
 
 bool GVOImage::stretchCopy( const GVOImage& src, uint32_t width, uint32_t height )
 {
-       if ( !createDIBImage( width, height ) ) {
+       if ( !createImage( width, height ) ) {
                return false;
        }
 
@@ -66,62 +66,60 @@ bool GVOImage::stretchCopy( const GVOImage& src, uint32_t width, uint32_t height
                ::BitBlt( hdcDst, 0, 0, m_size.cx, m_size.cy,
                        hdcSrc, 0, 0, SRCCOPY );
        }
+       m_pixelFormat = src.m_pixelFormat;
+
 
        ::RestoreDC( hdcSrc, -1 );
        ::DeleteDC( hdcSrc );
        ::RestoreDC( hdcDst, -1 );
        ::DeleteDC( hdcDst );
        ::ReleaseDC( NULL, hdc );
-       m_fileName = L"";
        return true;
 }
 
 
-bool GVOImage::createRGBAImage( int width, int height )
+bool GVOImage::createImage( int width, int height, GVOPixelFormat pixelFormat )
 {
        reset();
 
-       BITMAPV5HEADER bmih = { sizeof(bmih) };
-       bmih.bV5Compression = BI_BITFIELDS;
-       bmih.bV5BlueMask = 0xFF;
-       bmih.bV5GreenMask = 0xFF << 8;
-       bmih.bV5RedMask = 0xFF << 16;
-       bmih.bV5AlphaMask = 0xFF << 24;
-       bmih.bV5Width = width;
-       bmih.bV5Height = -height;
-       bmih.bV5Planes = 1;
-       bmih.bV5BitCount = 32;
-       bmih.bV5SizeImage = width * height * 4;
-       m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
-       if ( !m_hbmp ) {
+       uint32_t bitCount = 0;
+
+       if ( pixelFormat == k_GVOPixelFormat_RGB ) {
+               BITMAPINFOHEADER bmih = { sizeof(bmih) };
+               bmih.biWidth = width;
+               bmih.biHeight = -height;
+               bmih.biPlanes = 1;
+               bmih.biBitCount = 24;
+               bmih.biSizeImage = width * height * 3;
+               m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
+               bitCount = bmih.biBitCount;
+       }
+       else if ( pixelFormat == k_GVOPixelFormat_RGBA ) {
+               BITMAPV5HEADER bmih = { sizeof(bmih) };
+               bmih.bV5Compression = BI_BITFIELDS;
+               bmih.bV5BlueMask = 0xFF;
+               bmih.bV5GreenMask = 0xFF << 8;
+               bmih.bV5RedMask = 0xFF << 16;
+               bmih.bV5AlphaMask = 0xFF << 24;
+               bmih.bV5Width = width;
+               bmih.bV5Height = -height;
+               bmih.bV5Planes = 1;
+               bmih.bV5BitCount = 32;
+               bmih.bV5SizeImage = width * height * 4;
+               m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
+               bitCount = bmih.bV5BitCount;
+       }
+       else {
+               abort();
                return false;
        }
-       m_size.cx = width;
-       m_size.cy = height;
-       m_stride = width;
-       m_fileName = L"";
-       return true;
-}
-
-
-bool GVOImage::createDIBImage( int width, int height )
-{
-       reset();
-
-       BITMAPINFOHEADER bmih = { sizeof(bmih) };
-       bmih.biWidth = width;
-       bmih.biHeight = -height;
-       bmih.biPlanes = 1;
-       bmih.biBitCount = 24;
-       bmih.biSizeImage = width * height * 3;
-       m_hbmp = ::CreateDIBSection( NULL, (LPBITMAPINFO)&bmih, DIB_RGB_COLORS, (void **)&m_bits, NULL, 0 );
        if ( !m_hbmp ) {
                return false;
        }
        m_size.cx = width;
        m_size.cy = height;
-       m_stride = s_strideFromWidthAndBitsPerPixel( width, bmih.biBitCount );
-       m_fileName = L"";
+       m_pixelFormat = pixelFormat;
+       m_stride = s_strideFromWidthAndBitsPerPixel( width, bitCount );
        return true;
 }
 
@@ -134,13 +132,28 @@ bool GVOImage::loadFromFile( const std::wstring& fileName )
        HBITMAP hbmp = NULL;
        image.reset( Gdiplus::Bitmap::FromFile( fileName.c_str() ) );
        image->GetHBITMAP( Gdiplus::Color( 0, 0, 0 ), &hbmp );
+       Gdiplus::PixelFormat srcPixelFormat = image->GetPixelFormat();
        image.reset();
        if ( !hbmp ) {
                return false;
        }
+
+       GVOPixelFormat pixelFormat;
+
+       if ( srcPixelFormat & PixelFormatGDI ){
+               pixelFormat = k_GVOPixelFormat_RGB;
+       }
+       else if ( srcPixelFormat & (PixelFormatGDI | PixelFormatAlpha) ) {
+               pixelFormat = k_GVOPixelFormat_RGBA;
+       }
+       else {
+               abort();
+               return false;
+       }
+
        BITMAP bmp = { 0 };
        ::GetObject( hbmp, sizeof(bmp), &bmp );
-       if ( !createDIBImage( bmp.bmWidth, bmp.bmHeight ) ) {
+       if ( !createImage( bmp.bmWidth, bmp.bmHeight, pixelFormat ) ) {
                ::DeleteObject( hbmp );
                return false;
        }
@@ -150,18 +163,24 @@ bool GVOImage::loadFromFile( const std::wstring& fileName )
        ::GetBitmapBits( hbmp, buffer.size(), &buffer[0] );
        ::DeleteObject( hbmp );
 
-       switch ( bmp.bmBitsPixel ) {
-       case 24:
+       if ( pixelFormat == k_GVOPixelFormat_RGB ) {
+               switch ( bmp.bmBitsPixel ) {
+               case 24:
+                       ::memcpy( m_bits, &buffer[0], m_stride );
+                       break;
+               case 32:
+                       ::s_copyImage24From32( m_bits, &buffer[0], m_size.cx, m_size.cy );
+                       break;
+               default:
+                       return false;
+               }
+       }
+       else if ( pixelFormat == k_GVOPixelFormat_RGBA ) {
                ::memcpy( m_bits, &buffer[0], m_stride );
-               break;
-       case 32:
-               ::s_copyImage24From32( m_bits, &buffer[0], m_size.cx, m_size.cy );
-               break;
-       default:
+       }
+       else {
+               abort();
                return false;
        }
-
-       m_fileName = fileName;
-
        return true;
 }
index 5854e28..f922ba4 100644 (file)
@@ -7,11 +7,18 @@
 #include "GVONoncopyable.h"
 
 
+//!@brief \89æ\91\9c\82Ì\83s\83N\83Z\83\8b\83t\83H\81[\83}\83b\83g
+enum GVOPixelFormat {
+       k_GVOPixelFormat_Unknown,       //!<@brief \96¢\92è\8b`\92l
+       k_GVOPixelFormat_RGB,           //!<@brief R8G8B8
+       k_GVOPixelFormat_RGBA,          //!<@brief R8G8B8A8
+};
+
 class GVOImage :private GVONoncopyable {
 private:
-       std::wstring m_fileName;
        HBITMAP m_hbmp;
        SIZE m_size;
+       GVOPixelFormat m_pixelFormat;
        uint8_t * m_bits;
        uint32_t m_stride;
 
@@ -33,13 +40,13 @@ public:
                        ::DeleteObject( m_hbmp );
                        m_hbmp = NULL;
                }
-               m_fileName = L"";
                m_size = SIZE();
                m_stride = 0;
+               m_pixelFormat = k_GVOPixelFormat_Unknown;
        }
        void copy( const GVOImage & src )
        {
-               createDIBImage( src.m_size );
+               createImage( src.m_size, src.m_pixelFormat );
                ::memcpy( m_bits, src.m_bits, src.m_size.cy * src.m_stride );
        }
        bool stretchCopy( const GVOImage& src, const SIZE& size )
@@ -73,6 +80,10 @@ public:
        {
                return m_size.cy;
        }
+       GVOPixelFormat pixelFormat() const
+       {
+               return m_pixelFormat;
+       }
        uint32_t stride() const
        {
                return m_stride;
@@ -85,16 +96,11 @@ public:
        {
                return m_bits;
        }
-       const std::wstring& fileName() const
-       {
-               return m_fileName;
-       }
 
-       bool createRGBAImage( int width, int height );
-       bool createDIBImage( int width, int height );
-       bool createDIBImage( const SIZE& size )
+       bool createImage( int width, int height, GVOPixelFormat pixelFormat = k_GVOPixelFormat_RGB );
+       bool createImage( const SIZE& size, GVOPixelFormat pixelFormat = k_GVOPixelFormat_RGB )
        {
-               return createDIBImage( size.cx, size.cy );
+               return createImage( size.cx, size.cy, pixelFormat );
        }
        bool loadFromFile( const std::wstring& fileName );
 };
index 363dec5..a881194 100644 (file)
@@ -24,9 +24,6 @@ namespace {
                static const double k_knotFactor = (2 * M_PI * 6378.137) / 16384.0 / 0.4 / 1.852;
                return velocity * k_knotFactor;
        }
-
-       //GVOImage m_shipImage;
-       //GVOTexture m_shipImageTexture;
 }
 
 
@@ -36,15 +33,6 @@ void GVORenderer::setup( const GVOConfig * config, HDC hdcPrimary, const GVOWorl
        setConfig( config );
        setupGL();
        setWorldMap( worldMap );
-
-       //m_shipImage.createRGBAImage( 16, 16 );
-       //uint32_t * dst = reinterpret_cast<uint32_t *>(m_shipImage.mutableImageBits());
-       //for ( int y = 0; y < m_shipImage.height(); ++y ) {
-       //      for ( int x = 0; x < m_shipImage.width(); ++x ) {
-       //              *dst++ = RGB(255,0,0) | (y*15) << 24;
-       //      }
-       //}
-       //m_shipImageTexture.setImageWithAlpha( &m_shipImage );
 }
 
 
@@ -95,7 +83,7 @@ void GVORenderer::setWorldMap( const GVOWorldMap * worldMap )
 {
        m_worldMap = worldMap;
        m_worldMapTexture = new GVOTexture();
-       m_worldMapTexture->setImage( &worldMap->image() );
+       m_worldMapTexture->setImage( worldMap->image() );
 }
 
 
@@ -262,9 +250,7 @@ void GVORenderer::renderMap( const GVOVector& shipVector, GVOTexture * shipTextu
        ::glTranslatef( (float)xDrawOrigin, (float)yDrawOrigin, 0 );
        while ( drawn < m_viewSize.cx ) {
                // \92n\90}\82ð1\96\87\95`\89æ
-               m_worldMapTexture->bind();
-               renderTexture( (float)mapSize.cx, (float)mapSize.cy );
-               m_worldMapTexture->unbind();
+               renderTexture( *m_worldMapTexture, (float)mapSize.cx, (float)mapSize.cy );
 
                // \8fI\92\85\82µ\82Ä\82¢\82È\82¢\8dq\98H\82ð1\96\87\95ª\95`\89æ
                renderShipRouteList( mapSize.cx, mapSize.cy, shipRouteList );
@@ -330,9 +316,7 @@ void GVORenderer::renderMap( const GVOVector& shipVector, GVOTexture * shipTextu
                        ::glEnable( GL_BLEND );
                        ::glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
                        ::glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
-                       shipTexture->bind();
-                       renderTexture( x, y, shipMarkSize, shipMarkSize );
-                       shipTexture->unbind();
+                       renderTexture( *shipTexture, x, y, shipMarkSize, shipMarkSize );
                        ::glDisable( GL_BLEND );
 
                        xDrawOrigin += mapSize.cx;
@@ -384,7 +368,7 @@ void GVORenderer::renderShipRouteList( int width, int height, const GVOShipRoute
 
 void GVORenderer::renderSpeedMeter( double shipVelocity )
 {
-       // THE\8eè\94²\82«\95\8e\9a\83|\83\8a\83S\83\93\8dì\90¬
+       // THE\8eè\94²\82«\95\8e\9a\83e\83N\83X\83`\83\83\81[\8dì\90¬
 
        HDC hdcMem = ::CreateCompatibleDC( m_hdcPrimary );
        ::SaveDC( hdcMem );
@@ -399,30 +383,30 @@ void GVORenderer::renderSpeedMeter( double shipVelocity )
        const int stride = width + (4 - width % 4) % 4;
        const int height = rc.bottom - rc.top;
        GVOImage workImage;
-       workImage.createDIBImage( stride, height );
+       workImage.createImage( stride, height );
        ::SelectObject( hdcMem, workImage.bitmapHandle() );
        ::DrawText( hdcMem, buf, -1, &rc, DT_SINGLELINE | DT_RIGHT | DT_TOP );
        ::RestoreDC( hdcMem, -1 );
        ::DeleteDC( hdcMem );
 
        GVOTexture workTexture;
-       workTexture.setImage( &workImage );
+       workTexture.setImage( workImage );
 
        ::glMatrixMode( GL_MODELVIEW );
        ::glLoadIdentity();
-       workTexture.bind();
-       renderTexture( float( m_viewSize.cx - stride ), 0.0f, (float)stride, (float)height );
-       workTexture.unbind();
+       renderTexture( workTexture, float( m_viewSize.cx - stride ), 0.0f, (float)stride, (float)height );
 }
 
 
-void GVORenderer::renderTexture( float w, float h )
+void GVORenderer::renderTexture( GVOTexture & texture, float w, float h )
 {
-       renderTexture( 0, 0, w, h );
+       renderTexture( texture, 0, 0, w, h );
 }
 
-void GVORenderer::renderTexture( float x, float y, float w, float h )
+void GVORenderer::renderTexture( GVOTexture & texture, float x, float y, float w, float h )
 {
+       texture.bind();
+
        ::glBegin( GL_QUADS );
 
        ::glTexCoord2d( 0, 0 );
@@ -438,6 +422,8 @@ void GVORenderer::renderTexture( float x, float y, float w, float h )
        ::glVertex2d( x + w, y );
 
        ::glEnd();
+
+       texture.unbind();
 }
 
 
@@ -445,7 +431,7 @@ GVOTexture * GVORenderer::createTextureFromImage( const GVOImage & image )
 {
        ::wglMakeCurrent( m_hdcPrimary, m_hglrc );
        std::auto_ptr<GVOTexture> texture( new GVOTexture() );
-       texture->setImageWithAlpha( &image );
+       texture->setImageimage );
        ::wglMakeCurrent( NULL, NULL );
 
        return texture.release();
index beafa68..09b6ff8 100644 (file)
@@ -15,7 +15,6 @@ private:
 
        HDC m_hdcPrimary;
        HGLRC m_hglrc;
-       GVOImage m_backBuffer;
 
        SIZE m_viewSize;
        double m_viewScale;
@@ -91,8 +90,8 @@ private:
        void renderMap( const GVOVector& shipVector, GVOTexture * shipTexture, const GVOShipRouteList * shipRouteList );
        void renderShipRouteList(int width, int height, const GVOShipRouteList * shipRouteList );
        void renderSpeedMeter( double shipVelocity );
-       void renderTexture( float w, float h );
-       void renderTexture( float x, float y, float w, float h );
+       void renderTexture( GVOTexture & texture, float w, float h );
+       void renderTexture( GVOTexture & texture, float x, float y, float w, float h );
        inline POINT viewCenterPoint() const
        {
                POINT p = {
index 80a7d72..bdcb838 100644 (file)
@@ -23,39 +23,31 @@ GVOTexture::~GVOTexture()
 }
 
 
-void GVOTexture::setImage( const GVOImage * image )
+void GVOTexture::setImage( const GVOImage & image )
 {
-       if ( image ) {
-               bind();
+       bind();
 
+       if ( image.pixelFormat() == k_GVOPixelFormat_RGB ) {
                ::glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
                ::glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
-                       image->width(), image->height(),
+                       image.width(), image.height(),
                        0, GL_BGR_EXT,
-                       GL_UNSIGNED_BYTE, image->imageBits() );
-               m_width = image->width();
-               m_height = image->height();
-
-               unbind();
+                       GL_UNSIGNED_BYTE, image.imageBits() );
        }
-}
-
-
-void GVOTexture::setImageWithAlpha( const GVOImage * image )
-{
-       if ( image ) {
-               bind();
-
+       else if ( image.pixelFormat() == k_GVOPixelFormat_RGBA ) {
                ::glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );
                ::glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
-                       image->width(), image->height(),
+                       image.width(), image.height(),
                        0, GL_BGRA_EXT,
-                       GL_UNSIGNED_BYTE, image->imageBits() );
-               m_width = image->width();
-               m_height = image->height();
-
-               unbind();
+                       GL_UNSIGNED_BYTE, image.imageBits() );
        }
+       else {
+               abort();
+       }
+       m_width = image.width();
+       m_height = image.height();
+
+       unbind();
 }
 
 
index 96f17ac..868f3d4 100644 (file)
@@ -1,8 +1,9 @@
 #pragma once
 #include "GVONoncopyable.h"
+#include "GVOImage.h"
+
 
 
-class GVOImage;
 
 //!@brief OpenGL\83e\83N\83X\83`\83\83\83N\83\89\83X
 class GVOTexture : private GVONoncopyable {
@@ -25,8 +26,9 @@ public:
                return m_height;
        }
 
-       void setImage( const GVOImage * image );
-       void setImageWithAlpha( const GVOImage * image );
+       void setImage( const GVOImage & image );
+
        void bind();
+
        void unbind();
 };