diff options
-rw-r--r-- | brice.dat | 4 | ||||
-rw-r--r-- | include/ui.hpp | 1 | ||||
-rw-r--r-- | src/ui.cpp | 10 | ||||
-rw-r--r-- | src/ui_menu.cpp | 80 | ||||
-rw-r--r-- | xml/!town.xml | 4 |
5 files changed, 86 insertions, 13 deletions
@@ -1,7 +1,7 @@ 3 -canSprint +Slow 1 canJump 0 -Slow +canSprint 0 diff --git a/include/ui.hpp b/include/ui.hpp index 25fbd3c..56c484c 100644 --- a/include/ui.hpp +++ b/include/ui.hpp @@ -41,6 +41,7 @@ ** --------------------------------------------------------------------------*/ void setControl(unsigned int index, SDL_Keycode key); +SDL_Keycode getControl(unsigned int index); namespace ui { @@ -30,7 +30,7 @@ extern std::vector<NPC *> aipreload; extern bool gameRunning; -static SDL_Keycode controlMap[] = { +static std::array<SDL_Keycode, 7> controlMap = { SDLK_w, SDLK_s, SDLK_a, SDLK_d, SDLK_LSHIFT, SDLK_LCTRL, SDLK_e }; @@ -39,6 +39,14 @@ void setControl(unsigned int index, SDL_Keycode key) controlMap[index] = key; } +SDL_Keycode getControl(unsigned int index) +{ + if (index >= controlMap.size()) + return 0; + + return controlMap[index]; +} + /** * Freetype variables */ diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp index 35a6d01..d842ece 100644 --- a/src/ui_menu.cpp +++ b/src/ui_menu.cpp @@ -1,5 +1,7 @@ #include <ui_menu.hpp> +#include <fstream> + extern bool gameRunning; extern Menu *currentMenu; @@ -22,14 +24,75 @@ inline void segFault() { (*((int *)NULL))++; } +std::string& deleteWord(std::string& s) +{ + while (s.back() != ' ') + s.pop_back(); + + return s; +} + +std::string sym2str(const SDL_Keycode& c) +{ + std::string s = ""; + + switch (c) { + case SDLK_UP : s = "UP" ; break; + case SDLK_DOWN : s = "DOWN" ; break; + case SDLK_LEFT : s = "LEFT" ; break; + case SDLK_RIGHT : s = "RIGHT" ; break; + case SDLK_LSHIFT : s = "LSHIFT" ; break; + case SDLK_RSHIFT : s = "RSHIFT" ; break; + case SDLK_LALT : s = "LALT" ; break; + case SDLK_RALT : s = "RALT" ; break; + case SDLK_LCTRL : s = "LCONTROL"; break; + case SDLK_RCTRL : s = "RCONTROL"; break; + case SDLK_TAB : s = "TAB" ; break; + default : s = "SHIT" ; break; + } + + return s; +} + +void initControls(Menu *m) +{ + auto cfg = readFileA("config/controls.dat"); + unsigned i = 0; + SDL_Keycode z; + + for (const auto &l : cfg) { + + z = static_cast<SDL_Keycode>(std::stoi(l)); + setControl(i, z); + m->items[i++].button.text += sym2str(z); + } +} + +void saveControls(void) +{ + std::ofstream out ("config/controls.dat"); + SDL_Keycode q = 1; + unsigned int i = 0; + + while ((q = getControl(i++)) != 0) { + auto d = std::to_string(q) + '\n'; + out.write(d.data(), d.size()); + } + + out.close(); +} void setControlF(unsigned int index, menuItem &m) { SDL_Event e; + do SDL_WaitEvent(&e); while (e.type != SDL_KEYDOWN); + setControl(index, e.key.keysym.sym); - m.button.text.pop_back(); - m.button.text.push_back(e.key.keysym.sym); + deleteWord(m.button.text); + + m.button.text += sym2str(e.key.keysym.sym); + saveControls(); } namespace ui { @@ -108,19 +171,20 @@ namespace ui { optionsMenu.parent = &pauseMenu; // Create the controls menu - controlsMenu.items.push_back(ui::menu::createButton({-450,300}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Left: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,300}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Left: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(2, controlsMenu.items[0]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Right: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Right: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(3, controlsMenu.items[1]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Up: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Up: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(0, controlsMenu.items[2]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,0}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Sprint: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,0}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Sprint: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(4, controlsMenu.items[3]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,-100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Creep: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,-100}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Creep: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(5, controlsMenu.items[4]); }; - controlsMenu.items.push_back(ui::menu::createButton({-450,-200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Inventory: ", nullptr)); + controlsMenu.items.push_back(ui::menu::createButton({-450,-200}, {400, 75}, {0.0f, 0.0f, 0.0f}, "Inventory: ", nullptr)); controlsMenu.items.back().button.func = [](){ setControlF(6, controlsMenu.items[5]); }; controlsMenu.parent = &pauseMenu; + initControls(&controlsMenu); } void toggle(void) { diff --git a/xml/!town.xml b/xml/!town.xml index e824ef7..a7a1270 100644 --- a/xml/!town.xml +++ b/xml/!town.xml @@ -4,8 +4,8 @@ <generation type="Random" width="1600"/> <time>6000</time> <spawnx>-300</spawnx> - <npc name="Sanc" hasDialog="true" health="1" x="124.68252" y="61.799011" dindex="0"/> - <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="223.94865" y="63.39901" dindex="9999"/> + <npc name="Sanc" hasDialog="true" health="1" x="223.98889" y="74.797943" dindex="0"/> + <npc name="Bob" hasDialog="true" spawnx="30" health="1" x="143.87997" y="72.197983" dindex="0"/> <structure type="1" spawnx="300" alive="1"/> <structure inside="bobshouse.xml" type="1" spawnx="10" alive="1"/> <chest alive="1"/> |