Author Topic: Ufo Navigation  (Read 7329 times)

0 Members and 2 Guests are viewing this topic.

JRS

  • Guest
Re: Ufo Navigation
« Reply #15 on: March 05, 2014, 10:43:03 AM »
Thanks Mike for the explanation. That clears up a few things for me. I'm going to try and use Charles's SHIFT function he contributed to the project.

Glad to see you posting again after that nasty round with the flu.


@Peter: I have tried the examples you posted but without the info Mike provided it wasn't clear what to expect as a result.





Mike Lobanovsky

  • Guest
Re: Ufo Navigation
« Reply #16 on: March 05, 2014, 11:06:42 AM »
You're welcome John and thanks for your care.

I saw your broadcast in my mailbox and I'll email you back tomorrow. I do a quick glance-through of the BASIC forums every day but I hardly post anything unless I have to, and your question was very easy to respond to. The rest will go in my tomorrow's email.

Regards,

JRS

  • Guest
Re: Ufo Navigation
« Reply #17 on: March 05, 2014, 11:29:02 AM »
I think we are close. (guess)

Code: [Select]
'Peter's Pixel Test - SB GFX

IMPORT gfx.inc

s = gfx::Window(320, 240, "Peter's Pixel Test - SB GFX")
RANDOMIZE(gfx::Time())
gfx::ClearScreen s, 0xffffff
ts = gfx::Time()
FOR y = 0 TO 240
  FOR x = 0 TO 320
    gfx::pixelRGBA s, x, y, RND() AND 255, RND() AND 255, RND() AND 255, 255
  NEXT
NEXT
c = gfx::GetPixel(s, 20, 20)
PRINT "Color: ",c,"\n"
r = c AND 0xff
PRINT "R: ",r,"\n"
g = c AND 0xFF00
PRINT "G: ",g,"\n"
b = c AND 0xFF0000
PRINT "B: ",b,"\n"
a = c AND 0xFF000000
PRINT "A: ",a,"\n"
GOTO done

gfx::filledCircleRGBA s, 100, 100, 80, r, g, b, a
PRINT "Got Here\n"
GOTO done

c = gfx::GetPixel(s, 20, 20)
r = c AND x0FF
g = c AND x0FF00
b = c AND x0FF0000
a = c AND x0FF000000
gfx::boxRGBA s, 100, 20, 80, 60, r, g, b, a
gfx::Update
te = gfx::Time()
gfx::stringColor s, 20, 215,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
WHILE gfx::KeyName(1) <> "+escape"
WEND
done:
gfx::Close

jrs@laptop:~/sb/sb22/gfx$ scriba ppt.sb
Color: 8673871
R: 79
G: 23040
B: 8650752
A: 0
jrs@laptop:~/sb/sb22/gfx$

When I get to the point of getting valid RGBA values, I will print the RGBA values assigned for the pixel locations we retrieve to make sure apples are still apples.  :o

Charles Pegge

  • Guest
Re: Ufo Navigation
« Reply #18 on: March 05, 2014, 11:44:35 AM »

Hi John,

A quick glance at your code: some bit-shifting is required >>

r = c AND x0FF
g = (c AND x0FF00)>>8
b = (c AND x0FF0000)>>16
a = c >>24

JRS

  • Guest
Re: Ufo Navigation
« Reply #19 on: March 05, 2014, 12:06:22 PM »
Looks good. (guess until validation against values actually used to create the pixel)

I would of thought that the Alpha value would have been 255 as that is how it was created.

Code: [Select]
c = gfx::GetPixel(s, 20, 20)
PRINT "Color: ",c,"\n"
r = c AND 0xff
PRINT "R: ",r,"\n"
g = gfx::Shift((c AND 0xFF00),-8)
PRINT "G: ",g,"\n"
b = gfx::Shift((c AND 0xFF0000), -16)
PRINT "B: ",b,"\n"
a = gfx::Shift(c, -24)
PRINT "A: ",a,"\n"

jrs@laptop:~/sb/sb22/gfx$ scriba ppt.sb
Color: 3151299
R: 195
G: 21
B: 48
A: 0
jrs@laptop:~/sb/sb22/gfx$

JRS

  • Guest
Re: Ufo Navigation
« Reply #20 on: March 05, 2014, 12:14:31 PM »
I thought I would do some cross checking to validate we are looking at the same pixel and what we set is what we get.

Code: [Select]
'Peter's Pixel Test - SB GFX

IMPORT gfx.inc

s = gfx::Window(320, 240, "Peter's Pixel Test - SB GFX")
RANDOMIZE(gfx::Time())
gfx::ClearScreen s, 0xffffff
ts = gfx::Time()
FOR y = 0 TO 240
  FOR x = 0 TO 320
    r = RND() AND 255
    g = RND() AND 255
    b = RND() AND 255
    IF x = 20 AND y = 20 THEN
      PRINT "Set R: ",r,"\n"
      PRINT "Set G: ",g,"\n"
      PRINT "Set B: ",b,"\n"           
    END IF
    gfx::pixelRGBA s, x, y, r, g, b, 255
  NEXT
NEXT
c = gfx::GetPixel(s, 20, 20)
PRINT "Color: ",c,"\n"
r = c AND 0xff
PRINT "Get R: ",r,"\n"
g = gfx::Shift((c AND 0xFF00),-8)
PRINT "Get G: ",g,"\n"
b = gfx::Shift((c AND 0xFF0000), -16)
PRINT "Get B: ",b,"\n"
a = gfx::Shift(c, -24)
PRINT "Get A: ",a,"\n"
GOTO done

r = c AND x0FF
g = (c AND x0FF00)>>8
b = (c AND x0FF0000)>>16
a = c >>24

gfx::filledCircleRGBA s, 100, 100, 80, r, g, b, a
PRINT "Got Here\n"
GOTO done

c = gfx::GetPixel(s, 20, 20)
r = c AND x0FF
g = c AND x0FF00
b = c AND x0FF0000
a = c AND x0FF000000
gfx::boxRGBA s, 100, 20, 80, 60, r, g, b, a
gfx::Update
te = gfx::Time()
gfx::stringColor s, 20, 215,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
WHILE gfx::KeyName(1) <> "+escape"
WEND
done:
gfx::Close

jrs@laptop:~/sb/sb22/gfx$ scriba ppt.sb
Set R: 247
Set G: 134
Set B: 201
Color: 16221897
Get R: 201
Get G: 134
Get B: 247
Get A: 0
jrs@laptop:~/sb/sb22/gfx$

Charles Pegge

  • Guest
Re: Ufo Navigation
« Reply #21 on: March 05, 2014, 01:40:25 PM »
Easier to verify if you can display in hexadecimal. (the invention of base-10 numbers was an ancient disaster. :) )

JRS

  • Guest
Re: Ufo Navigation
« Reply #22 on: March 05, 2014, 02:00:23 PM »
I swapped R & B but Alpha seems unresponsive.  :(

Code: [Select]
FOR y = 0 TO 240
  FOR x = 0 TO 320
    r = RND() AND 255
    g = RND() AND 255
    b = RND() AND 255
    IF x = 20 AND y = 20 THEN
      PRINT "Set R: ",r,"\n"
      PRINT "Set G: ",g,"\n"
      PRINT "Set B: ",b,"\n"           
    END IF
    gfx::pixelRGBA s, x, y, r, g, b, 255
  NEXT
NEXT
c = gfx::GetPixel(s, 20, 20)
PRINT FORMAT("Color: %0X",c),"\n"
' r = c AND 0xff
r = gfx::Shift((c AND 0xFF0000), -16)
PRINT FORMAT("Get R: %0X",r),"\n"
g = gfx::Shift((c AND 0xFF00),-8)
PRINT FORMAT("Get G: %0X",g),"\n"
' b = gfx::Shift((c AND 0xFF0000), -16)
b = c AND 0xff
PRINT FORMAT("Get B: %0X",b),"\n"
a = gfx::Shift(c, -24)
PRINT FORMAT("Get A: %0X",a),"\n"
GOTO done

jrs@laptop:~/sb/sb22/gfx$ scriba ppt.sb
Set R: 183
Set G: 148
Set B: 238
Color: B794EE
Get R: B7
Get G: 94
Get B: EE
Get A: 0
jrs@laptop:~/sb/sb22/gfx$

JRS

  • Guest
Re: Ufo Navigation
« Reply #23 on: March 05, 2014, 02:23:42 PM »
I'm assuming at this point that the color value being returned doesn't include the alpha channel. I forced it to 255 (solid color) and will call this example completed unless there is something I'm missing. It would be nice to know how to add an alpha value to a returned value from gfx::GetPixel without having to break it down into components. Almost all GFX functions have paired functions. One takes a single color value in the form of 0XRRGGBBAA and the other is passed in its components. Thanks to all that helped get this working!

BTW I thought if I subtracted a known RGB value from a RGBA value, the difference should be the magic number I'm looking for. Not true grasshopper.  >:(



Code: [Select]
'Peter's Pixel Test - SB GFX

IMPORT gfx.inc

s = gfx::Window(320, 240, "Peter's Pixel Test - SB GFX")
RANDOMIZE(gfx::Time())
gfx::ClearScreen s, 0xffffff
ts = gfx::Time()
FOR y = 0 TO 240
  FOR x = 0 TO 320
    r = RND() AND 255
    g = RND() AND 255
    b = RND() AND 255
    gfx::pixelRGBA s, x, y, r, g, b, 255
  NEXT
NEXT
c = gfx::GetPixel(s, 20, 20)
r = gfx::Shift((c AND 0xFF0000), -16)
g = gfx::Shift((c AND 0xFF00),-8)
b = c AND 0xFF
gfx::filledCircleRGBA s, 100, 100, 80, r, g, b, 255
c = gfx::GetPixel(s, 10, 10)
r = gfx::Shift((c AND 0xFF0000), -16)
g = gfx::Shift((c AND 0xFF00),-8)
b = c AND 0xFF
gfx::boxRGBA s, 100, 20, 80, 60, r, g, b, 255
gfx::Update
te = gfx::Time()
gfx::stringColor s, 20, 215,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close
« Last Edit: March 05, 2014, 06:55:10 PM by John »

JRS

  • Guest
Re: Ufo Navigation
« Reply #24 on: March 05, 2014, 07:59:12 PM »
I was able to return a pixel color with an alpha value.  I had to create a blank surface with pixel masks defined to get an alpha extended value. It looks as if the returned color value is in ABGR. What would the mask  / shift be to extract it to a variable in RGBA format?


jrs@laptop:~/sb/sb22/gfx$ scriba ppt.sb
Set R: 88
Set G: 52
Set B: 117
FF753458
« Last Edit: March 05, 2014, 08:45:06 PM by John »

JRS

  • Guest
Re: Ufo Navigation
« Reply #25 on: March 05, 2014, 09:30:25 PM »
I think I got this resolved. It looks like the code I used to set the pixel masks in the ext. module for creating a new surface is reversed. Forcing a SDL_BIG_ENDIAN mask set solved the problem so I could use the results of gfx::GetPixel directly which included the alpha channel. It would be nice to be able to define the mask when the initial surface is created. I think this is a bug in SDL 1.2 and the workaround SDL_SetAlpha(screen, SDL_SRCALPHA, 0); didn't seem to work returning alpha values when getting pixel info.

@Peter - Thanks for posting this example which lead to a day of battling with SDL. You forced me to address this crazy pixel unraveling which until now had been successfully avoided.  ::)

Code: [Select]
//IF (SDL_BYTEORDER EQ SDL_BIG_ENDIAN) THEN
    rmask = 0xff000000;
    gmask = 0x00ff0000;
    bmask = 0x0000ff00;
    amask = 0x000000ff;
//ELSE
//  rmask = 0x000000ff;
//  gmask = 0x0000ff00;
//  bmask = 0x00ff0000;
//  amask = 0xff000000;
//END_IF



Code: [Select]
'Peter's Pixel Test - SB GFX

IMPORT gfx.inc

vs = gfx::Window(320, 240, "Peter's Pixel Test - SB GFX")
s = gfx::CreateSurface(320, 240, 32)
RANDOMIZE(gfx::Time())
ts = gfx::Time()
FOR y = 0 TO 240
  FOR x = 0 TO 320
    r = RND() AND 255
    g = RND() AND 255
    b = RND() AND 255
    gfx::pixelRGBA s, x, y, r, g, b, 255
  NEXT
NEXT
  gfx::filledCircleColor s, 100, 100, 80, gfx::GetPixel(s, 20, 20)
  gfx::boxColor s, 100, 20, 80, 60, gfx::GetPixel(s, 10, 10)
te = gfx::Time()
gfx::stringColor s, 20, 215,"Time: " & FORMAT("%.4f",(te-ts)/1000) & " Seconds." & CHR(0), 0xffffffff
gfx::BlitSurface s, 0, vs, 0
gfx::Update
WHILE gfx::KeyName(1) <> "+escape"
WEND
gfx::Close

The following example was created with an alpha value of 192.

« Last Edit: March 06, 2014, 12:02:58 AM by John »