2
« on: April 19, 2011, 08:24:24 am »
okay so I'm trying to add shaders to the pong example (taking them from the shader example), and they don't work... When trying to load them .LoadFromFile it fails to load them, except, I tried using the "nothing.sfx" shader, and that one works. Unless I change the code in it, then it fails o_0.
Am I failing to initialize something?
here's my code (sorry I added in a couple sounds too, that'll probably stop it from compiling, you might just copy the ball.wav file and renaming the copies just so it'll find them, if you need to compile it)
I think the only two relevant variables I added were
sf::RenderImage backBuffer;
and
sf::Shader shaderBlury;
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cmath>
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
// Defines PI
const float PI = 3.14159f;
if (sf::Shader::IsAvailable() == false){
//DisplayError();
return EXIT_FAILURE;
}
// Create the window of the application
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML Pong");
//declare backbuffer (for rendering multiple shaders)
sf::RenderImage backBuffer;
backBuffer.Create(800,600);
// Load the sounds used in the game
sf::SoundBuffer ballSoundBuffer;
if (!ballSoundBuffer.LoadFromFile("resources/ball.wav")){
return EXIT_FAILURE;
}
sf::Sound ballSound(ballSoundBuffer);
sf::SoundBuffer scoreSoundBuffer;
if (!scoreSoundBuffer.LoadFromFile("resources/good.wav")){
return EXIT_FAILURE;
}
sf::Sound scoreSound(scoreSoundBuffer);
sf::SoundBuffer hurtSoundBuffer;
if (!hurtSoundBuffer.LoadFromFile("resources/hurt.wav")){
return EXIT_FAILURE;
}
sf::Sound hurtSound(hurtSoundBuffer);
// Load the images used in the game
sf::Image backgroundImage, leftPaddleImage, rightPaddleImage, ballImage;
if (!backgroundImage.LoadFromFile("resources/background.png") ||
!leftPaddleImage.LoadFromFile("resources/paddle_left.png") ||
!rightPaddleImage.LoadFromFile("resources/paddle_right.png") ||
!ballImage.LoadFromFile("resources/ball.png"))
{
return EXIT_FAILURE;
}
// Load the text font
sf::Font font;
if (!font.LoadFromFile("resources/sansation.ttf"))
return EXIT_FAILURE;
// Initialize the end text
sf::Text end;
end.SetFont(font);
end.SetCharacterSize(60);
end.Move(150.f, 200.f);
end.SetColor(sf::Color(50, 50, 250));
// Create the sprites of the background, the paddles and the ball
sf::Sprite background(backgroundImage);
sf::Sprite leftPaddle(leftPaddleImage);
sf::Sprite rightPaddle(rightPaddleImage);
sf::Sprite ball(ballImage);
leftPaddle.Move(10, (window.GetView().GetSize().y - leftPaddle.GetSize().y) / 2);
rightPaddle.Move(window.GetView().GetSize().x - rightPaddle.GetSize().x - 10, (window.GetView().GetSize().y - rightPaddle.GetSize().y) / 2);
ball.Move((window.GetView().GetSize().x - ball.GetSize().x) / 2, (window.GetView().GetSize().y - ball.GetSize().y) / 2);
// Define the paddles properties
sf::Clock AITimer;
const float AITime = 0.1f;
float leftPaddleSpeed = 400.f;
float rightPaddleSpeed = 400.f;
//load shaders
sf::Shader shaderBlury;
if (!shaderBlury.LoadFromFile("resources/nothing.sfx"))//.LoadFromFile("resources/blur.sfx"))
{return EXIT_FAILURE;}
shaderBlury.SetCurrentTexture("texture");//I have no idea what texture that's supposed to be
//shaderBlury.SetParameter("offset",0.f);
//define the game mechanic properties
int playerLives = 5;
// Define the ball properties
float ballSpeed = 400.f;
float ballAngle;
do
{
// Make sure the ball initial angle is not too much vertical
ballAngle = PI;//sf::Randomizer::Random(0.f, 2 * PI);
} while (std::abs(std::cos(ballAngle)) < 0.7f);
bool isPlaying = true;
while (window.IsOpened())
{
// Handle events
sf::Event event;
while (window.PollEvent(event))
{
// Window closed or escape key pressed : exit
if ((event.Type == sf::Event::Closed) ||
((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape)))
{
window.Close();
break;
}
}
if (isPlaying)
{
// Check that the player has lives remaining
if (playerLives < 0){
isPlaying = false;
end.SetString("You lost !\n(press escape to exit)");
}
// Move the player's paddle
if (window.GetInput().IsKeyDown(sf::Key::Up) && (leftPaddle.GetPosition().y > 5.f))
leftPaddle.Move(0.f, -leftPaddleSpeed * window.GetFrameTime());
if (window.GetInput().IsKeyDown(sf::Key::Down) && (leftPaddle.GetPosition().y < window.GetView().GetSize().y - leftPaddle.GetSize().y - 5.f))
leftPaddle.Move(0.f, leftPaddleSpeed * window.GetFrameTime());
// Move the computer's paddle
if (((rightPaddleSpeed < 0.f) && (rightPaddle.GetPosition().y > 5.f)) ||
((rightPaddleSpeed > 0.f) && (rightPaddle.GetPosition().y < window.GetView().GetSize().y - rightPaddle.GetSize().y - 5.f)))
{
rightPaddle.Move(0.f, rightPaddleSpeed * window.GetFrameTime());
}
// Update the computer's paddle direction according to the ball position
if (AITimer.GetElapsedTime() > AITime)
{
AITimer.Reset();
if ((rightPaddleSpeed < 0) && (ball.GetPosition().y + ball.GetSize().y > rightPaddle.GetPosition().y + rightPaddle.GetSize().y))
rightPaddleSpeed = -rightPaddleSpeed;
if ((rightPaddleSpeed > 0) && (ball.GetPosition().y < rightPaddle.GetPosition().y))
rightPaddleSpeed = -rightPaddleSpeed;
}
// Move the ball
float factor = ballSpeed * window.GetFrameTime();
ball.Move(std::cos(ballAngle) * factor, std::sin(ballAngle) * factor);
// Check collisions between the ball and the screen
if (ball.GetPosition().x < 0.f)
{
playerLives -= 1;
hurtSound.Play();
//bounce the ball away
ballAngle = PI - ballAngle;
ball.SetX(leftPaddle.GetPosition().x + 0.1f);
//isPlaying = false;
//end.SetString("You lost !\n(press escape to exit)");
}
if (ball.GetPosition().x + ball.GetSize().x > window.GetView().GetSize().x)
{
isPlaying = false;
end.SetString("You won !\n(press escape to exit)");
}
if (ball.GetPosition().y < 0.f)
{
ballSound.Play();
ballAngle = -ballAngle;
ball.SetY(0.1f);
}
if (ball.GetPosition().y + ball.GetSize().y > window.GetView().GetSize().y)
{
ballSound.Play();
ballAngle = -ballAngle;
ball.SetY(window.GetView().GetSize().y - ball.GetSize().y - 0.1f);
}
// Check the collisions between the ball and the paddles
// Left Paddle
if (ball.GetPosition().x < leftPaddle.GetPosition().x + leftPaddle.GetSize().x &&
ball.GetPosition().x > leftPaddle.GetPosition().x + (leftPaddle.GetSize().x / 2.0f) &&
ball.GetPosition().y + ball.GetSize().y >= leftPaddle.GetPosition().y &&
ball.GetPosition().y <= leftPaddle.GetPosition().y + leftPaddle.GetSize().y)
{
ballSound.Play();
ballAngle = PI - ballAngle;
ball.SetX(leftPaddle.GetPosition().x + leftPaddle.GetSize().x + 0.1f);
}
// Right Paddle
if (ball.GetPosition().x + ball.GetSize().x > rightPaddle.GetPosition().x &&
ball.GetPosition().x + ball.GetSize().x < rightPaddle.GetPosition().x + (rightPaddle.GetSize().x / 2.0f) &&
ball.GetPosition().y + ball.GetSize().y >= rightPaddle.GetPosition().y &&
ball.GetPosition().y <= rightPaddle.GetPosition().y + rightPaddle.GetSize().y)
{
ballSound.Play();
ballAngle = PI - ballAngle;
ball.SetX(rightPaddle.GetPosition().x - ball.GetSize().x - 0.1f);
}
}
// Clear the window
backBuffer.Clear();
// Draw the background, paddles and ball sprites
backBuffer.Draw(background);
backBuffer.Draw(leftPaddle);
backBuffer.Draw(rightPaddle);
backBuffer.Draw(ball);
backBuffer.Display();//I guess this just flips the y values? weeeeird
//draw the shaders
backBuffer.Draw(sf::Sprite(backBuffer.GetImage()), shaderBlury);
backBuffer.Display();
window.Draw(sf::Sprite(backBuffer.GetImage()));
window.Draw(sf::Sprite(backBuffer.GetImage()));
// If the game is over, display the end message
if (!isPlaying)
window.Draw(end);
// Display things on screen
window.Display();
}
return EXIT_SUCCESS;
}
edit: oh I'm using Windows XP and Visual Studio 2008 Express Edition