SFML community forums

Help => General => Topic started by: NullEntity on June 23, 2011, 05:39:47 pm

Title: Heap Corruption?
Post 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:
Code: [Select]
#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;
}
Title: Heap Corruption?
Post by: Laurent on June 23, 2011, 06:08:56 pm
Quote
Code: [Select]
new sf::Uint8[_WIDTH_ + _HEIGHT_ * 4]

Should be _WIDTH_ * _HEIGHT_ * 4 ;)
Title: Heap Corruption?
Post by: NullEntity on June 23, 2011, 06:27:41 pm
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.
Title: Heap Corruption?
Post by: NullEntity on June 23, 2011, 06:29:06 pm
Now I actually put my rendering code in the right place, but it's still not displaying.
Title: Heap Corruption?
Post by: Laurent on June 23, 2011, 06:38:27 pm
ATI graphics card?
Title: Heap Corruption?
Post by: NullEntity on June 23, 2011, 06:40:18 pm
No, I have 2 GTX260Ms in SLi.

And here's my current code:
Code: [Select]
#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;
}
Title: Heap Corruption?
Post by: NullEntity on June 23, 2011, 07:20:07 pm
Wow, I had to replace
sf::VideoMode(_WIDTH_*_ZOOM_, _HEIGHT_*_ZOOM_) with sf::VideoMode(_WIDTH_ * _ZOOM_, _HEIGHT_ * _ZOOM_)
Title: Heap Corruption?
Post by: Nexus on June 23, 2011, 07:41:40 pm
By the way:

Code: [Select]
#define _WIDTH_ 32Identifiers 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:
Code: [Select]
const unsigned int WIDTH = 32;
Title: Heap Corruption?
Post by: NullEntity on June 23, 2011, 09:18:10 pm
Can you try this? When I try replacing the defines with const ints, the window refuses to display again.
Title: Heap Corruption?
Post by: Nexus on June 23, 2011, 09:37:22 pm
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.
Title: Heap Corruption?
Post by: NullEntity on June 24, 2011, 12:08:25 am
So of course I rewrite it and it starts working fine.