SFML community forums
Help => General => Topic started by: NullEntity on June 23, 2011, 05:39:47 pm
-
I always had difficulties with pointers. I'm trying to duplicate the code here (http://www.sfml-dev.org/forum/viewtopic.php?t=3543&sid=46a4965fc6d11ea01a447f1dd003bce8) to draw pixels on the screen. I think the issue is with my pointer to Display.
Here's my code:
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#define _WIDTH_ 32
#define _HEIGHT_ 64
#define _ZOOM_ 20
int main()
{
sf::RenderWindow App(sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_), "Test");
sf::View View(sf::FloatRect(0, 0, _WIDTH_, _HEIGHT_));
View.Zoom(_ZOOM_);
sf::Image Image(_WIDTH_, _HEIGHT_, sf::Color(0, 0, 0));
sf::Sprite Sprite;
sf::Uint8 *Display = new sf::Uint8[_WIDTH_ + _HEIGHT_ * 4];
while(App.IsOpened())
{
sf::Event e;
while(App.GetEvent(e))
{
if(e.Type == sf::Event::Closed)
App.Close();
for(int i = 0; i < _WIDTH_; i++)
for(int j = 0; j < _HEIGHT_; j++)
{
Display[(j+i*_WIDTH_)*4] = i*j % 255; // R
Display[(j+i*_WIDTH_+1)*4] = i*j % 255; // G
Display[(j+i*_WIDTH_+2)*4] = i*j % 255; // B
Display[(j+i*_WIDTH_+3)*4] = 255; // A
}
App.Clear();
Image.LoadFromPixels(_WIDTH_, _HEIGHT_, Display);
Sprite.SetImage(Image);
App.Draw(Sprite);
App.SetView(View);
App.Display();
}
}
delete [] Display;
return 0;
}
-
new sf::Uint8[_WIDTH_ + _HEIGHT_ * 4]
Should be _WIDTH_ * _HEIGHT_ * 4 ;)
-
FML. It's working now, but the window isn't showing. When I put my cursor over the icon on the taskbar, I can preview it, but it's not popping up/displaying.
-
Now I actually put my rendering code in the right place, but it's still not displaying.
-
ATI graphics card?
-
No, I have 2 GTX260Ms in SLi.
And here's my current code:
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#define _WIDTH_ 32
#define _HEIGHT_ 64
#define _ZOOM_ 20
int main()
{
sf::RenderWindow App(sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_), "Test");
sf::View View(sf::FloatRect(0, 0, _WIDTH_, _HEIGHT_));
View.Zoom(_ZOOM_);
sf::Image Image(_WIDTH_, _HEIGHT_, sf::Color(0, 0, 0));
sf::Sprite Sprite;
sf::Uint8 *Display = new sf::Uint8[_WIDTH_ * _HEIGHT_ * 4];
while(App.IsOpened())
{
sf::Event e;
while(App.GetEvent(e))
{
if(e.Type == sf::Event::Closed)
App.Close();
}
for(int i = 0; i < _WIDTH_; i++)
for(int j = 0; j < _HEIGHT_; j++)
{
Display[(j+i*_WIDTH_)*4] = i*j % 255; // R
Display[(j+i*_WIDTH_)*4+1] = i*j % 255; // G
Display[(j+i*_WIDTH_)*4+2] = i*j % 255; // B
Display[(j+i*_WIDTH_)*4+3] = 255; // A
}
App.Clear();
Image.LoadFromPixels(_WIDTH_, _HEIGHT_, Display);
Sprite.SetImage(Image);
App.Draw(Sprite);
App.SetView(View);
App.Display();
}
delete [] Display;
return 0;
}
-
Wow, I had to replace
sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_) with sf::VideoMode(_WIDTH_ * _ZOOM_, _HEIGHT_ * _ZOOM_)
-
By the way:
#define _WIDTH_ 32
Identifiers beginning with underlines are reserved for the compiler and the standard library implementation, don't use them.
Anyway, a constant is much better than a macro:
const unsigned int WIDTH = 32;
-
Can you try this? When I try replacing the defines with const ints, the window refuses to display again.
-
Ah, I haven't seen before that you copy the whole array every frame. Do that once before the main loop. As soon as the main loop begins, don't access the image anymore.
And seriously, if modifications like inserting spaces into an expression or replacing a #define with const int have visible effects, then your compiler must be really broken.
-
So of course I rewrite it and it starts working fine.