Skip to content

Commit

Permalink
Added m_ prefixes to Console
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 17, 2014
1 parent 75fc1b8 commit 289cdcc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 126 deletions.
215 changes: 108 additions & 107 deletions src/supertux/console.cpp
Expand Up @@ -29,41 +29,31 @@
static const float FADE_SPEED = 1;

Console::Console() :
history(),
history_position(history.end()),
lines(),
background(),
background2(),
vm(NULL),
vm_object(),
backgroundOffset(0),
height(0),
alpha(1.0),
offset(0),
focused(false),
font(),
fontheight(),
stayOpen(0)
m_history(),
m_history_position(m_history.end()),
m_lines(),
m_background(Surface::create("images/engine/console.png")),
m_background2(Surface::create("images/engine/console2.png")),
m_vm(NULL),
m_vm_object(),
m_backgroundOffset(0),
m_height(0),
m_alpha(1.0),
m_offset(0),
m_focused(false),
m_font(new Font(Font::FIXED, "fonts/andale12.stf", 1)),
m_stayOpen(0)
{
fontheight = 8;
}

Console::~Console()
{
if(vm != NULL) {
sq_release(scripting::global_vm, &vm_object);
if (m_vm != NULL)
{
sq_release(scripting::global_vm, &m_vm_object);
}
}

void
Console::init_graphics()
{
font.reset(new Font(Font::FIXED,"fonts/andale12.stf",1));
fontheight = font->get_height();
background = Surface::create("images/engine/console.png");
background2 = Surface::create("images/engine/console2.png");
}

void
Console::flush(ConsoleStreamBuffer* buffer)
{
Expand All @@ -80,18 +70,18 @@ Console::flush(ConsoleStreamBuffer* buffer)
void
Console::ready_vm()
{
if(vm == NULL) {
vm = scripting::global_vm;
HSQUIRRELVM new_vm = sq_newthread(vm, 16);
if(m_vm == NULL) {
m_vm = scripting::global_vm;
HSQUIRRELVM new_vm = sq_newthread(m_vm, 16);
if(new_vm == NULL)
throw scripting::SquirrelError(vm, "Couldn't create new VM thread for console");
throw scripting::SquirrelError(m_vm, "Couldn't create new VM thread for console");

// store reference to thread
sq_resetobject(&vm_object);
if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
throw scripting::SquirrelError(vm, "Couldn't get vm object for console");
sq_addref(vm, &vm_object);
sq_pop(vm, 1);
sq_resetobject(&m_vm_object);
if(SQ_FAILED(sq_getstackobj(m_vm, -1, &m_vm_object)))
throw scripting::SquirrelError(m_vm, "Couldn't get vm object for console");
sq_addref(m_vm, &m_vm_object);
sq_pop(m_vm, 1);

// create new roottable for thread
sq_newtable(new_vm);
Expand All @@ -101,12 +91,12 @@ Console::ready_vm()

sq_setroottable(new_vm);

vm = new_vm;
m_vm = new_vm;

try {
std::string filename = "scripts/console.nut";
IFileStream stream(filename);
scripting::compile_and_run(vm, stream, filename);
scripting::compile_and_run(m_vm, stream, filename);
} catch(std::exception& e) {
log_warning << "Couldn't load console.nut: " << e.what() << std::endl;
}
Expand All @@ -120,26 +110,26 @@ Console::execute_script(const std::string& command)

ready_vm();

SQInteger oldtop = sq_gettop(vm);
SQInteger oldtop = sq_gettop(m_vm);
try {
if(SQ_FAILED(sq_compilebuffer(vm, command.c_str(), command.length(),
if(SQ_FAILED(sq_compilebuffer(m_vm, command.c_str(), command.length(),
"", SQTrue)))
throw SquirrelError(vm, "Couldn't compile command");
throw SquirrelError(m_vm, "Couldn't compile command");

sq_pushroottable(vm);
if(SQ_FAILED(sq_call(vm, 1, SQTrue, SQTrue)))
throw SquirrelError(vm, "Problem while executing command");
sq_pushroottable(m_vm);
if(SQ_FAILED(sq_call(m_vm, 1, SQTrue, SQTrue)))
throw SquirrelError(m_vm, "Problem while executing command");

if(sq_gettype(vm, -1) != OT_NULL)
addLines(squirrel2string(vm, -1));
if(sq_gettype(m_vm, -1) != OT_NULL)
addLines(squirrel2string(m_vm, -1));
} catch(std::exception& e) {
addLines(e.what());
}
SQInteger newtop = sq_gettop(vm);
SQInteger newtop = sq_gettop(m_vm);
if(newtop < oldtop) {
log_fatal << "Script destroyed squirrel stack..." << std::endl;
} else {
sq_settop(vm, oldtop);
sq_settop(m_vm, oldtop);
}
}

Expand Down Expand Up @@ -179,26 +169,26 @@ Console::enter()
void
Console::scroll(int numLines)
{
offset += numLines;
if (offset > 0) offset = 0;
m_offset += numLines;
if (m_offset > 0) m_offset = 0;
}

void
Console::show_history(int offset_)
{
while ((offset_ > 0) && (history_position != history.end())) {
history_position++;
while ((offset_ > 0) && (m_history_position != m_history.end())) {
m_history_position++;
offset_--;
}
while ((offset_ < 0) && (history_position != history.begin())) {
history_position--;
while ((offset_ < 0) && (m_history_position != m_history.begin())) {
m_history_position--;
offset_++;
}
if (history_position == history.end()) {
if (m_history_position == m_history.end()) {
inputBuffer = "";
inputBufferPosition = 0;
} else {
inputBuffer = *history_position;
inputBuffer = *m_history_position;
inputBufferPosition = inputBuffer.length();
}
}
Expand Down Expand Up @@ -296,19 +286,19 @@ Console::autocomplete()
ready_vm();

// append all keys of the current root table to list
sq_pushroottable(vm); // push root table
sq_pushroottable(m_vm); // push root table
while(true) {
// check all keys (and their children) for matches
sq_insert_commands(cmds, vm, "", prefix);
sq_insert_commands(cmds, m_vm, "", prefix);

// cycle through parent(delegate) table
SQInteger oldtop = sq_gettop(vm);
if(SQ_FAILED(sq_getdelegate(vm, -1)) || oldtop == sq_gettop(vm)) {
SQInteger oldtop = sq_gettop(m_vm);
if(SQ_FAILED(sq_getdelegate(m_vm, -1)) || oldtop == sq_gettop(m_vm)) {
break;
}
sq_remove(vm, -2); // remove old table
sq_remove(m_vm, -2); // remove old table
}
sq_pop(vm, 1); // remove table
sq_pop(m_vm, 1); // remove table

// depending on number of hits, show matches or autocomplete
if (cmds.empty()) addLines("No known command starts with \""+prefix+"\"");
Expand Down Expand Up @@ -353,24 +343,24 @@ Console::addLine(std::string s)
std::string overflow;
unsigned int line_count = 0;
do {
lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
m_lines.push_front(Font::wrap_to_chars(s, 99, &overflow));
line_count++;
s = overflow;
} while (s.length() > 0);

// trim scrollback buffer
while (lines.size() >= 1000)
lines.pop_back();
while (m_lines.size() >= 1000)
m_lines.pop_back();

// increase console height if necessary
if ((stayOpen > 0) && (height < 64)) {
if(height < 4)
height = 4;
height += fontheight * line_count;
if ((m_stayOpen > 0) && (m_height < 64)) {
if(m_height < 4)
m_height = 4;
m_height += m_font->get_height() * line_count;
}

// reset console to full opacity
alpha = 1.0;
m_alpha = 1.0;
}

void
Expand All @@ -380,8 +370,8 @@ Console::parse(std::string s)
if (s.length() == 0) return;

// add line to history
history.push_back(s);
history_position = history.end();
m_history.push_back(s);
m_history_position = m_history.end();

// split line into list of args
std::vector<std::string> args;
Expand Down Expand Up @@ -419,7 +409,7 @@ Console::consoleCommand(std::string /*command*/, std::vector<std::string> /*argu
bool
Console::hasFocus()
{
return focused;
return m_focused;
}

void
Expand All @@ -428,25 +418,25 @@ Console::show()
if(!g_config->console_enabled)
return;

focused = true;
height = 256;
alpha = 1.0;
m_focused = true;
m_height = 256;
m_alpha = 1.0;
// SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); // Useless in SDL2 : if you want to disable repeat, then you need to check if the key was repeated and ignore it.
}

void
Console::open()
{
if(stayOpen < 2)
stayOpen += 1.5;
if(m_stayOpen < 2)
m_stayOpen += 1.5;
}

void
Console::hide()
{
focused = false;
height = 0;
stayOpen = 0;
m_focused = false;
m_height = 0;
m_stayOpen = 0;

// clear input buffer
inputBuffer = "";
Expand All @@ -468,56 +458,67 @@ Console::toggle()
void
Console::update(float elapsed_time)
{
if(stayOpen > 0) {
stayOpen -= elapsed_time;
if(stayOpen < 0)
stayOpen = 0;
} else if(!focused && height > 0) {
alpha -= elapsed_time * FADE_SPEED;
if(alpha < 0) {
alpha = 0;
height = 0;
if(m_stayOpen > 0) {
m_stayOpen -= elapsed_time;
if(m_stayOpen < 0)
m_stayOpen = 0;
} else if(!m_focused && m_height > 0) {
m_alpha -= elapsed_time * FADE_SPEED;
if(m_alpha < 0) {
m_alpha = 0;
m_height = 0;
}
}
}

void
Console::draw(DrawingContext& context)
{
if (height == 0)
if (m_height == 0)
return;

int layer = LAYER_GUI + 1;

context.push_transform();
context.set_alpha(alpha);
context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 - background->get_width() + backgroundOffset, height - background->get_height()), layer);
context.draw_surface(background2, Vector(SCREEN_WIDTH/2 - background->get_width()/2 + backgroundOffset, height - background->get_height()), layer);
for (int x = (SCREEN_WIDTH/2 - background->get_width()/2 - (static_cast<int>(ceilf((float)SCREEN_WIDTH / (float)background->get_width()) - 1) * background->get_width())); x < SCREEN_WIDTH; x+=background->get_width()) {
context.draw_surface(background, Vector(x, height - background->get_height()), layer);
context.set_alpha(m_alpha);
context.draw_surface(m_background2,
Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 - m_background->get_width() + m_backgroundOffset,
m_height - m_background->get_height()),
layer);
context.draw_surface(m_background2,
Vector(SCREEN_WIDTH/2 - m_background->get_width()/2 + m_backgroundOffset,
m_height - m_background->get_height()),
layer);
for (int x = (SCREEN_WIDTH/2 - m_background->get_width()/2
- (static_cast<int>(ceilf((float)SCREEN_WIDTH /
(float)m_background->get_width()) - 1) * m_background->get_width()));
x < SCREEN_WIDTH;
x += m_background->get_width())
{
context.draw_surface(m_background, Vector(x, m_height - m_background->get_height()), layer);
}
backgroundOffset+=10;
if (backgroundOffset > (int)background->get_width()) backgroundOffset -= (int)background->get_width();
m_backgroundOffset+=10;
if (m_backgroundOffset > (int)m_background->get_width()) m_backgroundOffset -= (int)m_background->get_width();

int lineNo = 0;

if (focused) {
if (m_focused) {
lineNo++;
float py = height-4-1 * font->get_height();
context.draw_text(font, "> "+inputBuffer, Vector(4, py), ALIGN_LEFT, layer);
float py = m_height-4-1 * m_font->get_height();
context.draw_text(m_font, "> "+inputBuffer, Vector(4, py), ALIGN_LEFT, layer);
if (SDL_GetTicks() % 1000 < 750) {
int cursor_px = 2 + inputBufferPosition;
context.draw_text(font, "_", Vector(4 + (cursor_px * font->get_text_width("X")), py), ALIGN_LEFT, layer);
context.draw_text(m_font, "_", Vector(4 + (cursor_px * m_font->get_text_width("X")), py), ALIGN_LEFT, layer);
}
}

int skipLines = -offset;
for (std::list<std::string>::iterator i = lines.begin(); i != lines.end(); i++) {
int skipLines = -m_offset;
for (std::list<std::string>::iterator i = m_lines.begin(); i != m_lines.end(); i++) {
if (skipLines-- > 0) continue;
lineNo++;
float py = height - 4 - lineNo*font->get_height();
if (py < -font->get_height()) break;
context.draw_text(font, *i, Vector(4, py), ALIGN_LEFT, layer);
float py = m_height - 4 - lineNo * m_font->get_height();
if (py < -m_font->get_height()) break;
context.draw_text(m_font, *i, Vector(4, py), ALIGN_LEFT, layer);
}
context.pop_transform();
}
Expand Down

0 comments on commit 289cdcc

Please sign in to comment.