Skip to content

Instantly share code, notes, and snippets.

@nekko1119 nekko1119/dxrx.cpp
Created Oct 15, 2017

Embed
What would you like to do?
#include <DxLib.h>
#include <rx.hpp>
class Input {
char input[256];
rxcpp::subjects::subject<int> zInput;
public:
void update() {
GetHitKeyStateAll(this->input);
zInput.get_subscriber().on_next(input[KEY_INPUT_Z]);
}
rxcpp::observable<int> onZInput() const {
return zInput.get_observable();
}
};
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
ChangeWindowMode(TRUE);
if (DxLib_Init() == -1) {
return 1;
}
SetDrawScreen(DX_SCREEN_BACK);
Input input;
int counter = 0;
bool released = false;
// キーを押している間
input.onZInput()
.filter([](int value) { return value != 0; })
.subscribe([&counter](int) { ++counter; });
// キーが離された瞬間
input.onZInput()
.buffer(2, 1)
.subscribe([&released](std::vector<int> input) { released = input[0] != 0 && input[1] == 0; });
while (ProcessMessage() == 0) {
input.update();
// 一瞬背景が赤くなる
if (released) {
SetBackgroundColor(255, 0, 0);
} else {
SetBackgroundColor(0, 0, 0);
}
ClearDrawScreen();
clsDx();
// 押している間だけcounterがインクリメントされる
printfDx("key down count %d\n", counter);
ScreenFlip();
}
DxLib_End();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.