/* * Copyright (c) 2020 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include #include "game-camera.h" #include "game-entity.h" #include "game-model.h" #include "game-renderer.h" #include "game-scene.h" #include "game-texture.h" #include "third-party/pico-json.h" #include using namespace Dali; using namespace picojson; using std::vector; using namespace GameUtils; GameScene::GameScene() { } GameScene::~GameScene() { } bool GameScene::Load(Window window, const char* filename) { ByteArray bytes; if(!LoadFile(filename, bytes)) { return false; } // add EOL bytes.push_back('\0'); picojson::value root; picojson::parse(root, bytes.data()); bool failed(false); if(root.is()) { object rootObject = root.get(); for(object::iterator it = rootObject.begin(); it != rootObject.end(); ++it) { std::string entityName((*it).first); GameEntity* entity = new GameEntity(entityName.c_str()); mEntities.PushBack(entity); value& val((*it).second); value& vLocation = val.get("location"); value& vRotation = val.get("rotation"); value& vScale = val.get("scale"); value& vSize = val.get("size"); value& vModel = val.get("model"); value& vTexture = val.get("texture"); if(!vLocation.is()) { array& location = vLocation.get(); entity->SetLocation(Vector3( location.at(0).get(), location.at(1).get(), location.at(2).get())); } if(!vRotation.is()) { array& rotation = vRotation.get(); entity->SetRotation(Quaternion(Vector4( -rotation.at(0).get(), rotation.at(1).get(), -rotation.at(2).get(), rotation.at(3).get()))); } if(!vScale.is()) { array& scale = vScale.get(); entity->SetScale(Vector3( scale.at(0).get(), scale.at(1).get(), scale.at(2).get())); } if(!vSize.is()) { array& size = vSize.get(); entity->SetSize(Vector3( size.at(0).get(), size.at(1).get(), size.at(2).get())); } GameModel* model(NULL); GameTexture* texture(NULL); if(!vModel.is()) { std::string& strModel = vModel.get(); model = GetResource(strModel.c_str(), mModelCache); } if(!vTexture.is()) { std::string& strTexture = vTexture.get(); texture = GetResource(strTexture.c_str(), mTextureCache); } if(!model || !texture) { failed = true; break; } entity->GetGameRenderer().SetModel(model); entity->GetGameRenderer().SetMainTexture(texture); } } if(failed) { return false; } // add all to the window mRootActor = Actor::New(); mRootActor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); mRootActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); window.GetRootLayer().Add(mRootActor); mRootActor.SetProperty(Actor::Property::SCALE, Vector3(-1.0, 1.0, 1.0)); mRootActor.SetProperty(Actor::Property::POSITION, Vector3(0.0, 0.0, 0.0)); mRootActor.SetProperty(Actor::Property::ORIENTATION, Quaternion(Degree(90), Vector3(1.0, 0.0, 0.0))); for(size_t i = 0; i < mEntities.Size(); ++i) { Actor actor(mEntities[i]->GetActor()); actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER); actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER); mRootActor.Add(actor); mEntities[i]->UpdateRenderer(); } // update camera mCamera.Initialise(window.GetRenderTaskList().GetTask(0).GetCameraActor(), 60.0f, 0.1f, 100.0f, window.GetSize()); return true; } Dali::Actor& GameScene::GetRootActor() { return mRootActor; }