1
Graphics / How to set texture for vertices array
« on: March 15, 2012, 11:39:50 pm »
Ok. i manage somehow to make solution for vs2010 and compiled debug & release libs of SMFL. Now to replace all function names to lowercase :cry:
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.
For example, to generate a Visual Studio 10 solution, you must choose "Visual Studio 10". To generate makefiles usable with Visual C++, select "NMake makefiles".
With Visual C++, you can either run CMake from the "Visual Studio command prompt" available from the start menu, or call the vcvars32.bat file of your Visual Studio installation; it will setup the environment variables properly.
> your_visual_studio_folder\VC\bin\vcvars32.bat
> cmake
#include <iostream>
#include <SFML/Graphics.hpp>
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
int main()
{
try
{
sf::RenderWindow wnd(sf::VideoMode( 200, 200, 32 ), "Test vertices");
sf::Texture tex;
if(!tex.LoadFromFile("smoke.tga"))
{
throw std::runtime_error("Failed to load texture!");
}
float tw = (float)tex.GetWidth();
float th = (float)tex.GetHeight();
sf::Transform transform;
transform.Translate(100.0f, 100.0f);
sf::RenderStates rs;
rs.Transform = transform;
rs.Texture = &tex;
rs.BlendMode = sf::BlendNone;
float size = 50.0f;
sf::Vertex vertices[6];
vertices[0].Color = sf::Color(255,255,255,255);
vertices[0].Position.x = size;
vertices[0].Position.y = size;
vertices[0].TexCoords = sf::Vector2f(tw, th);
vertices[1].Color = sf::Color(255,255,255,255);
vertices[1].Position.x = size;
vertices[1].Position.y = -size;
vertices[1].TexCoords = sf::Vector2f(tw, 0.0f);
vertices[2].Color = sf::Color(255,255,255,255);
vertices[2].Position.x = -size;
vertices[2].Position.y = -size;
vertices[2].TexCoords = sf::Vector2f(0.0f, 0.0f);
vertices[3].Color = sf::Color(255,255,255,255);
vertices[3].Position.x = size;
vertices[3].Position.y = size;
vertices[3].TexCoords = sf::Vector2f(tw, th);
vertices[4].Color = sf::Color(255,255,255,255);
vertices[4].Position.x = -size;
vertices[4].Position.y = -size;
vertices[4].TexCoords = sf::Vector2f(0.0f, 0.0f);
vertices[5].Color = sf::Color(255,255,255,255);
vertices[5].Position.x = -size;
vertices[5].Position.y = size;
vertices[5].TexCoords = sf::Vector2f(0.0f, th);
while(wnd.IsOpen())
{
sf::Event event;
while( wnd.PollEvent(event) )
{
switch(event.Type)
{
case sf::Event::Closed:
{
wnd.Close();
return false;
}break;
};
}
wnd.Clear(sf::Color(0, 0, 0));
wnd.Draw( vertices, 6, sf::Triangles, rs);
wnd.Display();
}
}
catch( const std::runtime_error& e)
{
std::cout << e.what() << std::endl;
std::cin.ignore();
return EXIT_FAILURE;
}
std::cin.ignore();
return EXIT_SUCCESS;
}
int main() {
...
sf::Texture tex;
if(!tex.LoadFromFile("smoke.tga"))
{
return 1;
}
sf::RenderStates rs;
rs.Texture = &tex;
rs.BlendMode = sf::BlendNone;
float size = 50.0f;
sf::Vector2f pos(400.0f, 300.0f);
sf::Vertex vertices[6];
vertices[0].Color = sf::Color(255,255,255,255);
vertices[0].Position.x = pos.x + size;
vertices[0].Position.y = pos.y + size;
vertices[0].TexCoords = sf::Vector2f(1.0f, 1.0f);
vertices[1].Color = sf::Color(255,255,255,255);
vertices[1].Position.x = pos.x + size;
vertices[1].Position.y = pos.y - size;
vertices[1].TexCoords = sf::Vector2f(1.0f, 0.0f);
vertices[2].Color = sf::Color(255,255,255,255);
vertices[2].Position.x = pos.x - size;
vertices[2].Position.y = pos.y - size;
vertices[2].TexCoords = sf::Vector2f(0.0f, 0.0f);
vertices[3].Color = sf::Color(255,255,255,255);
vertices[3].Position.x = pos.x + size;
vertices[3].Position.y = pos.y + size;
vertices[3].TexCoords = sf::Vector2f(1.0f, 1.0f);
vertices[4].Color = sf::Color(255,255,255,255);
vertices[4].Position.x = pos.x - size;
vertices[4].Position.y = pos.y - size;
vertices[4].TexCoords = sf::Vector2f(0.0f, 0.0f);
vertices[5].Color = sf::Color(255,255,255,255);
vertices[5].Position.x = pos.x - size;
vertices[5].Position.y = pos.y + size;
vertices[5].TexCoords = sf::Vector2f(0.0f, 1.0f);
...
wnd.Draw( vertices, 6, sf::Triangles, rs);
sf::Vertex* vert = new sf::Vertex[6];
...//fill vertices
...
??? SetTexture(???) ; // WHAT?
window.Draw( vert, 6, sf::Triangles);
#include <iostream>
// SMFL
#include <SFML/Graphics.hpp>
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
// Box2D
#include <Box2D/Box2D.h>
#pragma comment(lib, "Box2D.lib")
#define B2_PIXEL_RATIO 30.0f
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Vector2f winSize = window.GetDefaultView().GetSize();
sf::Vector2f shapePosition(winSize.x * 0.5f, winSize.y * 0.5f);
// Rect body
float radius = 20.0f;
sf::CircleShape rcShape(radius);
rcShape.SetPosition(shapePosition);
rcShape.SetFillColor(sf::Color(255,255,0,255));
b2World phWorld(b2Vec2(0, 0));
b2BodyDef shapeBodyDef;
shapeBodyDef.type = b2_dynamicBody;
shapeBodyDef.position = b2Vec2(shapePosition.x / B2_PIXEL_RATIO, shapePosition.y / B2_PIXEL_RATIO);
b2Body* shapeBody = phWorld.CreateBody(&shapeBodyDef);
b2CircleShape circleShape;
circleShape.m_radius = radius / B2_PIXEL_RATIO;
b2FixtureDef rcShapeFixDef;
rcShapeFixDef.shape = &circleShape;
rcShapeFixDef.density = 10.0f;
rcShapeFixDef.friction = 0.4f;
rcShapeFixDef.restitution = 1.0f;
shapeBody->CreateFixture(&rcShapeFixDef);
// Surround screen with static edges
b2FixtureDef groundBoxDef;
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = phWorld.CreateBody(&groundBodyDef);
b2Vec2 vs[5];
vs[0].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
vs[1].Set(winSize.x / B2_PIXEL_RATIO, winSize.y / B2_PIXEL_RATIO);
vs[2].Set(0.0f, winSize.y / B2_PIXEL_RATIO);
vs[3].Set(0.0f, 0.0f);
vs[4].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
b2ChainShape wallShape;
wallShape.CreateChain(vs, 5);
groundBoxDef.shape = &wallShape;
groundBody->CreateFixture(&groundBoxDef);
float scale = 5.0f;
shapeBody->SetLinearVelocity(b2Vec2(scale, scale));
while(window.IsOpen())
{
sf::Event event;
while (window.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
window.Close();
}
float timeStep = 1.0f / 6000.0f;
phWorld.Step(timeStep, 10, 10);
float x = shapeBody->GetPosition().x * B2_PIXEL_RATIO;
float y = shapeBody->GetPosition().y * B2_PIXEL_RATIO;
rcShape.SetPosition(x, y);
window.Clear();
window.Draw(rcShape);
window.Display();
}
return EXIT_SUCCESS;
}
#include <iostream>
// SMFL
#include <SFML/Graphics.hpp>
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
// Box2D
#include <Box2D/Box2D.h>
#pragma comment(lib, "Box2D.lib")
#define B2_PIXEL_RATIO 30.0f
class MyDebugDraw : public b2Draw
{
private:
sf::RenderWindow* wnd;
public:
MyDebugDraw(sf::RenderWindow* w) : wnd(w) {}
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{}
void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::Vertex* vert = new sf::Vertex[vertexCount];
for(int32 i = 0; i < vertexCount; ++i)
{
vert[i].Position.x = vertices[i].x * B2_PIXEL_RATIO;
vert[i].Position.y = vertices[i].y * B2_PIXEL_RATIO;
vert[i].Color.r = 255;
vert[i].Color.g = 0;
vert[i].Color.b = 0;
vert[i].Color.a = 255;
vert[i].TexCoords.x = 0.0f;
vert[i].TexCoords.y = 0.0f;
}
wnd->Draw(vert, vertexCount, sf::Lines);
delete [] vert;
}
void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color)
{}
void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color)
{}
void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
}
void DrawTransform(const b2Transform& xf)
{}
};
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
sf::Vector2f winSize = window.GetDefaultView().GetSize();
sf::Vector2f shapeSize(100.0f, 30.0f);
sf::Vector2f shapePosition(winSize.x * 0.5f, winSize.y * 0.5f);
// Rect body
sf::RectangleShape rcShape;
rcShape.SetSize(shapeSize);
rcShape.SetOrigin(shapeSize * 0.5f);
rcShape.SetPosition(shapePosition);
rcShape.SetFillColor(sf::Color(255,255,0,255));
b2World phWorld(b2Vec2(0.0f, 0.98f));
MyDebugDraw debugDraw(&window);
phWorld.SetDebugDraw(&debugDraw);
debugDraw.SetFlags(b2Draw::e_shapeBit);
b2BodyDef shapeBodyDef;
shapeBodyDef.type = b2_dynamicBody;
shapeBodyDef.position = b2Vec2(shapePosition.x / B2_PIXEL_RATIO, shapePosition.y / B2_PIXEL_RATIO);
b2Body* shapeBody = phWorld.CreateBody(&shapeBodyDef);
b2Vec2 vertices[4];
vertices[0].Set(0.0f, 0.0f);
vertices[1].Set(shapeSize.x / B2_PIXEL_RATIO, 0.0f);
vertices[2].Set(shapeSize.x / B2_PIXEL_RATIO, shapeSize.y / B2_PIXEL_RATIO);
vertices[3].Set(0.0f, shapeSize.y / B2_PIXEL_RATIO);
int32 count = 4;
b2PolygonShape rcPolyShape;
rcPolyShape.Set(vertices, 4);
b2FixtureDef rcShapeFixDef;
rcShapeFixDef.shape = &rcPolyShape;
rcShapeFixDef.density = 20.0f;
rcShapeFixDef.friction = 0.4f;
rcShapeFixDef.restitution = 0.1f;
shapeBody->CreateFixture(&rcShapeFixDef);
// Surround screen with static edges
b2FixtureDef groundBoxDef;
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body* groundBody = phWorld.CreateBody(&groundBodyDef);
b2Vec2 vs[5];
vs[0].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
vs[1].Set(winSize.x / B2_PIXEL_RATIO, winSize.y / B2_PIXEL_RATIO);
vs[2].Set(0.0f, winSize.y / B2_PIXEL_RATIO);
vs[3].Set(0.0f, 0.0f);
vs[4].Set(winSize.x / B2_PIXEL_RATIO, 0.0f);
b2ChainShape wallShape;
wallShape.CreateChain(vs, 5);
groundBoxDef.shape = &wallShape;
groundBody->CreateFixture(&groundBoxDef);
static sf::Clock clock;
while(window.IsOpen())
{
sf::Event event;
while (window.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
window.Close();
}
float timeStep = (float)clock.GetElapsedTime().AsMilliseconds() / 1000.0f;
phWorld.Step(timeStep, 10, 10);
float x = shapeBody->GetPosition().x * B2_PIXEL_RATIO;
float y = shapeBody->GetPosition().y * B2_PIXEL_RATIO;
rcShape.SetPosition(x, y);
window.Clear();
window.Draw(rcShape);
phWorld.DrawDebugData();
window.Display();
}
return EXIT_SUCCESS;
}
#include <iostream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#pragma comment(lib, "sfml-graphics-d.lib")
#pragma comment(lib, "sfml-window-d.lib")
#pragma comment(lib, "sfml-system-d.lib")
#pragma comment(lib, "sfml-audio-d.lib")
class Line : public sf::Shape
{
private:
sf::Vector2f pt[4];
public:
Line(const sf::Vector2f& pt1, const sf::Vector2f& pt2, float thick = 1.0f)
{
pt[0] = pt1 - sf::Vector2f((thick / 2.0f), (thick / 2.0f));
pt[1] = pt1 + sf::Vector2f((thick / 2.0f), (thick / 2.0f));
pt[2] = pt2 + sf::Vector2f((thick / 2.0f), (thick / 2.0f));
pt[3] = pt2 - sf::Vector2f((thick / 2.0f), (thick / 2.0f));
SetFillColor(sf::Color(255, 255, 255, 255));
}
unsigned int GetPointCount() const
{
return 4;
}
sf::Vector2f GetPoint(unsigned int index) const
{
return pt[index];
}
};
int main()
{
try
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
Line line = Line(sf::Vector2f(0.0f, 0.0f), sf::Vector2f(50.0f, 50.0f), 4.0f);
while (window.IsOpen())
{
sf::Event event;
while (window.PollEvent(event))
{
if (event.Type == sf::Event::Closed)
window.Close();
}
window.Clear();
window.Draw(line);
window.Display();
}
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}