3D Render Window
This commit is contained in:
parent
84eb390803
commit
522aefb7dd
|
@ -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 <cmath>
|
||||||
#include "Timer.hpp"
|
#include "Timer.hpp"
|
||||||
#include "Window.hpp"
|
#include "Window.hpp"
|
||||||
|
#include "Render.hpp"
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -2,22 +2,24 @@
|
||||||
|
|
||||||
class Window
|
class Window
|
||||||
{
|
{
|
||||||
private:
|
protected:
|
||||||
sf::RenderWindow window;
|
sf::RenderWindow window;
|
||||||
sf::Event event;
|
sf::Event event;
|
||||||
sf::Vector2u size;
|
sf::Vector2u size;
|
||||||
|
sf::Vector2u windowedSize;
|
||||||
std::string title;
|
std::string title;
|
||||||
sf::Uint32 style;
|
sf::Uint32 style;
|
||||||
Timer timer;
|
Timer timer;
|
||||||
bool isFocused;
|
bool isFocused;
|
||||||
|
bool fullscreen;
|
||||||
public:
|
public:
|
||||||
Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = 7U);
|
Window(unsigned int = 960, unsigned int = 540, std::string = "Window", sf::Uint32 = sf::Style::Titlebar | sf::Style::Close);
|
||||||
void CreateWindow();
|
virtual void CreateWindow();
|
||||||
void CloseWindow();
|
virtual void CloseWindow();
|
||||||
void Update();
|
virtual void Update();
|
||||||
void SetFrameRate(int = 0);
|
void SetFrameRate(int = 0);
|
||||||
void SetTitle(std::string);
|
void SetTitle(std::string);
|
||||||
void SetSize(unsigned int, unsigned int);
|
virtual void SetSize(unsigned int, unsigned int);
|
||||||
bool IsOpen();
|
bool IsOpen();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -31,6 +33,12 @@ void Window::Update()
|
||||||
isFocused = false;
|
isFocused = false;
|
||||||
else if (event.type == sf::Event::GainedFocus)
|
else if (event.type == sf::Event::GainedFocus)
|
||||||
isFocused = true;
|
isFocused = true;
|
||||||
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::F))
|
||||||
|
{
|
||||||
|
fullscreen = !fullscreen;
|
||||||
|
CloseWindow();
|
||||||
|
CreateWindow();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!isFocused)
|
if(!isFocused)
|
||||||
|
@ -45,7 +53,12 @@ Window::Window(unsigned int width, unsigned int height, std::string title, sf::U
|
||||||
this -> size.y = height;
|
this -> size.y = height;
|
||||||
this -> title = title;
|
this -> title = title;
|
||||||
this -> style = style;
|
this -> style = style;
|
||||||
|
|
||||||
|
windowedSize.x = width;
|
||||||
|
windowedSize.y = height;
|
||||||
|
|
||||||
isFocused = true;
|
isFocused = true;
|
||||||
|
fullscreen = false;
|
||||||
SetFrameRate();
|
SetFrameRate();
|
||||||
CreateWindow();
|
CreateWindow();
|
||||||
}
|
}
|
||||||
|
@ -55,8 +68,20 @@ void Window::CreateWindow()
|
||||||
if(window.isOpen())
|
if(window.isOpen())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
sf::VideoMode videoMode(size.x, size.y);
|
if(!fullscreen)
|
||||||
window.create(videoMode, title, style);
|
{
|
||||||
|
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();
|
timer.ResetTimer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,7 +118,11 @@ void Window::SetTitle(std::string title)
|
||||||
void Window::SetSize(unsigned int width, unsigned int height)
|
void Window::SetSize(unsigned int width, unsigned int height)
|
||||||
{
|
{
|
||||||
size = sf::Vector2u(width, height);
|
size = sf::Vector2u(width, height);
|
||||||
|
sf::Vector2i pos = window.getPosition();
|
||||||
|
CloseWindow();
|
||||||
window.setSize(size);
|
window.setSize(size);
|
||||||
|
CreateWindow();
|
||||||
|
window.setPosition(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Window::IsOpen()
|
bool Window::IsOpen()
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Window window;
|
RenderWindow window(1600, 900, "3D Test Syntriax");
|
||||||
|
|
||||||
while (window.IsOpen())
|
while (window.IsOpen())
|
||||||
window.Update();
|
window.Update();
|
||||||
|
|
Loading…
Reference in New Issue