Added FEATURE flag, fixed rare segfault

This commit is contained in:
zvon 2021-03-09 09:27:15 +01:00
parent bb502b37f4
commit fbe122a5b9
6 changed files with 71 additions and 19 deletions

View File

@ -10,7 +10,7 @@ OUTPUTFLAG = -Fo
else
UNAME_S := $(shell uname -s)
CXX ?= g++
CXXFLAGS = -std=c++14 -Wall -Wextra -pedantic -O2
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

View File

@ -1,6 +1,9 @@
#include "custom_classes.hpp"
std::shared_ptr< SDLPP::Texture > TetrisBlock::block_texture = nullptr;
#ifdef FEATURE
const std::vector<SDL_Rect> blockanim = {{0,0,125,125}, {125,0,250,125}, {125,125,250,250}, {0,125,125,250}};
#endif
TetrisBlock::TetrisBlock( double x, double y, double w, double h,
const std::shared_ptr< SDLPP::Renderer > &r,
@ -13,11 +16,19 @@ TetrisBlock::TetrisBlock( double x, double y, double w, double h,
pieces_bag[_index]--;
_scene = scene;
setColors();
if ( TetrisBlock::block_texture == nullptr )
if ( TetrisBlock::block_texture == nullptr ) {
TetrisBlock::block_texture =
std::make_shared< SDLPP::Texture >( renderer, "block.png" );
TetrisBlock::block_texture->setAlpha(240);
}
if ( g_show_3d )
#ifdef FEATURE
setTexture( TetrisBlock::block_texture, 0, 0, 125, 125 );
setAnimationFrames( blockanim );
setAnimationSpeed(4);
#else
setTexture( TetrisBlock::block_texture );
#endif
}
TetrisBlock::TetrisBlock( const TetrisBlock &other )
@ -66,7 +77,11 @@ void TetrisBlock::specialAction( int code ) {
// fallthrough
case PIECE_ACTION_UPDATE_BLOCK:
if ( g_show_3d )
setTexture( "block.png" );
#ifdef FEATURE
setTexture( TetrisBlock::block_texture, 0, 0, 125, 125 );
#else
setTexture( TetrisBlock::block_texture );
#endif
else
unsetTexture();
default:

View File

@ -101,6 +101,9 @@ check_floor:
g_update_scenes.push_back(g_game_over_scene);
g_active_scenes.push_back( g_game_over_scene );
g_input_functions.push_back( gameOverSceneInput );
#ifdef FEATURE
pauseBlocks();
#endif
break;
}
}
@ -419,6 +422,19 @@ void updateBlocks() {
x->specialAction( PIECE_ACTION_UPDATE_BLOCK );
}
}
#ifdef FEATURE
void pauseBlocks() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->pauseAnimation();
}
}
void resumeBlocks() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {
x->resumeAnimation();
}
}
#endif
void updateColors() {
for ( auto &x : g_main_scene->getObjects( { BRICK_ID, SHADOW_ID } ) ) {

View File

@ -35,5 +35,9 @@ tetrisZLeft( std::shared_ptr< SDLPP::Renderer > renderer,
void updateColors();
void updateBlocks();
void updateSize();
#ifdef FEATURE
void pauseBlocks();
void resumeBlocks();
#endif
#endif

View File

@ -27,6 +27,12 @@ void addMainSceneItems( SDLPP::Scene &scene,
bg->setPermanent();
bg->setId( BACKGROUND_ID );
scene.addObject( bg );
#ifdef FEATURE
auto testcircle = std::make_shared< SDLPP::CircleRender >(
LEFT_BORDER, 0.2, 0.05, r, "#FF00AA", true );
testcircle->centerX();
scene.addObject( testcircle );
#endif
// create coliders for counting blocks in line
double posy = 1;
@ -311,6 +317,9 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
g_update_scenes.push_back( g_menu_scene );
g_active_scenes.push_back( g_menu_scene );
g_input_functions.push_back( menuSceneInput );
#ifdef FEATURE
pauseBlocks();
#endif
break;
case SDLK_LEFT:
case SDLK_a:
@ -490,6 +499,9 @@ void handleKeyDownMenu( SDL_Keycode key ) {
g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back();
g_input_functions.pop_back();
#ifdef FEATURE
resumeBlocks();
#endif
} break;
#ifdef DEBUG
case SDLK_r:
@ -599,6 +611,9 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
case SDLK_RETURN:
switch ( g_game_over_select ) {
case GAME_OVER_RESTART:
#ifdef FEATURE
resumeBlocks();
#endif
resetGame();
break;
case GAME_OVER_QUIT:
@ -693,7 +708,8 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_objects.push_back(g_options_options[OPTIONS_MENU_SHADOW]);
g_update_objects.push_back(
g_options_options[OPTIONS_MENU_SHADOW] );
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;
@ -726,7 +742,8 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) );
g_update_objects.push_back(g_options_options[OPTIONS_MENU_SHADOW]);
g_update_objects.push_back(
g_options_options[OPTIONS_MENU_SHADOW] );
break;
case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d;

View File

@ -148,19 +148,19 @@ int main() {
updateSize();
g_update_size = false;
}
for(size_t i = 0; i < g_update_scenes.size(); i++) {
while ( !g_update_scenes.empty() ) {
g_update_scenes.back()->updateSizeAndPosition();
g_update_scenes.pop_back();
}
for(size_t i = 0; i < g_update_objects.size(); i++) {
while ( !g_update_objects.empty() ) {
g_update_objects.back()->updateSizeAndPosition();
g_update_objects.pop_back();
}
renderer->clearRenderer();
for ( auto &x : g_active_scenes ) {
x->renderScene( false );
for ( size_t i = 0; i < g_active_scenes.size(); i++ ) {
g_active_scenes[i]->updateScene();
g_active_scenes[i]->renderScene( false );
}
renderer->presentRenderer();
g_wait_for_anim = false;