game/mario/main.cpp

268 lines
8.5 KiB
C++
Raw Normal View History

2021-04-25 20:42:55 +00:00
#include "../sdlpp/sdlpp.hpp"
#include "sprites.hpp"
#ifdef _WIN32
#include "../sdlpp/SDL2/SDL2_framerate.h"
#include <ctime>
#include <string>
#include <windows.h>
#else
#include <SDL2/SDL2_framerate.h>
#endif // UNIX
#include <thread>
2021-05-27 14:33:00 +00:00
#include <mutex>
2021-04-25 20:42:55 +00:00
#include "global_vars.hpp"
#include "objectids.hpp"
#include "blocks.hpp"
#include "maploader.hpp"
2021-05-22 21:13:26 +00:00
#include "mario.hpp"
#include "visitors/visitor_generator.hpp"
2021-04-25 20:42:55 +00:00
bool quit = false;
2021-05-25 18:21:02 +00:00
bool update = false;
2021-05-22 21:13:26 +00:00
std::shared_ptr< Mario > mario = nullptr;
2021-04-26 19:59:21 +00:00
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
2021-05-25 18:21:02 +00:00
std::shared_ptr< SDLPP::TextRenderer > fps = nullptr;
std::shared_ptr< SDLPP::TextRenderer > coins = nullptr;
int coin_count = 0;
2021-04-25 20:42:55 +00:00
std::vector< std::shared_ptr< MarioBlock > > moving_objects = {};
2021-05-27 14:33:00 +00:00
std::mutex render_mutex;
2021-04-25 20:42:55 +00:00
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
switch ( key ) {
case SDLK_ESCAPE:
quit = true;
break;
case SDLK_a:
2021-05-22 21:13:26 +00:00
mario->walkLeft();
2021-04-25 20:42:55 +00:00
break;
case SDLK_d:
2021-05-22 21:13:26 +00:00
mario->walkRight();
2021-04-25 20:42:55 +00:00
break;
case SDLK_SPACE:
case SDLK_w:
2021-05-22 21:54:01 +00:00
mario->jump();
2021-04-25 20:42:55 +00:00
break;
case SDLK_s:
break;
case SDLK_r:
scene.getRenderer().setRenderColiders(
!scene.getRenderer().getRenderColiders() );
2021-05-25 18:21:02 +00:00
break;
case SDLK_f:
if ( fps ) {
fps->setHidden( !fps->getHidden() );
}
2021-04-25 20:42:55 +00:00
default:
break;
}
}
void handleKeyUp( SDL_Keycode key ) {
switch ( key ) {
case SDLK_a:
2021-05-22 21:13:26 +00:00
mario->walkRight();
2021-04-25 20:42:55 +00:00
break;
case SDLK_d:
2021-05-22 21:13:26 +00:00
mario->walkLeft();
2021-04-25 20:42:55 +00:00
break;
2021-05-22 21:54:01 +00:00
case SDLK_SPACE:
2021-04-25 20:42:55 +00:00
case SDLK_w:
2021-05-22 21:54:01 +00:00
mario->stopJump();
2021-04-25 20:42:55 +00:00
default:
break;
}
}
void pollEvents( SDLPP::Scene &scene ) {
SDL_Event event;
while ( SDLPP::getSDLEvent( event ) ) {
switch ( event.type ) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if ( !event.key.repeat ) {
2021-04-25 20:42:55 +00:00
handleKeyDown( event.key.keysym.sym, scene );
}
2021-04-25 20:42:55 +00:00
break;
case SDL_KEYUP:
handleKeyUp( event.key.keysym.sym );
break;
case SDL_WINDOWEVENT:
2021-04-26 19:59:21 +00:00
if ( event.window.event == SDL_WINDOWEVENT_RESIZED ) {
2021-04-25 20:42:55 +00:00
scene.updateSizeAndPosition();
2021-04-26 19:59:21 +00:00
if ( leftStop ) {
auto rendDims = renderer->getDoubleDimensions();
auto left = rendDims.getX() < 2.0
? -( rendDims.getX() - 1 ) / 2.0 - 0.1
: -0.5;
leftStop->setPos( left, 0 );
}
auto dimensions = renderer->getDoubleDimensions();
auto leftmost = -( dimensions.getX() - 1 ) / 2.0;
auto rightmost = 1 - leftmost;
auto marioPos = mario->getPos();
if ( marioPos.getX() < leftmost ) {
scene.moveEverything( leftmost - marioPos.getX(), 0 );
2021-05-25 18:21:02 +00:00
update = true;
2021-04-26 19:59:21 +00:00
} else if ( marioPos.getX() > rightmost ) {
scene.moveEverything( marioPos.getX() - rightmost, 0 );
2021-05-25 18:21:02 +00:00
update = true;
2021-04-26 19:59:21 +00:00
}
if ( mario->getPos().getX() <
leftStop->getDoubleRect().first.getX() +
leftStop->getDoubleRect().second.getX() ) {
mario->setPos( leftStop->getDoubleRect().first.getX() +
leftStop->getDoubleRect().second.getX(),
mario->getPos().getY() );
}
}
2021-04-25 20:42:55 +00:00
default:
break;
}
}
}
void doInput( std::shared_ptr< SDLPP::Scene > scene ) {
FPSmanager gFPS;
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 200 );
while ( true ) {
SDL_framerateDelay( &gFPS );
pollEvents( *scene );
std::lock_guard< std::mutex > lock( render_mutex );
2021-04-25 20:42:55 +00:00
scene->updateScene();
auto prev_coin_count = coin_count;
// TODO visit all moving objects
auto visitor = getVisitor(*mario, *scene, quit, coin_count, moving_objects);
scene->visitCollisions( *mario, *visitor );
mario->handleVisitor( *visitor );
2021-05-22 21:13:26 +00:00
if(coin_count != prev_coin_count) {
coins->changeText( std::to_string( coin_count ) + " COINS" );
}
2021-05-22 21:13:26 +00:00
// if player is > 0.7 of playground, move everything left
2021-04-25 20:42:55 +00:00
auto playerX = mario->getRect().x;
auto width = scene->getWidth();
auto rightBarrier = width * 0.7;
auto rightmostX =
scene->rightmost()->getRect().x + scene->rightmost()->getRect().w;
scene->moveEverything(
( playerX > rightBarrier && rightmostX > width ) *
( rightBarrier - playerX ) / width,
0 );
2021-05-25 18:21:02 +00:00
update = true;
2021-04-25 20:42:55 +00:00
}
}
#ifdef _WIN32
int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR szCmdLine, int nCmdShow ) {
#else
int main() {
#endif
SDLPP::init();
SDLPP::Window w( "Mario clone!" );
w.setResizable( true );
2021-04-26 19:59:21 +00:00
renderer = std::make_shared< SDLPP::Renderer >( w );
2021-04-25 20:42:55 +00:00
renderer->setBlendMode( SDL_BLENDMODE_BLEND );
2021-04-26 19:59:21 +00:00
2021-04-25 20:42:55 +00:00
// prepare global vars
2021-04-26 19:59:21 +00:00
g_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
auto scene = std::make_shared< SDLPP::Scene >( renderer );
2021-05-26 16:24:09 +00:00
g_playground = scene;
2021-04-26 19:59:21 +00:00
auto bg = std::make_shared< SDLPP::RectangleRender >(
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
bg->setPermanent();
2021-04-26 19:59:21 +00:00
bg->setStatic();
bg->setId( 1 );
scene->addObject( bg );
2021-05-22 21:13:26 +00:00
g_mario_texture = std::make_shared< SDLPP::Texture >(
2021-04-26 19:59:21 +00:00
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture = std::make_shared< SDLPP::Texture >(
renderer, "sprites/terrain.png", MARIO_OVERWORLD_COLORKEY );
g_translucent_terrain_texture->setAlpha( 100 );
mario = std::make_shared< Mario >( renderer );
scene->addObject( mario );
2021-04-26 19:59:21 +00:00
auto defeat =
std::make_shared< SDLPP::RectangleRender >( 0, 1.01, 0, 0, renderer );
defeat->setId( DEATH_ID );
2021-04-27 13:56:13 +00:00
defeat->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2021-04-26 19:59:21 +00:00
defeat->setPermanent();
auto defeatCol = SDLPP::RectColider( -1, 0, -1, -1 );
2021-04-25 20:42:55 +00:00
defeatCol.setInfinite();
2021-04-26 19:59:21 +00:00
defeat->addCollision( defeatCol );
scene->addObject( defeat );
2021-04-25 20:42:55 +00:00
2021-04-26 19:59:21 +00:00
leftStop = std::make_shared< SDLPP::RectangleRender >( -0.1, 0, 0.11, 0,
renderer );
leftStop->setId( STOP_MOVEMENT );
2021-04-27 13:56:13 +00:00
leftStop->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
2021-04-26 19:59:21 +00:00
leftStop->setPermanent();
auto leftStopCol = SDLPP::RectColider( 0, -1, 1, -1 );
leftStopCol.setInfinite();
leftStop->addCollision( leftStopCol );
leftStop->setColiderColor( "#FF00FF" );
scene->addObject( leftStop );
2021-04-25 20:42:55 +00:00
loadMap( scene, mario, "test_binary2.bin" );
2021-04-25 20:42:55 +00:00
2021-05-25 18:21:02 +00:00
auto font = std::make_shared< SDLPP::Font >( "testfont.ttf", 36 );
fps = std::make_shared< SDLPP::TextRenderer >(
0.2, 0, 0.78, 0.1, renderer, font, "0fps", "#FFFFFF", "#000000", 0.1,
SDLPP_TEXT_RIGHT );
fps->setAlignment( SDLPP::OBJ_END, SDLPP::OBJ_START );
fps->setId( 0 );
2021-05-25 18:21:02 +00:00
fps->setPermanent();
fps->setHidden( true );
scene->addObject( fps );
2021-05-25 18:21:02 +00:00
coins = std::make_shared< SDLPP::TextRenderer >(
0.2, 0, 0.78, 0.1, renderer, font, "0 COINS", "#FFFFFF", "#000000", 0.1,
SDLPP_TEXT_RIGHT );
coins->setAlignment( SDLPP::OBJ_START, SDLPP::OBJ_START );
coins->setId( 0 );
coins->setPermanent();
scene->addObject( coins );
2021-04-25 20:42:55 +00:00
FPSmanager gFPS;
SDL_initFramerate( &gFPS );
SDL_setFramerate( &gFPS, 60 );
auto base = SDL_GetTicks();
int frames = 0;
std::thread inputThread( doInput, scene );
inputThread.detach();
2021-04-26 19:59:21 +00:00
scene->moveEverything( -mario->getDoubleRect().first.getX() + 0.2, 0 );
2021-05-25 18:21:02 +00:00
update = true;
2021-04-25 20:42:55 +00:00
while ( !quit ) {
SDL_PumpEvents();
2021-04-26 19:59:21 +00:00
SDL_framerateDelay( &gFPS );
std::lock_guard< std::mutex > lock( render_mutex );
2021-05-22 21:13:26 +00:00
mario->setStanding();
if ( update ) {
2021-05-25 18:21:02 +00:00
scene->updateSizeAndPosition();
update = false;
}
2021-04-25 20:42:55 +00:00
scene->renderScene();
renderer->presentRenderer();
frames++;
if ( SDL_GetTicks() - base >= 1000 ) {
fps->changeText( std::to_string( frames ) + " fps" );
2021-04-25 20:42:55 +00:00
frames = 0;
base = SDL_GetTicks();
}
}
return 0;
}