Subversion Repositories gelsvn

Rev

Rev 630 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 630 Rev 647
Line 9... Line 9...
9
 * @author Anders Wang Kristensen <awk@imm.dtu.dk>
9
 * @author Anders Wang Kristensen <awk@imm.dtu.dk>
10
 * @date   Fri Oct 22 18:36:58 2011
10
 * @date   Fri Oct 22 18:36:58 2011
11
 */
11
 */
12
 
12
 
13
#include "../GLGraphics/Console.h"
13
#include "../GLGraphics/Console.h"
14
#include "../GLGraphics/gel_glut.h"
14
#include "../GLGraphics/gel_glu.h"
15
 
-
 
16
#include <cstdarg>
15
#include <cstdarg>
17
#include <set>
16
#include <set>
18
#include <iostream> //cerr
17
#include <iostream> //cerr
19
#include <iterator> //back_inserter
18
#include <iterator> //back_inserter
20
#include <fstream>
19
#include <fstream>
Line 97... Line 96...
97
    m_history_index = 0;
96
    m_history_index = 0;
98
}
97
}
99
 
98
 
100
//----------------------------------------------------------------------------
99
//----------------------------------------------------------------------------
101
 
100
 
102
void Console::display()
101
void Console::display(int scaling)
103
{
102
{
104
    if (m_font == 0)
103
    if (m_font == 0)
105
    {
104
    {
106
        glGenTextures(1, &m_font);
105
        glGenTextures(1, &m_font);
107
        glBindTexture(GL_TEXTURE_2D, m_font);
106
        glBindTexture(GL_TEXTURE_2D, m_font);
Line 115... Line 114...
115
            for (int w=0; w<width; ++w)
114
            for (int w=0; w<width; ++w)
116
                std::swap(*p++, *q++);
115
                std::swap(*p++, *q++);
117
        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, width, height, 0,
116
        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA8, width, height, 0,
118
            GL_ALPHA, GL_UNSIGNED_BYTE, data);
117
            GL_ALPHA, GL_UNSIGNED_BYTE, data);
119
        stbi_image_free(data);
118
        stbi_image_free(data);
120
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
119
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
121
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
120
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
122
        glBindTexture(GL_TEXTURE_2D, 0);
121
        glBindTexture(GL_TEXTURE_2D, 0);
123
    }
122
    }
124
 
123
 
125
    //setup matrices
124
    //setup matrices
126
    int vp[4];
125
    int vp[4];
Line 140... Line 139...
140
    glPushAttrib(GL_COLOR_BUFFER_BIT);
139
    glPushAttrib(GL_COLOR_BUFFER_BIT);
141
    glPushAttrib(GL_ENABLE_BIT);
140
    glPushAttrib(GL_ENABLE_BIT);
142
    glEnable(GL_BLEND);
141
    glEnable(GL_BLEND);
143
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
142
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
144
    glDisable(GL_LIGHTING);
143
    glDisable(GL_LIGHTING);
-
 
144
    glDisable(GL_DEPTH_TEST);
145
 
145
    
146
    glColor4f(0.15f, 0.15f, 0.15f, 0.75f);
146
    glColor4f(0.15f, 0.15f, 0.15f, 0.75f);
147
    glRecti(0, 0, width, height);
147
    glRecti(0, 0, width, height);
148
    glColor4f(0.7f, 0.7f, 0.7f, 0.75f);
-
 
149
    glRecti(0, 1, width, 2);
-
 
150
    glColor4f(0.5f, 0.5f, 0.5f, 0.75f);
-
 
151
    glRecti(0, 0, width, 1);
-
 
152
 
-
 
153
    glPopAttrib();
-
 
154
    glPopAttrib();
-
 
155
 
148
 
156
    int current = 3;
149
    int current = 3;
157
 
150
 
158
    //draw caret
151
    //draw caret
159
    if (!m_is_executing)
152
    if (!m_is_executing)
160
    {
153
    {
-
 
154
        glColor4f(1,1,0, 0.75f);
161
        const int cx = int(m_caret*char_spacing) + char_spacing + 1;
155
        const int cx = int(m_caret*char_spacing*scaling) + char_spacing*scaling + 1;
162
        glRecti(cx, current, cx+char_spacing, current+2);
156
        glRecti(cx, current, cx+char_spacing*scaling, current+2);
163
    }
157
    }
-
 
158
    glPopAttrib();
-
 
159
    glPopAttrib();
164
 
160
 
165
    //restore matrices
161
    //restore matrices
166
    glMatrixMode(GL_PROJECTION);
162
    glMatrixMode(GL_PROJECTION);
167
    glPopMatrix();
163
    glPopMatrix();
168
    glMatrixMode(GL_MODELVIEW);
164
    glMatrixMode(GL_MODELVIEW);
169
    glPopMatrix();
165
    glPopMatrix();
170
 
166
 
171
    //draw command line
167
    //draw command line
172
    if (!m_is_executing)
168
    if (!m_is_executing)
173
    {
169
    {
174
        draw_textf(1, current,
170
        draw_textf(scaling, 1, current,
175
                   0.9f, 0.9f, 0.9f,
171
                   0.9f, 0.9f, 0.9f,
176
                   ">%s", m_current_command.c_str());
172
                   ">%s", m_current_command.c_str());
177
        current += line_spacing;
173
        current += line_spacing*scaling;
178
    }
174
    }
179
 
175
 
180
    //draw rest
176
    //draw rest
181
    std::vector<std::string>::const_reverse_iterator it;
177
    std::vector<std::string>::const_reverse_iterator it;
182
    for (it=m_buffer.rbegin(); it!=m_buffer.rend(); ++it)
178
    for (it=m_buffer.rbegin(); it!=m_buffer.rend(); ++it)
183
    {
179
    {
184
        draw_text(1, current,
180
        draw_text(scaling, 1, current,
185
                  0.75f, 0.75f, 0.75f,
181
                  0.75f, 0.75f, 0.75f,
186
                  it->c_str());
182
                  it->c_str());
187
        current += line_spacing;
183
        current += scaling * line_spacing;
188
 
184
 
189
        if (current > height)
185
        if (current > height)
190
            break;
186
            break;
191
    }
187
    }
192
}
188
}
193
 
189
 
194
//----------------------------------------------------------------------------
190
//----------------------------------------------------------------------------
195
 
191
 
196
void Console::draw_text(int cx, int cy,
192
void Console::draw_text(int scaling, int cx, int cy,
197
                        float r, float g, float b,
193
                        float r, float g, float b,
198
                        const char* buffer)
194
                        const char* buffer)
199
{
195
{
200
    //save OpenGL state
196
    //save OpenGL state
201
    glPushAttrib(GL_ENABLE_BIT);
197
    glPushAttrib(GL_ENABLE_BIT);
Line 245... Line 241...
245
        float tyu = (fy + 15.f) / 256.f;
241
        float tyu = (fy + 15.f) / 256.f;
246
 
242
 
247
        glTexCoord2f(txl, tyl);
243
        glTexCoord2f(txl, tyl);
248
        glVertex2i(cx +  0, cy +  1);
244
        glVertex2i(cx +  0, cy +  1);
249
        glTexCoord2f(txr, tyl);
245
        glTexCoord2f(txr, tyl);
250
        glVertex2i(cx + 7, cy +  1);
246
        glVertex2i(cx + 7 * scaling, cy +  1);
251
        glTexCoord2f(txr, tyu);
247
        glTexCoord2f(txr, tyu);
252
        glVertex2i(cx + 7, cy + 16);
248
        glVertex2i(cx + 7 * scaling, cy +  16 * scaling);
253
        glTexCoord2f(txl, tyu);
249
        glTexCoord2f(txl, tyu);
254
        glVertex2i(cx +  0, cy + 16);
250
        glVertex2i(cx +  0, cy +  16 * scaling);
255
 
251
 
256
        cx += char_spacing;
252
        cx += char_spacing * scaling;
257
    }
253
    }
258
    glEnd();
254
    glEnd();
259
 
255
 
260
    glBindTexture(GL_TEXTURE_2D, 0);
256
    glBindTexture(GL_TEXTURE_2D, 0);
261
 
257
 
Line 269... Line 265...
269
    glPopAttrib();
265
    glPopAttrib();
270
    glPopAttrib();
266
    glPopAttrib();
271
    glPopAttrib();
267
    glPopAttrib();
272
}
268
}
273
 
269
 
274
void Console::draw_textf(int x, int y,
270
void Console::draw_textf(int scaling, int x, int y,
275
                         float r, float g, float b,
271
                         float r, float g, float b,
276
                         const char* fmt, ...)
272
                         const char* fmt, ...)
277
{
273
{
278
    //format text
274
    //format text
279
    char buffer[1024];
275
    char buffer[1024];
280
    va_list arguments;
276
    va_list arguments;
281
    va_start(arguments, fmt);
277
    va_start(arguments, fmt);
282
    vsnprintf(buffer, 1024, fmt, arguments);
278
    vsnprintf(buffer, 1024, fmt, arguments);
283
    va_end(arguments);
279
    va_end(arguments);
284
 
280
 
285
    draw_text(x, y, r, g, b, buffer);
281
    draw_text(scaling, x, y, r, g, b, buffer);
286
}
282
}
287
 
283
 
288
//----------------------------------------------------------------------------
284
//----------------------------------------------------------------------------
289
 
285
 
290
void Console::tab_completion()
286
void Console::tab_completion()
Line 384... Line 380...
384
        }
380
        }
385
        break;
381
        break;
386
    }
382
    }
387
}
383
}
388
 
384
 
389
void Console::special(int key)
385
void Console::key_left() {
390
{
-
 
391
    switch (key)
-
 
392
    {
-
 
393
    case GLUT_KEY_LEFT:
-
 
394
        if (m_caret > 0)
386
    if (m_caret > 0)
395
            m_caret -= 1;
387
        m_caret -= 1;
396
        break;
-
 
397
 
-
 
398
    case GLUT_KEY_RIGHT:
-
 
399
        if (m_caret < m_current_command.size())
-
 
400
            m_caret += 1;
-
 
401
        break;
-
 
402
 
388
}
403
    case GLUT_KEY_HOME:
-
 
404
        m_caret = 0;
-
 
405
        break;
-
 
406
 
389
 
407
    case GLUT_KEY_END:
390
void Console::key_right(){
408
        m_caret = m_current_command.size();
391
    if (m_caret < m_current_command.size())
409
        break;
392
        m_caret += 1;
-
 
393
}
410
 
394
 
411
    case GLUT_KEY_UP:
395
void Console::key_home() {
412
        if (!m_history.empty())
396
    m_caret = 0;
413
        {
397
}
414
            if (m_history_index > 0)
-
 
415
                m_history_index--;
-
 
416
 
398
 
417
            m_current_command = m_history[m_history_index];
399
void Console::key_end() {
418
            m_caret = m_current_command.size();
400
    m_caret = m_current_command.size();
419
        }
401
}
420
        break;
-
 
421
 
402
 
422
    case GLUT_KEY_DOWN:
403
void Console::key_up() {
423
        if (m_history_index < m_history.size())
404
    if (!m_history.empty())
424
        {
405
    {
425
            if (m_history_index < m_history.size()-1)
406
        if (m_history_index > 0)
426
                m_history_index++;
407
            m_history_index--;
-
 
408
        
-
 
409
        m_current_command = m_history[m_history_index];
-
 
410
        m_caret = m_current_command.size();
-
 
411
    }
-
 
412
}
427
 
413
 
-
 
414
void Console::key_down() {
428
            assert(m_history_index < m_history.size());
415
    if (m_history_index < m_history.size())
-
 
416
    {
429
            m_current_command = m_history[m_history_index];
417
        if (m_history_index < m_history.size()-1)
430
            m_caret = m_current_command.size();
418
            m_history_index++;
431
        }
419
        
-
 
420
        assert(m_history_index < m_history.size());
-
 
421
        m_current_command = m_history[m_history_index];
432
        break;
422
        m_caret = m_current_command.size();
433
    }
423
    }
434
}
424
}
435
 
425
 
-
 
426
 
436
//----------------------------------------------------------------------------
427
//----------------------------------------------------------------------------
437
 
428
 
438
void Console::execute(const char* buffer)
429
void Console::execute(const char* buffer)
439
{
430
{
440
    //split into command arg0 arg1 ..
431
    //split into command arg0 arg1 ..