Okay, I understand the idea but if I shouldn't use setPixel for an sf::Image what should I use?I think it's update(), but check out the documentation.
Why is "filling sf::Image into sf::Texture into sf::Sprite and display it" faster than "display each pixel"? Just for my own curiosity :p.Because you have one draw call and not thousands. Issuing a function call to the graphics card can be expensive, especially when doing so all the time.
Something else: why is there no constructor for loading directly something in a texture?Because loading may fail.
Okay, I understand the idea but if I shouldn't use setPixel for an sf::Image what should I use?Not an sf::Image at all, just a raw array of sf::Uint8.
Why is "filling sf::Image into sf::Texture into sf::Sprite and display it" faster than "display each pixel"? Just for my own curiosity :p.Draw each pixels like you did, was in fact drawing a huge amount of small entities. So, compared to drawing a single sprite + updating a texture, it's much faster.
void view::drawPixel(uint8_t i, uint8_t j){
//scrImg.setPixel(i,j,sf::Color::Black);
int tmp = j*NbPixelX+i;
pixels[tmp] = sf::Color::Black.r;
pixels[tmp+1] = sf::Color::Black.g;
pixels[tmp+2] = sf::Color::Black.b;
pixels[tmp+3] = sf::Color::Black.a;
}
(Later I use sf::texture.update() like you proposed)why not just declare it like:Because it would be declared on the stack, and the stack is not that big (--> stack overflow). If you declare it as a global variable it would work, though. But NbPixelX/Y would have to be constants.
"uint8_t pixels[NbPixelX*NbPixelY*4];"?
(And btw, is there any difference between sf::uint8 and uint8_t?)No, both are 8 bits unsigned integers. There's one from SFML and one from the C++11 standard library.
I can fill the texture with white with a simple loop, but when it comes to draw one pixel with the function above, I got some "random" colors. Therefore I assume I'm not at the right adress of pixels[] ?Yep, you must multiply the index by 4. Sorry it's my fault, my example was wrong.
Where am I wrong?
// Check if state changed
if(Periph->state_changed){
Periph->state_changed = false;
geometry::initialization(Periph->state);
}
// First Drawing Call
geometry::drawContext(App);
// Depending on View Type
switch(Periph->state){
case VIEW_EMPTY:
geometry::drawTitle(App);
break;
case VIEW_FULLSCREEN_LIGHT:
case VIEW_LIGHT:
for(uint8_t i=0;i<NbEm;i++) // For each diode of the Bezel
for(uint8_t j=0;j<NbPd;j++) // For each group of 8 OPD ( 8 OPD per Bezel Module )
if( (Device->USBData.projection[diodeX3::whichCycle[i]][j/8]&obstacle::NumberToPd[j%8]) != 0 && pdRange::areLinked(i,j) ) geometry::addLine(i,j);
geometry::drawLine(App);
break;
case VIEW_FULLSCREEN_OBSTACLE:
case VIEW_OBSTACLE:
for(uint8_t i=0;i<NbEm;i++) // For each diode of the Bezel
for(uint8_t j=0;j<NbPd;j++) // For each OPD ( 8 OPD per Bezel Module )
if( (Device->USBData.projection[diodeX3::whichCycle[i]][j/8]&obstacle::NumberToPd[j%8]) == 0 && pdRange::areLinked(i,j) ) geometry::addLine(i,j);
geometry::drawLine(App);
break;
case VIEW_FULLSCREEN_PROCESS:
case VIEW_PROCESS:
geometry::clearPixels();
processPixels(PROCESS_MODE_AVERAGE);
geometry::drawPixels(App);
break;
default:
break;
}
// Refresh Screen
App->display();
void geometry::addBezel(){
// Filling Screen first (order in which it's filled is really important, else it does not work properly)
ArrayQuad[4].position = sf::Vector2f(ScreenRef.x, ScreenRef.y);
ArrayQuad[5].position = sf::Vector2f(ScreenRef.x+ScreenSize.x, ScreenRef.y);
ArrayQuad[6].position = sf::Vector2f(ScreenRef.x+ScreenSize.x, ScreenRef.y+ScreenSize.y);
ArrayQuad[7].position = sf::Vector2f(ScreenRef.x, ScreenRef.y+ScreenSize.y);
for(uint8_t i=0;i<4;i++) ArrayQuad[i].color = sf::Color::White;
// Filling Bezel second
ArrayQuad[0].position = BezelRef;
ArrayQuad[1].position = sf::Vector2f(BezelRef.x+BezelSize.x,BezelRef.y);
ArrayQuad[2].position = sf::Vector2f(BezelRef.x+BezelSize.x,BezelRef.y+BezelSize.y);
ArrayQuad[3].position = sf::Vector2f(BezelRef.x,BezelRef.y+BezelSize.y);
for(uint8_t i=0;i<4;i++) ArrayQuad[i].color = sf::Color(230,216,174);
}
void geometry::addEm(){
// Processing Positions
initEm();
// Filling each emitters
for(uint8_t i=0;i<66;i++){
ArrayQuad[(2+i)*4].position = sf::Vector2f(TabEm[i].x-SIZE_EmX/2,TabEm[i].y-SIZE_EmY/2);
ArrayQuad[(2+i)*4+1].position = sf::Vector2f(TabEm[i].x+SIZE_EmX/2,TabEm[i].y-SIZE_EmY/2);
ArrayQuad[(2+i)*4+2].position = sf::Vector2f(TabEm[i].x+SIZE_EmX/2,TabEm[i].y+SIZE_EmY/2);
ArrayQuad[(2+i)*4+3].position = sf::Vector2f(TabEm[i].x-SIZE_EmX/2,TabEm[i].y+SIZE_EmY/2);
for(uint8_t j=0;j<4;j++) ArrayQuad[(2+i)*4+j].color = sf::Color::Red;
}
}
void geometry::addRcv(){
// Processing Positions
initRcv();
// Filling each recivers
for(uint8_t i=0;i<176;i++){
ArrayQuad[(2+66+i)*4].position = sf::Vector2f(TabPd[i].x-SIZE_PdX/2,TabPd[i].y-SIZE_PdY/2);
ArrayQuad[(2+66+i)*4+1].position = sf::Vector2f(TabPd[i].x+SIZE_PdX/2,TabPd[i].y-SIZE_PdY/2);
ArrayQuad[(2+66+i)*4+2].position = sf::Vector2f(TabPd[i].x+SIZE_PdX/2,TabPd[i].y+SIZE_PdY/2);
ArrayQuad[(2+66+i)*4+3].position = sf::Vector2f(TabPd[i].x-SIZE_PdX/2,TabPd[i].y+SIZE_PdY/2);
for(uint8_t j=0;j<4;j++) ArrayQuad[(2+66+i)*4+j].color = sf::Color::Blue;
}
}
void geometry::initialization(uint8_t state){
initConstants(state);
addBezel();
addEm();
addRcv();
}
// Draw Bezel, Screen, Emitters and Receivers
void geometry::drawContext(sf::RenderWindow *App){ App->draw(ArrayQuad); }
// Draw a "Welcome" Text on default view
void geometry::drawTitle(sf::RenderWindow *App){ App->draw(Title); }
// Add a line to draw in the array of Line
void geometry::addLine(uint8_t Em, uint8_t Pd){
ArrayLine.push_back(sf::Vertex(TabEm[Em],sf::Color::Blue));
ArrayLine.push_back(sf::Vertex(TabPd[Pd],sf::Color::Blue));
}
// Draw every line in the array
void geometry::drawLine(sf::RenderWindow *App){ App->draw(&ArrayLine[0], ArrayLine.size(), sf::Lines); ArrayLine.clear(); }