tetris: rely on c++ exceptions #621

This commit is contained in:
nick black 2020-05-21 17:37:39 -04:00
parent 7af027b895
commit 4ab0e67155
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
11 changed files with 33 additions and 108 deletions

View File

@ -1,27 +1,15 @@
void DrawBackground(const std::string& s) { // drawn to the standard plane void DrawBackground(const std::string& s) { // drawn to the standard plane
#ifdef USE_MULTIMEDIA
nc_err_e err; nc_err_e err;
try{ backg_ = std::make_unique<ncpp::Visual>(s.c_str(), &err, 0, 0, ncpp::NCScale::Stretch);
backg_ = std::make_unique<ncpp::Visual>(s.c_str(), &err, 0, 0, ncpp::NCScale::Stretch); backg_->decode();
}catch(std::exception& e){ backg_->render(0, 0, -1, -1);
throw TetrisNotcursesErr("visual(): " + s + ": " + e.what());
}
if(backg_->decode() != NCERR_SUCCESS){
throw TetrisNotcursesErr("decode(): " + s);
}
if(backg_->render(0, 0, -1, -1) <= 0){
throw TetrisNotcursesErr("render(): " + s);
}
backg_->get_plane()->greyscale(); backg_->get_plane()->greyscale();
#else
(void)s;
#endif
} }
void DrawBoard() { // draw all fixed components of the game void DrawBoard() { // draw all fixed components of the game
try{ try{
DrawBackground(BackgroundFile); DrawBackground(BackgroundFile);
}catch(TetrisNotcursesErr& e){ }catch(...){
stdplane_->printf(1, 1, "couldn't load %s", BackgroundFile.c_str()); stdplane_->printf(1, 1, "couldn't load %s", BackgroundFile.c_str());
} }
int y, x; int y, x;
@ -32,27 +20,18 @@ void DrawBoard() { // draw all fixed components of the game
uint64_t channels = 0; uint64_t channels = 0;
channels_set_fg(&channels, 0x00b040); channels_set_fg(&channels, 0x00b040);
channels_set_bg_alpha(&channels, CELL_ALPHA_TRANSPARENT); channels_set_bg_alpha(&channels, CELL_ALPHA_TRANSPARENT);
if(!board_->double_box(0, channels, BOARD_HEIGHT - 1, BOARD_WIDTH * 2 - 1, NCBOXMASK_TOP)){ board_->double_box(0, channels, BOARD_HEIGHT - 1, BOARD_WIDTH * 2 - 1, NCBOXMASK_TOP);
throw TetrisNotcursesErr("double_box()");
}
channels_set_fg_alpha(&channels, CELL_ALPHA_TRANSPARENT); channels_set_fg_alpha(&channels, CELL_ALPHA_TRANSPARENT);
board_->set_base("", 0, channels); board_->set_base("", 0, channels);
scoreplane_ = std::make_unique<ncpp::Plane>(2, 30, y - BOARD_HEIGHT, 2, nullptr); scoreplane_ = std::make_unique<ncpp::Plane>(2, 30, y - BOARD_HEIGHT, 2, nullptr);
if(!scoreplane_){
throw TetrisNotcursesErr("Plane()");
}
uint64_t scorechan = 0; uint64_t scorechan = 0;
channels_set_bg_alpha(&scorechan, CELL_ALPHA_TRANSPARENT); channels_set_bg_alpha(&scorechan, CELL_ALPHA_TRANSPARENT);
channels_set_fg_alpha(&scorechan, CELL_ALPHA_TRANSPARENT); channels_set_fg_alpha(&scorechan, CELL_ALPHA_TRANSPARENT);
if(!scoreplane_->set_base("", 0, scorechan)){ scoreplane_->set_base("", 0, scorechan);
throw TetrisNotcursesErr("set_base()");
}
scoreplane_->set_bg_alpha(CELL_ALPHA_TRANSPARENT); scoreplane_->set_bg_alpha(CELL_ALPHA_TRANSPARENT);
scoreplane_->set_fg(0xd040d0); scoreplane_->set_fg(0xd040d0);
scoreplane_->printf(0, 1, "%s", getpwuid(geteuid())->pw_name); scoreplane_->printf(0, 1, "%s", getpwuid(geteuid())->pw_name);
scoreplane_->set_fg(0x00d0a0); scoreplane_->set_fg(0x00d0a0);
UpdateScore(); UpdateScore();
if(!nc_.render()){ nc_.render();
throw TetrisNotcursesErr("render()");
}
} }

View File

@ -3,9 +3,7 @@ bool LineClear(int y){
int dimx = board_->get_dim_x(); int dimx = board_->get_dim_x();
for(int x = 1 ; x < dimx - 1 ; ++x){ for(int x = 1 ; x < dimx - 1 ; ++x){
ncpp::Cell c; ncpp::Cell c;
if(board_->get_at(y, x, &c) < 0){ board_->get_at(y, x, &c);
throw TetrisNotcursesErr("get_at()");
}
if(c.is_simple()){ if(c.is_simple()){
return false; return false;
} }

View File

@ -18,16 +18,10 @@ bool LockPiece(){ // returns true if game has ended by reaching level 16
for(int dy = y ; dy >= 0 ; --dy){ for(int dy = y ; dy >= 0 ; --dy){
for(int x = 1 ; x < bdimx - 1 ; ++x){ for(int x = 1 ; x < bdimx - 1 ; ++x){
ncpp::Cell c; ncpp::Cell c;
if(board_->get_at(dy, x, &c) < 0){ board_->get_at(dy, x, &c);
throw TetrisNotcursesErr("get_at()"); board_->putc(dy + cleared, x, &c);
}
if(board_->putc(dy + cleared, x, &c) < 0){
throw TetrisNotcursesErr("putc()");
}
c.get().gcluster = 0; c.get().gcluster = 0;
if(board_->putc(dy, x, &c) < 0){ // could just do this at end... board_->putc(dy, x, &c); // could just do this at end...
throw TetrisNotcursesErr("putc()");
}
} }
} }
linescleared_ += cleared; linescleared_ += cleared;

View File

@ -1,3 +1,4 @@
#define NCPP_EXCEPTIONS_PLEASE
#include <mutex> #include <mutex>
#include <array> #include <array>
#include <pwd.h> #include <pwd.h>
@ -16,19 +17,6 @@ const std::string BackgroundFile = NOTCURSES_SHARE "/tetris-background.jpg";
using namespace std::chrono_literals; using namespace std::chrono_literals;
class TetrisNotcursesErr : public std::runtime_error {
public:
TetrisNotcursesErr(const std::string& s) throw()
: std::runtime_error(s) {
}
TetrisNotcursesErr(char const* const message) throw()
: std::runtime_error(message) {
}
virtual char const* what() const throw() {
return exception::what();
}
};
class Tetris { class Tetris {
public: public:
Tetris(ncpp::NotCurses& nc, std::atomic_bool& gameover) : Tetris(ncpp::NotCurses& nc, std::atomic_bool& gameover) :

View File

@ -1,13 +1,9 @@
bool MoveDown() { // returns true if the game has ended as a result of this move bool MoveDown() { // returns true if the game has ended as a result of this move
int y, x; int y, x;
if(PrepForMove(&y, &x)){ if(PrepForMove(&y, &x)){
if(!curpiece_->move(y + 1, x)){ curpiece_->move(y + 1, x);
throw TetrisNotcursesErr("move()");
}
if(InvalidMove()){ if(InvalidMove()){
if(!curpiece_->move(y, x)){ curpiece_->move(y, x);
throw TetrisNotcursesErr("move()");
}
if(y <= board_top_y_ - 1){ if(y <= board_top_y_ - 1){
return true; return true;
} }
@ -18,9 +14,7 @@ bool MoveDown() { // returns true if the game has ended as a result of this move
}else{ }else{
++y; ++y;
} }
if(!nc_.render()){ nc_.render();
throw TetrisNotcursesErr("render()");
}
} }
return false; return false;
} }

View File

@ -2,18 +2,12 @@ void MoveLateral(int direction) { // pass in -1 for left, 1 for right
int shift = 2 * direction; int shift = 2 * direction;
int y, x; int y, x;
if(PrepForMove(&y, &x)){ if(PrepForMove(&y, &x)){
if(!curpiece_->move(y, x + shift)){ curpiece_->move(y, x + shift);
throw TetrisNotcursesErr("move()");
}
if(InvalidMove()){ if(InvalidMove()){
if(!curpiece_->move(y, x)){ curpiece_->move(y, x);
throw TetrisNotcursesErr("move()");
}
}else{ }else{
x += shift; x += shift;
if(!nc_.render()){ nc_.render();
throw TetrisNotcursesErr("render()");
}
} }
} }
} }

View File

@ -27,15 +27,11 @@ std::unique_ptr<ncpp::Plane> NewPiece() {
y = 0; x = 0; y = 0; x = 0;
for(size_t i = 0 ; i < strlen(t->texture) ; ++i){ for(size_t i = 0 ; i < strlen(t->texture) ; ++i){
if(t->texture[i] == '*'){ if(t->texture[i] == '*'){
if(n->putstr(y, x, "██") < 0){ n->putstr(y, x, "██");
throw TetrisNotcursesErr("putstr()");
}
} }
y += ((x = ((x + 2) % cols)) == 0); y += ((x = ((x + 2) % cols)) == 0);
} }
} }
if(!nc_.render()){ nc_.render();
throw TetrisNotcursesErr("render()");
}
return n; return n;
} }

View File

@ -3,15 +3,11 @@ void RotateCcw() {
if(!PrepForMove(&y, &x)){ if(!PrepForMove(&y, &x)){
return; return;
} }
if(!curpiece_->rotate_ccw()){ curpiece_->rotate_ccw();
throw TetrisNotcursesErr("rotate_ccw()"); if(InvalidMove()){
} curpiece_->rotate_cw();
if(InvalidMove() && !curpiece_->rotate_cw()){
throw TetrisNotcursesErr("rotate_cw()");
}
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
} }
nc_.render();
} }
void RotateCw() { void RotateCw() {
@ -19,13 +15,9 @@ void RotateCw() {
if(!PrepForMove(&y, &x)){ if(!PrepForMove(&y, &x)){
return; return;
} }
if(!curpiece_->rotate_cw()){ curpiece_->rotate_cw();
throw TetrisNotcursesErr("rotate_cw()"); if(InvalidMove()){
} curpiece_->rotate_ccw();
if(InvalidMove() && !curpiece_->rotate_ccw()){
throw TetrisNotcursesErr("rotate_ccw()");
}
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
} }
nc_.render();
} }

View File

@ -1,5 +1,3 @@
void UpdateScore(){ void UpdateScore(){
if(scoreplane_->printf(1, 0, "level: %02d score: %ju", level_, static_cast<uintmax_t>(score_)) < 0){ scoreplane_->printf(1, 0, "level: %02d score: %ju", level_, static_cast<uintmax_t>(score_));
throw TetrisNotcursesErr("printf()");
}
} }

View File

@ -1,14 +1,10 @@
void StainBoard(int dimy, int dimx){ void StainBoard(int dimy, int dimx){
if(!board_->cursor_move(0, 1)){ board_->cursor_move(0, 1);
throw TetrisNotcursesErr("cursor_move()");
}
int high = 0xff - level_ * 16, low = level_ * 16; // rgb calculation limits us to 16 levels (0--15) int high = 0xff - level_ * 16, low = level_ * 16; // rgb calculation limits us to 16 levels (0--15)
uint64_t tl = 0, tr = 0, bl = 0, br = 0; uint64_t tl = 0, tr = 0, bl = 0, br = 0;
channels_set_fg_rgb(&tl, high, 0xff, low); channels_set_bg_alpha(&tl, CELL_ALPHA_TRANSPARENT); channels_set_fg_rgb(&tl, high, 0xff, low); channels_set_bg_alpha(&tl, CELL_ALPHA_TRANSPARENT);
channels_set_fg_rgb(&tr, low, high, 0xff); channels_set_bg_alpha(&tr, CELL_ALPHA_TRANSPARENT); channels_set_fg_rgb(&tr, low, high, 0xff); channels_set_bg_alpha(&tr, CELL_ALPHA_TRANSPARENT);
channels_set_fg_rgb(&bl, 0xff, low, high); channels_set_bg_alpha(&bl, CELL_ALPHA_TRANSPARENT); channels_set_fg_rgb(&bl, 0xff, low, high); channels_set_bg_alpha(&bl, CELL_ALPHA_TRANSPARENT);
channels_set_fg_rgb(&br, 0xff, high, low); channels_set_bg_alpha(&br, CELL_ALPHA_TRANSPARENT); channels_set_fg_rgb(&br, 0xff, high, low); channels_set_bg_alpha(&br, CELL_ALPHA_TRANSPARENT);
if(!board_->stain(dimy - 2, dimx - 2, tl, tr, bl, br)){ board_->stain(dimy - 2, dimx - 2, tl, tr, bl, br);
throw TetrisNotcursesErr("stain()");
}
} }

View File

@ -5,9 +5,7 @@ bool InvalidMove() { // a bit wasteful, but piece are tiny
int x = dx; int x = dx;
while(x--){ while(x--){
ncpp::Cell c, b; ncpp::Cell c, b;
if(curpiece_->get_at(dy, x, &c) < 0){ curpiece_->get_at(dy, x, &c);
throw TetrisNotcursesErr("get_at()");
}
if(c.is_simple()){ if(c.is_simple()){
continue; continue;
} }
@ -17,9 +15,7 @@ bool InvalidMove() { // a bit wasteful, but piece are tiny
if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){ if(transy < 0 || transy >= board_->get_dim_y() || transx < 0 || transx >= board_->get_dim_x()){
return true; return true;
} }
if(board_->get_at(transy, transx, &b) < 0){ board_->get_at(transy, transx, &b);
throw TetrisNotcursesErr("get_at()");
}
if(!b.is_simple()){ if(!b.is_simple()){
return true; return true;
} }