From c4db89843942be83bee413e3fd6fc5f05400d38c Mon Sep 17 00:00:00 2001 From: hyzboy Date: Tue, 27 Nov 2018 20:03:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E5=AE=9A=E4=B9=89glfw?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E7=9A=84render=20device?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 + inc/hgl/render/device/RenderDevice.h | 103 ++++- inc/hgl/type/List.cpp | 482 +++++++++++++++++++++ inc/hgl/type/List.h | 186 ++++++++ inc/hgl/type/ObjectList.cpp | 249 +++++++++++ inc/hgl/type/_Object.h | 6 - src/CMakeLists.txt | 2 + src/RenderDevice/CMakeLists.txt | 1 + src/RenderDevice/GLFW/RenderDeviceGLFW.cpp | 147 ++++++- 9 files changed, 1166 insertions(+), 12 deletions(-) create mode 100644 inc/hgl/type/List.cpp create mode 100644 inc/hgl/type/List.h create mode 100644 inc/hgl/type/ObjectList.cpp create mode 100644 src/CMakeLists.txt create mode 100644 src/RenderDevice/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ae3347c..46c56380 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,4 +19,6 @@ include_directories(inc) add_subdirectory(3rdpty/MathGeoLib) +add_subdirectory(src) + add_subdirectory(example) diff --git a/inc/hgl/render/device/RenderDevice.h b/inc/hgl/render/device/RenderDevice.h index e3632164..128081c0 100644 --- a/inc/hgl/render/device/RenderDevice.h +++ b/inc/hgl/render/device/RenderDevice.h @@ -2,9 +2,96 @@ #define HGL_RENDER_DEVICE_INCLUDE #include -#include +#include + namespace hgl { + /** + * 显示模式数据结构 + */ + struct VideoMode + { + int width; ///<宽 + int height; ///<高 + int bit; ///<色彩位数 + int freq; ///<刷新率 + + int red; ///<红色位数 + int green; ///<绿色位数 + int blue; ///<蓝色位数 + };//struct VideoMode + + /** + * 显示屏数据结构 + */ + struct Display + { + UTF8String name; ///<显示屏名称 + int width,height; ///<显示屏尺寸(单位:毫米) + int x,y; ///<多显示屏时的排列位置 + + public: + + virtual const VideoMode *GetCurVideoMode()=0; + virtual const ObjectList &GetVideoModeList()=0; + }; + + struct WindowSetup + { + UTF8String Name; ///<窗口标题 + +// OSString IconFilename; ///<图标文件名称 +// OSString CursorFilename; ///<光标文件名称 + bool Edge; ///<是否显示边框 + + bool SysMenu; ///<是否显示系统菜单 + bool Right; ///<窗口是否使用靠右风格 + + bool Resize; ///<窗口大小是否可调整 + bool Minimize; ///<窗口是否可以最小化 + bool Maximize; ///<窗口是否可以最大化 + + bool TopMost; ///<永远在最上面 + bool AppTaskBar; ///<程序项在任务栏显示 + }; + + /** + * 渲染设备 + */ + struct RenderSetup + { + uint alpha; /// * 该类是程序与操作系统或其它系统库的访问交接模块 @@ -16,12 +103,18 @@ namespace hgl RenderDevice()=default; virtual ~RenderDevice()=default; + virtual const bool Init()=0; ///<初始化渲染设备 + virtual const void Close()=0; ///<关闭渲染设备 + + virtual const UTF8String GetName()=0; ///<取得设备名称 + + virtual const void GetDisplayList(List &); ///<取得显示屏列表 + virtual const Display * GetDefaultDisplay()=0; ///<取得默认显示屏 + public: - virtual UTF8String ToDebugString() override ///<输出调试用字符串 - { - return UTF8String(U8_TEXT("RenderDevice(),this is BUG,please override this Function.")); - } + virtual RenderWindow *Create(int,int,const WindowSetup *,const RenderSetup *)=0; ///<创建一个窗口渲染设备 + virtual RenderWindow *Create(const Display *,const VideoMode *,const RenderSetup *)=0; ///<创建一个全屏渲染设备 };//class RenderDevice RenderDevice *CreateRenderDeviceGLFW(); ///<创建一个基于GLFW的渲染设备 diff --git a/inc/hgl/type/List.cpp b/inc/hgl/type/List.cpp new file mode 100644 index 00000000..797b103c --- /dev/null +++ b/inc/hgl/type/List.cpp @@ -0,0 +1,482 @@ +#ifndef HGL_LIST_CPP +#define HGL_LIST_CPP + +#include +//-------------------------------------------------------------------------------------------------- +// 代码中的部分memcpy可替换为memmove,但这样会引起CodeGuard/Valgrind之类的内存调试器报错 +//-------------------------------------------------------------------------------------------------- +namespace hgl +{ + template + bool List::Get(int index,T &ti)const + { + if(!items||index<0||index>=count) + return(false); + + memcpy(&ti,items+index,sizeof(T)); + return(true); + } + + template + bool List::Begin(T &ti)const + { + if(!items) + return(false); + + memcpy(&ti,items,sizeof(T)); + return(true); + } + + template + bool List::End(T &ti)const + { + if(!items) + return(false); + + memcpy(&ti,items+count-1,sizeof(T)); + return(true); + } + + template + bool List::Rand(T &ti)const + { + if(!items||count<=0) + return(false); + + memcpy(&ti,items+(lrand48()%count),sizeof(T)); + return(true); + } + + template + bool List::Set(int index,const T &val) + { + if(!items||index<0||index>=count) + return(false); + + memcpy(items+index,&val,sizeof(T));//items[index]=val; + + return(true); + } + + /** + * 向列表中添加一个空数据 + * @return 这个数据的指针 + */ + template + T *List::Add() + { + if(!items) + { + count=1; + max_count=1; + items=hgl_aligned_malloc(1); + + return items; + } + else + { + max_count=power_to_2(count+1); + + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + + ++count; + return(items+(count-1)); + } + } + + /** + * 向列表中添加一个数据对象 + * @param data 要添加的数据对象 + * @return 这个数据的索引号 + */ + template + int List::Add(const T &data) + { + if(!items) + { + count=0; + max_count=1; + items=hgl_aligned_malloc(1); + } + else + { + max_count=power_to_2(count+1); + + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } + + memcpy(items+count,&data,sizeof(T));//items[count]=data; + return(count++); + } + + /** + * 重复向列表中添加一个数据对象 + * @param data 要添加的数据对象 + * @param n 要添加的数据个数 + * @return 这个数据的索引号 + * @return -1 出错 + */ + template + int List::Add(const T &data,int n) + { + if(n<=0)return(-1); + + if(!items) + { + count=0; + max_count=power_to_2(n); + items=hgl_aligned_malloc(max_count); + } + else + { + max_count=power_to_2(count+n); + + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } + + T *p=items; + int result=count; + + for(int i=0;i + int List::Add(const T *data,int n) + { + if(!items) + { + count=0; + max_count=power_to_2(n); + + items=hgl_aligned_malloc(max_count); + } + else + { + max_count=power_to_2(count+n); + + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } + + memcpy(items+count,data,n*sizeof(T)); + + int r=count; + + count+=n; + return(r); + } + + /** + * 清除整个列表 + */ + template + void List::Clear() + { + count=0; + max_count=0; + + if(items) + { + hgl_free(items); + items=0; + } + } + + /** + * 清除整个列表,但不清除缓冲区 + */ + template + void List::ClearData() + { + count=0; + } + + template + void List::DeleteClear() + { + if(count<=0) + return; + + T *p=items; + + while(count--) + { + delete *p; + ++p; + } + + count=0; + } + + /** + * 在列表中查找指定的数据项 + * @param data 要查找的数据项 + * @return 数据在列表中的位置 + */ + template + int List::Find(const T &data)const + { + int n=count; + + while(n--) +// if(items[n]==data)return(n); + if(memcmp(items+n,&data,sizeof(T))==0) + return(n); + + return(-1); + } + + /** + * 删除列表中的指定项,删除后将最后一个数据移到被删除的位置 + * @param index 要删除的数据项的索引值 + * @return 是否成功 + */ + template + bool List::Delete(int index) + { + if(!items||count<=0||index<0||index>=count) + return(false); + + --count; + + if(index + bool List::DeleteMove(int index) + { + if(!items||count<=0||index<0||index>=count) + return(false); + + --count; + + if(index + bool List::Delete(int start,int number) + { + if(start>=count)return(false); + + if(start<0) + { + number+=start; + start=0; + } + + if(start+number>count) + number=count-start; + + if(number<=0)return(false); + + count-=number; + + if(start + bool List::DeleteByValue(const T &dat) + { + const int index=Find(dat); + + if(index==-1) + return(false); + + Delete(index); + return(true); + } + + /** + * 删除列表中的指定项 + * @param data 要删除的数据项 + * @param n 要删除的数据个数 + */ + template + void List::DeleteByValue(const T *data,int n) + { + while(n--) + { + int index=Find(*data); + + ++data; + + if(index!=-1) + Delete(index); + } + } + + /** + * 交换列表中两个数据的位置 + * @param a 第一个数据的位置 + * @param b 第二个数据的位置 + */ + template + void List::Exchange(int a,int b) + { + //T t; + char t[sizeof(T)]; + +// t=items[a]; + +// items[a]=items[b]; +// items[b]=t; + + memcpy(&t,items+a,sizeof(T)); + memcpy(items+a,items+b,sizeof(T)); + memcpy(items+b,&t,sizeof(T)); + } + + /** + * 在列表的指定位置中插入一个数据 + * @param index 插入数据的位置 + * @param data 要插入的数据 + */ + template + void List::Insert(int index,const T &data) + { + if(index<0)index=0; + + if(index(max_count); + } + else + { + max_count=power_to_2(count+1); + + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } + + memmove(items+index+1,items+index,(count-index)*sizeof(T)); + + memcpy(items+index,&data,sizeof(T));//items[index]=data; + + ++count; + } + else + Add(data); + } + + /** + * 移动列表中的某一项到新的位置 + * @param index 要移动的数据位置 + * @param newindex 新的位置 + */ + template + void List::Move(int index,int newindex) + { + if(index==newindex)return; + if(index<=0||index>=count)return; + if(newindex<0)newindex=0; + if(newindex>=count)newindex=count-1; + + //T t; + char t[sizeof(T)]; + + memcpy(&t,items+index,sizeof(T));//t=items[index]; + + if(index + void List::PreMalloc(int new_count) + { + if(max_count>=new_count)return; + + max_count=power_to_2(new_count); + + if(!items) + items=hgl_aligned_malloc(max_count); + else + items=(T *)hgl_realloc(items,max_count*sizeof(T)); + } + + template + void List::SetCount(int new_count) + { + if(count==new_count)return; + + if(new_count<=0) + { + ClearData(); + return; + } + + PreMalloc(new_count); + + count=new_count; + } + + /** + * 复制整个列表 + * @param lt 列表 + */ + template + void List::operator = (const List <) + { + if(lt.count<=0) + { + count=0; + return; + } + + SetCount(lt.count); + + memcpy(items,lt.items,count*sizeof(T)); + } + + template + void List::operator = (const std::initializer_list &l) + { + ClearData(); + + SetCount((int)l.size()); + + memcpy(items,l.begin(),count*sizeof(T)); + } +}//namespace hgl +#endif//HGL_LIST_CPP diff --git a/inc/hgl/type/List.h b/inc/hgl/type/List.h new file mode 100644 index 00000000..439b9570 --- /dev/null +++ b/inc/hgl/type/List.h @@ -0,0 +1,186 @@ +#ifndef HGL_LIST_INCLUDE +#define HGL_LIST_INCLUDE + +#include +#include + +#if defined(__BORLANDC__)||defined(__TURBOC__)||defined(__CODEGEARC__) +#pragma warn -8022 //使hides vitual function警告无效 +#endif// + +namespace hgl +{ + /** + * List类用于保存数据列表。可以在列表中添加、删除、查找、访问和排序数据。 + */ + template class List ///列表处理类 + { + protected: + + int count=0; + int max_count=0; + T *items=nullptr; + + public: //属性 + + int GetCount ()const{return count;} ///<取得列表内数据数量 + virtual void SetCount (int); ///<设置列表内数据数量 + virtual void PreMalloc (int); ///<预分配指定数量的数据空间 + T * GetData ()const{return items;} ///<提供原始数据项 + int GetBytes ()const{return count*sizeof(T);} ///<取得原始数据总字节数 + + public: //方法 + + List(){}; ///<本类构造函数 + List(const List <){operator=(lt);} ///<本类构造函数 + List(const std::initializer_list <){operator=(lt);} + + virtual ~List(){Clear();} ///<本类析构函数 + + virtual T * Add(); ///<添加一个空数据 + virtual int Add(const T &); ///<增加一个数据 + virtual int Add(const T &,int n); ///<重复增加一个数据 + virtual int Add(const T *,int n); ///<增加一批数据 + int Add(const List &l){return Add(l.items,l.count);} ///<增加一批数据 + + virtual void Clear(); ///<清除所有数据 + virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 + virtual int Find(const T &)const; ///<查找指定数据的索引 + bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在 + virtual bool Delete(int); ///<删除指定索引的数据 + virtual bool Delete(int,int); ///<删除指定索引的数据 + virtual bool DeleteMove(int); ///<删除指定索引的数据,将后面紧邻的数据前移 + virtual bool DeleteByValue(const T &); ///<删除一个指定数据 + virtual void DeleteByValue(const T *,int); ///<删除一批指定的数据 + virtual void Exchange(int,int); ///<根据索引交换两个数据 + virtual void Insert(int,const T &); ///<在指定索引处插入一个数据 + virtual void Move(int,int); ///<移动一个数据到移指索引处 + + void DeleteClear(); ///<清除所有数据并全部调用delete + + virtual void operator = (const List &); ///<操作符重载复制一个列表 + virtual void operator = (const std::initializer_list &l); + + virtual void operator += (const T &obj){Add(obj);} ///<操作符重载添加一个数据 + virtual void operator << (const T &obj){Add(obj);} ///<操作符重载添加一个数据 + virtual void operator -= (const T &obj){DeleteByValue(obj);} ///<操作符重载删除一个数据 + + bool Get(int,T &)const; ///<取得指定索引处的数据 + bool Set(int,const T &); ///<设置指定索引处的数据 + bool Rand(T &)const; ///<随机取得一个数据 + + virtual bool Begin(T &)const; ///<取第一个数据 + virtual bool End(T &)const; ///<取最后一个数据 + + virtual void Enum(void (*enum_func)(T &)) ///<枚举所有数据成员 + { + T *obj=items; + + for(int i=0;i class List + + template T *GetObject(const List &list,const int index) + { + T *obj; + + if(list.Get(index,obj)) + return(obj); + + return(nullptr); + } +}//namespace hgl + +#include +//-------------------------------------------------------------------------------------------------- +namespace hgl +{ + /** + * 自定义对象列表处理类与标准列表处理类的区别在于它对数据生成/清除时会多调用虚拟函数Create/Delte + */ + template class CusObjectList:public List ///对象列表处理类 + { + virtual void SetCount(int); + +// virtual T * CreateObject()=0; ///<创建一个数据,但不加入列表 + virtual void DeleteObject(T *)=0; ///<删除一个数据 + + public: + + typedef T *ItemPointer; + + public: //方法 + + CusObjectList(){} + virtual ~CusObjectList(); + + public: + +// virtual T * Append(); ///<追加一个数据 +// virtual T * Insert(int); ///<在指定索引处创建一个数据 + void Insert(int,const ItemPointer &); ///<在指定索引处插入一个数据 + + virtual void Clear(); ///<清除所有数据 + virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 + + virtual bool Unlink(int index){return List::Delete(index);} ///<将指定索引处的数据与列表断开 + virtual bool UnlinkMove(int index){return List::DeleteMove(index);} ///<将指定索引处的数据与列表断开,将前移后面的数据 + virtual bool Unlink(int start,int number){return List::Delete(start,number);} ///<将指定索引处的数据与列表断开 + virtual bool UnlinkByValue(const ItemPointer &ip){return List::DeleteByValue(ip);} ///<将一个指定数据与列表断开 + virtual void UnlinkByValue(const ItemPointer *ip,int n){List::DeleteByValue(ip,n);} ///<将一批指定数据与列表断开 + virtual void UnlinkAll(){List::ClearData();} ///<断开所有数据 + + virtual bool Delete(int); ///<删除指定索引处的数据 + virtual bool DeleteMove(int); ///<删除指定索引处的数据 + virtual bool DeleteByValue(const ItemPointer &); ///<删除指定的一个数据 + virtual void DeleteByValue(const ItemPointer *,int); ///<删除指定的一批数据 + virtual void DeleteAll(); ///<删除所有数据 + + virtual ItemPointer &operator[](int n)const ///<操作符重载取得指定索引处的数据 + { + static T *null_pointer=nullptr; + + if(n<0||n>=this->count) + return(null_pointer); + + return this->items[n]; + } + + virtual void Enum(void (*enum_func)(T *)) ///<枚举所有数据成员 + { + T **obj=this->items; + + for(int i=0;icount;i++) + { + enum_func(*obj); + + ++obj; + } + } + };//template class CusObjectList + + /** + * 对象列表处理类
+ * 将自定义对象列表中的Create重载为new,Delete重载为delete + */ + template class ObjectList:public CusObjectList + { + private: + +// virtual T * CreateObject(){return(new T);} ///<创建一个数据 + virtual void DeleteObject(T *obj){if(obj)delete obj;} ///<删除一个数据 + + public: + + virtual ~ObjectList() + { + CusObjectList::Clear(); + } + };//class ObjectList +}//namespace hgl +#include +#endif//HGL_LIST_INCLUDE diff --git a/inc/hgl/type/ObjectList.cpp b/inc/hgl/type/ObjectList.cpp new file mode 100644 index 00000000..96f4b6bc --- /dev/null +++ b/inc/hgl/type/ObjectList.cpp @@ -0,0 +1,249 @@ +#ifndef HGL_OBJECT_LIST_CPP +#define HGL_OBJECT_LIST_CPP + +#include +namespace hgl +{ + /** + * 对象列表析构函数,会调用DeleteAll函数 + */ + template + CusObjectList::~CusObjectList() + { + Clear(); + } + +// /** +// * 生成一个对象,并返回它的指针 +// */ +// template +// T *CusObjectList::Append() +// { +// if(!this->items) +// { +// this->max_count=1; +// this->items=(T **)hgl_aligned_malloc(1); +// } +// else +// { +// if(this->count>=this->max_count) +// this->max_count<<=1; +// +// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); +// } +// +// return(this->items[this->count++]=CreateObject()); +// } + +// /** +// * 在指定位置插入一个对象 +// */ +// template +// T *CusObjectList::Insert(int index) +// { +// if(index<0)index=0; +// +// if(indexcount) +// { +// if(this->count>=this->max_count) +// this->max_count<<=1; +// +// this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); +// +// memmove(this->items+index+1,this->items+index,(this->count-index)*sizeof(T *)); +// +// this->count++; +// +// return(this->items[index]=CreateObject()); +// } +// +// return(Append()); +// } + + /** + * 在指定位置插入一个对象 + */ + template + void CusObjectList::Insert(int index,const ItemPointer &obj) + { + List::Insert(index,obj); + } + + /** + * 清除所有对象,作用和DeleteAll一样 + */ + template + void CusObjectList::Clear() + { + DeleteAll(); + List::Clear(); + } + + /** + * 清除所有对象,但不释放内存 + */ + template + void CusObjectList::ClearData() + { + DeleteAll(); + List::ClearData(); + } + + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param index 要删除的对象的索引值 + * @return 是否成功 + */ + template + bool CusObjectList::Delete(int index) + { + if(index>=0&&indexcount) + { + DeleteObject(this->items[index]); + + this->count--; + + if(indexcount) + memcpy(this->items+index,this->items+this->count,sizeof(T *)); + + return(true); + } + else + return(false); + } + + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param index 要删除的对象的索引值 + * @return 是否成功 + */ + template + bool CusObjectList::DeleteMove(int index) + { + if(index>=0&&indexcount) + { + DeleteObject(this->items[index]); + + this->count--; + + if(indexcount) + memmove(this->items+index,this->items+index+1,(this->count-index)*sizeof(T *)); + + return(true); + } + else + return(false); + } + + /** + * 删除列表中的指定项 + * + * 这个函数在删除指定对象时,附加使用delete方法 + * @param obj 要删除的对象 + * @return 是否成功 + */ + template + bool CusObjectList::DeleteByValue(const ItemPointer &obj) + { + int n=this->count; + + while(n--) + { + if(this->items[n]==obj) + { + DeleteObject(this->items[n]); + + this->count--; + + if(ncount) + memmove(this->items+n,this->items+n+1,(this->count-n)*sizeof(T *)); + + return(true); + } + } + + return(false); + } + + /** + * 将一批对象从列表中删除 + * @param obj 要删除的对象 + * @param n 要删除的对象个数 + */ + template + void CusObjectList::DeleteByValue(const ItemPointer *obj,int n) + { + while(n--) + { + int index=List::Find(*obj); + + obj++; + + if(index!=-1) + Delete(index); + } + } + /** + * 删除整个列表中的所有对象 + * + * 这个函数在删除每一个对象时,都会使用一次delete + */ + template + void CusObjectList::DeleteAll() + { + if(this->count) + { + int n=this->count; + + while(n--) + DeleteObject(this->items[n]); + + this->count=0; + } + } + + template + void CusObjectList::SetCount(int new_count) + { + if(this->count==new_count)return; + + if(new_count<=0) + { + DeleteAll(); + } + else + { + this->max_count=power_to_2(new_count); + + if(this->items) + { + if(new_count>this->count) + { + this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); + +// for(;this->countcount++) +// this->items[this->count]=CreateObject(); + } + else + { + while(this->count-->new_count) + DeleteObject(this->items[this->count]); + + this->items=(T **)hgl_realloc(this->items,this->max_count*sizeof(T *)); + } + } +// else +// { +// this->items=(T **)hgl_aligned_malloc(this->max_count); +// +// while(new_count--) +// this->items[this->count++]=CreateObject(); +// } + } + } +}//namespace hgl +#endif//HGL_OBJECT_LIST_CPP diff --git a/inc/hgl/type/_Object.h b/inc/hgl/type/_Object.h index 4ff67ec4..3c2e963a 100644 --- a/inc/hgl/type/_Object.h +++ b/inc/hgl/type/_Object.h @@ -1,8 +1,6 @@ #ifndef HGL__OBJECT_INCLUDE #define HGL__OBJECT_INCLUDE -#include - namespace hgl { /** @@ -13,10 +11,6 @@ namespace hgl public: virtual ~_Object()=default; ///<本类析构函数 - - public: //调试用 - - virtual UTF8String ToDebugString(){} ///<输出调试用字符串 };//class _Object typedef void (_Object::*ObjectMemberFunc)(); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 00000000..3c98883f --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,2 @@ + +add_subdirectory(RenderDevice) diff --git a/src/RenderDevice/CMakeLists.txt b/src/RenderDevice/CMakeLists.txt new file mode 100644 index 00000000..52c3ec24 --- /dev/null +++ b/src/RenderDevice/CMakeLists.txt @@ -0,0 +1 @@ +add_library(ULRE.RenderDevice STATIC GLFW/RenderDeviceGLFW.cpp) diff --git a/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp b/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp index e02abfc9..88de9016 100644 --- a/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp +++ b/src/RenderDevice/GLFW/RenderDeviceGLFW.cpp @@ -1 +1,146 @@ - +#include +#include +#include + +namespace hgl +{ + namespace + { + static RenderDevice *render_device_glfw=nullptr; + + void glfw_error_callback(int err,const char *desc) + { + std::cerr<<"glfw return error["< video_mode_list; + + public: + + const VideoMode *GetCurVideoMode()override{return cur_video_mode;} + const ObjectList &GetVideoModeList()override{return video_mode_list;} + }; + + VideoMode *ConvertVideoMode(const GLFWvidmode *mode) + { + if(mode) + return nullptr; + + VideoMode *vm=new VideoMode; + + vm->width =mode->width; + vm->height =mode->height; + vm->freq =mode->refreshRate; + + vm->red =mode->redBits; + vm->green =mode->greenBits; + vm->blue =mode->blueBits; + + vm->bit =vm->red+vm->green+vm->blue; + + return vm; + } + + DisplayGLFW *GetDisplayAttrib(GLFWmonitor *gm) + { + DisplayGLFW *m=new DisplayGLFW; + + m->glfw_monitor=gm; + m->name=glfwGetMonitorName(gm); + + glfwGetMonitorPhysicalSize(gm,&(m->width),&(m->height)); + glfwGetMonitorPos(gm,&(m->x),&(m->y)); + + { + const GLFWvidmode *mode=glfwGetVideoMode(gm); + + m->cur_video_mode=ConvertVideoMode(mode); + } + + { + int count; + + const GLFWvidmode *ml=glfwGetVideoModes(gm,&count); + + for(int i=0;ivideo_mode_list.Add(ConvertVideoMode(ml+i)); + } + + return m; + } + } + + class RenderDeviceGLFW:public RenderDevice + { + DisplayGLFW *default_display=nullptr; + + public: + + RenderDeviceGLFW() + { + render_device_glfw=this; + } + + ~RenderDeviceGLFW() + { + glfwTerminate(); + render_device_glfw=nullptr; + } + + const bool Init()override{return true;} + const void Close()override{}; + + const UTF8String GetName()override ///<取得设备名称 + { + return UTF8String("GLFW")+UTF8String(glfwGetVersionString()); + } + + const void GetDisplayList(List &disp_list) ///<取得显示屏列表 + { + int count=0; + + GLFWmonitor **ml=glfwGetMonitors(&count); + + for(int i=0;i