c8a24dad595323d9f91d682553bf3e3256f12891
[platform/core/uifw/dali-demo.git] / examples / fpp-game / game-renderer.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "game-renderer.h"
19 #include "game-model.h"
20 #include "game-texture.h"
21
22 #include "generated/game-renderer-vert.h"
23 #include "generated/game-renderer-frag.h"
24
25 #include <dali/dali.h>
26
27 GameRenderer::GameRenderer()
28 : mModel(NULL),
29   mTexture(NULL)
30 {
31 }
32
33 GameRenderer::~GameRenderer()
34 {
35 }
36
37 void GameRenderer::SetModel(GameModel* model)
38 {
39   mModel = model;
40   Setup();
41 }
42
43 void GameRenderer::SetMainTexture(GameTexture* texture)
44 {
45   mTexture = texture;
46   Setup();
47 }
48
49 void GameRenderer::Setup()
50 {
51   if(!mRenderer && mModel)
52   {
53     Dali::Shader shader = Dali::Shader::New(SHADER_GAME_RENDERER_VERT, SHADER_GAME_RENDERER_FRAG);
54     mRenderer           = Dali::Renderer::New(mModel->GetGeometry(), shader);
55     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_WRITE_MODE, Dali::DepthWriteMode::ON);
56     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_FUNCTION, Dali::DepthFunction::LESS_EQUAL);
57     mRenderer.SetProperty(Dali::Renderer::Property::DEPTH_TEST_MODE, Dali::DepthTestMode::ON);
58   }
59
60   Dali::TextureSet textureSet;
61   Dali::Geometry   geometry;
62
63   if(mModel)
64   {
65     geometry = mModel->GetGeometry();
66   }
67
68   if(mTexture && mTexture->GetTextureSet())
69   {
70     textureSet = mTexture->GetTextureSet();
71   }
72
73   if(textureSet && geometry)
74   {
75     mRenderer.SetGeometry(geometry);
76     mRenderer.SetTextures(textureSet);
77   }
78 }
79
80 Dali::Renderer& GameRenderer::GetRenderer()
81 {
82   return mRenderer;
83 }