Advertisement
Guest User

Untitled

a guest
Jan 31st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.40 KB | None | 0 0
  1. //engine
  2. #include <Urho3D/Engine/Application.h>
  3. #include <Urho3D/Engine/Engine.h>
  4. #include <Urho3D/Input/Input.h>
  5. #include <Urho3D/Input/InputEvents.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7.  
  8. #include <Urho3D/Resource/ResourceCache.h>
  9. #include <Urho3D/Scene/Scene.h>
  10. #include <Urho3D/Scene/SceneEvents.h>
  11. #include <Urho3D/Graphics/Octree.h>
  12. #include <Urho3D/Graphics/DebugRenderer.h>
  13. #include <Urho3D/Graphics/Camera.h>
  14. #include <Urho3D/Graphics/Viewport.h>
  15. #include <Urho3D/Graphics/Renderer.h>
  16.  
  17. #include <Urho3D/Graphics/Model.h>
  18. #include <Urho3D/Graphics/StaticModel.h>
  19. #include <Urho3D/Graphics/AnimatedModel.h>
  20. #include <Urho3D/Graphics/Material.h>
  21.  
  22. #include <Urho3D/Graphics/Light.h>
  23.  
  24. #include <Urho3D/Core/CoreEvents.h>
  25.  
  26. #include <Urho3D/Math/Vector3.h>
  27.  
  28. #include <Urho3D/UI/Window.h>
  29. #include <Urho3D/UI/Text.h>
  30. #include <Urho3D/UI/Font.h>
  31. #include <Urho3D/UI/UI.h>
  32.  
  33.  
  34. //include
  35. #include <string>
  36. #include <iostream>
  37. #include <sstream>
  38.  
  39. using namespace Urho3D;
  40.  
  41.  
  42. class projet : public Application
  43. {
  44.  
  45.     URHO3D_OBJECT(projet, Application)
  46.  
  47. public:
  48.  
  49. ////______________________
  50. ////    DEFINITION    
  51.  
  52.  
  53.     SharedPtr<Scene> my_scene;
  54.  
  55.     //about camera
  56.     SharedPtr<Node> camNode;
  57.  
  58.     //about text
  59.     Window* window;
  60.     Text* text;
  61.  
  62.     projet(Context* context) : Application(context)
  63.     {
  64.     }
  65.  
  66.     virtual void Setup()
  67.     {
  68.         engineParameters_["FullScreen"]=false;
  69.         engineParameters_["WindowWidth"]=1280;
  70.         engineParameters_["WindowHeight"]=720;
  71.         engineParameters_["WindowResizable"]=true;
  72.     }
  73.  
  74.     virtual void Start()
  75.     {
  76.  
  77.         ResourceCache* cache=GetSubsystem<ResourceCache>();
  78.  
  79.         my_scene=new Scene(context_);
  80.         my_scene->CreateComponent<Octree>();
  81.         my_scene->CreateComponent<DebugRenderer>();
  82.  
  83.  
  84.  
  85. ////__________________
  86. ////    CAMERA        
  87.  
  88.  
  89.         camNode=my_scene->CreateChild("camNode");
  90.         Camera* camObject=camNode->CreateComponent<Camera>();
  91.         camObject->SetFarClip(2000);
  92.     camNode->SetWorldPosition(Vector3(20,20,20));
  93.     camNode->LookAt(Vector3::ZERO);
  94.  
  95.         //camera light
  96.         {
  97.             Light* light=camNode->CreateComponent<Light>();
  98.             light->SetLightType(LIGHT_POINT);
  99.             light->SetRange(25);
  100.             light->SetBrightness(2.0);
  101.             light->SetColor(Color(.8,1,.8,1.0));
  102.         }
  103.  
  104.  
  105. ////________________
  106. ////    TEXT    
  107.  
  108.  
  109.         std::string str = (" ");
  110.         {
  111.             std::ostringstream ss;
  112.             ss<<"test";
  113.             std::string s(ss.str());
  114.             str.append(s/*.substr(0,60)*/);
  115.         }
  116.         String s(str.c_str(),str.size());
  117.  
  118.         window=new Window(context_);
  119.         GetSubsystem<UI>()->GetRoot()->AddChild(window);
  120.         window->SetStyle("Window");
  121.         window->SetSize(600,300);
  122.         window->SetColor(Color(.0,.15,.3,.5));
  123.         window->SetAlignment(HA_LEFT,VA_TOP);
  124.  
  125.         text=new Text(context_);
  126.         text->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"),14);
  127.         text->SetColor(Color(.8,.85,.9));
  128.         text->SetAlignment(HA_LEFT,VA_TOP);
  129.         text->SetText(s); //s
  130.         window->AddChild(text);
  131.  
  132.  
  133.  
  134. ////__________________
  135. ////    RENDER    
  136.  
  137.  
  138.         Renderer* renderer=GetSubsystem<Renderer>();
  139.         SharedPtr<Viewport> viewport(new Viewport(context_,my_scene,camNode->GetComponent<Camera>()));
  140.         renderer->SetViewport(0,viewport);
  141.  
  142.  
  143. ////__________________
  144. ////    EVENTS    
  145.  
  146.  
  147.     //SubscribeToEvent(E_BEGINFRAME,URHO3D_HANDLER(projet,HandleBeginFrame));
  148.     //SubscribeToEvent(E_KEYDOWN,URHO3D_HANDLER(projet,HandleKeyDown));
  149.     //SubscribeToEvent(E_UIMOUSECLICK,URHO3D_HANDLER(projet,HandleControlClicked));
  150.         SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(projet, HandleUpdate));
  151.     //SubscribeToEvent(E_POSTUPDATE,URHO3D_HANDLER(projet,HandlePostUpdate));
  152.     //SubscribeToEvent(E_RENDERUPDATE,URHO3D_HANDLER(projet,HandleRenderUpdate));
  153.     //SubscribeToEvent(E_POSTRENDERUPDATE,URHO3D_HANDLER(projet,HandlePostRenderUpdate));
  154.     //SubscribeToEvent(E_ENDFRAME,URHO3D_HANDLER(projet,HandleEndFrame));
  155.         SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(projet, HandleKeyDown));
  156.     }
  157.  
  158.     virtual void Stop()
  159.     {
  160.     }
  161.  
  162.  
  163. ////________
  164.  
  165.  
  166.     void HandleUpdate(StringHash eventType,VariantMap& eventData)
  167.     {
  168.         float timeStep=eventData[Update::P_TIMESTEP].GetFloat();
  169.     float MOVE_SPEED=50.0f;
  170.         Input* input=GetSubsystem<Input>();
  171.  
  172.     if(input->GetQualifierDown(1))  // 1 is shift, 2 is ctrl, 4 is alt
  173.             MOVE_SPEED*=4;
  174.  
  175.         if(input->GetKeyDown('D')) //rotate sens inverse horizontal
  176.             camNode->Translate(Vector3(1,0, 0)*MOVE_SPEED*timeStep);
  177.         if(input->GetKeyDown('Q')) //sens montre horizontal
  178.             camNode->Translate(Vector3(-1,0,0)*MOVE_SPEED*timeStep);
  179.         if(input->GetKeyDown('Z')) //zoom avant
  180.             camNode->Translate(Vector3(0,0,1)*MOVE_SPEED*timeStep);
  181.         if(input->GetKeyDown('S')) //zoom arriere
  182.             camNode->Translate(Vector3(0,0,-1)*MOVE_SPEED*timeStep);
  183.     if(input->GetKeyDown('E')) //rotate sens inverse vertical
  184.             camNode->Translate(Vector3(0,1,0)*MOVE_SPEED*timeStep);
  185.         if(input->GetKeyDown('A')) //sens montre vertical
  186.             camNode->Translate(Vector3(0,-1,0)*MOVE_SPEED*timeStep);
  187.  
  188.     if(!GetSubsystem<Input>()->IsMouseGrabbed())
  189.     {
  190.         IntVector2 mouseMove=input->GetMouseMove();
  191.        
  192.         if(mouseMove.x_>-2000000000&&mouseMove.y_>-2000000000)
  193.             {
  194.         camNode->LookAt(Vector3::ZERO); //look at 0,0,0
  195.             }
  196.     }
  197.  
  198.     }
  199.  
  200.  
  201.  
  202. ////________
  203.  
  204.  
  205.     void HandleKeyDown(StringHash eventType, VariantMap& eventData)
  206.     {
  207.         using namespace KeyDown;
  208.  
  209.         Graphics* graphics=GetSubsystem<Graphics>();
  210.         int key = eventData[P_KEY].GetInt();
  211.  
  212.         if (key == KEY_ESC) //ESC to quit
  213.         {
  214.             engine_->Exit();
  215.         }
  216.         else if(key == KEY_TAB) //TAB to toggle mouse cursor
  217.         {
  218.             GetSubsystem<Input>()->SetMouseVisible(!GetSubsystem<Input>()->IsMouseVisible());
  219.             GetSubsystem<Input>()->SetMouseGrabbed(!GetSubsystem<Input>()->IsMouseGrabbed());
  220.         }
  221.     else if(key == 'W') //W for fullscreen
  222.     {
  223.         graphics->ToggleFullscreen();
  224.     }
  225.         else if(key == 'I')
  226.         {
  227.             //GetSubsystem<UI>()->menu->ShowPopup ();
  228.         }
  229.     }
  230.  
  231.  
  232. ////________
  233.  
  234.  
  235. };
  236. URHO3D_DEFINE_APPLICATION_MAIN(projet)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement