Implement basic input system

This commit is contained in:
Daniel Ramírez 2025-04-18 21:16:31 +02:00
parent 4aef21edd5
commit 3e8a191739
5 changed files with 102 additions and 29 deletions

View File

@ -1,8 +1,11 @@
#include "core/engine.h" #include "core/engine.h"
#include <memory>
#include <nds.h> #include <nds.h>
#include <stdio.h> #include <stdio.h>
#include <memory>
#include "input/input.h"
// Main program function // Main program function
int main(void) { int main(void) {
@ -12,7 +15,6 @@ int main(void) {
exit(1); exit(1);
engine->run(); engine->run();
engine->shutdown();
return 0; return 0;
} }
@ -22,36 +24,34 @@ std::unique_ptr<Engine> Engine::Create() {
} }
void Engine::run() { void Engine::run() {
isRunning = true;
while (pmMainLoop()) { while (pmMainLoop()) {
processInput(); processInput();
update(); update();
renderer->render(); render();
swiWaitForVBlank(); swiWaitForVBlank();
} }
} }
void Engine::shutdown() { isRunning = false; } void Engine::processInput() { input->update(); }
void Engine::processInput() {
scanKeys();
int keys = keysDown();
}
void Engine::update() { void Engine::update() {
// Update // Update
} }
void Engine::render() { renderer->render(); }
Engine::Engine() { Engine::Engine() {
isRunning = false;
// Debug
// consoleDemoInit();
// Initialize the graphics renderer // Initialize the graphics renderer
renderer = Renderer::Create(); renderer = Renderer::Create();
if (!renderer) if (!renderer)
exit(2); exit(2);
// Initialize the input system
input = Input::Create();
if (!input)
exit(3);
// Debug
consoleDemoInit();
} }

View File

@ -3,23 +3,73 @@
#include <memory> #include <memory>
/**
* @class Renderer
* @brief A base class for rendering operations.
*
* The Renderer class provides an interface for rendering graphics, including
* methods for managing frames, clearing the screen, and drawing objects.
*/
class Renderer { class Renderer {
public: public:
/**
* @brief Virtual destructor for the Renderer class.
*
* Ensures proper cleanup of derived classes.
*/
virtual ~Renderer() = default; virtual ~Renderer() = default;
/**
* @brief Factory method to create a Renderer instance.
*
* @return A unique pointer to a Renderer instance.
*/
static std::unique_ptr<Renderer> Create(); static std::unique_ptr<Renderer> Create();
/**
* @brief Renders the current frame.
*
* This method handles the rendering process, including frame management
* and drawing operations.
*/
void render(); void render();
private: private:
/**
* @brief Prepares the renderer for a new frame.
*
* This method is called at the beginning of the rendering process.
*/
void beginFrame(); void beginFrame();
/**
* @brief Finalizes the rendering of the current frame.
*
* This method is called at the end of the rendering process.
*/
void endFrame(); void endFrame();
/**
* @brief Clears the screen to prepare for rendering.
*
* This method resets the screen to a default state, typically clearing
* any previous drawings.
*/
void clearScreen(); void clearScreen();
/**
* @brief Draws a cube of the specified size.
*
* @param size The size of the cube to be drawn.
*/
void drawCube(float size); void drawCube(float size);
/**
* @brief Constructor for the Renderer class.
*
* The constructor is private to enforce the use of the Create() factory
* method.
*/
Renderer(); Renderer();
}; };

View File

@ -1,11 +1,17 @@
#include "input/input.h" #include "input/input.h"
#include <nds.h> #include <nds.h>
#include <stdio.h>
Input::Input() { Input::Input() {
// Initialize input system // Initialize input system
scanKeys(); scanKeys();
} }
std::unique_ptr<Input> Input::Create() {
return std::unique_ptr<Input>(new Input());
}
void Input::update() { void Input::update() {
// Update key states // Update key states
scanKeys(); scanKeys();
@ -14,8 +20,8 @@ void Input::update() {
keysUpState = keysUp(); keysUpState = keysUp();
} }
bool Input::isKeyHeld(int key) const { return keysHeldState & key; } bool Input::isKeyHeld(EKey key) const { return keysHeldState & key; }
bool Input::isKeyDown(int key) const { return keysDownState & key; } bool Input::isKeyDown(EKey key) const { return keysDownState & key; }
bool Input::isKeyUp(int key) const { return keysUpState & key; } bool Input::isKeyUp(EKey key) const { return keysUpState & key; }

View File

@ -5,6 +5,7 @@
#include "graphics/renderer.h" #include "graphics/renderer.h"
class Input;
class MainLoop; class MainLoop;
class Renderer; class Renderer;
@ -17,17 +18,14 @@ public:
// Run the main game loop // Run the main game loop
void run(); void run();
// Shutdown the engine
void shutdown();
private: private:
bool isRunning; std::unique_ptr<Renderer> renderer;
std::unique_ptr<Input> input;
// Internal methods for the game loop // Internal methods for the game loop
void processInput(); void processInput();
void update(); void update();
void render();
std::unique_ptr<Renderer> renderer;
Engine(); Engine();
}; };

View File

@ -1,24 +1,43 @@
#ifndef XRBDS_INPUT_H #ifndef XRBDS_INPUT_H
#define XRBDS_INPUT_H #define XRBDS_INPUT_H
#include <memory>
#include <calico/types.h> #include <calico/types.h>
#include <nds/input.h>
enum EKey : u16 {
A = KEY_A,
B = KEY_B,
X = KEY_X,
Y = KEY_Y,
L = KEY_L,
R = KEY_R,
START = KEY_START,
SELECT = KEY_SELECT,
EKEY_COUNT
};
class Input { class Input {
public: public:
Input(); virtual ~Input() = default;
static std::unique_ptr<Input> Create();
void update(); void update();
bool isKeyHeld(int key) const; bool isKeyHeld(EKey key) const;
bool isKeyDown(int key) const; bool isKeyDown(EKey key) const;
bool isKeyUp(int key) const; bool isKeyUp(EKey key) const;
private: private:
u32 keysHeldState = 0; u32 keysHeldState = 0;
u32 keysDownState = 0; u32 keysDownState = 0;
u32 keysUpState = 0; u32 keysUpState = 0;
Input();
}; };
#endif // XRBDS_INPUT_H #endif // XRBDS_INPUT_H