Author Topic: Gradient test is failing why ? (partially solved, no pitch yet)  (Read 2023 times)

Offline Skybuck

  • Colonel
  • ****
  • Posts: 223
    • View Profile
Code: [Select]
void SpecialGradientTest( SDL_Surface *ParaSurface )
{
int x, y;
int w, h, p, b;
int offset;
SDL_Color color;
SDL_Color *pointer;

h = ParaSurface->h;
w = ParaSurface->w;
p = ParaSurface->pitch;
b = ParaSurface->format->BytesPerPixel;
pointer = (SDL_Color*)(ParaSurface->pixels);


for (y=0; y<h; y++)
{
for (x=0; x<w; x++)
{
offset = y * p + x * b;

color.r = x & 255;
color.b = y & 255;
color.g = 0;

pointer[offset] = color;

// pointer = (SDL_Color *)ParaSurface->pixels + (y * ParaSurface->pitch + x * ParaSurface->format->BytesPerPixel);
// *pointer = color;
}
}
}

Quote
*/
void Screen::flip()
{
   if (getWidth() != _baseWidth || getHeight() != _baseHeight || useOpenGL())
   {
      Zoom::flipWithZoom(_surface->getSurface(), _screen, _topBlackBand, _bottomBlackBand, _leftBlackBand, _rightBlackBand, &glOutput);
   }
   else
   {
//      _surface->lock();
//      SpecialGradientTest( _surface->getSurface() );
//      _surface->unlock();

      SDL_BlitSurface(_surface->getSurface(), 0, _screen, 0);
      SpecialGradientTest( _screen );
   }

^ Tried two different things...

Producing access violations ? Why ? both surfaces are set to 32 bits per pixel it shows during debugging...

This code sets it to 32 bits:

Code: [Select]

void Screen::resetDisplay(bool resetVideo)
{
int width = Options::displayWidth;
int height = Options::displayHeight;
#ifdef __linux__
Uint32 oldFlags = _flags;
#endif
makeVideoFlags();

if (!_surface || (_surface->getSurface()->format->BitsPerPixel != _bpp ||
_surface->getSurface()->w != _baseWidth ||
_surface->getSurface()->h != _baseHeight)) // don't reallocate _surface if not necessary, it's a waste of CPU cycles
{
if (_surface) delete _surface;
// _surface = new Surface(_baseWidth, _baseHeight, 0, 0, Screen::use32bitScaler() ? 32 : 8); // only HQX/XBRZ needs 32bpp for this surface; the OpenGL class has its own 32bpp buffer
_surface = new Surface(_baseWidth, _baseHeight, 0, 0, 32); // only HQX/XBRZ needs 32bpp for this surface; the OpenGL class has its own 32bpp buffer
« Last Edit: May 30, 2022, 07:45:46 pm by Skybuck »

Offline Skybuck

  • Colonel
  • ****
  • Posts: 223
    • View Profile
Re: Gradient test is failing why ?
« Reply #1 on: May 30, 2022, 07:36:54 pm »
Even with SDL locking it still crashes/access violations ?

It's like there are not enough pixels allocated ? HUH ?

Quote
void SpecialGradientTest( SDL_Surface *ParaSurface )
{
   int x, y;
   int w, h, p, b;
   int offset;
   SDL_Color color;
   SDL_Color *pointer;

   h = ParaSurface->h;
   w = ParaSurface->w;
   p = ParaSurface->pitch;
   b = ParaSurface->format->BytesPerPixel;
   pointer = (SDL_Color*)(ParaSurface->pixels);

   SDL_LockSurface( ParaSurface );

   for (y=0; y<h; y++)
   {
      for (x=0; x<w; x++)
      {
         offset = y * p + x * b;

         color.r = x & 255;
         color.b = y & 255;
         color.g = 0;

         pointer[offset] = color;

//         pointer = (SDL_Color *)ParaSurface->pixels + (y * ParaSurface->pitch + x * ParaSurface->format->BytesPerPixel);
//         *pointer = color;
      }
   }

    SDL_UnlockSurface( ParaSurface );
}

Offline Skybuck

  • Colonel
  • ****
  • Posts: 223
    • View Profile
Re: Gradient test is failing why ?
« Reply #2 on: May 30, 2022, 07:41:03 pm »
At least I see problem with pointer[offset].

C++ will compute address based on data type which is 4 bytes, not 1.

However this does not yet explain why the poiner math below it fails as well, last time I tried...

//         pointer = (SDL_Color *)ParaSurface->pixels + (y * ParaSurface->pitch + x * ParaSurface->format->BytesPerPixel);
//         *pointer = color;

Will look into this and adjust formula to not multiply by 4.

OK, fixed it, don't feel completely comfortable, leaving pitch out, but for now it works :)  :

Code: [Select]
void SpecialGradientTest( SDL_Surface *ParaSurface )
{
int x, y;
int w, h, p, b;
int offset;
SDL_Color color;
SDL_Color *pointer;

h = ParaSurface->h;
w = ParaSurface->w;
p = ParaSurface->pitch;
b = ParaSurface->format->BytesPerPixel;
pointer = (SDL_Color*)(ParaSurface->pixels);

SDL_LockSurface( ParaSurface );

for (y=0; y<h; y++)
{
for (x=0; x<w; x++)
{
offset = y * w + x;

color.r = x & 255;
color.b = y & 255;
color.g = 0;

pointer[offset] = color;

// pointer = (SDL_Color *)ParaSurface->pixels + (y * ParaSurface->pitch + x * ParaSurface->format->BytesPerPixel);
// *pointer = color;
}
}

    SDL_UnlockSurface( ParaSurface );
}
« Last Edit: May 30, 2022, 07:42:59 pm by Skybuck »