summaryrefslogtreecommitdiff
path: root/src/Frame.cpp
blob: ca0e41eb3388495ecfb08ec7e0693000580e5112 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "Frame.hpp"

Frame::Frame(string location, int nRow, int nCol)
{
    hasSuper = FALSE;
    super = NULL;
    filled = false;
    int i, j=0;
    string line;
    ifstream file (location);

    if(file.is_open())
    {
        getline(file,line);
        m.push_back(line);
        i=line.length();
        while(getline(file,line))
        {
            m.push_back(line);
            j++;
        }
    }

    w = newwin(j,i,nRow,nCol);
    height = j;
    width = i;
    row = nRow;
    col = nCol; 
}


Frame::Frame(Frame &sw, int rows, int cols, int nRow, int nCol)
{
    filled = false;
    hasSuper = TRUE;
    super = sw.getWin();
    w = derwin(super,rows,cols,nRow,nCol);
    height = rows;
    width = cols;
    row = nRow;
    col = nCol;
}

Frame::~Frame()
{
    delwin(w);
}

void Frame::center(Character &ch)
{
    if(hasSuper)
    {
        int rr = row;
        int cc = col;
        int hh, ww;
        int r = ch.getLocation().x - height/2;
        int c = ch.getLocation().y - width/2;

        getmaxyx(super, hh, ww);

        if(c + width >= ww)
        {
            int delta = ww - (c + width);
            cc = c + delta;
        }
        else
            cc = c;
        
        if(r + height >= hh)
        {
            int delta = hh - (r + height);
            rr = r + delta;
        }
        else
            rr = r;
        
        if(r < 0)
            rr = 0;
        
        if(c < 0)
            cc = 0;

        move(rr,cc);
    }
}

void Frame::refresh()
{
    if(hasSuper)
        touchwin(super);
    wrefresh(w);
}

void Frame::move(int nRow, int nCol)
{
    if(hasSuper)
    {
        mvderwin(w,nRow,nCol);
        row = nRow;
        col = nCol;
        refresh();
    }
}

void Frame::fillWindow()
{ 
    
    for(int i = 0; i < width; i++)
        for(int j = 0; j < height; j++)
        {
            if(m[j][i] == 'O' || m[j][i] == '0')
            {
                wattron(w,COLOR_PAIR(2));
                if(!filled)
                {
                    Location l(j,i);
                    impassable.push_back(l);
                }
            }
            else
                wattron(w,COLOR_PAIR(1));
    
            mvwaddch(w,j,i,m[j][i]);
        }
    
    wattron(w,COLOR_PAIR(3));
    for(int y = 0; y < height; ++y)
    {      
	mvwaddch(w, y, 0, '-');
	mvwaddch(w, y, width - 1, '-');
        if(!filled)
        {
            Location l1(y,0);
            Location l2(y,width-1);
            impassable.push_back(l1);
            impassable.push_back(l2);
        }
    }

    for(int x = 0; x < width; ++x) 
    {
	mvwaddch(w, 0, x, '|');
	mvwaddch(w, height - 1, x, '|');
        if(!filled)
        {
            Location l1(0,x);
            Location l2(height-1,x);
            impassable.push_back(l1);
            impassable.push_back(l2);
        }
    }
    wattroff(w,COLOR_PAIR(3));
    filled = true;
}