線程 Joining and Detaching相關函數4個
pthread_joinpthread_detach
pthread_attr_setdetachstate
pthread_attr_getdetachstate
Joining的作用
Joining是線程同步的方法之一
當且僅當線程被設置為joinable時其可被Joining
一個例子:
#include <pthread/pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4
void *BusyWork(void *t)
{
int i;
long tid;
double result=0.0;
tid = (long)t;
printf("Thread %ld starting
\n",tid);
for (i=0; i<1000000; i++)
{
result = result + sin(i) * tan(i);
}
printf("Thread %ld done. Result = %e\n",tid, result);
pthread_exit((void*) t);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc;
long t;
void *status;
//! 設置線程屬性
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(t=0; t<NUM_THREADS; t++)
{
printf("Main: creating thread %ld\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create()is %d\n", rc);
exit(-1);
}
}
//! 銷毀線程屬性
pthread_attr_destroy(&attr);
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_join(thread[t], &status);
if (rc)
{
printf("ERROR; return code from pthread_join()is %d\n", rc);
exit(-1);
}
printf("Main: completed join with thread %ld having a status of %ld\n",t,(long)status);
}
printf("Main: program completed. Exiting.\n");
pthread_exit(NULL);
}
1.使用pthread的理由
2個字:簡單
2.pthread組件
thread,
mutex,
condition var
synchronization
3.線程的終止和產生
小例:
#include <pthread/pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++)
{
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
return 0;
}
通過pthread_create來創建線程
其第一個參數為線程id,第二個為線程屬性,第三個為線程函數,最后一個為數據參數
那么線程如何終止呢:
1.從運行函數終止
2.調用pthread_exit終止
3.調用pthread_cance
4.進程終止
向線程傳遞參數
#include <pthread/pthread.h>
#include <stdio.h>
#include <string>
#include <iostream>
#define NUM_THREADS 5
struct Data
{
std::string name;
};
Data* data_impl;
void *PrintHello(void* data_ptr)
{
struct Data* data;
data = (Data*)data_ptr;
std::cout<<data->name<<std::endl;
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
data_impl = new Data[NUM_THREADS];
data_impl[0].name = "T1";
data_impl[1] .name= "T2";
data_impl[2] .name= "T3";
data_impl[3] .name= "T4";
data_impl[4] .name= "T5";
for(int t=0; t<NUM_THREADS; t++)
{
int rc = pthread_create(&threads[t], NULL, PrintHello,&data_impl[t]);
}
pthread_exit(NULL);
delete []data_impl;
return 0;
}
其他相關線程庫:
1.zthread,
2.opentherad
3.boost therad
4.原生態的平臺線程函數
boost是一個很好的庫
但是并不是完美的
在使用的時候需要詳細注意
std::string tag;
boost::tokenizer<> tok(std::string("貓 狗 豬"));
for(boost::tokenizer<>::iterator beg=tok.begin();beg!=tok.end();++beg)
{
tag += *beg;
tag += "+";
}
std::cout<<tag<<std::endl;
大家說說結果吧
1.原DLL分為2個gcore和device(這樣可以在其他軟件中使用前者)
2.支持xp,vista,win7,linux操作系統
3.支持msvc7.1,8.0,9.0,mingw,bcb,dev++,codeblock編譯器系列
4.提供方便易用的xml序列化框架一個
5.提供可插式圖形插件接口
6.移除原有的RefPtr對象改用boost系列智能指針
7.移除原有的Manager對象
8.修改了filesystem接口
9.在UI中增加了柵格主題,UI工廠,UI_ProgressBar增加了進度的顯示
10.粒子系統中增加了粒子工廠
11.移除了原有的Cursor類
12.移除線程類對象(改用boost thread.)
13.增加了Helper文件以方便用戶
14.增加了Sprite2對象
蓋莫引擎2.3.0算是做完了
不過這個做的很不盡人意(不過沒關系咱慢慢做只要堅持O(∩_∩)O~)
一個問題就是引擎中的RefPtr設計有點問題
索性下個引擎版本就是用boost庫吧
免得一些基礎需要自己寫
要使用boost庫其中的智能指針必不可少
林林總總有好幾個類型
不過常用得還是3個
scoped_ptr,
shared_ptr,
weak_ptr
這里先說shared_ptr
它還是引用計數類型的指針咯
貌似比較好用(但是根本還是在于使用的人)
為了正確使用它需要注意幾個問題
1.多線程環境和循環引用
應該配合weak_ptr
2.使用了shared_ptr就不應該使用其他內存管理機制
3.不要構造臨時的shared_tr作為函數參數
4.其他...
摘要: Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->【基本介紹】蓋莫音頻引擎SDK1.6.0(類似fmod音頻api)原本為蓋莫游戲引擎開發當前支持win7,xmp,vista操作系統支持10種音頻格式:mp3,ogg,wav,au,aiff,mod,it...
閱讀全文
接上篇
我們接著說UI部分的控件基類
控件基類應該具備的要素
1.按名生成
2.控件尺寸
3.控件大小
4.控件狀態
5.對消息事件的處理
6.控件渲染
7.控件檢測
8.控件文本
然后具體代碼如下:
///////////////////////////////////////////////////////////
/// 定義UI控件基類
///////////////////////////////////////////////////////////
class GAPI UI_Widget : public SlotHolder,public UI_EventHandler,public Object
{
public:
typedef std::list<UI_EventListener*> UIEventListener;
typedef std::list<UI_EventListener*>::iterator UIEventListenerItr;
public:
////////////////////////////////////////////////////////
/// 窗體構造和析構
////////////////////////////////////////////////////////
explicit UI_Widget(UI_Widget* parent = NULL,const engine_wstring& text = L"widget");
virtual ~UI_Widget();
public:
////////////////////////////////////////////////////////
/// 獲取,設置設置窗體文本
////////////////////////////////////////////////////////
engine_wstring GetText()const{return text_;}
void SetText(const engine_wstring& text);
public:
////////////////////////////////////////////////////////
/// 窗體大小和尺寸
////////////////////////////////////////////////////////
Size GetSize()const;
void SetSize(const Size& size);
Point GetPosition()const;
void SetPosition(const Point& point);
////////////////////////////////////////////////////////
/// 獲取窗體推薦大小
////////////////////////////////////////////////////////
virtual Size GetRecommendedSize()const;
public:
////////////////////////////////////////////////////////
/// 檢測給定點下控件
////////////////////////////////////////////////////////
UI_Widget* GetWidgetBelow(int x,int y);
////////////////////////////////////////////////////////
/// 獲取本控件的頂層控件
////////////////////////////////////////////////////////
UI_Widget* GetTopWidget()const;
////////////////////////////////////////////////////////
/// 獲取,設置父窗體
////////////////////////////////////////////////////////
UI_Widget* GetParentWidget()const;
void SetParentWidget(UI_Widget* parent);
public:
////////////////////////////////////////////////////////
/// 設置,獲取控件邊框
////////////////////////////////////////////////////////
void SetBorder(const RefPtr<UI_AbstractBorder>& border);
RefPtr<UI_AbstractBorder> GetBorder()const;
////////////////////////////////////////////////////////
/// 設置,獲取是否渲染邊框
////////////////////////////////////////////////////////
void SetBorderVisible(bool visible);
bool IsBorderVisible();
public:
////////////////////////////////////////////////////////
/// 增加,移除事件消息
////////////////////////////////////////////////////////
void AddEventListener(UI_EventListener* listener);
void RemoveEventListener(UI_EventListener* listener);
////////////////////////////////////////////////////////
/// 消息處理
////////////////////////////////////////////////////////
virtual bool Process(const UI_Event& event);
public:
////////////////////////////////////////////////////////
/// 焦點函數
////////////////////////////////////////////////////////
bool IsFocusOn()const;
void SetFocusOn(bool focus);
void ChangedFocusOn();
////////////////////////////////////////////////////////
/// 設置,檢測是否為活動控件
////////////////////////////////////////////////////////
void SetAsActiveWidget();
bool IsActiveWidget()const;
////////////////////////////////////////////////////////
/// 檢測控件層次關系
////////////////////////////////////////////////////////
virtual bool DoesHierarchyContain(UI_Widget* widget)const;
public:
////////////////////////////////////////////////////////
/// 顯示,隱藏窗體
////////////////////////////////////////////////////////
void SetVisible(bool visible);
void Show();
void Hide();
bool IsVisible()const;
////////////////////////////////////////////////////////
/// 窗體狀態函數
////////////////////////////////////////////////////////
void IsEnable(){enabled_ = true;}
void Disable(){enabled_ = false;}
bool IsEnabled()const{return enabled_;}
public:
////////////////////////////////////////////////////////
/// 窗體渲染
////////////////////////////////////////////////////////
void Render();
////////////////////////////////////////////////////////
/// 當窗體關閉的時候發射信號
////////////////////////////////////////////////////////
Signal0 closed_;
////////////////////////////////////////////////////////
/// 窗體數據載入
////////////////////////////////////////////////////////
virtual bool Load(const engine_string& file,const engine_string& widget);
protected:
////////////////////////////////////////////////////////
/// 執行函數
////////////////////////////////////////////////////////
virtual void OnResize(){}
virtual void OnMove(){}
virtual void OnTextChanged(){}
virtual void OnShow(){}
virtual void OnHide(){}
virtual void OnGainedFocus(){}
virtual void OnLostFocus(){}
virtual void OnDraw(){}
private:
UI_Widget* parent_;
UIEventListener message_listeners_;
engine_wstring text_;
Size size_;
Point position_;
bool visible_;
bool enabled_;
RefPtr<UI_AbstractBorder> border_;
bool border_visible_;
private:
DECLARE_OBJECT(UI_Widget)
};
}
#ifdef G_COMPILER_MSVC
#pragma warning(pop)
#endif
需要說明的就是這里有3個父類
一個是Object
一個是SlotHolder這是消息樁
另外一個是UI_EventHandler負責對事件的處理
需要說明的是UI_Widget總是和UI_WidgetManager配合使用的
畢竟UI上下文最大只能有一個活動控件
接上文:http://www.shnenglu.com/gaimor/archive/2010/09/30/128134.html
本文我們接著說UI庫的消息事件部分:
1.UI基本事件類型
基本上就下面幾種:
////////////////////////////////////////////////////////////
/// 枚舉UI消息類型
////////////////////////////////////////////////////////////
enum UI_EVENT
{
//! 鼠標移動
UI_EVENT_MOUSE_MOVE = 0,
//! 鼠標點擊
UI_EVENT_MOUSE_CLICK,
//! 鼠標進入
UI_EVENT_MOUSE_ENTERED,
//! 鼠標退出
UI_EVENT_MOUSE_EXITED,
//! 鼠標滾輪事件
UI_EVENT_MOUSE_WHEEL,
//! 字符輸入
UI_EVENT_CHAR_INPUT,
//! 鍵盤按鍵
UI_EVENT_KEY_PRESS,
//! 按鍵退出
UI_EVENT_KEY_RELEASE,
//! 焦點事件
UI_EVENT_LOST_FOCUSE,
UI_EVENT_GAIN_FOCUSE,
//! 滑塊事件
UI_EVENT_SLIDER_MOVE,
//! 編輯事件
UI_EVENT_EDIT,
//! 選擇,反選擇
UI_EVENT_SELECTED,
UI_EVENT_DESELECTED
};
2.
關于事件一般就3個相關對象:
事件,消息聽者和消息處理對象3個單元塊:
如下所示:
///////////////////////////////////////////////////////////
/// 定義引擎事件基類模板
///////////////////////////////////////////////////////////
template<class EventType = int>
class Event : NonCopyable
{
public:
///////////////////////////////////////////////////////
/// 事件基類構造函數
///////////////////////////////////////////////////////
Event(const EventType& type):type_(type){}
////////////////////////////////////////////////////////
/// 事件基類析構函數
////////////////////////////////////////////////////////
virtual ~Event(){}
////////////////////////////////////////////////////////
/// 獲取事件類型
////////////////////////////////////////////////////////
EventType GetEventType()const{return type_;}
private:
////////////////////////////////////////////////////////
/// 數據成員變量
////////////////////////////////////////////////////////
EventType type_;
};
////////////////////////////////////////////////////////////
/// 定義事件聽者基類
////////////////////////////////////////////////////////////
template<class Event,class Target>
class EventListener
{
public:
////////////////////////////////////////////////////////
/// 事件聽者虛析構函數
////////////////////////////////////////////////////////
virtual ~EventListener(){}
////////////////////////////////////////////////////////
/// 消息派送
////////////////////////////////////////////////////////
virtual bool Dispatch(const Event& message,Target object) = 0;
};
////////////////////////////////////////////////////////////
/// 定義事件處理者基類
////////////////////////////////////////////////////////////
template<class Event,class EventListener>
class EventHandler
{
public:
////////////////////////////////////////////////////////
/// 事件聽者虛析構函數
////////////////////////////////////////////////////////
virtual ~EventHandler(){}
////////////////////////////////////////////////////////
/// 消息處理
////////////////////////////////////////////////////////
virtual bool Process(const Event& message){return false;}
////////////////////////////////////////////////////////
/// 增加,移除事件消息
////////////////////////////////////////////////////////
virtual void AddEventListener(EventListener* listener){}
virtual void RemoveEventListener(EventListener* listener){}
};
然后便是模板實例:
typedef Event<UI_EVENT> UI_Event;
typedef EventListener<UI_Event,UI_Widget*> UI_EventListener;
typedef EventHandler<UI_Event,UI_EventListener*> UI_EventHandler;
說實話可以不這樣做而是用boost::function之類的函數綁定
但是沒法子這樣寫我習慣了 呵呵
2.下面是UI事件的承接部分:
通過UI管理器承接輸入輸出系統的消息響應:
{
////////////////////////////////////////////////////////////
/// 蓋莫GUI管理器
////////////////////////////////////////////////////////////
class GAPI UI_WidgetManager : public UI_EventListenerImpl
public:
////////////////////////////////////////////////////////
/// 按鍵處理
////////////////////////////////////////////////////////
bool OnMouseLeftDown(int x,int y);
bool OnMouseLeftUp(int x,int y);
bool OnMouseMiddleDown(int x,int y);
bool OnMouseMiddleUp(int x,int y);
bool OnMouseRightDown(int x,int y);
bool OnMouseRightUp(int x,int y);
bool OnMouseMove(int x,int y);
bool OnChar(wchar_t code);
bool OnKeyDown(int code);
bool OnKeyUp(int code);
bool OnMouseWheel(int z);
public:
3.事件生成:
以上2部分分別是UI事件對象系列和UI事件輸入部分
下面設計UI事件的生成
無論是鼠標還是鍵盤事件實際上都相當于生成了一個新的事件
舉例如下:
OnMouseLeftDown(int x,int y)
如果該函數被調用
那么就說明鼠標的左鍵被點擊同時我們還知道了點擊的位置坐標
這樣就生成了一個UI_MouseClickEvent
對象
不過這里需要考慮是鼠標雙擊還是單擊
這就要考慮本次點擊和上次點擊的時間間隔了
這樣就生成了一個UI鼠標事件
那本事件應該傳給誰?
應該是目標對象
直觀一點應該是傳給鼠標當前位置下的控件對象
但是考慮到實際情況
這里有一個聚焦控件和活動控件的概念
比如說我們打開一個對話框(這是一個聚焦控件)
對話框上有1個按鍵
當用戶點擊本按鍵則消息發送給這個按鍵了
但是當鼠標移出對話框之外
一般情況其他控件此時處于非活動狀態
消息應該發送給聚焦控件
所以這里至少有3各類型的控件指針分別為:聚焦控件,活動控件以及鼠標下控件(當然他們可以是同一控件)
在這里需要說明這三個控件是這樣切換的
如果發生特定的uI事件則修改當前的聚焦控件等對象了
4.具體控件對消息的處理:
生成特定消息,并發送給相應的控件對象之后那么控件就需要相應該消息了:
控件對象的相關函數:
///////////////////////////////////////////////////////////
/// 定義UI控件基類
///////////////////////////////////////////////////////////
class GAPI UI_Widget : public SlotHolder,public UI_EventHandler,public Object
{
public:
typedef std::list<UI_EventListener*> UIEventListener;
typedef std::list<UI_EventListener*>::iterator UIEventListenerItr;
void RemoveEventListener(UI_EventListener* listener);
////////////////////////////////////////////////////////
/// 消息處理
////////////////////////////////////////////////////////
virtual bool Process(const UI_Event& event)
在這里我們通過Process函數來接受UI管理器傳過來的消息對象.
這里是想要的處理
注意我們并不直接根據消息響應控件的各種狀態!
而是通過迭代消息聽者鏈表的
如下:
bool UI_Widget::Process(const UI_Event& event)
{
bool ret = false;
if(IsVisible() && IsEnabled())
{
UIEventListenerItr it;
for(it = message_listeners_.begin();it != message_listeners_.end();++it)
{
UI_EventListener* listener = *it;
ret = ret || listener->Dispatch(event,this);
}
//! 處理控件邊框事件
if(border_)
border_->Process(event);
}
return ret;
}
當消息傳來之后我們并不能確定這就是本控件所需要的消息需要驗證它
如何驗證?
就看當前控件是不是可顯示和活動的咯
同時如何控件有邊框對象我們則把消息發給它以改變可能的邊框外觀
下篇:UI設計概要4:UI控件對象
接上文:http://www.shnenglu.com/gaimor/archive/2010/09/27/127900.html
第一篇是關于UI布局管理器的文章
本文主要說說引擎UI部分組件的問題
在這里UI組件是指UI中的各個基礎模塊而非UI控件也
在本引擎之中當前UI組件有以下幾個基礎單元
1.UI_Widget UI控件的基類
2.UI_AbstractBorder UI控件邊框類
3.UI_AbstractLayouter UI布局管理器類
4.UI_Event/UI_EventHandler,UI_EventListerner UI事件處理家族
5.UI_Brush UI庫畫筆(當前基于OpenGL)
6.UI_AbstractTheme UI庫控件主題
7.UI_ProptyScheme UI庫主題對象
8.UI_WidgetManager UI庫管理器
9.UI_Factory UI庫工廠對象
10.其他通用對象
以上所有對象構成了UI庫的框架
下面說下各個部分的功能
1.UI_Widget UI基本控件 所有控件的基類
2.UI_AbstractBorder 控件控件對象邊框外觀
3.UI_AbstractTheme 控件主題
以上3著構成所見控件外觀
每一個控件都有一個主題和邊框對象
所有同質控件都采用統一的控件主題以保證統一的外觀
當前UI_Border設計了2中 一種為線性邊框一種為狀態邊框
當前主題設計了3中分別為簡單主題,位圖主題和矩形主題
UI布局管理器上文已經說過當前不再贅述
UI事件處理這塊內容比較多 下文說吧
UI畫筆的作用是繪制對象元素比如繪制線條,矩陣設置顏色等等很簡單的咯
UI_PertptyShceme是一個控件主題的集合包含了所有控件類型的主題
在渲染控件的時候調用對等的主題繪制控件
控件管理器是UI庫的中樞
負責統一渲染隊列控件和調度輸入輸出事件
關于UI工廠則負責按名生成控件指針咯
我一直力圖把UI庫的實際簡單化
但是總是做不到
而且越做越復雜的
注意當前并沒有涉及腳本,
UI編輯器以及完成所有控件對象的開發
關于腳本我想采用得到腳本有lua,anglescrip或者自定義腳本
關于編輯器我想采用wxwidget or qt
雖然很慢但是我一直在努力 呵呵
下篇:UI設計概要3:UI消息
一直想把引擎的UI部分做強做大可是發現這個難度還是不小
所以只能一步一步蠶食之
一步一步來吧
先說下控件布局管理器吧
布局管理器的目的是按照一定規則排列控件于給定范圍內
這是其基本目的
很顯然基本的布局管理對象應該包含以下2個基本功能
1.控件的加入和刪除
2.應用布局管理器
當然也可以有其他附加功能
比如獲取控件個數等等
所以我把抽象控件布局對象設計如下:
///////////////////////////////////////////////////////////
/// 定義引擎UI布局管理器
///////////////////////////////////////////////////////////
class GAPI UI_AbstractLayouter : public Object
{
public:
typedef std::list<UI_Widget*> Widgets;
public:
UI_AbstractLayouter();
virtual ~UI_AbstractLayouter();
public:
////////////////////////////////////////////////////////////
/// 布局管理器加載控件
////////////////////////////////////////////////////////////
void AddWidget(UI_Widget* widget);
////////////////////////////////////////////////////////////
/// 布局管理器移除控件
////////////////////////////////////////////////////////////
void RemoveWidget(UI_Widget* widget);
////////////////////////////////////////////////////////////
/// 布局管理器移除所有子控件
////////////////////////////////////////////////////////////
void RemoveAllWidget();
////////////////////////////////////////////////////////////
/// 布局管理器控件排列
////////////////////////////////////////////////////////////
virtual void ApplyLayouter(UI_Widget* parent) = 0;
protected:
Widgets widgets_;
private:
DECLARE_OBJECT(UI_AbstractLayouter)
};
然后就是具體的幾種布局管理器對象了
下面是我當前弄的4種布局管理器了
分別為:
UI_FlowLayouter
流式布局管理的布局策略是從左到右依次設定控件位置如果超出父控件則按照設定的豎直布局換行重新布局(注意它并不改變控件的大?。?br>UI_GridLayouter
格子布局管理的策略則是工具設定布局間隔和子控件,父親控件個數重寫設置所有的子控件大小(這樣所有的子控件大小就一樣了)然后把所有的子控件放置在一個一個的格子中
UI_VLayouter
UI_VLayouter和UI_HLayouter則是我從QT中借鑒來的
IU_VLayouter把所有子控件按照從上到下的方式依次布局(不改變子控件大小)(可能會超出父控件喔)
UI_HLayouter
這個就不說了
當然還可以定制其他布局管理器了
比如UI_DockLayouter
UI_CenterLayouter等等基本原理都是一樣的
做了游戲引擎這么久說實話
UI真是一個大塊頭
對于UI我的設計理念就是使用簡單,功能豐富
題外話1:08年11月的時候我就想設計一套UI庫
之前認為游戲UI是使用windows API話的 呵呵
題外話2:網上關于使用一些UI庫的例子很多但是關于設計UI庫的例子似乎不多
題外話3:說實話我覺得寫博客是交流思想共同探討而非其他