- Treballant en el menu del joc

This commit is contained in:
2024-12-03 15:29:05 +01:00
parent eee5753a7f
commit 2ebe1916a4
5 changed files with 177 additions and 4 deletions

View File

@@ -8,6 +8,8 @@
#include "zx_tape.h"
#include <SDL2/SDL.h>
#include <string.h>
#include "ui.h"
#include "ui_menu.h"
uint8_t memory[65536];
uint32_t time = 0;
@@ -26,6 +28,16 @@ int main(int argc, char *argv[])
z80debug::init();
zxscreen::init();
ui::menu::init();
int menu = ui::menu::addsubmenu("FILE");
ui::menu::addoption(menu, "LOAD TAPE", nullptr);
ui::menu::addoption(menu, "SAVE TAPE", nullptr);
ui::menu::addseparator(menu);
ui::menu::addoption(menu, "LOAD STATE", nullptr);
ui::menu::addoption(menu, "SAVE STATE", nullptr);
menu = ui::menu::addsubmenu("OPTIONS");
ui::menu::addbooloption(menu, "BERSERK MODE", false, nullptr);
zx_ula::sound_init();
zx_tape::load("alien8.tap");
@@ -91,6 +103,7 @@ int main(int argc, char *argv[])
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.scancode==SDL_SCANCODE_ESCAPE) {
z80debug::pause();
ui::setrenderer(zxscreen::getrenderer());
zxscreen::redraw();
} else if (e.key.keysym.scancode==SDL_SCANCODE_F1) {
zxscreen::decZoom();
@@ -141,6 +154,10 @@ int main(int argc, char *argv[])
time = SDL_GetTicks();
}
}
} else if (z80debug::paused()) {
zxscreen::redraw(false);
ui::menu::show();
zxscreen::present();
}
}

130
ui_menu.cpp Normal file
View File

@@ -0,0 +1,130 @@
#include "ui_menu.h"
#include "ui.h"
#include <vector>
#include <string>
#include "zx_screen.h"
namespace ui
{
namespace menu
{
#define OPTION_TYPE_NORMAL 0
#define OPTION_TYPE_SEPARATOR 1
#define OPTION_TYPE_BOOLEAN 2
struct option_t
{
std::string label;
int type;
int value;
int (*callback)(int);
};
struct menu_t
{
std::string label;
SDL_Rect rect;
std::vector<option_t> options;
};
std::vector<menu_t> menus;
int visible_menu = -1;
int menu_x = 0;
void init()
{
// No se si hi ha algo que fer ací...
}
void show()
{
int mx, my;
Uint32 mb = SDL_GetMouseState(&mx, &my);
mx=mx/CHR_W; my=my/CHR_H;
ui::printrect(0, 0, 59, 1, COLOR_BLACK);
int opt_pos=1;
int index=0;
for (auto menu : menus)
{
const int text_size = (menu.label.size()+2);
uint8_t text_color = COLOR_WHITE;
if (my<1 && mx>=opt_pos && mx<opt_pos+text_size)
{
ui::printrect(opt_pos-1, 0, text_size, 1, COLOR_DARK_BLUE);
text_color = COLOR_WHITE;
visible_menu = index;
menu_x = opt_pos-1;
}
ui::printtxt(opt_pos, 0, menu.label.c_str(), text_color);
opt_pos+=text_size;
index++;
}
if (visible_menu!=-1)
{
opt_pos=2;
menu_t m = menus[visible_menu];
ui::printrect(menu_x, 1, 22, m.options.size()+2, COLOR_BLACK);
ui::box(menu_x, 1, 22, m.options.size()+2, COLOR_WHITE);
for (auto option : m.options)
{
if (option.type!=OPTION_TYPE_SEPARATOR)
{
const int text_size = (option.label.size()+2);
uint8_t text_color = COLOR_WHITE;
if (my==opt_pos && mx>=menu_x && mx<menu_x+text_size)
{
ui::printrect(menu_x+1, opt_pos, 20, 1, COLOR_DARK_BLUE);
text_color = COLOR_WHITE;
}
ui::printtxt(menu_x+1, opt_pos, option.label.c_str(), text_color);
if (option.type==OPTION_TYPE_BOOLEAN)
{
const char *check = option.value?"[X]":"[ ]";
ui::printtxt(menu_x+18, opt_pos, check, text_color);
}
}
opt_pos++;
}
}
}
const int addsubmenu(const char *label)
{
menu_t m;
m.label = label;
m.rect = {0, 0, 100, 0};
menus.push_back(m);
return menus.size()-1;
}
void addoption(int menu, const char *label, int(*callback)(int))
{
option_t op;
op.type = OPTION_TYPE_NORMAL;
op.label = label;
op.callback = callback;
menus[menu].options.push_back(op);
menus[menu].rect.h += 15;
}
void addbooloption(int menu, const char *label, bool default_value, int(*callback)(int))
{
option_t op;
op.type = OPTION_TYPE_BOOLEAN;
op.label = label;
op.value = default_value?1:0;
op.callback = callback;
menus[menu].options.push_back(op);
menus[menu].rect.h += 15;
}
void addseparator(int menu)
{
option_t op;
op.type = OPTION_TYPE_SEPARATOR;
menus[menu].options.push_back(op);
}
}
}

14
ui_menu.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
namespace ui
{
namespace menu
{
void init();
void show();
const int addsubmenu(const char *label);
void addoption(int menu, const char *label, int(*callback)(int));
void addbooloption(int menu, const char *label, bool default_value, int(*callback)(int));
void addseparator(int menu);
}
}

View File

@@ -173,7 +173,7 @@ namespace zxscreen
}
}
void redraw()
void redraw(const bool present)
{
if (zx_tape::berserk()) return;
@@ -192,6 +192,11 @@ namespace zxscreen
// Pintem la textura a pantalla
SDL_RenderCopy(ren, tex, NULL, &dest_rect);
if (present) SDL_RenderPresent(ren);
}
void present()
{
SDL_RenderPresent(ren);
}
@@ -224,4 +229,8 @@ namespace zxscreen
reinit();
}
SDL_Renderer *getrenderer()
{
return ren;
}
}

View File

@@ -1,14 +1,17 @@
#pragma once
#include <stdint.h>
#include <SDL2/SDL.h>
namespace zxscreen
{
void init();
void reinit();
void refresh(const uint8_t dt);
void redraw();
void redraw(const bool present=true);
void present();
void incZoom();
void decZoom();
void toggleFullscreen();
SDL_Renderer *getrenderer();
}