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 else
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
CXX ?= g++ 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 OBJEXT = o
LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread LDFLAGS ?= -lSDL2 -lSDL2_image -lSDL2_gfx -lSDL2_ttf -pthread
OUTPUTFLAG = -o OUTPUTFLAG = -o

View File

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

View File

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

View File

@ -27,6 +27,12 @@ void addMainSceneItems( SDLPP::Scene &scene,
bg->setPermanent(); bg->setPermanent();
bg->setId( BACKGROUND_ID ); bg->setId( BACKGROUND_ID );
scene.addObject( bg ); 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 // create coliders for counting blocks in line
double posy = 1; double posy = 1;
@ -311,6 +317,9 @@ void handleKeyDownMain( SDL_Keycode key, SDLPP::Scene &scene ) {
g_update_scenes.push_back( g_menu_scene ); g_update_scenes.push_back( g_menu_scene );
g_active_scenes.push_back( g_menu_scene ); g_active_scenes.push_back( g_menu_scene );
g_input_functions.push_back( menuSceneInput ); g_input_functions.push_back( menuSceneInput );
#ifdef FEATURE
pauseBlocks();
#endif
break; break;
case SDLK_LEFT: case SDLK_LEFT:
case SDLK_a: case SDLK_a:
@ -422,7 +431,7 @@ void pollEventsMain( SDLPP::Scene &scene ) {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for ( auto &x : g_active_scenes ) for ( auto &x : g_active_scenes )
g_update_scenes.push_back(x); g_update_scenes.push_back( x );
g_update_size = true; g_update_size = true;
} }
default: default:
@ -490,6 +499,9 @@ void handleKeyDownMenu( SDL_Keycode key ) {
g_main_scene->setPrevTicks( SDL_GetTicks() ); g_main_scene->setPrevTicks( SDL_GetTicks() );
g_active_scenes.pop_back(); g_active_scenes.pop_back();
g_input_functions.pop_back(); g_input_functions.pop_back();
#ifdef FEATURE
resumeBlocks();
#endif
} break; } break;
#ifdef DEBUG #ifdef DEBUG
case SDLK_r: case SDLK_r:
@ -523,7 +535,7 @@ void handleKeyDownMenu( SDL_Keycode key ) {
g_input_functions.pop_back(); g_input_functions.pop_back();
} break; } break;
case MAIN_MENU_OPTIONS: case MAIN_MENU_OPTIONS:
g_update_scenes.push_back(g_options_scene); g_update_scenes.push_back( g_options_scene );
g_active_scenes.push_back( g_options_scene ); g_active_scenes.push_back( g_options_scene );
g_input_functions.push_back( optionsSceneInput ); g_input_functions.push_back( optionsSceneInput );
break; break;
@ -554,7 +566,7 @@ void pollEventsMenu() {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for ( auto &x : g_active_scenes ) for ( auto &x : g_active_scenes )
g_update_scenes.push_back(x); g_update_scenes.push_back( x );
g_update_size = true; g_update_size = true;
} }
default: default:
@ -599,6 +611,9 @@ void handleKeyDownGameOver( SDL_Keycode key ) {
case SDLK_RETURN: case SDLK_RETURN:
switch ( g_game_over_select ) { switch ( g_game_over_select ) {
case GAME_OVER_RESTART: case GAME_OVER_RESTART:
#ifdef FEATURE
resumeBlocks();
#endif
resetGame(); resetGame();
break; break;
case GAME_OVER_QUIT: case GAME_OVER_QUIT:
@ -625,7 +640,7 @@ void pollEventsGameOver() {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for ( auto &x : g_active_scenes ) for ( auto &x : g_active_scenes )
g_update_scenes.push_back(x); g_update_scenes.push_back( x );
g_update_size = true; g_update_size = true;
} }
default: default:
@ -693,7 +708,8 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() ) g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) + ->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) ); ( 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; break;
case OPTIONS_MENU_3D: case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d; g_show_3d = !g_show_3d;
@ -701,7 +717,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_3D].get() ) g_options_options[OPTIONS_MENU_3D].get() )
->changeText( std::string( "Show block texture: " ) + ->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) ); ( g_show_3d ? "YES" : "NO" ) );
g_update_objects.push_back(g_options_options[OPTIONS_MENU_3D]); g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
g_update_3d = true; g_update_3d = true;
default: default:
break; break;
@ -726,7 +742,8 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_SHADOW].get() ) g_options_options[OPTIONS_MENU_SHADOW].get() )
->changeText( std::string( "Show shadow: " ) + ->changeText( std::string( "Show shadow: " ) +
( g_show_shadow ? "YES" : "NO" ) ); ( 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; break;
case OPTIONS_MENU_3D: case OPTIONS_MENU_3D:
g_show_3d = !g_show_3d; g_show_3d = !g_show_3d;
@ -734,7 +751,7 @@ void handleKeyDownOptions( SDL_Keycode key ) {
g_options_options[OPTIONS_MENU_3D].get() ) g_options_options[OPTIONS_MENU_3D].get() )
->changeText( std::string( "Show block texture: " ) + ->changeText( std::string( "Show block texture: " ) +
( g_show_3d ? "YES" : "NO" ) ); ( g_show_3d ? "YES" : "NO" ) );
g_update_objects.push_back(g_options_options[OPTIONS_MENU_3D]); g_update_objects.push_back( g_options_options[OPTIONS_MENU_3D] );
g_update_3d = true; g_update_3d = true;
default: default:
break; break;
@ -774,7 +791,7 @@ void pollEventsOptions() {
case SDL_WINDOWEVENT: case SDL_WINDOWEVENT:
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) { if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
for ( auto &x : g_active_scenes ) for ( auto &x : g_active_scenes )
g_update_scenes.push_back(x); g_update_scenes.push_back( x );
g_update_size = true; g_update_size = true;
} }
default: default:

View File

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