some the_game() cleanup

This commit is contained in:
darkrose 2017-09-06 18:09:31 +10:00
parent d7cb000abd
commit 1a8840fd92
3 changed files with 108 additions and 166 deletions

View File

@ -91,6 +91,11 @@ struct ChatLine
text(a_text) text(a_text)
{ {
} }
ChatLine(const std::wstring &a_text, float a_age):
age(a_age),
text(a_text)
{
}
float age; float age;
std::wstring text; std::wstring text;
}; };
@ -656,6 +661,16 @@ void update_profiler_gui(gui::IGUIStaticText *guitext_profiler,
} }
} }
void chatline_add(ref_t **chat_lines, std::wstring txt, float time)
{
ref_t *ref = (ref_t*)malloc(sizeof(ref_t));
if (!ref)
return;
ref->ref = new ChatLine(txt,time);
*chat_lines = (ref_t*)list_push(chat_lines,ref);
}
void the_game( void the_game(
bool &kill, bool &kill,
InputHandler *input, InputHandler *input,
@ -672,16 +687,11 @@ void the_game(
u32 text_height = font->getDimension(L"Random test string").Height; u32 text_height = font->getDimension(L"Random test string").Height;
v2u32 screensize(0,0); v2u32 screensize(0,0);
v2u32 last_screensize(0,0);
screensize = driver->getScreenSize(); screensize = driver->getScreenSize();
const s32 hotbar_itemcount = 8;
s32 hotbar_imagesize = 48;
/* /*
Draw "Loading" screen Draw "Loading" screen
*/ */
//draw_load_screen(L"Loading...", driver, font);
drawLoadingScreen(device,narrow_to_wide(gettext("Loading..."))); drawLoadingScreen(device,narrow_to_wide(gettext("Loading...")));
/* /*
@ -704,7 +714,6 @@ void the_game(
Create client Create client
*/ */
//draw_load_screen(L"Creating client...", driver, font);
drawLoadingScreen(device,narrow_to_wide(gettext("Creating client..."))); drawLoadingScreen(device,narrow_to_wide(gettext("Creating client...")));
infostream<<"Creating client"<<std::endl; infostream<<"Creating client"<<std::endl;
MapDrawControl draw_control; MapDrawControl draw_control;
@ -736,30 +745,21 @@ void the_game(
/* /*
Attempt to connect to the server Attempt to connect to the server
*/ */
infostream<<"Connecting to server at ";
connect_address.print(&infostream);
infostream<<std::endl;
client.connect(connect_address); client.connect(connect_address);
bool could_connect = false; bool could_connect = false;
try{ try{
float time_counter = 0.0; float time_counter = 0.0;
for(;;) for (;;) {
{ if (client.connectedAndInitialized()) {
if(client.connectedAndInitialized())
{
could_connect = true; could_connect = true;
break; break;
} }
if(client.accessDenied()) if (client.accessDenied()) {
{
break; break;
} }
// Wait for 10 seconds // Wait for 10 seconds
if(time_counter >= 10.0) if (time_counter >= 10.0) {
{
break; break;
} }
@ -864,23 +864,13 @@ void the_game(
core::rect<s32>(0,0,500,text_height+5) + v2s32(100,200), core::rect<s32>(0,0,500,text_height+5) + v2s32(100,200),
false, false); false, false);
// Status text (displays info when showing and hiding GUI stuff, etc.)
gui::IGUIStaticText *guitext_status = guienv->addStaticText(
L"<Status>",
core::rect<s32>(0,0,0,0),
false, false);
guitext_status->setVisible(false);
std::wstring statustext;
float statustext_time = 0;
// Chat text // Chat text
gui::IGUIStaticText *guitext_chat = guienv->addStaticText( gui::IGUIStaticText *guitext_chat = guienv->addStaticText(
L"", L"",
core::rect<s32>(0,0,0,0), core::rect<s32>(0,0,0,0),
//false, false); // Disable word wrap as of now //false, false); // Disable word wrap as of now
false, true); false, true);
core::list<ChatLine> chat_lines; ref_t *chat_lines = NULL;
// Profiler text (size is updated when text is updated) // Profiler text (size is updated when text is updated)
gui::IGUIStaticText *guitext_profiler = guienv->addStaticText( gui::IGUIStaticText *guitext_profiler = guienv->addStaticText(
@ -985,19 +975,9 @@ void the_game(
/* /*
Random calculations Random calculations
*/ */
last_screensize = screensize;
screensize = driver->getScreenSize(); screensize = driver->getScreenSize();
v2s32 displaycenter(screensize.X/2,screensize.Y/2); v2s32 displaycenter(screensize.X/2,screensize.Y/2);
// Resize hotbar
if (screensize.Y <= 800) {
hotbar_imagesize = 32;
}else if (screensize.Y <= 1280) {
hotbar_imagesize = 48;
}else{
hotbar_imagesize = 64;
}
// Hilight boxes collected during the loop and displayed // Hilight boxes collected during the loop and displayed
core::list< core::aabbox3d<f32> > hilightboxes; core::list< core::aabbox3d<f32> > hilightboxes;
@ -1191,12 +1171,10 @@ void the_game(
}else if(input->wasKeyDown(getKeySetting(VLKC_FREEMOVE))) { }else if(input->wasKeyDown(getKeySetting(VLKC_FREEMOVE))) {
if (free_move) { if (free_move) {
free_move = false; free_move = false;
statustext = narrow_to_wide(gettext("free_move disabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("free_move disabled")),-103.00);
statustext_time = 0;
}else{ }else{
free_move = true; free_move = true;
statustext = narrow_to_wide(gettext("free_move enabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("free_move enabled")),-103.00);
statustext_time = 0;
} }
}else if(input->wasKeyDown(getKeySetting(VLKC_SCREENSHOT))) { }else if(input->wasKeyDown(getKeySetting(VLKC_SCREENSHOT))) {
irr::video::IImage* const image = driver->createScreenShot(); irr::video::IImage* const image = driver->createScreenShot();
@ -1211,8 +1189,7 @@ void the_game(
char buff[512]; char buff[512];
snprintf(buff, 512, gettext("Saved screenshot to '%s'"), path); snprintf(buff, 512, gettext("Saved screenshot to '%s'"), path);
infostream << "Saved screenshot to '" << fn << "'" << std::endl; infostream << "Saved screenshot to '" << fn << "'" << std::endl;
statustext = narrow_to_wide(buff); chatline_add(&chat_lines,narrow_to_wide(buff),-103.00);
statustext_time = 0;
}else{ }else{
infostream << "Failed to save screenshot '" << fn << "'"<<std::endl; infostream << "Failed to save screenshot '" << fn << "'"<<std::endl;
} }
@ -1223,35 +1200,31 @@ void the_game(
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_HUD))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_HUD))) {
show_hud = !show_hud; show_hud = !show_hud;
if (show_hud) { if (show_hud) {
statustext = narrow_to_wide(gettext("HUD shown")); chatline_add(&chat_lines,narrow_to_wide(gettext("HUD shown")),-103.00);
}else{ }else{
statustext = narrow_to_wide(gettext("HUD hidden")); chatline_add(&chat_lines,narrow_to_wide(gettext("HUD hidden")),-103.00);
} }
statustext_time = 0;
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_CHAT))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_CHAT))) {
show_chat = !show_chat; show_chat = !show_chat;
if (show_chat) { if (show_chat) {
statustext = narrow_to_wide(gettext("Chat shown")); chatline_add(&chat_lines,narrow_to_wide(gettext("Chat shown")),-103.00);
}else{ }else{
statustext = narrow_to_wide(gettext("Chat hidden")); chatline_add(&chat_lines,narrow_to_wide(gettext("Chat hidden")),-103.00);
} }
statustext_time = 0;
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_FOG))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_FOG))) {
force_fog_off = !force_fog_off; force_fog_off = !force_fog_off;
if (force_fog_off) { if (force_fog_off) {
statustext = narrow_to_wide(gettext("Fog disabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("Fog disabled")),-103.00);
}else{ }else{
statustext = narrow_to_wide(gettext("Fog enabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("Fog enabled")),-103.00);
} }
statustext_time = 0;
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_CAMERA))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_CAMERA))) {
disable_camera_update = !disable_camera_update; disable_camera_update = !disable_camera_update;
if (disable_camera_update) { if (disable_camera_update) {
statustext = narrow_to_wide(gettext("Camera update disabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("Camera update disabled")),-103.00);
}else{ }else{
statustext = narrow_to_wide(gettext("Camera update enabled")); chatline_add(&chat_lines,narrow_to_wide(gettext("Camera update enabled")),-103.00);
} }
statustext_time = 0;
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_DEBUG))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_DEBUG))) {
// Initial / 3x toggle: Chat only // Initial / 3x toggle: Chat only
// 1x toggle: Debug text with chat // 1x toggle: Debug text with chat
@ -1259,17 +1232,14 @@ void the_game(
if (!show_debug) { if (!show_debug) {
show_debug = true; show_debug = true;
show_debug_frametime = false; show_debug_frametime = false;
statustext = narrow_to_wide(gettext("Debug info shown")); chatline_add(&chat_lines,narrow_to_wide(gettext("Debug info shown")),-103.00);
statustext_time = 0;
}else if (show_debug_frametime) { }else if (show_debug_frametime) {
show_debug = false; show_debug = false;
show_debug_frametime = false; show_debug_frametime = false;
statustext = narrow_to_wide(gettext("Debug info and frametime graph hidden")); chatline_add(&chat_lines,narrow_to_wide(gettext("Debug info and frametime graph hidden")),-103.00);
statustext_time = 0;
}else{ }else{
show_debug_frametime = true; show_debug_frametime = true;
statustext = narrow_to_wide(gettext("Frametime graph shown")); chatline_add(&chat_lines,narrow_to_wide(gettext("Frametime graph shown")),-103.00);
statustext_time = 0;
} }
}else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_PROFILER))) { }else if (input->wasKeyDown(getKeySetting(VLKC_TOGGLE_PROFILER))) {
show_profiler = (show_profiler + 1) % (show_profiler_max + 1); show_profiler = (show_profiler + 1) % (show_profiler_max + 1);
@ -1281,11 +1251,9 @@ void the_game(
if (show_profiler != 0) { if (show_profiler != 0) {
char buff[512]; char buff[512];
snprintf(buff,512,gettext("Profiler shown (page %d of %d)"),show_profiler,show_profiler_max); snprintf(buff,512,gettext("Profiler shown (page %d of %d)"),show_profiler,show_profiler_max);
statustext = narrow_to_wide(buff); chatline_add(&chat_lines,narrow_to_wide(buff),-103.00);
statustext_time = 0;
}else{ }else{
statustext = narrow_to_wide(gettext("Profiler hidden")); chatline_add(&chat_lines,narrow_to_wide(gettext("Profiler hidden")),-103.00);
statustext_time = 0;
} }
}else if (input->wasKeyDown(getKeySetting(VLKC_RANGE_PLUS))) { }else if (input->wasKeyDown(getKeySetting(VLKC_RANGE_PLUS))) {
char buff[512]; char buff[512];
@ -1293,8 +1261,7 @@ void the_game(
range += 10; range += 10;
config_set_int("client.graphics.range.min",range); config_set_int("client.graphics.range.min",range);
snprintf(buff,512,gettext("Minimum viewing range changed to %d"),range); snprintf(buff,512,gettext("Minimum viewing range changed to %d"),range);
statustext = narrow_to_wide(buff); chatline_add(&chat_lines,narrow_to_wide(buff),-103.00);
statustext_time = 0;
}else if (input->wasKeyDown(getKeySetting(VLKC_RANGE_MINUS))) { }else if (input->wasKeyDown(getKeySetting(VLKC_RANGE_MINUS))) {
char buff[512]; char buff[512];
int range = config_get_int("client.graphics.range.min"); int range = config_get_int("client.graphics.range.min");
@ -1303,15 +1270,13 @@ void the_game(
range = 10; range = 10;
config_set_int("client.graphics.range.min",range); config_set_int("client.graphics.range.min",range);
snprintf(buff,512,gettext("Minimum viewing range changed to %d"),range); snprintf(buff,512,gettext("Minimum viewing range changed to %d"),range);
statustext = narrow_to_wide(buff); chatline_add(&chat_lines,narrow_to_wide(buff),-103.00);
statustext_time = 0;
} }
// Item selection with mouse wheel // Item selection with mouse wheel
{ {
s32 wheel = input->getMouseWheel(); s32 wheel = input->getMouseWheel();
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1, u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1,7);
hotbar_itemcount-1);
std::string wield_sound = "wield"; std::string wield_sound = "wield";
@ -1336,7 +1301,7 @@ void the_game(
for (u16 i=0; i<10; i++) { for (u16 i=0; i<10; i++) {
const KeyPress *kp = NumberKey + (i + 1) % 10; const KeyPress *kp = NumberKey + (i + 1) % 10;
if (input->wasKeyDown(*kp)) { if (input->wasKeyDown(*kp)) {
if (i < PLAYER_INVENTORY_SIZE && i < hotbar_itemcount) { if (i < PLAYER_INVENTORY_SIZE && i < 8) {
g_selected_item = i; g_selected_item = i;
infostream<<"Selected item: "<<g_selected_item<<std::endl; infostream<<"Selected item: "<<g_selected_item<<std::endl;
@ -1346,7 +1311,7 @@ void the_game(
} }
} }
if (input->wasKeyDown(getKeySetting(VLKC_SELECT_PREV))) { if (input->wasKeyDown(getKeySetting(VLKC_SELECT_PREV))) {
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1, hotbar_itemcount-1); u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1, 7);
if (g_selected_item > 0) { if (g_selected_item > 0) {
g_selected_item--; g_selected_item--;
}else{ }else{
@ -1356,7 +1321,7 @@ void the_game(
client.playSound(wield_sound,false); client.playSound(wield_sound,false);
} }
if (input->wasKeyDown(getKeySetting(VLKC_SELECT_NEXT))) { if (input->wasKeyDown(getKeySetting(VLKC_SELECT_NEXT))) {
u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1, hotbar_itemcount-1); u16 max_item = MYMIN(PLAYER_INVENTORY_SIZE-1, 7);
if (g_selected_item < max_item) { if (g_selected_item < max_item) {
g_selected_item++; g_selected_item++;
}else{ }else{
@ -1371,12 +1336,10 @@ void the_game(
draw_control.range_all = !draw_control.range_all; draw_control.range_all = !draw_control.range_all;
if (draw_control.range_all) { if (draw_control.range_all) {
infostream<<"Enabled full viewing range"<<std::endl; infostream<<"Enabled full viewing range"<<std::endl;
statustext = narrow_to_wide(gettext("Enabled full viewing range")); chatline_add(&chat_lines,narrow_to_wide(gettext("Enabled full viewing range")),-103.00);
statustext_time = 0;
}else{ }else{
infostream<<"Disabled full viewing range"<<std::endl; infostream<<"Disabled full viewing range"<<std::endl;
statustext = narrow_to_wide(gettext("Disabled full viewing range")); chatline_add(&chat_lines,narrow_to_wide(gettext("Disabled full viewing range")),-103.00);
statustext_time = 0;
} }
} }
@ -2047,81 +2010,63 @@ void the_game(
guitext_info->setVisible(false); guitext_info->setVisible(false);
} }
{
float statustext_time_max = 3.0;
if (!statustext.empty()) {
statustext_time += dtime;
if (statustext_time >= statustext_time_max) {
statustext = L"";
statustext_time = 0;
}
}
guitext_status->setText(statustext.c_str());
guitext_status->setVisible(!statustext.empty());
if (!statustext.empty()) {
s32 status_y = screensize.Y - 200;
core::rect<s32> rect(
10,
status_y - guitext_status->getTextHeight(),
screensize.X - 10,
status_y
);
guitext_status->setRelativePosition(rect);
// Fade out
video::SColor initial_color(255,0,0,0);
if (guienv->getSkin())
initial_color = guienv->getSkin()->getColor(gui::EGDC_BUTTON_TEXT);
video::SColor final_color = initial_color;
final_color.setAlpha(0);
video::SColor fade_color = initial_color.getInterpolated_quadratic(
initial_color,
final_color,
statustext_time / (float) statustext_time_max
);
guitext_status->setOverrideColor(fade_color);
guitext_status->enableOverrideColor(true);
}
}
/* /*
Get chat messages from client Get chat messages from client
*/ */
{ {
// Get new messages
std::wstring message; std::wstring message;
/* get new messages */
while (client.getChatMessage(message)) { while (client.getChatMessage(message)) {
chat_lines.push_back(ChatLine(message)); chatline_add(&chat_lines,message,0.0);
} }
// Append them to form the whole static text and throw }
// it to the gui element if (chat_lines) {
ref_t *ref;
ref_t *refn;
ChatLine *line;
std::wstring whole; std::wstring whole;
// This will correspond to the line number counted from s16 line_number = 0;
// top to bottom, from size-1 to 0
s16 line_number = chat_lines.size();
// Count of messages to be removed from the top
u16 to_be_removed_count = 0;
for (core::list<ChatLine>::Iterator i = chat_lines.begin(); i != chat_lines.end(); i++) {
// After this, line number is valid for this loop
line_number--;
// Increment age
(*i).age += dtime;
/*
This results in a maximum age of 60*6 to the
lowermost line and a maximum of 6 lines
*/
float allowed_age = (6-line_number) * 60.0;
if ((*i).age > allowed_age) { /* first, remove old status messages */
to_be_removed_count++; ref = chat_lines;
continue; while (ref) {
line = (ChatLine*)ref->ref;
if (line->age < -50) {
line->age += dtime;
if (line->age > -100.0) {
refn = ref;
ref = ref->next;
chat_lines = (ref_t*)list_remove(&chat_lines,refn);
delete line;
free(refn);
continue;
}
}else{
line_number++;
} }
whole += (*i).text + L'\n'; ref = ref->next;
} }
for (u16 i=0; i<to_be_removed_count; i++) {
core::list<ChatLine>::Iterator it = chat_lines.begin(); /* second, remove old and excess chat messages */
chat_lines.erase(it); ref = chat_lines;
while (ref) {
line = (ChatLine*)ref->ref;
if (line->age > -50) {
line_number--;
line->age += dtime;
float allowed_age = (6-line_number) * 60.0;
if (line->age > allowed_age) {
refn = ref;
ref = ref->next;
chat_lines = (ref_t*)list_remove(&chat_lines,refn);
delete line;
free(refn);
continue;
}
}
whole += line->text + L'\n';
ref = ref->next;
} }
guitext_chat->setText(whole.c_str()); guitext_chat->setText(whole.c_str());
@ -2138,8 +2083,9 @@ void the_game(
guitext_chat->setRelativePosition(rect); guitext_chat->setRelativePosition(rect);
// Don't show chat if empty or profiler or debug is enabled // Don't show chat if empty or profiler or debug is enabled
guitext_chat->setVisible(chat_lines.size() != 0 guitext_chat->setVisible(chat_lines != NULL && show_chat && show_profiler == 0);
&& show_chat && show_profiler == 0); }else{
guitext_chat->setVisible(false);
} }
/* /*
@ -2293,8 +2239,6 @@ void the_game(
driver, driver,
font, font,
v2s32(screensize.X/2,screensize.Y), v2s32(screensize.X/2,screensize.Y),
hotbar_imagesize,
hotbar_itemcount,
&local_inventory, &local_inventory,
client.getHP()/5, client.getHP()/5,
client.getAir()/5, client.getAir()/5,
@ -2334,8 +2278,6 @@ void the_game(
driver, driver,
font, font,
v2s32(screensize.X,screensize.Y), v2s32(screensize.X,screensize.Y),
hotbar_imagesize,
hotbar_itemcount,
show_index, show_index,
&local_inventory, &local_inventory,
client.getServerDamage(), client.getServerDamage(),

View File

@ -261,8 +261,6 @@ void hud_draw_old(
video::IVideoDriver *driver, video::IVideoDriver *driver,
gui::IGUIFont *font, gui::IGUIFont *font,
v2s32 centrelowerpos, v2s32 centrelowerpos,
s32 imgsize,
s32 itemcount,
Inventory *inventory, Inventory *inventory,
s32 halfheartcount, s32 halfheartcount,
s32 halfbubblecount, s32 halfbubblecount,
@ -275,20 +273,28 @@ void hud_draw_old(
return; return;
} }
s32 padding = imgsize/12; s32 imagesize = 64;
s32 width = itemcount*(imgsize+padding*2);
if (centrelowerpos.Y <= 400) {
imagesize = 32;
}else if (centrelowerpos.Y <= 640) {
imagesize = 48;
}
s32 padding = imagesize/12;
s32 width = 8*(imagesize+padding*2);
// Position of upper left corner of bar // Position of upper left corner of bar
v2s32 pos = centrelowerpos - v2s32(width/2, imgsize+padding*2); v2s32 pos = centrelowerpos - v2s32(width/2, imagesize+padding*2);
core::rect<s32> imgrect(0,0,imgsize,imgsize); core::rect<s32> imgrect(0,0,imagesize,imagesize);
std::wstring selected = L""; std::wstring selected = L"";
for (s32 i=0; i<itemcount; i++) { for (s32 i=0; i<8; i++) {
InventoryItem *item = mainlist->getItem(i); InventoryItem *item = mainlist->getItem(i);
core::rect<s32> rect = imgrect + pos + v2s32(padding+i*(imgsize+padding*2), padding); core::rect<s32> rect = imgrect + pos + v2s32(padding+i*(imagesize+padding*2), padding);
if (g_selected_item == i) { if (g_selected_item == i) {
video::SColor c_outside(255,255,0,0); video::SColor c_outside(255,255,0,0);
@ -373,8 +379,6 @@ void hud_draw(
video::IVideoDriver *driver, video::IVideoDriver *driver,
gui::IGUIFont *font, gui::IGUIFont *font,
v2s32 screensize, v2s32 screensize,
s32 imgsize,
s32 itemcount,
bool show_index, bool show_index,
Inventory *inventory, Inventory *inventory,
bool have_health, bool have_health,
@ -527,7 +531,7 @@ void hud_draw(
core::rect<s32> base_rect(screensize.X-104,screensize.Y-104,screensize.X-80,screensize.Y-80); core::rect<s32> base_rect(screensize.X-104,screensize.Y-104,screensize.X-80,screensize.Y-80);
for (s32 i=g_selected_item+1, p=3; ; i++,p--) { for (s32 i=g_selected_item+1, p=3; ; i++,p--) {
if (i >= itemcount) if (i >= 8)
i = 0; i = 0;
if (i == g_selected_item) if (i == g_selected_item)
break; break;

View File

@ -55,8 +55,6 @@ void hud_draw_old(
video::IVideoDriver *driver, video::IVideoDriver *driver,
gui::IGUIFont *font, gui::IGUIFont *font,
v2s32 centrelowerpos, v2s32 centrelowerpos,
s32 imgsize,
s32 itemcount,
Inventory *inventory, Inventory *inventory,
s32 halfheartcount, s32 halfheartcount,
s32 halfbubblecount, s32 halfbubblecount,
@ -66,8 +64,6 @@ void hud_draw(
video::IVideoDriver *driver, video::IVideoDriver *driver,
gui::IGUIFont *font, gui::IGUIFont *font,
v2s32 screensize, v2s32 screensize,
s32 imgsize,
s32 itemcount,
bool show_index, bool show_index,
Inventory *inventory, Inventory *inventory,
bool have_health, bool have_health,