LoginSignup
3
2

More than 5 years have passed since last update.

GBDKによるゲームボーイソフト制作-2回 文字を出してみる-

Last updated at Posted at 2018-03-29

前回:GBDKによるゲームボーイソフト制作-1回 Hello World!-
次回:GBDKによるゲームボーイソフト制作-3回 キー入力-

記事の内容

gbdk\examples\gb内の「filltest.gb」を参考に色々と試してみたのでその記録。

ソースコード

実際に書いたのは以下のようなプログラム。

draw_char.c
#include <gb/drawing.h>

void main() {
    UBYTE i,x,y;

    for (i = 0; i < 256; ++i) {
        y = i / 16;
        x = i % 16;
        gotogxy(x, y);
        color(3, 1, SOLID);
        gprintf("%c", i);
    }
}

実行結果

result.png

解説のようなもの

1行目

今回使う機能は「gb/drawing.h」内で定義されているため、includeする。

4行目 UBYTE

「UBYTE」という型は、「gbdk\include\asm\types.h」で以下の通り定義されている。

/** Unsigned 8 bit.
 */
typedef UINT8           UBYTE;

つまり、8ビット(0~255)の符号なし整数ということである。
BYTEとしてるのは8bitが1Byteだからかな?

8行目 gotogxy

この関数は「gb/drawing.h」で以下のように定義されている。

/** Sets the current text position to (x,y).  Note that x and y have units
   of cells (8 pixels) */
void
    gotogxy(UINT8 x, UINT8 y);

ざっくり意訳

テキストが描画される位置を設定する。
引数x,yは8ピクセル単位での座標を意味する。

ゲームボーイの画面解像度は160x144なので、8ピクセルの塊が横に20(0~19),縦に18(0~17)入ることになる。

9行目 color

この関数もまた「gb/drawing.h」で以下のように定義されている。

/** Set the current foreground colour (for pixels), background colour, and
   draw mode */
void    color(UINT8 forecolor, UINT8 backcolor, UINT8 mode);

ざっくり意訳

前景及び背景の色と描画モードを設定する。

色の指定はUINT8(8ビット符号なし整数)で定義されているが、ゲームボーイの描画色は白黒の4階調であるため、実際に取り得る値の範囲は0~3である。
第三引数のmodeは以下の指定ができるが、筆者が試した限り変化がなかったので何か使い方があるのだろう。

    #define SOLID   0x00        /* Overwrites the existing pixels */
    #define OR  0x01        /* Performs a logical OR */
    #define XOR 0x02        /* Performs a logical XOR */
    #define AND 0x03        /* Performs a logical AND */

10行目 gprintf

今回のメイン。
この関数もまた「gb/drawing.h」で以下略。

/** Print the formatted string 'fmt' with arguments '...' */
INT8    
    gprintf(char *fmt,...) NONBANKED;

ざっくり意訳

フォーマット指定文字列fmtに従って以降の引数で渡された内容を描画する。

簡単に言うとprintfのゲームボーイ版みたいなものだと思う。たぶん。
今回、変数iを0~255までループさせているので、その数字に対応した文字が描画されている。

ということで今回はこれでおしまい。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2