読者です 読者をやめる 読者になる 読者になる

NDSでMQOビューアをつくる(3)

前回のコードをNDSでも動くようにした。

#include <nds.h>
#include <fat.h>
#include <stdio.h>
#include <string>
#include <boost/xpressive/xpressive.hpp>

#include "drunkenlogo.h"


void init()
{
  // irqs are nice
  irqInit();
  irqSet(IRQ_VBLANK, 0);

  // set the mode for 2 text layers and two extended background layers
  videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE);

  // set the sub background up for text display (we could just print to one
  // of the main display text backgrounds just as easily
  videoSetModeSub(MODE_0_2D | DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text

  // set the first bank as background memory and the third as sub background memory
  // B and D are not used
  vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_LCD);

  // set up background for text
  SUB_BG0_CR = BG_MAP_BASE(31);

  BG_PALETTE_SUB[255] = RGB15(31,31,31);//by default font will be rendered with color 255

  // consoleInit() is a lot more flexible but this gets you up and running quick
  consoleInitDefault((u16*)SCREEN_BASE_BLOCK_SUB(31), (u16*)CHAR_BASE_BLOCK_SUB(0), 16);
}

int main()
{
  using namespace boost::xpressive;
  using namespace std;

  init();

  if (fatInitDefault() == NULL) {
    iprintf("fatInitDefault err\n");
  }

  FILE *fp = fopen("test.mqo", "r");
  if (fp == NULL) {
    iprintf("err\n");
  }

  sregex re = sregex::compile("(\\-?\\d+\\.?\\d*)|(\"[^\"]+\")|([_a-zA-Z][_a-zA-Z1-9]*)");
  string str;

  char line[1024];
  uint32 line_no = 0;
  while (1) {
    scanKeys();
    u32 key_down = keysDown();
    if (key_down & KEY_DOWN) {
      if (fgets(line, 1024, fp)) {
        iprintf("----------------- line:%03d\n", line_no);
        str = line;

        sregex_iterator it(str.begin(), str.end(), re);
        sregex_iterator end;
        for (; it != end; ++it) {
          iprintf("%s\n", string(it->str()).c_str());
        }

        line_no++;
      }
    }

    swiWaitForVBlank();
  }

  fclose(fp);

  return 0;
}

sregex_iteratorからヌル終端の文字列を取り出す方法が分からなかったので、std::stringのコンストラクタに突っ込んでc_str()でNULL終端文字列に変換してます。