add graphics options

This commit is contained in:
Haibo 2018-11-21 16:26:52 +08:00 committed by Nomango
parent bb03e6df5f
commit 0088bd88a3
10 changed files with 103 additions and 56 deletions

View File

@ -20,7 +20,6 @@
#include "Game.h" #include "Game.h"
#include "logs.h" #include "logs.h"
#include "render.h"
#include "input.h" #include "input.h"
#include "audio.h" #include "audio.h"
#include "modules.h" #include "modules.h"
@ -35,8 +34,7 @@
namespace easy2d namespace easy2d
{ {
Game::Game() Game::Game()
: quit_(true) : curr_scene_(nullptr)
, curr_scene_(nullptr)
, next_scene_(nullptr) , next_scene_(nullptr)
, transition_(nullptr) , transition_(nullptr)
, debug_enabled_(false) , debug_enabled_(false)
@ -79,7 +77,7 @@ namespace easy2d
::SetWindowLongW(hwnd_, GWLP_USERDATA, PtrToUlong(this)); ::SetWindowLongW(hwnd_, GWLP_USERDATA, PtrToUlong(this));
devices::Graphics::Instance()->Init(hwnd_, debug_enabled_); devices::Graphics::Instance()->Init(hwnd_, options.graphics_options, debug_enabled_);
devices::Input::Instance()->Init(hwnd_, window->GetContentScaleX(), window->GetContentScaleY(), debug_enabled_); devices::Input::Instance()->Init(hwnd_, window->GetContentScaleX(), window->GetContentScaleY(), debug_enabled_);
devices::Audio::Instance()->Init(debug_enabled_); devices::Audio::Instance()->Init(debug_enabled_);
@ -121,8 +119,6 @@ namespace easy2d
void Game::Run() void Game::Run()
{ {
quit_ = false;
if (next_scene_) if (next_scene_)
{ {
next_scene_->OnEnter(); next_scene_->OnEnter();
@ -134,7 +130,7 @@ namespace easy2d
::UpdateWindow(hwnd_); ::UpdateWindow(hwnd_);
MSG msg = {}; MSG msg = {};
while (::GetMessageW(&msg, nullptr, 0, 0) && !quit_) while (::GetMessageW(&msg, nullptr, 0, 0))
{ {
::TranslateMessage(&msg); ::TranslateMessage(&msg);
::DispatchMessageW(&msg); ::DispatchMessageW(&msg);
@ -143,7 +139,7 @@ namespace easy2d
void Game::Quit() void Game::Quit()
{ {
quit_ = true; ::DestroyWindow(hwnd_);
} }
bool Game::EnterScene(spScene const & scene) bool Game::EnterScene(spScene const & scene)
@ -367,7 +363,7 @@ namespace easy2d
{ {
if (game->OnClose()) if (game->OnClose())
{ {
game->Quit(); ::DestroyWindow(hwnd);
} }
} }
result = 0; result = 0;
@ -376,6 +372,7 @@ namespace easy2d
case WM_DESTROY: case WM_DESTROY:
{ {
game->OnExit();
::PostQuitMessage(0); ::PostQuitMessage(0);
} }
result = 1; result = 1;

View File

@ -22,6 +22,7 @@
#include "base.hpp" #include "base.hpp"
#include "window.h" #include "window.h"
#include "time.h" #include "time.h"
#include "render.h"
#include "KeyEvent.h" #include "KeyEvent.h"
#include "MouseEvent.h" #include "MouseEvent.h"
@ -29,11 +30,13 @@ namespace easy2d
{ {
struct Options struct Options
{ {
String title; /* 标题 */ String title; // 标题
int width; /* 宽度 */ int width; // 宽度
int height; /* 高度 */ int height; // 高度
LPCWSTR icon; /* 图标 */ LPCWSTR icon; // 图标
bool debug; /* 调试模式 */ bool debug; // 调试模式
GraphicsOptions graphics_options; // 图形渲染选项
Options() Options()
: title(L"Easy2D Game") : title(L"Easy2D Game")
@ -41,6 +44,7 @@ namespace easy2d
, height(480) , height(480)
, icon(nullptr) , icon(nullptr)
, debug(false) , debug(false)
, graphics_options()
{} {}
}; };
@ -107,7 +111,6 @@ namespace easy2d
private: private:
bool initialized_; bool initialized_;
bool debug_enabled_; bool debug_enabled_;
bool quit_;
bool window_inactived_; bool window_inactived_;
HWND hwnd_; HWND hwnd_;
spScene curr_scene_; spScene curr_scene_;

View File

@ -27,7 +27,7 @@ namespace easy2d
namespace devices namespace devices
{ {
InputDevice::InputDevice() InputDevice::InputDevice()
: initialized(false) : initialized_(false)
, hwnd_(nullptr) , hwnd_(nullptr)
, scale_x_(1.f) , scale_x_(1.f)
, scale_y_(1.f) , scale_y_(1.f)
@ -43,7 +43,7 @@ namespace easy2d
void InputDevice::Init(HWND hwnd, float scale_x, float scale_y, bool debug) void InputDevice::Init(HWND hwnd, float scale_x, float scale_y, bool debug)
{ {
if (initialized) if (initialized_)
return; return;
E2D_LOG("Initing input device"); E2D_LOG("Initing input device");
@ -52,7 +52,7 @@ namespace easy2d
scale_x_ = scale_x; scale_x_ = scale_x;
scale_y_ = scale_y; scale_y_ = scale_y;
initialized = true; initialized_ = true;
} }
void InputDevice::Update() void InputDevice::Update()

View File

@ -71,7 +71,7 @@ namespace easy2d
~InputDevice(); ~InputDevice();
protected: protected:
bool initialized; bool initialized_;
HWND hwnd_; HWND hwnd_;
float scale_x_; float scale_x_;
float scale_y_; float scale_y_;

View File

@ -164,7 +164,7 @@ namespace easy2d
AudioDevice::AudioDevice() AudioDevice::AudioDevice()
: x_audio2_(nullptr) : x_audio2_(nullptr)
, mastering_voice_(nullptr) , mastering_voice_(nullptr)
, initialized(false) , initialized_(false)
{ {
} }
@ -187,7 +187,7 @@ namespace easy2d
void AudioDevice::Init(bool debug) void AudioDevice::Init(bool debug)
{ {
if (initialized) if (initialized_)
return; return;
E2D_LOG("Initing audio device"); E2D_LOG("Initing audio device");
@ -204,7 +204,7 @@ namespace easy2d
x_audio2_->CreateMasteringVoice(&mastering_voice_) x_audio2_->CreateMasteringVoice(&mastering_voice_)
); );
initialized = true; initialized_ = true;
} }
HRESULT AudioDevice::CreateVoice(Voice& voice, const WAVEFORMATEX* wfx) HRESULT AudioDevice::CreateVoice(Voice& voice, const WAVEFORMATEX* wfx)

View File

@ -105,7 +105,7 @@ namespace easy2d
~AudioDevice(); ~AudioDevice();
protected: protected:
bool initialized; bool initialized_;
IXAudio2* x_audio2_; IXAudio2* x_audio2_;
IXAudio2MasteringVoice* mastering_voice_; IXAudio2MasteringVoice* mastering_voice_;
std::set<Voice*> voice_cache_; std::set<Voice*> voice_cache_;

View File

@ -35,8 +35,9 @@ namespace easy2d
, fps_text_layout_(nullptr) , fps_text_layout_(nullptr)
, clear_color_(D2D1::ColorF(D2D1::ColorF::Black)) , clear_color_(D2D1::ColorF(D2D1::ColorF::Black))
, opacity_(1.f) , opacity_(1.f)
, window_occluded(false) , window_occluded_(false)
, initialized(false) , initialized_(false)
, options_()
{ {
ZeroMemory(&d2d, sizeof(D2DResources)); ZeroMemory(&d2d, sizeof(D2DResources));
} }
@ -48,19 +49,22 @@ namespace easy2d
ClearImageCache(); ClearImageCache();
} }
void GraphicsDevice::Init(HWND hwnd, bool debug) void GraphicsDevice::Init(HWND hwnd, GraphicsOptions options, bool debug)
{ {
if (initialized) if (initialized_)
return; return;
E2D_LOG("Initing graphics device"); E2D_LOG("Initing graphics device");
D2D1_FACTORY_OPTIONS options{ debug ? D2D1_DEBUG_LEVEL_INFORMATION : D2D1_DEBUG_LEVEL_NONE }; options_ = options;
D2D1_FACTORY_OPTIONS fact_options;
fact_options.debugLevel = debug ? D2D1_DEBUG_LEVEL_INFORMATION : D2D1_DEBUG_LEVEL_NONE;
ThrowIfFailed( ThrowIfFailed(
modules::DirectX().D2D1CreateFactory( modules::DirectX().D2D1CreateFactory(
D2D1_FACTORY_TYPE_SINGLE_THREADED, D2D1_FACTORY_TYPE_SINGLE_THREADED,
__uuidof(ID2D1Factory), __uuidof(ID2D1Factory),
&options, &fact_options,
reinterpret_cast<void**>(&d2d.factory) reinterpret_cast<void**>(&d2d.factory)
) )
); );
@ -124,16 +128,16 @@ namespace easy2d
CreateDeviceResources(hwnd); CreateDeviceResources(hwnd);
initialized = true; initialized_ = true;
} }
void GraphicsDevice::BeginDraw(HWND hwnd) void GraphicsDevice::BeginDraw(HWND hwnd)
{ {
CreateDeviceResources(hwnd); CreateDeviceResources(hwnd);
window_occluded = !!(d2d.render_target->CheckWindowState() & D2D1_WINDOW_STATE_OCCLUDED); window_occluded_ = !!(d2d.render_target->CheckWindowState() & D2D1_WINDOW_STATE_OCCLUDED);
if (!window_occluded) if (!window_occluded_)
{ {
d2d.render_target->BeginDraw(); d2d.render_target->BeginDraw();
d2d.render_target->Clear(clear_color_); d2d.render_target->Clear(clear_color_);
@ -142,7 +146,7 @@ namespace easy2d
void GraphicsDevice::EndDraw() void GraphicsDevice::EndDraw()
{ {
if (!window_occluded) if (!window_occluded_)
{ {
HRESULT hr = d2d.render_target->EndDraw(); HRESULT hr = d2d.render_target->EndDraw();
@ -425,7 +429,7 @@ namespace easy2d
!d2d.render_target) !d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.solid_brush->SetColor(stroke_color); d2d.solid_brush->SetColor(stroke_color);
@ -444,7 +448,7 @@ namespace easy2d
!d2d.render_target) !d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.solid_brush->SetColor(fill_color); d2d.solid_brush->SetColor(fill_color);
@ -463,7 +467,7 @@ namespace easy2d
if (!image->GetBitmap()) if (!image->GetBitmap())
return S_OK; return S_OK;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.render_target->DrawBitmap( d2d.render_target->DrawBitmap(
@ -500,7 +504,7 @@ namespace easy2d
if (!d2d.render_target) if (!d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
// Do not crop bitmap // Do not crop bitmap
@ -520,7 +524,7 @@ namespace easy2d
if (!d2d.text_renderer) if (!d2d.text_renderer)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
return text_layout->Draw(nullptr, d2d.text_renderer.Get(), 0, 0); return text_layout->Draw(nullptr, d2d.text_renderer.Get(), 0, 0);
@ -531,7 +535,7 @@ namespace easy2d
if (!d2d.render_target) if (!d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.render_target->SetTransform(ConvertToD2DMatrix(clip_matrix)); d2d.render_target->SetTransform(ConvertToD2DMatrix(clip_matrix));
@ -547,7 +551,7 @@ namespace easy2d
if (!d2d.render_target) if (!d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.render_target->PopAxisAlignedClip(); d2d.render_target->PopAxisAlignedClip();
@ -560,7 +564,7 @@ namespace easy2d
!d2d.solid_brush) !d2d.solid_brush)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.render_target->PushLayer( d2d.render_target->PushLayer(
@ -583,7 +587,7 @@ namespace easy2d
if (!d2d.render_target) if (!d2d.render_target)
return E_UNEXPECTED; return E_UNEXPECTED;
if (window_occluded) if (window_occluded_)
return S_OK; return S_OK;
d2d.render_target->PopLayer(); d2d.render_target->PopLayer();
@ -863,10 +867,35 @@ namespace easy2d
D2D1::HwndRenderTargetProperties( D2D1::HwndRenderTargetProperties(
hwnd, hwnd,
size, size,
D2D1_PRESENT_OPTIONS_NONE), options_.vsync ? D2D1_PRESENT_OPTIONS_NONE : D2D1_PRESENT_OPTIONS_IMMEDIATELY
),
&d2d.render_target &d2d.render_target
) )
); );
d2d.render_target->SetAntialiasMode(
options_.antialias ? D2D1_ANTIALIAS_MODE_PER_PRIMITIVE : D2D1_ANTIALIAS_MODE_ALIASED
);
D2D1_TEXT_ANTIALIAS_MODE mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
switch (options_.text_antialias)
{
case TextAntialias::Default:
mode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
break;
case TextAntialias::ClearType:
mode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
break;
case TextAntialias::GrayScale:
mode = D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
break;
case TextAntialias::None:
mode = D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
break;
default:
break;
}
d2d.render_target->SetTextAntialiasMode(mode);
} }
if (!d2d.solid_brush) if (!d2d.solid_brush)

View File

@ -29,7 +29,27 @@
namespace easy2d namespace easy2d
{ {
class Image; enum class TextAntialias
{
Default, // 系统默认
ClearType, // ClearType 抗锯齿
GrayScale, // 灰度抗锯齿
None // 不启用抗锯齿
};
// 图形渲染选项
struct GraphicsOptions
{
bool vsync; // 垂直同步
bool antialias; // 抗锯齿
TextAntialias text_antialias; // 文字抗锯齿模式
GraphicsOptions()
: vsync(true)
, antialias(true)
, text_antialias(TextAntialias::ClearType)
{}
};
namespace devices namespace devices
{ {
@ -40,7 +60,7 @@ namespace easy2d
cpWriteFactory write_factory; cpWriteFactory write_factory;
cpTextRenderer text_renderer; cpTextRenderer text_renderer;
cpSolidColorBrush solid_brush; cpSolidColorBrush solid_brush;
cpHwndRenderTarget render_target; cpHwndRenderTarget render_target;
cpStrokeStyle miter_stroke_style; cpStrokeStyle miter_stroke_style;
cpStrokeStyle bevel_stroke_style; cpStrokeStyle bevel_stroke_style;
cpStrokeStyle round_stroke_style; cpStrokeStyle round_stroke_style;
@ -53,7 +73,7 @@ namespace easy2d
E2D_DECLARE_SINGLETON(GraphicsDevice); E2D_DECLARE_SINGLETON(GraphicsDevice);
public: public:
void Init(HWND hwnd, bool debug); void Init(HWND hwnd, GraphicsOptions options, bool debug);
// ¿ªÊ¼äÖȾ // ¿ªÊ¼äÖȾ
void BeginDraw(HWND hwnd); void BeginDraw(HWND hwnd);
@ -214,9 +234,10 @@ namespace easy2d
~GraphicsDevice(); ~GraphicsDevice();
protected: protected:
bool initialized; bool initialized_;
bool window_occluded; bool window_occluded_;
float opacity_; float opacity_;
GraphicsOptions options_;
D2DResources d2d; D2DResources d2d;
D2D1_COLOR_F clear_color_; D2D1_COLOR_F clear_color_;
cpTextFormat fps_text_format_; cpTextFormat fps_text_format_;

View File

@ -39,24 +39,21 @@ namespace easy2d
: handle(nullptr) : handle(nullptr)
, scale_x(1.f) , scale_x(1.f)
, scale_y(1.f) , scale_y(1.f)
, initialized(false) , initialized_(false)
{ {
} }
WindowImpl::~WindowImpl() WindowImpl::~WindowImpl()
{ {
E2D_LOG("Destroying window"); E2D_LOG("Destroying window");
if (handle)
::DestroyWindow(handle);
} }
void WindowImpl::Init(String title, int width, int height, LPCWSTR icon, WNDPROC proc, bool debug) void WindowImpl::Init(String title, int width, int height, LPCWSTR icon, WNDPROC proc, bool debug)
{ {
if (initialized) if (initialized_)
return; return;
E2D_LOG("Initing window"); E2D_LOG("Creating window");
HINSTANCE hinstance = GetModuleHandle(nullptr); HINSTANCE hinstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
@ -116,7 +113,7 @@ namespace easy2d
throw std::runtime_error(err); throw std::runtime_error(err);
} }
initialized = true; initialized_ = true;
} }
String WindowImpl::GetTitle() const String WindowImpl::GetTitle() const

View File

@ -72,7 +72,7 @@ namespace easy2d
~WindowImpl(); ~WindowImpl();
private: private:
bool initialized; bool initialized_;
HWND handle; HWND handle;
float scale_x; float scale_x;
float scale_y; float scale_y;