// Copyright (c) 2016-2018 Easy2D - Nomango // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #pragma once #include "e2dimpl.h" #include "e2dobject.h" #include "e2dtransition.h" namespace easy2d { // 图形设备 class Graphics { public: Graphics( HWND hwnd ); ~Graphics(); // 开始渲染 void BeginDraw(); // 结束渲染 void EndDraw(); // 渲染调试信息 void DrawDebugInfo(); // 获取 ID2D1Factory 对象 ID2D1Factory * GetFactory() const; // 获取 IWICImagingFactory 对象 IWICImagingFactory * GetImagingFactory() const; // 获取 IDWriteFactory 对象 IDWriteFactory * GetWriteFactory() const; // 获取 ID2D1HwndRenderTarget 对象 ID2D1HwndRenderTarget * GetRenderTarget() const; // 获取 ID2D1SolidColorBrush 对象 ID2D1SolidColorBrush * GetSolidBrush() const; // 获取文字渲染工具 E2DTextRenderer * GetTextRender() const; // 获取 Miter 样式的 ID2D1StrokeStyle ID2D1StrokeStyle * GetMiterStrokeStyle(); // 获取 Bevel 样式的 ID2D1StrokeStyle ID2D1StrokeStyle * GetBevelStrokeStyle(); // 获取 Round 样式的 ID2D1StrokeStyle ID2D1StrokeStyle * GetRoundStrokeStyle(); // 获取 DPI static float GetDpi(); protected: D2D1_COLOR_F clear_color_; ID2D1Factory* factory_; IWICImagingFactory* imaging_factory_; IDWriteFactory* write_factory_; ID2D1StrokeStyle* miter_stroke_style_; ID2D1StrokeStyle* bevel_stroke_style_; ID2D1StrokeStyle* round_stroke_style_; E2DTextRenderer* text_renderer_; IDWriteTextFormat* fps_text_format_; IDWriteTextLayout* fps_text_layout_; ID2D1SolidColorBrush* solid_brush_; ID2D1HwndRenderTarget* render_target_; }; // 输入设备 class Input { public: Input( HWND hwnd ); ~Input(); // 检测键盘某按键是否正被按下 bool IsDown( KeyCode key ); // 检测鼠标按键是否正被按下 bool IsDown( MouseCode code ); // 获得鼠标X轴坐标值 float GetMouseX(); // 获得鼠标Y轴坐标值 float GetMouseY(); // 获得鼠标坐标值 Point GetMousePos(); // 获得鼠标X轴坐标增量 float GetMouseDeltaX(); // 获得鼠标Y轴坐标增量 float GetMouseDeltaY(); // 获得鼠标Z轴(鼠标滚轮)坐标增量 float GetMouseDeltaZ(); // 刷新设备状态 void Flush(); protected: IDirectInput8W * direct_input_; IDirectInputDevice8W* keyboard_device_; IDirectInputDevice8W* mouse_device_; DIMOUSESTATE mouse_state_; char key_buffer_[256]; }; // 音频设备 class Audio { public: Audio(); ~Audio(); // 开启设备 void Open(); // 关闭设备 void Close(); // 创建音源 HRESULT CreateVoice( IXAudio2SourceVoice ** voice, WAVEFORMATEX * wfx ); protected: IXAudio2 * x_audio2_; IXAudio2MasteringVoice* mastering_voice_; }; // 设备 class Device { public: // 获取图形设备 static Graphics * GetGraphics(); // 获取输入设备 static Input * GetInput(); // 获取音频设备 static Audio * GetAudio(); // 初始化 static void Init( HWND hwnd ); // 销毁资源 static void Destroy(); }; // 启动选项 struct Options { String title; // 标题 int width; // 宽度 int height; // 高度 int icon; // 图标资源 ID bool debug_mode; // 调试模式 Options() : title(L"Easy2D Game") , width(640) , height(480) , icon(0) , debug_mode(false) { } }; // 游戏控制器 class Game { public: // 开始 virtual void Start() = 0; // 更新 virtual void Update(float dt) {} // 运行 void Run( const Options& options = Options() ); // 结束 void Quit(); // 关闭窗口时触发 // 返回值:返回 false 将阻止窗口关闭 virtual bool OnExit() { return true; } // 修改窗体大小 void SetSize( int width, /* 窗体宽度 */ int height /* 窗体高度 */ ); // 设置窗体标题 void SetTitle( const String& title /* 窗体标题 */ ); // 设置窗体图标 void SetIcon( int resource_id /* 图标资源 ID */ ); // 获取窗体标题 const String& GetTitle() const; // 获取窗体宽度 int GetWidth() const; // 获取窗体高度 int GetHeight() const; // 获取窗体大小 Size GetSize() const; // 获取窗口句柄 HWND GetHWnd() const; // 切换场景 void EnterScene( Scene * scene, /* 场景 */ Transition * transition = nullptr /* 场景动画 */ ); // 获取当前场景 Scene * GetCurrentScene(); // 是否正在进行场景过渡 bool IsTransitioning() const; // 更新场景内容 void UpdateScene( float dt ); // 渲染场景画面 void DrawScene(); // 获取实例 static Game * GetInstance(); protected: Game(); ~Game(); // 初始化 void Init(); // 根据客户区大小定位窗口 Rect Locate( int width, int height ); // Win32 窗口消息回调程序 static LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param ); protected: HWND hwnd_; String title_; int width_; int height_; int icon_; bool debug_mode_; bool quit_; Scene* curr_scene_; Scene* next_scene_; Transition* transition_; }; }