From 95641e549f2477585a107ed777ccb1fe11d601f2 Mon Sep 17 00:00:00 2001 From: WantSpeaking <39407700+WantSpeaking@users.noreply.github.com> Date: Tue, 18 Jun 2024 21:35:52 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BC=A0=E6=A0=87?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E3=80=81=E7=82=B9=E5=87=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdlms/Components/C_Cursor.cpp | 128 +++++++++++++++++++++++++ sdlms/Components/C_Cursor.h | 49 ++++++++++ sdlms/Core/EntityBuilder/EB_Cursor.cpp | 0 sdlms/Core/EntityBuilder/EB_Cursor.h | 27 ++++++ sdlms/Resource/Wz.cpp | 2 +- sdlms/Systems/S_Cursor.cpp | 48 ++++++++++ sdlms/Systems/S_Cursor.h | 27 ++++++ sdlms/main.cpp | 10 ++ 8 files changed, 290 insertions(+), 1 deletion(-) create mode 100644 sdlms/Components/C_Cursor.cpp create mode 100644 sdlms/Components/C_Cursor.h create mode 100644 sdlms/Core/EntityBuilder/EB_Cursor.cpp create mode 100644 sdlms/Core/EntityBuilder/EB_Cursor.h create mode 100644 sdlms/Systems/S_Cursor.cpp create mode 100644 sdlms/Systems/S_Cursor.h diff --git a/sdlms/Components/C_Cursor.cpp b/sdlms/Components/C_Cursor.cpp new file mode 100644 index 00000000..0c335c51 --- /dev/null +++ b/sdlms/Components/C_Cursor.cpp @@ -0,0 +1,128 @@ +#include "C_Cursor.h" + +#include "String.h" + +#include "Components/Sprite.h" +#include "Components/Transform.h" +#include "Components/RelativeTransform.h" +#include "Components/RandomInput.h" +#include "Components/LimitTransform.h" +#include "Components/Line.h" +#include "Components/Physic/Normal.h" +#include "Resource/Wz.h" + +C_Cursor::C_Cursor(wz::Node *node, int id, int rx0, int rx1, World *world) +{ + +} + +// 测试 +C_Cursor::C_Cursor(World *world) +{ + auto node_cursor = world->get_resource().UI->get_root()->find_from_path(u"Basic.img/Cursor"); + + for (auto &[name, val] : node_cursor->get_children()) + { + if (name != u"info") + { + auto aspr = new AnimatedSprite(val[0]); + auto id = std::stoi(std::string{name.begin(), name.end()}); + aspr_map[static_cast(id)] = aspr; + world->add_component(aspr); + } + } + + act = State::IDLE; + + /* add_component(aspr_map[state]); + + Transform *tr = new Transform(); + add_component(tr); + world->add_component(tr, 3000000); */ +} + +/* void C_Cursor::switch_act(State s) +{ + if (s != state) + { + if (aspr_map.contains(s)) + //if (aspr_map1.contains("")) + { + auto aspr = aspr_map[s]; + aspr->set_anim_index(0); + aspr->set_anim_time(0); + state = s; + add_component(aspr); + } + } +} + +void C_Cursor::start_animate() +{ + get_component()->set_animate(true); +} + +void C_Cursor::stop_animate() +{ + get_component()->set_animate(false); +} +*/ + +C_Cursor::~C_Cursor() +{ + /* auto world = World::get_world(); + + for (auto &[key, val] : aspr_map) + { + auto aspr = val; + world->destroy_component(aspr, false); + delete aspr; + } + + for (auto &[key, val] : get_entity()) + { + auto t = val->get_component(); + auto rtr = val->get_component(); + world->destroy_component(t, false); + world->destroy_component(rtr, false); + delete t; + delete rtr; + delete val; + } + + for (auto &[key, val] : get_entity()) + { + auto t = val->get_component(); + auto rtr = val->get_component(); + world->destroy_component(t, false); + world->destroy_component(rtr, false); + delete t; + delete rtr; + delete val; + } + + auto t = get_component(); + world->destroy_component(t, false); + delete t; + + auto nor = get_component(); + if (nor != nullptr) + { + world->destroy_component(nor, false); + delete nor; + } + + auto ran = get_component(); + if (ran != nullptr) + { + world->destroy_component(ran, false); + delete ran; + } + + auto ltr = get_component(); + if (ltr != nullptr) + { + world->destroy_component(ltr, false); + delete ltr; + } */ +} diff --git a/sdlms/Components/C_Cursor.h b/sdlms/Components/C_Cursor.h new file mode 100644 index 00000000..2e8eec9b --- /dev/null +++ b/sdlms/Components/C_Cursor.h @@ -0,0 +1,49 @@ +#pragma once +#include "Component.h" +#include "wz/Property.hpp" +#include "Core/World.h" +#include "Components/AnimatedSprite.h" + +class C_Cursor : public Component +{ + public: +enum State + { + IDLE, + CANCLICK, + GAME, + HOUSE, + CANCLICK2, + CANGRAB, + GIFT, + VSCROLL, + HSCROLL, + VSCROLLIDLE, + HSCROLLIDLE, + GRABBING, + CLICKING, + RCLICK, + LEAF = 18, + CHATBARVDRAG = 67, + CHATBARHDRAG, + CHATBARBLTRDRAG, + CHATBARMOVE = 72, + CHATBARBRTLDRAG, + LENGTH + }; + + C_Cursor(wz::Node *node, int id, int rx0, int rx1, World *world); + C_Cursor(World *world); + ~C_Cursor(); + + void switch_act(State s); + void stop_animate(); + void start_animate(); + +public: + std::unordered_map str_map; + State act = State::IDLE; + std::unordered_map aspr_map; + std::unordered_map aspr_map1; + //State state = State::IDLE; +}; \ No newline at end of file diff --git a/sdlms/Core/EntityBuilder/EB_Cursor.cpp b/sdlms/Core/EntityBuilder/EB_Cursor.cpp new file mode 100644 index 00000000..e69de29b diff --git a/sdlms/Core/EntityBuilder/EB_Cursor.h b/sdlms/Core/EntityBuilder/EB_Cursor.h new file mode 100644 index 00000000..2215da7e --- /dev/null +++ b/sdlms/Core/EntityBuilder/EB_Cursor.h @@ -0,0 +1,27 @@ +#pragma once + +#include "wz/Property.hpp" +#include "Core/World.h" +#include "Components/AnimatedSprite.h" +#include "Components/C_Cursor.h" +#include "Components/Transform.h" + +class EB_Cursor +{ +public: + static Entity* BuildEntity(World *world) + { + Entity *entity = new Entity(); + + C_Cursor *c_Cursor = new C_Cursor(world); + Transform *c_T = new Transform(0,0,0,true); + + entity->add_component(c_Cursor->aspr_map[c_Cursor->act]); + entity->add_component(c_Cursor); + entity->add_component(c_T); + + world->add_component(c_T, 3000000); + world->add_component(c_Cursor); + return entity; + } +}; \ No newline at end of file diff --git a/sdlms/Resource/Wz.cpp b/sdlms/Resource/Wz.cpp index 8efed9ca..b160e7e6 100644 --- a/sdlms/Resource/Wz.cpp +++ b/sdlms/Resource/Wz.cpp @@ -27,7 +27,7 @@ Wz::Wz(const std::string &filename_prefix) String = (new wz::File(iv, (std::string(filename_prefix) + std::string("String.wz")).c_str())); // TamingMob = (new wz::File(iv, (std::string(filename_prefix) + std::string("TamingMob.wz")).c_str())); UI = (new wz::File(iv, (std::string(filename_prefix) + std::string("UI.wz")).c_str())); -#endif + // Base->parse(); Character->parse(u"Character"); // Effect->parse(); diff --git a/sdlms/Systems/S_Cursor.cpp b/sdlms/Systems/S_Cursor.cpp new file mode 100644 index 00000000..e37b8372 --- /dev/null +++ b/sdlms/Systems/S_Cursor.cpp @@ -0,0 +1,48 @@ +#include "S_Cursor.h" +#include +#include +#include +#include "Components/C_Cursor.h" +#include "Core/Input.h" + +void S_Cursor::run(World &world) +{ + if (world.components_exist_of_type()) + { + for (auto &[index, pla] : world.get_components()) + { + if (Input::is_mouse_button_pressed(Input::MouseButton::ButtonLeft)) + { + switch_act(pla, C_Cursor::State::CLICKING); + } + else if (Input::is_mouse_button_released(Input::MouseButton::ButtonLeft)) + { + switch_act(pla, C_Cursor::State::IDLE); + } + + update_Transform(pla->get_owner_component()); + } + } +} + +void S_Cursor::switch_act(C_Cursor *cCursor, C_Cursor::State action) +{ + if (action != cCursor->act) + { + // if (cCursor->aspr_map.contains(action)) + { + auto aspr = cCursor->aspr_map[action]; + aspr->set_anim_index(0); + aspr->set_anim_time(0); + cCursor->act = action; + cCursor->get_owner()->add_component(aspr); + } + } +} + +void S_Cursor::update_Transform(Transform *t) +{ + int x, y; + Uint32 mouse_state = SDL_GetMouseState(&x, &y); + t->set_position(SDL_FPoint(x, y)); +} \ No newline at end of file diff --git a/sdlms/Systems/S_Cursor.h b/sdlms/Systems/S_Cursor.h new file mode 100644 index 00000000..b3e05aaa --- /dev/null +++ b/sdlms/Systems/S_Cursor.h @@ -0,0 +1,27 @@ +#pragma once +#include "Systems/System.h" +#include "Components/AnimatedSprite.h" +#include "Components/Transform.h" +#include "Components/HVMove.h" +#include "Components/Avatar.h" + +#include "Entities/Portal.h" + +#include "S_Cursor.h" +#include +#include +#include +#include "Components/C_Cursor.h" +#include "Core/Input.h" + +class S_Cursor : public System +{ +public: + void run(World &world) override; + +private: + bool update_animated_sprite(AnimatedSprite *aspr, int delta_time, World &world); + void update_avatar(Avatar *hvm, int delta_time, World &world); + void switch_act(C_Cursor* cCursor, C_Cursor::State action); + void update_Transform(Transform* cCursor); +}; diff --git a/sdlms/main.cpp b/sdlms/main.cpp index 96ec52d3..37c1a259 100644 --- a/sdlms/main.cpp +++ b/sdlms/main.cpp @@ -9,6 +9,7 @@ #include "Systems/InputSystem.h" #include "Systems/SpriteSystem.h" #include "Systems/TransformSystem.h" +#include "Systems/S_Cursor.h" #include "Components/Sound.h" #include "Components/Camera.h" #include "Components/Player.h" @@ -33,6 +34,7 @@ #include "Timer.h" #include "Constants.h" #include "Configuration.h" +#include "Core/EntityBuilder/EB_Cursor.h" namespace ms { @@ -171,6 +173,7 @@ int main(int argc, char *argv[]) #else Wz *wz = new Wz("./Data/"); FreeType::init("./Data/"); + SDL_ShowCursor(SDL_DISABLE); #endif world.add_resource(wz); @@ -191,6 +194,7 @@ int main(int argc, char *argv[]) world.add_system(new TransformSystem()); world.add_system(new RenderSystem()); + world.add_system(new S_Cursor()); Camera *camera = new Camera(0, 0, width, height); Player *pla = new Player(); // 玩家控制的角色 @@ -206,6 +210,12 @@ int main(int argc, char *argv[]) world.add_component(nor); world.add_component(pla); } +#ifndef __ANDROID__ + + Entity* cursor = EB_Cursor::BuildEntity(&world); + world.add_entity(cursor); +#endif + // { // Mob *mob = new Mob(&world, {0, 0}); // Normal *nor = new Normal(); From d1e9d364b75d24b429e35e234761213b4b4b2a4a Mon Sep 17 00:00:00 2001 From: WantSpeaking <39407700+WantSpeaking@users.noreply.github.com> Date: Tue, 18 Jun 2024 21:44:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=9C=A8=20=E5=88=9B=E5=BB=BA=E7=AA=97?= =?UTF-8?q?=E5=8F=A3=E4=B9=8B=E5=90=8E=E8=B0=83=E7=94=A8SDL=5FShowCursor(S?= =?UTF-8?q?DL=5FDISABLE);=E6=89=8D=E6=9C=89=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdlms/main.cpp | 109 ++----------------------------------------------- 1 file changed, 3 insertions(+), 106 deletions(-) diff --git a/sdlms/main.cpp b/sdlms/main.cpp index 37c1a259..f717bc7e 100644 --- a/sdlms/main.cpp +++ b/sdlms/main.cpp @@ -36,111 +36,6 @@ #include "Configuration.h" #include "Core/EntityBuilder/EB_Cursor.h" -namespace ms -{ - Error init() - { - /* if (Error error = Singleton::get().init()) - return error; */ - - UI::get().init(); - - return Error::NONE; - } - - void update() - { - UI::get().update(); - } - - void draw(float alpha) - { - UI::get().draw(alpha); - } - - bool running() - { - return UI::get().not_quitted(); - } - - void loop() - { - Timer::get().start(); - - int64_t timestep = Constants::TIMESTEP * 1000; - int64_t accumulator = timestep; - - int64_t period = 0; - int32_t samples = 0; - - bool show_fps = Configuration::get().get_show_fps(); - - while (running()) - { - int64_t elapsed = Timer::get().stop(); - - // Update game with constant timestep as many times as possible. - for (accumulator += elapsed; accumulator >= timestep; accumulator -= timestep) - update(); - - // Draw the game. Interpolate to account for remaining time. - float alpha = static_cast(accumulator) / timestep; - draw(alpha); - - if (show_fps) - { - if (samples < 100) - { - period += elapsed; - samples++; - } - else if (period) - { - int64_t fps = (samples * 1000000) / period; - - std::cout << "FPS: " << fps << std::endl; - - period = 0; - samples = 0; - } - - - - } - } - } - - void start() - { - // Initialize and check for errors - if (Error error = init()) - { - const char *message = error.get_message(); - const char *args = error.get_args(); - bool can_retry = error.can_retry(); - - std::cout << "Error: " << message << std::endl; - - if (args && args[0]) - std::cout << "Message: " << args << std::endl; - - if (can_retry) - std::cout << "Enter 'retry' to try again." << std::endl; - - std::string command; - std::cin >> command; - - if (can_retry && command == "retry") - start(); - } - else - { - loop(); - } - } - -} - int width = 800; int height = 600; @@ -173,7 +68,7 @@ int main(int argc, char *argv[]) #else Wz *wz = new Wz("./Data/"); FreeType::init("./Data/"); - SDL_ShowCursor(SDL_DISABLE); + #endif world.add_resource(wz); @@ -214,6 +109,8 @@ int main(int argc, char *argv[]) Entity* cursor = EB_Cursor::BuildEntity(&world); world.add_entity(cursor); + + SDL_ShowCursor(SDL_DISABLE); #endif // {