diff --git a/inc/hgl/render/RenderWindow.h b/inc/hgl/render/RenderWindow.h new file mode 100644 index 00000000..490919b0 --- /dev/null +++ b/inc/hgl/render/RenderWindow.h @@ -0,0 +1,51 @@ +#ifndef HGL_RENDER_WINDOW_INCLUDE +#define HGL_RENDER_WINDOW_INCLUDE + +#include +#include +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 diff --git a/inc/hgl/type/Vertex2.cpp b/inc/hgl/type/Vertex2.cpp new file mode 100644 index 00000000..b732e548 --- /dev/null +++ b/inc/hgl/type/Vertex2.cpp @@ -0,0 +1,147 @@ +#ifndef HGL_VERTEX_2_CPP +#define HGL_VERTEX_2_CPP + +#include +#include + +namespace hgl +{ + template + void Vertex2::Scale(double vx,double vy,double scale) + { + x=vx+(x-vx)*scale; + y=vy+(y-vy)*scale; + } + + template + void Vertex2::Scale(double vx,double vy,double sx,double sy) + { + x=vx+(x-vx)*sx; + y=vy+(y-vy)*sy; + } + + template template + void Vertex2::Scale(const Vertex2 &v,double scale) + { + x=v.x+(x-v.x)*scale; + y=v.y+(y-v.y)*scale; + } + + template template + void Vertex2::Scale(const Vertex2 &v,double sx,double sy) + { + x=v.x+(x-v.x)*sx; + y=v.y+(y-v.y)*sy; + } + + template + void Vertex2::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 + float Vertex2::Length() const + { + return sqrt(x*x+y*y); + } + + template + float Vertex2::Length(float vx,float vy) const + { + float w=x-vx; + float h=y-vy; + + return float(sqrt(double(w*w+h*h))); + } + + template + float Vertex2::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 template + Vertex2 Vertex2::To(const Vertex2 &target,double pos)const + { + if(pos==0)return(*this);else + if(pos==1)return(target);else + { + Vertex2 result; + + result.x=x+(target.x-x)*pos; + result.y=y+(target.y-y)*pos; + + return(result); + } + } + + template + void Vertex2::Normalize() + { + double size=sqrt(x*x+y*y); + + if(size>0) + { + x/=size; + y/=size; + } + + x=0;y=0; + } + + template template + Vertex2 Vertex2::ResizeLength(const Vertex2 &target,double scale)const + { + Vertex2 result; + + result.x=target.x+(x-target.x)*scale; + result.y=target.y+(y-target.y)*scale; + + return(result); + } + + template template + Vertex2 Vertex2::ToLength(const Vertex2 &target,double new_length)const + { + return ResizeLength(target,new_length/Length(target)); + } +}//namespace hgl +#endif//HGL_VERTEX_2_CPP diff --git a/inc/hgl/type/Vertex2.h b/inc/hgl/type/Vertex2.h new file mode 100644 index 00000000..bdd94560 --- /dev/null +++ b/inc/hgl/type/Vertex2.h @@ -0,0 +1,113 @@ +#ifndef HGL_VERTEX_2_INCLUDE +#define HGL_VERTEX_2_INCLUDE + +#include +#include +namespace hgl +{ + /** + * 2D顶点坐标类模板 + */ + template class Vertex2 ///XY 2D顶点坐标类模板 + { + public: + + T x,y; ///<坐标值 + + public: + + Vertex2(){x=y=0;} ///<本类构造函数 + Vertex2(T nx,T ny){x=nx,y=ny;} ///<本类构造函数 + template + Vertex2(const Vertex2 &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 void Flip(const Vertex2 &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 void Scale(const Vertex2 &,double); ///<相对某一点做缩放 + template void Scale(const Vertex2 &,double,double); ///<相对某一点做指定比例的缩放 + + void Rotate(double,double,double); ///<绕指定坐标旋转指定角度 + template void Rotate(const Vertex2 &v,double ang){Rotate(v.x,v.y,ang);} ///<绕指定坐标旋转指定角度 + + void Move(float vx,float vy){x+=vx;y+=vy;} ///<移动 + template + void Scale(const Vertex2 &v,const Vertex2 &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 float Length(const Vertex2 &v)const{return(Length(v.x,v.y));} ///<计算当前点与指定点之间的距离 + template float Angle(const Vertex2 &v)const{return(Angle(v.x,v.y));} ///<计算当前点与指定点之间的角度 + void Normalize(); + + template Vertex2 ResizeLength(const Vertex2 &,double)const; ///<调整这个方向上两点的距离 + template Vertex2 ToLength(const Vertex2 &,double)const; ///<按这个方向将两点的距离调到指定长度 + + template Vertex2 To(const Vertex2 &,double)const; + + public: //操作符重载 + + template bool operator == (const Vertex2 &v)const + { + if(x!=v.x)return(false); + if(y!=v.y)return(false); + return(true); + } + + template bool operator != (const Vertex2 &v)const + { + if(x!=v.x)return(true); + if(y!=v.y)return(true); + return(false); + } + + Vertex2 &operator = (const float *v){x=*v++;y=*v;return *this;} + + template Vertex2 &operator +=(const Vertex2 &v){x+=v.x;y+=v.y;return *this;} + template Vertex2 &operator -=(const Vertex2 &v){x-=v.x;y-=v.y;return *this;} + template Vertex2 &operator *=(const Vertex2 &v){x*=v.x;y*=v.y;return *this;} + template Vertex2 &operator /=(const Vertex2 &v){x/=v.x;y/=v.y;return *this;} + + Vertex2 &operator *=(const float v){x*=v;y*=v;return *this;} + Vertex2 &operator /=(const float v){x/=v;y/=v;return *this;} + + template Vertex2 operator + (const Vertex2 &v)const{return(Vertex2(x+v.x,y+v.y));} + template Vertex2 operator - (const Vertex2 &v)const{return(Vertex2(x-v.x,y-v.y));} + template Vertex2 operator * (const Vertex2 &v)const{return(Vertex2(x*v.x,y*v.y));} + template Vertex2 operator / (const Vertex2 &v)const{return(Vertex2(x/v.x,y/v.y));} + + Vertex2 operator * (const float v)const{return(Vertex2(x*v,y*v));} + Vertex2 operator / (const float v)const{return(Vertex2(x/v,y/v));} + + Vertex2 operator - () const {return(Vertex2(-x,-y));} + + operator T *() const {return(( T *)this);} ///<使得本类可以直接当做T *使用 + operator const T *() const {return((const T *)this);} ///<使得本类可以直接当做const T *使用 + };//class Vertex2 + + typedef Vertex2 Vertex2i; + typedef Vertex2 Vertex2ui; + typedef Vertex2 Vertex2f; + typedef Vertex2 Vertex2d; + + typedef Vertex2 Vertex2b; + typedef Vertex2 Vertex2ub; + typedef Vertex2 Vertex2s; + typedef Vertex2 Vertex2us; +}//namespace hgl +#include +#endif//HGL_VERTEX_2_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 34f54d4c..24414f70 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,2 +1,2 @@ add_subdirectory(RenderDevice) -add_subdirectory(RenderDriver) +# add_subdirectory(RenderDriver) diff --git a/src/RenderDevice/CMakeLists.txt b/src/RenderDevice/CMakeLists.txt index 52c3ec24..c18906cf 100644 --- a/src/RenderDevice/CMakeLists.txt +++ b/src/RenderDevice/CMakeLists.txt @@ -1 +1,2 @@ -add_library(ULRE.RenderDevice STATIC GLFW/RenderDeviceGLFW.cpp) +add_library(ULRE.RenderDevice STATIC GLFW/RenderDeviceGLFW.cpp + GLFW/RenderWindowGLFW.cpp) diff --git a/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp b/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp index f540d54c..5f4a1728 100644 --- a/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp +++ b/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp @@ -4,6 +4,8 @@ namespace hgl { + RenderWindow *CreateRenderWindowGLFW(GLFWwindow *,bool is_fullscreen); + namespace { static RenderDevice *render_device_glfw=nullptr; @@ -169,7 +171,7 @@ namespace hgl 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 @@ -189,7 +191,7 @@ namespace hgl if(!win)return(nullptr); - return(new WindowGLFW(win,true)); + return(CreateRenderWindowGLFW(win,true)); } };//class RenderDevice diff --git a/src/RenderDevice/GLFW/RenderWindowGLFW.cpp b/src/RenderDevice/GLFW/RenderWindowGLFW.cpp new file mode 100644 index 00000000..0dede751 --- /dev/null +++ b/src/RenderDevice/GLFW/RenderWindowGLFW.cpp @@ -0,0 +1,69 @@ +#include +#include + +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