fork download
  1. // console.cpp --- 仮想コンソール
  2. #include "DxLib.h"
  3. #include <string> // for std::string
  4. #include <cassert> // for assert
  5.  
  6. #define CHAR_HEIGHT 32 // 文字の高さ
  7. #define CHAR_WIDTH (CHAR_HEIGHT / 2) // 半角文字の幅
  8. #define COLUMNS 40 // 列の個数
  9. #define ROWS 15 // 行の個数
  10. #define CONSOLE_WIDTH (COLUMNS * CHAR_WIDTH) // コンソールの幅
  11. #define CONSOLE_HEIGHT (ROWS * CHAR_HEIGHT) // コンソールの高さ
  12. #define BLINK_RATE 200 // 点滅間隔(ミリ秒)
  13. #define PROMPT "C:\\> " // プロンプト
  14.  
  15. char gp_buf[ROWS][COLUMNS]; // 仮想コンソール
  16. int gp_cx = 0, gp_cy = 0; // キャレット位置(文字単位)
  17. int gp_px = 0, gp_py = 0; // ???
  18. int gp_font; // フォントハンドル
  19. bool gp_blink = false; // キャレットの点滅用
  20. std::string gp_output;
  21.  
  22. // 仮想コンソールを描画する。
  23. void gp_DrawConsole(void)
  24. {
  25. char str[2];
  26. str[1] = 0;
  27. for (int y = 0; y < ROWS; ++y)
  28. {
  29. for (int x = 0; x < COLUMNS; ++x)
  30. {
  31. str[0] = gp_buf[y][x];
  32. DrawStringToHandle(x * CHAR_WIDTH, y * CHAR_HEIGHT,
  33. str, 0xFFFFFF, gp_font);
  34. if (gp_cx == x && gp_cy == y && gp_blink)
  35. {
  36. str[0] = '_';
  37. DrawStringToHandle(x * CHAR_WIDTH, y * CHAR_HEIGHT,
  38. str, 0xFFFF00, gp_font);
  39. }
  40. }
  41. }
  42. }
  43.  
  44. // スクロールする。
  45. void gp_Scroll(void)
  46. {
  47. memmove(gp_buf[0], gp_buf[1], COLUMNS * (ROWS - 1));
  48. memset(gp_buf[ROWS - 1], ' ', COLUMNS);
  49. }
  50.  
  51. // 一文字右に移動。
  52. void gp_MoveRight(void)
  53. {
  54. ++gp_cx;
  55. if (gp_cx < COLUMNS)
  56. return;
  57.  
  58. gp_cx = 0;
  59. ++gp_cy;
  60. if (gp_cy < ROWS)
  61. return;
  62.  
  63. --gp_cy;
  64. gp_Scroll();
  65. }
  66.  
  67. // 改行。
  68. void gp_NewLine(void)
  69. {
  70. gp_cx = 0;
  71. ++gp_cy;
  72. if (gp_cy >= ROWS)
  73. {
  74. --gp_cy;
  75. gp_Scroll();
  76. }
  77. }
  78.  
  79. // 一文字出力する。
  80. void gp_PutChar(char ch)
  81. {
  82. gp_buf[gp_cy][gp_cx] = (char)ch;
  83. gp_MoveRight();
  84. }
  85.  
  86. // 入力を受け取る。
  87. void gp_ReadInput(void)
  88. {
  89. char ch = GetInputChar(TRUE);
  90. if (ch == 0)
  91. return;
  92.  
  93. if (ch == '\r')
  94. {
  95. gp_NewLine();
  96. gp_output = PROMPT;
  97. return;
  98. }
  99.  
  100. gp_PutChar(ch);
  101. }
  102.  
  103. // 初期化を行う。
  104. void gp_Init(void)
  105. {
  106. #if 0
  107. TCHAR gp_buf[32];
  108. wsprintf(gp_buf, TEXT("%d, %d"), CONSOLE_WIDTH, CONSOLE_HEIGHT);
  109. MessageBox(NULL, gp_buf, NULL, 0);
  110. #endif
  111. assert(CONSOLE_WIDTH == 640);
  112. assert(CONSOLE_HEIGHT == 480);
  113. //assert(CONSOLE_WIDTH == 800);
  114. //assert(CONSOLE_HEIGHT == 600);
  115.  
  116. SetDrawScreen(DX_SCREEN_BACK);
  117. memset(gp_buf, ' ', sizeof(gp_buf));
  118. gp_font = CreateFontToHandle("MS ゴシック", CHAR_HEIGHT, 0,
  119. DX_FONTTYPE_ANTIALIASING);
  120.  
  121. gp_output = PROMPT;
  122. }
  123.  
  124. INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT)
  125. {
  126. SetGraphMode(CONSOLE_WIDTH, CONSOLE_HEIGHT, 32);
  127. ChangeWindowMode(TRUE);
  128. if (DxLib_Init() == -1)
  129. {
  130. return -1;
  131. }
  132.  
  133. gp_Init();
  134.  
  135. // 点滅用のカウンタ。
  136. int counter = GetNowCount();
  137.  
  138. while (!ScreenFlip() && !ProcessMessage() && !ClearDrawScreen())
  139. {
  140. gp_DrawConsole();
  141. gp_ReadInput();
  142.  
  143. // 出力中か?
  144. if (gp_output.empty())
  145. {
  146. // 点滅のため、一定時間ごとにフラグを切り替える。
  147. if (GetNowCount() - counter >= BLINK_RATE)
  148. {
  149. gp_blink = !gp_blink;
  150. counter = GetNowCount();
  151. }
  152. }
  153. else
  154. {
  155. // 一文字出力する。
  156. gp_PutChar(gp_output[0]);
  157. gp_output.erase(gp_output.begin());
  158.  
  159. gp_px = gp_cx;
  160. gp_py = gp_cy;
  161. gp_blink = false;
  162. }
  163. }
  164. DxLib_End();
  165. return 0;
  166. }
  167.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:2:19: fatal error: DxLib.h: No such file or directory
compilation terminated.
stdout
Standard output is empty