/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-03-21 20:35:28 UTC
  • Revision ID: tim@ed.am-20120321203528-wfhpych1tub75rgj
fixed bug initialising text services on mode activation

Show diffs side-by-side

added added

removed removed

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