2018-10-03 22:02:46 +08:00
// 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.
2018-10-17 00:16:26 +08:00
# ifndef __E2D_UTIL_H__
# define __E2D_UTIL_H__
2018-09-05 13:33:39 +08:00
# include "e2dmacros.h"
2017-10-12 23:34:13 +08:00
2018-10-16 14:13:15 +08:00
namespace easy2d
2017-10-12 23:34:13 +08:00
{
2018-09-05 13:33:39 +08:00
class Size ;
2018-04-13 00:39:27 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> <EFBFBD>
2018-10-29 10:01:53 +08:00
//
// Usage:
// <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> ά<EFBFBD> ռ <EFBFBD> <D5BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> : Point origin(0, 0);
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> : float distance = Point::Distance(p1, p2);
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ӽ<EFBFBD> : Point p = Point(10, 10) + Point(20, 20); // p <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (30, 30)
//
2018-09-05 13:33:39 +08:00
class Point
{
public :
float x ; // X <20> <> <EFBFBD> <EFBFBD>
float y ; // Y <20> <> <EFBFBD> <EFBFBD>
2018-04-13 00:39:27 +08:00
2018-09-05 13:33:39 +08:00
public :
Point ( ) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point (
float x ,
float y
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point (
const Point & other
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Point operator + ( const Point & other ) const ;
Point operator - ( const Point & other ) const ;
Point operator * ( float value ) const ;
Point operator / ( float value ) const ;
Point operator - ( ) const ;
bool operator = = ( const Point & other ) const ;
2018-05-17 23:53:27 +08:00
2018-10-16 14:13:15 +08:00
E2D_OP_EXPLICIT operator easy2d : : Size ( ) const ;
2018-08-19 20:40:44 +08:00
2018-09-05 13:33:39 +08:00
// <20> ж<EFBFBD> <D0B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static float Distance (
const Point & p1 ,
const Point & p2
) ;
} ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> С
2018-10-29 10:01:53 +08:00
//
// Usage:
// <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> ά<EFBFBD> <CEAC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ĵ<EFBFBD> С : Size s(10, 5); // <20> <> Ϊ 10, <20> <> Ϊ 5
// <20> <> С <EFBFBD> <D0A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ӽ<EFBFBD> : Size s = Size(10, 10) + Size(20, 20); // s <20> Ĵ<EFBFBD> С <EFBFBD> ǿ<EFBFBD> <C7BF> ߾<EFBFBD> Ϊ 30
//
2018-09-05 13:33:39 +08:00
class Size
{
public :
float width ; // <20> <> <EFBFBD> <EFBFBD>
float height ; // <20> ߶<EFBFBD>
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
public :
Size ( ) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size (
float width ,
float height
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size (
const Size & other
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Size operator + ( const Size & other ) const ;
Size operator - ( const Size & other ) const ;
Size operator * ( float value ) const ;
Size operator / ( float value ) const ;
Size operator - ( ) const ;
bool operator = = ( const Size & other ) const ;
2018-05-22 11:58:45 +08:00
2018-10-16 14:13:15 +08:00
E2D_OP_EXPLICIT operator easy2d : : Point ( ) const ;
2018-09-05 13:33:39 +08:00
} ;
2018-04-13 00:39:27 +08:00
2018-05-22 11:58:45 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> <EFBFBD>
2018-10-29 10:01:53 +08:00
//
// Usage:
// <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> ά<EFBFBD> <CEAC> <EFBFBD> <EFBFBD> : Rect rect = Rect(10, 20, 30, 40); // <20> <> <EFBFBD> Ͻ<EFBFBD> <CFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ (10, 20), <20> <> Ϊ 30, <20> <> Ϊ 40
// <20> <> <EFBFBD> ο <EFBFBD> <CEBF> <EFBFBD> ͨ<EFBFBD> <CDA8> Point + Size <20> <> <EFBFBD> <EFBFBD> , Point <20> <> ʾ <EFBFBD> <CABE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ͻ<EFBFBD> <CFBD> <EFBFBD> <EFBFBD> <EFBFBD> , Size <20> <> ʾ <EFBFBD> <CABE> <EFBFBD> ο <EFBFBD> <CEBF> <EFBFBD>
// <20> ж<EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> ھ <EFBFBD> <DABE> <EFBFBD> <EFBFBD> <EFBFBD> : bool contains = rect.ContainsPoint(p);
// <20> ж<EFBFBD> <D0B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> ཻ: bool intersects = rect1.Intersects(rect2);
//
2018-09-05 13:33:39 +08:00
class Rect
{
public :
2018-10-29 10:01:53 +08:00
Point origin ; // <20> <> <EFBFBD> Ͻ<EFBFBD> <CFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2018-09-05 13:33:39 +08:00
Size size ; // <20> <> <EFBFBD> Ⱥ߶<CDB8>
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
public :
Rect ( ) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect (
float x ,
float y ,
float width ,
float height
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect (
const Point & pos ,
const Size & size
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect (
const Rect & other
) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
Rect & operator = ( const Rect & other ) ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
bool operator = = ( const Rect & rect ) const ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20> жϵ<D0B6> <CFB5> Ƿ<EFBFBD> <C7B7> ھ <EFBFBD> <DABE> <EFBFBD> <EFBFBD> <EFBFBD>
bool ContainsPoint (
const Point & point
) const ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20> ж<EFBFBD> <D0B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> ཻ
bool Intersects (
const Rect & rect
) const ;
} ;
2018-05-17 23:53:27 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ɫ
2018-10-29 10:01:53 +08:00
//
// Usage:
// ʹ <> <CAB9> ö<EFBFBD> ٱ<EFBFBD> ʾ <EFBFBD> <CABE> ɫ: Color blue = Color::Blue;
// ʹ <> <CAB9> RGB <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> ɫ: Color red(1.0f, 0.0f, 0.0f);
// ʹ <> <CAB9> RGBA <20> <> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <CDB8> <EFBFBD> ȵ<EFBFBD> <C8B5> <EFBFBD> ɫ: Color not_black(1.0f, 1.0f, 1.0f, 0.5f);
// ʹ <> <CAB9> һ <EFBFBD> <D2BB> UINT <20> <> <EFBFBD> ͵<EFBFBD> ֵ<EFBFBD> <D6B5> ʾ RGB: Color black(0x000000);
//
2018-09-05 13:33:39 +08:00
class Color
{
public :
Color ( ) ;
Color (
float r ,
float g ,
float b
) ;
Color (
float r ,
float g ,
float b ,
float alpha
) ;
Color (
UINT rgb
) ;
Color (
UINT rgb ,
float alpha
) ;
Color (
const D2D1_COLOR_F & color
) ;
E2D_OP_EXPLICIT operator D2D1_COLOR_F ( ) const ;
public :
enum Value : UINT
{
2018-10-18 13:38:31 +08:00
Black = 0x000000 ,
Blue = 0x0000FF ,
BlueViolet = 0x8A2BE2 ,
Brown = 0xA52A2A ,
Chocolate = 0xD2691E ,
DarkBlue = 0x00008B ,
DarkGray = 0xA9A9A9 ,
DarkGreen = 0x006400 ,
DarkOrange = 0xFF8C00 ,
DarkRed = 0x8B0000 ,
DarkViolet = 0x9400D3 ,
ForestGreen = 0x228B22 ,
Gold = 0xFFD700 ,
Gray = 0x808080 ,
Green = 0x008000 ,
GreenYellow = 0xADFF2F ,
LightBlue = 0xADD8E6 ,
LightCyan = 0xE0FFFF ,
LightGreen = 0x90EE90 ,
LightGray = 0xD3D3D3 ,
LightPink = 0xFFB6C1 ,
LightSeaGreen = 0x20B2AA ,
LightSkyBlue = 0x87CEFA ,
LightYellow = 0xFFFFE0 ,
Orange = 0xFFA500 ,
OrangeRed = 0xFF4500 ,
Pink = 0xFFC0CB ,
Purple = 0x800080 ,
Red = 0xFF0000 ,
Silver = 0xC0C0C0 ,
SkyBlue = 0x87CEEB ,
Snow = 0xFFFAFA ,
Violet = 0xEE82EE ,
Wheat = 0xF5DEB3 ,
White = 0xFFFFFF ,
WhiteSmoke = 0xF5F5F5 ,
Wood = 0xDEB887 ,
Yellow = 0xFFFF00 ,
YellowGreen = 0x9ACD32
2018-09-05 13:33:39 +08:00
} ;
public :
float r ;
float g ;
float b ;
float a ;
} ;
2018-10-18 13:38:31 +08:00
// <20> <> <EFBFBD> <EFBFBD>
enum class Direction : int
{
Up , /* <20> <> */
Down , /* <20> <> */
Left , /* <20> <> */
Right /* <20> <> */
} ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ཻ<EFBFBD> <E0BDBB> ʽ
enum class Stroke : int
{
Miter = 0 , /* б <> <D0B1> */
Bevel = 1 , /* б <> <D0B1> */
Round = 2 /* Բ<> <D4B2> */
} ;
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> ̼<EFBFBD> ֵ
enum class KeyCode : int
{
2018-10-18 13:38:31 +08:00
Unknown = 0 ,
Up = 0xC8 ,
Left = 0xCB ,
Right = 0xCD ,
Down = 0xD0 ,
Enter = 0x1C ,
Space = 0x39 ,
Esc = 0x01 ,
Q = 0x10 ,
W = 0x11 ,
E = 0x12 ,
R = 0x13 ,
T = 0x14 ,
Y = 0x15 ,
U = 0x16 ,
I = 0x17 ,
O = 0x18 ,
P = 0x19 ,
A = 0x1E ,
S = 0x1F ,
D = 0x20 ,
F = 0x21 ,
G = 0x22 ,
H = 0x23 ,
J = 0x24 ,
K = 0x25 ,
L = 0x26 ,
Z = 0x2C ,
X = 0x2D ,
C = 0x2E ,
V = 0x2F ,
B = 0x30 ,
N = 0x31 ,
M = 0x32 ,
Num1 = 0x02 ,
Num2 = 0x03 ,
Num3 = 0x04 ,
Num4 = 0x05 ,
Num5 = 0x06 ,
Num6 = 0x07 ,
Num7 = 0x08 ,
Num8 = 0x09 ,
Num9 = 0x0A ,
Num0 = 0x0B ,
Numpad7 = 0x47 ,
Numpad8 = 0x48 ,
Numpad9 = 0x49 ,
Numpad4 = 0x4B ,
Numpad5 = 0x4C ,
Numpad6 = 0x4D ,
Numpad1 = 0x4F ,
Numpad2 = 0x50 ,
Numpad3 = 0x51 ,
Numpad0 = 0x52 ,
2018-09-05 13:33:39 +08:00
} ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֵ
enum class MouseCode : int
{
Left , /* <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> */
Right , /* <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Ҽ<EFBFBD> */
Middle /* <20> <> <EFBFBD> <EFBFBD> <EFBFBD> м<EFBFBD> */
} ;
2018-08-19 20:40:44 +08:00
2018-09-05 13:33:39 +08:00
// ʱ<> <CAB1> <EFBFBD> <EFBFBD>
2018-10-29 10:01:53 +08:00
//
// Usage:
// 5 <20> <> : Duration::Second * 5
// 1.5 Сʱ: Duration::Hour * 1.5
// 3 Сʱ 45 <20> <> 15 <20> <> : Duration::Hour * 3 + Duration::Minute * 45 + Duration::Second * 15
//
2018-09-05 13:33:39 +08:00
class Duration
{
2018-10-18 15:33:14 +08:00
public :
static const Duration Millisecond ; // <20> <> <EFBFBD> <EFBFBD>
static const Duration Second ; // <20> <>
static const Duration Minute ; // <20> <> <EFBFBD> <EFBFBD>
static const Duration Hour ; // Сʱ
2018-09-05 13:33:39 +08:00
public :
Duration ( ) ;
2018-04-13 00:39:27 +08:00
2018-09-05 13:33:39 +08:00
explicit Duration (
2018-10-18 23:10:09 +08:00
int milliseconds
2018-09-05 13:33:39 +08:00
) ;
2018-04-27 17:07:47 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2018-10-18 23:10:09 +08:00
int Milliseconds ( ) const ;
2018-04-27 17:07:47 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <EFBFBD>
float Seconds ( ) const ;
2018-07-03 20:19:00 +08:00
2018-10-18 15:33:14 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float Minutes ( ) const ;
// <20> <> ȡС ʱ<D0A1> <CAB1>
float Hours ( ) const ;
2018-09-05 13:33:39 +08:00
bool operator = = ( const Duration & ) const ;
bool operator ! = ( const Duration & ) const ;
bool operator > ( const Duration & ) const ;
bool operator > = ( const Duration & ) const ;
bool operator < ( const Duration & ) const ;
bool operator < = ( const Duration & ) const ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
Duration operator + ( Duration const & ) const ;
Duration operator - ( Duration const & ) const ;
2018-10-18 15:33:14 +08:00
Duration operator * ( int ) const ;
Duration operator * ( float ) const ;
2018-10-18 23:10:09 +08:00
Duration operator * ( double ) const ;
2018-10-18 15:33:14 +08:00
Duration operator / ( int ) const ;
Duration operator / ( float ) const ;
2018-10-18 23:10:09 +08:00
Duration operator / ( double ) const ;
2018-09-02 14:30:48 +08:00
2018-09-05 13:33:39 +08:00
Duration & operator + = ( Duration const & ) ;
Duration & operator - = ( Duration const & ) ;
2018-10-18 15:33:14 +08:00
Duration & operator * = ( int ) ;
Duration & operator * = ( float ) ;
2018-10-18 23:10:09 +08:00
Duration & operator * = ( double ) ;
2018-10-18 15:33:14 +08:00
Duration & operator / = ( int ) ;
Duration & operator / = ( float ) ;
2018-10-18 23:10:09 +08:00
Duration & operator / = ( double ) ;
friend Duration operator * ( int , const Duration & ) ;
friend Duration operator * ( float , const Duration & ) ;
friend Duration operator * ( double , const Duration & ) ;
friend Duration operator / ( int , const Duration & ) ;
friend Duration operator / ( float , const Duration & ) ;
friend Duration operator / ( double , const Duration & ) ;
2018-09-02 14:30:48 +08:00
2018-10-18 00:17:27 +08:00
private :
2018-10-18 23:10:09 +08:00
int milliseconds_ ;
2018-09-05 13:33:39 +08:00
} ;
2018-09-02 14:30:48 +08:00
2018-09-05 13:33:39 +08:00
// ʱ<> <CAB1> <EFBFBD> <EFBFBD>
2018-10-29 10:01:53 +08:00
//
// Usage:
// ʹ <> <CAB9> Time::Now <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȡ<EFBFBD> <C8A1> ǰʱ<C7B0> <CAB1> : Time now = Time::Now();
// <20> <> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> õ<EFBFBD> <C3B5> Ľ<EFBFBD> <C4BD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> Duration <20> <> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> :
// Time t1 = Time::Now();
// ... // <20> <> Щʲô
// Time t2 = Time::Now();
// Ȼ<> <C8BB> <EFBFBD> <EFBFBD> ȡ<EFBFBD> <C8A1> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ĺ<EFBFBD> <C4BA> <EFBFBD> <EFBFBD> <EFBFBD> :
// int ms = (t2 - t1).Milliseconds();
//
2018-09-05 13:33:39 +08:00
class Time
{
public :
Time ( ) ;
2018-01-30 16:45:38 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ȡʱ<C8A1> <CAB1> <EFBFBD> <EFBFBD>
time_t GetTimeStamp ( ) const ;
2017-10-14 01:07:34 +08:00
2018-10-18 15:33:14 +08:00
// <20> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD> ʱ
2018-09-05 13:33:39 +08:00
bool IsZero ( ) const ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
Time operator + ( Duration const & ) const ;
Time operator - ( Duration const & ) const ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
Time & operator + = ( Duration const & ) ;
Time & operator - = ( Duration const & ) ;
2017-10-17 21:22:25 +08:00
2018-09-05 13:33:39 +08:00
Duration operator - ( Time const & ) const ;
2017-10-17 21:22:25 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> ǰʱ<C7B0> <CAB1>
static Time Now ( ) ;
2017-10-21 19:09:31 +08:00
2018-10-18 00:17:27 +08:00
private :
2018-09-05 13:33:39 +08:00
std : : chrono : : steady_clock : : time_point time_ ;
} ;
2017-12-04 11:18:38 +08:00
2017-10-21 19:09:31 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> <EFBFBD>
class Font
{
public :
2018-10-28 23:24:01 +08:00
std : : wstring family ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float size ; // <20> ֺ<EFBFBD>
UINT weight ; // <20> <> ϸֵ
bool italic ; // <20> Ƿ<EFBFBD> б <EFBFBD> <D0B1>
2018-09-05 13:33:39 +08:00
public :
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ϸֵ
enum Weight : UINT
{
2018-10-18 13:38:31 +08:00
Thin = 100 ,
ExtraLight = 200 ,
Light = 300 ,
Normal = 400 ,
Medium = 500 ,
Bold = 700 ,
ExtraBold = 800 ,
Black = 900 ,
ExtraBlack = 950
2018-09-05 13:33:39 +08:00
} ;
public :
explicit Font (
2018-10-28 23:24:01 +08:00
const std : : wstring & family = L " " ,
2018-10-18 13:38:31 +08:00
float size = 22 ,
UINT weight = Font : : Weight : : Normal ,
bool italic = false
2018-09-05 13:33:39 +08:00
) ;
} ;
// <20> <> Դ
2018-10-29 10:01:53 +08:00
//
// Usage:
// Resource <20> <> <EFBFBD> ڻ<EFBFBD> ȡ<EFBFBD> <C8A1> ִ<EFBFBD> <D6B4> <EFBFBD> ļ<EFBFBD> (exe) <20> е <EFBFBD> <D0B5> <EFBFBD> Դ, <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ڹ<EFBFBD> <DAB9> 캯<EFBFBD> <ECBAAF> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// <20> <> Դ<EFBFBD> <D4B4> <EFBFBD> ͺ <EFBFBD> <CDBA> <EFBFBD> <EFBFBD> Ʊ<EFBFBD> ʶ<EFBFBD> <CAB6> <EFBFBD> <EFBFBD>
// <20> <> <EFBFBD> <EFBFBD> , һ <> <D2BB> <EFBFBD> <EFBFBD> Ƶ<EFBFBD> <C6B5> Դ<EFBFBD> <D4B4> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ L"WAVE", <20> <> <EFBFBD> Ʊ<EFBFBD> ʶ<EFBFBD> <CAB6> Ϊ IDR_WAVE_1, <20> <> ô<EFBFBD> <C3B4> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Դ:
// Resource res(MAKEINTRESOURCE(IDR_WAVE_1), L"WAVE");
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ҫ<EFBFBD> ֶ<EFBFBD> <D6B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Դ, <20> <> <EFBFBD> <EFBFBD> ͨ<EFBFBD> <CDA8> Load <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȡ<EFBFBD> <C8A1> Դ<EFBFBD> <D4B4> <EFBFBD> <EFBFBD>
// if (res.Load()) {
// LPVOID data = res.GetData();
// DWORD size = res.GetDataSize();
// }
// <20> ˽<EFBFBD> <CBBD> <EFBFBD> Դ<EFBFBD> ĸ<EFBFBD> <C4B8> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ: https://docs.microsoft.com/en-us/windows/desktop/menurc/resources
//
2018-09-05 13:33:39 +08:00
class Resource
{
public :
Resource (
2018-10-28 23:24:01 +08:00
LPCWSTR name , /* <20> <> Դ<EFBFBD> <D4B4> <EFBFBD> <EFBFBD> */
LPCWSTR type /* <20> <> Դ<EFBFBD> <D4B4> <EFBFBD> <EFBFBD> */
2018-09-05 13:33:39 +08:00
) ;
2018-04-24 09:02:06 +08:00
2018-10-28 23:24:01 +08:00
bool Load ( ) ;
LPCWSTR GetName ( ) const ;
LPCWSTR GetType ( ) const ;
LPVOID GetData ( ) const ;
DWORD GetDataSize ( ) const ;
size_t GetHashCode ( ) const ;
private :
bool loaded_ ;
LPCWSTR name_ ;
LPCWSTR type_ ;
LPVOID data_ ;
DWORD data_size_ ;
2018-09-05 13:33:39 +08:00
} ;
2018-07-05 22:05:23 +08:00
2018-09-16 16:07:51 +08:00
// <20> <> άת<CEAC> <D7AA>
class Transform
{
public :
Point position ; // <20> <> <EFBFBD> <EFBFBD>
Size size ; // <20> <> С
float scale_x ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float scale_y ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float rotation ; // <20> <> ת
float skew_x ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> б <EFBFBD> Ƕ<EFBFBD>
float skew_y ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> б <EFBFBD> Ƕ<EFBFBD>
float pivot_x ; // ֧<> <D6A7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float pivot_y ; // ֧<> <D6A7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2018-10-03 18:04:04 +08:00
public :
Transform ( ) ;
E2D_OP_EXPLICIT operator D2D1 : : Matrix3x2F ( ) const ;
bool operator = = ( const Transform & other ) const ;
2018-09-16 16:07:51 +08:00
} ;
2018-10-28 23:35:07 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2018-10-29 10:01:53 +08:00
//
// Usage:
// ʹ <> þ<EFBFBD> ̬<EFBFBD> <CCAC> <EFBFBD> <EFBFBD> Range <20> <> ȡָ<C8A1> <D6B8> <EFBFBD> <EFBFBD> Χ <EFBFBD> ڵ<EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> <> :
// int n = Random::Range(1, 5); // <20> <> ȡ 1~6 <20> ڵ<EFBFBD> <DAB5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> <> <EFBFBD> <EFBFBD> 1 <20> <> 6
// <20> <> <EFBFBD> <EFBFBD> ͬ<EFBFBD> <CDAC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ڸ<EFBFBD> <DAB8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> , <20> <> :
// double d = Random::Range(1.2, 1.5);
// ע<> <D7A2> , <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȡ<EFBFBD> <C8A1> <EFBFBD> ڲ<EFBFBD> <DAB2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ͡<EFBFBD>
//
2018-10-28 23:35:07 +08:00
class Random
{
public :
// ȡ<> ÷<EFBFBD> Χ <EFBFBD> ڵ<EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
template < typename T >
static inline T Range ( T min , T max )
{
return easy2d : : Random : : RandomInt ( min , max ) ;
}
// ȡ<> ÷<EFBFBD> Χ <EFBFBD> ڵ<EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static inline float Range ( float min , float max )
{
return easy2d : : Random : : RandomReal ( min , max ) ;
}
// ȡ<> ÷<EFBFBD> Χ <EFBFBD> ڵ<EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static inline double Range ( double min , double max )
{
return easy2d : : Random : : RandomReal ( min , max ) ;
}
private :
template < typename T >
static T RandomInt ( T min , T max )
{
std : : uniform_int_distribution < T > dist ( min , max ) ;
return dist ( Random : : GetEngine ( ) ) ;
}
template < typename T >
static T RandomReal ( T min , T max )
{
std : : uniform_real_distribution < T > dist ( min , max ) ;
return dist ( Random : : GetEngine ( ) ) ;
}
static std : : default_random_engine & GetEngine ( ) ;
} ;
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> ü<EFBFBD> <C3BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
class Ref
{
public :
Ref ( ) ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
virtual ~ Ref ( ) ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ü<EFBFBD> <C3BC> <EFBFBD>
2018-09-07 00:28:54 +08:00
LONG Retain ( ) ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ü<EFBFBD> <C3BC> <EFBFBD>
2018-09-07 00:28:54 +08:00
LONG Release ( ) ;
2018-07-05 22:05:23 +08:00
2018-09-05 13:33:39 +08:00
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> ü<EFBFBD> <C3BC> <EFBFBD>
2018-09-07 00:28:54 +08:00
LONG GetRefCount ( ) const ;
2017-10-14 18:43:32 +08:00
2018-10-18 00:17:27 +08:00
private :
2018-09-07 00:28:54 +08:00
LONG ref_count_ ;
2018-09-05 13:33:39 +08:00
} ;
2017-10-19 12:48:58 +08:00
2018-09-04 22:57:40 +08:00
2018-09-07 00:28:54 +08:00
template < class Interface >
inline void SafeRelease ( Interface * & p )
{
if ( p ! = nullptr )
{
p - > Release ( ) ;
p = nullptr ;
}
}
2018-07-08 02:41:44 +08:00
2018-10-17 00:49:49 +08:00
inline void ThrowIfFailed ( HRESULT hr )
{
if ( FAILED ( hr ) )
{
// <20> ڴ˴ <DAB4> <CBB4> <EFBFBD> <EFBFBD> öϵ<C3B6> <CFB5> Բ<EFBFBD> <D4B2> <EFBFBD> ϵͳ<CFB5> 쳣.
static char s_str [ 64 ] = { } ;
sprintf_s ( s_str , " Failure with HRESULT of %08X " , static_cast < unsigned int > ( hr ) ) ;
2018-10-28 23:35:07 +08:00
throw std : : runtime_error ( s_str ) ;
2018-10-17 00:49:49 +08:00
}
}
2018-10-17 00:16:26 +08:00
} // end of easy2d namespace
2018-07-08 02:41:44 +08:00
2018-10-17 00:16:26 +08:00
# endif // __E2D_UTIL_H__