Cursor Sprite in code with utility.

Share your advanced PureBasic knowledge/code with the community.
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Cursor Sprite in code with utility.

Post by Jeromyal »

I created this file as an include to have sprite mouse cursor in code.
exec_converter() will call the utility I wrote to dump .png files into hex(alpha,green,blue,red) text file.
(note: I did not spend a lot of time writing safety catches with the utility. so if there is errors if you cancel or it cant write to a locked volume if you try to it might not tell you.)

InitSpriteCursor() ; will load the data section that is the copy pasted text dump of the file generated.

I included a nice mouse pointer in the data section but you can replace using the utility.

I just wanted a way to have a hard coded resource of a mouse pointer.

free for anyone to use and expand on. :)

Code: Select all

Global Cursor 

Procedure exec_Converter(); utility to conver png file including alpha channel into a text dump.
  Define file$,img,oid,wfile$,f,h,w,tmp$,dat$,r,c,pix
  UsePNGImageDecoder()
  file$ = OpenFileRequester("Choose .png to convert","","PNG|*.png|Any|*.*",0)
  If file$<>""
    img = LoadImage(#PB_Any,file$)
    
    oid = ImageOutput(img)
    wfile$ = SaveFileRequester("What to name the dump file?",GetFilePart(file$,#PB_FileSystem_NoExtension)+".txt","Text|*.txt",0)
    f = CreateFile(#PB_Any,wfile$)
    StartDrawing(oid)
    DrawingMode(#PB_2DDrawing_AllChannels)
    
    For h = 0 To OutputHeight()-1
      Dat$ = "Data.d "
      For w = 0 To OutputWidth()-1
        pix = Point(w,h)
        
        tmp$ = "$"
        If Len(Hex(Alpha(pix)))<2 
          tmp$ + "0"
        EndIf
        tmp$ + Hex(Alpha(pix))
        
        If Len(Hex(Blue(pix)))<2 
          tmp$ + "0"
        EndIf
        tmp$ + Hex(Blue(pix))
        
        If Len(Hex(Green(pix)))<2 
          tmp$ + "0"
        EndIf
        tmp$ + Hex(Green(pix))
        
        If Len(Hex(Red(pix)))<2 
          tmp$ + "0"
        EndIf
        tmp$ + Hex(Red(pix))
        If w<OutputWidth()-1
          tmp$+","
        EndIf
        dat$ + tmp$
      Next
      ;write dat$ line to file
      r = WriteStringN(f,dat$)
    Next
    StopDrawing()
  EndIf
  CloseFile(f)
  
EndProcedure

Procedure InitSpriteCursor(); call in your main.pb to create a sprite Named Cursor for you to blit to screen
  Cursor = CreateSprite(#PB_Any,32,32,#PB_Sprite_PixelCollision|#PB_Sprite_AlphaBlending)
  StartDrawing(SpriteOutput(Cursor))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Define w,h,c
  For h = 0 To 31
    For w = 0 To 31
      Read.d c
      Plot(w,h,c)
    Next
  Next
  StopDrawing()
EndProcedure

DataSection
  ;This is the copy pasted data from the 32x32 bit cursor.png convereted into a text dump.
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$E36456C9,$F76C5FC6,$9F5140D2,$173621DE,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$F76C5FC6,$FFAAA2E4,$FF948AD6,$F77064C7,$9F5140D2,$173621DE,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$9F5140D2,$FF8D83D2,$FF9D93F0,$FFB1AAEB,$FF978ED6,$F77064C7,$BD5848CE,$2D3B25DC,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$173621DE,$F76D60C5,$FF857AE2,$FF7E70EB,$FFA49AF1,$FFB8B1ED,$FFA299DD,$FF796DC7,$BD5848CD,$2D3B25DC,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$9F503FD0,$FF7E73CD,$FF7263EA,$FF513FE5,$FF7869EA,$FFA399F1,$FFC0B9F2,$FFA39ADC,$FF786CC5,$BD5747CC,$57422DD8,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$173621DE,$F76A5DC2,$FF7D6FE2,$FF4E3BE4,$FF4B38E4,$FF5C4AE6,$FF7A6CEB,$FFA89EF2,$FFC4BEF3,$FFA299DD,$FF8277C9,$D75D4EC7,$57412DD8,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$BD5343C8,$FF7B6FCF,$FF6150E7,$FF3721E1,$FF4531E3,$FF5543E5,$FF6656E8,$FF8477EC,$FFA99FF2,$FFC7C1F5,$FFB0A8E3,$FF8177C8,$D75C4DC6,$6B4430D5,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$2D3924DB,$FF695DB7,$FF786BE1,$FF422EE2,$FF341EE0,$FF3F2AE2,$FF503DE4,$FF6150E7,$FF7162E9,$FF887BED,$FFADA5F3,$FFC9C4F6,$FFB0AAE3,$FF867CCA,$ED6254C1,$7D4635D2,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$BF4E3EBF,$FF766BC5,$FF604FE3,$FF331DE0,$FF331DE0,$FF3924E1,$FF4936E3,$FF5A48E6,$FF6B5BE9,$FF7C6EEB,$FF9186EE,$FFB3ABF3,$FFC8C3F6,$FFB9B3E9,$FF897ECC,$ED6152BF,$7D4634D2,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$2E3823D9,$FF6156AA,$FF7569D8,$FF412DDC,$FF331DDF,$FF331DE0,$FF3621E0,$FF4430E3,$FF5542E5,$FF6554E8,$FF7567EA,$FF8679ED,$FF978CEF,$FFA79EF2,$FFBCB5F4,$FFAFA7E6,$FF857BC9,$E35C4DC1,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$C1493AB7,$FF6F64BF,$FF5646D8,$FF311CD6,$FF321DDD,$FF331DE0,$FF341EE0,$FF3D29E2,$FF4E3BE4,$FF5F4EE7,$FF7D6FEB,$FF9387EF,$FF9D92F0,$FF988EE6,$FF8F86D6,$FF7E73C6,$E35A4CC0,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$5B3B28CD,$FF5F55A1,$FF6F63D2,$FF3622CE,$FF301BD4,$FF321CDC,$FF331DE0,$FF331DE0,$FF3923E1,$FF7566EA,$FF8D82E3,$FF8177D2,$FF7469C1,$F75F52B9,$BD5141C6,$6B422ED3,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$DC473AA5,$FF6D63B7,$FF5343CD,$FF2E1AC9,$FF301BD1,$FF311CD9,$FF331DDF,$FF331DE0,$FF6A5AE8,$FF8478D3,$FF6154B5,$9F4A39CB,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$5C3826CB,$FF585096,$FF6C61C9,$FF3422C2,$FF2D1AC7,$FF2F1BCE,$FF6353E0,$FF6858E6,$FF4936E3,$FF887BED,$FF8B80D4,$F75C4FB7,$433B27D8,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$DD41349C,$FF695FAD,$FF5243C4,$FF2B18BD,$FF402FC9,$FF7569D1,$FF6C61C3,$FF6C5CE5,$FF4A37E4,$FF877AED,$FF8980D3,$F75B4EB5,$433B27D7,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$723825C1,$FF564F90,$FF6559C1,$FF2D1CB5,$FF5244C7,$FF6A60B8,$FF53479F,$FF6C60C1,$FF6B5CE3,$FF4936E2,$FF877AED,$FF897FD3,$F75A4DB4,$433B26D7,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$F13D3286,$FF685FAC,$FF4739B5,$FF6558C6,$FF5D549D,$A44030BB,$F74F43A0,$FF695EBE,$FF6B5CE2,$FF4A36E1,$FF887BED,$FF887ED2,$F7594BB2,$433A26D7,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$873625B7,$FF514985,$FF675DB9,$FF6D64B8,$F8473D8F,$00000000,$453824D2,$F74D419C,$FF685DBC,$FF6A5BDF,$FF4835DE,$FF877AEC,$FF877ED1,$F7574AB1,$433A26D7,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$F2372E7C,$FF645CA3,$FF5F579D,$C53B2DA2,$00000000,$00000000,$453824D2,$F84B409A,$FF675CB9,$FF695ADD,$FF4936DC,$FF877AEB,$FF877DD0,$ED5446B3,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$893422B2,$FF4C437B,$FF4D4681,$733623BE,$00000000,$00000000,$00000000,$453723D1,$F8493D97,$FF655AB7,$FF695ADB,$FF6A5BE1,$FF7B70CA,$ED5344B2,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$EA33297D,$EA332A7F,$00000000,$08010101,$10020202,$10020202,$18020202,$603322B7,$F9443B88,$FF6259AA,$FF655BAF,$F84D419B,$463825D2,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$04000000,$10020202,$10020202,$14020202,$20030303,$20030303,$24040404,$30050505,$30050505,$38060606,$40070707,$40070707,$40070707,$40070707,$40070707,$40070707,$40070707,$743121A3,$F340377F,$F3423982,$733223A6,$40070707,$34050505,$20030303,$10020202,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
  Data.d $00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000,$00000000
EndDataSection
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: Cursor Sprite in code with utility.

Post by Jeromyal »

I have noticed that if you need to reload sprites such as after a closeScreen() and creating a new windowedscreen or screen,
that you need to create a label at top of data section and a restore in the begining of the inispritecursor() procedure.

I know most of you know that but just saying if there is someone not so seasoned in programming with Purebasic I just gave you a heads up to an interesting error.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Cursor Sprite in code with utility.

Post by coco2 »

Should it be Data.l and not Data.d?
Post Reply