Skip to content

Commit

Permalink
Merge branch 'master' of github.com:daid/SeriousProton
Browse files Browse the repository at this point in the history
  • Loading branch information
daid committed Apr 9, 2020
2 parents 583ac97 + 8cc1a02 commit b846d91
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ void InputHandler::handleEvent(sf::Event& event)

void InputHandler::postEventsUpdate()
{
input_event_handlers.update();
joystick_event_handlers.update();

if (last_key_press.code != sf::Keyboard::Unknown)
{
InputHandler::fireKeyEvent(last_key_press, -1);
Expand Down
99 changes: 53 additions & 46 deletions src/postProcessManager.cpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,82 @@
#include "logging.h"
#include "postProcessManager.h"
#include "logging.h"
#include "postProcessManager.h"

bool PostProcessor::global_post_processor_enabled = true;

static int powerOfTwo(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}

static int powerOfTwo(int v)
{
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}

PostProcessor::PostProcessor(string name, RenderChain* chain)
: chain(chain)
: chain(chain)
{
if (sf::Shader::isAvailable())
{
if (shader.loadFromFile("resources/" + name + ".frag", sf::Shader::Fragment))
{
LOG(INFO) << "Loaded shader: " << name;
enabled = true;
}else{
LOG(WARNING) << "Failed to load shader:" << name;
enabled = false;
{
if (shader.loadFromFile("resources/" + name + ".frag", sf::Shader::Fragment))
{
LOG(INFO) << "Loaded shader: " << name;
enabled = true;
}else{
LOG(WARNING) << "Failed to load shader:" << name;
enabled = false;
}
}else{
LOG(WARNING) << "Did not load load shader: " << name;
LOG(WARNING) << "Because of no shader support in video card driver.";
enabled = false;
}
}

void PostProcessor::render(sf::RenderTarget& window)
}

void PostProcessor::render(sf::RenderTarget& window)
{
if (!enabled || !sf::Shader::isAvailable() || !global_post_processor_enabled)
{
chain->render(window);
return;
}
if (renderTexture.getSize().x < 1)

sf::View view(window.getView());

// If the window or texture size is 0/impossible, or if the window size has
// changed, resize the viewport, render texture, and input/textureSizes.
if (size.x < 1 || renderTexture.getSize().x < 1 || size != window.getSize())
{
//Setup a backBuffer to render the game on. Then we can render the backbuffer back to the main screen with full-screen shader effects
int w = window.getView().getViewport().width * window.getSize().x;
int h = window.getView().getViewport().height * window.getSize().y;
int tw = powerOfTwo(w);
int th = powerOfTwo(h);
sf::View view(window.getView());
view.setViewport(sf::FloatRect(0, 1.0 - float(h) / float(th), float(w) / float(tw), float(h) / float(th)));

renderTexture.create(tw, th, true);
renderTexture.setRepeated(true);
renderTexture.setSmooth(true);
renderTexture.setView(view);
size = window.getSize();

//Setup a backBuffer to render the game on. Then we can render the backbuffer back to the main screen with full-screen shader effects
int w = view.getViewport().width * size.x;
int h = view.getViewport().height * size.y;
int tw = powerOfTwo(w);
int th = powerOfTwo(h);
view.setViewport(sf::FloatRect(0, 1.0 - float(h) / float(th), float(w) / float(tw), float(h) / float(th)));

renderTexture.create(tw, th, true);
renderTexture.setRepeated(true);
renderTexture.setSmooth(true);
renderTexture.setView(view);

shader.setUniform("inputSize", sf::Vector2f(window.getSize().x, window.getSize().y));
shader.setUniform("inputSize", sf::Vector2f(size.x, size.y));
shader.setUniform("textureSize", sf::Vector2f(renderTexture.getSize().x, renderTexture.getSize().y));
}

renderTexture.clear(sf::Color(20, 20, 20));
chain->render(renderTexture);

renderTexture.display();
sf::Sprite backBufferSprite(renderTexture.getTexture(), sf::IntRect(0, renderTexture.getSize().y - window.getView().getViewport().height * window.getSize().y, window.getView().getViewport().width * window.getSize().x, window.getView().getViewport().height * window.getSize().y));
backBufferSprite.setScale(window.getView().getSize().x/float(renderTexture.getSize().x)/renderTexture.getView().getViewport().width, window.getView().getSize().y/float(renderTexture.getSize().y)/renderTexture.getView().getViewport().height);

window.draw(backBufferSprite, &shader);
}
renderTexture.display();

sf::Sprite backBufferSprite(renderTexture.getTexture(), sf::IntRect(0, renderTexture.getSize().y - view.getViewport().height * size.y, view.getViewport().width * size.x, view.getViewport().height * size.y));
backBufferSprite.setScale(view.getSize().x / float(renderTexture.getSize().x) / renderTexture.getView().getViewport().width, view.getSize().y / float(renderTexture.getSize().y) / renderTexture.getView().getViewport().height);

window.draw(backBufferSprite, &shader);
}

void PostProcessor::setUniform(string name, float value)
{
Expand Down
37 changes: 19 additions & 18 deletions src/postProcessManager.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#ifndef POST_PROCESS_MANAGER_H
#define POST_PROCESS_MANAGER_H

#include <SFML/Graphics.hpp>

#include "stringImproved.h"
#ifndef POST_PROCESS_MANAGER_H
#define POST_PROCESS_MANAGER_H

#include <SFML/Graphics.hpp>

#include "stringImproved.h"
#include "Updatable.h"
#include "Renderable.h"

class PostProcessor : public RenderChain
{
private:
#include "Renderable.h"

class PostProcessor : public RenderChain
{
private:
sf::Shader shader;
sf::RenderTexture renderTexture;
sf::Vector2u size;

RenderChain* chain;

static bool global_post_processor_enabled;
public:
public:
bool enabled;

PostProcessor(string name, RenderChain* chain);
PostProcessor(string name, RenderChain* chain);
virtual ~PostProcessor() {}

virtual void render(sf::RenderTarget& window);

void setUniform(string name, float value);

static void setEnable(bool enable) { global_post_processor_enabled = enable; }
static bool isEnabled() { return global_post_processor_enabled; }
};

#endif//POST_PROCESS_MANAGER_H
static void setEnable(bool enable) { global_post_processor_enabled = enable; }
static bool isEnabled() { return global_post_processor_enabled; }
};

#endif//POST_PROCESS_MANAGER_H

0 comments on commit b846d91

Please sign in to comment.