Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
### v4.0.7
* `extras/menu.h` - New widget added for adding a vertical scrolling menu
* Added unit testing in `tests/` directory
* Added zig build option
* `extras/menu.h` - New widget added for adding a vertical scrolling menu
* `screen.h` - Added hashing for `Pos` object to use as a key in
`std::unordered_map` and other hashed objects
* `text.h` - Add `raw_str` function to strip a string of any added ansi escape
codes
* Added new examples:
* `examples/vertical_menu.cpp` for trialing menu.h
* `examples/game.cpp` porting a simple "collect the coin" game to Rawterm


### v4.0.6
* `extras/pane.h` - Added handling for blacklisting Regions from the cursor
Expand Down
2 changes: 2 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_executable(keys keys.cpp)
add_executable(raw_escapes raw_escapes.cpp)
add_executable(red_blue_panes red_blue_panes.cpp)
add_executable(vertical_menu vertical_menu.cpp)
add_executable(game game.cpp)

target_link_libraries(cells PUBLIC rawterm)
target_link_libraries(colors PUBLIC rawterm)
Expand All @@ -17,3 +18,4 @@ target_link_libraries(keys PUBLIC rawterm)
target_link_libraries(raw_escapes PUBLIC rawterm)
target_link_libraries(red_blue_panes PUBLIC rawterm)
target_link_libraries(vertical_menu PUBLIC rawterm)
target_link_libraries(game PUBLIC rawterm)
101 changes: 101 additions & 0 deletions examples/game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <rawterm/core.h>
#include <rawterm/cursor.h>

#include <format>
#include <iostream>
#include <random>

// A recreation of a simple "collect the coin" game from this tutorial in Go.
// See: https://www.youtube.com/watch?v=H1X16MWKrCA

struct Sprite {
char rune;
int vert;
int horiz;

Sprite(char c, int x, int y) : rune(c), vert(x), horiz(y) {}
void draw(rawterm::Cursor& cur) {
cur.move({vert, horiz});
std::cout << rune;
}
};

std::vector<Sprite> setupCoins(const int& lvl, const rawterm::Pos& term_size) {
// setup random
// https://stackoverflow.com/a/7560564
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen(rd()); // seed the generator
std::uniform_int_distribution<> distr_vert(2, term_size.vertical); // define the range
std::uniform_int_distribution<> distr_horiz(2, term_size.horizontal);

std::vector<Sprite> coins = {};

for (int i = 0; i < lvl + 3; i++) {
coins.emplace_back('0', distr_vert(gen), distr_horiz(gen));
}

return coins;
}

int main() {
rawterm::enable_raw_mode();
rawterm::enter_alt_screen();

rawterm::Cursor::cursor_hide();
rawterm::Cursor cur;
cur.reset();

unsigned short score = 0;
unsigned int lvl = 1;
Sprite player = Sprite('@', 10, 10);
std::vector<Sprite> coins = setupCoins(lvl, rawterm::get_term_size());

// Helper lambda for redrawing term
auto draw_all = [&player, &coins, &cur, &score, &lvl] {
rawterm::clear_screen();
cur.move({1, 1});
std::cout << std::format("Score: {} Level: {}", score, lvl);
player.draw(cur);

for (auto&& coin : coins) {
coin.draw(cur);
} // Doesn't draw last coin?
};

// Game loop
while (true) {
draw_all();
auto k = rawterm::wait_for_input();

if (k == rawterm::Key('q')) {
break;
} else if (k == rawterm::Key('h')) {
player.horiz--;
} else if (k == rawterm::Key('j')) {
player.vert++;
} else if (k == rawterm::Key('k')) {
player.vert--;
} else if (k == rawterm::Key('l')) {
player.horiz++;
}

// Remove collected coin
for (int i = 0; i < coins.size(); i++) {
Sprite* c = &coins.at(i);
if (player.vert == c->vert && player.horiz == c->horiz) {
coins.erase(coins.begin() + i);
score++;

// Check for next level
if (coins.size() == 1) {
lvl++;
coins = setupCoins(lvl, rawterm::get_term_size());
}
break;
}
}
}

rawterm::Cursor::cursor_show();
return 0;
}
9 changes: 3 additions & 6 deletions examples/keys.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#include <rawterm/color.h>
#include <rawterm/core.h>
#include <rawterm/extras/extras.h>
#include <sys/types.h>
// #include <sys/types.h>

#include <cctype>
#include <deque>
#include <ios>
#include <iostream>

// This is a demonstration on how `rawterm` works. You can compile this with
// `./build.sh` and run with `./out/keys`. Any keypress you enter then will
// show it's output as a Key object in the terminal. Press `q` to quit.
// This is a demonstration on how `rawterm` works. Any keypress you enter
// will show it's output as a Key object in the terminal. Press `q` to quit.

int main() {
rawterm::enable_raw_mode();
Expand Down
4 changes: 3 additions & 1 deletion rawterm/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ namespace rawterm {
case '\x69':
return Key('i', rawterm::Mod::None, raw);
case '\x6A':
return Key('j', rawterm::Mod::Insert, raw);
// NOTE: This can also be insert
// TODO: Figure out a way for this to be both None and Insert
return Key('j', rawterm::Mod::None, raw);
case '\x6B':
return Key('k', rawterm::Mod::None, raw);
case '\x6C':
Expand Down
Loading