GLBasic forum

Main forum => FAQ => Topic started by: Kyo on 2013-Jan-26

Title: Generic Questions
Post by: Kyo on 2013-Jan-26
Hi  to all people of the forums!
I do not read the forum for long time and I had a few questions.

1) GLBasic support ADS for Android? and iPhone?
2) Is implemented WIFI for Andoid? and iPhone?
3) and Bluetooth? (Android and iPhone)

Thanks for the replies!  :good:
Title: Re: Generic Questions
Post by: hardyx on 2013-Jan-26
All you are asking must be implemented by the programmer.
The language allows inline C++ code, and writting objetive-c modules in iPhone.
There is Ads library and Bluetooth library for iPhone in the forum.
Title: Re: Generic Questions
Post by: kanonet on 2013-Jan-26
Hmm im not sure how he means his questions. With native GLB you cant establish a wifi connection, but if the user established a connection, you can use it with GLB NET_ and SOCK_ commands and program networking games.
Title: Re: Generic Questions
Post by: mentalthink on 2013-Jan-26
I comme here every day, and now I know about the BT for iphone...  :O
Thanks hardyx... but it's you know if it's possible to connect an Arduno whit the Bluetooth of iPhone?¿, I think in the new model can be done, because the bluetooth it's the new SmartBluetooth, protocol or model, I'm not sure really what it's.
Title: Generic Questions
Post by: Kitty Hello on 2013-Jan-27
No. Apple dors not allow to program bluetooth other than the gamechannel connection. You can get an SDK for a high price. Android should be posdible. Wifi is no problem.
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-27
Thanks for the reply!  :good:

Another question:
how to read with the command sprite2mem, the colors palette of a sprite?
Title: Re: Generic Questions
Post by: backspace on 2013-Jan-27
The SPRITE2MEM reference in the help is pretty useful for that. You will need to sequentially traverse through the pixels to pick up their individual colors. a=alpha. r,g,b are self explanatory. x=sprite column, y=sprite row. width=width of sprite.

Quote
//SPRITE2MEM()
ok = SPRITE2MEM(pixels%[], num%)

LOCAL r%,g%,b%,a%
LOCAL abgr% = pixels%[x + y*width]
r = bAND(abgr, 0xff)
g = bAND(ASR(abgr,8), 0xff)
b = bAND(ASR(abgr,16), 0xff)
a = bAND(ASR(abgr,24), 0xff)
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-30
I read the guide!  :good:

But I'm not sure to read the pixels in the correct way:

Code (glbasic) Select

   REDIM pixels%[width*height]


   SPRITE2MEM(pixels%[], num%)

LOCAL abgr%

    test$ = "test1.txt"
OPENFILE(1, test$, FALSE)

FOR y = 0 TO (height) - 1
FOR x = 0 TO (width) - 1

      abgr% = pixels%[y+x]
     
WRITELINE  1, abgr%

   NEXT
  NEXT



is that correct?
Title: Re: Generic Questions
Post by: MrTAToad on 2013-Jan-30
No - basically you are just reading one line.  You need something like :

Code (glbasic) Select
abgr%=pixels(x+(y*640)]

where 640 should be replaced with your actual screen width
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-31
mmmm ... I dont not understand ...
you can explain it with an example?

Thanks  :booze:
Title: Re: Generic Questions
Post by: Moru on 2013-Jan-31
Haven't read the thread fully but I'm guessing the problem is that you don't read the array right. Memory is only one dimension, it's one long line. Every line on the screen is a small section on this memory area. To be able to use X and Y to locate a pixel in this memory you need to multiply Y with the width of the screen. Then add X to get the horizontal position. This gives you the memory address you need to get to the right pixel

Formula becomes:
memory_position = (Y * width_of_screen) + X

First line, fifth pixel will be (0 * 640) + 4 = 4
Second line, fifth pixel will be (1 * 640) + 4 = 644

Look up the position 644 in the array and you get the right pixel
array[644] = <your color>
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-31

I tried, but I get error on the size of the array:


Code (glbasic) Select

   REDIM pixels%[width*height]

   SPRITE2MEM(pixels%[], num%)

LOCAL abgr%

    test$ = "test1.txt"
OPENFILE(1, test$, FALSE)

FOR y = 0 TO (height) - 1
FOR x = 0 TO (width) - 1

      abgr% = pixels%[x + (y*480)] // 480 width of screen
WRITELINE  1, abgr% + "\n"
   NEXT
NEXT


Title: Re: Generic Questions
Post by: MrTAToad on 2013-Jan-31
Sorry - it should be the width of the sprite, and not the screen.
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-31
Ok now it works,  :booze:
but it is very very slow to read a sprite 200x200 px.

Code (glbasic) Select

FOR y = 0 TO (height) - 1
FOR x = 0 TO (width) - 1

      abgr% = pixels%[x + (y*width)]

   NEXT
NEXT

How can I optimize the reading?
Title: Re: Generic Questions
Post by: kanonet on 2013-Jan-31
Should be no task for a modern computer at all, they are fast enough to do this. But some tiny improvements:
Code (glbasic) Select
DEC height
DEC width
FOR y% = 0 TO height
FOR x% = 0 TO width

      abgr% = pixels%[x + (y*width)]

NEXT
NEXT

And also make sure, that width and height are integers. And if you dont need to do this every loop, dont do it! Just do it as often as you need, it, if you need it just one time, do it before the main loop.
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-31
mmmmm .... I think the real problem is in the format of the sprite.
I use a sprite png 24-Bit and then memorize over 2000 colors.

I want to change the colors of 200 sprites.
and do this with photoshop is a big job!

I have not found a program that does this job automatically in many sprites.

The code I wrote seems to work, but for a single sprite are requests more than 2 hours of work.  :blink:

Another question:
I tried to use the command setshoebox, I work well with sprites and music, but I do not read txt and ini is this normal?
Title: Re: Generic Questions
Post by: kanonet on 2013-Jan-31
Two hours for a 200x200 sprite? Are programming on a pocket calculator? lol
There must be something going on really bad or how much do you do inside the loop? It should never take that long. Be sure that you dont have debugmode enabled.
If you want you can post your code here or pm it to me, i will have a look at it. Maybe you should also include one sprite too.
Title: Re: Generic Questions
Post by: erico on 2013-Jan-31
Quote from: diego on 2013-Jan-31
...
I want to change the colors of 200 sprites.
and do this with photoshop is a big job!
...

You need to set up a good ACTION in photoshop and batch things.

Programming such on GLB is more versatile though.
Title: Re: Generic Questions
Post by: Kyo on 2013-Jan-31
thanks for help.

I use 2 routines in the first, loading into memory 2 Sprites.

This function, reads the colors of the sprite 1 (1.png) and compares them with the sprite 2 (2.png), if they are different saves them in a txt:
Code (glbasic) Select

4
840832112|843650153
1563106969|1568677516
-1893064546|-1887035245
-1524032099|-1517871729


4  -> is the number of different colors between sprite 1 and sprite 2
840832112|843650153 -> where 840832112 the first color of sprite 1 and 843650153 is the first color of sprite 2
1563106969|1568677516 - > where 1563106969 the second color of sprite 1 and 1568677516 is the second color of sprite 2
ecc...

Code (glbasic) Select

LOADSPRITE "Media/1.png", 0
LOADSPRITE "Media/2.png", 1
LOADSPRITE "Media/3.png", 2



Code (glbasic) Select

   GETSPRITESIZE img, w2,h2

   MakeImage(0, 1, w2, h2)



Code (glbasic) Select


FUNCTION MakeImage: num%, num1%, width%, height%
LOCAL x%, y%, pixels%[], pixels1%[], memo$[]


   REDIM pixels%[width*height]

   REDIM pixels1%[width*height]

   DIM memo$[100000]

   SPRITE2MEM(pixels%[], num%)

   SPRITE2MEM(pixels1%[], num1%)

LOCAL abgr%

conta1 = 1
FOR y = 0 TO (height*width) - 1

      abgr% = pixels%[x + (y)]
      abgr1% = pixels1%[x + (y)]

IF abgr% <> abgr1%


FOR g = 0 TO conta1
IF memo$[g] = abgr% + "|" + abgr1%
GOTO giu
ELSE
IF memo$[g] = ""
memo$[g] = abgr% + "|" + abgr1%
conta1 = conta1 + 1
GOTO giu
ENDIF
ENDIF
NEXT

ENDIF
giu:

NEXT

test$ = "test2.txt"

OPENFILE(1, test$, FALSE)
WRITELINE  1, conta1  + "\n"

FOR g = 0 TO conta1
WRITELINE  1, memo$[g]
NEXT

   CLOSEFILE 1


ENDFUNCTION



The second Function seeks in the third sprite (3.png) the colors of the first column of txt and replace them with the colors of the second column:

Code (glbasic) Select

GETSPRITESIZE 2, w2,h2
change_color(2, w2, h2)


Code (glbasic) Select

FUNCTION change_color: num%,  width%, height%

LOCAL x%, y%, pixels%[], pixels1%[], memo%[]
LOCAL s, f$
LOCAL splits$[]


DIM memo%[1000000][6]

  test$ = "test2.txt"
OPENFILE(1, test$, TRUE)

READLINE  1, s
value_palette = s


FOR x = 0 TO value_palette

READLINE  1, f$

IF f$ = "" THEN DEBUG "Null" + "\n"
IF f$ <> "" THEN DEBUG f$ + "\n"
DEBUG x + "\n"
SPLITSTR(f$, splits$[], "|")


IF f$ <> ""
memo%[x][0] = splits$[0]
memo%[x][1] = splits$[1]
ENDIF

NEXT

CLOSEFILE 1




REDIM pixels%[width*height]
SPRITE2MEM(pixels%[], num%)

FOR i = 0 TO (height*width) - 1
FOR x = 0 TO value_palette

PRINT "X:" + x,220,140
PRINT "Y:" + i,220,160

//DEBUG x + "\n"
SHOWSCREEN

IF pixels%[i] = memo%[x][0]
pixels%[i] =  memo%[x][1]
GOTO esci
ENDIF
NEXT
esci:
NEXT

MEM2SPRITE(pixels%[], num%, width%, height%)
CLEARSCREEN

DRAWSPRITE num%, 0,0
SAVESPRITE "new.png", num%
SHOWSCREEN

ENDFUNCTION



Result: the sprite 3.png shall have the same colors of the sprite 2.png!

But everything is really slow! I do not have debugging turned on!  :)



[attachment deleted by admin]
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-01
now it works well!

In practice compares sprites 1.png and 2.png, and change the sprite palette 3.png, and creates the sprite 4.png

unfortunately does not work well with too many colors (I tried with 2000 colors).

I Attach the source if you want to try!

[attachment deleted by admin]
Title: Re: Generic Questions
Post by: kanonet on 2013-Feb-01
I did a rework of your (1st posted) code, just in case you want to see how it could be done in a more proper way and with faster execution speed. Hope that helps you:
Code (glbasic) Select
LOCAL pixels%[], pixels1%[]
LOCAL w2%, h2%

LOADSPRITEMEM("Media/1.png", w2, h2, pixels[])
LOADSPRITEMEM("Media/2.png", w2, h2, pixels1[])

LOCAL abgr%, abgr1%
LOCAL memo%[]
DIM memo[10][2]

LOCAL conta1%, tmp% = w2 * h2 - 1
FOR y% = 0 TO tmp
abgr = pixels%[y]
abgr1 = pixels1%[y]
IF abgr <> abgr1
FOR g% = 0 TO conta1
IF memo[g][0] = abgr AND memo[g][1] = abgr1
BREAK
ELSE
IF memo[g][0] = 0 AND memo[g][1] = 0
memo[g][0] = abgr
memo[g][1] = abgr1
INC conta1
IF conta1>=BOUNDS(memo[],0) THEN REDIM memo[BOUNDS(memo[],0)+100][2]
BREAK
ENDIF
ENDIF
NEXT
ENDIF
NEXT


//// start of writing to file - this is not needed!
//LOCAL test$ = "test2.txt"
// OPENFILE(1, test$, FALSE)
// WRITELINE  1, conta1  + "\n"
// FOR g% = 0 TO conta1
// WRITELINE  1, memo[g][0]+"|"+memo[g][1]
// NEXT
// CLOSEFILE 1
//// end of writing to file
//
//
//// start of reading from file - this is not needed!
// OPENFILE(1, test$, TRUE)
// LOCAL f$, splits$[]
// READLINE  1, conta1
// DIM memo[conta1+1][2]
// FOR x = 0 TO conta1
// READLINE  1, f$
// IF f$ = "" THEN DEBUG "Null" + "\n"
// IF f$ <> "" THEN DEBUG f$ + "\n"
// DEBUG x + "\n"
// IF f$ <> ""
// SPLITSTR(f$, splits$[], "|")
// memo%[x][0] = splits$[0]
// memo%[x][1] = splits$[1]
// ENDIF
// NEXT
// CLOSEFILE 1
//// end if reading from file


LOADSPRITEMEM("Media/3.png", w2,h2, pixels[])
tmp = w2 * h2 -1
FOREACH p IN pixels[]
FOR x% = 0 TO conta1
IF p = memo[x][0]
p =  memo[x][1]
BREAK
ENDIF
NEXT
NEXT

MEM2SPRITE(pixels[], 0, w2,h2)
CLEARSCREEN

DRAWSPRITE 0, 0,0
SAVESPRITE "Media/4.png", 0
SHOWSCREEN


Btw. the part where you write to a file and read from it again is not necessary for it to work, I just included it here in case you need it for a different part of your program (uncomment it in this case).
Title: Re: Generic Questions
Post by: backspace on 2013-Feb-01
Here's my attempt at a solution to a color changer - Hope you find it useful.
Click on the color to change. Set the new color in the ChangeColors function.
Images for this demo are attached.


Code (glbasic) Select
// ------------------------------------------- //
// Project:     image_utils_6
// Description: Simple image color changer
// ------------------------------------------- //

GLOBAL px%[]   //buffer  - global to be seen by functions

LOCAL  mx,my,mbl,mbr
LOCAL currColor
LOCAL MBLDown = FALSE   //is mouse button-left down?

//load the image
LOADSPRITE "colorbox.png", 1

//load cursor
LOADSPRITE "cursor.png", 2


//main process loop
WHILE TRUE

//get mouse info
MOUSESTATE mx, my, mbl, mbr

//display input image sprite
DRAWSPRITE 1, 0,0

//get pixel color under mouse - if left mouse button is NOT down
IF NOT mbl THEN currColor = GETPIXEL(mx,my)

//draw a rectangle to show current color
DRAWRECT 450,20, 100, 100, currColor

//if left mouse button then change color
IF mbl AND NOT MBLDown
ChangeColors(currColor)
MBLDown = TRUE
ENDIF

//if left mouse button up, reset indicator
IF NOT mbl THEN MBLDown = FALSE

//draw mouse cursor - Always draw last to appear on top
DRAWSPRITE 2,mx,my

SHOWSCREEN
WEND

FUNCTION ChangeColors: color#
LOCAL width%, height%
LOCAL newColor = RGB(0,0,0) //color for replacement
LOCAL abgr%, r%, g%, b%, a%
LOCAL cabgr%, cr%, cg%, cb%, ca%

//break down color components
cabgr = color
cr = bAND(cabgr, 0xff)
cg = bAND(ASR(cabgr,8), 0xff)
cb = bAND(ASR(cabgr,16), 0xff)
ca = bAND(ASR(cabgr,24), 0xff)

//get sprite info
GETSPRITESIZE 1, width, height

//drop into sprite into buffer
SPRITE2MEM (px[],1)

//replace all occurences of color, with newcolor
//since the buffer is one long entity, we just loop through the data in sequene
FOR i% = 0 TO (width*height)-1

//break down sprite color components
abgr% = px[i]
r = bAND(abgr, 0xff)
g = bAND(ASR(abgr,8), 0xff)
b = bAND(ASR(abgr,16), 0xff)
a = bAND(ASR(abgr,24), 0xff)

//test and change
IF r = cr AND g = cg AND b = cb
px[i] = newColor
ENDIF

NEXT

//put buffer back into sprite
MEM2SPRITE(px[], 1, width, height)

ENDFUNCTION


[attachment deleted by admin]
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-01
Thanks kanonet and backspace  :good: :booze: :good:

Another question:
I tried to use the command setshoebox, I work well with sprites and music, but I do not read txt and ini is this normal?
Title: Re: Generic Questions
Post by: erico on 2013-Feb-01
 :offtopic:
Good choice of sprites!
I love KOF and the XIII, altough not as tough as 98, scores high for me.

I´m particularly impressed on how they drew those sprites, I sense 3d was used for base movements and shadow renders, I think they drew over this first skeleton, I have to figure out. Beautifull technic :-*

I could do without transparencies on the visual effects though...
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-01
Can you help me make this code?

this is the map of the keys:

Code (glbasic) Select

Key_p$ = "N"

IF KEY(205) THEN Key_p$ = "L"
IF KEY(203) THEN Key_p$ = "R"
IF KEY(200) THEN Key_p$ = "U"
IF KEY(208) THEN Key_p$ = "D"


IF KEY(30) THEN  Key_p$ = "A"
IF KEY(31) THEN  Key_p$ = "B"




If I press the button 205 - char L and press again in less than 1 second it print the string L | L

If i press the button 200 - chat R - and press the button 208 - char D ( less than 1 second ) and press the button 205 - char L ( less than 1 second )  it print the string  R| D|L

the string must contain a maximum of 4 letters: ->  R|D|L|A

I'm trying to store them in an array with a progressive timer, but does not works well!

Thanks for the help :good:
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-05
Another question!
I have the same image in 2 sizes:

1) Width: 122 Height: 200 (122x200), 7.2 Kb, 24 Bit
2) Width: 122 Height: 200 (122x200), 7.2 Kb, 8 Bit 256 Palette

how much difference there is in runtime memory?


Title: Re: Generic Questions
Post by: erico on 2013-Feb-05
I would think none, I heard GLBasic threats images as 24bit internally.
Title: Re: Generic Questions
Post by: MrTAToad on 2013-Feb-05
If they were both BMP's then probably none.  However, PNG and JPEG may require extra memory for decompression...  How much would depend probably on any compression ratio...
Title: Re: Generic Questions
Post by: erico on 2013-Feb-06
The png+alpha (32bit) might take a bit more too.
the png 24bit using the purple background color mask may use less mem too.
Title: Re: Generic Questions
Post by: hardyx on 2013-Feb-06
Quote from: mentalthink on 2013-Jan-26
I comme here every day, and now I know about the BT for iphone...  :O
Thanks hardyx... but it's you know if it's possible to connect an Arduno whit the Bluetooth of iPhone?¿, I think in the new model can be done, because the bluetooth it's the new SmartBluetooth, protocol or model, I'm not sure really what it's.
Here is the library for Bluetooth in iDevices. Like Gernot says, Apple allows you to use in games only.
http://www.glbasic.com/forum/index.php?topic=4380.0
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-06
QuoteMrTAToad: If they were both BMP's then probably none. However, PNG and JPEG may require extra memory for decompression... How much would depend probably on any compression ratio...

Quoteerico: The png+alpha (32bit) might take a bit more too.
the png 24bit using the purple background color mask may use less mem too.


I do not understand ...
what is the sense to use an image in 8-bit format 256 Palette,  if in memory is equal to an image 24 bit??
only to save space on hdd?
If I using the image 8-bit 256 Palette I have an image less detailed compared to an Image 24-bit...

It makes no sense!  :S

hardyx thanks for the links  :good:
Title: Re: Generic Questions
Post by: erico on 2013-Feb-06
I don´t quite know the reasons, but even a 2 bit image is treated as 24bit internally.
I´m not sure about a png 32 bit (with alpha).

The former I read in another post, maybe someone can explain better. :-[
It may have something to do with open gl.

I think the only benefit is hdd space too.

I could be nice to have indexed colors (256 colors or less) and the ability to shift pallete similar to Amos on amiga, ye the oldschool stuff.
But I´m not sure this would be necessary as a feature to all users and the actual trouble to have it going that way.
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-06
You are right!
The images are used in memory only in 24-bit format ....

I made a test image:
The exe without images in taskmanager is:  KB 16.105

I used the same image (128x227 - WxH) in three resolutions:
1) 24 Bit - 7,79 KB
2) 8 Bit - 256 Palette - 4,24 KB
3) 8 Bit - 16 Palette - 3,2 KB

the difference is minimal in the three resolutions!

This is the result of the test in taskmanager:



[attachment deleted by admin]
Title: Re: Generic Questions
Post by: MrTAToad on 2013-Feb-06
Quote from: diego on 2013-Feb-06
QuoteMrTAToad: If they were both BMP's then probably none. However, PNG and JPEG may require extra memory for decompression... How much would depend probably on any compression ratio...

Quoteerico: The png+alpha (32bit) might take a bit more too.
the png 24bit using the purple background color mask may use less mem too.


I do not understand ...
what is the sense to use an image in 8-bit format 256 Palette,  if in memory is equal to an image 24 bit??
only to save space on hdd?
If I using the image 8-bit 256 Palette I have an image less detailed compared to an Image 24-bit...

It makes no sense!  :S

hardyx thanks for the links  :good:
Graphic cards have a fix way of processing stuff...
Title: Re: Generic Questions
Post by: Kyo on 2013-Feb-06
And Mobile Phone? have no problem with many images in memory in 24 Bit?
Title: Re: Generic Questions
Post by: MrTAToad on 2013-Feb-06
They used to ise just 16-bit displays, but probably now use 24