Advertisement
SSS_Krut

Игровой движок для вопроса в беседе Wk

Jun 13th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. //OOP_aplication.h
  2. #ifndef APPLICATION_TYPE_HEADER
  3. #define APPLICATION_TYPE_HEADER
  4.  
  5. #include "Main.h"
  6. #include "InterfaceMapWidget.h"
  7.  
  8. namespace Render {
  9.     class OOP_aplication {
  10.     protected:
  11.         sf::RenderWindow* Window;
  12.         InterfaceMapWidget *MapWidget;
  13.     public:
  14.         OOP_aplication();
  15.         ~OOP_aplication();
  16.         void Init();
  17.         void Run();
  18.         void End();
  19.     };
  20.    
  21. }
  22.  
  23.  
  24. #endif
  25.  
  26.  
  27. //OOP_aplication.cpp
  28. #include "OOP_aplication.h"
  29. namespace Render {
  30.     OOP_aplication::OOP_aplication() :
  31.         Window(nullptr),
  32.         MapWidget(nullptr) {
  33.  
  34.         }
  35.  
  36.     //OOP_aplication::~OOP_aplication() {
  37.  
  38.     //}
  39.  
  40.     void OOP_aplication::Init() {
  41.             Window = new sf::RenderWindow(sf::VideoMode(640, 640), "First Version of Engine");
  42.             MapWidget = new InterfaceMapWidget(0, 0, 640, 640);
  43.        
  44.     }
  45.  
  46.     void OOP_aplication::Run() {
  47.         while (Window->isOpen()) {
  48.             sf::Event event;
  49.             while (Window->pollEvent(event)) {
  50.                 if (event.type == sf::Event::Closed)
  51.                     Window->close();
  52.             }
  53.             Window->clear(sf::Color::Black);
  54.             MapWidget->Draw(Window);
  55.             Window->display();
  56.         }
  57.     }
  58.  
  59.     void OOP_aplication::End() {
  60.         if (Window != nullptr) {
  61.             delete Window;
  62.             Window = nullptr;
  63.         }
  64.         if (MapWidget != nullptr) {
  65.             delete MapWidget;
  66.             MapWidget = nullptr;
  67.         }
  68.     }
  69. }
  70. //Main.h
  71. #ifndef MAIN_HEADER
  72. #define MAIN_HEADER
  73. #include <SFML/Graphics.hpp>
  74. #include <SFML/Window.hpp>
  75. #include <SFML/Audio.hpp>
  76. #include <SFML/Network.hpp>
  77. #include <SFML/System.hpp>
  78. #include "InterfaceMapWidget.h"
  79. #include "OOP_aplication.h"
  80. #endif
  81. //Main.cpp
  82. #include "Main.h"
  83.  
  84.  
  85. int main(int argc, char* argv[]) {
  86.     Render::OOP_aplication Application;
  87.     Application.Init();
  88.     Application.Run();
  89.     Application.End();
  90.  
  91.     return 0;
  92. }
  93.  
  94. //InterfaceMapWidget.h
  95. #ifndef INTERFACEMAPWIDGET_HEADER
  96. #define INTERFACEMAPWIDGET_HEADER
  97. #include "Main.h"
  98.  
  99. class InterfaceMapWidget {
  100. protected:
  101.     int X, Y, Width, Height;
  102.     sf::Texture SomeTexture;
  103.     //методы класса
  104.  
  105.     //поля класса
  106. public:
  107.     InterfaceMapWidget(int aX, int aY, int aWidth, int aHeight);
  108.     ~InterfaceMapWidget ();
  109.     void Draw(sf::RenderWindow* Window);
  110. };
  111. #endif
  112.  
  113. //InterfaceMapWidget.cpp
  114. #include "InterfaceMapWidget.h"
  115.  
  116. InterfaceMapWidget::InterfaceMapWidget(int aX, int aY, int aWidth, int aHeight) :
  117.     X(aX),
  118.     Y(aY),
  119.     Width(aWidth),
  120.     Height(aHeight),
  121.     SomeTexture() {
  122.     SomeTexture.loadFromFile("../images/Test.png");
  123. }
  124.  
  125. InterfaceMapWidget:: ~InterfaceMapWidget() {
  126. }
  127.  
  128.  
  129. void InterfaceMapWidget::Draw(sf::RenderWindow * Window) {
  130.     sf::Sprite SpriteStuff;
  131.     SpriteStuff.setTexture(SomeTexture);
  132.     SpriteStuff.setPosition(X, Y);
  133.     float WidgetWidth = (float)Width / (float)SomeTexture.getSize().x;
  134.     float WidgetHeight = (float)Height / (float)SomeTexture.getSize().y;
  135.     SpriteStuff.setScale(WidgetWidth, WidgetHeight);
  136.  
  137.     Window->draw(SpriteStuff);
  138.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement