/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/text.cc

  • Committer: Tim Marston
  • Date: 2012-05-18 18:29:50 UTC
  • Revision ID: tim@ed.am-20120518182950-t85bn9a21n72uzm8
text messages are now individually enabled and draw()n automatically

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
// mode-specific message parameters
40
40
static signed long _message_params[ NUM_MESSAGE_BUFFERS ];
41
41
 
 
42
// text scaling factors
 
43
static char _message_scale[ NUM_MESSAGE_BUFFERS ];
 
44
 
42
45
// time last frame
43
46
static unsigned long _last_millis;
44
47
 
49
52
static bool _do_draw_reset;
50
53
 
51
54
 
 
55
#define MASK_MODE_SIZE 0x07
 
56
#define MASK_MODE_POS 0x40
 
57
 
52
58
void reset_message_param( int message_num )
53
59
{
54
60
        if( _message_modes[ message_num ] & Text::MODE_HSCROLL )
55
61
        {
56
 
                switch( _message_modes[ message_num ] & 0x1c )
 
62
                switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
57
63
                {
58
64
                case Text::MODE_ONEQUARTER:
59
65
                        _message_params[ message_num ] = -NUM_SEGMENTS / 4; break;
71
77
        }
72
78
        else
73
79
        {
74
 
                switch( _message_modes[ message_num ] & 0x1c )
 
80
                switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
75
81
                {
76
82
                case Text::MODE_ONEQUARTER:
77
83
                        _message_params[ message_num ] = NUM_SEGMENTS / 4; break;
87
93
                        _message_params[ message_num ] = NUM_SEGMENTS; break;
88
94
                }
89
95
                _message_params[ message_num ] = -( _message_params[ message_num ] -
90
 
                        TextRenderer::get_width( _message_lens[ message_num ] ) ) / 2;
 
96
                        TextRenderer::get_width( _message_lens[ message_num ],
 
97
                                        _message_scale[ message_num ] ) ) / 2;
91
98
        }
92
99
}
93
100
 
94
101
 
95
 
void Text::set_message( int message_num, PString &pstring )
96
 
{
97
 
        // update message
98
 
        _message_lens[ message_num ] = pstring.length();
99
 
}
100
 
 
101
 
 
102
 
void Text::reset_message( int message_num, char mode )
103
 
{
104
 
        _message_modes[ message_num ] = mode;
105
 
        reset_message_param( message_num );
106
 
}
107
 
 
108
 
 
109
102
void Text::reset()
110
103
{
111
104
        // reset renderer
113
106
        _last_millis = ::millis();
114
107
        _do_draw_reset = false;
115
108
 
 
109
        // clear all message modes (disable messages)
 
110
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
 
111
                _message_modes[ a ] = 0;
 
112
 
116
113
        // reset the text renderer's output buffer
117
114
        TextRenderer::reset_buffer();
118
115
}
119
116
 
120
117
 
 
118
void Text::set_up_message( int message_num, char mode, char scale )
 
119
{
 
120
        _message_modes[ message_num ] = mode;
 
121
        _message_scale[ message_num ] = scale;
 
122
}
 
123
 
 
124
 
 
125
void Text::set_message_text( int message_num, PString &pstring )
 
126
{
 
127
        // update message
 
128
        _message_lens[ message_num ] = pstring.length();
 
129
}
 
130
 
 
131
 
121
132
void Text::draw_reset()
122
133
{
123
134
        // how many milliseconds have elapsed since last frame?
130
141
}
131
142
 
132
143
 
133
 
void Text::draw( int message_num, int segment )
 
144
static void draw_message( int message_num, int segment )
134
145
{
 
146
        TextRenderer::buffer_in_use();
 
147
 
135
148
        // perform a mid-frame reset, as necessary
136
 
        if( _do_draw_reset && segment > NUM_SEGMENTS / 2 ) {
137
 
                _do_draw_reset = false;
 
149
        if( _do_draw_reset ) // && segment > NUM_SEGMENTS / 2 )
 
150
        {
 
151
                // reset message param
 
152
                reset_message_param( message_num );
138
153
 
139
 
                if( _message_modes[ message_num ] & MODE_HSCROLL )
140
 
                {
141
 
                        // add to horizontal scroll position
 
154
                // add to horizontal scroll position
 
155
                if( _message_modes[ message_num ] & Text::MODE_HSCROLL ) {
142
156
                        _message_params[ message_num ] +=
143
157
                                _duration * TEXT_SCROLL_SPEED / 1000;
144
158
                        long excess = _message_params[ message_num ] -
145
 
                                TextRenderer::get_width( _message_lens[ message_num ] );
 
159
                                TextRenderer::get_width( _message_lens[ message_num ],
 
160
                                                _message_scale[ message_num ] );
146
161
                        if( excess >= 0 ) {
147
162
                                reset_message_param( message_num );
148
163
                                _message_params[ message_num ] += excess;
152
167
 
153
168
        // calculate the width
154
169
        int width;
155
 
        switch( _message_modes[ message_num ] & 0x1c )
 
170
        switch( _message_modes[ message_num ] & MASK_MODE_SIZE )
156
171
        {
157
 
        case MODE_ONEQUARTER:
 
172
        case Text::MODE_ONEQUARTER:
158
173
                width = NUM_SEGMENTS / 4;
159
174
                break;
160
 
        case MODE_ONETHIRD:
 
175
        case Text::MODE_ONETHIRD:
161
176
                width = NUM_SEGMENTS / 3;
162
177
                break;
163
 
        case MODE_HALF:
 
178
        case Text::MODE_HALF:
164
179
                width = NUM_SEGMENTS / 2;
165
180
                break;
166
 
        case MODE_TWOTHIRDS:
 
181
        case Text::MODE_TWOTHIRDS:
167
182
                width = NUM_SEGMENTS * 2 / 3;
168
183
                break;
169
 
        case MODE_THREEQUARTERS:
 
184
        case Text::MODE_THREEQUARTERS:
170
185
                width = NUM_SEGMENTS * 3 / 4;
171
186
                break;
172
 
        case MODE_ALL:
 
187
        case Text::MODE_ALL:
173
188
                width = NUM_SEGMENTS;
174
189
                break;
175
190
        }
176
191
 
177
192
        // convert segment to display-space
178
 
        switch( _message_modes[ message_num ] & 0x03 )
 
193
        switch( _message_modes[ message_num ] & MASK_MODE_POS )
179
194
        {
180
 
        case MODE_TOP:
 
195
        case Text::MODE_TOP:
181
196
                if( segment < width / 2 )
182
197
                        segment += width / 2;
183
198
                else if( segment >= NUM_SEGMENTS - width / 2 )
185
200
                else
186
201
                        return;
187
202
                break;
188
 
        case MODE_BOTTOM:
 
203
        case Text::MODE_BOTTOM:
189
204
                if( segment > ( NUM_SEGMENTS - width ) / 2 &&
190
205
                        segment < ( NUM_SEGMENTS - width ) / 2 + width )
191
206
                {
198
213
 
199
214
        // render
200
215
        TextRenderer::render(
201
 
                _messages[ message_num ], _message_lens[ message_num ],
 
216
                Text::_messages[ message_num ], _message_lens[ message_num ],
202
217
                (long)segment + _message_params[ message_num ],
203
 
                ( _message_modes[ message_num ] & 1 ) == 0, 0 );
204
 
}
 
218
                ( _message_modes[ message_num ] & MASK_MODE_POS ) == Text::MODE_BOTTOM,
 
219
                0, _message_scale[ message_num ] );
 
220
}
 
221
 
 
222
 
 
223
void Text::draw( int segment )
 
224
{
 
225
        // draw any active messages
 
226
        for( int a = 0; a < NUM_MESSAGE_BUFFERS; a++ )
 
227
                if( _message_modes[ a ] & MASK_MODE_SIZE )
 
228
                        draw_message( a, segment );
 
229
 
 
230
        // turn off any requested draw reset after all messages are drawn
 
231
        _do_draw_reset = false;
 
232
}
 
233