[回答ではありません] "きろく"と入力されたとき、直近の5行分を applog.txt に書くサンプル。
C++
1#include <DxLib.h>
2#include <string>
3#include <deque>
4#include <fstream>
5
6int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
7
8 std::deque<std::string> messages;
9 int InputHandle;
10
11 SetGraphMode(700, 780, 32);
12 ChangeWindowMode(TRUE);
13 if (DxLib_Init() == -1) return -1;
14 SetDrawScreen(DX_SCREEN_BACK);
15 SetFontSize(64);
16
17 InputHandle = MakeKeyInput(50, FALSE, FALSE, FALSE);
18 SetActiveKeyInput(InputHandle);
19
20 while (!ProcessMessage()) {
21 ClearDrawScreen();
22
23 if (CheckKeyInput(InputHandle) != 0) {
24 char buffer[256];
25 GetKeyInputString(buffer, InputHandle);
26 std::string input = buffer;
27 DrawString(0, 0, input.c_str(), GetColor(255, 255, 255));
28 if ( input == "きろく" ) {
29 std::ofstream stream("applog.txt");
30 while ( messages.size() != 0 ) {
31 std::string line = messages.front();
32 messages.pop_front();
33 stream << line << std::endl;
34 }
35 } else {
36 messages.push_back(input);
37 }
38 while ( messages.size() > 5 ) {
39 messages.pop_front();
40 }
41 SetActiveKeyInput(InputHandle);
42 SetKeyInputString("", InputHandle);
43 }
44
45 DrawKeyInputModeString(640, 480);
46
47 int height = 100;
48 for ( std::string message : messages ) {
49 DrawString(100, height, message.c_str(), GetColor(200, 200, 255));
50 height += 60;
51 }
52 DrawKeyInputString(0, 0, InputHandle);
53
54 ScreenFlip();
55 }
56
57 DeleteKeyInput(InputHandle);
58 DxLib_End();
59
60 return 0;
61}
[追記] "きろく"の直後から二度目の"きろく"の直前までを applog.txt に書き込むサンプル
C++
1#include <DxLib.h>
2#include <string>
3#include <deque>
4#include <vector>
5#include <fstream>
6
7int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
8
9 std::deque<std::string> messages;
10 std::vector<std::string> log;
11 bool logging = false;
12
13 int InputHandle;
14
15 SetGraphMode(700, 780, 32);
16 ChangeWindowMode(TRUE);
17 if (DxLib_Init() == -1) return -1;
18 SetDrawScreen(DX_SCREEN_BACK);
19 SetFontSize(64);
20
21 InputHandle = MakeKeyInput(50, FALSE, FALSE, FALSE);
22 SetActiveKeyInput(InputHandle);
23
24 while (!ProcessMessage()) {
25 ClearDrawScreen();
26
27 if (CheckKeyInput(InputHandle) != 0) {
28 char buffer[256];
29 GetKeyInputString(buffer, InputHandle);
30 std::string input = buffer;
31 DrawString(0, 0, input.c_str(), GetColor(255, 255, 255));
32 if ( input == "きろく" ) {
33 if ( !logging ) { // 記録開始
34 log.clear();
35 logging = true;
36 } else { // 記録終了
37 std::ofstream stream("applog.txt");
38 for ( std::string line : log ) {
39 stream << line << std::endl;
40 }
41 logging = false;
42 }
43 } else {
44 if ( logging ) {
45 log.push_back(input);
46 }
47 messages.push_back(input);
48 }
49 while ( messages.size() > 5 ) {
50 messages.pop_front();
51 }
52 SetActiveKeyInput(InputHandle);
53 SetKeyInputString("", InputHandle);
54 }
55
56 DrawKeyInputModeString(640, 480);
57 int height = 100;
58 for ( std::string message : messages ) {
59 DrawString(100, height, message.c_str(), GetColor(200, 200, 255));
60 height += 60;
61 }
62 DrawKeyInputString(0, 0, InputHandle);
63
64 ScreenFlip();
65 }
66
67 DeleteKeyInput(InputHandle);
68 DxLib_End();
69
70 return 0;
71}