1
Graphics / Settings texture wrapping to repeat using sf::Renderer
« on: June 08, 2011, 08:16:21 pm »
Ah, I didn't know that, but it works perfectly now .
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
float textureSize = 64.0f //The texture is 64x64 so this should display it at the real scale.
// The two sides of the point in the track.
leftVertex.texCoords = sf::Vector2f(0.0f, distance / textureSize);
rightVertex.texCoords = sf::Vector2f(1.0f, distance / textureSize);
renderer.SaveGLStates();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_WRAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_WRAP);
renderer.SetTexture(&m_Texture);
renderer.Begin(sf::Renderer::TriangleStrip);
for (int i = 0; i < m_sideVertices.size(); i++)
{
renderer.AddVertex(m_sideVertices[i].position.x,
m_sideVertices[i].position.y,
m_sideVertices[i].texCoords.x,
m_sideVertices[i].texCoords.y,
sf::Color::White);
}
renderer.End();
renderer.RestoreGLStates();
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
float lerp(float value, float start, float end)
{
return start + (end - start) * value;
}
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
const sf::Input& input = App.GetInput();
// Make a few shapes for testing.
sf::Shape rect1 = sf::Shape::Rectangle(10, 10, 60, 60, sf::Color(255, 0, 0));
sf::Shape circle1 = sf::Shape::Circle(100, 100, 40, sf::Color(0, 255, 0));
sf::Shape line1 = sf::Shape::Line(200, 200, 300, 300, 2, sf::Color(0, 0, 255));
sf::Vector2f currentCenter;
sf::Vector2f targetCenter;
float targetZoom = 0.0f;
float currentZoom = 0.0f;
float previousZoom = 0.0f;
bool zooming = false;
float lerpFactor = 0.01f; // The speed of the zooming.
while (App.IsOpened())
{
sf::Event Event;
while (App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
else if(Event.Type == sf::Event::MouseWheelMoved)
{
zooming = true;
// Zooming in or out.
if (Event.MouseWheel.Delta == 1)
targetZoom = 1.3f;
else
targetZoom = 0.7f;
currentZoom = 1.0f;
previousZoom = currentZoom;
// Store the current center of the view and our target (the position of the mouse).
currentCenter = App.GetDefaultView().GetCenter();
targetCenter = App.ConvertCoords(input.GetMouseX(), input.GetMouseY(), &App.GetDefaultView());
}
}
if (zooming)
{
// Interpolate between the current zoom and the target zoom for a smooth effect.
currentZoom = lerp(lerpFactor, currentZoom, targetZoom);
App.GetDefaultView().Zoom(1.0f + (currentZoom - previousZoom));
// Interpolate the between the current center of the view and the target.
currentCenter.x = lerp(lerpFactor, currentCenter.x, targetCenter.x);
currentCenter.y = lerp(lerpFactor, currentCenter.y, targetCenter.y);
App.GetDefaultView().SetCenter(currentCenter);
// Store the previous zoom because we will need the difference between the previous and current zoom.
// This is because sf::View::Zoom zooms relative to the current zoom.
previousZoom = currentZoom;
// If the difference between the current and previous zoom is less then 0.01, stop zooming.
// Linear interpolation will never reach the target value so we stop here.
if (fabs(targetZoom - currentZoom) < 0.01f)
zooming = false;
}
App.Clear();
App.Draw(rect1);
App.Draw(circle1);
App.Draw(line1);
App.Display();
}
return 0;
}
float lerp(float value, float start, float end)
{
return start + (end - start) * value;
}
targetzoom = 0
currentzoom = 0
previouszoom = 0
zooming = false
void update()
{
if (move mousewheel)
{
zooming = true
targetzoom = 0.5
currentzoom = 0
}
if (zooming)
{
currentzoom = lerp(0.01, currentzoom, targetzoom)
view.Zoom(currentzoom - previouszoom) // Zoom is relative.
previouszoom = currentzoom
if (abs(targetzoom - currentzoom) < 0.1) // Lerp will never reach the target.
zooming = false
}
}
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600), "SFML window");
sf::Image Image;
Image.LoadFromFile("data/circle.png");
sf::Sprite Sprite(Image);
while (App.IsOpened())
{
sf::Event event;
while (App.GetEvent(event))
{
if (event.Type == sf::Event::Closed)
App.Close();
}
sprite.Move(20 * App.GetFrameTime(), 0);
// Clear screen
App.Clear();
App.Draw(sprite);
// Update the window
App.Display();
}
return 0;
}