diff --git a/3DTest/Render.hpp b/3DTest/Render.hpp new file mode 100644 index 0000000..67df778 --- /dev/null +++ b/3DTest/Render.hpp @@ -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; +} diff --git a/3DTest/SynGame.hpp b/3DTest/SynGame.hpp index ef4c81a..2d60818 100644 --- a/3DTest/SynGame.hpp +++ b/3DTest/SynGame.hpp @@ -9,4 +9,5 @@ #include #include "Timer.hpp" #include "Window.hpp" + #include "Render.hpp" #endif diff --git a/3DTest/Window.hpp b/3DTest/Window.hpp index 8d0ab56..3009d74 100644 --- a/3DTest/Window.hpp +++ b/3DTest/Window.hpp @@ -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() diff --git a/3DTest/main.cpp b/3DTest/main.cpp index b072ccb..66d5361 100644 --- a/3DTest/main.cpp +++ b/3DTest/main.cpp @@ -2,7 +2,7 @@ int main() { - Window window; + RenderWindow window(1600, 900, "3D Test Syntriax"); while (window.IsOpen()) window.Update();