diff --git a/doc/samples/btphystest.h b/doc/samples/btphystest.h index cc11751c..0a5d0612 100644 --- a/doc/samples/btphystest.h +++ b/doc/samples/btphystest.h @@ -1,12 +1,20 @@ // -// BtPhysTest +// Lol Engine — Bullet physics test // -// Copyright: (c) 2009-2013 Benjamin "Touky" Huet -// (c) 2012 Sam Hocevar +// Copyright © 2009—2013 Benjamin “Touky” Huet +// © 2012—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once +#include + class CatShaderData : public GpuShaderData { friend class CRenderer; @@ -18,14 +26,14 @@ public: void SetupDefaultData(); virtual void SetupShaderDatas(mat4 const &model); //-- - virtual lol::String GetInVertexName() { return lol::String("in_vertex"); } - virtual lol::String GetInNormalName() { return lol::String("in_normal"); } - virtual lol::String GetInColorName() { return lol::String("in_color"); } - virtual lol::String GetInTexCoordName() { return lol::String("in_texcoord"); } - - TextureUniform m_tex_uniform; - float m_sprite_orientation; - float m_sprite_flip; + virtual std::string GetInVertexName() { return "in_vertex"; } + virtual std::string GetInNormalName() { return "in_normal"; } + virtual std::string GetInColorName() { return "in_color"; } + virtual std::string GetInTexCoordName() { return "in_texcoord"; } + + TextureUniform m_tex_uniform; + float m_sprite_orientation; + float m_sprite_flip; }; class BtPhysTest : public WorldEntity @@ -62,7 +70,7 @@ private: KEY_MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[KEY_MOVE_FORWARD] = g_name_key_Up; enum_map[KEY_MOVE_BACK] = g_name_key_Down; diff --git a/doc/samples/meshviewer/meshviewer.cpp b/doc/samples/meshviewer/meshviewer.cpp index fae9fc18..a39b040a 100644 --- a/doc/samples/meshviewer/meshviewer.cpp +++ b/doc/samples/meshviewer/meshviewer.cpp @@ -1,7 +1,7 @@ // // Lol Engine — EasyMesh tutorial // -// Copyright © 2011—2015 Sam Hocevar +// Copyright © 2011—2018 Sam Hocevar // © 2012—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -16,6 +16,7 @@ #endif #include /* for FLT_MAX */ +#include #include #include @@ -112,18 +113,18 @@ void EasyMeshViewerObject::TickDraw(float seconds, Scene &scene) //EasyMeshLoadJob ------------------------------------------------------------- bool EasyMeshLoadJob::DoWork() { - map meshes; + map meshes; if (m_loader.ExecLuaFile(m_path) && EasyMeshLuaLoader::GetRegisteredMeshes(meshes)) { - array keys = meshes.keys(); - for (String key : keys) + array keys = meshes.keys(); + for (auto const &key : keys) m_meshes << new EasyMeshViewerObject(key, meshes[key]->GetMesh()); } return !!m_meshes.count(); } //----------------------------------------------------------------------------- -MeshViewerLoadJob* EasyMeshLoadJob::GetInstance(String const& path) +MeshViewerLoadJob* EasyMeshLoadJob::GetInstance(std::string const& path) { if (Check(path)) return new EasyMeshLoadJob(path); @@ -242,8 +243,8 @@ void MeshViewer::Stop() void MeshViewer::UpdateSceneSetup(bool only_destroy) { //Delete previous setups - array keys = m_ssetups.keys(); - for (String key : keys) + array keys = m_ssetups.keys(); + for (auto const &key : keys) delete m_ssetups[key]; m_ssetups.empty(); if (m_ssetup_file_status) @@ -260,15 +261,15 @@ void MeshViewer::UpdateSceneSetup(bool only_destroy) if (m_ssetup_loader.GetLoadedSetups(m_ssetups)) { m_ssetup_file_status = m_file_check->RegisterFile(m_ssetup_file_name); - array keys = m_ssetups.keys(); - if (!m_ssetup_name.count() || !keys.find(m_ssetup_name)) + array keys = m_ssetups.keys(); + if (!m_ssetup_name.length() || !keys.find(m_ssetup_name)) m_ssetup_name = keys[0]; } } } //----------------------------------------------------------------------------- -MeshViewerLoadJob* MeshViewer::GetLoadJob(String const& path) +MeshViewerLoadJob* MeshViewer::GetLoadJob(std::string const& path) { MeshViewerLoadJob* job = nullptr; if (job = EasyMeshLoadJob::GetInstance(path)) return job; @@ -303,15 +304,15 @@ void MeshViewer::TickGame(float seconds) //static float f; //static int mesh_idx = 0; //static array mesh_names_char; - //static array mesh_names_str; + //static array mesh_names_str; //Draw viewer objects m_menu_mesh_names_char.empty(); m_menu_mesh_names_str.empty(); for (ViewerObject* obj : m_objs) m_menu_mesh_names_str << obj->GetName(); - for (auto str : m_menu_mesh_names_str) - m_menu_mesh_names_char << str.C(); + for (auto const &str : m_menu_mesh_names_str) + m_menu_mesh_names_char << str.c_str(); ImGuiIO& io = ImGui::GetIO(); //CAMERA UI --------------------------------------------------------------- @@ -382,7 +383,7 @@ void MeshViewer::TickDraw(float seconds, Scene &scene) if (m_menu_mesh_idx >= 0 && m_menu_mesh_idx < m_objs.count()) m_objs[m_menu_mesh_idx]->TickDraw(seconds, scene); - m_text->SetText(String("CECI EST UN TEST\n")); + m_text->SetText("CECI EST UN TEST\n"); //Draw gizmos & grid Debug::DrawGizmo(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f); @@ -486,7 +487,7 @@ void MeshViewer::Prepare() m_controller = new Controller("Default"); m_controller->SetInputCount(MAX_KEYS, MAX_AXIS); - if (InputDevice::Get(g_name_mouse.C())) + if (InputDevice::Get(g_name_mouse.c_str())) { m_input_usage |= (1 << IPT_MV_MOUSE); @@ -498,7 +499,7 @@ void MeshViewer::Prepare() m_controller->GetAxis(MSEX_CAM_X).BindMouse("X"); } - if (InputDevice::Get(g_name_keyboard.C())) + if (InputDevice::Get(g_name_keyboard.c_str())) { m_input_usage |= (1 << IPT_MV_KBOARD); @@ -804,7 +805,7 @@ void MeshViewer::Update(float seconds) //-- //Message Service //-- - String mesh(""); + std::string mesh; int u = 1; while (u-- > 0 && MessageService::FetchFirst(MessageBucket::AppIn, mesh)) { @@ -812,7 +813,7 @@ void MeshViewer::Update(float seconds) while (o-- > 0) { SceneSetup* new_ssetup = new SceneSetup(); - if (false) //new_ssetup->Compile(mesh.C()) && new_ssetup->m_lights.count()) + if (false) //new_ssetup->Compile(mesh.c_str()) && new_ssetup->m_lights.count()) { //Store current light datas, in World array light_datas; @@ -866,7 +867,7 @@ void MeshViewer::Update(float seconds) { //Create a new mesh EasyMesh* em = new EasyMesh(); - if (em->Compile(m_ssetup->m_custom_cmd[i].m2.C(), false)) + if (em->Compile(m_ssetup->m_custom_cmd[i].m2.c_str(), false)) { em->BD()->Cmdi() = 0; if (m_mesh_id == m_meshes.count() - 1) @@ -906,8 +907,8 @@ void MeshViewer::Update(float seconds) m_stream_update_time = 0.f; File f; - f.Open(m_file_name.C(), FileAccess::Read); - String cmd = f.ReadString(); + f.Open(m_file_name.c_str(), FileAccess::Read); + std::string cmd = f.ReadString().C(); f.Close(); if (cmd.count() diff --git a/doc/samples/meshviewer/meshviewer.h b/doc/samples/meshviewer/meshviewer.h index 1b1f1a4f..9d7f55cb 100644 --- a/doc/samples/meshviewer/meshviewer.h +++ b/doc/samples/meshviewer/meshviewer.h @@ -2,6 +2,7 @@ // Lol Engine — EasyMesh tutorial // // Copyright © 2009—2015 Benjamin “Touky” Huet +// © 2012—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +13,8 @@ #pragma once +#include + namespace lol { @@ -90,7 +93,7 @@ struct MeshViewerKeyInputBase : public StructSafeEnum MAX = MSE_END, }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Exit] = g_name_key_Escape; @@ -133,14 +136,14 @@ class ViewerObject { public: ViewerObject() { } - ViewerObject(String const& name) : m_name(name) { } + ViewerObject(std::string const& name) : m_name(name) { } virtual ~ViewerObject() { } virtual void TickDraw(float seconds, Scene &scene) { } - String GetName() { return m_name; } + std::string GetName() { return m_name; } protected: - String m_name; + std::string m_name; }; //EasyMeshViewerObject -------------------------------------------------------- @@ -151,7 +154,7 @@ class EasyMeshViewerObject : public ViewerObject public: EasyMeshViewerObject() : ViewerObject() { } - EasyMeshViewerObject(String const& name, EasyMesh const& mesh) + EasyMeshViewerObject(std::string const& name, EasyMesh const& mesh) : ViewerObject(name) { Init(name, mesh); @@ -160,7 +163,7 @@ public: virtual void TickDraw(float seconds, Scene &scene); - void Init(String const& name, EasyMesh const& mesh) + void Init(std::string const& name, EasyMesh const& mesh) { m_name = name; m_mesh = mesh; @@ -178,7 +181,7 @@ class MeshViewerLoadJob : public ThreadJob public: inline MeshViewerLoadJob() : ThreadJob() { } - inline MeshViewerLoadJob(String const& path) + inline MeshViewerLoadJob(std::string const& path) : ThreadJob(ThreadJobType::WORK_TODO), m_path(path) { } virtual ~MeshViewerLoadJob() { } @@ -188,7 +191,7 @@ protected: virtual bool DoWork() { return super::DoWork(); } protected: - String m_path; + std::string m_path; }; //EasyMeshLoadJob ------------------------------------------------------------- @@ -199,15 +202,15 @@ class EasyMeshLoadJob : public MeshViewerLoadJob public: inline EasyMeshLoadJob() : MeshViewerLoadJob() { } - inline EasyMeshLoadJob(String const& path) + inline EasyMeshLoadJob(std::string const& path) : MeshViewerLoadJob(path) { } virtual ~EasyMeshLoadJob() { } - static MeshViewerLoadJob* GetInstance(String const& path); + static MeshViewerLoadJob* GetInstance(std::string const& path); virtual void RetrieveResult(class MeshViewer* app); protected: - static bool Check(String const& path) { return path.contains(".easymesh"); } + static bool Check(std::string const& path) { return path.find(".easymesh") != std::string::npos; } virtual bool DoWork(); protected: @@ -228,7 +231,7 @@ public: void UpdateSceneSetup(bool only_destroy = false); - MeshViewerLoadJob* GetLoadJob(String const& path); + MeshViewerLoadJob* GetLoadJob(std::string const& path); void AddViewerObj(ViewerObject* obj) { m_objs << obj; } virtual void TickGame(float seconds); @@ -260,18 +263,18 @@ private: float m_menu_cam_fov = radians(40.f); vec3 m_menu_cam_pos = vec3(20.f, 45.f, 45.f); int m_menu_mesh_idx = 0; - array m_menu_mesh_names_char; - array m_menu_mesh_names_str; + array m_menu_mesh_names_char; + array m_menu_mesh_names_str; //Scene setup data - SceneSetupLuaLoader m_ssetup_loader; - FileUpdateStatus* m_ssetup_file_status = nullptr; - String m_ssetup_file_name; - String m_ssetup_name; - map m_ssetups; + SceneSetupLuaLoader m_ssetup_loader; + FileUpdateStatus *m_ssetup_file_status = nullptr; + std::string m_ssetup_file_name; + std::string m_ssetup_name; + map m_ssetups; //File data - String m_file_name; + std::string m_file_name; FileUpdateStatus* m_file_status; //Object data @@ -288,47 +291,47 @@ private: DefaultThreadManager* m_file_loader = nullptr; //OLD --------------------------------------------------------------------- - SceneSetup* m_ssetup = nullptr; - array m_light_datas; - short m_input_usage; - mat4 m_mat; - mat4 m_mat_prev; + SceneSetup *m_ssetup = nullptr; + array m_light_datas; + short m_input_usage; + mat4 m_mat; + mat4 m_mat_prev; //Camera Setup - float m_reset_timer; - float m_fov; - float m_fov_mesh; - float m_fov_speed; - float m_zoom; - float m_zoom_mesh; - float m_zoom_speed; - vec2 m_rot; - vec2 m_rot_mesh; - vec2 m_rot_speed; - vec2 m_pos; - vec2 m_pos_mesh; - vec2 m_pos_speed; - vec2 m_hist_scale; - vec2 m_hist_scale_mesh; - vec2 m_hist_scale_speed; - vec2 m_screen_offset; + float m_reset_timer; + float m_fov; + float m_fov_mesh; + float m_fov_speed; + float m_zoom; + float m_zoom_mesh; + float m_zoom_speed; + vec2 m_rot; + vec2 m_rot_mesh; + vec2 m_rot_speed; + vec2 m_pos; + vec2 m_pos_mesh; + vec2 m_pos_speed; + vec2 m_hist_scale; + vec2 m_hist_scale_mesh; + vec2 m_hist_scale_speed; + vec2 m_screen_offset; //Mesh update timer - float m_build_timer; - float m_build_time; + float m_build_timer; + float m_build_time; //Mesh infos - vec2 m_render_max; - int m_mesh_render; - int m_mesh_id; - float m_mesh_id1; + vec2 m_render_max; + int m_mesh_render; + int m_mesh_id; + float m_mesh_id1; array m_meshes; - array m_gizmos; + array m_gizmos; //File data - array m_cmdlist; - float m_stream_update_time; - float m_stream_update_timer; + array m_cmdlist; + float m_stream_update_time; + float m_stream_update_timer; //misc datas Shader * m_texture_shader; diff --git a/doc/samples/meshviewer/scenesetup.cpp b/doc/samples/meshviewer/scenesetup.cpp index 0249642b..23eb36ff 100644 --- a/doc/samples/meshviewer/scenesetup.cpp +++ b/doc/samples/meshviewer/scenesetup.cpp @@ -1,12 +1,14 @@ // -// Lol Engine +// Lol Engine — Mesh viewer // -// Copyright: (c) 2013 Benjamin "Touky" Huet -// 2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2003—2015 Benjamin “Touky” Huet +// © 2013—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #if HAVE_CONFIG_H @@ -16,6 +18,8 @@ #include #include +#include + #include "scenesetup.h" namespace lol @@ -23,7 +27,7 @@ namespace lol //----------------------------------------------------------------------------- //CTor/DTor -SceneSetup::SceneSetup(String const& name) +SceneSetup::SceneSetup(std::string const& name) { m_name = name; m_clear_color = vec4(vec3::zero, 1.f); @@ -147,7 +151,7 @@ void SceneSetup::Set(SceneSetup::Display const& d, DisplayFlag const& f) } //----------------------------------------------------------------------------- -SceneSetupLuaObject::SceneSetupLuaObject(String& name) : LuaObject() +SceneSetupLuaObject::SceneSetupLuaObject(std::string const& name) : LuaObject() { m_setup = new SceneSetup(name); SceneSetupLuaLoader::RegisterSetup(m_setup); @@ -163,7 +167,7 @@ SceneSetupLuaObject* SceneSetupLuaObject::New(lua_State* l, int arg_nb) { UNUSED(arg_nb); auto s = LuaStack::Begin(l); - auto n = s.Get(); + auto n = s.Get(); return new SceneSetupLuaObject(n); } @@ -172,8 +176,8 @@ int SceneSetupLuaObject::AddLight(lua_State* l) { auto s = LuaStack::Begin(l); auto o = s.GetPtr(); - auto t = s.Get(); - o->m_setup->AddLight(FindValue(t.C())); + auto t = s.Get(); + o->m_setup->AddLight(FindValue(t)); return s.End(); } int SceneSetupLuaObject::SetupScene(lua_State* l) @@ -260,7 +264,7 @@ const LuaObjectLibrary* SceneSetupLuaObject::GetLib() } //----------------------------------------------------------------------------- -map SceneSetupLuaLoader::m_setups; +map SceneSetupLuaLoader::m_setups; SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader() { lua_State* l = GetLuaState(); @@ -292,14 +296,14 @@ void SceneSetupLuaLoader::RegisterSetup(SceneSetup* setup) } //----------------------------------------------------------------------------- -bool SceneSetupLuaLoader::GetRegisteredSetups(map& setups) +bool SceneSetupLuaLoader::GetRegisteredSetups(map& setups) { setups = m_setups; return !!m_setups.count(); } //----------------------------------------------------------------------------- -bool SceneSetupLuaLoader::GetLoadedSetups(map& setups) +bool SceneSetupLuaLoader::GetLoadedSetups(map& setups) { return GetRegisteredSetups(setups); } diff --git a/doc/samples/meshviewer/scenesetup.h b/doc/samples/meshviewer/scenesetup.h index 23694a7b..1b99f69b 100644 --- a/doc/samples/meshviewer/scenesetup.h +++ b/doc/samples/meshviewer/scenesetup.h @@ -1,12 +1,14 @@ // -// Lol Engine +// Lol Engine — Mesh viewer // -// Copyright: (c) 2013 Benjamin "Touky" Huet -// (c) 2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2003—2015 Benjamin “Touky” Huet +// © 2013—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -16,6 +18,8 @@ // ---------------- // +#include + namespace lol { @@ -28,7 +32,7 @@ class SceneSetup public: //CTor/DTor SceneSetup() : SceneSetup("default") { } - SceneSetup(String const& name); + SceneSetup(std::string const& name); ~SceneSetup(); static char const *GetName() { return ""; } @@ -57,7 +61,7 @@ public: Max }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Gizmo] = "Gizmo"; enum_map[Light] = "Light"; @@ -84,7 +88,7 @@ protected: Max }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[AddLight] = "AddLight"; enum_map[SetupScene] = "SetupScene"; @@ -95,13 +99,13 @@ protected: public: //-- - String m_name; - Command m_last_cmd; - vec4 m_clear_color; - array m_lights; - array m_custom_cmd; - bool m_show_gizmo; - bool m_show_lights; + std::string m_name; + Command m_last_cmd; + vec4 m_clear_color; + array m_lights; + array m_custom_cmd; + bool m_show_gizmo; + bool m_show_lights; }; //----------------------------------------------------------------------------- @@ -109,7 +113,7 @@ class SceneSetupLuaObject : public LuaObject { public: //------------------------------------------------------------------------- - SceneSetupLuaObject(String& name); + SceneSetupLuaObject(std::string const& name); virtual ~SceneSetupLuaObject(); //------------------------------------------------------------------------- @@ -159,12 +163,12 @@ public: //------------------------------------------------------------------------- protected: static void RegisterSetup(SceneSetup* setup); - static bool GetRegisteredSetups(map& setups); + static bool GetRegisteredSetups(map& setups); public: - bool GetLoadedSetups(map& setups); + bool GetLoadedSetups(map& setups); private: - static map m_setups; + static map m_setups; }; /* diff --git a/doc/tutorial/13_shader_builder.cpp b/doc/tutorial/13_shader_builder.cpp index 59912af5..88da91d1 100644 --- a/doc/tutorial/13_shader_builder.cpp +++ b/doc/tutorial/13_shader_builder.cpp @@ -1,7 +1,8 @@ // // Lol Engine — Shader builder tutorial // -// Copyright © 2012—2015 Sam Hocevar +// Copyright © 2002—2015 Benjamin “Touky” Huet +// © 2012—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -16,12 +17,12 @@ #include #include "loldebug.h" + #include +#include using namespace lol; -#define Line(x) ((x) + "\n") - class ShaderBuilderDemo : public WorldEntity { public: @@ -41,7 +42,7 @@ public: File file; file.Open("13_shader_builder_export.txt", FileAccess::Write); //file.Open("13_shader_builder_export.txt", FileAccess::Read); - String code; + std::string code; ShaderBuilder builder("red_blue_green", "120"); ShaderBlock nothing_vertex("NothingVertex"); @@ -60,9 +61,9 @@ public: << in_color << pass_color; nothing_vertex.AddVar(out_vertex); - nothing_vertex.SetMainCode(String() + - Line(pass_color + " = " + in_color + ";") + - Line(out_vertex + " = vec4(" + in_position + ", 0.f);") + nothing_vertex.SetMainCode( + pass_color.tostring() + " = " + in_color.tostring() + ";\n" + + out_vertex.tostring() + " = vec4(" + in_position.tostring() + ", 0.f);\n" ); ShaderVar ambient = ShaderVar(ShaderVariable::InOut, ShaderVariableType::Vec4, "ambient"); @@ -70,34 +71,34 @@ public: red_pixel.AddVar(pass_color); red_pixel.AddVar(out_pixel); red_pixel.AddVar(ambient); - red_pixel.SetMainCode(String() + - out_pixel + " = " + pass_color + ";\n" + - out_pixel + ".r = 1.0;\n" + + red_pixel.SetMainCode( + out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" + + out_pixel.tostring() + ".r = 1.0;\n" "ambient = vec4(1.0);\n" ); green_pixel.AddVar(pass_color); green_pixel.AddVar(out_pixel); green_pixel.AddVar(ambient); - green_pixel.SetMainCode(String() + - out_pixel + " = " + pass_color + ";\n" + - out_pixel + ".g = 1.0;\n" + + green_pixel.SetMainCode( + out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" + + out_pixel.tostring() + ".g = 1.0;\n" "ambient.r = 0.0;\n" ); blue_pixel.AddVar(pass_color); blue_pixel.AddVar(out_pixel); blue_pixel.AddVar(ambient); - blue_pixel.SetCustomCode(String() + - "void SetAmbient(inout vec4 ambient)\n" + - "{\n" + - " ambient = vec4(1.0, 1.0, 1.0, 1.0);\n" + + blue_pixel.SetCustomCode( + "void SetAmbient(inout vec4 ambient)\n" + "{\n" + " ambient = vec4(1.0, 1.0, 1.0, 1.0);\n" "}\n"); - blue_pixel.SetMainCode(String() + - out_pixel + " = " + pass_color + ";\n" + - out_pixel + ".b = 1.0;\n" + + blue_pixel.SetMainCode( + out_pixel.tostring() + " = " + pass_color.tostring() + ";\n" + + out_pixel.tostring() + ".b = 1.0;\n" "SetAmbient(ambient);\n" + - out_pixel + " *= ambient;\n" + out_pixel.tostring() + " *= ambient;\n" ); builder << ShaderProgram::Vertex @@ -109,7 +110,7 @@ public: builder.Build(code); - file.WriteString(code); + file.WriteString(code.c_str()); //code = file.ReadString(); file.Close(); diff --git a/doc/tutorial/15_lolimgui.cpp b/doc/tutorial/15_lolimgui.cpp index fb7a16cd..08fc24b6 100644 --- a/doc/tutorial/15_lolimgui.cpp +++ b/doc/tutorial/15_lolimgui.cpp @@ -1,7 +1,8 @@ // -// Lol Engine — Shader builder tutorial +// Lol Engine — Imgui tutorial // -// Copyright © 2012—2015 Sam Hocevar +// Copyright © 2002—2015 Benjamin “Touky” Huet +// © 2012—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,8 +16,11 @@ #endif #include -#include "loldebug.h" + #include +#include + +#include "loldebug.h" using namespace lol; @@ -64,7 +68,7 @@ public: ImGui::Text("Scroll: %f", io.MouseWheel); ImGui::Text("Maj: %s", io.KeyShift ? "true" : "false"); ImGui::Text("Ctrl: %s", io.KeyCtrl ? "true" : "false"); - ImGui::Text("Clipboard %s", LolImGui::GetClipboard().C()); + ImGui::Text("Clipboard %s", LolImGui::GetClipboard().c_str()); ImGui::InputText("base input", buf, 512); } ImGui::End(); diff --git a/src/base/string.cpp b/src/base/string.cpp index 8446737f..fe51c7d3 100644 --- a/src/base/string.cpp +++ b/src/base/string.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,7 @@ #include #include +#include #if defined(_WIN32) # define WIN32_LEAN_AND_MEAN @@ -21,6 +22,7 @@ #endif #include +#include namespace lol { @@ -54,6 +56,34 @@ array split(std::string const &s, std::string const &seps) return ret; } +bool starts_with(std::string const &s, std::string const &prefix) +{ + return s.size() >= prefix.size() && + s.compare(0, prefix.size(), prefix) == 0; +} + +bool ends_with(std::string const &s, std::string const &suffix) +{ + return s.size() >= suffix.size() && + s.compare(s.size() - suffix.size(), suffix.size(), suffix) == 0; +} + +std::string tolower(std::string const &s) +{ + std::string ret; + std::transform(s.begin(), s.end(), std::back_inserter(ret), + [](unsigned char c){ return std::tolower(c); }); + return ret; +} + +std::string toupper(std::string const &s) +{ + std::string ret; + std::transform(s.begin(), s.end(), std::back_inserter(ret), + [](unsigned char c){ return std::toupper(c); }); + return ret; +} + std::string format(char const *format, ...) { va_list ap; diff --git a/src/easymesh/easymeshlua.cpp b/src/easymesh/easymeshlua.cpp index 30752f84..6b313c36 100644 --- a/src/easymesh/easymeshlua.cpp +++ b/src/easymesh/easymeshlua.cpp @@ -2,6 +2,7 @@ // Lol Engine — EasyMesh Lua loader // // Copyright © 2009—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,10 +15,12 @@ # include "config.h" #endif -#include - #include #include + +#include +#include + #include "loldebug.h" using namespace lol; @@ -52,23 +55,23 @@ array& EasyMeshLuaLoader::GetInstances() } //----------------------------------------------------------------------------- -map EasyMeshLuaLoader::m_meshes; -void EasyMeshLuaLoader::RegisterMesh(EasyMeshLuaObject* mesh, String const& name) +map EasyMeshLuaLoader::m_meshes; +void EasyMeshLuaLoader::RegisterMesh(EasyMeshLuaObject* mesh, std::string const& name) { m_meshes[name] = mesh; } //----------------------------------------------------------------------------- -bool EasyMeshLuaLoader::GetRegisteredMeshes(map& meshes) +bool EasyMeshLuaLoader::GetRegisteredMeshes(map& meshes) { meshes = m_meshes; - return !!m_meshes.count(); + return m_meshes.count() > 0; } //----------------------------------------------------------------------------- -EasyMeshLuaObject::EasyMeshLuaObject(String const& name) : LuaObject() +EasyMeshLuaObject::EasyMeshLuaObject(std::string const& name) : LuaObject() { - if (!!name.count()) + if (name.length() > 0) EasyMeshLuaLoader::RegisterMesh(this, name); } @@ -83,7 +86,7 @@ EasyMeshLuaObject* EasyMeshLuaObject::New(lua_State* l, int arg_nb) UNUSED(l); UNUSED(arg_nb); LuaStack s = LuaStack::Begin(l); - String str = s.Get(""); + std::string str = s.Get(""); return new EasyMeshLuaObject(str); } diff --git a/src/easymesh/easymeshlua.h b/src/easymesh/easymeshlua.h index 6c786c55..35c051c6 100644 --- a/src/easymesh/easymeshlua.h +++ b/src/easymesh/easymeshlua.h @@ -2,6 +2,7 @@ // Lol Engine — EasyMesh Lua loader // // Copyright © 2009—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +13,8 @@ #pragma once +#include + namespace lol { @@ -21,7 +24,7 @@ class EasyMeshLuaObject : public LuaObject EasyMesh m_instance; public: //------------------------------------------------------------------------- - EasyMeshLuaObject(String const& name); + EasyMeshLuaObject(std::string const& name); virtual ~EasyMeshLuaObject(); EasyMesh& GetMesh() { return m_instance; } @@ -137,12 +140,12 @@ public: //------------------------------------------------------------------------- protected: - static void RegisterMesh(EasyMeshLuaObject* mesh, String const& name); + static void RegisterMesh(EasyMeshLuaObject* mesh, std::string const& name); public: - static bool GetRegisteredMeshes(map& meshes); + static bool GetRegisteredMeshes(map& meshes); private: - static map m_meshes; + static map m_meshes; }; } /* namespace lol */ diff --git a/src/gpu/shader.cpp b/src/gpu/shader.cpp index 6ba4e2f0..d31a6c90 100644 --- a/src/gpu/shader.cpp +++ b/src/gpu/shader.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +12,7 @@ #include +#include #include #include @@ -76,7 +77,7 @@ class ShaderData friend class Shader; private: - String m_name; + std::string m_name; GLuint prog_id, vert_id, frag_id; // Benlitz: using a simple array could be faster since there is never more than a few attribute locations to store @@ -86,7 +87,7 @@ private: /* Shader patcher */ static int GetVersion(); - static String Patch(String const &code, ShaderType type); + static std::string Patch(std::string const &code, ShaderType type); /* Global shader cache */ static Shader *shaders[]; @@ -108,8 +109,8 @@ using namespace pegtl; struct lolfx_parser { public: - String m_section; - map m_programs; + std::string m_section; + map m_programs; private: // title <- '[' (!']')+ ']' .{eol} @@ -155,10 +156,10 @@ private: struct action : nothing {}; public: - lolfx_parser(String const &code) + lolfx_parser(std::string const &code) : m_section("header") { - string_input<> in(code.C(), "shader"); + string_input<> in(code, "shader"); pegtl::parse(in, this); } }; @@ -169,7 +170,7 @@ struct lolfx_parser::action template static void apply(INPUT const &in, lolfx_parser *that) { - that->m_section = in.string().c_str(); + that->m_section = in.string(); } }; @@ -179,7 +180,7 @@ struct lolfx_parser::action template static void apply(INPUT const &in, lolfx_parser *that) { - that->m_programs[that->m_section] = in.string().c_str(); + that->m_programs[that->m_section] = in.string(); } }; @@ -187,18 +188,18 @@ struct lolfx_parser::action * Public Shader class */ -Shader *Shader::Create(String const &name, String const &code) +Shader *Shader::Create(std::string const &name, std::string const &code) { lolfx_parser p(code); ASSERT(p.m_programs.has_key("vert.glsl"), - "no vertex shader in %s", name.C()); + "no vertex shader in %s", name.c_str()); ASSERT(p.m_programs.has_key("frag.glsl"), - "no fragment shader in %s", name.C()); + "no fragment shader in %s", name.c_str()); - String vert = p.m_programs["vert.glsl"]; - String frag = p.m_programs["frag.glsl"]; + std::string vert = p.m_programs["vert.glsl"]; + std::string frag = p.m_programs["frag.glsl"]; uint32_t new_vert_crc = ShaderData::Hash(vert); uint32_t new_frag_crc = ShaderData::Hash(frag); @@ -225,14 +226,14 @@ void Shader::Destroy(Shader *shader) UNUSED(shader); } -Shader::Shader(String const &name, - String const &vert, String const &frag) +Shader::Shader(std::string const &name, + std::string const &vert, std::string const &frag) : data(new ShaderData()) { data->m_name = name; char errbuf[4096]; - String shader_code; + std::string shader_code; GLchar const *gl_code; GLint status; GLsizei len; @@ -242,7 +243,7 @@ Shader::Shader(String const &name, shader_code = ShaderData::Patch(vert, ShaderType::Vertex); data->vert_id = glCreateShader(GL_VERTEX_SHADER); - gl_code = shader_code.C(); + gl_code = shader_code.c_str(); glShaderSource(data->vert_id, 1, &gl_code, nullptr); glCompileShader(data->vert_id); @@ -251,13 +252,13 @@ Shader::Shader(String const &name, if (status != GL_TRUE) { msg::error("failed to compile vertex shader %s: %s\n", - name.C(), errbuf); - msg::error("shader source:\n%s\n", shader_code.C()); + name.c_str(), errbuf); + msg::error("shader source:\n%s\n", shader_code.c_str()); } else if (len > 16) { - msg::debug("compile log for vertex shader %s: %s\n", name.C(), errbuf); - msg::debug("shader source:\n%s\n", shader_code.C()); + msg::debug("compile log for vertex shader %s: %s\n", name.c_str(), errbuf); + msg::debug("shader source:\n%s\n", shader_code.c_str()); } /* Compile fragment shader */ @@ -265,7 +266,7 @@ Shader::Shader(String const &name, shader_code = ShaderData::Patch(frag, ShaderType::Fragment); data->frag_id = glCreateShader(GL_FRAGMENT_SHADER); - gl_code = shader_code.C(); + gl_code = shader_code.c_str(); glShaderSource(data->frag_id, 1, &gl_code, nullptr); glCompileShader(data->frag_id); @@ -274,14 +275,14 @@ Shader::Shader(String const &name, if (status != GL_TRUE) { msg::error("failed to compile fragment shader %s: %s\n", - name.C(), errbuf); - msg::error("shader source:\n%s\n", shader_code.C()); + name.c_str(), errbuf); + msg::error("shader source:\n%s\n", shader_code.c_str()); } else if (len > 16) { msg::debug("compile log for fragment shader %s: %s\n", - name.C(), errbuf); - msg::debug("shader source:\n%s\n", shader_code.C()); + name.c_str(), errbuf); + msg::debug("shader source:\n%s\n", shader_code.c_str()); } /* Create program */ @@ -294,11 +295,11 @@ Shader::Shader(String const &name, glGetProgramiv(data->prog_id, GL_LINK_STATUS, &status); if (status != GL_TRUE) { - msg::error("failed to link program %s: %s\n", name.C(), errbuf); + msg::error("failed to link program %s: %s\n", name.c_str(), errbuf); } else if (len > 16) { - msg::debug("link log for program %s: %s\n", name.C(), errbuf); + msg::debug("link log for program %s: %s\n", name.c_str(), errbuf); } GLint validated; @@ -306,7 +307,7 @@ Shader::Shader(String const &name, glGetProgramiv(data->prog_id, GL_VALIDATE_STATUS, &validated); if (validated != GL_TRUE) { - msg::error("failed to validate program %s\n", name.C()); + msg::error("failed to validate program %s\n", name.c_str()); } GLint num_attribs; @@ -327,16 +328,16 @@ Shader::Shader(String const &name, int attrib_type; glGetActiveAttrib(data->prog_id, i, max_len, &attrib_len, (GLint*)&attrib_size, (GLenum*)&attrib_type, name_buffer); - String attr_name(name_buffer); + std::string attr_name(name_buffer); int index = -1; VertexUsage usage = VertexUsage::MAX; for (int j = 0; j < (int)VertexUsage::MAX; ++j) { - if (attr_name.starts_with(attribute_names[j]) || - attr_name.starts_with(String(attribute_names[j]).to_lower())) + if (starts_with(attr_name, attribute_names[j]) || + starts_with(attr_name, tolower(attribute_names[j]))) { usage = VertexUsage(j); - char* idx_ptr = attr_name.C() + strlen(attribute_names[j]); + char* idx_ptr = &attr_name[0] + strlen(attribute_names[j]); index = strtol(idx_ptr, nullptr, 10); break; } @@ -357,7 +358,7 @@ Shader::Shader(String const &name, if (data->attrib_locations.has_key(flags)) { msg::error("error while parsing attribute semantics in %s\n", - attr_name.C()); + attr_name.c_str()); } #endif data->attrib_locations[flags] = location; @@ -386,7 +387,7 @@ ShaderAttrib Shader::GetAttribLocation(VertexUsage usage, int index) const if (!data->attrib_errors.has_key(ret.m_flags)) { msg::error("attribute %s not found in shader %s\n", - usage.ToString().C(), data->m_name.C()); + usage.tostring().c_str(), data->m_name.c_str()); data->attrib_errors[ret.m_flags] = true; } } @@ -394,9 +395,9 @@ ShaderAttrib Shader::GetAttribLocation(VertexUsage usage, int index) const return ret; } -ShaderUniform Shader::GetUniformLocation(String const& uni) const +ShaderUniform Shader::GetUniformLocation(std::string const& uni) const { - return GetUniformLocation(uni.C()); + return GetUniformLocation(uni.c_str()); } ShaderUniform Shader::GetUniformLocation(char const *uni) const { @@ -572,16 +573,17 @@ int ShaderData::GetVersion() /* * Simple shader source patching for old GLSL versions. */ -String ShaderData::Patch(String const &code, ShaderType type) +std::string ShaderData::Patch(std::string const &code, ShaderType type) { int ver_driver = GetVersion(); - String patched_code = code; + std::string patched_code = code; if (ver_driver >= 130) return patched_code; + /* FIXME: use std::string instead of char * for parsing? */ int ver_shader = 110; - char *parser = strstr(patched_code.C(), "#version"); + char *parser = strstr(&patched_code[0], "#version"); if (parser) ver_shader = atoi(parser + strlen("#version")); @@ -590,7 +592,7 @@ String ShaderData::Patch(String const &code, ShaderType type) { /* FIXME: this isn't elegant but honestly, we don't care, this * whole file is going to die soon. */ - char *p = strstr(patched_code.C(), "#version"); + char *p = strstr(&patched_code[0], "#version"); if (p) { p += 8; @@ -603,10 +605,10 @@ String ShaderData::Patch(String const &code, ShaderType type) if (ver_shader > 120 && ver_driver <= 120) { - char const *end = patched_code.C() + patched_code.count() + 1; + char const *end = patched_code.c_str() + patched_code.length() + 1; /* Find main() */ - parser = strstr(patched_code.C(), "main"); + parser = strstr(&patched_code[0], "main"); if (!parser) return patched_code; parser = strstr(parser, "("); if (!parser) return patched_code; @@ -651,7 +653,7 @@ String ShaderData::Patch(String const &code, ShaderType type) for (char const * const *rep = main_replaces; rep[0]; rep += 2) { - char *match = strstr(patched_code.C(), rep[0]); + char *match = strstr(&patched_code[0], rep[0]); if (match && match < main) { size_t l0 = strlen(rep[0]); @@ -685,18 +687,18 @@ String ShaderData::Patch(String const &code, ShaderType type) { while (true) { - int index = patched_code.index_of(rep[0]); - if (index == INDEX_NONE) + size_t index = patched_code.find(rep[0]); + if (index == std::string::npos) break; size_t l0 = strlen(rep[0]); size_t l1 = strlen(rep[1]); UNUSED(l1); - String left = patched_code.sub(0, index); - String right = patched_code.sub(index + l0, patched_code.count() - (index + l0)); + std::string left = patched_code.substr(0, index); + std::string right = patched_code.substr(index + l0, patched_code.length() - (index + l0)); - patched_code = left + String(rep[1]) + right; + patched_code = left + std::string(rep[1]) + right; } } } @@ -704,112 +706,112 @@ String ShaderData::Patch(String const &code, ShaderType type) return patched_code; } -static const String g_ret = "\n"; -static const String g_eol = ";"; -static const String g_bop = "{"; -static const String g_bcl = "}"; -static const String g_tab = " "; +static const std::string g_ret = "\n"; +static const std::string g_eol = ";"; +static const std::string g_bop = "{"; +static const std::string g_bcl = "}"; +static const std::string g_tab = " "; //---- -String Shader::GetVariablePrefix(const ShaderVariable variable) +std::string Shader::GetVariablePrefix(const ShaderVariable variable) { switch (variable.ToScalar()) { - case ShaderVariable::Attribute: return String("in_"); - case ShaderVariable::Uniform: return String("u_"); - case ShaderVariable::Varying: return String("pass_"); + case ShaderVariable::Attribute: return "in_"; + case ShaderVariable::Uniform: return "u_"; + case ShaderVariable::Varying: return "pass_"; case ShaderVariable::InOut: - default: return String(); + default: return ""; } } //---- -String Shader::GetVariableQualifier(const ShaderVariable variable) +std::string Shader::GetVariableQualifier(const ShaderVariable variable) { switch (variable.ToScalar()) { - case ShaderVariable::Attribute: return String("attribute"); - case ShaderVariable::Uniform: return String("uniform"); - case ShaderVariable::Varying: return String("varying"); + case ShaderVariable::Attribute: return "attribute"; + case ShaderVariable::Uniform: return "uniform"; + case ShaderVariable::Varying: return "varying"; case ShaderVariable::InOut: - default: return String(); + default: return ""; } } //---- -String Shader::GetFunctionQualifier(const ShaderVariable variable, const ShaderProgram program) +std::string Shader::GetFunctionQualifier(const ShaderVariable variable, const ShaderProgram program) { switch (program.ToScalar()) { case ShaderProgram::Geometry: { //TODO : L O L ---------------- - return String(); + return ""; } case ShaderProgram::Vertex: { switch (variable.ToScalar()) { - case ShaderVariable::Attribute: return String("in"); - case ShaderVariable::Uniform: return String("in"); - case ShaderVariable::Varying: return String("inout"); - case ShaderVariable::InOut: return String("inout"); - default: return String(); + case ShaderVariable::Attribute: return "in"; + case ShaderVariable::Uniform: return "in"; + case ShaderVariable::Varying: return "inout"; + case ShaderVariable::InOut: return "inout"; + default: return ""; } - return String(); + return ""; } case ShaderProgram::Pixel: { switch (variable.ToScalar()) { - case ShaderVariable::Attribute: return String("in"); - case ShaderVariable::Uniform: return String("in"); - case ShaderVariable::Varying: return String("in"); - case ShaderVariable::InOut: return String("inout"); - default: return String(); + case ShaderVariable::Attribute: return "in"; + case ShaderVariable::Uniform: return "in"; + case ShaderVariable::Varying: return "in"; + case ShaderVariable::InOut: return "inout"; + default: return ""; } - return String(); + return ""; } default: { - return String(); + return ""; } } } //---- -String Shader::GetProgramQualifier(const ShaderProgram program) +std::string Shader::GetProgramQualifier(const ShaderProgram program) { switch (program.ToScalar()) { - case ShaderProgram::Geometry: return String(); //TODO : L O L --------- - case ShaderProgram::Vertex: return String("[vert.glsl]"); - case ShaderProgram::Pixel: return String("[frag.glsl]"); - default: return String(); + case ShaderProgram::Geometry: return ""; //TODO : L O L --------- + case ShaderProgram::Vertex: return "[vert.glsl]"; + case ShaderProgram::Pixel: return "[frag.glsl]"; + default: return ""; } } //---- -String Shader::GetProgramOutVariable(const ShaderProgram program) +std::string Shader::GetProgramOutVariable(const ShaderProgram program) { switch (program.ToScalar()) { - case ShaderProgram::Geometry: return String(); //TODO : L O L --------- - case ShaderProgram::Vertex: return String("gl_Position"); - case ShaderProgram::Pixel: return String("gl_FragColor"); - default: return String(); + case ShaderProgram::Geometry: return ""; //TODO : L O L --------- + case ShaderProgram::Vertex: return "gl_Position"; + case ShaderProgram::Pixel: return "gl_FragColor"; + default: return ""; } } //---- -String Shader::GetProgramOutVariableLocal(const ShaderProgram program) +std::string Shader::GetProgramOutVariableLocal(const ShaderProgram program) { switch (program.ToScalar()) { - case ShaderProgram::Geometry: return String(); //TODO : L O L --------- - case ShaderProgram::Vertex: return String("out_position"); - case ShaderProgram::Pixel: return String("out_frag_color"); - default: return String(); + case ShaderProgram::Geometry: return ""; //TODO : L O L --------- + case ShaderProgram::Vertex: return "out_position"; + case ShaderProgram::Pixel: return "out_frag_color"; + default: return ""; } } @@ -829,70 +831,70 @@ ShaderVar ShaderVar::GetShaderOut(ShaderProgram program) void ShaderBlock::AddVar(ShaderVar const& var) { ShaderVariable qualifier = var.GetQualifier(); - String type = var.GetType(); - String name = Shader::GetVariablePrefix(qualifier) + var.m_name; + std::string type = var.GetType(); + std::string name = Shader::GetVariablePrefix(qualifier) + var.m_name; ASSERT(!m_parameters[qualifier.ToScalar()].has_key(name)); m_parameters[qualifier.ToScalar()][name] = type; } //---- -void ShaderBlock::AddCallParameters(map const& variables, String& result) +void ShaderBlock::AddCallParameters(map const& variables, std::string& result) { - array keys = variables.keys(); - for (String key : keys) + array keys = variables.keys(); + for (auto const &key : keys) { - if (result.count() > 0) + if (result.length() > 0) result += ", "; result += key; } } //---- -void ShaderBlock::AddDefinitionParameters(const ShaderVariable type, const ShaderProgram program, map& variables, String& result) +void ShaderBlock::AddDefinitionParameters(const ShaderVariable type, const ShaderProgram program, map& variables, std::string& result) { - array keys = variables.keys(); - for (String key : keys) + array keys = variables.keys(); + for (auto const &key : keys) { - if (result.count() > 0) + if (result.length() > 0) result += ", "; result += Shader::GetFunctionQualifier(type, program) + " "; result += variables[key]; - result += String(" "); + result += " "; result += key; } } //---- -void ShaderBlock::Build(const ShaderProgram program, String& call, String& function) +void ShaderBlock::Build(const ShaderProgram program, std::string& call, std::string& function) { - ASSERT(m_name.count()); + ASSERT(m_name.length()); ASSERT(m_parameters[ShaderVariable::InOut].count()); //Build call in main - String call_name = String("Call_") + m_name; + std::string call_name = std::string("Call_") + m_name; call = call_name + "("; - String call_parameters; + std::string call_parameters; for (int i = 0; i < ShaderVariable::MAX; i++) AddCallParameters(/*(ShaderVariable)i, */m_parameters[i], call_parameters); call += call_parameters + ");"; //Build function declaration - function = String("void ") + call_name + "("; - String def_parameters; + function = std::string("void ") + call_name + "("; + std::string def_parameters; for (int i = 0; i < ShaderVariable::MAX; i++) AddDefinitionParameters((ShaderVariable)i, program, m_parameters[i], def_parameters); function += def_parameters + ")" + g_ret + "{" + g_ret + - m_code_main + ((m_code_main.ends_with(g_ret)) ? (String()) : (g_ret)) + + m_code_main + (ends_with(m_code_main, g_ret) ? std::string() : g_ret) + "}"; } //Shader Builder implementation class ----------------------------------------- -ShaderBuilder::ShaderBuilder(String const& name, String const& version) +ShaderBuilder::ShaderBuilder(std::string const& name, std::string const& version) : m_name(name), m_version(version) { - ASSERT(name.count()); - ASSERT(version.count()); + ASSERT(name.length()); + ASSERT(version.length()); } //---- @@ -901,7 +903,7 @@ ShaderBuilder::~ShaderBuilder() } //---- -String const& ShaderBuilder::GetName() +std::string const& ShaderBuilder::GetName() { return m_name; } @@ -930,10 +932,10 @@ ShaderBuilder& ShaderBuilder::operator<<(ShaderBlock const& block) } //---- -String ShaderBuilder::AddSlotOutVariableLocal(const ShaderProgram program) +std::string ShaderBuilder::AddSlotOutVariableLocal(const ShaderProgram program) { ShaderVariable var = ShaderVariable::InOut; - String result = Shader::GetProgramOutVariableLocal(program); + std::string result = Shader::GetProgramOutVariableLocal(program); switch (program.ToScalar()) { case ShaderProgram::Geometry: @@ -960,17 +962,17 @@ String ShaderBuilder::AddSlotOutVariableLocal(const ShaderProgram program) } //---- -void ShaderBuilder::MergeParameters(map& variables, map& merged) +void ShaderBuilder::MergeParameters(map& variables, map& merged) { - array keys = variables.keys(); - for (String key : keys) + array keys = variables.keys(); + for (auto const &key : keys) { bool has_key = merged.has_key(key); //Key exists, check the type to make sure it's the same ASSERT(!has_key || (has_key && merged[key] == variables[key]), "has_key=%d, key=%s merged[key]=%s, variables[key]=%s\n", - (int)has_key, key.C(), merged[key].C(), variables[key].C()); + (int)has_key, key.c_str(), merged[key].c_str(), variables[key].c_str()); //does not exist, had it if (!has_key) @@ -979,7 +981,7 @@ void ShaderBuilder::MergeParameters(map& variables, map keys = m_parameters[prog][var].keys(); + array keys = m_parameters[prog][var].keys(); if (keys.count()) { - code += String("//- ") + Shader::GetVariableQualifier((ShaderVariable)var) + " ----" + g_ret; - for (String key : keys) + code += std::string("//- ") + Shader::GetVariableQualifier((ShaderVariable)var) + " ----" + g_ret; + for (auto const &key : keys) { code += Shader::GetVariableQualifier((ShaderVariable)var) + " "; code += m_parameters[prog][var][key] + " " + key + ";" + g_ret; @@ -1025,30 +1027,30 @@ void ShaderBuilder::Build(String& code) code += g_ret; //Build Blocks code and add it - array calls; + array calls; for (int block = 0; block < m_blocks[prog].count(); block++) { - String call; - String function; + std::string call; + std::string function; m_blocks[prog][block]->Build(ShaderProgram(prog), call, function); calls << call; - if (m_blocks[prog][block]->m_code_custom.count()) + if (m_blocks[prog][block]->m_code_custom.length()) { - code += String("//- ") + m_blocks[prog][block]->GetName() + " custom code ----" + g_ret; + code += std::string("//- ") + m_blocks[prog][block]->GetName() + " custom code ----" + g_ret; code += m_blocks[prog][block]->m_code_custom + g_ret + g_ret; } - code += String("//- ") + m_blocks[prog][block]->GetName() + " main code ----" + g_ret; + code += std::string("//- ") + m_blocks[prog][block]->GetName() + " main code ----" + g_ret; code += function + g_ret + g_ret; } //Added main definition - code += String("//- Main ----") + g_ret + - String("void main(void)") + g_ret + "{" + g_ret; + code += std::string("//- Main ----") + g_ret + + "void main(void)" + g_ret + "{" + g_ret; //Add local variables int var = ShaderVariable::InOut; - array keys = m_parameters[prog][var].keys(); - for (String key : keys) + array keys = m_parameters[prog][var].keys(); + for (auto const &key : keys) { if (keys.count()) { @@ -1058,13 +1060,13 @@ void ShaderBuilder::Build(String& code) code += g_ret; //Add calls - code += g_tab + String("//- Calls ----") + g_ret; - for (String call : calls) + code += g_tab + "//- Calls ----" + g_ret; + for (auto const &call : calls) code += g_tab + call + g_ret; code += g_ret; code += g_tab + Shader::GetProgramOutVariable((ShaderProgram)prog) + " = " + out_local_var + ";" + g_ret + - String("}") + g_ret + g_ret; + "}" + g_ret + g_ret; } } diff --git a/src/image/codec/oric-image.cpp b/src/image/codec/oric-image.cpp index 3cf79210..d4120771 100644 --- a/src/image/codec/oric-image.cpp +++ b/src/image/codec/oric-image.cpp @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,7 @@ #include #include +#include #include "../../image/resource-private.h" @@ -37,7 +38,7 @@ public: virtual bool Save(char const *path, ResourceCodecData* data); private: - static String ReadScreen(char const *name); + static std::string ReadScreen(char const *name); static void WriteScreen(image &image, array &result); }; @@ -61,11 +62,11 @@ ResourceCodecData* OricImageCodec::Load(char const *path) u8vec4(0xff, 0xff, 0xff, 0xff), }; - String screen = ReadScreen(path); - if (screen.count() == 0) + std::string screen = ReadScreen(path); + if (screen.length() == 0) return nullptr; - auto data = new ResourceImageData(new image(ivec2(WIDTH, screen.count() * 6 / WIDTH))); + auto data = new ResourceImageData(new image(ivec2(WIDTH, screen.length() * 6 / WIDTH))); auto img = data->m_image; u8vec4 *pixels = img->lock(); @@ -116,9 +117,9 @@ bool OricImageCodec::Save(char const *path, ResourceCodecData* data) int len = (int)strlen(path); if (len < 4 || path[len - 4] != '.' - || toupper(path[len - 3]) != 'T' - || toupper(path[len - 2]) != 'A' - || toupper(path[len - 1]) != 'P') + || std::toupper(path[len - 3]) != 'T' + || std::toupper(path[len - 2]) != 'A' + || std::toupper(path[len - 1]) != 'P') return false; array result; @@ -152,11 +153,11 @@ bool OricImageCodec::Save(char const *path, ResourceCodecData* data) return true; } -String OricImageCodec::ReadScreen(char const *name) +std::string OricImageCodec::ReadScreen(char const *name) { File f; f.Open(name, FileAccess::Read); - String data = f.ReadString(); + std::string data = f.ReadString().C(); f.Close(); /* Skip the sync bytes */ @@ -170,17 +171,17 @@ String OricImageCodec::ReadScreen(char const *name) ++header; /* Skip the header, ignoring the last byte’s value */ - if (data.sub(header, 8) != String("\x00\xff\x80\x00\xbf\x3f\xa0\x00", 8)) + if (!starts_with(data, std::string("\x00\xff\x80\x00\xbf\x3f\xa0\x00", 8))) return ""; /* Skip the file name, including trailing nul char */ - data = data.sub(header + 8); - int filename_end = data.index_of('\0'); - if (filename_end < 0) - return ""; + data = data.substr(header + 8); + size_t filename_end = data.find('\0'); + if (filename_end == std::string::npos) + return ""; /* Read screen data */ - return data.sub(filename_end + 1); + return data.substr(filename_end + 1); } /* Error diffusion table, similar to Floyd-Steinberg. I choose not to diff --git a/src/image/codec/zed-image.cpp b/src/image/codec/zed-image.cpp index 0d8e00d7..9f05c27b 100644 --- a/src/image/codec/zed-image.cpp +++ b/src/image/codec/zed-image.cpp @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar -// 2014 Benjamin Huet +// Copyright © 2009—2014 Benjamin “Touky” Huet +// © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,8 @@ #include +#include + #include "../../image/resource-private.h" namespace lol @@ -38,7 +40,7 @@ DECLARE_IMAGE_CODEC(ZedImageCodec, 10) ResourceCodecData* ZedImageCodec::Load(char const *path) { - if (!lol::String(path).ends_with(".RSC")) + if (!ends_with(path, ".RSC")) return nullptr; // Compacter definition diff --git a/src/image/codec/zed-palette-image.cpp b/src/image/codec/zed-palette-image.cpp index e67e0788..79793334 100644 --- a/src/image/codec/zed-palette-image.cpp +++ b/src/image/codec/zed-palette-image.cpp @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar -// 2014 Benjamin Huet +// Copyright © 2009—2014 Benjamin “Touky” Huet +// © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -13,6 +13,8 @@ #include +#include + #include "../../image/resource-private.h" namespace lol @@ -38,7 +40,7 @@ DECLARE_IMAGE_CODEC(ZedPaletteImageCodec, 10) ResourceCodecData* ZedPaletteImageCodec::Load(char const *path) { - if (!lol::String(path).ends_with(".pal")) + if (!ends_with(path, ".pal")) return nullptr; File file; diff --git a/src/input/controller.cpp b/src/input/controller.cpp index a36c3df3..0c2222da 100644 --- a/src/input/controller.cpp +++ b/src/input/controller.cpp @@ -2,6 +2,7 @@ // Lol Engine // // Copyright © 2010—2015 Benjamin Litzelmann +// © 2017—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,20 +13,22 @@ #include +#include + namespace lol { /////////////////////////////////////////////////////////////////////////////// // KeyBinding -void KeyBinding::Bind(const String& device_name, const String& key_name) +void KeyBinding::Bind(const std::string& device_name, const std::string& key_name) { const InputDevice* device = InputDevice::Get(device_name); if (!device) { msg::warn("trying to bind key to nonexistent input device %s\n", - device_name.C()); + device_name.c_str()); return; } @@ -33,14 +36,14 @@ void KeyBinding::Bind(const String& device_name, const String& key_name) if (keyindex < 0) { msg::warn("trying to bind nonexistent key %s.%s\n", - device_name.C(), key_name.C()); + device_name.c_str(), key_name.c_str()); return; } m_keybindings.push(device, keyindex); } -bool KeyBinding::Unbind(const String& device_name, const String& key_name) +bool KeyBinding::Unbind(const std::string& device_name, const std::string& key_name) { for (int i = 0; i < m_keybindings.count(); ++i) { @@ -64,13 +67,13 @@ void KeyBinding::ClearBindings() /////////////////////////////////////////////////////////////////////////////// // AxisBinding -void AxisBinding::Bind(const String& device_name, const String& axis_name) +void AxisBinding::Bind(const std::string& device_name, const std::string& axis_name) { const InputDevice* device = InputDevice::Get(device_name); if (!device) { msg::warn("trying to bind axis to nonexistent input device %s\n", - device_name.C()); + device_name.c_str()); return; } @@ -78,20 +81,20 @@ void AxisBinding::Bind(const String& device_name, const String& axis_name) if (axisindex < 0) { msg::warn("trying to bind nonexistent axis %s.%s\n", - device_name.C(), axis_name.C()); + device_name.c_str(), axis_name.c_str()); return; } m_axisbindings.push(device, axisindex); } -void AxisBinding::BindKey(const String& device_name, const String& key_name) +void AxisBinding::BindKey(const std::string& device_name, const std::string& key_name) { const InputDevice* device = InputDevice::Get(device_name); if (!device) { msg::warn("trying to bind axis key to nonexistent input device %s\n", - device_name.C()); + device_name.c_str()); return; } @@ -99,20 +102,20 @@ void AxisBinding::BindKey(const String& device_name, const String& key_name) if (keyindex < 0) { msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.C(), key_name.C()); + device_name.c_str(), key_name.c_str()); return; } m_keybindings.push(device, -1, keyindex); } -void AxisBinding::BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name) +void AxisBinding::BindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name) { const InputDevice* device = InputDevice::Get(device_name); if (!device) { msg::warn("trying to bind axis keys to nonexistent input device %s\n", - device_name.C()); + device_name.c_str()); return; } @@ -120,7 +123,7 @@ void AxisBinding::BindKeys(const String& device_name, const String& min_key_name if (minkeyindex < 0) { msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.C(), min_key_name.C()); + device_name.c_str(), min_key_name.c_str()); return; } @@ -128,14 +131,14 @@ void AxisBinding::BindKeys(const String& device_name, const String& min_key_name if (maxkeyindex < 0) { msg::warn("trying to bind nonexistent axis key %s.%s\n", - device_name.C(), max_key_name.C()); + device_name.c_str(), max_key_name.c_str()); return; } m_keybindings.push(device, minkeyindex, maxkeyindex); } -bool AxisBinding::Unbind(const String& device_name, const String& axis_name) +bool AxisBinding::Unbind(const std::string& device_name, const std::string& axis_name) { for (int i = 0; i < m_keybindings.count(); ++i) { @@ -151,7 +154,7 @@ bool AxisBinding::Unbind(const String& device_name, const String& axis_name) return false; } -bool AxisBinding::UnbindKey(const String& device_name, const String& key_name) +bool AxisBinding::UnbindKey(const std::string& device_name, const std::string& key_name) { for (int i = 0; i < m_keybindings.count(); ++i) { @@ -167,7 +170,7 @@ bool AxisBinding::UnbindKey(const String& device_name, const String& key_name) return false; } -bool AxisBinding::UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name) +bool AxisBinding::UnbindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name) { for (int i = 0; i < m_keybindings.count(); ++i) { @@ -229,7 +232,7 @@ array Controller::controllers; uint32_t Controller::m_active_layer = ~((uint32_t)0); //----------------------------------------------------------------------------- -Controller::Controller(String const &name) +Controller::Controller(std::string const &name) { m_gamegroup = GAMEGROUP_INPUT; m_name = name; @@ -238,12 +241,12 @@ Controller::Controller(String const &name) m_active = false; if (Get(name) != nullptr) { - msg::warn("controller “%s” has already been registered\n", name.C()); + msg::warn("controller “%s” has already been registered\n", name.c_str()); } controllers.push(this); } -Controller::Controller(String const &name, InputProfile const& profile) +Controller::Controller(std::string const &name, InputProfile const& profile) : Controller(name) { Init(profile); @@ -342,13 +345,11 @@ float Controller::GetAxisDelta(int index) const } //----------------------------------------------------------------------------- -Controller* Controller::Get(String const &name) +Controller* Controller::Get(std::string const &name) { - for (int i = 0; i < controllers.count(); ++i) - { - if (controllers[i]->m_name == name) - return controllers[i]; - } + for (auto controller : controllers) + if (controller->m_name == name) + return controller; return nullptr; } diff --git a/src/input/controller.h b/src/input/controller.h index 82fe7944..fe0b1ee4 100644 --- a/src/input/controller.h +++ b/src/input/controller.h @@ -1,15 +1,20 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Benjamin Litzelmann -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2015 Benjamin Litzelmann +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once +#include + namespace lol { @@ -37,16 +42,16 @@ protected: public: //Binding methods --------------------------------------------------------- /** Bind a physical device and key */ - void Bind(const String& device_name, const String& key_name); + void Bind(const std::string& device_name, const std::string& key_name); /** Unbind a previously bound physical device and key. Returns true if the binding was existing. */ - bool Unbind(const String& device_name, const String& key_name); + bool Unbind(const std::string& device_name, const std::string& key_name); /* Small helpers */ - void BindMouse(const String& key_name) { Bind(g_name_mouse, key_name); } - void BindKeyboard(const String& key_name) { Bind(g_name_keyboard, key_name); } - void BindJoystick(const uint64_t num, const String& key_name) { Bind(g_name_joystick(num), key_name); } - bool UnbindMouse(const String& key_name) { return Unbind(g_name_mouse, key_name); } - bool UnbindKeyboard(const String& key_name) { return Unbind(g_name_keyboard, key_name); } - bool UnbindJoystick(const uint64_t num, const String& key_name) { return Unbind(g_name_joystick(num), key_name); } + void BindMouse(const std::string& key_name) { Bind(g_name_mouse, key_name); } + void BindKeyboard(const std::string& key_name) { Bind(g_name_keyboard, key_name); } + void BindJoystick(const uint64_t num, const std::string& key_name) { Bind(g_name_joystick(num), key_name); } + bool UnbindMouse(const std::string& key_name) { return Unbind(g_name_mouse, key_name); } + bool UnbindKeyboard(const std::string& key_name) { return Unbind(g_name_keyboard, key_name); } + bool UnbindJoystick(const uint64_t num, const std::string& key_name) { return Unbind(g_name_joystick(num), key_name); } /** Clear current binding */ void ClearBindings(); /** Indicate wheither a physical device and key has been bound. Returns the number of bindings set. */ @@ -92,31 +97,31 @@ protected: public: //Binding methods --------------------------------------------------------- /** Bind a physical device and axis */ - void Bind(const String& device_name, const String& axis_name); + void Bind(const std::string& device_name, const std::string& axis_name); /** Bind a physical device and key over this axis. The axis value will be 0 if the key is up and 1 if it's down */ - void BindKey(const String& device_name, const String& key_name); + void BindKey(const std::string& device_name, const std::string& key_name); /** Bind physical device and keys over this axis. The axis value will be 0 if both the key are up, -1 if minkey is down, and 1 if maxkey is down */ - void BindKeys(const String& device_name, const String& min_key_name, const String& max_key_name); + void BindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool Unbind(const String& device_name, const String& axis_name); + bool Unbind(const std::string& device_name, const std::string& axis_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKey(const String& device_name, const String& key_name); + bool UnbindKey(const std::string& device_name, const std::string& key_name); /** Unbind a previously bound physical device and axis. Returns true if the binding was existing. */ - bool UnbindKeys(const String& device_name, const String& min_key_name, const String& max_key_name); + bool UnbindKeys(const std::string& device_name, const std::string& min_key_name, const std::string& max_key_name); /* Small helpers */ - void BindMouse(const String& axis_name) { Bind(g_name_mouse, axis_name); } - void BindMouseKey(const String& key_name) { BindKey(g_name_mouse, key_name); } - void BindMouseKeys(const String& min_key_name, const String& max_key_name) { BindKeys(g_name_mouse, min_key_name, max_key_name); } - bool UnbindMouse(const String& axis_name) { return Unbind(g_name_mouse, axis_name); } - bool UnbindMouseKey(const String& key_name) { return UnbindKey(g_name_mouse, key_name); } - bool UnbindMouseKeys(const String& min_key_name, const String& max_key_name){ return UnbindKeys(g_name_mouse, min_key_name, max_key_name); } + void BindMouse(const std::string& axis_name) { Bind(g_name_mouse, axis_name); } + void BindMouseKey(const std::string& key_name) { BindKey(g_name_mouse, key_name); } + void BindMouseKeys(const std::string& min_key_name, const std::string& max_key_name) { BindKeys(g_name_mouse, min_key_name, max_key_name); } + bool UnbindMouse(const std::string& axis_name) { return Unbind(g_name_mouse, axis_name); } + bool UnbindMouseKey(const std::string& key_name) { return UnbindKey(g_name_mouse, key_name); } + bool UnbindMouseKeys(const std::string& min_key_name, const std::string& max_key_name){ return UnbindKeys(g_name_mouse, min_key_name, max_key_name); } /* */ - void BindJoystick(const uint64_t num, const String& axis_name) { Bind(g_name_joystick(num), axis_name); } - void BindJoystickKey(const uint64_t num, const String& key_name) { BindKey(g_name_joystick(num), key_name); } - void BindJoystickKeys(const uint64_t num, const String& min_key_name, const String& max_key_name) { BindKeys(g_name_joystick(num), min_key_name, max_key_name); } - bool UnbindJoystick(const uint64_t num, const String& axis_name) { return Unbind(g_name_joystick(num), axis_name); } - bool UnbindJoystickKey(const uint64_t num, const String& key_name) { return UnbindKey(g_name_joystick(num), key_name); } - bool UnbindJoystickKeys(const uint64_t num, const String& min_key_name, const String& max_key_name){ return UnbindKeys(g_name_joystick(num), min_key_name, max_key_name); } + void BindJoystick(const uint64_t num, const std::string& axis_name) { Bind(g_name_joystick(num), axis_name); } + void BindJoystickKey(const uint64_t num, const std::string& key_name) { BindKey(g_name_joystick(num), key_name); } + void BindJoystickKeys(const uint64_t num, const std::string& min_key_name, const std::string& max_key_name) { BindKeys(g_name_joystick(num), min_key_name, max_key_name); } + bool UnbindJoystick(const uint64_t num, const std::string& axis_name) { return Unbind(g_name_joystick(num), axis_name); } + bool UnbindJoystickKey(const uint64_t num, const std::string& key_name) { return UnbindKey(g_name_joystick(num), key_name); } + bool UnbindJoystickKeys(const uint64_t num, const std::string& min_key_name, const std::string& max_key_name){ return UnbindKeys(g_name_joystick(num), min_key_name, max_key_name); } /** Clear current binding */ void ClearBindings(); /** Indicate wheither a physical device and axis has been bound. Returns the number of bindings set. */ @@ -150,13 +155,13 @@ private: friend class InputProfile; public: Key() { } - Key(int idx, String const& name) : m_idx(idx), m_name(name) { } + Key(int idx, std::string const& name) : m_idx(idx), m_name(name) { } Key(const Key& other) : m_idx(other.m_idx), m_name(other.m_name) { } ~Key() { } bool operator==(const Key& other) { return m_name == other.m_name; } private: int m_idx = 0; - String m_name; + std::string m_name; }; //--------------------------------------------------------------------- class Joystick @@ -165,14 +170,14 @@ private: friend class InputProfile; public: Joystick() { } - Joystick(uint64_t joy, int idx, String const& name) : m_joy(joy), m_idx(idx), m_name(name) { } + Joystick(uint64_t joy, int idx, std::string const& name) : m_joy(joy), m_idx(idx), m_name(name) { } Joystick(const Joystick& other) : m_joy(other.m_joy), m_idx(other.m_idx), m_name(other.m_name) { } ~Joystick() { } bool operator==(const Joystick& other) { return m_name == other.m_name; } private: uint64_t m_joy = 0; int m_idx = 0; - String m_name; + std::string m_name; }; public: //--------------------------------------------------------------------- @@ -182,7 +187,7 @@ public: friend class InputProfile; public: Keyboard() : Key() { } - Keyboard(int idx, String const& name) : Key(idx, name) { } + Keyboard(int idx, std::string const& name) : Key(idx, name) { } Keyboard(const Keyboard& other) : Key(other.m_idx, other.m_name) { } }; //--------------------------------------------------------------------- @@ -192,7 +197,7 @@ public: friend class InputProfile; public: MouseKey() : Key() { } - MouseKey(int idx, String const& name) : Key(idx, name) { } + MouseKey(int idx, std::string const& name) : Key(idx, name) { } MouseKey(const Keyboard& other) : Key(other.m_idx, other.m_name) { } }; //--------------------------------------------------------------------- @@ -202,7 +207,7 @@ public: friend class InputProfile; public: MouseAxis() : Key() { } - MouseAxis(int idx, String const& name) : Key(idx, name) { } + MouseAxis(int idx, std::string const& name) : Key(idx, name) { } MouseAxis(const Keyboard& other) : Key(other.m_idx, other.m_name) { } }; //--------------------------------------------------------------------- @@ -212,7 +217,7 @@ public: friend class InputProfile; public: JoystickKey() : Joystick() { } - JoystickKey(uint64_t joy, int idx, String const& name) : Joystick(joy, idx, name) { } + JoystickKey(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } JoystickKey(const JoystickKey& other) : Joystick(other.m_joy, other.m_idx, other.m_name) { } }; //--------------------------------------------------------------------- @@ -222,7 +227,7 @@ public: friend class InputProfile; public: JoystickAxis() : Joystick() { } - JoystickAxis(uint64_t joy, int idx, String const& name) : Joystick(joy, idx, name) { } + JoystickAxis(uint64_t joy, int idx, std::string const& name) : Joystick(joy, idx, name) { } JoystickAxis(const JoystickAxis& other) : Joystick(other.m_joy, other.m_idx, other.m_name) { } }; public: @@ -326,7 +331,7 @@ public: MAX, }; protected: - virtual bool BuildEnumMap(map& enum_map) { UNUSED(enum_map); return true; } + virtual bool BuildEnumMap(map& enum_map) { UNUSED(enum_map); return true; } }; typedef SafeEnum InputType; @@ -338,11 +343,11 @@ public: { switch (type.ToScalar()) { - case InputType::Keyboard:/******/*this << InputProfile::Keyboard/******/(/***/i, T(i).ToString()); break; - case InputType::MouseKey:/******/*this << InputProfile::MouseKey/******/(/***/i, T(i).ToString()); break; - case InputType::JoystickKey:/***/*this << InputProfile::JoystickKey/***/(joy, i, T(i).ToString()); break; - case InputType::MouseAxis:/*****/*this << InputProfile::MouseAxis/*****/(/***/i, T(i).ToString()); break; - case InputType::JoystickAxis:/**/*this << InputProfile::JoystickAxis/**/(joy, i, T(i).ToString()); break; + case InputType::Keyboard:/******/*this << InputProfile::Keyboard/******/(/***/i, T(i).tostring()); break; + case InputType::MouseKey:/******/*this << InputProfile::MouseKey/******/(/***/i, T(i).tostring()); break; + case InputType::JoystickKey:/***/*this << InputProfile::JoystickKey/***/(joy, i, T(i).tostring()); break; + case InputType::MouseAxis:/*****/*this << InputProfile::MouseAxis/*****/(/***/i, T(i).tostring()); break; + case InputType::JoystickAxis:/**/*this << InputProfile::JoystickAxis/**/(joy, i, T(i).tostring()); break; default: break; } } @@ -363,8 +368,8 @@ typedef InputProfile::InputType InputProfileType; class Controller : public Entity { public: - Controller(String const &name); - Controller(String const &name, InputProfile const& setup); + Controller(std::string const &name); + Controller(std::string const &name, InputProfile const& setup); virtual ~Controller(); virtual void TickGame(float seconds); @@ -413,7 +418,7 @@ public: float GetAxisDelta(int index) const; /** Get named controller */ - static Controller* Get(String const &name); + static Controller* Get(std::string const &name); protected: /** Input profile system */ @@ -427,7 +432,7 @@ private: static uint32_t m_active_layer; //All active by default static array controllers; - String m_name; + std::string m_name; bool m_activate_nextframe; bool m_deactivate_nextframe; bool m_active; diff --git a/src/input/input.cpp b/src/input/input.cpp index f3742609..81c60a96 100644 --- a/src/input/input.cpp +++ b/src/input/input.cpp @@ -1,15 +1,20 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Benjamin Litzelmann -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2015 Benjamin Litzelmann +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include +#include + #include "input/input_internal.h" namespace lol @@ -19,19 +24,17 @@ array InputDevice::devices; int InputDevice::joystick_count = 0; bool InputDevice::m_capturemouse; -array InputDevice::GetAvailableDevices() +array InputDevice::GetAvailableDevices() { - array result; - for (int i = 0; i < devices.count(); ++i) - { - result.push(devices[i]->m_name); - } + array result; + for (auto const &device : devices) + result.push(device->m_name); return result; } -String InputDevice::GetText() +std::string InputDevice::GetText() { - String ret = m_text; + std::string ret = m_text; m_text = ""; return ret; } @@ -92,7 +95,7 @@ void InputDeviceInternal::AddCursor(int index, const char* name) InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard() { - InputDeviceInternal* keyboard = new InputDeviceInternal(g_name_keyboard.C()); + InputDeviceInternal* keyboard = new InputDeviceInternal(g_name_keyboard.c_str()); /* Register all scancodes known to SDL (from the USB standard) */ # define _SC(id, str, name) keyboard->AddKey(id, #name); @@ -103,20 +106,20 @@ InputDeviceInternal* InputDeviceInternal::CreateStandardKeyboard() InputDeviceInternal* InputDeviceInternal::CreateStandardMouse() { - InputDeviceInternal* mouse = new InputDeviceInternal(g_name_mouse.C()); - mouse->AddKey(g_name_mouse_key_left.C()); - mouse->AddKey(g_name_mouse_key_middle.C()); - mouse->AddKey(g_name_mouse_key_right.C()); + InputDeviceInternal* mouse = new InputDeviceInternal(g_name_mouse.c_str()); + mouse->AddKey(g_name_mouse_key_left.c_str()); + mouse->AddKey(g_name_mouse_key_middle.c_str()); + mouse->AddKey(g_name_mouse_key_right.c_str()); //Added to manage if mouse is in the screen or not. - mouse->AddKey(g_name_mouse_key_in_screen.C()); + mouse->AddKey(g_name_mouse_key_in_screen.c_str()); - mouse->AddAxis(g_name_mouse_axis_x.C()); - mouse->AddAxis(g_name_mouse_axis_y.C()); - mouse->AddAxis(g_name_mouse_axis_xpixel.C()); - mouse->AddAxis(g_name_mouse_axis_ypixel.C()); - mouse->AddAxis(g_name_mouse_axis_scroll.C(), .0000001f); + mouse->AddAxis(g_name_mouse_axis_x.c_str()); + mouse->AddAxis(g_name_mouse_axis_y.c_str()); + mouse->AddAxis(g_name_mouse_axis_xpixel.c_str()); + mouse->AddAxis(g_name_mouse_axis_ypixel.c_str()); + mouse->AddAxis(g_name_mouse_axis_scroll.c_str(), .0000001f); - mouse->AddCursor(g_name_mouse_cursor.C()); + mouse->AddCursor(g_name_mouse_cursor.c_str()); // TODO: extended button, and wheel (as axis or as buttons? or both?) return mouse; diff --git a/src/input/input.h b/src/input/input.h index 658a279e..ad906f9a 100644 --- a/src/input/input.h +++ b/src/input/input.h @@ -1,90 +1,93 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Benjamin Litzelmann -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2015 Benjamin Litzelmann +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once +#include + namespace lol { -const String g_name_max("MAX"); -const String g_name_mouse("Mouse"); -const String g_name_keyboard("Keyboard"); -static String g_name_joystick() -{ - return String("Joystick"); -} +const std::string g_name_max("MAX"); +const std::string g_name_mouse("Mouse"); +const std::string g_name_keyboard("Keyboard"); + +static std::string g_name_joystick() { return "Joystick"; } static std::string g_name_joystick(const uint64_t num) { return format("Joystick%d", (int)num); } -# define _SC(id, str, name) const String g_name_key_##name(#name); +# define _SC(id, str, name) const std::string g_name_key_##name(#name); # include "input/keys.h" //Mouse default buttons/axis -const String g_name_mouse_key_left("Left"); -const String g_name_mouse_key_middle("Middle"); -const String g_name_mouse_key_right("Right"); -const String g_name_mouse_key_in_screen("InScreen"); -const String g_name_mouse_axis_x("X"); -const String g_name_mouse_axis_y("Y"); -const String g_name_mouse_axis_xpixel("XPixel"); -const String g_name_mouse_axis_ypixel("YPixel"); -const String g_name_mouse_axis_scroll("Scroll"); -const String g_name_mouse_cursor("Cursor"); +const std::string g_name_mouse_key_left("Left"); +const std::string g_name_mouse_key_middle("Middle"); +const std::string g_name_mouse_key_right("Right"); +const std::string g_name_mouse_key_in_screen("InScreen"); +const std::string g_name_mouse_axis_x("X"); +const std::string g_name_mouse_axis_y("Y"); +const std::string g_name_mouse_axis_xpixel("XPixel"); +const std::string g_name_mouse_axis_ypixel("YPixel"); +const std::string g_name_mouse_axis_scroll("Scroll"); +const std::string g_name_mouse_cursor("Cursor"); //Xbox default buttons/axis -const String g_name_xbox_key_dpad_up("DPadUp"); -const String g_name_xbox_key_dpad_down("DPadDown"); -const String g_name_xbox_key_dpad_left("DPadLeft"); -const String g_name_xbox_key_dpad_right("DPadRight"); -const String g_name_xbox_key_left_thumb("LeftThumb"); -const String g_name_xbox_key_right_thumb("RightThumb"); -const String g_name_xbox_key_left_shoulder("LeftShoulder"); -const String g_name_xbox_key_right_shoulder("Rightshoulder"); -const String g_name_xbox_key_a("A"); -const String g_name_xbox_key_b("B"); -const String g_name_xbox_key_x("X"); -const String g_name_xbox_key_y("Y"); -const String g_name_xbox_key_start("Start"); -const String g_name_xbox_key_back("Back"); -const String g_name_xbox_axis_left_x("Axis1"); -const String g_name_xbox_axis_left_y("Axis2"); -const String g_name_xbox_axis_right_x("Axis3"); -const String g_name_xbox_axis_right_y("Axis4"); -const String g_name_xbox_axis_left_trigger("Axis5"); -const String g_name_xbox_axis_right_trigger("Axis6"); +const std::string g_name_xbox_key_dpad_up("DPadUp"); +const std::string g_name_xbox_key_dpad_down("DPadDown"); +const std::string g_name_xbox_key_dpad_left("DPadLeft"); +const std::string g_name_xbox_key_dpad_right("DPadRight"); +const std::string g_name_xbox_key_left_thumb("LeftThumb"); +const std::string g_name_xbox_key_right_thumb("RightThumb"); +const std::string g_name_xbox_key_left_shoulder("LeftShoulder"); +const std::string g_name_xbox_key_right_shoulder("Rightshoulder"); +const std::string g_name_xbox_key_a("A"); +const std::string g_name_xbox_key_b("B"); +const std::string g_name_xbox_key_x("X"); +const std::string g_name_xbox_key_y("Y"); +const std::string g_name_xbox_key_start("Start"); +const std::string g_name_xbox_key_back("Back"); +const std::string g_name_xbox_axis_left_x("Axis1"); +const std::string g_name_xbox_axis_left_y("Axis2"); +const std::string g_name_xbox_axis_right_x("Axis3"); +const std::string g_name_xbox_axis_right_y("Axis4"); +const std::string g_name_xbox_axis_left_trigger("Axis5"); +const std::string g_name_xbox_axis_right_trigger("Axis6"); class InputDevice { public: /** Gets the name of this input device */ - const String& GetName() const + const std::string& GetName() const { return m_name; } /** Gets the index of the corresponding key, needed to call GetKey */ - ptrdiff_t GetKeyIndex(String const &name) const + ptrdiff_t GetKeyIndex(std::string const &name) const { return GetItemIndex(name, m_keynames); } /** Gets the index of the corresponding axis, needed to call GetAxis */ - ptrdiff_t GetAxisIndex(String const &name) const + ptrdiff_t GetAxisIndex(std::string const &name) const { return GetItemIndex(name, m_axisnames); } /** Gets the index of the corresponding cursor, needed to call GetCursor */ - ptrdiff_t GetCursorIndex(String const &name) const + ptrdiff_t GetCursorIndex(std::string const &name) const { return GetItemIndex(name, m_cursornames); } @@ -97,7 +100,7 @@ public: } /** Gets the latest contents of text input. */ - String GetText(); + std::string GetText(); bool IsTextInputActive(); void SetTextInputActive(bool status); @@ -139,26 +142,26 @@ public: } /** Gets a list of the name of all available keys in this device */ - const array& GetAllKeys() const + const array& GetAllKeys() const { return m_keynames; } /** Gets a list of the name of all available axis in this device */ - const array& GetAllAxis() const + const array& GetAllAxis() const { return m_axisnames; } /** Gets a list of the name of all available cursors in this device */ - const array& GetAllCursors() const + const array& GetAllCursors() const { return m_cursornames; } /** Gets a list of the name of all available input devices */ - static array GetAvailableDevices(); + static array GetAvailableDevices(); /** Gets an input device by its name */ - static InputDevice* Get(String const &name) + static InputDevice* Get(std::string const &name) { return GetDevice(name); } @@ -190,17 +193,17 @@ public: protected: // TODO: hide all of this in a InputDeviceData? - String m_name; + std::string m_name; - array m_keynames; - array m_axisnames; - array m_cursornames; + array m_keynames; + array m_axisnames; + array m_cursornames; /** Key states (pressed/released) */ array m_keys; /** Text input state */ - String m_text; + std::string m_text; bool m_input_active; /** Axis states (value and sensitivity) */ @@ -211,7 +214,7 @@ protected: static bool m_capturemouse; - InputDevice(String const &name) + InputDevice(std::string const &name) : m_name(name), m_input_active(false) { @@ -235,7 +238,7 @@ private: static int joystick_count; template - ptrdiff_t GetItemIndex(String const &name, const array& a) const + ptrdiff_t GetItemIndex(std::string const &name, const array& a) const { for (int i = 0; i < a.count(); ++i) { @@ -245,10 +248,11 @@ private: return -1; } - static InputDevice* GetDevice(String const &name) + static InputDevice* GetDevice(std::string const &name) { //Count the device types. TODO: Multi mouse/keyboard - if (name.contains(g_name_joystick())) joystick_count++; + if (name.find(g_name_joystick()) != std::string::npos) + ++joystick_count; for (int i = 0; i < devices.count(); ++i) { diff --git a/src/input/input_internal.h b/src/input/input_internal.h index ca6b2cc8..207e5b59 100644 --- a/src/input/input_internal.h +++ b/src/input/input_internal.h @@ -1,15 +1,20 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Benjamin Litzelmann -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2015 Benjamin Litzelmann +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once +#include + namespace lol { @@ -19,7 +24,7 @@ namespace lol class InputDeviceInternal : public InputDevice { public: - inline InputDeviceInternal(String const& name) : InputDevice(name) { } + inline InputDeviceInternal(std::string const& name) : InputDevice(name) { } void AddKey(int id, char const * name); @@ -47,7 +52,7 @@ public: m_keys[id] = state; } - void AddText(String const &text) + void AddText(std::string const &text) { m_text += text; } diff --git a/src/light.h b/src/light.h index 548a1edb..8fbda990 100644 --- a/src/light.h +++ b/src/light.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -15,10 +17,11 @@ // --------------- // -#include /* for FLT_MAX */ - #include "engine/worldentity.h" +#include /* for FLT_MAX */ +#include + namespace lol { @@ -33,7 +36,7 @@ struct LightType } m_value; - String ToString() + std::string tostring() { switch (m_value) { diff --git a/src/lol/base/enum.h b/src/lol/base/enum.h index 425b57b7..485995bc 100644 --- a/src/lol/base/enum.h +++ b/src/lol/base/enum.h @@ -1,8 +1,8 @@ // // Lol Engine // -// Copyright © 2010-2015 Sam Hocevar -// © 2013-2015 Guillaume Bittoun +// Copyright © 2010—2018 Sam Hocevar +// © 2013—2015 Guillaume Bittoun // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,6 +15,8 @@ #include +#include + namespace lol { @@ -25,7 +27,7 @@ namespace lol // { // }; //protected: -// virtual bool BuildEnumMap(map& enum_map) +// virtual bool BuildEnumMap(map& enum_map) // { // enum_map[] = ""; // return true; @@ -38,7 +40,7 @@ struct StructSafeEnum { protected: /* Convert to string stuff */ - virtual bool BuildEnumMap(map&) { return false; } + virtual bool BuildEnumMap(map&) { return false; } }; //----------------------------------------------------------------------------- template @@ -54,14 +56,14 @@ public: /* Allow conversion from int and to the underlying type */ inline explicit SafeEnum(int i) : m_value(T(i)) {} inline Type ToScalar() const { return m_value; } - //inline class String ToString() const { return ToString(); } + //inline std::string tostring() const { return tostring(); } /* Convert to string stuff */ - inline class String ToString() + inline std::string tostring() { /* FIXME: we all know this isn’t thread safe. But is it really * a big deal? */ - static map enum_map; + static map enum_map; static bool ready = false; if (ready || this->BuildEnumMap(enum_map)) @@ -112,7 +114,7 @@ struct DisplayFlagBase : public StructSafeEnum MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[On] = "On"; enum_map[Off] = "Off"; diff --git a/src/lol/base/string.h b/src/lol/base/string.h index b1bb6d81..4d4afb43 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2017 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2013—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -25,11 +25,20 @@ namespace lol { /* Split a string along a single separator */ -array split(std::string const &s, char sep); +array split(std::string const &s, char sep = '\n'); /* Split a string along multiple separators */ array split(std::string const &s, std::string const &seps); +/* Check whether a string starts or ends with a given substring */ +bool starts_with(std::string const &s, std::string const &prefix); +bool ends_with(std::string const &s, std::string const &suffix); + +/* Convert a string to lowercase or uppercase */ +std::string tolower(std::string const &s); +std::string toupper(std::string const &s); + +/* Format a string, printf-style */ std::string format(char const *format, ...) LOL_ATTR_FORMAT(1, 2); std::string vformat(char const *format, va_list ap); @@ -158,61 +167,6 @@ public: return String(&(*this)[start], item_count); } - bool contains(String const &s) const - { - return index_of(s.C()) != INDEX_NONE; - } - - int index_of(char token) const - { - using namespace std; - - char const *tmp = strchr(C(), token); - return tmp ? int(tmp - C()) : INDEX_NONE; - } - - int index_of(String const& token) const { return index_of(token.C()); } - int index_of(char const* token) const - { - using namespace std; - - char const *tmp = strstr(C(), token); - return tmp ? int(tmp - C()) : INDEX_NONE; - } - - int last_index_of(char token) const - { - using namespace std; - - char const *tmp = strrchr(C(), token); - return tmp ? int(tmp - C()) : INDEX_NONE; - } - - int last_index_of(String const& token) const { return last_index_of(token.C()); } - int last_index_of(char const* token) const - { - using namespace std; - - int token_len = (int)strlen(token); - for (int i = count() - token_len; i >= 0; --i) - if (strstr(C() + i, token)) - return i; - return -1; - } - - int count_occurence(String const& token) const { return last_index_of(token.C()); } - int count_occurence(char const* token) const - { - int count = 0; - const char *match = strstr(C(), token); - while (match) - { - count++; - match = strstr(match + 1, token); - } - return count; - } - int replace(char const old_token, char const new_token, bool all_occurrences = false) { @@ -230,74 +184,6 @@ public: return res; } - inline String& to_lower() - { - String ret(*this); - for (int i = 0; i < ret.count(); ++i) - { - if ('A' <= ret[i] && ret[i] <= 'Z') - ret[i] += 'a' - 'A'; - } - *this = ret; - return *this; - } - - inline String& to_upper() - { - String ret(*this); - for (int i = 0; i < ret.count(); ++i) - { - if ('a' <= ret[i] && ret[i] <= 'z') - ret[i] += 'A' - 'a'; - } - *this = ret; - return *this; - } - inline String& case_change(bool case_to_upper) - { - return case_to_upper ? to_upper() : to_lower(); - } - - bool starts_with(String const &s) const - { - using namespace std; - return count() >= s.count() - && memcmp(C(), s.C(), s.count()) == 0; - } - - bool ends_with(String const &s) const - { - using namespace std; - return count() >= s.count() - && memcmp(C() + count() - s.count(), s.C(), s.count()) == 0; - } - - array split(char c = '\n') const - { - array ret; - for (int start = 0; start < m_count; ) - { - char const *tmp = strchr(C() + start, c); - if (!tmp) - { - ret.push(String(C() + start)); - break; - } - int size = int(tmp - C()) - start; - ret.push(String(C() + start, size)); - start += size + 1; - } - return ret; - } - - bool is_alpha() const - { - for (int i = 0; i < m_count; i++) - if (m_data[i] != '\0' && (m_data[i] < '0' || '9' < m_data[i])) - return false; - return true; - } - inline String operator +(String const &s) const { String ret(*this); diff --git a/src/lol/gpu/shader.h b/src/lol/gpu/shader.h index eee18ba0..87f830c6 100644 --- a/src/lol/gpu/shader.h +++ b/src/lol/gpu/shader.h @@ -1,11 +1,13 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2010-2011 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2010—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once @@ -15,7 +17,8 @@ // ---------------- // -#include +#include +#include #include "engine/entity.h" @@ -57,7 +60,7 @@ struct VertexUsageBase : public StructSafeEnum }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Position] = "Position"; enum_map[BlendWeight] = "BlendWeight"; @@ -99,7 +102,7 @@ struct ShaderVariableBase MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Attribute] = "Attribute"; enum_map[Uniform] = "Uniform"; @@ -123,7 +126,7 @@ struct ShaderProgramBase MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Geometry] = "Geometry"; enum_map[Vertex] = "Vertex"; @@ -198,7 +201,7 @@ struct ShaderVariableTypeBase MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Bool] = "bool"; enum_map[Int] = "int"; enum_map[UInt] = "uint"; @@ -312,13 +315,13 @@ class ShaderData; class Shader : public Entity { public: - static Shader *Create(String const &name, String const &code); + static Shader *Create(std::string const &name, std::string const &code); static void Destroy(Shader *shader); int GetAttribCount() const; ShaderAttrib GetAttribLocation(VertexUsage usage, int index) const; - ShaderUniform GetUniformLocation(String const& uni) const; + ShaderUniform GetUniformLocation(std::string const& uni) const; ShaderUniform GetUniformLocation(char const *uni) const; void SetUniform(ShaderUniform const &uni, int i); void SetUniform(ShaderUniform const &uni, ivec2 const &v); @@ -342,19 +345,19 @@ public: void Unbind() const; protected: - Shader(String const &name, String const &vert, String const &frag); + Shader(std::string const &name, std::string const &vert, std::string const &frag); ~Shader(); private: ShaderData *data; public: - static String GetVariablePrefix(const ShaderVariable variable); - static String GetVariableQualifier(const ShaderVariable variable); - static String GetFunctionQualifier(const ShaderVariable variable, const ShaderProgram program); - static String GetProgramQualifier(const ShaderProgram program); - static String GetProgramOutVariable(const ShaderProgram program); - static String GetProgramOutVariableLocal(const ShaderProgram program); + static std::string GetVariablePrefix(const ShaderVariable variable); + static std::string GetVariableQualifier(const ShaderVariable variable); + static std::string GetFunctionQualifier(const ShaderVariable variable, const ShaderProgram program); + static std::string GetProgramQualifier(const ShaderProgram program); + static std::string GetProgramOutVariable(const ShaderProgram program); + static std::string GetProgramOutVariableLocal(const ShaderProgram program); }; //ShaderVar ------------------------------------------------------------------- @@ -363,31 +366,31 @@ class ShaderVar friend class ShaderBuilder; friend class ShaderBlock; -protected: public: ShaderVar() { } - ShaderVar(ShaderVariable const& qualifier, String const& type, String const& name) + ShaderVar(ShaderVariable const& qualifier, std::string const& type, std::string const& name) { m_qualifier = qualifier; m_type = type; m_name = name; } - ShaderVar(ShaderVariable const& qualifier, ShaderVariableType const& type, String const& name) - : ShaderVar(qualifier, ShaderVariableType(type).ToString(), name) + ShaderVar(ShaderVariable const& qualifier, ShaderVariableType const& type, std::string const& name) + : ShaderVar(qualifier, ShaderVariableType(type).tostring(), name) { } + ~ShaderVar() { } - inline operator String() const { return Shader::GetVariablePrefix(m_qualifier) + m_name; } + inline std::string tostring() const { return Shader::GetVariablePrefix(m_qualifier) + m_name; } inline ShaderVariable GetQualifier() const { return m_qualifier; } - inline String GetType() const { return m_type; } - inline String operator+(String const& value) { return String() + *this + value; } + inline std::string GetType() const { return m_type; } + inline std::string operator+(std::string const& s) { return tostring() + s; } static ShaderVar GetShaderOut(ShaderProgram program); protected: ShaderVariable m_qualifier; - String m_type; - String m_name; + std::string m_type; + std::string m_name; }; //ShaderBlock ----------------------------------------------------------------- @@ -396,28 +399,28 @@ class ShaderBlock friend class ShaderBuilder; protected: - String m_name; + std::string m_name; //-------------------------- //map : //-------------------------- //Main shader parameters - map m_parameters[ShaderVariable::MAX]; + map m_parameters[ShaderVariable::MAX]; //Actual code - String m_code_main; - String m_code_custom; + std::string m_code_main; + std::string m_code_custom; public: - ShaderBlock(String const& name) : m_name(name) { } + ShaderBlock(std::string const& name) : m_name(name) { } ~ShaderBlock() { } - String const& GetName() { return m_name; } + std::string const& GetName() { return m_name; } //Sets code that will be used in the main - void SetMainCode(String const& code_main) { m_code_main = code_main; } + void SetMainCode(std::string const& code_main) { m_code_main = code_main; } //Sets custom code that will be put before the main -so functions- - void SetCustomCode(String const& code_custom) { m_code_custom = code_custom; } + void SetCustomCode(std::string const& code_custom) { m_code_custom = code_custom; } //Add parameter to the block void AddVar(ShaderVar const& var); inline ShaderBlock& operator<<(ShaderVar const& var) @@ -427,40 +430,40 @@ public: } protected: - void AddCallParameters(map const& variables, String& result); - void AddDefinitionParameters(const ShaderVariable variable, const ShaderProgram program, map& variables, String& result); - void Build(const ShaderProgram program, String& call, String& function); + void AddCallParameters(map const& variables, std::string& result); + void AddDefinitionParameters(const ShaderVariable variable, const ShaderProgram program, map& variables, std::string& result); + void Build(const ShaderProgram program, std::string& call, std::string& function); }; //Shaderbuilder --------------------------------------------------------------- class ShaderBuilder { protected: - String m_name; - String m_version; + std::string m_name; + std::string m_version; ShaderProgram m_current_program = ShaderProgram::MAX; //Blocks array m_blocks[ShaderProgram::MAX]; //Final shader parameters - map m_parameters[ShaderProgram::MAX][ShaderVariable::MAX]; + map m_parameters[ShaderProgram::MAX][ShaderVariable::MAX]; public: - ShaderBuilder(String const& name, String const& version); + ShaderBuilder(std::string const& name, std::string const& version); ~ShaderBuilder(); - String const& GetName(); + std::string const& GetName(); ShaderBuilder& operator<<(const ShaderProgram program); ShaderBuilder& operator<<(ShaderBlock* block); ShaderBuilder& operator<<(ShaderBlock const& block); protected: - String AddSlotOutVariableLocal(const ShaderProgram program); - void MergeParameters(map& variables, map& merged); + std::string AddSlotOutVariableLocal(const ShaderProgram program); + void MergeParameters(map& variables, map& merged); public: - void Build(String& code); + void Build(std::string& code); }; } /* namespace lol */ diff --git a/src/lolimgui.cpp b/src/lolimgui.cpp index 718b86a2..93e4d021 100644 --- a/src/lolimgui.cpp +++ b/src/lolimgui.cpp @@ -1,7 +1,8 @@ // -// imGui integration in lolengine +// Lol Engine // // Copyright © 2009—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -15,6 +16,11 @@ #include "imgui.cpp" #include +#include + +// +// The Imgui integration +// using namespace lol; @@ -111,18 +117,18 @@ LolImGui::LolImGui() << out_vertex << m_ortho << in_position << pass_texcoord << in_texcoord << pass_color << in_color; - imgui_vertex.SetMainCode(String() + - Line(out_vertex + " = .5 *" + m_ortho + " * vec4(" + in_position + ", -1.0, 1.0);") - + Line(pass_texcoord + " = " + in_texcoord + ";") - + Line(pass_color + " = " + in_color + ";") + imgui_vertex.SetMainCode(std::string() + + Line(out_vertex + " = .5 *" + m_ortho.tostring() + " * vec4(" + in_position.tostring() + ", -1.0, 1.0);") + + Line(pass_texcoord + " = " + in_texcoord.tostring() + ";") + + Line(pass_color + " = " + in_color.tostring() + ";") ); ShaderBlock imgui_pixel("imgui_pixel"); imgui_pixel << m_texture << pass_texcoord << pass_color << out_pixel; - imgui_pixel.SetMainCode(String() + - Line(String() - + "vec4 col = " + pass_color + " * texture2D(" + m_texture + ", " + pass_texcoord + ");") - + Line(String() + imgui_pixel.SetMainCode(std::string() + + Line(std::string() + + "vec4 col = " + pass_color.tostring() + " * texture2D(" + m_texture.tostring() + ", " + pass_texcoord.tostring() + ");") + + Line(std::string() + "if (col.a == 0.0) discard; ") + Line(out_pixel + " = col;") ); @@ -135,12 +141,12 @@ LolImGui::LolImGui() InputProfile& ip = m_profile; ip.AddBindings(InputProfileType::Keyboard); //for (int i = LolImGuiKey::KEY_START; i < LolImGuiKey::KEY_END; ++i) - // m_profile << InputProfile::Keyboard(i, LolImGuiKey(i).ToString()); + // m_profile << InputProfile::Keyboard(i, LolImGuiKey(i).tostring()); for (int i = LolImGuiKey::MOUSE_KEY_START; i < LolImGuiKey::MOUSE_KEY_END; ++i) - m_profile << InputProfile::MouseKey(i, LolImGuiKey(i).ToString()); + m_profile << InputProfile::MouseKey(i, LolImGuiKey(i).tostring()); for (int i = LolImGuiAxis::MOUSE_AXIS_START; i < LolImGuiAxis::MOUSE_AXIS_END; ++i) - m_profile << InputProfile::MouseAxis(i, LolImGuiAxis(i).ToString()); + m_profile << InputProfile::MouseAxis(i, LolImGuiAxis(i).tostring()); Ticker::Ref(m_controller = new Controller("ImGui_Controller")); m_controller->Init(m_profile); @@ -221,20 +227,20 @@ void LolImGui::Shutdown() } //----------------------------------------------------------------------------- -String LolImGui::GetClipboard() +std::string LolImGui::GetClipboard() { return g_lolimgui ? g_lolimgui->m_clipboard : ""; } void LolImGui::SetClipboardCallback(void *data, const char* text) { - String *clipboard = (String *)data; + std::string *clipboard = (std::string *)data; *clipboard = text; } const char* LolImGui::GetClipboardCallback(void *data) { - String *clipboard = (String *)data; - return clipboard->C(); + std::string *clipboard = (std::string *)data; + return clipboard->c_str(); } //----------------------------------------------------------------------------- @@ -296,10 +302,10 @@ void LolImGui::TickGame(float seconds) m_keyboard->SetTextInputActive(io.WantTextInput); //Update text input - String text = m_keyboard->GetText(); + std::string text = m_keyboard->GetText(); //text.case_change(io.KeyShift); - for (int i = 0; i < text.count(); ++i) - io.AddInputCharacter(text[i]); + for (auto ch : text) + io.AddInputCharacter(ch); //Update mouse if (m_mouse) @@ -384,14 +390,14 @@ void LolImGui::RenderDrawListsMethod(ImDrawData* draw_data) //Create shader if (!m_shader) { - String code; + std::string code; m_builder.Build(code); m_shader = Shader::Create(m_builder.GetName(), code); ASSERT(m_shader); - m_ortho.m_uniform = m_shader->GetUniformLocation(m_ortho.m_var); - m_texture.m_uniform = m_shader->GetUniformLocation(m_texture.m_var); + m_ortho.m_uniform = m_shader->GetUniformLocation(m_ortho.m_var.tostring()); + m_texture.m_uniform = m_shader->GetUniformLocation(m_texture.m_var.tostring()); m_attribs << m_shader->GetAttribLocation(VertexUsage::Position, 0) diff --git a/src/lolimgui.h b/src/lolimgui.h index 3b82c4c9..d63b4f67 100644 --- a/src/lolimgui.h +++ b/src/lolimgui.h @@ -1,7 +1,8 @@ // -// Lol Engine — imGui integration +// Lol Engine // // Copyright © 2009—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,6 +13,12 @@ #pragma once +#include + +// +// The Imgui integration +// + #define IM_VEC2_CLASS_EXTRA ImVec2(const lol::vec2 &v) { x = v.x; y = v.y; } \ ImVec2(const lol::ivec2 &v) : ImVec2(lol::vec2(v)) { } \ operator lol::vec2() const { return lol::vec2(x, y); } \ @@ -105,7 +112,7 @@ class LolImGui : public Entity MAX = MOUSE_KEY_END, }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Tab] = g_name_key_Tab; enum_map[LeftArrow] = g_name_key_Left; @@ -155,7 +162,7 @@ class LolImGui : public Entity MAX = MOUSE_AXIS_END, }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[Scroll] = g_name_mouse_axis_scroll; @@ -175,7 +182,7 @@ public: static void Shutdown(); //------------------------------------------------------------------------- - static String GetClipboard(); + static std::string GetClipboard(); protected: virtual void TickGame(float seconds); @@ -191,9 +198,9 @@ protected: { Uniform() { } Uniform(ShaderVar var) { m_var = var; } - operator String() { return m_var; } - operator ShaderVar() { return m_var; } - operator ShaderUniform() { return m_uniform; } + std::string tostring() const { return m_var.tostring(); } + operator ShaderVar() const { return m_var; } + operator ShaderUniform() const { return m_uniform; } //-- ShaderVar m_var; ShaderUniform m_uniform; @@ -212,7 +219,7 @@ protected: InputDevice* m_keyboard = nullptr; InputProfile m_profile; //map m_keys; - String m_clipboard; + std::string m_clipboard; }; //----------------------------------------------------------------------------- diff --git a/src/lolua/baselua.cpp b/src/lolua/baselua.cpp index d1726472..394ad3f7 100644 --- a/src/lolua/baselua.cpp +++ b/src/lolua/baselua.cpp @@ -1,7 +1,7 @@ // -// Base Lua class for Lua script loading +// Lol Engine // -// Copyright © 2009—2015 Sam Hocevar +// Copyright © 2017—2018 Sam Hocevar // © 2009—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -14,9 +14,13 @@ #include #include -#include +#include #include -#include +#include + +// +// Base Lua class for Lua script loading +// namespace lol { @@ -36,14 +40,14 @@ class LuaBaseData } //Exec lua code ----------------------------------------------------------- - static int LuaDoCode(lua_State *l, String const& s) + static int LuaDoCode(lua_State *l, std::string const& s) { - int status = luaL_dostring(l, s.C()); + int status = luaL_dostring(l, s.c_str()); if (status == 1) { auto stack = LuaStack::Begin(l, -1); - auto error = stack.Get(); - msg::error("Lua error %s\n", error.C()); + auto error = stack.Get(); + msg::error("Lua error %s\n", error.c_str()); lua_pop(l, 1); } return status; @@ -65,7 +69,7 @@ class LuaBaseData f.Open(candidate, FileAccess::Read); if (f.IsValid()) { - String s = f.ReadString(); + std::string s = f.ReadString().C(); f.Close(); msg::debug("loading Lua file %s\n", candidate.C()); @@ -79,8 +83,8 @@ class LuaBaseData else if (status == 1) { stack.SetIndex(-1); - auto error = stack.Get(); - msg::error("Lua error %s\n", error.C()); + auto error = stack.Get(); + msg::error("Lua error %s\n", error.c_str()); lua_pop(l, 1); } @@ -150,15 +154,15 @@ void Loader::StoreObject(lua_State* l, Object* obj) } //----------------------------------------------------------------------------- -bool Loader::ExecLuaFile(String const &lua) +bool Loader::ExecLuaFile(std::string const &lua) { - lua_pushstring(m_lua_state, lua.C()); + lua_pushstring(m_lua_state, lua.c_str()); int status = LuaBaseData::LuaDoFile(m_lua_state); return status == 0; } //----------------------------------------------------------------------------- -bool Loader::ExecLuaCode(String const &lua) +bool Loader::ExecLuaCode(std::string const &lua) { return 0 == LuaBaseData::LuaDoCode(m_lua_state, lua); } diff --git a/src/lolua/baselua.h b/src/lolua/baselua.h index 0be1ac20..a8e749c7 100644 --- a/src/lolua/baselua.h +++ b/src/lolua/baselua.h @@ -1,7 +1,7 @@ // -// Lol Engine — base class for Lua script loading +// Lol Engine // -// Copyright © 2009—2015 Sam Hocevar +// Copyright © 2017—2018 Sam Hocevar // © 2009—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -11,10 +11,16 @@ // See http://www.wtfpl.net/ for more details. // +#pragma once + #include "3rdparty/lua/src/lua.hpp" //#include "lua/luawrapper.hpp" -#pragma once +#include + +// +// Base Lua class for Lua script loading +// namespace lol { @@ -42,20 +48,20 @@ public: typedef struct ClassVarStr { ClassVarStr() { } - ClassVarStr(String var_name, lua_CFunction get, lua_CFunction set) + ClassVarStr(std::string const &var_name, lua_CFunction get, lua_CFunction set) { - m_get_name = String("Get") + var_name; - m_set_name = String("Set") + var_name; + m_get_name = std::string("Get") + var_name; + m_set_name = std::string("Set") + var_name; m_get = get; m_set = set; } - String m_get_name = ""; - String m_set_name = ""; + std::string m_get_name = ""; + std::string m_set_name = ""; lua_CFunction m_get = nullptr; lua_CFunction m_set = nullptr; } ClassVarStr; - Library(String class_name, + Library(std::string const &class_name, array const& statics, array const& methods, array const& variables) @@ -89,9 +95,9 @@ public: || variables.last().set != nullptr) m_variables.push(ClassVarStr()); } - String m_class_name = ""; - String m_static_name = ""; - String m_method_name = ""; + std::string m_class_name = ""; + std::string m_static_name = ""; + std::string m_method_name = ""; array m_statics; array m_methods; array m_variables; @@ -172,11 +178,11 @@ public: //Add getter lua_pushcfunction(l, var.m_get); - lua_setfield(l, -2, var.m_get_name.C()); + lua_setfield(l, -2, var.m_get_name.c_str()); //Add setter lua_pushcfunction(l, var.m_set); - lua_setfield(l, -2, var.m_set_name.C()); + lua_setfield(l, -2, var.m_set_name.c_str()); } //Don't set it to global, but pop the stack to hide the metatable @@ -196,11 +202,11 @@ private: public: //------------------------------------------------------------------------- template - static const char* GetObjectName() { return GetLibrary()->m_class_name.C(); } + static const char* GetObjectName() { return GetLibrary()->m_class_name.c_str(); } template - static const char* GetStaticName() { return GetLibrary()->m_static_name.C(); } + static const char* GetStaticName() { return GetLibrary()->m_static_name.c_str(); } template - static const char* GetMethodName() { return GetLibrary()->m_method_name.C(); } + static const char* GetMethodName() { return GetLibrary()->m_method_name.c_str(); } template static const ClassMethod* GetStaticMethods() { return GetLibrary()->m_statics.data(); } template @@ -379,9 +385,9 @@ protected: #ifndef INNER_SAFE_ENUM template SafeEnum InnerDefaultSafeEnum() { return SafeEnum(); } - template bool InnerIsValidSafeEnum() { return InnerIsValid(); } - template SafeEnum InnerGetSafeEnum(SafeEnum value) { return FindValue >(InnerGet(value.ToString())); } - template int InnerPushSafeEnum(SafeEnum value) { return InnerPush(value.ToString()); } + template bool InnerIsValidSafeEnum() { return InnerIsValid(); } + template SafeEnum InnerGetSafeEnum(SafeEnum value) { return FindValue >(InnerGet(value.tostring())); } + template int InnerPushSafeEnum(SafeEnum value) { return InnerPush(value.tostring()); } #endif //STACK_STRING #ifndef INNER_PTR @@ -498,10 +504,10 @@ template<> inline int Stack::InnerPush(char const* value) { //----------------------------------------------------------------------------- #ifndef STACK_STRING -template<> inline String Stack::InnerDefault() { return String(); } -template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } -template<> inline String Stack::InnerGet(String value) { return String(InnerGet(value.C())); } -template<> inline int Stack::InnerPush(String value) { return InnerPush(value.C()); } +template<> inline std::string Stack::InnerDefault() { return ""; } +template<> inline bool Stack::InnerIsValid() { return InnerIsValid(); } +template<> inline std::string Stack::InnerGet(std::string value) { return std::string(InnerGet(value.c_str())); } +template<> inline int Stack::InnerPush(std::string value) { return InnerPush(value.c_str()); } #endif //STACK_STRING //----------------------------------------------------------------------------- @@ -577,15 +583,15 @@ public: Loader(); virtual ~Loader(); - bool ExecLuaFile(String const &lua); - bool ExecLuaCode(String const &lua); + bool ExecLuaFile(std::string const &lua); + bool ExecLuaCode(std::string const &lua); //------------------------------------------------------------------------- #define DECLARE_LOADER_GET(T0, T1, GET_NAME) \ template \ - T1 GET_NAME(String const &name) \ + T1 GET_NAME(std::string const &name) \ { \ - lua_getglobal(m_lua_state, name.C()); \ + lua_getglobal(m_lua_state, name.c_str()); \ auto stack = Lolua::Stack::Begin(m_lua_state, -1); \ auto result = stack.GET_NAME(); \ lua_pop(m_lua_state, 1); \ diff --git a/src/messageservice.cpp b/src/messageservice.cpp index b248b1cc..1644dd62 100644 --- a/src/messageservice.cpp +++ b/src/messageservice.cpp @@ -1,16 +1,19 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2013 Benjamin "Touky" Huet -// 2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2013—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #include +#include #include #include #include @@ -24,7 +27,7 @@ extern "C" { int C_Send(const char* message) { return (int)MessageService::Send(MessageBucket::AppIn, message); } //NOT IMPLEMENTED - //bool C_FetchFirst(String& message); + //bool C_FetchFirst(std::string& message); } #endif //EMSCRIPTEN @@ -59,31 +62,20 @@ void MessageService::Destroy() } //----------------------------------------------------------------------------- -bool MessageService::Send(MessageBucket id, const String& message) -{ - if (g_messageservice) - { - ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count()); - return g_messageservice->Send(id, message.C()); - } - return false; -} - -bool MessageService::Send(MessageBucket id, const char* message) +bool MessageService::Send(MessageBucket id, const std::string& message) { if (g_messageservice) { - ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count()); MessageService& g = *g_messageservice; array& bucket = g.m_bucket[id.ToScalar()]; - bucket << MessageList(time(nullptr), String(message)); + bucket << MessageList(time(nullptr), message); return true; } return false; } //---- -bool MessageService::FetchFirst(MessageBucket id, String& message) +bool MessageService::FetchFirst(MessageBucket id, std::string& message) { if (g_messageservice) { @@ -94,7 +86,7 @@ bool MessageService::FetchFirst(MessageBucket id, String& message) return false; } -bool MessageService::FetchFirst(MessageBucket id, String& message, time_t& timestamp) +bool MessageService::FetchFirst(MessageBucket id, std::string& message, time_t& timestamp) { if (g_messageservice) { @@ -114,7 +106,7 @@ bool MessageService::FetchFirst(MessageBucket id, String& message, time_t& times } //---- -bool MessageService::FetchAll(MessageBucket id, String& message) +bool MessageService::FetchAll(MessageBucket id, std::string& message) { if (g_messageservice) { @@ -125,14 +117,14 @@ bool MessageService::FetchAll(MessageBucket id, String& message) return false; } -bool MessageService::FetchAll(MessageBucket id, String& message, time_t& first_timestamp) +bool MessageService::FetchAll(MessageBucket id, std::string& message, time_t& first_timestamp) { if (g_messageservice) { ASSERT(0 <= id.ToScalar() && id.ToScalar() < g_messageservice->m_bucket.count()); MessageService& g = *g_messageservice; array& bucket = g.m_bucket[id.ToScalar()]; - message = String(""); + message = ""; if (bucket.count()) { diff --git a/src/messageservice.h b/src/messageservice.h index 572715b1..f9db0bde 100644 --- a/src/messageservice.h +++ b/src/messageservice.h @@ -1,19 +1,23 @@ // -// Lol Engine +// Lol Engine // -// Copyright: (c) 2013 Benjamin "Touky" Huet -// (c) 2013 Sam Hocevar -// This program is free software; you can redistribute it and/or -// modify it under the terms of the Do What The Fuck You Want To -// Public License, Version 2, as published by Sam Hocevar. See -// http://www.wtfpl.net/ for more details. +// Copyright © 2013—2015 Benjamin “Touky” Huet +// © 2017—2018 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. // #pragma once +#include + // // The Message Service class -// ---------------- +// ------------------------- // namespace lol @@ -41,7 +45,7 @@ struct MessageBucketBase : public StructSafeEnum MAX }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[AppIn] = "AppIn"; enum_map[AppOut] = "AppOut"; @@ -64,14 +68,14 @@ typedef SafeEnum MessageBucket; //Message list container with time in it struct MessageList { - MessageList(time_t timestamp, const String& message) + MessageList(time_t timestamp, const std::string& message) + : m_timestamp(timestamp), + m_message(message) { - m_timestamp = timestamp; - m_message = message; } - time_t m_timestamp; - String m_message; + time_t m_timestamp; + std::string m_message; }; /* @@ -91,12 +95,11 @@ public: static void Destroy(); //Common interactions - static bool Send(MessageBucket id, const String& message); - static bool Send(MessageBucket id, const char* message); - static bool FetchFirst(MessageBucket id, String& message); - static bool FetchFirst(MessageBucket id, String& message, time_t ×tamp); - static bool FetchAll(MessageBucket id, String& message); - static bool FetchAll(MessageBucket id, String& message, time_t &first_timestamp); + static bool Send(MessageBucket id, const std::string& message); + static bool FetchFirst(MessageBucket id, std::string& message); + static bool FetchFirst(MessageBucket id, std::string& message, time_t ×tamp); + static bool FetchAll(MessageBucket id, std::string& message); + static bool FetchAll(MessageBucket id, std::string& message, time_t &first_timestamp); private: array > m_bucket; diff --git a/src/t/base/enum.cpp b/src/t/base/enum.cpp index 08d9934a..02f7eaf5 100644 --- a/src/t/base/enum.cpp +++ b/src/t/base/enum.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -14,6 +14,8 @@ #include +#include + namespace lol { @@ -31,7 +33,7 @@ lolunit_declare_fixture(enum_test) }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[first] = "first"; enum_map[second] = "second"; @@ -42,18 +44,18 @@ lolunit_declare_fixture(enum_test) typedef SafeEnum my_enum; my_enum e = my_enum::first; - lolunit_assert(e.ToString() == "first"); + lolunit_assert(e.tostring() == "first"); e = my_enum::second; - lolunit_assert(e.ToString() == "second"); + lolunit_assert(e.tostring() == "second"); e = my_enum::third; - lolunit_assert(e.ToString() == "third"); + lolunit_assert(e.tostring() == "third"); e = my_enum(42); - lolunit_assert(e.ToString() != "first"); - lolunit_assert(e.ToString() != "second"); - lolunit_assert(e.ToString() != "third"); + lolunit_assert(e.tostring() != "first"); + lolunit_assert(e.tostring() != "second"); + lolunit_assert(e.tostring() != "third"); } }; diff --git a/src/t/base/string.cpp b/src/t/base/string.cpp index 677f137f..6d8b279a 100644 --- a/src/t/base/string.cpp +++ b/src/t/base/string.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2014—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -13,6 +13,8 @@ #include +#include + #include namespace lol @@ -20,130 +22,6 @@ namespace lol lolunit_declare_fixture(string_test) { - lolunit_declare_test(string_build) - { - String s1; - lolunit_assert_equal(s1.count(), 0); - lolunit_assert_equal(s1[0], '\0'); - - String s2(""); - lolunit_assert_equal(s2.count(), 0); - lolunit_assert_equal(s2[0], '\0'); - - String s3("a"); - lolunit_assert_equal(s3.count(), 1); - lolunit_assert_equal(s3[0], 'a'); - lolunit_assert_equal(s3[1], '\0'); - - String s4(s3); - lolunit_assert_equal(s4.count(), 1); - lolunit_assert_equal(s4[0], 'a'); - lolunit_assert_equal(s4[1], '\0'); - } - - lolunit_declare_test(string_append_char) - { - String s; - s += 'a'; - s += 'b'; - s += 'c'; - - lolunit_assert_equal(s[0], 'a'); - lolunit_assert_equal(s[1], 'b'); - lolunit_assert_equal(s[2], 'c'); - lolunit_assert_equal(s[3], '\0'); - } - - lolunit_declare_test(string_copy) - { - String s1 = "abc"; - - String s2 = s1; - - lolunit_assert_equal(s1[0], s2[0]); - lolunit_assert_equal(s1[1], s2[1]); - lolunit_assert_equal(s1[2], s2[2]); - lolunit_assert_equal(s1[3], s2[3]); - } - - lolunit_declare_test(string_concat) - { - String s1("ab"), s2("cd"); - - String s3 = s1 + s2; - lolunit_assert_equal(s3[0], 'a'); - lolunit_assert_equal(s3[1], 'b'); - lolunit_assert_equal(s3[2], 'c'); - lolunit_assert_equal(s3[3], 'd'); - lolunit_assert_equal(s3[4], '\0'); - } - - lolunit_declare_test(string_append_string) - { - String s1("ab"), s2("cd"); - - s1 += s2; - lolunit_assert_equal(s1.count(), 4); - lolunit_assert_equal(s1[0], 'a'); - lolunit_assert_equal(s1[1], 'b'); - lolunit_assert_equal(s1[2], 'c'); - lolunit_assert_equal(s1[3], 'd'); - lolunit_assert_equal(s1[4], '\0'); - - s2 += s2; - lolunit_assert_equal(s2.count(), 4); - lolunit_assert_equal(s2[0], 'c'); - lolunit_assert_equal(s2[1], 'd'); - lolunit_assert_equal(s2[2], 'c'); - lolunit_assert_equal(s2[3], 'd'); - lolunit_assert_equal(s2[4], '\0'); - } - - lolunit_declare_test(string_equal) - { - String s1("abc"); - String s2("abc"); - String s3("ab"); - - lolunit_assert(s1 == s2); - lolunit_assert(!(s1 == s3)); - } - - lolunit_declare_test(string_different) - { - String s1("abc"); - String s2("ab"); - String s3("abc"); - - lolunit_assert(s1 != s2); - lolunit_assert(!(s1 != s3)); - } - - lolunit_declare_test(string_chars_equal) - { - char const* sz = "abc"; - String s1("abc"); - String s2("ab"); - - lolunit_assert(s1 == sz); - lolunit_assert(sz == s1); - lolunit_assert(!(s2 == sz)); - lolunit_assert(!(sz == s2)); - } - - lolunit_declare_test(string_chars_different) - { - char const* sz = "abc"; - String s1("ab"); - String s2("abc"); - - lolunit_assert(s1 != sz); - lolunit_assert(sz != s1); - lolunit_assert(!(s2 != sz)); - lolunit_assert(!(sz != s2)); - } - - lolunit_declare_test(string_format) { std::string s1 = "3a"; @@ -157,113 +35,42 @@ lolunit_declare_fixture(string_test) lolunit_assert(s3 == s4); } - lolunit_declare_test(substring) - { - String s1 = "Hello World"; - - String s2 = s1.sub(0, 5); - String s3 = "Hello"; - lolunit_assert(s2 == s3); - - String s4 = s1.sub(6, 5); - String s5 = "World"; - lolunit_assert(s4 == s5); - } - - lolunit_declare_test(index_of) - { - String s1 = "Hello World"; - int i1 = s1.index_of('H'); - int i2 = s1.index_of('W'); - int i3 = s1.index_of('d'); - int i4 = s1.index_of("Hello"); - int i5 = s1.index_of("World"); - int i6 = s1.index_of("lo"); - int i7 = s1.index_of("Hello World"); - int i8 = s1.index_of("Sup' dude"); - - lolunit_assert(i1 == 0); - lolunit_assert(i2 == 6); - lolunit_assert(i3 == 10); - lolunit_assert(i4 == i1); - lolunit_assert(i5 == i2); - lolunit_assert(i6 == 3); - lolunit_assert(i7 == 0); - lolunit_assert(i8 == -1); - } - - lolunit_declare_test(last_index_of) - { - String s1 = "Hello World"; - int i1 = s1.last_index_of('H'); - int i2 = s1.last_index_of('W'); - int i3 = s1.last_index_of('d'); - int i4 = s1.last_index_of("Hello"); - int i5 = s1.last_index_of("World"); - int i6 = s1.last_index_of("lo"); - int i7 = s1.last_index_of("Hello World"); - int i8 = s1.last_index_of("Sup' dude"); - int i9 = s1.last_index_of('l'); - - lolunit_assert(i1 == 0); - lolunit_assert(i2 == 6); - lolunit_assert(i3 == 10); - lolunit_assert(i4 == i1); - lolunit_assert(i5 == i2); - lolunit_assert(i6 == 3); - lolunit_assert(i7 == 0); - lolunit_assert(i8 == -1); - lolunit_assert(i9 == 9); - } - lolunit_declare_test(starts_ends_with) { - String s = "lolilol"; - lolunit_assert(s.starts_with("loli")); - lolunit_assert(!s.starts_with("lolo")); - lolunit_assert(!s.starts_with("lolilolilol")); - lolunit_assert(s.ends_with("ilol")); - lolunit_assert(!s.ends_with("olol")); - lolunit_assert(!s.ends_with("lolilolilol")); - } + std::string s = "lolilol"; - lolunit_declare_test(string_compare) - { - String s1 = "lolilol"; - String s2 = s1; - String s3 = "trololol"; - String s4 = "lolilololol"; + lolunit_assert(starts_with(s, "loli")); + lolunit_assert(!starts_with(s, "lolo")); + lolunit_assert(!starts_with(s, "lolilolilol")); - lolunit_assert(!(s1 < s2)); - lolunit_assert(!(s2 < s1)); - lolunit_assert(s1 < s3); - lolunit_assert(s1 < s4); - lolunit_assert(!(s4 < s1)); + lolunit_assert(ends_with(s, "ilol")); + lolunit_assert(!ends_with(s, "olol")); + lolunit_assert(!ends_with(s, "lolilolilol")); } lolunit_declare_test(string_split) { - auto l1 = String("abc").split(); + auto l1 = split(std::string("abc")); lolunit_assert(l1.count() == 1); lolunit_assert(l1[0] == "abc"); - auto l2 = String("\nabc").split(); + auto l2 = split(std::string("\nabc")); lolunit_assert(l2.count() == 2); lolunit_assert(l2[0] == ""); lolunit_assert(l2[1] == "abc"); - auto l3 = String("abc\n").split(); + auto l3 = split(std::string("abc\n")); lolunit_assert(l3.count() == 2); lolunit_assert(l3[0] == "abc"); lolunit_assert(l3[1] == ""); - auto l4 = String("\n\n").split(); + auto l4 = split(std::string("\n\n")); lolunit_assert(l4.count() == 3); lolunit_assert(l4[0] == ""); lolunit_assert(l4[1] == ""); lolunit_assert(l4[2] == ""); - auto l5 = String("abc\nde\n\nf\n").split(); + auto l5 = split(std::string("abc\nde\n\nf\n")); lolunit_assert(l5.count() == 5); lolunit_assert(l5[0] == "abc"); lolunit_assert(l5[1] == "de"); diff --git a/src/t/sys/thread.cpp b/src/t/sys/thread.cpp index b47d773d..fde42d42 100644 --- a/src/t/sys/thread.cpp +++ b/src/t/sys/thread.cpp @@ -1,7 +1,7 @@ // // Lol Engine — Unit tests // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2014—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -13,6 +13,8 @@ #include +#include + #include namespace lol @@ -61,7 +63,7 @@ lolunit_declare_fixture(thread_test) DONE, }; protected: - virtual bool BuildEnumMap(map& enum_map) + virtual bool BuildEnumMap(map& enum_map) { enum_map[NOT_QUEUED] = "NOT_QUEUED"; enum_map[QUEUED] = "QUEUED"; @@ -99,7 +101,7 @@ lolunit_declare_fixture(thread_test) case UnitTestStatus::NOT_QUEUED: if (!!GetDispatchCount()) { - msg::info("%s TICKGAME %s\n", GetName(), m_status.ToString().C()); + msg::info("%s TICKGAME %s\n", GetName(), m_status.tostring().c_str()); m_status = UnitTestStatus::QUEUED; } break; @@ -110,14 +112,14 @@ lolunit_declare_fixture(thread_test) if (GetDispatchedCount()) #endif { - msg::info("%s TICKGAME %s\n", GetName(), m_status.ToString().C()); + msg::info("%s TICKGAME %s\n", GetName(), m_status.tostring().c_str()); m_status = UnitTestStatus::RETRIEVED; } break; case UnitTestStatus::RETRIEVED: if (m_job_result.count() == 4) { - msg::info("%s TICKGAME %s\n", GetName(), m_status.ToString().C()); + msg::info("%s TICKGAME %s\n", GetName(), m_status.tostring().c_str()); m_status = UnitTestStatus::DONE; } break; diff --git a/src/utils.h b/src/utils.h index 5929394e..5113268e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2015 Sam Hocevar +// Copyright © 2010—2018 Sam Hocevar // © 2012—2015 Benjamin “Touky” Huet // // Lol Engine is free software. It comes without any warranty, to @@ -15,11 +15,13 @@ // // Various basic convenience functions -// ------------------ +// ----------------------------------- // #include +#include + namespace lol { @@ -60,22 +62,12 @@ template< class T > inline int GetRandom(array src) } // Gets the value for the given enum type. -template inline T FindValue(const char* name) -{ - auto str = String(name); - return FindValue(str); -} - -template inline T FindValue(String const& name) +template inline T FindValue(std::string const& name) { - String n = name; - n.to_lower(); + std::string needle = tolower(name); for (int i = 0; i < T::Max; ++i) - { - String s = T(i).ToString().to_lower(); - if (s.contains(n)) + if (tolower(T(i).tostring()).find(needle) != std::string::npos) return T(i); - } return T::Max; }