Skip to content

Instantly share code, notes, and snippets.

@lucasnlm
Created May 7, 2014 01:06
Show Gist options
  • Save lucasnlm/55d85e55315ced4aa5f5 to your computer and use it in GitHub Desktop.
Save lucasnlm/55d85e55315ced4aa5f5 to your computer and use it in GitHub Desktop.
#include <Windows.h> // Para a janela de selecionar o arquivo
#include <string>
#include <cstring>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
// Abre a janela para selecionar uma imagem.
sf::Texture LoadTexture(void);
int main()
{
sf::Texture textura = LoadTexture();
// Vincula a texture ao sprite.
sf::Sprite sprite;
sprite.setTexture(textura);
sf::RenderWindow window;
window.create(sf::VideoMode(textura.getSize().x, textura.getSize().y), "Janela");
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(30);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(sprite);
window.display();
}
return 0;
}
sf::Texture LoadTexture(void)
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = 0;
ofn.lpstrDefExt = 0;
ofn.lpstrFile = new TCHAR[512];
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = 512;
ofn.lpstrFilter = NULL;
ofn.nFilterIndex = 0;
ofn.lpstrInitialDir = NULL;
ofn.lpstrTitle = L"Selecione uma Imagem";
ofn.Flags = 0;
GetOpenFileName(&ofn);
// Converte para std::string.
std::wstring wstr = ofn.lpstrFile;
std::string str(wstr.begin(), wstr.end());
// Armazenará a imagem.
sf::Texture texture;
// Verifica se o arquivo é válido e se foi possível carregar.
if (!texture.loadFromFile(str))
{
MessageBox(NULL, L"Imagem inválida!", L"Erro", MB_OK);
}
return texture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment