Trying to switch to more object-oriented

This commit is contained in:
2021-03-12 22:33:46 +01:00
parent fbe122a5b9
commit 258ce51cfe
32 changed files with 693 additions and 249 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ OUTPUTFLAG = -Fo
else
UNAME_S := $(shell uname -s)
CXX ?= g++
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 # -DDEBUG -DFEATURE # -g -fsanitize=address
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2 -DDEBUG -DFEATURE -g -fsanitize=address
OBJEXT = o
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
OUTPUTFLAG = -o
+26 -24
View File
@@ -32,10 +32,10 @@ TetrisBlock::TetrisBlock( double x, double y, double w, double h,
}
TetrisBlock::TetrisBlock( const TetrisBlock &other )
: TetrisBlock( other.getDoubleRect().first.first,
other.getDoubleRect().first.second,
other.getDoubleRect().second.first,
other.getDoubleRect().second.second, other.getRenderer(),
: TetrisBlock( other.getDoubleRect().first.getX(),
other.getDoubleRect().first.getY(),
other.getDoubleRect().second.getX(),
other.getDoubleRect().second.getY(), other.getRenderer(),
other.getColor(), true, other._index, other._scene,
other.pieces_bag ) {}
@@ -63,9 +63,9 @@ std::shared_ptr< TetrisBlock > TetrisBlock::copyInScene() {
bool TetrisBlock::isSamePos( const SDLPP::RenderObject &other ) const {
auto mypos = getPos();
auto otherpos = other.getPos();
auto diff1 = mypos.first - otherpos.first;
auto diff1 = mypos.getX() - otherpos.getX();
diff1 = ( diff1 < 0 ) * ( -1 ) * diff1 + ( diff1 > 0 ) * diff1;
auto diff2 = mypos.second - otherpos.second;
auto diff2 = mypos.getY() - otherpos.getY();
diff2 = ( diff2 < 0 ) * ( -1 ) * diff2 + ( diff2 > 0 ) * diff2;
return diff1 < 0.0001 && diff2 < 0.0001;
}
@@ -152,23 +152,23 @@ void TetrisPiece::rotate() {
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
auto position = piece->getPos();
original_pos[i] = position;
position.first += positions[0] * BLOCK_SIZE;
position.first -= positions[1] * BLOCK_SIZE;
position.second += positions[2] * BLOCK_SIZE;
position.second -= positions[3] * BLOCK_SIZE;
auto piece_position = piece->getPos();
original_pos[i] = piece_position;
piece_position += { positions[0] * BLOCK_SIZE -
positions[1] * BLOCK_SIZE,
positions[2] * BLOCK_SIZE -
positions[3] * BLOCK_SIZE };
auto bottom = positions[3];
auto top = positions[2];
positions[3] = positions[1];
positions[2] = positions[0];
positions[1] = top;
positions[0] = bottom;
position.first -= positions[0] * BLOCK_SIZE;
position.first += positions[1] * BLOCK_SIZE;
position.second -= positions[2] * BLOCK_SIZE;
position.second += positions[3] * BLOCK_SIZE;
piece->setPos( position.first, position.second );
piece_position += { positions[1] * BLOCK_SIZE -
positions[0] * BLOCK_SIZE,
positions[3] * BLOCK_SIZE -
positions[2] * BLOCK_SIZE };
piece->setPos( piece_position );
}
}
@@ -176,7 +176,7 @@ void TetrisPiece::revert() {
for ( unsigned long i = 0; i < pieces.size(); i++ ) {
auto &piece = pieces[i];
auto &positions = pieces_rel_position[i];
piece->setPos( original_pos[i].first, original_pos[i].second );
piece->setPos( original_pos[i] );
auto top = positions[1];
auto bottom = positions[0];
positions[1] = positions[3];
@@ -207,14 +207,16 @@ void TetrisPiece::setPos( const std::pair< double, double > &pos ) {
setPos( pos.first, pos.second );
}
std::pair< double, double > TetrisPiece::getPos() {
void TetrisPiece::setPos( const SDLPP::Vec2D<double> &vec ) {
setPos( vec.getX(), vec.getY() );
}
SDLPP::Vec2D<double> TetrisPiece::getPos() {
auto &piece = pieces[0];
auto &relpositions = pieces_rel_position[0];
auto pos = piece->getPos();
pos.first += relpositions[0] * BLOCK_SIZE;
pos.first -= relpositions[1] * BLOCK_SIZE;
pos.second += relpositions[2] * BLOCK_SIZE;
pos.second -= relpositions[3] * BLOCK_SIZE;
pos += { relpositions[0] * BLOCK_SIZE - relpositions[1] * BLOCK_SIZE,
relpositions[2] * BLOCK_SIZE - relpositions[3] * BLOCK_SIZE };
return pos;
}
@@ -258,7 +260,7 @@ bool TetrisPiece::isRight( const SDLPP::RenderObject &block ) const {
void TetrisPiece::movePiece( double x, double y ) {
for ( auto &block : getObjects() ) {
auto pos = block->getPos();
block->setPos( pos.first + x, pos.second + y );
block->setPos( pos + SDLPP::Vec2D<double>( x, y ) );
}
}
+3 -2
View File
@@ -40,7 +40,8 @@ public:
std::vector< std::shared_ptr< TetrisBlock > > &getObjects();
void setPos( double x, double y );
void setPos( const std::pair< double, double > &pos );
std::pair< double, double > getPos();
void setPos( const SDLPP::Vec2D<double> &vec );
SDLPP::Vec2D<double> getPos();
void clear();
void startDescend();
void stopDescend();
@@ -69,7 +70,7 @@ private:
std::vector< std::vector< int > > pieces_rel_position;
std::vector< std::shared_ptr< TetrisBlock > > pieces;
std::vector< std::pair< double, double > > original_pos;
std::vector< SDLPP::Vec2D<double> > original_pos;
bool descend = false;
int userMovement = 0;
bool rotate_allowed = true;
+6 -6
View File
@@ -27,19 +27,19 @@ void updateShadow( SDLPP::Scene &scene ) {
auto &invalid_objects = g_cur_object->getObjects();
for ( auto &x : g_cur_shadow->getObjects() ) {
auto block_pos = x->getPos();
if ( BOTTOM_BORDER - block_pos.second < shadow_drop )
shadow_drop = BOTTOM_BORDER - block_pos.second;
if ( BOTTOM_BORDER - block_pos.getY() < shadow_drop )
shadow_drop = BOTTOM_BORDER - block_pos.getY();
// set colider column's position to current block's X position
g_shadow_colider->setPos( block_pos.first, TOP_BORDER );
g_shadow_colider->setPos( block_pos.getX(), TOP_BORDER );
auto collisions =
scene.getCollisions( *g_shadow_colider, { BRICK_ID } );
auto curY = block_pos.second;
auto curY = block_pos.getY();
for ( auto &col : collisions ) {
// if collision with g_cur_object, ignore
if ( std::find( invalid_objects.begin(), invalid_objects.end(),
col ) != invalid_objects.end() )
continue;
auto possible_drop = col->getPos().second - curY;
auto possible_drop = col->getPos().getY() - curY;
if ( possible_drop < shadow_drop && possible_drop >= 0 )
shadow_drop = possible_drop;
}
@@ -47,7 +47,7 @@ void updateShadow( SDLPP::Scene &scene ) {
// we want the shadow to rest on top of the nearest floor
shadow_drop -= BLOCK_SIZE;
auto shadow_pos = g_cur_shadow->getPos();
g_cur_shadow->setPos( shadow_pos.first, shadow_pos.second + shadow_drop );
g_cur_shadow->setPos( shadow_pos.getX(), shadow_pos.getY() + shadow_drop );
}
void moveThem( std::shared_ptr< SDLPP::Scene > scene, int ticks ) {
+3 -3
View File
@@ -457,11 +457,11 @@ void mainSceneInput(
for ( auto &col : collisions ) {
col->destroy();
}
auto colider_y = colider->getPos().second;
auto colider_y = colider->getPos().getY();
for ( auto &elem : scene->getObjects( { BRICK_ID } ) ) {
auto pos = elem->getPos();
if ( pos.second < colider_y && pos.first <= RIGHT_BORDER ) {
elem->setPos( pos.first, pos.second + BLOCK_SIZE );
if ( pos.getY() < colider_y && pos.getX() <= RIGHT_BORDER ) {
elem->setPos( pos.getX(), pos.getY() + BLOCK_SIZE );
}
}
using namespace std::chrono_literals;