Skip to content

Commit

Permalink
working on errors for controller input
Browse files Browse the repository at this point in the history
  • Loading branch information
durkisneer1 committed Dec 10, 2024
1 parent f60b960 commit 22ff852
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 52 deletions.
13 changes: 3 additions & 10 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
#include <KrakenEngine.hpp>

#include "include/Player.hpp"

int main()
{
kn::window::init({320, 180}, "Night Terror", 4);
kn::time::Clock clock;
kn::camera = {-32, -26};

const kn::TileMap tileMap("../example/assets/room.tmx");

Player player(tileMap);

kn::Event event;
while (kn::window::isOpen())
{
const double dt = clock.tick();
clock.tick(30); // comment this out for the controller input to work

kn::window::pollEvent(event);
if (event.type == kn::KEYDOWN && event.key.keysym.sym == kn::K_ESCAPE)
kn::window::close();

kn::window::clear({21, 18, 37});

tileMap.drawMap();
player.update(dt);
const auto inputDir = kn::input::getVector();
kn::draw::circle(inputDir * 50 + kn::window::getSize() / 2, 10, {255, 255, 255});

kn::window::flip();
}
Expand Down
7 changes: 3 additions & 4 deletions include/Time.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class Clock final
~Clock() = default;

/**
* @brief Get the delta time between frames.
* @brief Get the delta time between frames in milliseconds.
*
* @param frameRate The frame rate to calculate the delta time.
* @param frameRate The frame rate to cap the program at.
*
* @return The delta time between frames.
*/
Expand All @@ -27,10 +27,9 @@ class Clock final
private:
double m_frameTime = 0.0;
double m_targetFrameTime = 0.0;
double m_deltaTime = 0.0;
double m_frequency = static_cast<double>(SDL_GetPerformanceFrequency());
double m_now = static_cast<double>(SDL_GetPerformanceCounter());
double m_last = static_cast<double>(SDL_GetPerformanceCounter());
double m_last = m_now;
};

/**
Expand Down
3 changes: 2 additions & 1 deletion include/_globals.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#pragma once

#include "Math.hpp"
#include <SDL.h>

namespace kn
{
inline SDL_GameController* _controller = nullptr;
inline SDL_GameController* _controller;
} // namespace kn
2 changes: 1 addition & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
option(
'build_example',
type : 'boolean',
value : false,
value : true,
description : 'Build example demo. Off by default.',
)

Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
meson
ninja
ninja
pre-commit
30 changes: 15 additions & 15 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#include "Math.hpp"
#include "Window.hpp"

#include <_globals.hpp>

namespace kn::input
{
// static float _deadZone = 0.1f;
static float _deadZone = 0.1f;

math::Vec2 getMousePos()
{
Expand Down Expand Up @@ -45,20 +47,18 @@ math::Vec2 getVector(const std::vector<KEYS>& left, const std::vector<KEYS>& rig
if (vector.getLength() > 1)
vector.normalize();

// if (_controller)
// {
// const double leftX = SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_LEFTX) /
// 32767.0; const double leftY = SDL_GameControllerGetAxis(_controller,
// SDL_CONTROLLER_AXIS_LEFTY) / 32767.0; if (std::abs(leftX) > _deadZone || std::abs(leftY)
// > _deadZone)
// {
// vector.x += leftX;
// vector.y += leftY;
// }
// }
//
// if (vector.getLength() > 1)
// vector.scaleToLength(1);
if (_controller)
{
math::Vec2 controllerDir = {
SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_LEFTX),
SDL_GameControllerGetAxis(_controller, SDL_CONTROLLER_AXIS_LEFTY)};
controllerDir /= 32767.0;
if (controllerDir.getLength() > _deadZone)
vector += controllerDir;
}

if (vector.getLength() > 1)
vector.scaleToLength(1);

return vector;
}
Expand Down
2 changes: 0 additions & 2 deletions src/tile_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ Rect TileMap::getFittedRect(const SDL_Surface* surface, const Rect& srcRect,
const int bytesPerPixel = surface->format->BytesPerPixel;

for (int y = rectInt.y; y < rectInt.y + rectInt.h; y++)
{
for (int x = rectInt.x; x < rectInt.x + rectInt.w; x++)
{
const Uint8* pixel = pixels + y * pitch + x * bytesPerPixel;
Expand All @@ -192,7 +191,6 @@ Rect TileMap::getFittedRect(const SDL_Surface* surface, const Rect& srcRect,
left = std::min(left, x - rectInt.x);
right = std::max(right, x - rectInt.x);
}
}

return {static_cast<int>(position.x + left), static_cast<int>(position.y + top),
right - left + 1, bottom - top + 1};
Expand Down
8 changes: 4 additions & 4 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ double Clock::tick(int frameRate)
frameRate = 1;

m_targetFrameTime = 1000.0 / frameRate;
const double currentTime = static_cast<double>(SDL_GetPerformanceCounter()) / m_frequency;
m_frameTime = (currentTime - m_last / m_frequency) * 1000.0;
m_now = static_cast<double>(SDL_GetPerformanceCounter());
m_frameTime = (m_now - m_last) / m_frequency * 1000.0;

if (m_frameTime < m_targetFrameTime)
SDL_Delay(static_cast<uint32_t>(m_targetFrameTime - m_frameTime));

m_now = static_cast<double>(SDL_GetPerformanceCounter());
m_deltaTime = (m_now - m_last) / m_frequency;
m_frameTime = (m_now - m_last) / m_frequency * 1000.0;
m_last = m_now;

return std::min(m_deltaTime, 0.033); // Limit delta time at 33ms or 30fps
return m_frameTime;
}

double getTicks() { return SDL_GetTicks() / 1000.0; }
Expand Down
28 changes: 14 additions & 14 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,20 +112,20 @@ void pollEvent(Event& event)
case QUIT:
close();
break;
// case CONTROLLERDEVICEADDED:
// {
// if (!_controller)
// _controller = SDL_GameControllerOpen(event.cdevice.which);
// break;
// }
// case CONTROLLERDEVICEREMOVED:
// if (_controller && event.cdevice.which == SDL_JoystickInstanceID(
// SDL_GameControllerGetJoystick(_controller)))
// {
// SDL_GameControllerClose(_controller);
// _controller = nullptr;
// }
// break;
case CONTROLLERDEVICEADDED:
{
if (!_controller)
_controller = SDL_GameControllerOpen(event.cdevice.which);
break;
}
case CONTROLLERDEVICEREMOVED:
if (_controller && event.cdevice.which ==
SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(_controller)))
{
SDL_GameControllerClose(_controller);
_controller = nullptr;
}
break;
default:
break;
}
Expand Down

0 comments on commit 22ff852

Please sign in to comment.