2020-11-21 19:58:52 +00:00
|
|
|
#include "../sdlpp/sdlpp.hpp"
|
2020-09-11 13:05:41 +00:00
|
|
|
#include "config.hpp"
|
2020-09-11 19:34:43 +00:00
|
|
|
#include "custom_classes.hpp"
|
|
|
|
#include "scenes.hpp"
|
|
|
|
#include "global_vars.hpp"
|
|
|
|
#include "functions.hpp"
|
2020-08-22 23:15:46 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <mutex>
|
2020-09-12 11:09:20 +00:00
|
|
|
#ifdef _WIN32
|
2020-11-22 22:39:49 +00:00
|
|
|
#include "../sdlpp/SDL2/SDL2_framerate.h"
|
2020-09-12 11:09:20 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <string>
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2020-08-22 23:15:46 +00:00
|
|
|
#include <SDL2/SDL2_framerate.h>
|
2020-09-12 11:09:20 +00:00
|
|
|
#endif // UNIX
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-09-11 19:34:43 +00:00
|
|
|
std::vector< std::shared_ptr< SDLPP::RenderObject > > line_coliders{};
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2021-01-30 15:48:40 +00:00
|
|
|
void updateScore() {
|
|
|
|
g_score_texture->changeText( std::to_string( g_score ) );
|
2021-01-30 22:12:30 +00:00
|
|
|
g_score_texture->updateSizeAndPosition();
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 19:34:43 +00:00
|
|
|
void doInput() {
|
2020-08-22 23:15:46 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 200 );
|
2020-08-22 23:15:46 +00:00
|
|
|
auto base = SDL_GetTicks();
|
2020-09-11 19:34:43 +00:00
|
|
|
while ( !g_quit ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
base = SDL_GetTicks();
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-11-21 19:58:52 +00:00
|
|
|
g_input_functions.back()( g_main_scene, base, line_coliders );
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:58:52 +00:00
|
|
|
void prepareShadowColider( std::shared_ptr< SDLPP::Renderer > r ) {
|
|
|
|
g_shadow_colider = std::make_shared< SDLPP::RectangleRender >(
|
|
|
|
0, TOP_BORDER, BLOCK_SIZE, BOTTOM_BORDER - TOP_BORDER, r );
|
|
|
|
g_shadow_colider->addCollision(
|
|
|
|
SDLPP::RectColider( 0.1, 0.01, 0.8, 0.98 ) );
|
2020-09-11 19:34:43 +00:00
|
|
|
g_shadow_colider->setId( COLIDER_ID );
|
|
|
|
g_shadow_colider->setStatic();
|
2021-04-29 11:25:55 +00:00
|
|
|
g_shadow_colider->setAlignment(SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER);
|
2020-08-24 16:44:42 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 11:09:20 +00:00
|
|
|
#ifdef _WIN32
|
2020-11-21 19:58:52 +00:00
|
|
|
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|
|
|
PWSTR szCmdLine, int nCmdShow ) {
|
2020-09-12 11:09:20 +00:00
|
|
|
#else
|
2020-08-22 23:15:46 +00:00
|
|
|
int main() {
|
2020-09-12 11:09:20 +00:00
|
|
|
#endif
|
2020-08-22 23:15:46 +00:00
|
|
|
SDLPP::init();
|
2020-08-23 07:50:05 +00:00
|
|
|
SDLPP::Window w( "Tetris clone!" );
|
2020-11-21 19:58:52 +00:00
|
|
|
w.setResizable( true );
|
2020-09-11 19:34:43 +00:00
|
|
|
|
2020-08-23 07:50:05 +00:00
|
|
|
auto renderer = std::make_shared< SDLPP::Renderer >( w );
|
2020-09-11 19:34:43 +00:00
|
|
|
g_active_renderer = renderer;
|
2020-08-23 07:50:05 +00:00
|
|
|
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
|
2020-11-21 19:58:52 +00:00
|
|
|
prepareShadowColider( renderer );
|
2020-09-11 19:34:43 +00:00
|
|
|
|
2020-09-12 23:33:51 +00:00
|
|
|
g_font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2021-01-30 20:35:25 +00:00
|
|
|
g_font_config = std::make_shared< SDLPP::FontConfiguration >(
|
|
|
|
g_font, colors["text"], colors["text_out"], 0.1 );
|
|
|
|
g_main_scene = prepareMainScene( renderer );
|
2020-11-21 19:58:52 +00:00
|
|
|
line_coliders = g_main_scene->getObjects( { COLIDER_ID } );
|
|
|
|
g_score_texture = std::dynamic_pointer_cast< SDLPP::TextRenderer >(
|
|
|
|
g_main_scene->getObjects( { SCORE_TEXTURE_ID } )[0] );
|
2020-09-11 19:34:43 +00:00
|
|
|
g_active_scenes.push_back( g_main_scene );
|
|
|
|
g_main_scene->saveScene();
|
|
|
|
|
2021-01-30 20:35:25 +00:00
|
|
|
g_menu_scene = prepareMenuScene( renderer );
|
|
|
|
g_game_over_scene = prepareGameOverScene( renderer );
|
|
|
|
g_options_scene = prepareOptionsScene( renderer );
|
2020-08-22 23:15:46 +00:00
|
|
|
|
2020-08-23 13:30:45 +00:00
|
|
|
auto base = SDL_GetTicks();
|
2020-08-22 23:15:46 +00:00
|
|
|
int frames = 0;
|
2020-08-23 07:50:05 +00:00
|
|
|
std::srand( std::time( nullptr ) );
|
2020-09-11 19:34:43 +00:00
|
|
|
|
2020-08-22 23:15:46 +00:00
|
|
|
FPSmanager gFPS;
|
2020-08-23 07:50:05 +00:00
|
|
|
SDL_initFramerate( &gFPS );
|
|
|
|
SDL_setFramerate( &gFPS, 60 );
|
2020-09-11 19:34:43 +00:00
|
|
|
|
|
|
|
std::thread inputThread( doInput );
|
2020-08-22 23:15:46 +00:00
|
|
|
inputThread.detach();
|
2020-09-11 19:34:43 +00:00
|
|
|
|
|
|
|
g_next_object = g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
|
|
|
renderer, g_main_scene );
|
|
|
|
g_next_object->setPos( 0.9, 0.5 );
|
2020-11-21 19:58:52 +00:00
|
|
|
g_input_functions.push_back( mainSceneInput );
|
2020-09-11 19:34:43 +00:00
|
|
|
|
|
|
|
while ( !g_quit ) {
|
2020-09-12 11:09:20 +00:00
|
|
|
SDL_PumpEvents();
|
2020-08-24 06:48:25 +00:00
|
|
|
SDL_framerateDelay( &gFPS );
|
2020-09-11 19:34:43 +00:00
|
|
|
if ( !g_cur_object && g_checked_line ) {
|
|
|
|
std::lock_guard< std::mutex > guard( g_movement_mutex );
|
2020-09-26 18:13:27 +00:00
|
|
|
if ( !g_next_object ) {
|
2020-11-21 19:58:52 +00:00
|
|
|
g_next_object =
|
|
|
|
g_tetrisFunctions[std::rand() / ( ( RAND_MAX + 1u ) / 7 )](
|
|
|
|
g_main_scene->getRendererShared(), g_main_scene );
|
2020-09-26 18:13:27 +00:00
|
|
|
g_next_object->setPos( 0.9, 0.5 );
|
|
|
|
}
|
2020-09-13 12:14:39 +00:00
|
|
|
|
2020-09-11 19:34:43 +00:00
|
|
|
g_cur_object = g_next_object;
|
|
|
|
g_cur_object->setPos( 0.5, TOP_BORDER - BLOCK_SIZE );
|
|
|
|
g_cur_shadow = g_cur_object->copySelf();
|
|
|
|
g_cur_shadow->turnIntoShadow();
|
2020-11-21 19:58:52 +00:00
|
|
|
for ( auto &piece : g_cur_shadow->getObjects() ) {
|
2020-09-13 12:14:39 +00:00
|
|
|
// make it so shadow gets covered by g_cur_object
|
2020-11-21 19:58:52 +00:00
|
|
|
g_main_scene->moveZ( piece, -4 );
|
2020-09-10 14:50:36 +00:00
|
|
|
}
|
2020-11-21 19:58:52 +00:00
|
|
|
updateShadow( *g_main_scene );
|
2020-09-13 12:14:39 +00:00
|
|
|
|
2020-08-23 13:49:38 +00:00
|
|
|
auto rand_index = std::rand() / ( ( RAND_MAX + 1u ) / 7 );
|
|
|
|
int retries = 0;
|
2020-09-11 19:34:43 +00:00
|
|
|
while ( g_bag[rand_index] < 4 ) {
|
2020-08-24 06:48:25 +00:00
|
|
|
rand_index = ( rand_index + 1 ) % 7;
|
2020-08-23 13:49:38 +00:00
|
|
|
retries++;
|
2020-09-13 12:14:39 +00:00
|
|
|
// bag is empty
|
|
|
|
if ( retries == 7 ) {
|
|
|
|
g_game_over_scene->updateSizeAndPosition();
|
|
|
|
g_active_scenes.push_back( g_game_over_scene );
|
2020-11-21 19:58:52 +00:00
|
|
|
g_input_functions.push_back( gameOverSceneInput );
|
2020-09-13 12:14:39 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-08-23 13:49:38 +00:00
|
|
|
}
|
2020-09-11 19:34:43 +00:00
|
|
|
g_next_object.reset();
|
2020-11-21 19:58:52 +00:00
|
|
|
g_next_object =
|
|
|
|
g_tetrisFunctions[rand_index]( renderer, g_main_scene );
|
2020-09-11 19:34:43 +00:00
|
|
|
g_next_object->setPos( 0.9, 0.5 );
|
|
|
|
g_checked_line = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-09-11 19:34:43 +00:00
|
|
|
if ( g_update_score ) {
|
2021-01-30 15:48:40 +00:00
|
|
|
updateScore();
|
2020-09-11 19:34:43 +00:00
|
|
|
g_update_score = false;
|
|
|
|
}
|
|
|
|
if ( g_update_colors ) {
|
|
|
|
updateColors();
|
|
|
|
g_update_colors = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2021-01-30 20:35:25 +00:00
|
|
|
if ( g_update_3d ) {
|
|
|
|
updateBlocks();
|
|
|
|
g_update_3d = false;
|
|
|
|
}
|
2020-12-18 14:14:08 +00:00
|
|
|
if ( g_update_size ) {
|
|
|
|
updateSize();
|
|
|
|
g_update_size = false;
|
|
|
|
}
|
2021-03-09 08:27:15 +00:00
|
|
|
while ( !g_update_scenes.empty() ) {
|
2021-01-30 22:01:36 +00:00
|
|
|
g_update_scenes.back()->updateSizeAndPosition();
|
|
|
|
g_update_scenes.pop_back();
|
|
|
|
}
|
2021-03-09 08:27:15 +00:00
|
|
|
while ( !g_update_objects.empty() ) {
|
2021-01-30 22:01:36 +00:00
|
|
|
g_update_objects.back()->updateSizeAndPosition();
|
|
|
|
g_update_objects.pop_back();
|
|
|
|
}
|
|
|
|
|
2021-03-14 16:02:41 +00:00
|
|
|
{
|
|
|
|
std::lock_guard< std::mutex > guard( g_render_mutex );
|
|
|
|
renderer->clearRenderer();
|
|
|
|
for ( size_t i = 0; i < g_active_scenes.size(); i++ ) {
|
|
|
|
g_active_scenes[i]->updateScene();
|
|
|
|
g_active_scenes[i]->renderScene( false );
|
|
|
|
}
|
|
|
|
renderer->presentRenderer();
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|
2020-09-11 19:34:43 +00:00
|
|
|
g_wait_for_anim = false;
|
2020-08-22 23:15:46 +00:00
|
|
|
frames++;
|
2020-08-23 07:50:05 +00:00
|
|
|
if ( SDL_GetTicks() - base >= 1000 ) {
|
2020-08-22 23:15:46 +00:00
|
|
|
base = SDL_GetTicks();
|
2020-08-23 07:49:04 +00:00
|
|
|
std::cout << "FPS: " << frames << std::endl;
|
2020-08-22 23:15:46 +00:00
|
|
|
frames = 0;
|
|
|
|
}
|
|
|
|
}
|
2020-09-11 19:34:43 +00:00
|
|
|
|
2020-08-24 06:48:25 +00:00
|
|
|
return 0;
|
2020-08-22 23:15:46 +00:00
|
|
|
}
|