增加RenderWindow定义和GLFW简单实现
This commit is contained in:
parent
8f63b19f44
commit
f521af3702
51
inc/hgl/render/RenderWindow.h
Normal file
51
inc/hgl/render/RenderWindow.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#ifndef HGL_RENDER_WINDOW_INCLUDE
|
||||||
|
#define HGL_RENDER_WINDOW_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/type/Vertex2.h>
|
||||||
|
#include<hgl/type/BaseString.h>
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 渲染窗口基类
|
||||||
|
*/
|
||||||
|
class RenderWindow
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
|
||||||
|
OSString caption;
|
||||||
|
bool full_screen;
|
||||||
|
|
||||||
|
int width,height;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
const uint GetWidth()const{return width;}
|
||||||
|
const uint GetHeight()const{return height;}
|
||||||
|
const bool GetFullScreen()const{return full_screen;}
|
||||||
|
|
||||||
|
public: //方法
|
||||||
|
|
||||||
|
RenderWindow()=default; ///<本类构造函数
|
||||||
|
virtual ~RenderWindow()=default; ///<本类析构函数
|
||||||
|
|
||||||
|
virtual void ToMin()=0; ///<窗口最小化
|
||||||
|
virtual void ToMax()=0; ///<窗口最大化
|
||||||
|
|
||||||
|
virtual void Show()=0; ///<显示窗口
|
||||||
|
virtual void Hide()=0; ///<隐藏窗口
|
||||||
|
|
||||||
|
virtual const OSString &GetCaption()const{return caption;}
|
||||||
|
virtual void SetCaption(const OSString &)=0;
|
||||||
|
|
||||||
|
public: //被实际操作系统接口层所调用的函数,在不了解的情况下请不要使用
|
||||||
|
|
||||||
|
virtual void SetSize(int w,int h)=0; ///<设置窗口大小
|
||||||
|
|
||||||
|
virtual void MakeToCurrent()=0; ///<切换到当前
|
||||||
|
virtual void SwapBuffer()=0; ///<交换缓冲区
|
||||||
|
virtual void WaitEvent(const double &time_out=0)=0; ///<等待下一个事件
|
||||||
|
virtual void PollEvent()=0; ///<轮询事件
|
||||||
|
virtual bool IsOpen()=0; ///<是否依然存在
|
||||||
|
};//class RenderWindow
|
||||||
|
}//namespace hgl
|
||||||
|
#endif//HGL_RENDER_WINDOW_INCLUDE
|
147
inc/hgl/type/Vertex2.cpp
Normal file
147
inc/hgl/type/Vertex2.cpp
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
#ifndef HGL_VERTEX_2_CPP
|
||||||
|
#define HGL_VERTEX_2_CPP
|
||||||
|
|
||||||
|
#include<string.h>
|
||||||
|
#include<math.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
void Vertex2<T>::Scale(double vx,double vy,double scale)
|
||||||
|
{
|
||||||
|
x=vx+(x-vx)*scale;
|
||||||
|
y=vy+(y-vy)*scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Vertex2<T>::Scale(double vx,double vy,double sx,double sy)
|
||||||
|
{
|
||||||
|
x=vx+(x-vx)*sx;
|
||||||
|
y=vy+(y-vy)*sy;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> template<typename N>
|
||||||
|
void Vertex2<T>::Scale(const Vertex2<N> &v,double scale)
|
||||||
|
{
|
||||||
|
x=v.x+(x-v.x)*scale;
|
||||||
|
y=v.y+(y-v.y)*scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> template<typename N>
|
||||||
|
void Vertex2<T>::Scale(const Vertex2<N> &v,double sx,double sy)
|
||||||
|
{
|
||||||
|
x=v.x+(x-v.x)*sx;
|
||||||
|
y=v.y+(y-v.y)*sy;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Vertex2<T>::Rotate(double vx,double vy,double ang)
|
||||||
|
{
|
||||||
|
double as,ac;
|
||||||
|
double nx,ny;
|
||||||
|
|
||||||
|
as=sin(ang*HGL_PI/180.0f);
|
||||||
|
ac=cos(ang*HGL_PI/180.0f);
|
||||||
|
|
||||||
|
nx=vx+((x-vx)*ac-(y-vy)*as);
|
||||||
|
ny=vy+((x-vx)*as+(y-vy)*ac);
|
||||||
|
|
||||||
|
x=nx;y=ny;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
float Vertex2<T>::Length() const
|
||||||
|
{
|
||||||
|
return sqrt(x*x+y*y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
float Vertex2<T>::Length(float vx,float vy) const
|
||||||
|
{
|
||||||
|
float w=x-vx;
|
||||||
|
float h=y-vy;
|
||||||
|
|
||||||
|
return float(sqrt(double(w*w+h*h)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
float Vertex2<T>::Angle(float vx,float vy) const
|
||||||
|
{
|
||||||
|
if(vx==x)
|
||||||
|
{
|
||||||
|
if(vy>y)return(90);
|
||||||
|
else return(270);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(vy==y)
|
||||||
|
{
|
||||||
|
if(vx>x)return(0);
|
||||||
|
else return(180);
|
||||||
|
}
|
||||||
|
|
||||||
|
// asin 参数范围必须在 -1 到 +1 之间,否则会报错
|
||||||
|
//
|
||||||
|
|
||||||
|
double xx=vx-x;
|
||||||
|
double yy=vy-y;
|
||||||
|
|
||||||
|
double ang = atan(yy/xx)/HGL_PI*180.0f;
|
||||||
|
|
||||||
|
if(xx<0)
|
||||||
|
return(ang+180);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(yy<0)
|
||||||
|
return(ang+360);
|
||||||
|
else
|
||||||
|
return(ang);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> template<typename N>
|
||||||
|
Vertex2<T> Vertex2<T>::To(const Vertex2<N> &target,double pos)const
|
||||||
|
{
|
||||||
|
if(pos==0)return(*this);else
|
||||||
|
if(pos==1)return(target);else
|
||||||
|
{
|
||||||
|
Vertex2<T> result;
|
||||||
|
|
||||||
|
result.x=x+(target.x-x)*pos;
|
||||||
|
result.y=y+(target.y-y)*pos;
|
||||||
|
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void Vertex2<T>::Normalize()
|
||||||
|
{
|
||||||
|
double size=sqrt(x*x+y*y);
|
||||||
|
|
||||||
|
if(size>0)
|
||||||
|
{
|
||||||
|
x/=size;
|
||||||
|
y/=size;
|
||||||
|
}
|
||||||
|
|
||||||
|
x=0;y=0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> template<typename N>
|
||||||
|
Vertex2<T> Vertex2<T>::ResizeLength(const Vertex2<N> &target,double scale)const
|
||||||
|
{
|
||||||
|
Vertex2<T> result;
|
||||||
|
|
||||||
|
result.x=target.x+(x-target.x)*scale;
|
||||||
|
result.y=target.y+(y-target.y)*scale;
|
||||||
|
|
||||||
|
return(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> template<typename N>
|
||||||
|
Vertex2<T> Vertex2<T>::ToLength(const Vertex2<N> &target,double new_length)const
|
||||||
|
{
|
||||||
|
return ResizeLength(target,new_length/Length(target));
|
||||||
|
}
|
||||||
|
}//namespace hgl
|
||||||
|
#endif//HGL_VERTEX_2_CPP
|
113
inc/hgl/type/Vertex2.h
Normal file
113
inc/hgl/type/Vertex2.h
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
#ifndef HGL_VERTEX_2_INCLUDE
|
||||||
|
#define HGL_VERTEX_2_INCLUDE
|
||||||
|
|
||||||
|
#include<hgl/type/DataType.h>
|
||||||
|
#include<hgl/CompOperator.h>
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 2D顶点坐标类模板
|
||||||
|
*/
|
||||||
|
template<typename T> class Vertex2 ///XY 2D顶点坐标类模板
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
T x,y; ///<坐标值
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Vertex2<T>(){x=y=0;} ///<本类构造函数
|
||||||
|
Vertex2<T>(T nx,T ny){x=nx,y=ny;} ///<本类构造函数
|
||||||
|
template<typename N>
|
||||||
|
Vertex2<T>(const Vertex2<N> &v){x=v.x,y=v.y;} ///<本类构造函数
|
||||||
|
|
||||||
|
void Zero(){x=0,y=0;} ///<将x和y全清为0
|
||||||
|
void One(){x=1,y=1;} ///<将x和y全清为1
|
||||||
|
|
||||||
|
void Set(float nx,float ny){x=nx,y=ny;} ///<设置坐标值为nx,ny
|
||||||
|
|
||||||
|
void Flip(float vx,float vy){FlipX(vy);FlipY(vx);} ///<对应vx,vy做翻转
|
||||||
|
template<typename N> void Flip(const Vertex2<N> &v){Flip(v.x,v.y);} ///<对应v做翻转
|
||||||
|
|
||||||
|
void FlipX(){y=-y;} ///<对应X轴翻转
|
||||||
|
void FlipY(){x=-x;} ///<对应Y轴翻转
|
||||||
|
void FlipX(float v){y=v-(y-v);} ///<对应指定X轴做翻转
|
||||||
|
void FlipY(float v){x=v-(x-v);} ///<对应指定Y轴做翻转
|
||||||
|
|
||||||
|
void Scale(double,double,double); ///<相对某一点做缩放
|
||||||
|
void Scale(double,double,double,double); ///<相对某一点做缩放
|
||||||
|
template<typename N> void Scale(const Vertex2<N> &,double); ///<相对某一点做缩放
|
||||||
|
template<typename N> void Scale(const Vertex2<N> &,double,double); ///<相对某一点做指定比例的缩放
|
||||||
|
|
||||||
|
void Rotate(double,double,double); ///<绕指定坐标旋转指定角度
|
||||||
|
template<typename N> void Rotate(const Vertex2<N> &v,double ang){Rotate(v.x,v.y,ang);} ///<绕指定坐标旋转指定角度
|
||||||
|
|
||||||
|
void Move(float vx,float vy){x+=vx;y+=vy;} ///<移动
|
||||||
|
template<typename N1,typename N2>
|
||||||
|
void Scale(const Vertex2<N1> &v,const Vertex2<N2> &l){Scale(v,l.x,l.y);} ///<相对某一点做指定比例的缩放
|
||||||
|
|
||||||
|
float Length()const; ///<计算当前点与零点之间的距离
|
||||||
|
float Length(float vx,float vy)const; ///<计算当前点与指定点之间的距离
|
||||||
|
float Angle(float vx,float vy)const; ///<计算当前点与指定点之间的角度
|
||||||
|
|
||||||
|
template<typename N> float Length(const Vertex2<N> &v)const{return(Length(v.x,v.y));} ///<计算当前点与指定点之间的距离
|
||||||
|
template<typename N> float Angle(const Vertex2<N> &v)const{return(Angle(v.x,v.y));} ///<计算当前点与指定点之间的角度
|
||||||
|
void Normalize();
|
||||||
|
|
||||||
|
template<typename N> Vertex2<T> ResizeLength(const Vertex2<N> &,double)const; ///<调整这个方向上两点的距离
|
||||||
|
template<typename N> Vertex2<T> ToLength(const Vertex2<N> &,double)const; ///<按这个方向将两点的距离调到指定长度
|
||||||
|
|
||||||
|
template<typename N> Vertex2<T> To(const Vertex2<N> &,double)const;
|
||||||
|
|
||||||
|
public: //操作符重载
|
||||||
|
|
||||||
|
template<typename N> bool operator == (const Vertex2<N> &v)const
|
||||||
|
{
|
||||||
|
if(x!=v.x)return(false);
|
||||||
|
if(y!=v.y)return(false);
|
||||||
|
return(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename N> bool operator != (const Vertex2<N> &v)const
|
||||||
|
{
|
||||||
|
if(x!=v.x)return(true);
|
||||||
|
if(y!=v.y)return(true);
|
||||||
|
return(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Vertex2<T> &operator = (const float *v){x=*v++;y=*v;return *this;}
|
||||||
|
|
||||||
|
template<typename N> Vertex2<T> &operator +=(const Vertex2<N> &v){x+=v.x;y+=v.y;return *this;}
|
||||||
|
template<typename N> Vertex2<T> &operator -=(const Vertex2<N> &v){x-=v.x;y-=v.y;return *this;}
|
||||||
|
template<typename N> Vertex2<T> &operator *=(const Vertex2<N> &v){x*=v.x;y*=v.y;return *this;}
|
||||||
|
template<typename N> Vertex2<T> &operator /=(const Vertex2<N> &v){x/=v.x;y/=v.y;return *this;}
|
||||||
|
|
||||||
|
Vertex2<T> &operator *=(const float v){x*=v;y*=v;return *this;}
|
||||||
|
Vertex2<T> &operator /=(const float v){x/=v;y/=v;return *this;}
|
||||||
|
|
||||||
|
template<typename N> Vertex2<T> operator + (const Vertex2<N> &v)const{return(Vertex2<T>(x+v.x,y+v.y));}
|
||||||
|
template<typename N> Vertex2<T> operator - (const Vertex2<N> &v)const{return(Vertex2<T>(x-v.x,y-v.y));}
|
||||||
|
template<typename N> Vertex2<T> operator * (const Vertex2<N> &v)const{return(Vertex2<T>(x*v.x,y*v.y));}
|
||||||
|
template<typename N> Vertex2<T> operator / (const Vertex2<N> &v)const{return(Vertex2<T>(x/v.x,y/v.y));}
|
||||||
|
|
||||||
|
Vertex2<T> operator * (const float v)const{return(Vertex2<T>(x*v,y*v));}
|
||||||
|
Vertex2<T> operator / (const float v)const{return(Vertex2<T>(x/v,y/v));}
|
||||||
|
|
||||||
|
Vertex2<T> operator - () const {return(Vertex2<T>(-x,-y));}
|
||||||
|
|
||||||
|
operator T *() const {return(( T *)this);} ///<使得本类可以直接当做T *使用
|
||||||
|
operator const T *() const {return((const T *)this);} ///<使得本类可以直接当做const T *使用
|
||||||
|
};//class Vertex2
|
||||||
|
|
||||||
|
typedef Vertex2<int> Vertex2i;
|
||||||
|
typedef Vertex2<uint> Vertex2ui;
|
||||||
|
typedef Vertex2<float> Vertex2f;
|
||||||
|
typedef Vertex2<double> Vertex2d;
|
||||||
|
|
||||||
|
typedef Vertex2<int8> Vertex2b;
|
||||||
|
typedef Vertex2<uint8> Vertex2ub;
|
||||||
|
typedef Vertex2<int16> Vertex2s;
|
||||||
|
typedef Vertex2<uint16> Vertex2us;
|
||||||
|
}//namespace hgl
|
||||||
|
#include<hgl/type/Vertex2.cpp>
|
||||||
|
#endif//HGL_VERTEX_2_INCLUDE
|
@ -1,2 +1,2 @@
|
|||||||
add_subdirectory(RenderDevice)
|
add_subdirectory(RenderDevice)
|
||||||
add_subdirectory(RenderDriver)
|
# add_subdirectory(RenderDriver)
|
||||||
|
@ -1 +1,2 @@
|
|||||||
add_library(ULRE.RenderDevice STATIC GLFW/RenderDeviceGLFW.cpp)
|
add_library(ULRE.RenderDevice STATIC GLFW/RenderDeviceGLFW.cpp
|
||||||
|
GLFW/RenderWindowGLFW.cpp)
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
namespace hgl
|
namespace hgl
|
||||||
{
|
{
|
||||||
|
RenderWindow *CreateRenderWindowGLFW(GLFWwindow *,bool is_fullscreen);
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
static RenderDevice *render_device_glfw=nullptr;
|
static RenderDevice *render_device_glfw=nullptr;
|
||||||
@ -169,7 +171,7 @@ namespace hgl
|
|||||||
|
|
||||||
if(!win)return(nullptr);
|
if(!win)return(nullptr);
|
||||||
|
|
||||||
return(new WindowGLFW(win,false));
|
return(CreateRenderWindowGLFW(win,false));
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderWindow *Create(const Display *disp,const VideoMode *vm,const RenderSetup *rs) override
|
RenderWindow *Create(const Display *disp,const VideoMode *vm,const RenderSetup *rs) override
|
||||||
@ -189,7 +191,7 @@ namespace hgl
|
|||||||
|
|
||||||
if(!win)return(nullptr);
|
if(!win)return(nullptr);
|
||||||
|
|
||||||
return(new WindowGLFW(win,true));
|
return(CreateRenderWindowGLFW(win,true));
|
||||||
}
|
}
|
||||||
};//class RenderDevice
|
};//class RenderDevice
|
||||||
|
|
||||||
|
69
src/RenderDevice/GLFW/RenderWindowGLFW.cpp
Normal file
69
src/RenderDevice/GLFW/RenderWindowGLFW.cpp
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
#include<hgl/render/RenderWindow.h>
|
||||||
|
#include<GLFW/glfw3.h>
|
||||||
|
|
||||||
|
namespace hgl
|
||||||
|
{
|
||||||
|
class RenderWindowGLFW:public RenderWindow
|
||||||
|
{
|
||||||
|
GLFWwindow *glfw_win;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
RenderWindowGLFW(GLFWwindow *w,bool is_fullscreen)
|
||||||
|
{
|
||||||
|
glfw_win=w;
|
||||||
|
full_screen=is_fullscreen;
|
||||||
|
|
||||||
|
glfwGetWindowSize(glfw_win,&width,&height);
|
||||||
|
}
|
||||||
|
|
||||||
|
~RenderWindowGLFW()
|
||||||
|
{
|
||||||
|
glfwDestroyWindow(glfw_win);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ToMin()override{glfwIconifyWindow(glfw_win);}
|
||||||
|
void ToMax()override{glfwMaximizeWindow(glfw_win);}
|
||||||
|
|
||||||
|
void Show()override{glfwShowWindow(glfw_win);}
|
||||||
|
void Hide()override{glfwHideWindow(glfw_win);}
|
||||||
|
|
||||||
|
void SetCaption(const OSString &name) override
|
||||||
|
{
|
||||||
|
#if HGL_OS == HGL_OS_Windows
|
||||||
|
glfwSetWindowTitle(glfw_win,to_u8(name));
|
||||||
|
#else
|
||||||
|
glfwSetWindowTitle(glfw_win,name);
|
||||||
|
#endif//
|
||||||
|
this->caption=name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetSize(int w,int h) override
|
||||||
|
{
|
||||||
|
glfwSetWindowSize(glfw_win,w,h);
|
||||||
|
this->width=w;
|
||||||
|
this->height=h;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MakeToCurrent()override{glfwMakeContextCurrent(glfw_win);}
|
||||||
|
|
||||||
|
void SwapBuffer()override{glfwSwapBuffers(glfw_win);}
|
||||||
|
|
||||||
|
void WaitEvent(const double &time_out)override
|
||||||
|
{
|
||||||
|
if(time_out>0)
|
||||||
|
glfwWaitEventsTimeout(time_out);
|
||||||
|
else
|
||||||
|
glfwWaitEvents();
|
||||||
|
}
|
||||||
|
|
||||||
|
void PollEvent()override{glfwPollEvents();}
|
||||||
|
|
||||||
|
bool IsOpen()override{return(!glfwWindowShouldClose(glfw_win));}
|
||||||
|
};//class RenderWindowGLFW:public RenderWindow
|
||||||
|
|
||||||
|
RenderWindow *CreateRenderWindowGLFW(GLFWwindow *win,bool is_fullscreen)
|
||||||
|
{
|
||||||
|
return(new RenderWindowGLFW(win,is_fullscreen));
|
||||||
|
}
|
||||||
|
}//namespace hgl
|
Loading…
x
Reference in New Issue
Block a user