I just start to play with C++ and SFML.
I use Code::Blocks with TDM-GCC 4.9.2, 64 bit and SFML-2.3.1-windows-gcc-4.8.1-tdm-64-bit.zip.
My system is: Windows 7 64 bit, NVIDIA GeForce GT610
I try to make window with effect of snowflakes as on the old televisions. Make two variants. The PROBLEMS are the same:
- flickering
- when I set
texture (window) size - power of 2 (as I read that this is faster for the graphic card), on the screen are almost
static image of dots, more or less grouped in rows or columns.
- at different size of the screen - the effects are different - flickering (the best case, shown in the next examples), or "moving" random pixels...
The VerticalSync or FrameRate do not change anything special.
Is proper this my code or I make some error logic?
First variant:
----------------
(with Texture from Image)
#include <sstream>
#include <time.h>
#include <stdlib.h>
using namespace std;
string num2str(int i){
stringstream s;
s << i;
return s.str();
}
int main()
{
const unsigned int s = 500;
unsigned char c;
sf::RenderWindow window(sf::VideoMode(s, s),
"Random",
sf::Style::Close);
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
sf::Image img;
img.create(s, s, sf::Color(0, 0, 0));
sf::Texture texture;
sf::Sprite sprite;
//window.setTitle(num2str(sf::Texture::getMaximumSize())); // for my card this is 16384
texture.loadFromImage(img);
sprite.setTexture(texture, true);
srand(time(0));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed) window.close();
for (unsigned int i=0; i<s; i++)
for (unsigned int j=0; j<s; j++){
c = (rand()%2 == 0) ? 0 : 255;
img.setPixel(i, j, sf::Color(c, c, c));
}
texture.update(img);
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}
Second variant:
--------------------
(with Texture from Array)
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdlib.h>
using namespace std;
string num2str(int i){
stringstream s;
s << i;
return s.str();
}
int main()
{
const unsigned int s = 500;
const unsigned int sz = s*s*4;
sf::Uint8 px[sz];
for (unsigned int i=3; i<sz; i+=4) px[i] = 255; // set alfa once
sf::RenderWindow window(sf::VideoMode(s, s),
"Random",
sf::Style::Close);
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
sf::Texture texture;
texture.create(s, s);
sf::Sprite sprite;
//window.setTitle(num2str(sf::Texture::getMaximumSize()));
//window.setTitle(num2str(RAND_MAX));
sprite.setTexture(texture);
srand(time(0));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
if (event.type == sf::Event::Closed) window.close();
/* [b]I try this to optimize the random fast pixel choice, but the screen become black with only little white random dots[/b]
for (unsigned int i=0; i<sz-2*16*4; i+=16*4){
c = rand();
for (unsigned int j=0; j<16; j++){
px[i+j] = (((c >> j) & 1u) == 0) ? 0 : 255;
px[i+j+1] = px[i+j]; px[i+j+2] = px[i+j];
px[i+j+3] = 255;
}
}
*/
for (unsigned int i=0; i<sz-4; i+=4){
px[i] = (rand()%2 == 0) ? 0 : 255;
px[i+1] = px[i]; px[i+2] = px[i];
}
texture.update(px);
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}