PDA

View Full Version : SDL2 Help geting rgb colors



siv@
09-01-2015, 01:36 PM
using free pascal 2.6.2 (32) not (64)
with sdl2 and portaudio
windows 7


Im developing an audio visualizer, have gotten the oscilliscope working, but unfortunately I cannot figure out how to get the sdl_getrgb to work have no examples on the net about this( none for pascal just c which i can not make heads or tails of lol). I was able to figure out how to do it in sdl1 but Im not wanting to reprogram back to sdl1 so any one able to help me with a simple example of how to retrieve r g b color values from the x,y position in SDL2 would be great, im sure others would like to know as well. Thanks in advance to who ever can help with this. I plan to release my source code on sourceforge when it is complete.

SilverWarior
09-01-2015, 10:33 PM
If you point us to C based example we might be able to help you translate it to Pascal.

siv@
10-01-2015, 07:27 AM
Uint32 getpixel(SDL_Surface *surface, int num)
{/*pxl direct read access*//**1.0**/

int bpp = surface->format->BytesPerPixel;
/* P est l'adresse du pixel à retrouver*/
uint8_t *p = (uint8_t *)surface->pixels + num * bpp;

switch(bpp) {
case 1:
return *p;

case 2:
return *(Uint16 *)p;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;

case 4:
return *(Uint32 *)p;

default:
return 0;
}
}

best i could find for now... most other get_rgb sources i found were not reading from the screen i believe this is. this looks like it returns rgb if im understanding this.

siv@
10-01-2015, 07:55 AM
I hope the previous code works not sure how it returns the rgb, i cant find much source on sdl2's get_rgb or get_rgba..thanks again would like to use those functions but im not sure how to yet maybe someone has figured it out...any help is appreciated. If i can get the previous posted code to work then good enough for now.

User137
10-01-2015, 11:05 AM
I can't test this code, but it seems something like:


function getpixel(surface: SDL_Surface; num: integer): cardinal;
var bpp: integer;
p: PByte;
begin
bpp := surface.format.BytesPerPixel;
p := pointer(surface.pixels + num * bpp);
case bpp of
1: result := p^;
2: result := PWord(p)^;
3: begin
if (SDL_BYTEORDER = SDL_BIG_ENDIAN)
result := p[0] shl 16 or p[1] shl 8 or p[2]
else
result := p[0] or p[1] shl 8 or p[2] shl 16;
end;
4: result := PCardinal(p)^;
default: result := 0;
end;
end;

edit: If you have the pixel, you can try this function
http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlgetrgb.html
or
http://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlgetrgba.html

siv@
10-01-2015, 01:41 PM
I had trouble compiling this didnt like sdl_surface, wanted psdl_surface, so i tried that but on the bpp := line it stops compiling said invalid types got psdl_surface expected longint so I think the translation is right the original code or the variables for the pascal headers im using are a bit different the the c version no idea. Thanks for the translation. I think im going to have to use sdl1 I cant figure out how to get the pixel for getrgb or getrgba. If i figure it out some how I will post my results here.

Carver413
10-01-2015, 05:55 PM
Sdl surfaces are hardware excelerated and not easy to do pixel access perhaps this will provide a better option http://www.programmersranch.com/2014/02/sdl2-pixel-drawing.html