Skip to content
Permalink
Browse files

Modernize client code (#6250)

* Various code style fixes
* Use range based for loops
* Use empty instead of empty objects
* Use C++11 default keyword for trivial constructors and destructors
* Drop some useless casts
* Use emplace_back instead of push_back to improve performance of some vectors push
  • Loading branch information...
nerzhul committed Aug 15, 2017
1 parent 64c7a68 commit 9dd0f952e09471f15f59a98c1ae650d99499cd77
@@ -132,8 +132,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
g_menuclouds->setHeight(100.0f);
g_menuclouds->update(v3f(0, 0, 0), video::SColor(255, 200, 200, 255));
scene::ICameraSceneNode* camera;
camera = g_menucloudsmgr->addCameraSceneNode(0,
v3f(0, 0, 0), v3f(0, 60, 100));
camera = g_menucloudsmgr->addCameraSceneNode(NULL, v3f(0, 0, 0), v3f(0, 60, 100));
camera->setFarValue(10000);

/*
@@ -192,13 +191,13 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
if (!game_has_run) {
if (skip_main_menu)
break;
else
continue;

continue;
}

// Break out of menu-game loop to shut down cleanly
if (!RenderingEngine::get_raw_device()->run() || *kill) {
if (g_settings_path != "")
if (!g_settings_path.empty())
g_settings->updateConfigFile(g_settings_path.c_str());
break;
}
@@ -285,7 +284,7 @@ void ClientLauncher::init_args(GameParams &game_params, const Settings &cmd_args
* supplied on the command line
*/
address = g_settings->get("address");
if (game_params.world_path != "" && !skip_main_menu)
if (!game_params.world_path.empty() && !skip_main_menu)
address = "";
else if (cmd_args.exists("address"))
address = cmd_args.get("address");
@@ -355,11 +354,11 @@ bool ClientLauncher::launch_game(std::string &error_message,
menudata.password = cmd_args.get("password");

// If a world was commanded, append and select it
if (game_params.world_path != "") {
if (!game_params.world_path.empty()) {
worldspec.gameid = getWorldGameId(game_params.world_path, true);
worldspec.name = _("[--world parameter]");

if (worldspec.gameid == "") { // Create new
if (worldspec.gameid.empty()) { // Create new
worldspec.gameid = g_settings->get("default_game");
worldspec.name += " [new]";
}
@@ -400,7 +399,7 @@ bool ClientLauncher::launch_game(std::string &error_message,
return false;
}

if (menudata.name == "" && !simple_singleplayer_mode) {
if (menudata.name.empty() && !simple_singleplayer_mode) {
error_message = gettext("Please choose a name!");
errorstream << error_message << std::endl;
return false;
@@ -416,14 +415,14 @@ bool ClientLauncher::launch_game(std::string &error_message,

// If using simple singleplayer mode, override
if (simple_singleplayer_mode) {
assert(skip_main_menu == false);
assert(!skip_main_menu);
current_playername = "singleplayer";
current_password = "";
current_address = "";
current_port = myrand_range(49152, 65535);
} else {
g_settings->set("name", playername);
if (address != "") {
if (!address.empty()) {
ServerListSpec server;
server["name"] = menudata.servername;
server["address"] = menudata.address;
@@ -436,8 +435,8 @@ bool ClientLauncher::launch_game(std::string &error_message,
infostream << "Selected world: " << worldspec.name
<< " [" << worldspec.path << "]" << std::endl;

if (current_address == "") { // If local game
if (worldspec.path == "") {
if (current_address.empty()) { // If local game
if (worldspec.path.empty()) {
error_message = gettext("No world selected and no address "
"provided. Nothing to do.");
errorstream << error_message << std::endl;
@@ -488,7 +487,7 @@ void ClientLauncher::main_menu(MainMenuData *menudata)
video::IVideoDriver *driver = RenderingEngine::get_video_driver();

infostream << "Waiting for other menus" << std::endl;
while (RenderingEngine::get_raw_device()->run() && *kill == false) {
while (RenderingEngine::get_raw_device()->run() && !*kill) {
if (!isMenuActive())
break;
driver->beginScene(true, true, video::SColor(255, 128, 128, 128));
@@ -29,7 +29,7 @@ class RenderingEngine;
class ClientLauncher
{
public:
ClientLauncher() {}
ClientLauncher() = default;

~ClientLauncher();

@@ -102,8 +102,7 @@ bool MyEventReceiver::OnEvent(const SEvent &event)
};
assert(event.LogEvent.Level < ARRLEN(irr_loglev_conv));
g_logger.log(irr_loglev_conv[event.LogEvent.Level],
std::string("Irrlicht: ") +
(const char *)event.LogEvent.Text);
std::string("Irrlicht: ") + event.LogEvent.Text);
return true;
}
/* always return false in order to continue processing events */
@@ -180,8 +180,9 @@ class MyEventReceiver : public IEventReceiver
class InputHandler
{
public:
InputHandler() {}
virtual ~InputHandler() {}
InputHandler() = default;

virtual ~InputHandler() = default;

virtual bool isKeyDown(const KeyPress &keyCode) = 0;
virtual bool wasKeyDown(const KeyPress &keyCode) = 0;
@@ -243,9 +244,10 @@ class RealInputHandler : public InputHandler
return RenderingEngine::get_raw_device()
->getCursorControl()
->getPosition();
} else {
return m_mousepos;
}

return m_mousepos;

}
virtual void setMousePos(s32 x, s32 y)
{
@@ -287,7 +289,8 @@ class RealInputHandler : public InputHandler
class RandomInputHandler : public InputHandler
{
public:
RandomInputHandler() {}
RandomInputHandler() = default;

virtual bool isKeyDown(const KeyPress &keyCode) { return keydown[keyCode]; }
virtual bool wasKeyDown(const KeyPress &keyCode) { return false; }
virtual v2s32 getMousePos() { return mousepos; }
@@ -41,8 +41,8 @@ bool JoystickAxisCmb::isTriggered(const irr::SEvent::SJoystickEvent &ev) const
}

// spares many characters
#define JLO_B_PB(A, B, C) jlo.button_keys.push_back(JoystickButtonCmb(A, B, C))
#define JLO_A_PB(A, B, C, D) jlo.axis_keys.push_back(JoystickAxisCmb(A, B, C, D))
#define JLO_B_PB(A, B, C) jlo.button_keys.emplace_back(A, B, C)
#define JLO_A_PB(A, B, C, D) jlo.axis_keys.emplace_back(A, B, C, D)

JoystickLayout create_default_layout()
{
@@ -157,8 +157,8 @@ JoystickLayout create_xbox_layout()
JoystickController::JoystickController() :
doubling_dtime(g_settings->getFloat("repeat_joystick_button_time"))
{
for (size_t i = 0; i < KeyType::INTERNAL_ENUM_COUNT; i++) {
m_past_pressed_time[i] = 0;
for (float &i : m_past_pressed_time) {
i = 0;
}
clear();
}
@@ -203,15 +203,15 @@ bool JoystickController::handleEvent(const irr::SEvent::SJoystickEvent &ev)

// First generate a list of keys pressed

for (size_t i = 0; i < m_layout.button_keys.size(); i++) {
if (m_layout.button_keys[i].isTriggered(ev)) {
keys_pressed.set(m_layout.button_keys[i].key);
for (const auto &button_key : m_layout.button_keys) {
if (button_key.isTriggered(ev)) {
keys_pressed.set(button_key.key);
}
}

for (size_t i = 0; i < m_layout.axis_keys.size(); i++) {
if (m_layout.axis_keys[i].isTriggered(ev)) {
keys_pressed.set(m_layout.axis_keys[i].key);
for (const auto &axis_key : m_layout.axis_keys) {
if (axis_key.isTriggered(ev)) {
keys_pressed.set(axis_key.key);
}
}

@@ -52,7 +52,8 @@ struct JoystickCombination {

struct JoystickButtonCmb : public JoystickCombination {

JoystickButtonCmb() {}
JoystickButtonCmb() = default;

JoystickButtonCmb(GameKeyType key, u32 filter_mask, u32 compare_mask) :
filter_mask(filter_mask),
compare_mask(compare_mask)
@@ -68,7 +69,8 @@ struct JoystickButtonCmb : public JoystickCombination {

struct JoystickAxisCmb : public JoystickCombination {

JoystickAxisCmb() {}
JoystickAxisCmb() = default;

JoystickAxisCmb(GameKeyType key, u16 axis_to_compare, int direction, s16 thresh) :
axis_to_compare(axis_to_compare),
direction(direction),
@@ -419,8 +419,7 @@ std::vector<core::vector3d<u32>> RenderingEngine::getSupportedVideoModes()
for (s32 i = 0; i != num_modes; i++) {
core::dimension2d<u32> mode_res = modelist->getVideoModeResolution(i);
u32 mode_depth = (u32)modelist->getVideoModeDepth(i);
mlist.push_back(core::vector3d<u32>(
mode_res.Width, mode_res.Height, mode_depth));
mlist.emplace_back(mode_res.Width, mode_res.Height, mode_depth);
}

nulldevice->drop();
Oops, something went wrong.

0 comments on commit 9dd0f95

Please sign in to comment.
You can’t perform that action at this time.