mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 13:43:41 +00:00
working controller input
-> does not work for main menu navigation
This commit is contained in:
parent
11a206fe0a
commit
04c4e19413
@ -11,6 +11,7 @@ constexpr std::size_t MAX_COMPONENTS = 32;
|
|||||||
constexpr std::size_t MAX_GROUPS = 32;
|
constexpr std::size_t MAX_GROUPS = 32;
|
||||||
constexpr std::size_t MAX_STATS = 8;
|
constexpr std::size_t MAX_STATS = 8;
|
||||||
constexpr std::size_t MAX_TEAMS = 8;
|
constexpr std::size_t MAX_TEAMS = 8;
|
||||||
|
constexpr std::size_t MAX_GAME_CONTROLLERS = 4;
|
||||||
|
|
||||||
constexpr int SCREEN_SIZE_HEIGHT = 640;
|
constexpr int SCREEN_SIZE_HEIGHT = 640;
|
||||||
constexpr int SCREEN_SIZE_WIDTH = 800;
|
constexpr int SCREEN_SIZE_WIDTH = 800;
|
||||||
|
|||||||
@ -2,10 +2,13 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
#include "Component.h"
|
#include "Component.h"
|
||||||
|
#include "SDL_gamecontroller.h"
|
||||||
#include "Vector2D.h"
|
#include "Vector2D.h"
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
#include "SoundManager.h"
|
#include "SoundManager.h"
|
||||||
|
|
||||||
|
#define MIN_JOYSTICK_INPUT 4096 // dpad stuck as -129 after input
|
||||||
|
|
||||||
class TransformComponent;
|
class TransformComponent;
|
||||||
class SpriteComponent;
|
class SpriteComponent;
|
||||||
|
|
||||||
@ -13,12 +16,16 @@ class KeyboardController : public Component
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TransformComponent* transform;
|
TransformComponent* transform;
|
||||||
|
|
||||||
|
SDL_GameController* gameController;
|
||||||
|
/*
|
||||||
const uint8_t* keystates = SDL_GetKeyboardState(NULL);
|
const uint8_t* keystates = SDL_GetKeyboardState(NULL);
|
||||||
SDL_Scancode up;
|
SDL_Scancode up;
|
||||||
SDL_Scancode down;
|
SDL_Scancode down;
|
||||||
SDL_Scancode left;
|
SDL_Scancode left;
|
||||||
SDL_Scancode right;
|
SDL_Scancode right;
|
||||||
SDL_Scancode fire;
|
SDL_Scancode fire;
|
||||||
|
*/
|
||||||
|
|
||||||
SpriteComponent* sprite;
|
SpriteComponent* sprite;
|
||||||
|
|
||||||
@ -27,7 +34,7 @@ public:
|
|||||||
uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
|
uint32_t fireCooldown = 1000; //in ms can be adjusted to change possible attack-speed
|
||||||
|
|
||||||
KeyboardController() = default;
|
KeyboardController() = default;
|
||||||
KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity);
|
KeyboardController(SDL_GameController* gameController, Vector2D fireVelocity);
|
||||||
~KeyboardController() = default;
|
~KeyboardController() = default;
|
||||||
|
|
||||||
void init() override;
|
void init() override;
|
||||||
|
|||||||
17
src/Game.cpp
17
src/Game.cpp
@ -9,6 +9,8 @@
|
|||||||
#include "Entity.h"
|
#include "Entity.h"
|
||||||
#include "HealthComponent.h"
|
#include "HealthComponent.h"
|
||||||
#include "Map.h"
|
#include "Map.h"
|
||||||
|
#include "SDL_gamecontroller.h"
|
||||||
|
#include "SDL_joystick.h"
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
#include "StatEffectsComponent.h"
|
#include "StatEffectsComponent.h"
|
||||||
#include "Constants.h"
|
#include "Constants.h"
|
||||||
@ -21,6 +23,7 @@ TextureManager* Game::textureManager = new TextureManager();
|
|||||||
SoundManager* Game::soundManager = new SoundManager();
|
SoundManager* Game::soundManager = new SoundManager();
|
||||||
|
|
||||||
CollisionHandler* Game::collisionHandler = new CollisionHandler(manager);
|
CollisionHandler* Game::collisionHandler = new CollisionHandler(manager);
|
||||||
|
std::array<SDL_GameController*, MAX_GAME_CONTROLLERS> gameControllers;
|
||||||
|
|
||||||
SDL_Renderer* Game::renderer = nullptr;
|
SDL_Renderer* Game::renderer = nullptr;
|
||||||
|
|
||||||
@ -37,6 +40,7 @@ Game::~Game() = default;
|
|||||||
|
|
||||||
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
void Game::init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen)
|
||||||
{
|
{
|
||||||
|
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,"1");
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
if (fullscreen)
|
if (fullscreen)
|
||||||
{
|
{
|
||||||
@ -50,6 +54,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0, c = 0; i < SDL_NumJoysticks(); i++) {
|
||||||
|
if (SDL_IsGameController(i)) {
|
||||||
|
gameControllers.at(c++) = SDL_GameControllerOpen(i);
|
||||||
|
}
|
||||||
|
if (c > MAX_GAME_CONTROLLERS) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
|
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
|
||||||
std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl;
|
std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl;
|
||||||
return;
|
return;
|
||||||
@ -162,7 +175,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
player1.setTeam(TeamLabel::BLUE);
|
player1.setTeam(TeamLabel::BLUE);
|
||||||
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
player1.addComponent<TransformComponent>(80,80,2); //posx, posy, scale
|
||||||
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
player1.addComponent<SpriteComponent>(player1Sprite, true); //adds sprite (32x32px), path needed
|
||||||
player1.addComponent<KeyboardController>(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D, SDL_SCANCODE_E, Vector2D(2, 0));//custom keycontrols can be added
|
player1.addComponent<KeyboardController>(gameControllers.at(0), Vector2D(2, 0));//custom keycontrols can be added
|
||||||
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
player1.addComponent<ColliderComponent>("player", 0.8f); //adds tag (for further use, reference tag)
|
||||||
player1.addComponent<HealthComponent>(5, Direction::LEFT);
|
player1.addComponent<HealthComponent>(5, Direction::LEFT);
|
||||||
player1.addComponent<StatEffectsComponent>();
|
player1.addComponent<StatEffectsComponent>();
|
||||||
@ -172,7 +185,7 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
|||||||
player2.setTeam(TeamLabel::RED);
|
player2.setTeam(TeamLabel::RED);
|
||||||
player2.addComponent<TransformComponent>(600, 500, 2);
|
player2.addComponent<TransformComponent>(600, 500, 2);
|
||||||
player2.addComponent<SpriteComponent>(player2Sprite, true);
|
player2.addComponent<SpriteComponent>(player2Sprite, true);
|
||||||
player2.addComponent<KeyboardController>(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_RCTRL, Vector2D(-2, 0));
|
player2.addComponent<KeyboardController>(gameControllers.at(1), Vector2D(-2, 0));
|
||||||
player2.addComponent<ColliderComponent>("enemy", 0.8f);
|
player2.addComponent<ColliderComponent>("enemy", 0.8f);
|
||||||
player2.addComponent<HealthComponent>(5, Direction::RIGHT);
|
player2.addComponent<HealthComponent>(5, Direction::RIGHT);
|
||||||
player2.addComponent<StatEffectsComponent>();
|
player2.addComponent<StatEffectsComponent>();
|
||||||
|
|||||||
@ -3,15 +3,12 @@
|
|||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "Components.h"
|
#include "Components.h"
|
||||||
#include "AssetManager.h"
|
#include "AssetManager.h"
|
||||||
|
#include "SDL_gamecontroller.h"
|
||||||
#include "SpriteComponent.h"
|
#include "SpriteComponent.h"
|
||||||
|
|
||||||
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity)
|
KeyboardController::KeyboardController(SDL_GameController* gameController, Vector2D fireVelocity)
|
||||||
{
|
{
|
||||||
this->up = up;
|
this->gameController = gameController;
|
||||||
this->down = down;
|
|
||||||
this->left = left;
|
|
||||||
this->right = right;
|
|
||||||
this->fire = fire;
|
|
||||||
this->fireVelocity = fireVelocity;
|
this->fireVelocity = fireVelocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,30 +24,30 @@ void KeyboardController::update()
|
|||||||
transform->direction.y = 0;
|
transform->direction.y = 0;
|
||||||
sprite->playAnimation(IDLE);
|
sprite->playAnimation(IDLE);
|
||||||
|
|
||||||
if (keystates[this->up]) {
|
if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 1) < MIN_JOYSTICK_INPUT * -1) { //
|
||||||
transform->direction.y = -1;
|
transform->direction.y = -1;
|
||||||
sprite->playAnimation(WALK);
|
sprite->playAnimation(WALK);
|
||||||
SoundManager::playSound(STEPS);
|
SoundManager::playSound(STEPS);
|
||||||
}
|
}
|
||||||
if (keystates[this->left]) {
|
if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 0) < MIN_JOYSTICK_INPUT * -1) {
|
||||||
transform->direction.x = -1;
|
transform->direction.x = -1;
|
||||||
sprite->playAnimation(WALK);
|
sprite->playAnimation(WALK);
|
||||||
sprite->setDirection(Direction::LEFT);
|
sprite->setDirection(Direction::LEFT);
|
||||||
SoundManager::playSound(STEPS);
|
SoundManager::playSound(STEPS);
|
||||||
}
|
}
|
||||||
if (keystates[this->down]) {
|
if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 1) > MIN_JOYSTICK_INPUT) {
|
||||||
transform->direction.y = 1;
|
transform->direction.y = 1;
|
||||||
sprite->playAnimation(WALK);
|
sprite->playAnimation(WALK);
|
||||||
SoundManager::playSound(STEPS);
|
SoundManager::playSound(STEPS);
|
||||||
}
|
}
|
||||||
if (keystates[this->right]) {
|
if (SDL_JoystickGetAxis(SDL_GameControllerGetJoystick(this->gameController), 0) > MIN_JOYSTICK_INPUT) {
|
||||||
transform->direction.x = 1;
|
transform->direction.x = 1;
|
||||||
sprite->playAnimation(WALK);
|
sprite->playAnimation(WALK);
|
||||||
sprite->setDirection(Direction::RIGHT);
|
sprite->setDirection(Direction::RIGHT);
|
||||||
SoundManager::playSound(STEPS);
|
SoundManager::playSound(STEPS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keystates[this->fire]) {
|
if (SDL_GameControllerGetButton(this->gameController, SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_A)) {
|
||||||
|
|
||||||
Uint32 currentTicks = SDL_GetTicks();
|
Uint32 currentTicks = SDL_GetTicks();
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user