📄 基于 linux 和 minigui 的嵌入式系统软件开发指南(六).htm
字号:
tip=Game&of&Minesweaper
icon=res/kmines.gif
[app2]
path=../controlpanel/
name=controlpanel
layer=
tip=Control&Panel
icon=res/kcmx.gif
</CODE></PRE></TD></TR></TBODY></TABLE></P>
<P>其中的 [mginit] 段记录了应用程序个数(nr键),以及自动启动的应用程序索引(autostart键)。而 [appX]
段记录了每个应用程序的信息,包括该应用程序的路径、名称、图标等等。下面的代码演示了如何使用 MiniGU的配置文件函数获取这些信息(该代码段来自
mde 演示包中的 mginit 程序):
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1><TBODY>
<TR>
<TD><PRE><CODE>
#define APP_INFO_FILE "mginit.rc"
static BOOL get_app_info (void)
{
int i;
APPITEM* item;
/* 获取应用程序个数信息 */
if (GetIntValueFromEtcFile (APP_INFO_FILE, "mginit", "nr", &app_info.nr_apps) != ETC_OK)
return FALSE;
if (app_info.nr_apps <= 0)
return FALSE;
/* 获取自动启动的应用程序索引 */
GetIntValueFromEtcFile (APP_INFO_FILE, "mginit", "autostart", &app_info.autostart);
if (app_info.autostart >= app_info.nr_apps || app_info.autostart < 0)
app_info.autostart = 0;
/* 分配应用程序信息结构 */
if ((app_info.app_items = (APPITEM*)calloc (app_info.nr_apps, sizeof (APPITEM))) == NULL) {
return FALSE;
}
/* 获取每个应用程序的路径、名称、图标等信息 */
item = app_info.app_items;
for (i = 0; i < app_info.nr_apps; i++, item++) {
char section [10];
sprintf (section, "app%d", i);
if (GetValueFromEtcFile (APP_INFO_FILE, section, "path", item->path, PATH_MAX) != ETC_OK)
goto error;
if (GetValueFromEtcFile (APP_INFO_FILE, section, "name", item->name, NAME_MAX) != ETC_OK)
goto error;
if (GetValueFromEtcFile (APP_INFO_FILE, section, "layer", item->layer, LEN_LAYER_NAME) != ETC_OK)
goto error;
if (GetValueFromEtcFile (APP_INFO_FILE, section, "tip", item->tip, TIP_MAX) != ETC_OK)
goto error;
strsubchr (item->tip, '&', ' ');
if (GetValueFromEtcFile (APP_INFO_FILE, section, "icon", item->bmp_path, PATH_MAX + NAME_MAX) != ETC_OK)
goto error;
if (LoadBitmap (HDC_SCREEN, &item->bmp, item->bmp_path) != ERR_BMP_OK)
goto error;
item->cdpath = TRUE;
}
return TRUE;
error:
free_app_info ();
return FALSE;
}</CODE></PRE></TD></TR></TBODY></TABLE></P>
<P><SPAN class=atitle3>5.2 定点数运算</SPAN><BR>通常在进行数学运算时,我们采用浮点数表示实数,并利用
头文件中所声明的函数进行浮点数运算。我们知道,浮点数运算是一种非常耗时的运算过程。为了减少因为浮点数运算而带来的额外 CPU
指令,在一些三维图形库当中,通常会采用定点数来表示实数,并利用定点数进行运算,这样,将大大提高三维图形的运算速度。MiniGUI
也提供了一些定点数运算函数,分为如下几类:
<UL class=n01>
<LI>整数、浮点数和定点数之间的转换。利用 itofix 和 fixtoi 函数可实现整数和定点数之间的相互转换;利用 ftofix 和
fixtof 函数可实现浮点数和定点数之间的转换。
<LI>定点数加、减、乘、除等基本运算。利用 fadd、fsub、fmul、fdiv、fsqrt等函数可实现定点数加、减、乘、除以及平方根运算。
<LI>定点数的三角运算。利用 fcos、fsin、ftan、facos、fasin 等函数可求给定定点数的余弦、正弦、正切、反余弦、反正弦值。
<LI>矩阵、向量等运算。矩阵、向量相关运算在三维图形中非常重要,限于篇幅,本文不会详细讲述这些运算,读者可参阅MiniGUI 的
include/fixedmath.h 头文件。 </LI></UL>
<P></P>
<P>下面的代码段演示了定点数的用法,该程序段根据给定的三个点(pts[0]、pts[1]、pts[2])画一个弧线,其中 pts[0]
作为圆心,pts[1] 是圆弧的起点,而 pts[2] 是圆弧终点和圆心连线上的一个点:
<TABLE cellSpacing=0 cellPadding=5 width="100%" bgColor=#cccccc
border=1><TBODY>
<TR>
<TD><PRE><CODE>
void draw_arc (HDC hdc, POINT* pts)
{
int sx = pts [0].x, sy = pts [0].y;
int dx = pts [1].x - sx, dy = pts [1].y - sy;
int r = sqrt (dx * dx * 1.0 + dy * dy * 1.0);
double cos_d = dx * 1.0 / r;
fixed cos_f = ftofix (cos_d);
fixed ang1 = facos (cos_f);
int r2;
fixed ang2;
if (dy > 0) {
ang1 = fsub (0, ang1);
}
dx = pts [2].x - sx;
dy = pts [2].y - sy;
r2 = sqrt (dx * dx * 1.0 + dy * dy * 1.0);
cos_d = dx * 1.0 / r2;
cos_f = ftofix (cos_d);
ang2 = facos (cos_f);
if (dy > 0) {
ang2 = fsub (0, ang2);
}
Arc (hdc, sx, sy, r, ang1, ang2);
}</CODE></PRE></TD></TR></TBODY></TABLE></P>
<P>上述程序的计算非常简单,步骤如下(该程序段来自 mde 演示程序包中的 painter/painter.c 程序):
<OL class=n01>
<LI>根据 pts[0] 和 pts[1] 计算圆弧的半径,然后计算圆弧的起始偏角,即 ang1,使用了ftofix 函数和 facos
函数。
<LI>计算 pts[2] 点和圆心连线的夹角,即 ang2,使用了 ftofix 和 facos 函数。
<LI>调用 Arc 函数绘制圆弧。</LI></OL>
<P></P>
<P><A name=6><SPAN class=atitle2>6 小结</SPAN></A><BR>本文讲述了 MiniGUI
为应用程序提供的一些非 GUI/GDI 的接口。这些接口中,某些是为了解决和操作系统的交互而设计的,以便 MiniGUI
应用程序能够更好地与操作系统提供的机制融合在一起;而某些提供了对 UNIX Domain Socket
良好封装的接口,可帮助应用程序方便进行进程间通讯或者扩展其功能;其他接口则专注于嵌入式系统的特殊性,为应用程序提供了可移植的文件 I/O
封装代码。在这些接口的帮助下,嵌入式系统开发人员可以编写功能强大而灵活的应用程序。</P><!-- RESOURCES-->
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD><A name=author1><SPAN class=atitle2>关于作者</SPAN></A><BR>魏永明(<A
href="mailto:ymwei@minigui.org">ymwei@minigui.org</A>),男,27
岁,工学硕士。国内最有影响的自由软件项目之一--MiniGUI 的创始人及主要开发人员。著有《Linux 实用教程》与《学用 Linux
与 Windows NT》,并主持翻译了《Red Hat Linux 奥秘》、《Linux 编程宝典》 等大量优秀的 Linux
技术著作。是清华大学 AKA Linux
编程技术系列讲座的主讲人。</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- END PAPER BODY--><TR>
<TABLE>
<TBODY>
<TR>
<TD width=10><IMG height=1 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=10
border=0></TD></TR></TBODY></TABLE></TR><BR clear=all><IMG height=10 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=100 border=0><BR>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD align=right width="100%"><A
href="http://www-900.ibm.com/developerWorks/cn/linux/embed/minigui/guide/part6/index.shtml#top">到页首</A></TD>
<TD width=5><IMG height=1 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=5
border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#000000 colSpan=2><IMG height=1 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=100
border=0></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff colSpan=2><IMG height=8 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
<TBODY>
<TR vAlign=top>
<TD>
<FORM action=/developerWorks/cn/cnratings.nsf/RateArticle?CreateDocument
method=post><INPUT type=hidden
value="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南主题六:MiniGUI 提供的非 GUI/GDI 接口"
name=ArticleTitle> <INPUT type=hidden value=linux name=Zone> <INPUT
type=hidden value=/developerWorks/cn/thankyou/feedback-linux.html
name=RedirectURL> <A name=rating><B>您对这篇文章的看法如何?</B></A>
<TABLE cellSpacing=0 cellPadding=0 width=600 border=0>
<TBODY>
<TR>
<TD colSpan=5><IMG height=8 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=100
border=0></TD></TR>
<TR vAlign=top>
<TD width="16%"><INPUT type=radio value=5 name=Rating>真棒!(5)</TD>
<TD width="20%"><INPUT type=radio value=4 name=Rating>好材料 (4)</TD>
<TD width="24%"><INPUT type=radio value=3 name=Rating>一般;尚可 (3)</TD>
<TD width="22%"><INPUT type=radio value=2 name=Rating>需提高 (2)</TD>
<TD width="18%"><INPUT type=radio value=1 name=Rating>太差!
(1)</TD></TR></TBODY></TABLE><BR><B>建议?</B><BR><TEXTAREA name=Comments rows=5 wrap=virtual cols=60></TEXTAREA><BR><BR><INPUT type=submit value=提交反馈意见></FORM></TD></TR>
<TR vAlign=top>
<TD bgColor=#ffffff><IMG height=8 alt=""
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/c.gif" width=100
border=0></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align=right>(c) Copyright IBM Corp. 2001, (c) Copyright IBM China
2001, All Right Reserved</TD></TR>
<TR vAlign=top>
<TD class=bbg height=21> <A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/index.shtml&origin=dwhead">关于
IBM</A><SPAN class=divider> | </SPAN><A
class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/privacy/index.shtml&origin=dwhead">隐私条约</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/legal/index.shtml&origin=dwhead">使用条款</A><SPAN
class=divider> | </SPAN><A class=mainlink
href="http://www-900.ibm.com/developerWorks/cn/cgi-bin/click.cgi?url=www-900.ibm.com/cn/ibm/contact/index.shtml&origin=dwhead">联系
IBM</A></TD></TR></TBODY></TABLE>
<SCRIPT language=JavaScript1.2
src="基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files/stats.js"
type=text/javascript></SCRIPT>
<NOSCRIPT><IMG height=1 alt=""
src="D:\新建文件夹\基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(六).files\c(1).gif" width=1
border=0></NOSCRIPT> </A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -