diff --git a/kernel/components/state_machine.cc b/kernel/components/state_machine.cc index d1c4e96..310afbc 100644 --- a/kernel/components/state_machine.cc +++ b/kernel/components/state_machine.cc @@ -180,4 +180,14 @@ void StateMachine::initArgument(luabridge::LuaRef& ref, const typename resources } } +luabridge::LuaRef StateMachine::getComponent(Object* object, const std::string& name) { + auto machine = object->getComponent(name); + if (machine == nullptr) { + return luabridge::LuaRef(script::LuaState::getInstance()->getState()); + } + else { + return machine->_instance; + } +} + } // namespace ngind::components \ No newline at end of file diff --git a/kernel/components/state_machine.h b/kernel/components/state_machine.h index c4e97f4..899cd00 100644 --- a/kernel/components/state_machine.h +++ b/kernel/components/state_machine.h @@ -120,6 +120,8 @@ class StateMachine : public Component { inline luabridge::LuaRef getInstance() { return _instance; } + + static luabridge::LuaRef getComponent(Object* object, const std::string& name); private: /** * The instance of this component in lua environment. @@ -154,6 +156,7 @@ NGIND_LUA_BRIDGE_REGISTRATION(StateMachine) { .addFunction("halt", &StateMachine::halt) .addFunction("notify", &StateMachine::notify) .addFunction("notifyAll", &StateMachine::notifyAll) + .addStaticFunction("getComponent", &StateMachine::getComponent) .endClass() .endNamespace(); diff --git a/kernel/objects/object.h b/kernel/objects/object.h index aeff33e..3d73ab0 100644 --- a/kernel/objects/object.h +++ b/kernel/objects/object.h @@ -107,6 +107,8 @@ class Object : public memory::AutoCollectionObject, public UpdatableObject { _components[name] = component; component->addReference(); component->setParent(this); + } else { + // TODO: } } diff --git a/kernel/objects/world.cc b/kernel/objects/world.cc index 7e4efdd..83eb753 100644 --- a/kernel/objects/world.cc +++ b/kernel/objects/world.cc @@ -37,7 +37,7 @@ namespace ngind::objects { World::World(std::string name) : Object(), _name(std::move(name)), _config(nullptr), _background_color() { - _config = resources::ResourcesManager::getInstance()->load("worlds/world-" + _name + ".json"); + _config = resources::ResourcesManager::getInstance()->load("worlds/" + _name + ".json"); _background_color = rendering::Color(_config->getDocument()["background-color"].GetString()); ui::EventSystem::getInstance()->init(); diff --git a/kernel/rendering/program.cc b/kernel/rendering/program.cc index 4633c4a..31c0a87 100644 --- a/kernel/rendering/program.cc +++ b/kernel/rendering/program.cc @@ -30,8 +30,11 @@ namespace ngind::rendering { Program::Program(const std::string& program_name) { - _vs = resources::ResourcesManager::getInstance()->load(program_name + ".vs"); - _fs = resources::ResourcesManager::getInstance()->load(program_name + ".frag"); + auto manager = resources::ResourcesManager::getInstance(); + _program_config = manager->load("programs/" + program_name + ".json"); + + _vs = manager->load(std::string{_program_config->getDocument()["vertex"].GetString()} + ".vs"); + _fs = manager->load(std::string{_program_config->getDocument()["fragment"].GetString()} + ".frag"); this->_program = glCreateProgram(); glAttachShader(this->_program, _vs->getShader()); @@ -47,6 +50,11 @@ Program::Program(const std::string& program_name) { Program::~Program() { glDeleteProgram(this->_program); + + auto manager = resources::ResourcesManager::getInstance(); + manager->release(_vs); + manager->release(_fs); + manager->release(_program_config); } GLint Program::getUniform(const std::string& name) const { @@ -58,4 +66,88 @@ GLint Program::getUniform(const std::string& name) const { return res; } +void Program::prepare() { + auto args = _program_config->getDocument()["args"].GetArray(); + for (const auto& arg : args) { + std::string name = arg["name"].GetString(); + std::string type = arg["type"].GetString(); + + if (type == "float") { + this->setFloat(name, arg["value"].GetFloat()); + } + else if (type == "float2") { + // TODO: + } + else if (type == "float3") { + + } + else if (type == "float4") { + + } + else if (type == "int") { + + } + else if (type == "int2") { + + } + else if (type == "int3") { + + } + else if (type == "int4") { + + } + else if (type == "int") { + + } + else if (type == "int2") { + + } + else if (type == "int3") { + + } + else if (type == "int4") { + + } + else if (type == "unsigned") { + + } + else if (type == "unsigned2") { + + } + else if (type == "unsigned3") { + + } + else if (type == "unsigned4") { + + } + else if (type == "matrix2") { + + } + else if (type == "matrix3") { + + } + else if (type == "matrix4") { + + } + else if (type == "matrix23") { + + } + else if (type == "matrix24") { + + } + else if (type == "matrix32") { + + } + else if (type == "matrix34") { + + } + else if (type == "matrix42") { + + } + else if (type == "matrix43") { + + } + } +} + } // namespace ngind::rendering \ No newline at end of file diff --git a/kernel/rendering/program.h b/kernel/rendering/program.h index 6801fb5..5be1f6e 100644 --- a/kernel/rendering/program.h +++ b/kernel/rendering/program.h @@ -29,6 +29,7 @@ #include #include "resources/shader_resource.h" +#include "resources/config_resource.h" #include "glm/glm.hpp" #include "glm/gtc/type_ptr.hpp" @@ -43,7 +44,7 @@ class Program { /** * @param name: program's name */ - explicit Program(const std::string& name); + explicit Program(const std::string& program_name); ~Program(); @@ -168,6 +169,8 @@ class Program { glUniformMatrix4x3fv(getUniform(name), 1, GL_FALSE, glm::value_ptr(m)); } + void prepare(); + private: /** * The index of program @@ -183,6 +186,8 @@ class Program { * Reference of segment shader */ resources::ShaderResource* _fs; + + resources::ConfigResource* _program_config; }; } // namespace ngind::rendering diff --git a/kernel/rendering/rendering_command.h b/kernel/rendering/rendering_command.h index a57345c..ec8d0a8 100644 --- a/kernel/rendering/rendering_command.h +++ b/kernel/rendering/rendering_command.h @@ -116,6 +116,8 @@ class RenderingCommand : public memory::AutoCollectionObject { color.b / 255.0f, color.a / 255.0f); this->getProgram()->setMatrix4("projection", Camera::getInstance()->getProjection()); this->getProgram()->setMatrix4("model", this->getModel()); + + this->getProgram()->prepare(); } private: /** diff --git a/resources/config/programs/opaque.json b/resources/config/programs/opaque.json new file mode 100644 index 0000000..d99e6f3 --- /dev/null +++ b/resources/config/programs/opaque.json @@ -0,0 +1,11 @@ +{ + "vertex": "sprite", + "fragment": "opaque", + "args": [ + { + "name": "opaque", + "type": "float", + "value": 0.6 + } + ] +} \ No newline at end of file diff --git a/resources/config/programs/sprite.json b/resources/config/programs/sprite.json new file mode 100644 index 0000000..62e0b6a --- /dev/null +++ b/resources/config/programs/sprite.json @@ -0,0 +1,5 @@ +{ + "vertex": "sprite", + "fragment": "sprite", + "args": [] +} \ No newline at end of file diff --git a/resources/config/programs/text.json b/resources/config/programs/text.json new file mode 100644 index 0000000..496bb5a --- /dev/null +++ b/resources/config/programs/text.json @@ -0,0 +1,5 @@ +{ + "vertex": "text", + "fragment": "text", + "args": [] +} \ No newline at end of file diff --git a/resources/config/worlds/world-animation.json b/resources/config/worlds/animation.json similarity index 100% rename from resources/config/worlds/world-animation.json rename to resources/config/worlds/animation.json diff --git a/resources/config/worlds/world-archive.json b/resources/config/worlds/archive.json similarity index 100% rename from resources/config/worlds/world-archive.json rename to resources/config/worlds/archive.json diff --git a/resources/config/worlds/world-audio.json b/resources/config/worlds/audio.json similarity index 100% rename from resources/config/worlds/world-audio.json rename to resources/config/worlds/audio.json diff --git a/resources/config/worlds/world-button.json b/resources/config/worlds/button.json similarity index 100% rename from resources/config/worlds/world-button.json rename to resources/config/worlds/button.json diff --git a/resources/config/worlds/world-camera.json b/resources/config/worlds/camera.json similarity index 100% rename from resources/config/worlds/world-camera.json rename to resources/config/worlds/camera.json diff --git a/resources/config/worlds/world-capture.json b/resources/config/worlds/capture.json similarity index 100% rename from resources/config/worlds/world-capture.json rename to resources/config/worlds/capture.json diff --git a/resources/config/worlds/world-colorful-labels.json b/resources/config/worlds/colorful-labels.json similarity index 100% rename from resources/config/worlds/world-colorful-labels.json rename to resources/config/worlds/colorful-labels.json diff --git a/resources/config/worlds/world-i18n.json b/resources/config/worlds/i18n.json similarity index 100% rename from resources/config/worlds/world-i18n.json rename to resources/config/worlds/i18n.json diff --git a/resources/config/worlds/world-joint.json b/resources/config/worlds/joint.json similarity index 100% rename from resources/config/worlds/world-joint.json rename to resources/config/worlds/joint.json diff --git a/resources/config/worlds/world-physics.json b/resources/config/worlds/physics.json similarity index 100% rename from resources/config/worlds/world-physics.json rename to resources/config/worlds/physics.json diff --git a/resources/config/worlds/world-prefab.json b/resources/config/worlds/prefab.json similarity index 100% rename from resources/config/worlds/world-prefab.json rename to resources/config/worlds/prefab.json diff --git a/resources/config/worlds/shader.json b/resources/config/worlds/shader.json new file mode 100644 index 0000000..af2c88f --- /dev/null +++ b/resources/config/worlds/shader.json @@ -0,0 +1,82 @@ +{ + "world-name": "shader", + "background-color": "#000000FF", + "size": { + "width": 1024, + "height": 768 + }, + "camera": { + "x": 512, + "y": 384 + }, + "children": [ + { + "id": 1, + "name": "title", + "position": { + "x": 512, + "y": 700 + }, + "scale": { + "x": 1, + "y": 1 + }, + "rotate": 0, + "z-order": 0, + "components": [ + { + "type": "Label", + "name": "Label", + "font": "manaspc.ttf", + "size": 36, + "text": "Shader Parameters Test", + "color": "#FFFFFFFF", + "alignment": 2 + } + ] + }, + { + "id": 2, + "name": "icon", + "position": { + "x": 512, + "y": 384 + }, + "scale": { + "x": 2, + "y": 2 + }, + "rotate": 0, + "z-order": 0, + "components": [ + { + "type": "Sprite", + "name": "Sprite", + "filename": "dice.png", + "shader": "opaque", + "boundary": { + "left-bottom": { + "x": 0, + "y": 0 + }, + "right-up": { + "x": 80, + "y": 64 + } + }, + "color": "#FFFFFFFF" + } + ] + } + ], + "components": [ + { + "type": "StateMachine", + "name": "WorldSwitch", + "driver-script": "world_switch.lua", + "classname": "WorldSwitch", + "subscription": [ + ] + } + ] +} \ No newline at end of file diff --git a/resources/config/worlds/world-welcome.json b/resources/config/worlds/welcome.json similarity index 100% rename from resources/config/worlds/world-welcome.json rename to resources/config/worlds/welcome.json diff --git a/resources/script/kernel/components.lua b/resources/script/kernel/components.lua index e6f2c30..7445ba7 100644 --- a/resources/script/kernel/components.lua +++ b/resources/script/kernel/components.lua @@ -30,4 +30,9 @@ end Label = {} Label.getComponent = function(object) return engine.Label.getComponent(object) +end + +StateMachine = {} +StateMachine.getComponent = function(object, name) + return engine.StateMachine.getComponent(object, name) end \ No newline at end of file diff --git a/resources/script/prefab_loader.lua b/resources/script/prefab_loader.lua index 2ac214c..73a1c5f 100644 --- a/resources/script/prefab_loader.lua +++ b/resources/script/prefab_loader.lua @@ -33,11 +33,10 @@ end function PrefabLoader:updateCreate(delta) local spin1 = PrefabFactory.loadPrefab("spin") - --local spin2 = PrefabFactory.loadPrefab("spin") spin1:setPosition(engine.vec2(200, 384)) - --spin2:setPosition(engine.vec2(824, 384)) + local script = StateMachine.getComponent(spin1, "Spin") + script.direction = 1 self.game_object:addChild("spin1", spin1) - --self.game_object:addChild("spin2", spin2) self.move("Idle") end diff --git a/resources/script/world_switch.lua b/resources/script/world_switch.lua index 9937347..502815d 100644 --- a/resources/script/world_switch.lua +++ b/resources/script/world_switch.lua @@ -49,7 +49,7 @@ end WORLD_SWITCH_TABLE = {} WORLD_SWITCH_TABLE["welcome"] = {} WORLD_SWITCH_TABLE["welcome"]["next"] = "colorful-labels" -WORLD_SWITCH_TABLE["welcome"]["prev"] = "prefab" +WORLD_SWITCH_TABLE["welcome"]["prev"] = "shader" WORLD_SWITCH_TABLE["colorful-labels"] = {} WORLD_SWITCH_TABLE["colorful-labels"]["next"] = "audio" WORLD_SWITCH_TABLE["colorful-labels"]["prev"] = "welcome" @@ -81,5 +81,8 @@ WORLD_SWITCH_TABLE["archive"] = {} WORLD_SWITCH_TABLE["archive"]["next"] = "prefab" WORLD_SWITCH_TABLE["archive"]["prev"] = "camera" WORLD_SWITCH_TABLE["prefab"] = {} -WORLD_SWITCH_TABLE["prefab"]["next"] = "welcome" -WORLD_SWITCH_TABLE["prefab"]["prev"] = "archive" \ No newline at end of file +WORLD_SWITCH_TABLE["prefab"]["next"] = "shader" +WORLD_SWITCH_TABLE["prefab"]["prev"] = "archive" +WORLD_SWITCH_TABLE["shader"] = {} +WORLD_SWITCH_TABLE["shader"]["next"] = "welcome" +WORLD_SWITCH_TABLE["shader"]["prev"] = "prefab" \ No newline at end of file diff --git a/resources/shaders/opaque.frag b/resources/shaders/opaque.frag new file mode 100644 index 0000000..6007293 --- /dev/null +++ b/resources/shaders/opaque.frag @@ -0,0 +1,12 @@ +#version 330 core +in vec2 TexCoord; + +out vec4 color; + +uniform sampler2D image; +uniform vec4 my_color; +uniform float opaque; + +void main() { + color = opaque * my_color * texture(image, TexCoord); +} \ No newline at end of file