3
« on: April 04, 2012, 12:48:28 pm »
I tested some things and noticed that it worked if i did this:
---Thread 1---
window->setActive("true");
sf::Sprite sp;
sp.loadfromtexture(text);
window->setActive("false");
Do i really need to do this to draw an image?
Draw.h
#ifndef INCLUDED_DRAW
#define INCLUDED_DRAW
#include <vector>
#include <list>
#include <SFML\System\Thread.hpp>
namespace sf{
class Drawable;
class RenderWindow;
class Image;
class Clock;
template <typename T>
class Vector2;
typedef Vector2<int> Vector2i;
};
class Draw{
public:
typedef std::vector<std::list<sf::Drawable*>> VectorWithListWithDrawable;
typedef std::list<sf::Drawable*> ListWithDrawable;
enum WindowStyle{
Window=4,
Fullscreen=8,
Resize=2,
Titlebar=1,
None=0
};
Draw(sf::Vector2i Windowsize,unsigned int Bit,std::string NameOfWindow,int style=4);
Draw(int x,int y,unsigned int Bit,std::string NameOfWindow,int style=4);
~Draw();
//Draw options
void draw();
void addSprite(sf::Drawable*, int Layer);
void clearDrawBuffer();
void clearInsertBuffer();
sf::Image* getScreenDump();
//Settings
bool isOpen();
void threadWait();
void FPS();
void setWindowIcon(std::string IconPath);
sf::RenderWindow* getWindow();
private:
void Run();
void privatedraw();
void swapBuffer();
VectorWithListWithDrawable* ActualBuffer;
VectorWithListWithDrawable* BackBuffer;
sf::RenderWindow* ActualWindow;
sf::Image* WindowIcon;
sf::Clock* time;
int FPSCount;
bool screenShot;
sf::Image* screndump;
sf::Thread* drawThread;
};
#endif
Draw.cpp
#include "Draw.h"
#include <assert.h>
#include <iostream>
#include <SFML/Graphics.hpp>
Draw::Draw(sf::Vector2i Windowsize,unsigned int Bit,std::string NameOfWindows,int style){
ActualWindow = new sf::RenderWindow(sf::VideoMode(Windowsize.x, Windowsize.y, Bit), NameOfWindows,style);
ActualWindow->setActive(false);
ActualBuffer = new VectorWithListWithDrawable;
BackBuffer = new VectorWithListWithDrawable;
time=new sf::Clock();
WindowIcon=0;
FPSCount=0;
screenShot=false;
drawThread = new sf::Thread(&Draw::Run,this);
}
Draw::Draw(int x,int y,unsigned int Bit,std::string NameOfWindows,int style){
ActualWindow = new sf::RenderWindow(sf::VideoMode(x,y, Bit), NameOfWindows,style);
ActualWindow->setActive(false);
ActualBuffer = new VectorWithListWithDrawable;
BackBuffer = new VectorWithListWithDrawable;
time=new sf::Clock();
WindowIcon=0;
FPSCount=0;
screenShot=false;
drawThread = new sf::Thread(&Draw::Run,this);
}
Draw::~Draw(){
drawThread->wait();
ActualWindow->close();
delete ActualWindow;
delete WindowIcon;
delete time;
drawThread->terminate();
delete drawThread;
}
void Draw::privatedraw(){
ActualWindow->setActive(true);
ActualWindow->clear(sf::Color(0,0,0,255));
for(unsigned int i=0;i<(int)BackBuffer->size();i++){
for(ListWithDrawable::iterator it=BackBuffer->at(i).begin();it!=BackBuffer->at(i).end();it++){
ActualWindow->draw(*(*it));
}
}
ActualWindow->display();
clearDrawBuffer();
if(screenShot){
screndump=new sf::Image(ActualWindow->capture());
screenShot=false;
}
}
void Draw::clearDrawBuffer(){
for(int i = 0;i<(int)BackBuffer->size();i++){
for(ListWithDrawable::iterator it=BackBuffer->at(i).begin();it!=BackBuffer->at(i).end();it++){
delete (*it);
}
BackBuffer->at(i).clear();
}
}
void Draw::clearInsertBuffer(){
for(int i = 0;i<(int)ActualBuffer->size();i++){
for(ListWithDrawable::iterator it=ActualBuffer->at(i).begin();it!=ActualBuffer->at(i).end();it++){
delete (*it);
}
ActualBuffer->at(i).clear();
}
}
sf::Image* Draw::getScreenDump(){
this->screenShot=true;
drawThread->wait();
return screndump;
}
void Draw::addSprite(sf::Drawable* s, int Layer){
sf::Drawable* drawObj=0;
if(sf::Sprite* sp = dynamic_cast<sf::Sprite*>(s)){
drawObj = new sf::Sprite(*sp);
}
if(Layer>=(int)ActualBuffer->size()){
while(Layer>(int)ActualBuffer->size()-1){
ActualBuffer->push_back(Draw::ListWithDrawable());
}
}
if(Layer>=0){
ActualBuffer->at(Layer).push_back(drawObj);
}
else{
ActualBuffer->at(0).push_back(drawObj);
}
}
void Draw::threadWait(){
drawThread->wait();
}
bool Draw::isOpen(){
return ActualWindow->isOpen();
}
void Draw::FPS(){
FPSCount++;
if(time->getElapsedTime().asMilliseconds()>1){
std::cout<<"FPS: "<<FPSCount<<std::endl;
time->restart();
FPSCount=0;
}
}
sf::RenderWindow* Draw::getWindow(){
return ActualWindow;
}
void Draw::setWindowIcon(std::string IconPath){
if(WindowIcon!=0){
delete WindowIcon;
}
WindowIcon=new sf::Image();
WindowIcon->loadFromFile(IconPath);
ActualWindow->setIcon(32,32,WindowIcon->getPixelsPtr());
}
void Draw::Run(){
privatedraw();
}
void Draw::draw(){
drawThread->wait();
swapBuffer();
drawThread->launch();
}
void Draw::swapBuffer(){
VectorWithListWithDrawable* temp=BackBuffer;
BackBuffer=ActualBuffer;
ActualBuffer=temp;
}
main.cpp
#include "Draw.h"
#include "SFML\Graphics.hpp"
#include "ResourceManager.h"
#include <iostream>
int main(){
Draw* d=new Draw(1024,768,32,"Test");
sf::Event ev;
sf::Image im(/*RecourceManager::getImageHalfSize(*/RecourceManager::getImage("dragons7.jpg")/*)*/);
sf::Texture* text=new sf::Texture();
text->loadFromImage(im);
sf::Sprite* sp=new sf::Sprite();
sp->setTexture(*text);
while(d->isOpen()){
while(d->getWindow()->pollEvent(ev)){
if (ev.type == sf::Event::Closed)
d->getWindow()->close();
}
d->addSprite(sp,0);
d->draw();
}
d->threadWait();
delete sp;
delete d;
return 0;
}