Input processor interface and sdl implementation added, now frees text textures after displaying text, need to look into caching so we can let manager handle it
This commit is contained in:
@@ -24,7 +24,8 @@ void Engine::start_loop() {
|
||||
auto last_frame = std::chrono::high_resolution_clock::now();
|
||||
while(true) {
|
||||
auto starttime = std::chrono::high_resolution_clock::now();
|
||||
bool end = game->update();
|
||||
auto input = this->input_processor->process_input();
|
||||
bool end = game->update(input);
|
||||
if(end) {
|
||||
break;
|
||||
}
|
||||
@@ -37,9 +38,11 @@ void Engine::start_loop() {
|
||||
}
|
||||
}
|
||||
|
||||
Engine::Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer) {
|
||||
Engine::Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
||||
std::unique_ptr<InputProcessor> input_processor) {
|
||||
this->renderer = std::move(renderer);
|
||||
this->game = std::move(game);
|
||||
this->input_processor = std::move(input_processor);
|
||||
}
|
||||
|
||||
std::chrono::time_point<std::chrono::system_clock>
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "game/game.hpp"
|
||||
#include <fmt/format.h>
|
||||
#include "rendering/renderer.hpp"
|
||||
#include "input/inputprocessor.hpp"
|
||||
static const std::string MISSING_GAME = "Game pointer is null";
|
||||
static const std::string MISSING_RENDERER = "Renderer pointer is null";
|
||||
static const bool SHOW_FPS = true;
|
||||
@@ -21,7 +22,8 @@ static const TextRenderDetails DEFAULT_FONT = TextRenderDetails(
|
||||
);
|
||||
class Engine {
|
||||
public:
|
||||
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer);
|
||||
Engine(std::unique_ptr<Game> game, std::unique_ptr<Renderer> renderer,
|
||||
std::unique_ptr<InputProcessor> input_processor);
|
||||
void start_loop();
|
||||
std::chrono::time_point<std::chrono::system_clock> render_fps(std::chrono::time_point<std::chrono::system_clock> end, std::chrono::time_point<std::chrono::system_clock> lastframe);
|
||||
|
||||
@@ -30,6 +32,7 @@ public:
|
||||
private:
|
||||
std::unique_ptr<Game> game = nullptr;
|
||||
std::unique_ptr<Renderer> renderer = nullptr;
|
||||
std::unique_ptr<InputProcessor> input_processor = nullptr;
|
||||
};
|
||||
|
||||
class MissingGameException : public std::exception {
|
||||
|
||||
@@ -5,9 +5,10 @@
|
||||
#ifndef RLA_IIPP_GAME_HPP
|
||||
#define RLA_IIPP_GAME_HPP
|
||||
#include "../rendering/renderer.hpp"
|
||||
#include "../input/inputprocessor.hpp"
|
||||
class Game {
|
||||
public:
|
||||
virtual bool update() = 0;
|
||||
virtual bool update(InputResult input) = 0;
|
||||
virtual void render(Renderer* renderer) = 0;
|
||||
virtual RendererParams get_renderer_params() = 0;
|
||||
virtual unsigned int get_framerate() = 0;
|
||||
|
||||
274
src/engine/input/inputprocessor.hpp
Normal file
274
src/engine/input/inputprocessor.hpp
Normal file
@@ -0,0 +1,274 @@
|
||||
//
|
||||
// Created by m on 12/6/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_INPUTPROCESSOR_HPP
|
||||
#define RLA_IIPP_INPUTPROCESSOR_HPP
|
||||
|
||||
enum InputProcessorKeycode {
|
||||
KEY_UNKNOWN,
|
||||
|
||||
KEY_RETURN,
|
||||
KEY_ESCAPE,
|
||||
KEY_BACKSPACE,
|
||||
KEY_TAB,
|
||||
KEY_SPACE,
|
||||
KEY_EXCLAIM,
|
||||
KEY_QUOTEDBL,
|
||||
KEY_HASH,
|
||||
KEY_PERCENT,
|
||||
KEY_DOLLAR,
|
||||
KEY_AMPERSAND,
|
||||
KEY_QUOTE,
|
||||
KEY_LEFTPAREN,
|
||||
KEY_RIGHTPAREN,
|
||||
KEY_ASTERISK,
|
||||
KEY_PLUS,
|
||||
KEY_COMMA,
|
||||
KEY_MINUS,
|
||||
KEY_PERIOD,
|
||||
KEY_SLASH,
|
||||
KEY_0,
|
||||
KEY_1,
|
||||
KEY_2,
|
||||
KEY_3,
|
||||
KEY_4,
|
||||
KEY_5,
|
||||
KEY_6,
|
||||
KEY_7,
|
||||
KEY_8,
|
||||
KEY_9,
|
||||
KEY_COLON,
|
||||
KEY_SEMICOLON,
|
||||
KEY_LESS,
|
||||
KEY_EQUALS,
|
||||
KEY_GREATER,
|
||||
KEY_QUESTION,
|
||||
KEY_AT,
|
||||
|
||||
KEY_LEFTBRACKET,
|
||||
KEY_BACKSLASH,
|
||||
KEY_RIGHTBRACKET,
|
||||
KEY_CARET,
|
||||
KEY_UNDERSCORE,
|
||||
KEY_BACKQUOTE,
|
||||
KEY_a,
|
||||
KEY_b,
|
||||
KEY_c,
|
||||
KEY_d,
|
||||
KEY_e,
|
||||
KEY_f,
|
||||
KEY_g,
|
||||
KEY_h,
|
||||
KEY_i,
|
||||
KEY_j,
|
||||
KEY_k,
|
||||
KEY_l,
|
||||
KEY_m,
|
||||
KEY_n,
|
||||
KEY_o,
|
||||
KEY_p,
|
||||
KEY_q,
|
||||
KEY_r,
|
||||
KEY_s,
|
||||
KEY_t,
|
||||
KEY_u,
|
||||
KEY_v,
|
||||
KEY_w,
|
||||
KEY_x,
|
||||
KEY_y,
|
||||
KEY_z,
|
||||
|
||||
KEY_CAPSLOCK,
|
||||
|
||||
KEY_F1,
|
||||
KEY_F2,
|
||||
KEY_F3,
|
||||
KEY_F4,
|
||||
KEY_F5,
|
||||
KEY_F6,
|
||||
KEY_F7,
|
||||
KEY_F8,
|
||||
KEY_F9,
|
||||
KEY_F10,
|
||||
KEY_F11,
|
||||
KEY_F12,
|
||||
|
||||
KEY_PRINTSCREEN,
|
||||
KEY_SCROLLLOCK,
|
||||
KEY_PAUSE,
|
||||
KEY_INSERT,
|
||||
KEY_HOME,
|
||||
KEY_PAGEUP,
|
||||
KEY_DELETE,
|
||||
KEY_END,
|
||||
KEY_PAGEDOWN,
|
||||
KEY_RIGHT,
|
||||
KEY_LEFT,
|
||||
KEY_DOWN,
|
||||
KEY_UP,
|
||||
|
||||
KEY_NUMLOCKCLEAR,
|
||||
KEY_KP_DIVIDE,
|
||||
KEY_KP_MULTIPLY,
|
||||
KEY_KP_MINUS,
|
||||
KEY_KP_PLUS,
|
||||
KEY_KP_ENTER,
|
||||
KEY_KP_1,
|
||||
KEY_KP_2,
|
||||
KEY_KP_3,
|
||||
KEY_KP_4,
|
||||
KEY_KP_5,
|
||||
KEY_KP_6,
|
||||
KEY_KP_7,
|
||||
KEY_KP_8,
|
||||
KEY_KP_9,
|
||||
KEY_KP_0,
|
||||
KEY_KP_PERIOD,
|
||||
|
||||
KEY_APPLICATION,
|
||||
KEY_POWER,
|
||||
KEY_KP_EQUALS,
|
||||
KEY_F13,
|
||||
KEY_F14,
|
||||
KEY_F15,
|
||||
KEY_F16,
|
||||
KEY_F17,
|
||||
KEY_F18,
|
||||
KEY_F19,
|
||||
KEY_F20,
|
||||
KEY_F21,
|
||||
KEY_F22,
|
||||
KEY_F23,
|
||||
KEY_F24,
|
||||
KEY_EXECUTE,
|
||||
KEY_HELP,
|
||||
KEY_MENU,
|
||||
KEY_SELECT,
|
||||
KEY_STOP,
|
||||
KEY_AGAIN,
|
||||
KEY_UNDO,
|
||||
KEY_CUT,
|
||||
KEY_COPY,
|
||||
KEY_PASTE,
|
||||
KEY_FIND,
|
||||
KEY_MUTE,
|
||||
KEY_VOLUMEUP,
|
||||
KEY_VOLUMEDOWN,
|
||||
KEY_KP_COMMA,
|
||||
KEY_KP_EQUALSAS400,
|
||||
|
||||
KEY_ALTERASE,
|
||||
KEY_SYSREQ,
|
||||
KEY_CANCEL,
|
||||
KEY_CLEAR,
|
||||
KEY_PRIOR,
|
||||
KEY_RETURN2,
|
||||
KEY_SEPARATOR,
|
||||
KEY_OUT,
|
||||
KEY_OPER,
|
||||
KEY_CLEARAGAIN,
|
||||
KEY_CRSEL,
|
||||
KEY_EXSEL,
|
||||
|
||||
KEY_KP_00,
|
||||
KEY_KP_000,
|
||||
KEY_THOUSANDSSEPARATOR,
|
||||
KEY_DECIMALSEPARATOR,
|
||||
KEY_CURRENCYUNIT,
|
||||
KEY_CURRENCYSUBUNIT,
|
||||
KEY_KP_LEFTPAREN,
|
||||
KEY_KP_RIGHTPAREN,
|
||||
KEY_KP_LEFTBRACE,
|
||||
KEY_KP_RIGHTBRACE,
|
||||
KEY_KP_TAB,
|
||||
KEY_KP_BACKSPACE,
|
||||
KEY_KP_A,
|
||||
KEY_KP_B,
|
||||
KEY_KP_C,
|
||||
KEY_KP_D,
|
||||
KEY_KP_E,
|
||||
KEY_KP_F,
|
||||
KEY_KP_XOR,
|
||||
KEY_KP_POWER,
|
||||
KEY_KP_PERCENT,
|
||||
KEY_KP_LESS,
|
||||
KEY_KP_GREATER,
|
||||
KEY_KP_AMPERSAND,
|
||||
KEY_KP_DBLAMPERSAND,
|
||||
KEY_KP_VERTICALBAR,
|
||||
KEY_KP_DBLVERTICALBAR,
|
||||
KEY_KP_COLON,
|
||||
KEY_KP_HASH,
|
||||
KEY_KP_SPACE,
|
||||
KEY_KP_AT,
|
||||
KEY_KP_EXCLAM,
|
||||
KEY_KP_MEMSTORE,
|
||||
KEY_KP_MEMRECALL,
|
||||
KEY_KP_MEMCLEAR,
|
||||
KEY_KP_MEMADD,
|
||||
KEY_KP_MEMSUBTRACT,
|
||||
KEY_KP_MEMMULTIPLY,
|
||||
KEY_KP_MEMDIVIDE,
|
||||
KEY_KP_PLUSMINUS,
|
||||
KEY_KP_CLEAR,
|
||||
KEY_KP_CLEARENTRY,
|
||||
KEY_KP_BINARY,
|
||||
KEY_KP_OCTAL,
|
||||
KEY_KP_DECIMAL,
|
||||
KEY_KP_HEXADECIMAL,
|
||||
|
||||
KEY_LCTRL,
|
||||
KEY_LSHIFT,
|
||||
KEY_LALT,
|
||||
KEY_LGUI,
|
||||
KEY_RCTRL,
|
||||
KEY_RSHIFT,
|
||||
KEY_RALT,
|
||||
KEY_RGUI,
|
||||
|
||||
KEY_MODE,
|
||||
|
||||
KEY_AUDIONEXT,
|
||||
KEY_AUDIOPREV,
|
||||
KEY_AUDIOSTOP,
|
||||
KEY_AUDIOPLAY,
|
||||
KEY_AUDIOMUTE,
|
||||
KEY_MEDIASELECT,
|
||||
KEY_WWW,
|
||||
KEY_MAIL,
|
||||
KEY_CALCULATOR,
|
||||
KEY_COMPUTER,
|
||||
KEY_AC_SEARCH,
|
||||
KEY_AC_HOME,
|
||||
KEY_AC_BACK,
|
||||
KEY_AC_FORWARD,
|
||||
KEY_AC_STOP,
|
||||
KEY_AC_REFRESH,
|
||||
KEY_AC_BOOKMARKS,
|
||||
|
||||
KEY_BRIGHTNESSDOWN,
|
||||
KEY_BRIGHTNESSUP,
|
||||
KEY_DISPLAYSWITCH,
|
||||
KEY_KBDILLUMTOGGLE,
|
||||
KEY_KBDILLUMDOWN,
|
||||
KEY_KBDILLUMUP,
|
||||
KEY_EJECT,
|
||||
KEY_SLEEP,
|
||||
KEY_APP1,
|
||||
KEY_APP2,
|
||||
|
||||
KEY_AUDIOREWIND,
|
||||
KEY_AUDIOFASTFORWARD
|
||||
};
|
||||
|
||||
struct InputResult {
|
||||
InputProcessorKeycode keycode = KEY_UNKNOWN;
|
||||
};
|
||||
|
||||
class InputProcessor {
|
||||
public:
|
||||
virtual InputResult process_input()=0;
|
||||
};
|
||||
|
||||
#endif //RLA_IIPP_INPUTPROCESSOR_HPP
|
||||
295
src/engine/input/sdlinputprocessor.cpp
Normal file
295
src/engine/input/sdlinputprocessor.cpp
Normal file
@@ -0,0 +1,295 @@
|
||||
//
|
||||
// Created by m on 12/6/21.
|
||||
//
|
||||
|
||||
#include "sdlinputprocessor.hpp"
|
||||
|
||||
InputResult SdlInputProcessor::process_input() {
|
||||
SDL_Event event;
|
||||
InputResult result;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
switch(event.type) {
|
||||
case SDL_QUIT: exit(EXIT_SUCCESS); // immediately exit here, eventually maybe make this na input
|
||||
case SDL_KEYDOWN: result.keycode = get_keycode(event.key.keysym.sym); break;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
SdlInputProcessor::SdlInputProcessor() {
|
||||
this->keycode_map[SDLK_UNKNOWN] = KEY_UNKNOWN;
|
||||
|
||||
this->keycode_map[SDLK_RETURN] = KEY_RETURN;
|
||||
this->keycode_map[SDLK_ESCAPE] = KEY_ESCAPE;
|
||||
this->keycode_map[SDLK_BACKSPACE] = KEY_BACKSPACE;
|
||||
this->keycode_map[SDLK_TAB] = KEY_TAB;
|
||||
this->keycode_map[SDLK_SPACE] = KEY_SPACE;
|
||||
this->keycode_map[SDLK_EXCLAIM] = KEY_EXCLAIM;
|
||||
this->keycode_map[SDLK_QUOTEDBL] = KEY_QUOTEDBL;
|
||||
this->keycode_map[SDLK_HASH] = KEY_HASH;
|
||||
this->keycode_map[SDLK_PERCENT] = KEY_PERCENT;
|
||||
this->keycode_map[SDLK_DOLLAR] = KEY_DOLLAR;
|
||||
this->keycode_map[SDLK_AMPERSAND] = KEY_AMPERSAND;
|
||||
this->keycode_map[SDLK_QUOTE] = KEY_QUOTE;
|
||||
this->keycode_map[SDLK_LEFTPAREN] = KEY_LEFTPAREN;
|
||||
this->keycode_map[SDLK_RIGHTPAREN] = KEY_RIGHTPAREN;
|
||||
this->keycode_map[SDLK_ASTERISK] = KEY_ASTERISK;
|
||||
this->keycode_map[SDLK_PLUS] = KEY_PLUS;
|
||||
this->keycode_map[SDLK_COMMA] = KEY_COMMA;
|
||||
this->keycode_map[SDLK_MINUS] = KEY_MINUS;
|
||||
this->keycode_map[SDLK_PERIOD] = KEY_PERIOD;
|
||||
this->keycode_map[SDLK_SLASH] = KEY_SLASH;
|
||||
this->keycode_map[SDLK_0] = KEY_0;
|
||||
this->keycode_map[SDLK_1] = KEY_1;
|
||||
this->keycode_map[SDLK_2] = KEY_2;
|
||||
this->keycode_map[SDLK_3] = KEY_3;
|
||||
this->keycode_map[SDLK_4] = KEY_4;
|
||||
this->keycode_map[SDLK_5] = KEY_5;
|
||||
this->keycode_map[SDLK_6] = KEY_6;
|
||||
this->keycode_map[SDLK_7] = KEY_7;
|
||||
this->keycode_map[SDLK_8] = KEY_8;
|
||||
this->keycode_map[SDLK_9] = KEY_9;
|
||||
this->keycode_map[SDLK_COLON] = KEY_COLON;
|
||||
this->keycode_map[SDLK_SEMICOLON] = KEY_SEMICOLON;
|
||||
this->keycode_map[SDLK_LESS] = KEY_LESS;
|
||||
this->keycode_map[SDLK_EQUALS] = KEY_EQUALS;
|
||||
this->keycode_map[SDLK_GREATER] = KEY_GREATER;
|
||||
this->keycode_map[SDLK_QUESTION] = KEY_QUESTION;
|
||||
this->keycode_map[SDLK_AT] = KEY_AT;
|
||||
|
||||
|
||||
this->keycode_map[SDLK_LEFTBRACKET] = KEY_LEFTBRACKET;
|
||||
this->keycode_map[SDLK_BACKSLASH] = KEY_BACKSLASH;
|
||||
this->keycode_map[SDLK_RIGHTBRACKET] = KEY_RIGHTBRACKET;
|
||||
this->keycode_map[SDLK_CARET] = KEY_CARET;
|
||||
this->keycode_map[SDLK_UNDERSCORE] = KEY_UNDERSCORE;
|
||||
this->keycode_map[SDLK_BACKQUOTE] = KEY_BACKQUOTE;
|
||||
this->keycode_map[SDLK_a] = KEY_a;
|
||||
this->keycode_map[SDLK_b] = KEY_b;
|
||||
this->keycode_map[SDLK_c] = KEY_c;
|
||||
this->keycode_map[SDLK_d] = KEY_d;
|
||||
this->keycode_map[SDLK_e] = KEY_e;
|
||||
this->keycode_map[SDLK_f] = KEY_f;
|
||||
this->keycode_map[SDLK_g] = KEY_g;
|
||||
this->keycode_map[SDLK_h] = KEY_h;
|
||||
this->keycode_map[SDLK_i] = KEY_i;
|
||||
this->keycode_map[SDLK_j] = KEY_j;
|
||||
this->keycode_map[SDLK_k] = KEY_k;
|
||||
this->keycode_map[SDLK_l] = KEY_l;
|
||||
this->keycode_map[SDLK_m] = KEY_m;
|
||||
this->keycode_map[SDLK_n] = KEY_n;
|
||||
this->keycode_map[SDLK_o] = KEY_o;
|
||||
this->keycode_map[SDLK_p] = KEY_p;
|
||||
this->keycode_map[SDLK_q] = KEY_q;
|
||||
this->keycode_map[SDLK_r] = KEY_r;
|
||||
this->keycode_map[SDLK_s] = KEY_s;
|
||||
this->keycode_map[SDLK_t] = KEY_t;
|
||||
this->keycode_map[SDLK_u] = KEY_u;
|
||||
this->keycode_map[SDLK_v] = KEY_v;
|
||||
this->keycode_map[SDLK_w] = KEY_w;
|
||||
this->keycode_map[SDLK_x] = KEY_x;
|
||||
this->keycode_map[SDLK_y] = KEY_y;
|
||||
this->keycode_map[SDLK_z] = KEY_z;
|
||||
|
||||
this->keycode_map[SDLK_CAPSLOCK] = KEY_CAPSLOCK;
|
||||
|
||||
this->keycode_map[SDLK_F1] = KEY_F1;
|
||||
this->keycode_map[SDLK_F2] = KEY_F2;
|
||||
this->keycode_map[SDLK_F3] = KEY_F3;
|
||||
this->keycode_map[SDLK_F4] = KEY_F4;
|
||||
this->keycode_map[SDLK_F5] = KEY_F5;
|
||||
this->keycode_map[SDLK_F6] = KEY_F6;
|
||||
this->keycode_map[SDLK_F7] = KEY_F7;
|
||||
this->keycode_map[SDLK_F8] = KEY_F8;
|
||||
this->keycode_map[SDLK_F9] = KEY_F9;
|
||||
this->keycode_map[SDLK_F10] = KEY_F10;
|
||||
this->keycode_map[SDLK_F11] = KEY_F11;
|
||||
this->keycode_map[SDLK_F12] = KEY_F12;
|
||||
|
||||
this->keycode_map[SDLK_PRINTSCREEN] = KEY_PRINTSCREEN;
|
||||
this->keycode_map[SDLK_SCROLLLOCK] = KEY_SCROLLLOCK;
|
||||
this->keycode_map[SDLK_PAUSE] = KEY_PAUSE;
|
||||
this->keycode_map[SDLK_INSERT] = KEY_INSERT;
|
||||
this->keycode_map[SDLK_HOME] = KEY_HOME;
|
||||
this->keycode_map[SDLK_PAGEUP] = KEY_PAGEUP;
|
||||
this->keycode_map[SDLK_DELETE] = KEY_DELETE;
|
||||
this->keycode_map[SDLK_END] = KEY_END;
|
||||
this->keycode_map[SDLK_PAGEDOWN] = KEY_PAGEDOWN;
|
||||
this->keycode_map[SDLK_RIGHT] = KEY_RIGHT;
|
||||
this->keycode_map[SDLK_LEFT] = KEY_LEFT;
|
||||
this->keycode_map[SDLK_DOWN] = KEY_DOWN;
|
||||
this->keycode_map[SDLK_UP] = KEY_UP;
|
||||
|
||||
this->keycode_map[SDLK_NUMLOCKCLEAR] = KEY_NUMLOCKCLEAR;
|
||||
this->keycode_map[SDLK_KP_DIVIDE] = KEY_KP_DIVIDE;
|
||||
this->keycode_map[SDLK_KP_MULTIPLY] = KEY_KP_MULTIPLY;
|
||||
this->keycode_map[SDLK_KP_MINUS] = KEY_KP_MINUS;
|
||||
this->keycode_map[SDLK_KP_PLUS] = KEY_KP_PLUS;
|
||||
this->keycode_map[SDLK_KP_ENTER] = KEY_KP_ENTER;
|
||||
this->keycode_map[SDLK_KP_1] = KEY_KP_1;
|
||||
this->keycode_map[SDLK_KP_2] = KEY_KP_2;
|
||||
this->keycode_map[SDLK_KP_3] = KEY_KP_3;
|
||||
this->keycode_map[SDLK_KP_4] = KEY_KP_4;
|
||||
this->keycode_map[SDLK_KP_5] = KEY_KP_5;
|
||||
this->keycode_map[SDLK_KP_6] = KEY_KP_6;
|
||||
this->keycode_map[SDLK_KP_7] = KEY_KP_7;
|
||||
this->keycode_map[SDLK_KP_8] = KEY_KP_8;
|
||||
this->keycode_map[SDLK_KP_9] = KEY_KP_9;
|
||||
this->keycode_map[SDLK_KP_0] = KEY_KP_0;
|
||||
this->keycode_map[SDLK_KP_PERIOD] = KEY_KP_PERIOD;
|
||||
|
||||
this->keycode_map[SDLK_APPLICATION] = KEY_APPLICATION;
|
||||
this->keycode_map[SDLK_POWER] = KEY_POWER;
|
||||
this->keycode_map[SDLK_KP_EQUALS] = KEY_KP_EQUALS;
|
||||
this->keycode_map[SDLK_F13] = KEY_F13;
|
||||
this->keycode_map[SDLK_F14] = KEY_F14;
|
||||
this->keycode_map[SDLK_F15] = KEY_F15;
|
||||
this->keycode_map[SDLK_F16] = KEY_F16;
|
||||
this->keycode_map[SDLK_F17] = KEY_F17;
|
||||
this->keycode_map[SDLK_F18] = KEY_F18;
|
||||
this->keycode_map[SDLK_F19] = KEY_F19;
|
||||
this->keycode_map[SDLK_F20] = KEY_F20;
|
||||
this->keycode_map[SDLK_F21] = KEY_F21;
|
||||
this->keycode_map[SDLK_F22] = KEY_F22;
|
||||
this->keycode_map[SDLK_F23] = KEY_F23;
|
||||
this->keycode_map[SDLK_F24] = KEY_F24;
|
||||
this->keycode_map[SDLK_EXECUTE] = KEY_EXECUTE;
|
||||
this->keycode_map[SDLK_HELP] = KEY_HELP;
|
||||
this->keycode_map[SDLK_MENU] = KEY_MENU;
|
||||
this->keycode_map[SDLK_SELECT] = KEY_SELECT;
|
||||
this->keycode_map[SDLK_STOP] = KEY_STOP;
|
||||
this->keycode_map[SDLK_AGAIN] = KEY_AGAIN;
|
||||
this->keycode_map[SDLK_UNDO] = KEY_UNDO;
|
||||
this->keycode_map[SDLK_CUT] = KEY_CUT;
|
||||
this->keycode_map[SDLK_COPY] = KEY_COPY;
|
||||
this->keycode_map[SDLK_PASTE] = KEY_PASTE;
|
||||
this->keycode_map[SDLK_FIND] = KEY_FIND;
|
||||
this->keycode_map[SDLK_MUTE] = KEY_MUTE;
|
||||
this->keycode_map[SDLK_VOLUMEUP] = KEY_VOLUMEUP;
|
||||
this->keycode_map[SDLK_VOLUMEDOWN] = KEY_VOLUMEDOWN;
|
||||
this->keycode_map[SDLK_KP_COMMA] = KEY_KP_COMMA;
|
||||
this->keycode_map[SDLK_KP_EQUALSAS400] = KEY_KP_EQUALSAS400;
|
||||
|
||||
|
||||
this->keycode_map[SDLK_ALTERASE] = KEY_ALTERASE;
|
||||
this->keycode_map[SDLK_SYSREQ] = KEY_SYSREQ;
|
||||
this->keycode_map[SDLK_CANCEL] = KEY_CANCEL;
|
||||
this->keycode_map[SDLK_CLEAR] = KEY_CLEAR;
|
||||
this->keycode_map[SDLK_PRIOR] = KEY_PRIOR;
|
||||
this->keycode_map[SDLK_RETURN2] = KEY_RETURN2;
|
||||
this->keycode_map[SDLK_SEPARATOR] = KEY_SEPARATOR;
|
||||
this->keycode_map[SDLK_OUT] = KEY_OUT;
|
||||
this->keycode_map[SDLK_OPER] = KEY_OPER;
|
||||
this->keycode_map[SDLK_CLEARAGAIN] = KEY_CLEARAGAIN;
|
||||
this->keycode_map[SDLK_CRSEL] = KEY_CRSEL;
|
||||
this->keycode_map[SDLK_EXSEL] = KEY_EXSEL;
|
||||
|
||||
this->keycode_map[SDLK_KP_00] = KEY_KP_00;
|
||||
this->keycode_map[SDLK_KP_000] = KEY_KP_000;
|
||||
this->keycode_map[SDLK_THOUSANDSSEPARATOR] = KEY_THOUSANDSSEPARATOR;
|
||||
|
||||
this->keycode_map[SDLK_DECIMALSEPARATOR] = KEY_DECIMALSEPARATOR;
|
||||
|
||||
this->keycode_map[SDLK_CURRENCYUNIT] = KEY_CURRENCYUNIT;
|
||||
this->keycode_map[SDLK_CURRENCYSUBUNIT] = KEY_CURRENCYSUBUNIT;
|
||||
|
||||
this->keycode_map[SDLK_KP_LEFTPAREN] = KEY_KP_LEFTPAREN;
|
||||
this->keycode_map[SDLK_KP_RIGHTPAREN] = KEY_KP_RIGHTPAREN;
|
||||
this->keycode_map[SDLK_KP_LEFTBRACE] = KEY_KP_LEFTBRACE;
|
||||
this->keycode_map[SDLK_KP_RIGHTBRACE] = KEY_KP_RIGHTBRACE;
|
||||
this->keycode_map[SDLK_KP_TAB] = KEY_KP_TAB;
|
||||
this->keycode_map[SDLK_KP_BACKSPACE] = KEY_KP_BACKSPACE;
|
||||
this->keycode_map[SDLK_KP_A] = KEY_KP_A;
|
||||
this->keycode_map[SDLK_KP_B] = KEY_KP_B;
|
||||
this->keycode_map[SDLK_KP_C] = KEY_KP_C;
|
||||
this->keycode_map[SDLK_KP_D] = KEY_KP_D;
|
||||
this->keycode_map[SDLK_KP_E] = KEY_KP_E;
|
||||
this->keycode_map[SDLK_KP_F] = KEY_KP_F;
|
||||
this->keycode_map[SDLK_KP_XOR] = KEY_KP_XOR;
|
||||
this->keycode_map[SDLK_KP_POWER] = KEY_KP_POWER;
|
||||
this->keycode_map[SDLK_KP_PERCENT] = KEY_KP_PERCENT;
|
||||
this->keycode_map[SDLK_KP_LESS] = KEY_KP_LESS;
|
||||
this->keycode_map[SDLK_KP_GREATER] = KEY_KP_GREATER;
|
||||
this->keycode_map[SDLK_KP_AMPERSAND] = KEY_KP_AMPERSAND;
|
||||
this->keycode_map[SDLK_KP_DBLAMPERSAND] = KEY_KP_DBLAMPERSAND;
|
||||
|
||||
this->keycode_map[SDLK_KP_VERTICALBAR] = KEY_KP_VERTICALBAR;
|
||||
|
||||
this->keycode_map[SDLK_KP_DBLVERTICALBAR] = KEY_KP_DBLVERTICALBAR;
|
||||
|
||||
this->keycode_map[SDLK_KP_COLON] = KEY_KP_COLON;
|
||||
this->keycode_map[SDLK_KP_HASH] = KEY_KP_HASH;
|
||||
this->keycode_map[SDLK_KP_SPACE] = KEY_KP_SPACE;
|
||||
this->keycode_map[SDLK_KP_AT] = KEY_KP_AT;
|
||||
this->keycode_map[SDLK_KP_EXCLAM] = KEY_KP_EXCLAM;
|
||||
this->keycode_map[SDLK_KP_MEMSTORE] = KEY_KP_MEMSTORE;
|
||||
this->keycode_map[SDLK_KP_MEMRECALL] = KEY_KP_MEMRECALL;
|
||||
this->keycode_map[SDLK_KP_MEMCLEAR] = KEY_KP_MEMCLEAR;
|
||||
this->keycode_map[SDLK_KP_MEMADD] = KEY_KP_MEMADD;
|
||||
this->keycode_map[SDLK_KP_MEMSUBTRACT] = KEY_KP_MEMSUBTRACT;
|
||||
|
||||
this->keycode_map[SDLK_KP_MEMMULTIPLY] = KEY_KP_MEMMULTIPLY;
|
||||
|
||||
this->keycode_map[SDLK_KP_MEMDIVIDE] = KEY_KP_MEMDIVIDE;
|
||||
this->keycode_map[SDLK_KP_PLUSMINUS] = KEY_KP_PLUSMINUS;
|
||||
this->keycode_map[SDLK_KP_CLEAR] = KEY_KP_CLEAR;
|
||||
this->keycode_map[SDLK_KP_CLEARENTRY] = KEY_KP_CLEARENTRY;
|
||||
this->keycode_map[SDLK_KP_BINARY] = KEY_KP_BINARY;
|
||||
this->keycode_map[SDLK_KP_OCTAL] = KEY_KP_OCTAL;
|
||||
this->keycode_map[SDLK_KP_DECIMAL] = KEY_KP_DECIMAL;
|
||||
this->keycode_map[SDLK_KP_HEXADECIMAL] = KEY_KP_HEXADECIMAL;
|
||||
|
||||
|
||||
this->keycode_map[SDLK_LCTRL] = KEY_LCTRL;
|
||||
this->keycode_map[SDLK_LSHIFT] = KEY_LSHIFT;
|
||||
this->keycode_map[SDLK_LALT] = KEY_LALT;
|
||||
this->keycode_map[SDLK_LGUI] = KEY_LGUI;
|
||||
this->keycode_map[SDLK_RCTRL] = KEY_RCTRL;
|
||||
this->keycode_map[SDLK_RSHIFT] = KEY_RSHIFT;
|
||||
this->keycode_map[SDLK_RALT] = KEY_RALT;
|
||||
this->keycode_map[SDLK_RGUI] = KEY_RGUI;
|
||||
|
||||
this->keycode_map[SDLK_MODE] = KEY_MODE;
|
||||
|
||||
this->keycode_map[SDLK_AUDIONEXT] = KEY_AUDIONEXT;
|
||||
this->keycode_map[SDLK_AUDIOPREV] = KEY_AUDIOPREV;
|
||||
this->keycode_map[SDLK_AUDIOSTOP] = KEY_AUDIOSTOP;
|
||||
this->keycode_map[SDLK_AUDIOPLAY] = KEY_AUDIOPLAY;
|
||||
this->keycode_map[SDLK_AUDIOMUTE] = KEY_AUDIOMUTE;
|
||||
this->keycode_map[SDLK_MEDIASELECT] = KEY_MEDIASELECT;
|
||||
this->keycode_map[SDLK_WWW] = KEY_WWW;
|
||||
this->keycode_map[SDLK_MAIL] = KEY_MAIL;
|
||||
this->keycode_map[SDLK_CALCULATOR] = KEY_CALCULATOR;
|
||||
this->keycode_map[SDLK_COMPUTER] = KEY_COMPUTER;
|
||||
this->keycode_map[SDLK_AC_SEARCH] = KEY_AC_SEARCH;
|
||||
this->keycode_map[SDLK_AC_HOME] = KEY_AC_HOME;
|
||||
this->keycode_map[SDLK_AC_BACK] = KEY_AC_BACK;
|
||||
this->keycode_map[SDLK_AC_FORWARD] = KEY_AC_FORWARD;
|
||||
this->keycode_map[SDLK_AC_STOP] = KEY_AC_STOP;
|
||||
this->keycode_map[SDLK_AC_REFRESH] = KEY_AC_REFRESH;
|
||||
this->keycode_map[SDLK_AC_BOOKMARKS] = KEY_AC_BOOKMARKS;
|
||||
|
||||
this->keycode_map[SDLK_BRIGHTNESSDOWN] = KEY_BRIGHTNESSDOWN;
|
||||
|
||||
this->keycode_map[SDLK_BRIGHTNESSUP] = KEY_BRIGHTNESSUP;
|
||||
this->keycode_map[SDLK_DISPLAYSWITCH] = KEY_DISPLAYSWITCH;
|
||||
this->keycode_map[SDLK_KBDILLUMTOGGLE] = KEY_KBDILLUMTOGGLE;
|
||||
|
||||
this->keycode_map[SDLK_KBDILLUMDOWN] = KEY_KBDILLUMDOWN;
|
||||
this->keycode_map[SDLK_KBDILLUMUP] = KEY_KBDILLUMUP;
|
||||
this->keycode_map[SDLK_EJECT] = KEY_EJECT;
|
||||
this->keycode_map[SDLK_SLEEP] = KEY_SLEEP;
|
||||
this->keycode_map[SDLK_APP1] = KEY_APP1;
|
||||
this->keycode_map[SDLK_APP2] = KEY_APP2;
|
||||
|
||||
this->keycode_map[SDLK_AUDIOREWIND] = KEY_AUDIOREWIND;
|
||||
this->keycode_map[SDLK_AUDIOFASTFORWARD] = KEY_AUDIOFASTFORWARD;
|
||||
}
|
||||
|
||||
InputProcessorKeycode SdlInputProcessor::get_keycode(SDL_Keycode keycode) {
|
||||
if(this->keycode_map.find(keycode) == this->keycode_map.end()) {
|
||||
return KEY_UNKNOWN;
|
||||
}
|
||||
printf("Detected keypress for %i\n", this->keycode_map[keycode]);
|
||||
return this->keycode_map[keycode];
|
||||
}
|
||||
24
src/engine/input/sdlinputprocessor.hpp
Normal file
24
src/engine/input/sdlinputprocessor.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Created by m on 12/6/21.
|
||||
//
|
||||
|
||||
#ifndef RLA_IIPP_SDLINPUTPROCESSOR_HPP
|
||||
#define RLA_IIPP_SDLINPUTPROCESSOR_HPP
|
||||
|
||||
|
||||
#include "inputprocessor.hpp"
|
||||
#include <SDL.h>
|
||||
#include <unordered_map>
|
||||
|
||||
class SdlInputProcessor : public InputProcessor {
|
||||
InputResult process_input() override;
|
||||
public:
|
||||
SdlInputProcessor();
|
||||
|
||||
private:
|
||||
std::unordered_map<SDL_Keycode,InputProcessorKeycode> keycode_map;
|
||||
InputProcessorKeycode get_keycode(SDL_Keycode keycode);
|
||||
};
|
||||
|
||||
|
||||
#endif //RLA_IIPP_SDLINPUTPROCESSOR_HPP
|
||||
@@ -51,6 +51,7 @@ void SdlRenderer::draw_text(TextRenderDetails details, std::string text, int x,
|
||||
TTF_Font *font = this->font_manager->fetch_resource(SdlFontArgs(details.size, details.font_path));
|
||||
SDL_Texture *texture = this->texture_manager->load_text_as_texture(text, font, details.color);
|
||||
render_texture(x, y, texture, nullptr);
|
||||
SDL_DestroyTexture(texture);
|
||||
}
|
||||
|
||||
SdlRenderer::~SdlRenderer() {
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
//
|
||||
|
||||
#include "visualizer.hpp"
|
||||
bool Visualizer::update() {
|
||||
bool Visualizer::update(InputResult input) {
|
||||
this->x += 1;
|
||||
if(this->x > WIN_WIDTH) {
|
||||
if(this->x > WIN_WIDTH || input.keycode == KEY_ESCAPE) {
|
||||
this->x = 0;
|
||||
return true;
|
||||
}
|
||||
if(input.keycode == KEY_RIGHT) {
|
||||
this->sprite_index += 1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -23,7 +26,7 @@ unsigned int Visualizer::get_framerate() {
|
||||
void Visualizer::render(Renderer *renderer) {
|
||||
Color color = COLOR_WHITE;
|
||||
SpriteSheet sheet = SpriteSheet{"sprites/character.bmp", 48, 48};
|
||||
auto sprite = Sprite{&sheet, 1};
|
||||
auto sprite = Sprite{&sheet, this->sprite_index};
|
||||
RendererParams window_params = get_renderer_params();
|
||||
renderer->draw_sprite(sprite, window_params.width/2, window_params.height/2);
|
||||
for(int y=0;y<WIN_HEIGHT;y++) {
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
class Visualizer : public Game {
|
||||
public:
|
||||
bool update() override;
|
||||
bool update(InputResult input) override;
|
||||
|
||||
void render(Renderer* renderer) override;
|
||||
|
||||
@@ -25,6 +25,7 @@ public:
|
||||
|
||||
private:
|
||||
int x = 0;
|
||||
int sprite_index = 1;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -4,12 +4,14 @@
|
||||
#include "engine/rendering/sdl/sdlrenderer.hpp"
|
||||
#include "engine/engine.hpp"
|
||||
#include "game/visualizer.hpp"
|
||||
#include "engine/input/sdlinputprocessor.hpp"
|
||||
|
||||
int main() {
|
||||
|
||||
std::unique_ptr<Renderer> renderer = std::make_unique<SdlRenderer>();
|
||||
std::unique_ptr<Game> game = std::make_unique<Visualizer>();
|
||||
auto* engine = new Engine(std::move(game), std::move(renderer));
|
||||
std::unique_ptr<InputProcessor> input_processor = std::make_unique<SdlInputProcessor>();
|
||||
auto* engine = new Engine(std::move(game), std::move(renderer), std::move(input_processor));
|
||||
engine->start_loop();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user