3D Render Window
This commit is contained in:
		
							
								
								
									
										92
									
								
								3DTest/Render.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										92
									
								
								3DTest/Render.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,92 @@
 | 
			
		||||
#include "SynGame.hpp"
 | 
			
		||||
 | 
			
		||||
class RenderWindow : public Window
 | 
			
		||||
{
 | 
			
		||||
    private:
 | 
			
		||||
        sf::Vertex *vertices;
 | 
			
		||||
        unsigned int pixelSize;
 | 
			
		||||
        sf::Vertex *_CreateVertexBuffer();
 | 
			
		||||
        void ReloadScreenBuffer();
 | 
			
		||||
    public:
 | 
			
		||||
        RenderWindow(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
 | 
			
		||||
        ~RenderWindow();
 | 
			
		||||
        void Update();
 | 
			
		||||
        void CreateWindow();
 | 
			
		||||
        void CloseWindow();
 | 
			
		||||
        void SetSize(unsigned int, unsigned int);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void RenderWindow::Update()
 | 
			
		||||
{
 | 
			
		||||
    if(!isFocused) 
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    Window::Update();
 | 
			
		||||
    window.clear();
 | 
			
		||||
    window.draw(vertices, size.x * size.y, sf::Points);
 | 
			
		||||
    window.display();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vertex *RenderWindow::_CreateVertexBuffer()
 | 
			
		||||
{
 | 
			
		||||
    int y;
 | 
			
		||||
    int x;
 | 
			
		||||
    sf::Vertex *newBuffer = new sf::Vertex[size.x * size.y];
 | 
			
		||||
    sf::Vertex *ptr = newBuffer;
 | 
			
		||||
 | 
			
		||||
    for (y = 0; y < size.y; y++)
 | 
			
		||||
        for (x = 0; x < size.x; x++)
 | 
			
		||||
        {
 | 
			
		||||
            ptr -> color = sf::Color::Black;
 | 
			
		||||
            ptr++ -> position = sf::Vector2f(x, y); 
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    return newBuffer;    
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RenderWindow::ReloadScreenBuffer()
 | 
			
		||||
{
 | 
			
		||||
    if(vertices)
 | 
			
		||||
        delete[] vertices;
 | 
			
		||||
 | 
			
		||||
    vertices = _CreateVertexBuffer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RenderWindow::CreateWindow()
 | 
			
		||||
{
 | 
			
		||||
    if(window.isOpen())
 | 
			
		||||
        return;
 | 
			
		||||
    
 | 
			
		||||
    Window::CreateWindow();
 | 
			
		||||
    ReloadScreenBuffer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RenderWindow::CloseWindow()
 | 
			
		||||
{
 | 
			
		||||
    if(!window.isOpen())
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    Window::CloseWindow();
 | 
			
		||||
    
 | 
			
		||||
    if(vertices)
 | 
			
		||||
        delete[] vertices;
 | 
			
		||||
 | 
			
		||||
    vertices = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void RenderWindow::SetSize(unsigned int width, unsigned int height)
 | 
			
		||||
{
 | 
			
		||||
    Window::SetSize(width, height);
 | 
			
		||||
    ReloadScreenBuffer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RenderWindow::RenderWindow(unsigned int width, unsigned int height, std::string title, sf::Uint32 style) : Window(width, height, title, style)
 | 
			
		||||
{
 | 
			
		||||
    ReloadScreenBuffer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
RenderWindow::~RenderWindow()
 | 
			
		||||
{
 | 
			
		||||
    if(vertices)
 | 
			
		||||
        delete[] vertices;
 | 
			
		||||
}
 | 
			
		||||
@@ -9,4 +9,5 @@
 | 
			
		||||
    #include <cmath>
 | 
			
		||||
    #include "Timer.hpp"
 | 
			
		||||
    #include "Window.hpp"
 | 
			
		||||
    #include "Render.hpp"
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -2,22 +2,24 @@
 | 
			
		||||
 | 
			
		||||
class Window
 | 
			
		||||
{
 | 
			
		||||
    private:
 | 
			
		||||
    protected:
 | 
			
		||||
        sf::RenderWindow window;
 | 
			
		||||
        sf::Event event;
 | 
			
		||||
        sf::Vector2u size;
 | 
			
		||||
        sf::Vector2u windowedSize;
 | 
			
		||||
        std::string title;
 | 
			
		||||
        sf::Uint32 style;
 | 
			
		||||
        Timer timer; 
 | 
			
		||||
        bool isFocused;
 | 
			
		||||
        bool fullscreen;
 | 
			
		||||
    public:
 | 
			
		||||
        Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = 7U);
 | 
			
		||||
        void CreateWindow();
 | 
			
		||||
        void CloseWindow();
 | 
			
		||||
        void Update();
 | 
			
		||||
        Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
 | 
			
		||||
        virtual void CreateWindow();
 | 
			
		||||
        virtual void CloseWindow();
 | 
			
		||||
        virtual void Update();
 | 
			
		||||
        void SetFrameRate(int = 0);
 | 
			
		||||
        void SetTitle(std::string);
 | 
			
		||||
        void SetSize(unsigned int, unsigned int);
 | 
			
		||||
        virtual void SetSize(unsigned int, unsigned int);
 | 
			
		||||
        bool IsOpen();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -31,6 +33,12 @@ void Window::Update()
 | 
			
		||||
            isFocused = false;
 | 
			
		||||
        else if (event.type == sf::Event::GainedFocus)
 | 
			
		||||
            isFocused = true;
 | 
			
		||||
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
 | 
			
		||||
        {
 | 
			
		||||
            fullscreen = !fullscreen;
 | 
			
		||||
            CloseWindow();
 | 
			
		||||
            CreateWindow();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if(!isFocused)
 | 
			
		||||
@@ -45,7 +53,12 @@ Window::Window(unsigned int width, unsigned int height, std::string title, sf::U
 | 
			
		||||
    this -> size.y = height;
 | 
			
		||||
    this -> title  = title;
 | 
			
		||||
    this -> style  = style;
 | 
			
		||||
 | 
			
		||||
    windowedSize.x = width;
 | 
			
		||||
    windowedSize.y = height;
 | 
			
		||||
    
 | 
			
		||||
    isFocused = true;
 | 
			
		||||
    fullscreen = false;
 | 
			
		||||
    SetFrameRate();
 | 
			
		||||
    CreateWindow();
 | 
			
		||||
}
 | 
			
		||||
@@ -55,8 +68,20 @@ void Window::CreateWindow()
 | 
			
		||||
    if(window.isOpen())
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    sf::VideoMode videoMode(size.x, size.y);
 | 
			
		||||
    window.create(videoMode, title, style);
 | 
			
		||||
    if(!fullscreen)
 | 
			
		||||
    {
 | 
			
		||||
        size = windowedSize;
 | 
			
		||||
        sf::VideoMode videoMode(windowedSize.x, windowedSize.y);
 | 
			
		||||
        window.create(videoMode, title, style);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        sf::VideoMode videoMode(sf::VideoMode::getDesktopMode());
 | 
			
		||||
        size.x = videoMode.width;
 | 
			
		||||
        size.y = videoMode.height;
 | 
			
		||||
        window.create(videoMode, title, sf::Style::Fullscreen);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    timer.ResetTimer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -93,7 +118,11 @@ void Window::SetTitle(std::string title)
 | 
			
		||||
void Window::SetSize(unsigned int width, unsigned int height)
 | 
			
		||||
{
 | 
			
		||||
    size = sf::Vector2u(width, height);
 | 
			
		||||
    sf::Vector2i pos = window.getPosition();
 | 
			
		||||
    CloseWindow();
 | 
			
		||||
    window.setSize(size);
 | 
			
		||||
    CreateWindow();
 | 
			
		||||
    window.setPosition(pos);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool Window::IsOpen()
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    Window window;
 | 
			
		||||
    RenderWindow window(1600, 900, "3D Test Syntriax");
 | 
			
		||||
 | 
			
		||||
    while (window.IsOpen())
 | 
			
		||||
        window.Update();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user