Author Topic: Generic Questions  (Read 8289 times)

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #15 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?

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Generic Questions
« Reply #16 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.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4174
    • View Profile
    • Portfolio
Re: Generic Questions
« Reply #17 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.

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #18 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]
« Last Edit: 2013-Jan-31 by diego »

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #19 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]

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1142
    • View Profile
    • My GLBasic code archiv
Re: Generic Questions
« Reply #20 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).
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline backspace

  • Mr. Drawsprite
  • **
  • Posts: 53
    • View Profile
Re: Generic Questions
« Reply #21 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]
I came, I saw, I coded.

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #22 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?

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4174
    • View Profile
    • Portfolio
Re: Generic Questions
« Reply #23 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...

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #24 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:

Offline Kyo

  • Dr. Type
  • ****
  • Posts: 350
    • View Profile
Re: Generic Questions
« Reply #25 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?



Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4174
    • View Profile
    • Portfolio
Re: Generic Questions
« Reply #26 on: 2013-Feb-05 »
I would think none, I heard GLBasic threats images as 24bit internally.

MrTAToad

  • Guest
Re: Generic Questions
« Reply #27 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...

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4174
    • View Profile
    • Portfolio
Re: Generic Questions
« Reply #28 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.

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 489
    • View Profile
Re: Generic Questions
« Reply #29 on: 2013-Feb-06 »
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