I've been dabbling in C++ graphics programming as a hobby, but I've noticed that I struggle with making decisions on designs and solutions. I feel like my indecisiveness stems from not fully understanding the options available to me. While I often learn from books or open source projects, I find myself unsure about why certain methods are chosen. Currently, I'm working on input handling for my engine and I've come across a couple of approaches. One option is to use singletons for the keyboard and mouse:
```cpp
class Keyboard : public Singleton {};
class Mouse : public Singleton {};
```
This seems logical because it allows me to easily check the keyboard state when needed. Alternatively, I could structure it like this:
```cpp
class UserInput
{
private:
InputDevice* keyboard;
InputDevice* mouse;
};
```
I often have similar indecisiveness with various aspects of my projects. How do others settle on a design or solution?
5 Answers
For a simple, single-player engine, using singletons for Keyboard and Mouse is a reasonable choice. It's clean and intuitive. However, if you ever plan to expand your project to support multiplayer or sophisticated input systems, you might want to explore more abstract designs later on. Start simple and evolve when necessary!
The key is to first focus on functionality, then refine the aesthetics. Aim for a minimum viable product and iterate from there. As you gain experience, you'll pick design patterns that make your code easier to manage and extend.
Consider learning refactoring as an essential skill. Get started on a basic architecture, then refine it as your project develops. Use resources like the book "Pragmatic Programmer" to deepen your understanding of design patterns and software architecture. Your project’s direction will become clearer as you build out its foundational elements.
Just pick the simplest option that seems feasible, and keep progressing. If you make a wrong choice, you'll learn and adapt quickly. Consider creating functions to check key states or mouse positions directly; it’ll streamline things. Your coding journey is all about experimentation!
Both designs you've proposed are valid, but singletons may work best in your case if you don't need multiple instances of input devices. You might require multiple states for each device, especially in complex environments. These singletons often help simplify things by getting the whole state at once. For instance, capturing keyboard input could look like this:
```cpp
KeyboardState keyState = Keyboard.getState();
if (keyState.KeyW) { /* action */ }
```

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically