Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]General Pointer/Graphics Help plz  (Read 6757 times)

0 Members and 1 Guest are viewing this topic.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« on: May 01, 2010, 09:42:35 pm »
I don't have much pointer experience so some help would be great.

I'm using sf::Uint8 pointers to store colors but they appear to be corrupted by my other pointers that I need (for image processing).

Here is a simplified example:

Code: [Select]
#include <SFML/Graphics.hpp>

#include <iostream>

void Color_Sort(sf::Uint8* color_ptr)
{

//wrong number(99)--How do I keep this as 5?
    std::cout<<"color:"<<static_cast <int>(color_ptr[0])<<'\n';

}

int main()
{
    sf::Uint8 *color_ptr=new sf::Uint8();

    sf::Uint8 *num;
    num[0]=5;

    color_ptr=num;

//correct number:
    std::cout<<"color:"<<static_cast <int>(color_ptr[0])<<'\n';

    sf::Uint8 *color_ptr2=new sf::Uint8();
    sf::Uint8 *num2;
    num2[0]=99;

    color_ptr2=num2;

    Color_Sort(color_ptr);


    delete[] color_ptr2;
    delete[] color_ptr;

    return 0;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED]General Pointer/Graphics Help plz
« Reply #1 on: May 01, 2010, 09:59:55 pm »
Code: [Select]
   sf::Uint8 *num;
    num[0]=5;
Dereferencing an uninitialized pointer leads to undefined behaviour.

Code: [Select]
   sf::Uint8 *color_ptr=new sf::Uint8();
    color_ptr=num;
Here you create a memory leak. color_ptr is overwritten, you have no chance to get the dynamically allocated memory back.

Code: [Select]
   delete[] color_ptr2;
    delete[] color_ptr;
In this code, you apply the delete[] operator to two memory areas that haven't been allocated by the new[] operator, which also evokes undefined behaviour.

The understanding of pointers and memory management is essential in C++. I recommend you to read this tutorial, or even better, the respective chapter in a good book.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #2 on: May 01, 2010, 11:19:11 pm »
Quote from: "Nexus"
Code: [Select]
   sf::Uint8 *num;
    num[0]=5;
Dereferencing an uninitialized pointer leads to undefined behaviour.


What's the difference between that and this (from the tutorial) which is "perfectly valid":

Code: [Select]

  int numbers[5];
  int * p;
  p = numbers;

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED]General Pointer/Graphics Help plz
« Reply #3 on: May 01, 2010, 11:21:52 pm »
Your code
Code: [Select]
num[0] dereferences the pointer num. Using the index operator [], it is equivalent to the expression
Code: [Select]
*(num + 0)
In contrast,
Code: [Select]
pdoes not dereference p. It just makes the pointer (and not the object behind it) a target of the assignment.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #4 on: May 01, 2010, 11:39:00 pm »
Okay, so to access the specific Uint8 value within I have to break it apart like this?

Code: [Select]
sf::Uint8 num[3];
num[0]=5;


Or is there a shortcut to do something like:

Code: [Select]
sf::Uint8 *num;
num[[0]]=5;

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED]General Pointer/Graphics Help plz
« Reply #5 on: May 02, 2010, 12:57:26 am »
Your problem is that you use pointers that don't actually point to anything valid. It's like sending a mail to someone before knowing its address.

You should really take some time to get a better understanding of how pointers work. We could try to explain it to you on this forum, but honestly it would be more efficient with a good book or tutorial ;)
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #6 on: May 02, 2010, 01:00:39 pm »
I understand what you're saying (and I have looked at many tutorials), but maybe I wasn't clear before.

My problem is specific to returning a pointer AND using new.

For example:

Code: [Select]
sf::Uint8* Get_Color()
{
    sf::Uint8 color[3];
    color[0]=255;
    return color;
}

int main()
{
    sf::Uint8* color_ptr2=new sf::Uint8();
    sf::Uint8* num2;

    num2=Get_Color();
    color_ptr2=num2;

   return 0;
}


I understand what is happening and why, but I don't know how to return a pointer into a new memory allocation.  

This is important because the pointers will not work within a loop's scope.

Is this even possible--do I have to find another solution? I've been searching for a while, but the keywords are very common.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED]General Pointer/Graphics Help plz
« Reply #7 on: May 02, 2010, 01:27:11 pm »
You should rather tell us what you're trying to do (I mean, your high level goal), so that we can find a clean and good solution. I'm sure that it won't involve such pointer manipulations.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[SOLVED]General Pointer/Graphics Help plz
« Reply #8 on: May 02, 2010, 01:31:36 pm »
I don't know what you exactly want to achieve. Maybe you should rather tell us what you want instead of how.

Your current code contains two mistakes (pointee goes out of scope, memory leak). You can't return C style arrays. You can return pointers to dynamically allocated memory, but this is dangerous, especially at complex code. Instead, you can return wrapped arrays (using std::tr1::array).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #9 on: May 02, 2010, 02:22:07 pm »
Okay, I wrote my image processor and it works great.

I used pointers of sf::Color to store palettes, etc.

Then I realized if I went down to a lower level and instead used sf::Uint8 I could do more stuff.

Okay so now I have converted all my code to run on pointers of sf::Uint8 instead.

The problem is the pointers are now going out of scope/not being accurate. When I originally wrote my code I only had a few pointers.

Now I have quite a bit and I need to protect the pointer data returned from functions.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED]General Pointer/Graphics Help plz
« Reply #10 on: May 02, 2010, 02:32:53 pm »
The only memory that you need to allocate is the image itself, after that you just need to get pointers to pixels and access their components.

For example
Code: [Select]
sf::Uint8* pixel(sf::Uint8* image, std::size_t width, std::size_t x, std::size_t y)
{
    return image + (x + width * y) * 4;
}

sf::Uint8* image = new sf::Uint8[width * height * 4];
// ... fill image data ...

// Modify the pixel at (10, 20)
sf::Uint8* p = pixel(image, width, 10, 20);
p[0] = 255;
p[1] = 128;
p[2] = 0;
p[3] = 255;

// Print the pixel at (5, 8)
sf::Uint8* p = pixel(image, width, 5, 8);
std::cout << "r = " << p[0] << " "
          << "g = " << p[1] << " "
          << "b = " << p[2] << " "
          << "a = " << p[3] << std::endl;

Of course you should wrap all this stuff into lightweight (inlined) C++ wrappers, but that's how it should look like internally.
Laurent Gomila - SFML developer

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #11 on: May 02, 2010, 03:21:02 pm »
Alright, thanks a lot!

Didn't think of filling a blank sf::Uint8

Sorry about that.

Should have looked at the constructors.

Man it's a lot faster using sf::Uint8

Finishes an image in 1 second that used to take like 20 minutes.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #12 on: May 03, 2010, 11:13:09 am »
I started testing larger images and this problem started occurring again but with my GetPixelPtr pointers.

I solved my previous problem with my custom color data because I created the data and put it into an sf::Uint8 array(which is immune to loop scope problems) and then filled my sf::Uint8 pointer image.

Maybe I need a GetPixelArray to make a pointer from?

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[SOLVED]General Pointer/Graphics Help plz
« Reply #13 on: May 03, 2010, 03:29:21 pm »
Perhaps I'm mistaken, but it looks like I need to be able to do something like this and create my own pointers:

Code: [Select]
sf::Uint8* Construct_Pixels(sf::Image write_image,int w,int h,int x,int y,sf::Uint8* dest_ptr)
{
    int range=(w*h)*4;
    int index=(x+y*w)*4;

    for(int i=0;i!=range;i+=4)
    {
        dest_ptr[i]=write_image.myPixels[i+index];
        dest_ptr[i+1]=write_image.myPixels[i+index+1];
        dest_ptr[i+2]=write_image.myPixels[i+index+2];
        dest_ptr[i+3]=write_image.myPixels[i+index+3];
    }
    return dest_ptr;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[SOLVED]General Pointer/Graphics Help plz
« Reply #14 on: May 03, 2010, 08:35:07 pm »
This can be done in one line of code:
Code: [Select]
std::vector<sf::Uint8> dest(image.GetPixelsPtr(), image.GetPixelsPtr() + image.GetWidth() * image.GetHeight() * 4);
Laurent Gomila - SFML developer