bug fix and make menu scrollable

This commit is contained in:
mmmmmar 2019-01-22 14:35:38 +08:00 committed by maruipu
parent 52b7920d98
commit cb1cad52d8
8 changed files with 71 additions and 69 deletions

View file

@ -87,9 +87,9 @@ void load_settings()
set_size(screen_size);
/* Control settings */
for (int p = 0; p < 1; p++)
for (int p = 0; p <= 1; p++)
{
const char* section = p==0?"controls p1":"controls p2";
const char* section = (p == 0) ?"controls p1":"controls p2";
useJoystick[p] = (ini.GetValue(section, "usejoy", "no"))[0] == 'y';
if (useJoystick[p])
@ -120,32 +120,32 @@ void load_settings()
void save_settings()
{
/* Screen settings */
char buf[2] = {0};
char buf[10];
sprintf(buf, "%d", last_window_size);
ini.SetValue("screen", "size", buf);
/* Control settings */
for (int p = 0; p < 1; p++)
for (int p = 0; p <= 1; p++)
{
const char* section = p==0?"controls p1":"controls p2";
const char* section = (p == 0) ? "controls p1" : "controls p2";
sprintf(buf, "%d", useJoystick[p]?BTN_UP[p]:KEY_UP[p]);
ini.SetValue("section", "UP", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_DOWN[p]:KEY_DOWN[p]);
ini.SetValue("section", "DOWN", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_LEFT[p]:KEY_LEFT[p]);
ini.SetValue("section", "LEFT", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_RIGHT[p]:KEY_RIGHT[p]);
ini.SetValue("section", "RIGHT", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_A[p]:KEY_A[p]);
ini.SetValue("section", "A", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_B[p]:KEY_B[p]);
ini.SetValue("section", "B", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_SELECT[p]:KEY_SELECT[p]);
ini.SetValue("section", "SELECT", buf);
sprintf(buf, "%d", useJoystick[p]?BTN_START[p]:KEY_START[p]);
ini.SetValue("section", "START", buf);
ini.SetValue("section", "usejoy", useJoystick[p]?"yes":"no");
sprintf(buf, "%d", useJoystick[p] ? BTN_UP[p]:KEY_UP[p]);
ini.SetValue(section, "UP", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_DOWN[p]:KEY_DOWN[p]);
ini.SetValue(section, "DOWN", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_LEFT[p]:KEY_LEFT[p]);
ini.SetValue(section, "LEFT", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_RIGHT[p]:KEY_RIGHT[p]);
ini.SetValue(section, "RIGHT", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_A[p]:KEY_A[p]);
ini.SetValue(section, "A", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_B[p]:KEY_B[p]);
ini.SetValue(section, "B", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_SELECT[p]:KEY_SELECT[p]);
ini.SetValue(section, "SELECT", buf);
sprintf(buf, "%d", useJoystick[p] ? BTN_START[p]:KEY_START[p]);
ini.SetValue(section, "START", buf);
ini.SetValue(section, "usejoy", useJoystick[p]? "yes":"no");
}
char path[CONFIG_PATH_MAX];

View file

@ -122,7 +122,13 @@ void PLA() { T; T; A = pop(); upd_nz(A); }
void PHA() { T; push(A); }
/* Flow control (branches, jumps) */
template<Flag f, bool v> void br() { s8 j = rd(imm()); if (P[f] == v) { T; PC += j; } }
template<Flag f, bool v> void br() {
s8 j = rd(imm());
if (P[f] == v) {
if (cross(PC, j)) T;
T; PC += j;
}
}
void JMP_IND() { u16 i = rd16(imm16()); PC = rd16_d(i, (i&0xFF00) | ((i+1) % 0x100)); }
void JMP() { PC = rd16(imm16()); }
void JSR() { u16 t = PC+1; T; push(t >> 8); push(t); PC = rd16(imm16()); }

View file

@ -11,10 +11,6 @@
namespace GUI {
// Screen size:
const unsigned WIDTH = 256;
const unsigned HEIGHT = 240;
// SDL structures:
SDL_Window* window;
SDL_Renderer* renderer;

View file

@ -1,7 +1,6 @@
#pragma once
#include <cstdint>
#define NTH_BIT(x, n) (((x) >> (n)) & 1)
/* Integer type shortcuts */

View file

@ -6,7 +6,9 @@
namespace GUI {
// Screen size:
const unsigned WIDTH = 256;
const unsigned HEIGHT = 240;
const int TEXT_CENTER = -1;
const int TEXT_RIGHT = -2;
const unsigned FONT_SZ = 15;

View file

@ -10,8 +10,6 @@ namespace GUI {
class Entry
{
int x, y;
std::string label;
std::function<void()> callback;
@ -20,19 +18,16 @@ class Entry
SDL_Texture* redTexture = nullptr;
public:
Entry(std::string label, std::function<void()> callback = []{}, int x = TEXT_CENTER, int y = 0);
Entry(std::string label, std::function<void()> callback = []{});
~Entry();
virtual void setX(int x) { this->x = x; }
virtual void setY(int y) { this->y = y; }
int getX() { return x; }
int getY() { return y; }
void setLabel(std::string label);
inline std::string& getLabel() { return label; }
virtual void select() { selected = true; };
virtual void unselect() { selected = false; };
void trigger() { callback(); };
virtual void render();
virtual void render(int x, int y);
};
class ControlEntry : public Entry
@ -42,20 +37,23 @@ class ControlEntry : public Entry
Entry* keyEntry;
public:
ControlEntry(std::string action, SDL_Scancode* key, int x = 0, int y = 0);
ControlEntry(std::string action, int* button, int x = 0, int y = 0);
void setY(int y) { Entry::setY(y); keyEntry->setY(y); }
ControlEntry(std::string action, SDL_Scancode* key);
ControlEntry(std::string action, int* button);
void select() { Entry::select(); keyEntry->select(); }
void unselect() { Entry::unselect(); keyEntry->unselect(); }
void render() { Entry::render(); keyEntry->render(); }
void render(int x, int y) { Entry::render(x, y); keyEntry->render(TEXT_RIGHT, y); }
};
class Menu
{
std::vector<Entry*> entries;
const int MAX_ENTRY = GUI::HEIGHT / FONT_SZ - 1;
int cursor = 0;
int top = 0;
int bottom = MAX_ENTRY;
public:
std::vector<Entry*> entries;
void add(Entry* entry);
void clear();
void update(u8 const* keys);

View file

@ -8,7 +8,7 @@ namespace GUI {
using namespace std;
Entry::Entry(string label, function<void()> callback, int x, int y) : callback(callback), x(x), y(y)
Entry::Entry(string label, function<void()> callback) : callback(callback)
{
setLabel(label);
}
@ -30,30 +30,24 @@ void Entry::setLabel(string label)
redTexture = gen_text(label, { 255, 0, 0 });
}
void Entry::render()
{
render_texture(selected ? redTexture : whiteTexture, getX(), getY());
void Entry::render(int x, int y) {
render_texture(selected ? redTexture : whiteTexture, x, y);
}
ControlEntry::ControlEntry(string action, SDL_Scancode* key, int x, int y) : key(key),
ControlEntry::ControlEntry(string action, SDL_Scancode* key) : key(key),
Entry::Entry(
action,
[&]{ keyEntry->setLabel(SDL_GetScancodeName(*(this->key) = query_key())); },
x,
y)
[&]{ keyEntry->setLabel(SDL_GetScancodeName(*(this->key) = query_key())); })
{
this->keyEntry = new Entry(SDL_GetScancodeName(*key), []{}, TEXT_RIGHT, y);
this->keyEntry = new Entry(SDL_GetScancodeName(*key), []{});
}
ControlEntry::ControlEntry(string action, int* button, int x, int y) : button(button),
ControlEntry::ControlEntry(string action, int* button) : button(button),
Entry::Entry(
action,
[&]{ keyEntry->setLabel(to_string(*(this->button) = query_button())); },
x,
y)
[&]{ keyEntry->setLabel(to_string(*(this->button) = query_button())); })
{
this->keyEntry = new Entry(to_string(*button), []{}, TEXT_RIGHT, y);
this->keyEntry = new Entry(to_string(*button), []{});
}
@ -61,7 +55,6 @@ void Menu::add(Entry* entry)
{
if (entries.empty())
entry->select();
entry->setY(entries.size() * FONT_SZ);
entries.push_back(entry);
}
@ -77,11 +70,20 @@ void Menu::update(u8 const* keys)
{
int oldCursor = cursor;
if (keys[SDL_SCANCODE_DOWN] and cursor < entries.size() - 1)
if (keys[SDL_SCANCODE_DOWN] and cursor < entries.size() - 1) {
cursor++;
else if (keys[SDL_SCANCODE_UP] and cursor > 0)
if (cursor == bottom) {
bottom += 1;
top += 1;
}
}
else if (keys[SDL_SCANCODE_UP] and cursor > 0) {
cursor--;
if (cursor < top) {
top -= 1;
bottom -= 1;
}
}
entries[oldCursor]->unselect();
entries[cursor]->select();
@ -91,11 +93,12 @@ void Menu::update(u8 const* keys)
void Menu::render()
{
for (auto entry : entries)
entry->render();
for (int i = top; i < entries.size() && i < bottom; ++i) {
int y = (i - top) * FONT_SZ;
entries[i]->render(TEXT_CENTER, y);
}
}
void FileMenu::change_dir(string dir)
{
clear();
@ -113,14 +116,12 @@ void FileMenu::change_dir(string dir)
if (dirp->d_type == DT_DIR)
{
add(new Entry(name + "/",
[=]{ change_dir(path); },
0));
[=]{ change_dir(path); }));
}
else if (name.size() > 4 and name.substr(name.size() - 4) == ".nes")
{
add(new Entry(name,
[=]{ Cartridge::load(path.c_str()); toggle_pause(); },
0));
[=]{ Cartridge::load(path.c_str()); toggle_pause(); }));
}
}
closedir(dp);

View file

@ -184,7 +184,7 @@ void eval_sprites()
secOam[n].attr = oamMem[i*4 + 2];
secOam[n].x = oamMem[i*4 + 3];
if (++n > 8)
if (++n >= 8)
{
status.sprOvf = true;
break;