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>
|
|
|
|
#include "global_vars.hpp"
|
|
|
|
#include "objectids.hpp"
|
|
|
|
#include "blocks.hpp"
|
|
|
|
#include "maploader.hpp"
|
|
|
|
#include "mario_visitor.hpp"
|
|
|
|
|
|
|
|
#define SIDE_MOVEMENT 0.8
|
|
|
|
#define FALL_MOVEMENT 1
|
|
|
|
|
|
|
|
bool quit = false;
|
2021-04-26 19:59:21 +00:00
|
|
|
std::shared_ptr< SDLPP::RectangleRender > mario = nullptr;
|
|
|
|
std::shared_ptr< SDLPP::Texture > mario_texture = nullptr;
|
|
|
|
std::shared_ptr< SDLPP::RectangleRender > leftStop = nullptr;
|
|
|
|
std::shared_ptr< SDLPP::Renderer > renderer = nullptr;
|
2021-04-29 11:08:08 +00:00
|
|
|
bool mario_facing_right = true;
|
2021-04-25 20:42:55 +00:00
|
|
|
|
|
|
|
void setMarioStanding() {
|
2021-04-26 19:59:21 +00:00
|
|
|
if ( mario->getMovement().getX() == 0 ) {
|
2021-04-25 20:42:55 +00:00
|
|
|
mario->pauseAnimation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleKeyDown( SDL_Keycode key, SDLPP::Scene &scene ) {
|
|
|
|
switch ( key ) {
|
|
|
|
case SDLK_ESCAPE:
|
|
|
|
quit = true;
|
|
|
|
break;
|
|
|
|
case SDLK_a:
|
|
|
|
mario->resumeAnimation();
|
|
|
|
mario->addMovement( -SIDE_MOVEMENT, 0 );
|
2021-04-29 11:08:08 +00:00
|
|
|
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
|
|
|
|
mario->flipHorizontally();
|
|
|
|
mario_facing_right = false;
|
|
|
|
}
|
2021-04-25 20:42:55 +00:00
|
|
|
break;
|
|
|
|
case SDLK_d:
|
|
|
|
mario->resumeAnimation();
|
|
|
|
mario->addMovement( SIDE_MOVEMENT, 0 );
|
2021-04-29 11:08:08 +00:00
|
|
|
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
|
|
|
|
mario->flipHorizontally();
|
|
|
|
mario_facing_right = true;
|
|
|
|
}
|
2021-04-25 20:42:55 +00:00
|
|
|
break;
|
|
|
|
case SDLK_SPACE:
|
|
|
|
case SDLK_w:
|
|
|
|
break;
|
|
|
|
case SDLK_s:
|
|
|
|
break;
|
|
|
|
case SDLK_r:
|
|
|
|
scene.getRenderer().setRenderColiders(
|
|
|
|
!scene.getRenderer().getRenderColiders() );
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleKeyUp( SDL_Keycode key ) {
|
|
|
|
switch ( key ) {
|
|
|
|
case SDLK_a:
|
|
|
|
mario->resumeAnimation();
|
|
|
|
mario->addMovement( SIDE_MOVEMENT, 0 );
|
2021-04-29 11:08:08 +00:00
|
|
|
if ( mario->getMovement().getX() > 0 && !mario_facing_right ) {
|
|
|
|
mario->flipHorizontally();
|
|
|
|
mario_facing_right = true;
|
|
|
|
}
|
2021-04-25 20:42:55 +00:00
|
|
|
break;
|
|
|
|
case SDLK_d:
|
|
|
|
mario->resumeAnimation();
|
|
|
|
mario->addMovement( -SIDE_MOVEMENT, 0 );
|
2021-04-29 11:08:08 +00:00
|
|
|
if ( mario->getMovement().getX() < 0 && mario_facing_right ) {
|
|
|
|
mario->flipHorizontally();
|
|
|
|
mario_facing_right = false;
|
|
|
|
}
|
2021-04-25 20:42:55 +00:00
|
|
|
break;
|
|
|
|
case SDLK_w:
|
|
|
|
case SDLK_s:
|
|
|
|
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 )
|
|
|
|
handleKeyDown( event.key.keysym.sym, scene );
|
|
|
|
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 );
|
|
|
|
} else if ( marioPos.getX() > rightmost ) {
|
|
|
|
scene.moveEverything( marioPos.getX() - rightmost, 0 );
|
|
|
|
}
|
|
|
|
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 );
|
2021-04-26 19:59:21 +00:00
|
|
|
auto prevPos = mario->getDoubleRect().first;
|
2021-04-25 20:42:55 +00:00
|
|
|
scene->updateScene();
|
|
|
|
MarioVisitor mv{};
|
2021-04-26 19:59:21 +00:00
|
|
|
scene->visitCollisions( *mario, mv );
|
|
|
|
if ( mv.isDead() ) {
|
2021-04-25 20:42:55 +00:00
|
|
|
quit = true;
|
|
|
|
}
|
2021-04-26 19:59:21 +00:00
|
|
|
if ( !mv.isOnGround() ) {
|
|
|
|
mario->setMovement( mario->getMovement().getX(), FALL_MOVEMENT );
|
2021-04-25 20:42:55 +00:00
|
|
|
} else {
|
|
|
|
mario->resetMovementY();
|
|
|
|
}
|
2021-04-29 11:08:08 +00:00
|
|
|
if ( mv.isStopped() ||
|
|
|
|
( !mv.canGoLeft() && prevPos.getX() > mario->getPos().getX() ) ||
|
|
|
|
( !mv.canGoRight() && prevPos.getX() < mario->getPos().getX() ) ) {
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->setPos( prevPos.getX(), mario->getPos().getY() );
|
|
|
|
}
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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 );
|
|
|
|
auto bg = std::make_shared< SDLPP::RectangleRender >(
|
|
|
|
0, 0, 10, 10, renderer, MARIO_OVERWORLD_COLORKEY, true );
|
2021-05-07 21:17:05 +00:00
|
|
|
bg->setPermanent();
|
2021-04-26 19:59:21 +00:00
|
|
|
bg->setStatic();
|
|
|
|
bg->setId( 1 );
|
|
|
|
scene->addObject( bg );
|
|
|
|
mario_texture = std::make_shared< SDLPP::Texture >(
|
|
|
|
renderer, "sprites/mario.png", MARIO_OVERWORLD_COLORKEY );
|
|
|
|
mario = std::make_shared< SDLPP::RectangleRender >(
|
|
|
|
0, 0, BLOCK_SIZE, BLOCK_SIZE, renderer, mario_texture,
|
2021-04-30 07:10:58 +00:00
|
|
|
MARIO_STANDING_SRC );
|
|
|
|
mario->setAnimationFrames( MARIO_WALK_ANIM );
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->setId( 2 );
|
2021-04-27 13:56:13 +00:00
|
|
|
mario->setAlignment( SDLPP::OBJ_CENTER, SDLPP::OBJ_CENTER );
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->setAnimationSpeed( 12.5 );
|
2021-04-25 20:42:55 +00:00
|
|
|
mario->pauseAnimation();
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->setMovement( 0, 0 );
|
|
|
|
mario->setMovementSpeed( 0.4 );
|
|
|
|
mario->addCollision(
|
|
|
|
SDLPP::RectColider( 0.21, 0.85, 0.65, 0.16, MARIO_FLOOR_DETECT ) );
|
|
|
|
mario->addCollision(
|
2021-05-01 19:55:43 +00:00
|
|
|
SDLPP::RectColider( 0, 0.1, 0.1, 0.8, MARIO_LEFT_SIDE_DETECT ) );
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->addCollision(
|
2021-05-01 19:55:43 +00:00
|
|
|
SDLPP::RectColider( 0.9, 0.1, 0.1, 0.8, MARIO_RIGHT_SIDE_DETECT ) );
|
2021-04-26 19:59:21 +00:00
|
|
|
mario->setStatic( false );
|
|
|
|
scene->addObject( mario );
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-05-01 19:55:43 +00:00
|
|
|
loadMap( scene, mario, "test_binary2.bin", renderer );
|
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-04-25 20:42:55 +00:00
|
|
|
while ( !quit ) {
|
|
|
|
SDL_PumpEvents();
|
2021-04-26 19:59:21 +00:00
|
|
|
SDL_framerateDelay( &gFPS );
|
2021-04-25 20:42:55 +00:00
|
|
|
setMarioStanding();
|
|
|
|
scene->renderScene();
|
|
|
|
renderer->presentRenderer();
|
|
|
|
frames++;
|
|
|
|
if ( SDL_GetTicks() - base >= 1000 ) {
|
|
|
|
std::cout << "FPS: " << frames << std::endl;
|
|
|
|
frames = 0;
|
|
|
|
base = SDL_GetTicks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|