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

View File

@ -3,9 +3,7 @@ bool LineClear(int y){
int dimx = board_->get_dim_x();
for(int x = 1 ; x < dimx - 1 ; ++x){
ncpp::Cell c;
if(board_->get_at(y, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
board_->get_at(y, x, &c);
if(c.is_simple()){
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 x = 1 ; x < bdimx - 1 ; ++x){
ncpp::Cell c;
if(board_->get_at(dy, x, &c) < 0){
throw TetrisNotcursesErr("get_at()");
}
if(board_->putc(dy + cleared, x, &c) < 0){
throw TetrisNotcursesErr("putc()");
}
board_->get_at(dy, x, &c);
board_->putc(dy + cleared, x, &c);
c.get().gcluster = 0;
if(board_->putc(dy, x, &c) < 0){ // could just do this at end...
throw TetrisNotcursesErr("putc()");
}
board_->putc(dy, x, &c); // could just do this at end...
}
}
linescleared_ += cleared;

View File

@ -1,3 +1,4 @@
#define NCPP_EXCEPTIONS_PLEASE
#include <mutex>
#include <array>
#include <pwd.h>
@ -16,19 +17,6 @@ const std::string BackgroundFile = NOTCURSES_SHARE "/tetris-background.jpg";
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 {
public:
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
int y, x;
if(PrepForMove(&y, &x)){
if(!curpiece_->move(y + 1, x)){
throw TetrisNotcursesErr("move()");
}
curpiece_->move(y + 1, x);
if(InvalidMove()){
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
curpiece_->move(y, x);
if(y <= board_top_y_ - 1){
return true;
}
@ -18,9 +14,7 @@ bool MoveDown() { // returns true if the game has ended as a result of this move
}else{
++y;
}
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
}
nc_.render();
}
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 y, x;
if(PrepForMove(&y, &x)){
if(!curpiece_->move(y, x + shift)){
throw TetrisNotcursesErr("move()");
}
curpiece_->move(y, x + shift);
if(InvalidMove()){
if(!curpiece_->move(y, x)){
throw TetrisNotcursesErr("move()");
}
curpiece_->move(y, x);
}else{
x += shift;
if(!nc_.render()){
throw TetrisNotcursesErr("render()");
}
nc_.render();
}
}
}

View File

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

View File

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

View File

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

View File

@ -1,14 +1,10 @@
void StainBoard(int dimy, int dimx){
if(!board_->cursor_move(0, 1)){
throw TetrisNotcursesErr("cursor_move()");
}
board_->cursor_move(0, 1);
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;
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(&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);
if(!board_->stain(dimy - 2, dimx - 2, tl, tr, bl, br)){
throw TetrisNotcursesErr("stain()");
}
board_->stain(dimy - 2, dimx - 2, tl, tr, bl, br);
}

View File

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