Commit 2fd7699d authored by chili's avatar chili
Browse files

GDI+ Surface image load test

parent dfb87d50
......@@ -5,59 +5,64 @@
#include <memory>
#include <algorithm>
#include "ChiliMath.h"
#include "Surface.h"
#include "GDIPlusManager.h"
GDIPlusManager gdipm;
App::App()
:
wnd( 800,600,"The Donkey Fart Box" )
{
//class Factory
//{
//public:
// Factory( Graphics& gfx )
// :
// gfx( gfx )
// {}
// std::unique_ptr<Drawable> operator()()
// {
// switch( typedist( rng ) )
// {
// case 0:
// return std::make_unique<Pyramid>(
// gfx,rng,adist,ddist,
// odist,rdist
// );
// case 1:
// return std::make_unique<Box>(
// gfx,rng,adist,ddist,
// odist,rdist,bdist
// );
// case 2:
// return std::make_unique<Melon>(
// gfx,rng,adist,ddist,
// odist,rdist,longdist,latdist
// );
// default:
// assert( false && "bad drawable type in factory" );
// return {};
// }
// }
//private:
// Graphics& gfx;
// std::mt19937 rng{ std::random_device{}() };
// std::uniform_real_distribution<float> adist{ 0.0f,PI * 2.0f };
// std::uniform_real_distribution<float> ddist{ 0.0f,PI * 0.5f };
// std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f };
// std::uniform_real_distribution<float> rdist{ 6.0f,20.0f };
// std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
// std::uniform_int_distribution<int> latdist{ 5,20 };
// std::uniform_int_distribution<int> longdist{ 10,40 };
// std::uniform_int_distribution<int> typedist{ 0,2 };
//};
class Factory
{
public:
Factory( Graphics& gfx )
:
gfx( gfx )
{}
std::unique_ptr<Drawable> operator()()
{
switch( typedist( rng ) )
{
case 0:
return std::make_unique<Pyramid>(
gfx,rng,adist,ddist,
odist,rdist
);
case 1:
return std::make_unique<Box>(
gfx,rng,adist,ddist,
odist,rdist,bdist
);
case 2:
return std::make_unique<Melon>(
gfx,rng,adist,ddist,
odist,rdist,longdist,latdist
);
default:
assert( false && "bad drawable type in factory" );
return {};
}
}
private:
Graphics& gfx;
std::mt19937 rng{ std::random_device{}() };
std::uniform_real_distribution<float> adist{ 0.0f,PI * 2.0f };
std::uniform_real_distribution<float> ddist{ 0.0f,PI * 0.5f };
std::uniform_real_distribution<float> odist{ 0.0f,PI * 0.08f };
std::uniform_real_distribution<float> rdist{ 6.0f,20.0f };
std::uniform_real_distribution<float> bdist{ 0.4f,3.0f };
std::uniform_int_distribution<int> latdist{ 5,20 };
std::uniform_int_distribution<int> longdist{ 10,40 };
std::uniform_int_distribution<int> typedist{ 0,2 };
};
Factory f( wnd.Gfx() );
drawables.reserve( nDrawables );
std::generate_n( std::back_inserter( drawables ),nDrawables,f );
//Factory f( wnd.Gfx() );
//drawables.reserve( nDrawables );
//std::generate_n( std::back_inserter( drawables ),nDrawables,f );
const auto s = Surface::FromFile( "Images\\kappa50.png" );
wnd.Gfx().SetProjection( DirectX::XMMatrixPerspectiveLH( 1.0f,3.0f / 4.0f,0.5f,40.0f ) );
}
......
......@@ -25,6 +25,7 @@
// The following #defines disable a bunch of unused windows stuff. If you
// get weird errors when trying to do some windows stuff, try removing some
// (or all) of these defines (it will increase build time though).
#ifndef FULL_WINTARD
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS
#define NOSYSMETRICS
......@@ -42,7 +43,6 @@
#define NONLS
#define NOMEMMGR
#define NOMETAFILE
#define NOMINMAX
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
......@@ -59,6 +59,9 @@
#define NOPROXYSTUB
#define NOIMAGE
#define NOTAPE
#endif
#define NOMINMAX
#define STRICT
......
/******************************************************************************************
* Chili DirectX Framework Version 16.10.01 *
* GDIPlusManager.cpp *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#define FULL_WINTARD
#include "ChiliWin.h"
#include "GDIPlusManager.h"
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
}
#include <gdiplus.h>
ULONG_PTR GDIPlusManager::token = 0;
int GDIPlusManager::refCount = 0;
GDIPlusManager::GDIPlusManager()
{
if( refCount++ == 0 )
{
Gdiplus::GdiplusStartupInput input;
input.GdiplusVersion = 1;
input.DebugEventCallback = nullptr;
input.SuppressBackgroundThread = false;
Gdiplus::GdiplusStartup( &token,&input,nullptr );
}
}
GDIPlusManager::~GDIPlusManager()
{
if( --refCount == 0 )
{
Gdiplus::GdiplusShutdown( token );
}
}
/******************************************************************************************
* Chili DirectX Framework Version 16.10.01 *
* GDIPlusManager.h *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include "ChiliWin.h"
class GDIPlusManager
{
public:
GDIPlusManager();
~GDIPlusManager();
private:
static ULONG_PTR token;
static int refCount;
};
\ No newline at end of file
/******************************************************************************************
* Chili DirectX Framework Version 16.10.01 *
* Surface.cpp *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#define FULL_WINTARD
#include "Surface.h"
#include <algorithm>
namespace Gdiplus
{
using std::min;
using std::max;
}
#include <gdiplus.h>
#include <sstream>
#pragma comment( lib,"gdiplus.lib" )
Surface::Surface( unsigned int width,unsigned int height,unsigned int pitch ) noexcept
:
pBuffer( std::make_unique<Color[]>( pitch * height ) ),
width( width ),
height( height )
{}
Surface& Surface::operator=( Surface&& donor ) noexcept
{
width = donor.width;
height = donor.height;
pBuffer = std::move( donor.pBuffer );
donor.pBuffer = nullptr;
return *this;
}
Surface::Surface( unsigned int width,unsigned int height ) noexcept
:
Surface( width,height,width )
{}
Surface::Surface( Surface && source ) noexcept
:
pBuffer( std::move( source.pBuffer ) ),
width( source.width ),
height( source.height )
{}
Surface::~Surface()
{}
void Surface::Clear( Color fillValue ) noexcept
{
memset( pBuffer.get(),fillValue.dword,width * height * sizeof( Color ) );
}
void Surface::PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEBUG)
{
assert( x >= 0 );
assert( y >= 0 );
assert( x < width );
assert( y < height );
pBuffer[y * width + x] = c;
}
Surface::Color Surface::GetPixel( unsigned int x,unsigned int y ) const noexcept(!IS_DEBUG)
{
assert( x >= 0 );
assert( y >= 0 );
assert( x < width );
assert( y < height );
return pBuffer[y * width + x];
}
unsigned int Surface::GetWidth() const noexcept
{
return width;
}
unsigned int Surface::GetHeight() const noexcept
{
return height;
}
Surface::Color* Surface::GetBufferPtr() noexcept
{
return pBuffer.get();
}
const Surface::Color* Surface::GetBufferPtr() const noexcept
{
return pBuffer.get();
}
const Surface::Color* Surface::GetBufferPtrConst() const noexcept
{
return pBuffer.get();
}
Surface Surface::FromFile( const std::string& name )
{
unsigned int width = 0;
unsigned int height = 0;
unsigned int pitch = 0;
std::unique_ptr<Color[]> pBuffer = nullptr;
{
// convert filenam to wide string (for Gdiplus)
wchar_t wideName[512];
mbstowcs_s( nullptr,wideName,name.c_str(),_TRUNCATE );
Gdiplus::Bitmap bitmap( wideName );
if( bitmap.GetLastStatus() != Gdiplus::Status::Ok )
{
std::stringstream ss;
ss << "Loading image [" << name << "]: failed to load.";
throw Exception( __LINE__,__FILE__ ,ss.str() );
}
height = bitmap.GetHeight();
pBuffer = std::make_unique<Color[]>( width * height );
for( unsigned int y = 0; y < height; y++ )
{
for( unsigned int x = 0; x < width; x++ )
{
Gdiplus::Color c;
bitmap.GetPixel( x,y,&c );
pBuffer[y * pitch + x] = c.GetValue();
}
}
}
return Surface( width,height,std::move( pBuffer ) );
}
void Surface::Save( const std::string& filename ) const
{
auto GetEncoderClsid = [&filename]( const WCHAR* format,CLSID* pClsid ) -> void
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
Gdiplus::ImageCodecInfo* pImageCodecInfo = nullptr;
Gdiplus::GetImageEncodersSize( &num,&size );
if( size == 0 )
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to get encoder; size == 0.";
throw Exception( __LINE__,__FILE__ ,ss.str() );
}
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc( size ));
if( pImageCodecInfo == nullptr )
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to get encoder; failed to allocate memory.";
throw Exception( __LINE__,__FILE__,ss.str() );
}
GetImageEncoders( num,size,pImageCodecInfo );
for( UINT j = 0; j < num; ++j )
{
if( wcscmp( pImageCodecInfo[j].MimeType,format ) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free( pImageCodecInfo );
return;
}
}
free( pImageCodecInfo );
std::stringstream ss;
ss << "Saving surface to [" << filename <<
"]: failed to get encoder; failed to find matching encoder.";
throw Exception( __LINE__,__FILE__,ss.str() );
};
CLSID bmpID;
GetEncoderClsid( L"image/bmp",&bmpID );
// convert filenam to wide string (for Gdiplus)
wchar_t wideName[512];
mbstowcs_s( nullptr,wideName,filename.c_str(),_TRUNCATE );
Gdiplus::Bitmap bitmap( width,height,width * sizeof( Color ),PixelFormat32bppARGB,(BYTE*)pBuffer.get() );
if( bitmap.Save( wideName,&bmpID,nullptr ) != Gdiplus::Status::Ok )
{
std::stringstream ss;
ss << "Saving surface to [" << filename << "]: failed to save.";
throw Exception( __LINE__,__FILE__,ss.str() );
}
}
void Surface::Copy( const Surface & src ) noexcept(!IS_DEBUG)
{
assert( width == src.width );
assert( height == src.height );
memcpy( pBuffer.get(),src.pBuffer.get(),width * height * sizeof( Color ) );
}
Surface::Surface( unsigned int width,unsigned int height,std::unique_ptr<Color[]> pBufferParam ) noexcept
:
width( width ),
height( height ),
pBuffer( std::move( pBufferParam ) )
{}
// surface exception stuff
Surface::Exception::Exception( int line,const char* file,std::string note ) noexcept
:
ChiliException( line,file ),
note( std::move( note ) )
{}
const char* Surface::Exception::what() const noexcept
{
std::ostringstream oss;
oss << ChiliException::what() << std::endl
<< "[Note] " << GetNote();
whatBuffer = oss.str();
return whatBuffer.c_str();
}
const char* Surface::Exception::GetType() const noexcept
{
return "Chili Graphics Exception";
}
const std::string& Surface::Exception::GetNote() const noexcept
{
return note;
}
/******************************************************************************************
* Chili DirectX Framework Version 16.10.01 *
* Surface.h *
* Copyright 2016 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili DirectX Framework is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili DirectX Framework. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include "ChiliWin.h"
#include "ChiliException.h"
#include <string>
#include <assert.h>
#include <memory>
class Surface
{
public:
class Color
{
public:
unsigned int dword;
public:
constexpr Color() noexcept : dword()
{}
constexpr Color( const Color& col ) noexcept
:
dword( col.dword )
{}
constexpr Color( unsigned int dw ) noexcept
:
dword( dw )
{}
constexpr Color( unsigned char x,unsigned char r,unsigned char g,unsigned char b ) noexcept
:
dword( (x << 24u) | (r << 16u) | (g << 8u) | b )
{}
constexpr Color( unsigned char r,unsigned char g,unsigned char b ) noexcept
:
dword( (r << 16u) | (g << 8u) | b )
{}
constexpr Color( Color col,unsigned char x ) noexcept
:
Color( (x << 24u) | col.dword )
{}
Color& operator =( Color color ) noexcept
{
dword = color.dword;
return *this;
}
constexpr unsigned char GetX() const noexcept
{
return dword >> 24u;
}
constexpr unsigned char GetA() const noexcept
{
return GetX();
}
constexpr unsigned char GetR() const noexcept
{
return (dword >> 16u) & 0xFFu;
}
constexpr unsigned char GetG() const noexcept
{
return (dword >> 8u) & 0xFFu;
}
constexpr unsigned char GetB() const noexcept
{
return dword & 0xFFu;
}
void SetX( unsigned char x ) noexcept
{
dword = (dword & 0xFFFFFFu) | (x << 24u);
}
void SetA( unsigned char a ) noexcept
{
SetX( a );
}
void SetR( unsigned char r ) noexcept
{
dword = (dword & 0xFF00FFFFu) | (r << 16u);
}
void SetG( unsigned char g ) noexcept
{
dword = (dword & 0xFFFF00FFu) | (g << 8u);
}
void SetB( unsigned char b ) noexcept
{
dword = (dword & 0xFFFFFF00u) | b;
}
};
public:
class Exception : public ChiliException
{
public:
Exception( int line,const char* file,std::string note ) noexcept;
const char* what() const noexcept override;
const char* GetType() const noexcept override;
const std::string& GetNote() const noexcept;
private:
std::string note;
};
public:
Surface( unsigned int width,unsigned int height,unsigned int pitch ) noexcept;
Surface( unsigned int width,unsigned int height ) noexcept;
Surface( Surface&& source ) noexcept;
Surface( Surface& ) = delete;
Surface& operator=( Surface&& donor ) noexcept;
Surface& operator=( const Surface& ) = delete;
~Surface();
void Clear( Color fillValue ) noexcept;
void PutPixel( unsigned int x,unsigned int y,Color c ) noexcept(!IS_DEBUG);
Color GetPixel( unsigned int x,unsigned int y ) const noexcept(!IS_DEBUG);
unsigned int GetWidth() const noexcept;
unsigned int GetHeight() const noexcept;
Color* GetBufferPtr() noexcept;
const Color* GetBufferPtr() const noexcept;
const Color* GetBufferPtrConst() const noexcept;
static Surface FromFile( const std::string& name );
void Save( const std::string& filename ) const;
void Copy( const Surface& src ) noexcept(!IS_DEBUG);
private:
Surface( unsigned int width,unsigned int height,std::unique_ptr<Color[]> pBufferParam ) noexcept;
private:
std::unique_ptr<Color[]> pBuffer;
unsigned int width;
unsigned int height;
};
\ No newline at end of file
......@@ -155,6 +155,7 @@
<ClCompile Include="Drawable.cpp" />
<ClCompile Include="dxerr.cpp" />
<ClCompile Include="DxgiInfoManager.cpp" />
<ClCompile Include="GDIPlusManager.cpp" />
<ClCompile Include="Graphics.cpp" />
<ClCompile Include="IndexBuffer.cpp" />
<ClCompile Include="InputLayout.cpp" />
......@@ -163,6 +164,7 @@
<ClCompile Include="Mouse.cpp" />
<ClCompile Include="PixelShader.cpp" />
<ClCompile Include="Pyramid.cpp" />
<ClCompile Include="Surface.cpp" />
<ClCompile Include="Topology.cpp" />
<ClCompile Include="TransformCbuf.cpp" />
<ClCompile Include="VertexBuffer.cpp" />
......@@ -187,6 +189,7 @@
<ClInclude Include="DrawableBase.h" />
<ClInclude Include="dxerr.h" />
<ClInclude Include="DxgiInfoManager.h" />
<ClInclude Include="GDIPlusManager.h" />
<ClInclude Include="Graphics.h" />
<ClInclude Include="GraphicsThrowMacros.h" />
<ClInclude Include="IndexBuffer.h" />
......@@ -201,6 +204,7 @@
<ClInclude Include="Pyramid.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="Sphere.h" />
<ClInclude Include="Surface.h" />
<ClInclude Include="Topology.h" />
<ClInclude Include="TransformCbuf.h" />
<ClInclude Include="VertexBuffer.h" />
......
......@@ -108,6 +108,12 @@
<ClCompile Include="Melon.cpp">
<Filter>Source Files\Drawable</Filter>
</ClCompile>
<ClCompile Include="Surface.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="GDIPlusManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WindowsMessageMap.h">
......@@ -218,6 +224,12 @@
<ClInclude Include="Pyramid.h">
<Filter>Header Files\Drawable</Filter>
</ClInclude>
<ClInclude Include="Surface.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GDIPlusManager.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="hw3d.rc">
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment