Permalink
Browse files

Transparency when player is behind GUI.

  • Loading branch information...
1 parent 6487856 commit 1512d1b41427e607904a596861cfa9ccac062e2e @spacechase0 committed Apr 18, 2012
Showing with 48 additions and 11 deletions.
  1. +46 −11 src/game/GameInterface.cpp
  2. +2 −0 src/game/GameInterface.h
@@ -18,6 +18,30 @@ namespace
{
return std::shared_ptr< BaseRenderer >( new NpcRenderer( obj ) );
}
+
+ void doTransparency( bool trans )
+ {
+ }
+
+ template< typename DRAWABLE >
+ void doTransparency( bool trans, DRAWABLE& draw )
+ {
+ if ( trans )
+ {
+ draw.setColor( sf::Color( 255, 255, 255, 96 ) );
+ }
+ else
+ {
+ draw.setColor( sf::Color::White );
+ }
+ }
+
+ template < typename DRAWABLE, typename... Args >
+ void doTransparency( bool trans, DRAWABLE& draw, Args&... args )
+ {
+ doTransparency( trans, draw );
+ doTransparency( trans, args... );
+ }
}
void GameInterface::initialize( World& world )
@@ -157,35 +181,35 @@ void GameInterface::render( sf::RenderWindow& window, World& world )
renderWorld( window, world );
renderInterface( window, world );
}
-#include<iostream>
+
void GameInterface::renderWorld( sf::RenderWindow& window, World& world )
{
std::shared_ptr< const Player > player = world.getPlayer();
const MapData& map = world.getMaps()[ player->getMap() ];
sf::View oldView = window.getView();
{
- sf::View newView( player->getPosition() * 32.f, sf::Vector2f( 640, 480 ) );
+ worldView = sf::View( player->getPosition() * 32.f, sf::Vector2f( 640, 480 ) );
- if ( newView.getCenter().x - ( newView.getSize().x / 2 ) < 0 )
+ if ( worldView.getCenter().x - ( worldView.getSize().x / 2 ) < 0 )
{
- newView.setCenter( newView.getSize().x / 2, newView.getCenter().y );
+ worldView.setCenter( worldView.getSize().x / 2, worldView.getCenter().y );
}
- if ( newView.getCenter().y - ( newView.getSize().y / 2 ) < 0 )
+ if ( worldView.getCenter().y - ( worldView.getSize().y / 2 ) < 0 )
{
- newView.setCenter( newView.getCenter().x, newView.getSize().y / 2 );
+ worldView.setCenter( worldView.getCenter().x, worldView.getSize().y / 2 );
}
- if ( newView.getCenter().x + ( newView.getSize().x / 2 ) > map.getSize().x * 32 )
+ if ( worldView.getCenter().x + ( worldView.getSize().x / 2 ) > map.getSize().x * 32 )
{
- newView.setCenter( ( map.getSize().x * 32 ) - ( newView.getSize().x / 2 ), newView.getCenter().y );
+ worldView.setCenter( ( map.getSize().x * 32 ) - ( worldView.getSize().x / 2 ), worldView.getCenter().y );
}
- if ( newView.getCenter().y + ( newView.getSize().y / 2 ) > map.getSize().y * 32 )
+ if ( worldView.getCenter().y + ( worldView.getSize().y / 2 ) > map.getSize().y * 32 )
{
- newView.setCenter( newView.getCenter().x, ( map.getSize().y * 32 ) - ( newView.getSize().y / 2 ) );
+ worldView.setCenter( worldView.getCenter().x, ( map.getSize().y * 32 ) - ( worldView.getSize().y / 2 ) );
}
- window.setView( newView );
+ window.setView( worldView );
}
{
for ( std::size_t i = 0; i < map.getLayerCount(); ++i )
@@ -247,6 +271,11 @@ void GameInterface::renderWorld( sf::RenderWindow& window, World& world )
void GameInterface::renderInterface( sf::RenderWindow& window, World& world )
{
+ sf::Vector2f playerPos = world.getPlayer()->getPosition();
+ playerPos.x *= 32;
+ playerPos.y *= 32;
+ playerPos -= worldView.getCenter() - sf::Vector2f( 320, 240 );
+
{
int mainRot = ( world.getTime() / 1440.f ) * -360 + 180;
int bodyRot = mainRot + 45;
@@ -256,6 +285,9 @@ void GameInterface::renderInterface( sf::RenderWindow& window, World& world )
time.moon .setRotation( bodyRot + 180 );
time.sun .setRotation( bodyRot );
+ doTransparency( time.rim.getGlobalBounds().contains( playerPos ),
+ time.sky, time.stars, time.glow, time.moon, time.sun, time.ground, time.rim );
+
window.draw( time.sky );
window.draw( time.stars );
//window.draw( time.glow );
@@ -271,6 +303,9 @@ void GameInterface::renderInterface( sf::RenderWindow& window, World& world )
bars.fat.setTextureRect( sf::IntRect( 0, 22, fatWidth, 8 ) );
bars.star.setTextureRect( sf::IntRect( 0, 30, starWidth, 8 ) );
+ doTransparency( bars.back.getGlobalBounds().contains( playerPos ),
+ bars.back, bars.fat, bars.star );
+
window.draw( bars.back );
window.draw( bars.fat );
window.draw( bars.star );
@@ -5,6 +5,7 @@
#include <map>
#include <memory>
#include <SFML/Graphics/Sprite.hpp>
+#include <SFML/Graphics/View.hpp>
#include <SFML/System/Clock.hpp>
#include <string>
#include <util/unsorted_map.h>
@@ -32,6 +33,7 @@ class GameInterface
util::unsorted_map< GameObject*, std::shared_ptr< BaseRenderer > > renderers;
sf::Clock rendererClearTimer;
+ sf::View worldView;
void renderWorld( sf::RenderWindow& window, World& world );
void renderInterface( sf::RenderWindow& window, World& world );

0 comments on commit 1512d1b

Please sign in to comment.