本篇首先完善文件压缩的功能,完成操作系统功能的开发,然后进行应用程序的开发。首先完成了C语言的几个标准函数,然后完成了bball程序与外星人游戏程序的开发。

1. 压缩文件功能
本篇的开头,最后再对操作系统的功能进行一些优化,就是让操作系统支持压缩的功能。为了减小操作系统文件的大小,我们可以将字库以及应用程序等文件进行压缩。操作系统具备了解压缩功能后,可以对载入的文件进行解压缩,这样也可以节省启动过程中需要载入的文件大小,加快启动的速度。
不同格式的压缩文件存在压缩率与解压速度的矛盾,压缩率高的,解压速度可能很慢,反之解压速度快的压缩率又上不去,需要在其中找到一种平衡。这里作者自己开发了一种比较平衡的tek格式的压缩文件。具体的算法就不进行介绍了,毕竟这里主要是操作系统的开发。直接引入作者开发的tek格式的相关函数:
int tek_getsize(unsigned char *p)
{
static char header[15] = {
0xff, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x4f, 0x53, 0x41, 0x53, 0x4b, 0x43, 0x4d, 0x50
};
int size = -1;
if (memcmp(p + 1, header, 15) == 0 && (*p == 0x83 || *p == 0x85 || *p == 0x89)) {
p += 16;
size = tek_getnum_s7s(&p);
}
return size;
}
int tek_decomp(unsigned char *p, char *q, int size)
{
int err = -1;
if (*p == 0x83) {
err = tek_decode1(size, p, q);
} else if (*p == 0x85) {
err = tek_decode2(size, p, q);
} else if (*p == 0x89) {
err = tek_decode5(size, p, q);
}
if (err != 0) {
return -1; /* 失败 */
}
return 0; /* 成功 */
}
这里tek_getsize函数用于判断文件是否为tek格式的压缩文件,如果是tek格式的压缩文件,则返回解压后的文件大小。而tek_decomp函数则用于完成解压操作。
使用上面两个函数,编写一个file_loadfile2的函数:
char *file_loadfile2(int clustno, int *psize, int *fat)
{
int size = *psize, size2;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
char *buf, *buf2;
buf = (char *) memman_alloc_4k(memman, size);
file_loadfile(clustno, size, buf, fat, (char *) (ADR_DISKIMG + 0x003e00));
if (size >= 17) {
size2 = tek_getsize(buf);
if (size2 > 0) { /* 使用tek格式压缩的文件 */
buf2 = (char *) memman_alloc_4k(memman, size2);
tek_decomp(buf, buf2, size2);
memman_free_4k(memman, (int) buf, size);
buf = buf2;
*psize = size2;
}
}
return buf;
}
这个函数首先使用memman_alloc_4k申请必要的内存空间,然后使用file_loadfile函数将文件内容载入内存。由于tek格式的文件带有一个用于识别格式的文件头,大小17字节,因此判断文件大小超过17字节才有可能是一个tek格式的压缩文件,于是调用tek_getsize进行判断。判断该文件确实为tek格式,则调用tek_decomp函数进行解压缩,舍弃掉解压缩之前的文件内容。
接下来我们就可以使用file_loadfile2函数修改载入字库nihongo.fnt的部分。
// 载入nihongo.txt文件
fat = (int *) memman_alloc_4k(memman, 4 * 2880);
file_readfat(fat, (unsigned char *) (ADR_DISKIMG + 0x000200));
finfo = file_search("nihongo.fnt", (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
if (finfo != 0) {
// 使用file_loadfile2载入文件并解压缩
i = finfo->size;
nihongo = file_loadfile2(finfo->clustno, &i, fat);
} else {
nihongo = (unsigned char *) memman_alloc_4k(memman, 16 * 256 + 32 * 94 * 47);
for (i = 0; i < 16 * 256; i++) {
nihongo[i] = hankaku[i];
}
for (i = 16 * 256; i < 16 * 256 + 32 * 94 * 47; i++) {
nihongo[i] = 0xff;
}
}
*((int *) 0x0fe8) = (int) nihongo;
memman_free_4k(memman, (int) fat, 4 * 2880);
使用压缩过的nihongo.txt文件进行测试,可以正常显示日文文字:

实现了压缩nihongo.fnt文件,我们可以更近一步,将应用程序也进行压缩:
int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
{
……
int i, segsiz, datsiz, esp, dathrb, appsiz;
……
if (finfo != 0) {
appsiz = finfo->size;
p = file_loadfile2(finfo->clustno, &appsiz, fat);
if (appsiz >= 36 && strncmp(p + 4, "Hari", 4) == 0 && *p == 0x00) {
……
} else {
cons_putstr0(cons, ".hrb file format error.\n");
}
memman_free_4k(memman, (int) p, appsiz);
cons_newline(cons);
return 1;
}
return 0;
}
压缩后的应用程序也可以正常运行,并且文件的大小缩减了很多。
2. C语言标准函数
C语言中有一些常用的函数,被称为“标准函数”,大多数操作系统都会提供这样的库,如printf,putchar,strcmp与malloc等。我们在操作系统中也实现一部分标准函数。
首先时putchar,用于在屏幕上显示一个特定字符。在我们的操作系统中,可以通过api_putcha轻易实现:
#include "apilib.h"
int putchar(int c)
{
api_putchar(c);
return c;
}
接下来是exit,由api_end来实现。
#include "apilib.h"
void exit(int status)
{
api_end();
}
然后是printf函数。printf在C语言中很常用,但是似乎两个函数来说要难实现一些,因为调用方式是不固定的。但实际实现起来却并没有那么复杂,因为可以使用编译器附带的函数。
#include <stdio.h>
#include <stdarg.h>
#include "apilib.h"
int printf(char *format, …)
{
va_list ap;
char s[1000];
int i;
va_start(ap, format);
i = vsprintf(s, format, ap);
api_putstr0(s);
va_end(ap);
return i;
}
这里通过省略号"…"传递的参数,可以通过va_list来获取。va_start用于初始化,va_end用来结束。有一个版本的snprintf是可以接受va_list为参数的,名字就叫vsprintf,使用这个函数即可完成处理。vsprintf也是编译器附带的,可以直接使用。
同样的malloc、free等函数也是编译器自带的,只要include <stdlib.h>就可以使用了。
3. bball程序
到此为止操作系统的开发已经基本完成了,接下来进行的是应用程序的开发。
bball程序是作者开发的一个绘图程序,可以绘制出一个球形:
#include "apilib.h"
void HariMain(void)
{
int win, i, j, dis;
char buf[216 * 237];
struct POINT {
int x, y;
};
static struct POINT table[16] = {
{ 204, 129 }, { 195, 90 }, { 172, 58 }, { 137, 38 }, { 98, 34 },
{ 61, 46 }, { 31, 73 }, { 15, 110 }, { 15, 148 }, { 31, 185 },
{ 61, 212 }, { 98, 224 }, { 137, 220 }, { 172, 200 }, { 195, 168 },
{ 204, 129 }
};
win = api_openwin(buf, 216, 237, -1, "bball");
api_boxfilwin(win, 8, 29, 207, 228, 0);
for (i = 0; i <= 14; i++) {
for (j = i + 1; j <= 15; j++) {
dis = j - i;
if (dis >= 8) {
dis = 15 - dis;
}
if (dis != 0) {
api_linewin(win, table[i].x, table[i].y, table[j].x, table[j].y, 8 - dis);
}
}
}
for (;;) {
if (api_getkey(1) == 0x0a) {
break;
}
}
api_end();
}
其实只是简单地画线,但是却可以组成一个很漂亮的球形:

4. 外星人游戏程序
接下来我们开发一个外星人的游戏程序。
#include <stdio.h> /* sprintf */
#include <string.h> /* strlen */
#include "apilib.h"
void putstr(int win, char *winbuf, int x, int y, int col, unsigned char *s);
void wait(int i, int timer, char *keyflag);
static unsigned char charset[16 * 8] = {
/* invader(0) */
0x00, 0x00, 0x00, 0x43, 0x5f, 0x5f, 0x5f, 0x7f,
0x1f, 0x1f, 0x1f, 0x1f, 0x00, 0x20, 0x3f, 0x00,
/* invader(1) */
0x00, 0x0f, 0x7f, 0xff, 0xcf, 0xcf, 0xcf, 0xff,
0xff, 0xe0, 0xff, 0xff, 0xc0, 0xc0, 0xc0, 0x00,
/* invader(2) */
0x00, 0xf0, 0xfe, 0xff, 0xf3, 0xf3, 0xf3, 0xff,
0xff, 0x07, 0xff, 0xff, 0x03, 0x03, 0x03, 0x00,
/* invader(3) */
0x00, 0x00, 0x00, 0xc2, 0xfa, 0xfa, 0xfa, 0xfe,
0xf8, 0xf8, 0xf8, 0xf8, 0x00, 0x04, 0xfc, 0x00,
/* fighter(0) */
0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x43, 0x47, 0x4f, 0x5f, 0x7f, 0x7f, 0x00,
/* fighter(1) */
0x18, 0x7e, 0xff, 0xc3, 0xc3, 0xc3, 0xc3, 0xff,
0xff, 0xff, 0xe7, 0xe7, 0xe7, 0xe7, 0xff, 0x00,
/* fighter(2) */
0x00, 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0xc2, 0xe2, 0xf2, 0xfa, 0xfe, 0xfe, 0x00,
/* laser */
0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00
};
/* invader:"abcd", fighter:"efg", laser:"h" */
void HariMain(void)
{
int win, timer, i, j, fx, laserwait, lx = 0, ly;
int ix, iy, movewait0, movewait, idir;
int invline, score, high, point;
char winbuf[336 * 261], invstr[32 * 6], s[12], keyflag[4], *p;
static char invstr0[32] = " abcd abcd abcd abcd abcd ";
win = api_openwin(winbuf, 336, 261, -1, "invader");
api_boxfilwin(win, 6, 27, 329, 254, 0);
timer = api_alloctimer();
api_inittimer(timer, 128);
high = 0;
putstr(win, winbuf, 22, 0, 7, "HIGH:00000000");
restart:
score = 0;
point = 1;
putstr(win, winbuf, 4, 0, 7, "SCORE:00000000");
movewait0 = 20;
fx = 18;
putstr(win, winbuf, fx, 13, 6, "efg");
wait(100, timer, keyflag);
next_group:
wait(100, timer, keyflag);
ix = 7;
iy = 1;
invline = 6;
for (i = 0; i < 6; i++) {
for (j = 0; j < 27; j++) {
invstr[i * 32 + j] = invstr0[j];
}
putstr(win, winbuf, ix, iy + i, 2, invstr + i * 32);
}
keyflag[0] = 0;
keyflag[1] = 0;
keyflag[2] = 0;
ly = 0; /* 不显示 */
laserwait = 0;
movewait = movewait0;
idir = +1;
wait(100, timer, keyflag);
for (;;) {
if (laserwait != 0) {
laserwait--;
keyflag[2 /* space */] = 0;
}
wait(4, timer, keyflag);
/* 自机的处理 */
if (keyflag[0 /* left */] != 0 && fx > 0) {
fx--;
putstr(win, winbuf, fx, 13, 6, "efg ");
keyflag[0 /* left */] = 0;
}
if (keyflag[1 /* right */] != 0 && fx < 37) {
putstr(win, winbuf, fx, 13, 6, " efg");
fx++;
keyflag[1 /* right */] = 0;
}
if (keyflag[2 /* space */] != 0 && laserwait == 0) {
laserwait = 15;
lx = fx + 1;
ly = 13;
}
/* 外星人移动 */
if (movewait != 0) {
movewait--;
} else {
movewait = movewait0;
if (ix + idir > 14 || ix + idir < 0) {
if (iy + invline == 13) {
break; /* GAME OVER */
}
idir = - idir;
putstr(win, winbuf, ix + 1, iy, 0, " ");
iy++;
} else {
ix += idir;
}
for (i = 0; i < invline; i++) {
putstr(win, winbuf, ix, iy + i, 2, invstr + i * 32);
}
}
/* 炮弹处理 */
if (ly > 0) {
if (ly < 13) {
if (ix < lx && lx < ix + 25 && iy <= ly && ly < iy + invline) {
putstr(win, winbuf, ix, ly, 2, invstr + (ly - iy) * 32);
} else {
putstr(win, winbuf, lx, ly, 0, " ");
}
}
ly--;
if (ly > 0) {
putstr(win, winbuf, lx, ly, 3, "h");
} else {
point -= 10;
if (point <= 0) {
point = 1;
}
}
if (ix < lx && lx < ix + 25 && iy <= ly && ly < iy + invline) {
p = invstr + (ly - iy) * 32 + (lx - ix);
if (*p != ' ') {
/* hit ! */
score += point;
point++;
sprintf(s, "%08d", score);
putstr(win, winbuf, 10, 0, 7, s);
if (high < score) {
high = score;
putstr(win, winbuf, 27, 0, 7, s);
}
for (p--; *p != ' '; p--) { }
for (i = 1; i < 5; i++) {
p[i] = ' ';
}
putstr(win, winbuf, ix, ly, 2, invstr + (ly - iy) * 32);
for (; invline > 0; invline--) {
for (p = invstr + (invline - 1) * 32; *p != 0; p++) {
if (*p != ' ') {
goto alive;
}
}
}
/* 全部消灭 */
movewait0 -= movewait0 / 3;
goto next_group;
alive:
ly = 0;
}
}
}
}
/* GAME OVER */
putstr(win, winbuf, 15, 6, 1, "GAME OVER");
wait(0, timer, keyflag);
for (i = 1; i < 14; i++) {
putstr(win, winbuf, 0, i, 0, " ");
}
goto restart;
}
void putstr(int win, char *winbuf, int x, int y, int col, unsigned char *s)
{
int c, x0, i;
char *p, *q, t[2];
x = x * 8 + 8;
y = y * 16 + 29;
x0 = x;
i = strlen(s); /* 计算s的字符数 */
api_boxfilwin(win + 1, x, y, x + i * 8 - 1, y + 15, 0);
q = winbuf + y * 336;
t[1] = 0;
for (;;) {
c = *s;
if (c == 0) {
break;
}
if (c != ' ') {
if ('a' <= c && c <= 'h') {
p = charset + 16 * (c - 'a');
q += x;
for (i = 0; i < 16; i++) {
if ((p[i] & 0x80) != 0) { q[0] = col; }
if ((p[i] & 0x40) != 0) { q[1] = col; }
if ((p[i] & 0x20) != 0) { q[2] = col; }
if ((p[i] & 0x10) != 0) { q[3] = col; }
if ((p[i] & 0x08) != 0) { q[4] = col; }
if ((p[i] & 0x04) != 0) { q[5] = col; }
if ((p[i] & 0x02) != 0) { q[6] = col; }
if ((p[i] & 0x01) != 0) { q[7] = col; }
q += 336;
}
q -= 336 * 16 + x;
} else {
t[0] = *s;
api_putstrwin(win + 1, x, y, col, 1, t);
}
}
s++;
x += 8;
}
api_refreshwin(win, x0, y, x, y + 16);
return;
}
void wait(int i, int timer, char *keyflag)
{
int j;
if (i > 0) {
/* 等待一段时间 */
api_settimer(timer, i);
i = 128;
} else {
i = 0x0a; /* Enter */
}
for (;;) {
j = api_getkey(1);
if (i == j) {
break;
}
if (j == '4') {
keyflag[0 /* left */] = 1;
}
if (j == '6') {
keyflag[1 /* right */] = 1;
}
if (j == ' ') {
keyflag[2 /* space */] = 1;
}
}
return;
}
游戏的背景是玩家驾驶一艘飞船,通过发射炮弹消灭出现在屏幕上的外星人。通过空格键发射炮弹,通过小键盘的4和6进行左右移动。外星人到达屏幕底端则游戏结束。
程序比较长,但主要是分为3部分,分别处理自己的飞船,发射的炮弹和外星人。
对于自己飞船的处理,通过数组key_flag的3个变量分别保存左右移动和发射炮弹的按键,如果对应的变量不为0表示接收到了按键,则进行相应的处理;此外还需要对飞船的x坐标进行限制,防止超出画面之外;根据设定,炮弹需要一定的充能时间,因此对于发射炮弹按键的处理增加了充能时间的判断。
对于外星人的处理,是一组外星人在左右移动的同时,每隔一段时间前进一段距离,到达屏幕底端则游戏结束。如果一组外星人被消灭掉,隔一小段时间再重新产生一组新的外星人。
对于炮弹的处理,则要考虑炮弹击中外星人后的消除,以及分数的增加。
程序运行的结果如图所示:

虽然简单,但游戏的操作性还挺强的,不得不对作者表示佩服。
下一篇内容将继续进行应用程序的开发,敬请期待。
2300




被折叠的 条评论
为什么被折叠?



