Video menu.
This commit is contained in:
parent
240793027f
commit
90bf26c018
38
src/gui.cpp
38
src/gui.cpp
|
@ -8,6 +8,11 @@
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
|
|
||||||
|
// Screen size:
|
||||||
|
const unsigned width = 256;
|
||||||
|
const unsigned height = 240;
|
||||||
|
const unsigned fontSz = 16;
|
||||||
|
|
||||||
// SDL structures:
|
// SDL structures:
|
||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
|
@ -19,10 +24,27 @@ u8 const* keys;
|
||||||
Menu* menu;
|
Menu* menu;
|
||||||
Menu mainMenu;
|
Menu mainMenu;
|
||||||
Menu settingsMenu;
|
Menu settingsMenu;
|
||||||
|
Menu videoMenu;
|
||||||
FileMenu* fileMenu;
|
FileMenu* fileMenu;
|
||||||
|
|
||||||
bool pause = true;
|
bool pause = true;
|
||||||
|
|
||||||
|
/* Change the size of the window */
|
||||||
|
void set_size(int mul)
|
||||||
|
{
|
||||||
|
// Generate a new font:
|
||||||
|
TTF_CloseFont(font);
|
||||||
|
font = TTF_OpenFont("res/font.ttf", fontSz * mul);
|
||||||
|
|
||||||
|
// Set the new size:
|
||||||
|
SDL_SetWindowSize(window, 256 * mul, 240 * mul);
|
||||||
|
|
||||||
|
// Regenerate the menu entries:
|
||||||
|
mainMenu.regen();
|
||||||
|
settingsMenu.regen();
|
||||||
|
videoMenu.regen();
|
||||||
|
}
|
||||||
|
|
||||||
/* Initialize GUI */
|
/* Initialize GUI */
|
||||||
void init()
|
void init()
|
||||||
{
|
{
|
||||||
|
@ -42,7 +64,7 @@ void init()
|
||||||
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
|
SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
|
||||||
width, height);
|
width, height);
|
||||||
|
|
||||||
font = TTF_OpenFont("res/font.ttf", fontPt);
|
font = TTF_OpenFont("res/font.ttf", fontSz);
|
||||||
|
|
||||||
keys = SDL_GetKeyboardState(0);
|
keys = SDL_GetKeyboardState(0);
|
||||||
signal(SIGINT, SIG_DFL); // CTRL+C kills the application.
|
signal(SIGINT, SIG_DFL); // CTRL+C kills the application.
|
||||||
|
@ -52,8 +74,12 @@ void init()
|
||||||
mainMenu.add ("Settings", []{ menu = settingsMenu.reset(); });
|
mainMenu.add ("Settings", []{ menu = settingsMenu.reset(); });
|
||||||
mainMenu.add ("Exit", []{ exit(0); });
|
mainMenu.add ("Exit", []{ exit(0); });
|
||||||
settingsMenu.add("<", []{ menu = mainMenu.reset(); });
|
settingsMenu.add("<", []{ menu = mainMenu.reset(); });
|
||||||
|
settingsMenu.add("Video", []{ menu = videoMenu.reset(); });
|
||||||
settingsMenu.add("Controls");
|
settingsMenu.add("Controls");
|
||||||
settingsMenu.add("Video");
|
videoMenu.add ("<", []{ menu = settingsMenu.reset(); });
|
||||||
|
videoMenu.add ("Size 1x", []{ set_size(1); });
|
||||||
|
videoMenu.add ("Size 2x", []{ set_size(2); });
|
||||||
|
videoMenu.add ("Size 3x", []{ set_size(3); });
|
||||||
fileMenu = new FileMenu;
|
fileMenu = new FileMenu;
|
||||||
menu = &mainMenu;
|
menu = &mainMenu;
|
||||||
}
|
}
|
||||||
|
@ -62,11 +88,15 @@ void init()
|
||||||
void render_texture(SDL_Texture* texture, int x, int y)
|
void render_texture(SDL_Texture* texture, int x, int y)
|
||||||
{
|
{
|
||||||
int w, h;
|
int w, h;
|
||||||
|
int winW, winH;
|
||||||
SDL_Rect dest;
|
SDL_Rect dest;
|
||||||
|
|
||||||
|
SDL_GetWindowSize(window, &winW, &winH);
|
||||||
SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);
|
SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h);
|
||||||
dest.x = (x < 0) ? ((width / 2) - (dest.w / 2)) : x;
|
|
||||||
dest.y = (y < 0) ? ((height / 2) - (dest.h / 2)) : y;
|
dest.x = (x < 0) ? ((winW / 2) - (dest.w / 2)) : x;
|
||||||
|
dest.y = (y < 0) ? ((winH / 2) - (dest.h / 2)) : y;
|
||||||
|
|
||||||
SDL_RenderCopy(renderer, texture, NULL, &dest);
|
SDL_RenderCopy(renderer, texture, NULL, &dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,11 +6,6 @@
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
|
|
||||||
// Screen size:
|
|
||||||
const unsigned width = 256;
|
|
||||||
const unsigned height = 240;
|
|
||||||
const unsigned fontPt = 16; // Font size.
|
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void toggle_pause();
|
void toggle_pause();
|
||||||
void run();
|
void run();
|
||||||
|
|
43
src/menu.cpp
43
src/menu.cpp
|
@ -9,23 +9,33 @@ namespace GUI {
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
MenuEntry::MenuEntry(string label, function<void()> callback)
|
MenuEntry::MenuEntry(string label, function<void()> callback, int x)
|
||||||
{
|
{
|
||||||
this->label = label;
|
this->label = label;
|
||||||
this->callback = callback;
|
this->callback = callback;
|
||||||
|
this->x = x;
|
||||||
unselected = gen_text(label, white);
|
unselected = gen_text(label, white);
|
||||||
selected = gen_text(label, red);
|
selected = gen_text(label, red);
|
||||||
}
|
}
|
||||||
|
|
||||||
MenuEntry::~MenuEntry()
|
MenuEntry::~MenuEntry()
|
||||||
{
|
{
|
||||||
SDL_DestroyTexture(selected);
|
|
||||||
SDL_DestroyTexture(unselected);
|
SDL_DestroyTexture(unselected);
|
||||||
|
SDL_DestroyTexture(selected);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::add(string label, function<void()> callback)
|
void MenuEntry::regen()
|
||||||
{
|
{
|
||||||
entries.push_back(new MenuEntry(label, callback));
|
SDL_DestroyTexture(unselected);
|
||||||
|
SDL_DestroyTexture(selected);
|
||||||
|
unselected = gen_text(label, white);
|
||||||
|
selected = gen_text(label, red);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Menu::add(string label, function<void()> callback, int x)
|
||||||
|
{
|
||||||
|
entries.push_back(new MenuEntry(label, callback, x));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Menu::update(u8 const* keys)
|
void Menu::update(u8 const* keys)
|
||||||
|
@ -40,9 +50,17 @@ void Menu::update(u8 const* keys)
|
||||||
|
|
||||||
void Menu::render()
|
void Menu::render()
|
||||||
{
|
{
|
||||||
|
int h; SDL_QueryTexture(entries[0]->selected, NULL, NULL, NULL, &h);
|
||||||
|
|
||||||
for (int i = 0; i < entries.size(); i++)
|
for (int i = 0; i < entries.size(); i++)
|
||||||
render_texture(entries[i]->unselected, -1, i * fontPt);
|
render_texture(entries[i]->unselected, entries[i]->x, i * h);
|
||||||
render_texture(entries[cursor]->selected, -1, cursor * fontPt);
|
render_texture(entries[cursor]->selected, entries[cursor]->x, cursor * h);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Menu::regen()
|
||||||
|
{
|
||||||
|
for (auto entry : entries)
|
||||||
|
entry->regen();
|
||||||
}
|
}
|
||||||
|
|
||||||
Menu* Menu::reset()
|
Menu* Menu::reset()
|
||||||
|
@ -69,20 +87,13 @@ void FileMenu::change_dir(string dir)
|
||||||
|
|
||||||
if (name[0] == '.' and name != "..") continue;
|
if (name[0] == '.' and name != "..") continue;
|
||||||
if (dirp->d_type == DT_DIR)
|
if (dirp->d_type == DT_DIR)
|
||||||
add(name + "/", [=]{ change_dir(path); });
|
add(name + "/", [=]{ change_dir(path); }, 10);
|
||||||
else
|
else if (name.size() > 4 and name.substr(name.size() - 4) == ".nes")
|
||||||
add(name, [=]{ Cartridge::load(path.c_str()); toggle_pause(); });
|
add(name, [=]{ Cartridge::load(path.c_str()); toggle_pause(); }, 10);
|
||||||
}
|
}
|
||||||
closedir(dp);
|
closedir(dp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FileMenu::render()
|
|
||||||
{
|
|
||||||
for (int i = 0; i < entries.size(); i++)
|
|
||||||
render_texture(entries[i]->unselected, 10, i * fontPt);
|
|
||||||
render_texture(entries[cursor]->selected, 10, cursor * fontPt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Menu* FileMenu::reset()
|
Menu* FileMenu::reset()
|
||||||
{
|
{
|
||||||
change_dir(getwd(NULL));
|
change_dir(getwd(NULL));
|
||||||
|
|
10
src/menu.hpp
10
src/menu.hpp
|
@ -12,14 +12,16 @@ struct MenuEntry
|
||||||
SDL_Color white = { 255, 255, 255 };
|
SDL_Color white = { 255, 255, 255 };
|
||||||
SDL_Color red = { 255, 0, 0 };
|
SDL_Color red = { 255, 0, 0 };
|
||||||
|
|
||||||
|
int x;
|
||||||
std::string label;
|
std::string label;
|
||||||
std::function<void()> callback;
|
std::function<void()> callback;
|
||||||
|
|
||||||
SDL_Texture* selected;
|
SDL_Texture* selected;
|
||||||
SDL_Texture* unselected;
|
SDL_Texture* unselected;
|
||||||
|
|
||||||
MenuEntry(std::string label, std::function<void()> callback);
|
MenuEntry(std::string label, std::function<void()> callback, int x);
|
||||||
~MenuEntry();
|
~MenuEntry();
|
||||||
|
void regen();
|
||||||
};
|
};
|
||||||
|
|
||||||
class Menu
|
class Menu
|
||||||
|
@ -29,9 +31,10 @@ class Menu
|
||||||
int cursor = 0;
|
int cursor = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void add(std::string label, std::function<void()> callback = []{});
|
void add(std::string label, std::function<void()> callback = []{}, int x = -1);
|
||||||
void update(u8 const* keys);
|
void update(u8 const* keys);
|
||||||
virtual void render();
|
void render();
|
||||||
|
void regen();
|
||||||
virtual Menu* reset();
|
virtual Menu* reset();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -40,7 +43,6 @@ class FileMenu : public Menu
|
||||||
void change_dir(std::string dir);
|
void change_dir(std::string dir);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void render();
|
|
||||||
Menu* reset();
|
Menu* reset();
|
||||||
FileMenu();
|
FileMenu();
|
||||||
};
|
};
|
||||||
|
|
Reference in a new issue