aboutsummaryrefslogtreecommitdiffstats
path: root/src/titlescreen.cpp
blob: a416338027a93ac2efdd53cb0f8b6e5439222d5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "titlescreen.h"
#include "game.h"

#include "boxii/boxii.h"
#include "boxii/input.h"

#include <fstream>

static void
printTitle(std::shared_ptr<Boxii::Window> graphicWindow)
{
    std::fstream titlefile;
    std::string tmp;
    int cx = 0;
    int cy = 0;
    int startb = 0;

    (void)graphicWindow->getCenter(cx, cy);
    titlefile.open("assets/title.txt", std::ios::in);
    if(!titlefile.is_open()) {
        return;
    }

    cy /= 3;
    while(getline(titlefile, tmp)){
        int r = 255;
        int b = startb;
        int x = cx - (tmp.length() / 2);
        for(const char *c = tmp.c_str(); *c; c++){
            graphicWindow->plot(x++, cy, *c, (r << 16) | (b), 0);
            r -= 3;
            b += 3;
        }
        startb += 18;
        cy++;
    }
}

static const int selectionsTotal = 5;
static const std::string selectionTexts[selectionsTotal] = 
{
    "    Play    ",
    "   Worlds   ",
    "  Settings  ",
    "  Test Gen  ",
    "    Quit    "
};

static void
printSelections(std::shared_ptr<Boxii::TextWindow> selectionWindow, int selection)
{
    int selwidth = selectionWindow->getBodyWidth() / selectionsTotal;
    int selcenter = selwidth / 2;
    int sely = (selectionWindow->getBodyHeight() / 2);

    for(int i = 0; i < selectionsTotal; i++){
        if(selection == i){
            selectionWindow->setfg(0x0);
            selectionWindow->setbg(0xFFFFFF);
        }
        selectionWindow->moveCursor((selwidth * i) + (selcenter - (selectionTexts[i].length() / 2)), sely);
        selectionWindow->print(selectionTexts[i].c_str());
        selectionWindow->setbg(0x0);
        selectionWindow->setfg(0xFFFFFF);
        selectionWindow->print("        ");
    }
}

TitleScreen::TitleScreen() :
    m_graphicWindow(nullptr),
    m_selectionWindow(nullptr),
    m_selection(0)
{
    int sw = Boxii::getTerminalWidth();
    int sh = Boxii::getTerminalHeight();

    int graphicHeight = (sh / 4) * 3;
    int selectionHeight = (sh / 4);

    m_graphicWindow = Boxii::makeWindow<Boxii::Window>(0, 0, sw, graphicHeight + 1);
    m_selectionWindow = Boxii::makeWindow<Boxii::TextWindow>(0, graphicHeight + 1, sw, selectionHeight + 1);

    m_selectionWindow->border('_', '_', '_', '_', '_', ' ', 0xFFFFFF, 0x0);

    printTitle(m_graphicWindow);
    printSelections(m_selectionWindow, m_selection);
}

TitleScreen::~TitleScreen()
{

 }

void
TitleScreen::activate()
{
    Boxii::pushToFront(m_graphicWindow);
    Boxii::pushToFront(m_selectionWindow);
}

void
TitleScreen::deactivate()
{
    Boxii::remove(m_graphicWindow);
    Boxii::remove(m_selectionWindow);
}

void
TitleScreen::parseSelection()
{
    if(m_selection == static_cast<int>(Selection::QUIT)) Game::stop();
    if(m_selection == static_cast<int>(Selection::TESTGEN)) {
        Game::popScreen();
        Game::pushScreen("test");
    }
}

void 
TitleScreen::update()
{
    int c = Boxii::Input::getch(true);
    if(c == Boxii::KEY_ESCAPE) {
        Game::stop();
    }

    if(c == Boxii::KEY_LEFT){
        m_selection--;
        if(m_selection < 0) m_selection = selectionsTotal - 1;
    }
    if(c == Boxii::KEY_RIGHT){
        m_selection++;
        if(m_selection >= selectionsTotal) m_selection = 0;
    }
    if(c == Boxii::KEY_RETURN){
        parseSelection();
    }

    printSelections(m_selectionWindow, m_selection);
}