Author Topic: Bitmap Text + Bitmap Loader  (Read 3944 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Bitmap Text + Bitmap Loader
« on: May 04, 2006 »
Quote
rbraz
CBM 128

Posts: 186
(21/4/06 15:29)
Reply
 Â Bitmap Text + Bitmap Loader
--------------------------------------------------------------------------------
 Here is a code to load your bitmap (256 color palette) font image and display it on screen, without any "dll".
This will help a lot of people, who are trying to code your first demo in FreeBasic.

Click here to get the source code + font example
Code: [Select]

'-------------------------
' Â .: Bitmap Text :.
' Â  Â  Â  Â  +
' Â .: Font Loader :.
'
' Â Using Bitmap 256 color
' Â palette image
'
' Â Whithout "DLL"s !!!
'
'-------------------------
' Â  Â by Rbraz 2006
'-------------------------

Option Explicit

'Windowed
#define PTC_WIN

'-------------------------------------
' Includes.
'-------------------------------------
#Include Once "tinyptc.bi"

'Screen constants
Const XRES=640 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Screen Width
Const YRES=480 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Screen Height
Const ARES=XRES * YRES Â  Â  Â  Â  Â  Â  Â 'Array Width

'BitmapFont constants
Const FontW=32 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Font Width
Const FontH=32 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Font Height
Const FontL=64 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Number Of letters in the font

'Sub Routines
Declare Sub Draw_Text(byval message as string, byval xpos as integer, byval ypos as integer, byval inc as integer)
Declare Sub LoadAnimImage( stringFilename As string, byval FrameW, byval FrameH )
Declare Sub DrawImage(byval xpos as integer, byval ypos as integer, byval character as integer, _
 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â byval FrameW as integer, byval FrameH as integer)
Declare Sub Load_Bitmap(byval filename as string)
Declare Sub ClearScreen()
Declare Sub FPS_Count()

'Variables
Dim Shared Buffer(ARES) as integer   Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Tinyptc buffer
Dim Shared BitmapFont( FontW, FontH, FontL ) as integer   'Font buffer

'Bitmap (256 color palette) loader variables
ReDim Shared img_buffer(1) as ubyte   Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  'Bitmap Image buffer
Dim Shared img_r(256), img_g(256), img_b(256) as ubyte   Â 'RGB color palette buffer
Dim Shared img_w, img_h as short   Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 'Image Width / Height

'FPS Counter
Dim Shared iFPS, bSettime,iSecStart,iFrameCount,iFrameStart as integer

'Image file name
Dim file_name as string
file_name="Media\Tilerred.bmp"

'Load our bitmap font
LoadAnimImage( file_name,FontW,FontH )
 Â  Â
'Open TinyPTC window
If( ptc_open( "Bitmap Text + Font Loader", XRES, YRES ) = 0 ) Then
 Â  Â End -1
End if

'Main Loop
While Inkey$() <> Chr$( 27 )
 Â  Â  Â  Â
 Â  Â ClearScreen()
 Â  Â
 Â  Â Draw_Text("FPS : "& iFPS,10,10,FontW)
 Â  Â
 Â  Â Draw_Text("BITMAP TEXT",132,150,FontW)
 Â  Â Draw_Text("+",290,190,FontW)
 Â  Â Draw_Text("FONT LOADER",132,230,FontW)
 Â  Â Draw_Text("BY RBRAZ - 2006",90,320,FontW) Â
 Â  Â
 Â  Â FPS_Count()
 Â  Â
 Â  Â  
 Â Ptc_Update @Buffer(0)
 Â
Wend

'Close TinyPTC window
ptc_close()


'Draw text on screen
Sub Draw_Text(byval message as string, byval xpos as integer, byval ypos as integer, byval inc as integer)
 Â  Â Dim a,i as integer
 Â  Â Dim character as integer
 Â  Â Dim char as string
 Â  Â Dim alphatab as string

 Â  Â For a=1 To Len(message)
 Â  Â  Â  Â char = Mid$(message,a,1)
 Â  Â  Â  Â character = Asc(char)-32 Â 'Make sure that your font are into this range
 Â  Â  Â  Â If (character>-1) And (character<FontL) then
 Â  Â  Â  Â  Â  Â DrawImage(xpos,ypos,character,FontW,FontH)
 Â  Â  Â  Â End If
 Â  Â  Â  Â xpos=xpos+inc
 Â  Â Next
End Sub

'Load frame images
Sub LoadAnimImage( Filename As string, _
 Â  Â  Â  Â  Â  Â  Â  Â  Â  byval FrameW, byval FrameH )
 Â  Â Dim intX, intY, FrameWidth, FrameHeight, FrameNum
 Â  Â Dim rect_x1, rect_x2, rect_y1, rect_y2, a, b
 Â  Â Dim pixel
 Â  Â
 Â  Â 'Load bitmap 256 color palette
 Â  Â Load_Bitmap(Filename)

 Â  Â FrameWidth   = img_w/FrameW
 Â  Â FrameHeight  = img_h/FrameH
 Â  Â
 Â  Â FrameNum   = 0
 Â  Â rect_x1 Â  Â = 0
 Â  Â rect_x2 Â  Â = FrameW
 Â  Â rect_y1 Â  Â = 0
 Â  Â rect_y2 Â  Â = FrameH
 Â  Â
 Â  Â For b = 0 to FrameHeight-1
 Â  Â
 Â  Â  Â  For a = 0 to FrameWidth-1
 Â  Â  Â  Â
 Â  Â  Â  Â  For intY = rect_y1 to rect_y2-1
 Â  Â  Â  Â
 Â  Â  Â  Â  Â  Â  For intX = rect_x1 to rect_x2-1
 Â  Â  Â  Â  
 Â  Â  Â  Â  Â  Â  Â  Â  pixel= img_buffer( intX + ( intY * img_w ) )
 Â  Â  Â  Â  Â  Â
 Â  Â  Â  Â  Â  Â  Â  Â  BitmapFont( intX Mod FrameW, intY Mod FrameH, FrameNum ) = (img_r(pixel) Shl 16) Or (img_g(pixel) Shl 8 ) Â Or img_b(pixel)
 Â  Â  Â  Â  Â  Â
 Â  Â  Â  Â  Â  Â  Next Â
 Â  Â  Â  Â  Â  Â  
 Â  Â  Â  Â  Next
 Â  Â
 Â  Â  Â  Â  rect_x1 = rect_x2
 Â  Â  Â  Â  rect_x2 = rect_x2 + FrameW
 Â  Â  Â  Â  FrameNum = FrameNum + 1
 Â  Â  Â  Â  
 Â  Â  Â  Next Â
 Â  Â  Â  
 Â  Â  Â  rect_x1 = 0
 Â  Â  Â  rect_x2 = FrameW
 Â  Â  Â  rect_y1 = rect_y2
 Â  Â  Â  rect_y2 = rect_y2 + FrameH
 Â  Â
 Â  Â Next   
End Sub

'Draw image into Buffer
Sub DrawImage(byval xpos as integer, byval ypos as integer, byval character as integer, _
 Â  Â  Â  Â  Â  Â  Â byval FrameW as integer, byval FrameH as integer)
Dim intX, intY As integer

 Â  Â For intY=0 to FrameH-1
 Â  Â  Â  Â For intX=0 to FrameW-1
 Â  Â  Â  Â  Â  Â
 Â  Â  Â  Â  Â  Â if (xpos+intX) < (XRES - 1) and (xpos+intX) > 0 then
 Â  Â  Â  Â  Â  Â  Â  Â
 Â  Â  Â  Â  Â  Â  Â  Â Buffer( ((intY+ypos) * XRES) + (intX+xpos)) = BitmapFont(intX, intY, character) Â
 Â  Â  Â  Â  Â  
 Â  Â  Â  Â  Â  Â end if
 Â  Â  Â  Â Next
 Â  Â Next

end sub

'----------------------------------------
' For 256 color palette image only
'----------------------------------------
Sub Load_Bitmap(byval filename as string)
 Â  Â Dim i,j,n,k,l,cnt as integer
 Â  Â Dim Bmp_len, file as integer
 Â  Â Dim byt as ubyte
 
 Â  Â file = FreeFile
 
 Â  Â OPEN filename FOR BINARY AS #file

 Â  Â Get #file,19,img_w   Â  Â  Â ' bmp width
 Â  Â Get #file,23,img_h   Â  Â  Â ' bmp height

 Â  Â Bmp_len = img_w * img_h   ' Bmp size

 Â  Â ReDim img_buffer(Bmp_len)
 Â  Â Dim temp(Bmp_len)

 Â  Â 'Color palette
 Â  Â cnt = 55
 Â  Â For i = 0 To 255
 Â  Â  Â  Â Get #file,cnt,byt
 Â  Â  Â  Â img_b(i) = byt
 Â  Â  Â  Â cnt+=1
 Â  Â  Â  Â Get #file,cnt,byt
 Â  Â  Â  Â img_g(i) = byt
 Â  Â  Â  Â cnt+=1
 Â  Â  Â  Â Get #file,cnt,byt
 Â  Â  Â  Â img_r(i) = byt
 Â  Â  Â  Â cnt+=2
 Â  Â Next

 Â  Â 'Image pixels
 Â  Â cnt = 1079
 Â  Â For i = 0 To Bmp_len-1
 Â  Â  Â  Â Get #file,cnt,byt
 Â  Â  Â  Â img_buffer(i) = byt
 Â  Â  Â  Â cnt+=1
 Â  Â Next
 
 Â  Â Close #file

 Â  Â For i = -(Bmp_len-1) Â To 0
 Â  Â  Â  Â temp(j) = img_buffer(Abs(i))
 Â  Â  Â  Â j = j + 1
 Â  Â Next

 Â  Â 'Flip image
 Â  Â Do
 Â  Â  Â  Â For j = 0 To img_w
 Â  Â  Â  Â k = (j + (n * img_w))
 Â  Â  Â  Â l = ((img_w - j) + (n * img_w))
 Â  Â  Â  Â img_buffer(l) = temp(k)
 Â  Â  Â  Â Next
 Â  Â  Â  Â n = n + 1
 Â  Â Loop Until n = img_h

End Sub

Sub ClearScreen() Â
 Â  Â Dim i as integer
 Â  Â for i = 0 to ARES
 Â  Â  Â  Â  Buffer(i) = 0
 Â  Â next
End Sub

Sub FPS_Count()
 Â  Â  Â  Â  If bSettime = 1 then
 Â  Â  Â  Â  Â iSecStart = Timer() * 1000.0
 Â  Â  Â  Â  Â iFrameStart = iFrameCount
 Â  Â  Â  Â  Â bSettime = 0
 Â  Â  EndIf Â
 Â  Â  If (Timer()*1000.0) >= iSecStart + 1000 then
 Â  Â  Â  Â  Â iFPS = iFrameCount - iFrameStart
 Â  Â  Â  Â  Â bSettime = 1
 Â  Â  EndIf
 Â  Â  iFrameCount = iFrameCount + 1 Â  Â  
End Sub

 
Quote
jimshawx
ZX SPECTRUM

Posts: 37
(22/4/06 3:01)
Reply  bmp loader
--------------------------------------------------------------------------------
 Here's a little present. It should load any BMP file of any format into a 32bit texture. It uses Windows API and so it is very small.

Code: [Select]
' Jim's bmp decoder

option explicit

#define PTC_WIN

#include once "tinyptc.bi"
#include once "windows.bi"
#include once "crt.bi"

declare function LoadTexture(fname as string, byval tex as uinteger ptr) as Integer

dim buffer(640*480) as uinteger
loadtexture("monkeys.bmp", @buffer(0))

ptc_open("Bitmap Test", 640,480)

while inkey$ <> chr$(27)
 Â  Â  Â  Â ptc_update @buffer(0)
wend

ptc_close()
end

type MYHEADER
 Â  Â  Â bmpi as BITMAPINFO
 Â  Â  Â masks(3) as RGBQUAD
end type

 
function LoadTexture(fname as string, byval tex as uinteger ptr) as Integer
 Â  Â  Â  Â dim as HBITMAP bmp
 Â  Â  Â  Â dim as HDC hdc
 Â  Â  Â  Â DIM header AS MYHEADER

 Â  Â  Â  Â memset(@header.bmpi, 0, sizeof(header.bmpi))
 Â  Â  Â  Â header.bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
 Â  Â  Â  Â bmp = cast(HBITMAP, LoadImage(NULL, fname, IMAGE_BITMAP, 0,0, LR_LOADFROMFILE))

 Â  Â  Â  Â hdc = GetDC(NULL)

 Â  Â  Â  Â GetDIBits(hdc, bmp, 0,0, NULL, @header.bmpi, 0)
 Â  Â  Â  Â header.bmpi.bmiHeader.biBitCount = 32
 Â  Â  Â  Â GetDIBits(hdc, bmp, 0, header.bmpi.bmiHeader.biHeight, tex, @header.bmpi, DIB_RGB_COLORS)

 Â  Â  Â  Â ReleaseDC(NULL, hdc)
 Â  Â  Â  Â
 Â  Â  Â  Â rem only need this bit to flip it upside down
 Â  Â  Â  Â dim line0 as uinteger ptr
 Â  Â  Â  Â dim lineN as uinteger ptr
 Â  Â  Â  Â lineN = tex+(header.bmpi.bmiHeader.biHeight-1)*header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â line0 = tex
 Â  Â  Â  Â dim as uinteger tmpln(1024)
 Â  Â  Â  Â dim y as integer
 Â  Â  Â  Â for y = 0 TO (header.bmpi.bmiHeader.biHeight/2)-1
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(@tmpln(0), line0, header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(line0, lineN, header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(lineN, @tmpln(0), header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â line0 = line0 + header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â  Â  Â  Â  Â lineN = lineN - header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â next
 Â  Â  Â  Â
 Â  Â  Â  Â return 1
end function


 
Quote
Clyde Radcliffe Â
 Fuzzy Wuzzy

Posts: 18271
(22/4/06 9:38)
Reply
 Â Re: bmp loader
--------------------------------------------------------------------------------
 Cool nice work there dudes.
Im trying to add bmps / pngs as a resource to the exe. So everythings more compact. Dont suppose you've sussed a solution out for that?

One again welldone,
Cheers - Clyde Â
 


Quote
jimshawx
ZX SPECTRUM

Posts: 38
(22/4/06 11:47)
Reply  resource loading
--------------------------------------------------------------------------------
 It's a one-line change to make my example load bmp from resources, once you work out how to get them in. Shame Windows doesn't have any real image format support to draw on. Everyone uses jpeg6b and libpng libaries for image formats.

Jim

Quote
5H0CKW4VE
 *Administrator*

Posts: 8016
(22/4/06 13:53)
Reply
ezSupporter

 Â Re: resource loading
--------------------------------------------------------------------------------
 Thanks guys, I am sure these listings will be a huge help to people writing thier first FB stuff Â


--------------------------------------------------------------------------------


¤´¨)
¸.·´¸.·*´¨) ¸.·*¨)
(¸.·´ (¸.·`¤... SHOCKWAVE / DBF...¤

VISIT DARK BIT FACTORY INTERACTIVE! (please!)
Quote
rbraz
CBM 128

Posts: 190
(22/4/06 17:41)
Reply
 Â Re: resource loading
--------------------------------------------------------------------------------
 Nice one Jim Â

Yeah, to load it from resource it should be something like this:


Quote:
--------------------------------------------------------------------------------

#define bmp1 115

bmp = cast(HBITMAP, LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(bmp1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION))

--------------------------------------------------------------------------------




Quote:
--------------------------------------------------------------------------------

Resource.rc file
bmp1 BITMAP "Image.bmp"

--------------------------------------------------------------------------------




Quote:
--------------------------------------------------------------------------------

And compile with:
fbc Examples\LoadTexture.bas Examples\Resource.rc

--------------------------------------------------------------------------------



But it doesn't work  (Works fine in C++). Maybe there be an another way to do that...




Edited by: rbraz at: 22/4/06 17:49
 


Quote
jimshawx
ZX SPECTRUM

Posts: 39
(23/4/06 2:51)
Reply  load resources
--------------------------------------------------------------------------------
 The problem is your .rc file doesn't know the value of bmp1. You need to add
#define bmp1 115
to the .rc file too.

Works fine after that.

Jim

Quote
Clyde Radcliffe Â
 Fuzzy Wuzzy

Posts: 18286
(23/4/06 13:57)
Reply
 Â Re: load resources
--------------------------------------------------------------------------------
 Im a tad confused with what 115 means and does.
Dont suppose you could knock up a listing of what to do for using resources please dudes?

And does that program also load in as anim images / tilesets?

Cheers and many thanks,
Clyde Â

Edited by: Clyde Radcliffe   at: 23/4/06 13:58

Quote
rbraz
CBM 128

Posts: 192
(23/4/06 17:18)
Reply
 Â Re: load resources
--------------------------------------------------------------------------------
 Now it works fine Jim, thanks!

Just forgot when I create a resource file in C, i need to include the resource.h

And noticed that I need to remove quotes " " from the resouce file name.

To work you must run the compiled version (exe), doesn't work from IDE :(

Code: [Select]
----------------------Load Texture.bas ----------------------------------

 
' Jim's bmp decoder
'
' Added Loading from Resource
'

option explicit

#define PTC_WIN

#include once "tinyptc.bi"
#include once "windows.bi"
#include once "crt.bi"


declare function LoadTexture(byval tex as uinteger ptr) as Integer

dim buffer(640*480) as uinteger
loadtexture(@buffer(0))

ptc_open("Bitmap Test", 640,480)

while inkey$ <> chr$(27)
 Â  Â  Â  Â ptc_update @buffer(0)
wend

ptc_close()
end

type MYHEADER
 Â  Â  Â bmpi as BITMAPINFO
 Â  Â  Â masks(3) as RGBQUAD
end type

 
function LoadTexture(byval tex as uinteger ptr) as Integer
 Â  Â  Â  Â dim as HBITMAP bmp
 Â  Â  Â  Â DIM header AS MYHEADER
 Â  Â  Â  
 Â  Â  Â  Â #define bmp1 115
 Â  Â  Â  
 Â  Â  Â  Â memset(@header.bmpi, 0, sizeof(header.bmpi))
 Â  Â  Â  Â header.bmpi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER)
 Â  Â  Â  Â bmp = cast(HBITMAP, LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(bmp1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION))


 Â  Â  Â  Â GetDIBits(GetDC(NULL), bmp, 0,0, NULL, @header.bmpi, 0)
 Â  Â  Â  Â header.bmpi.bmiHeader.biBitCount = 32
 Â  Â  Â  Â GetDIBits(GetDC(NULL), bmp, 0, header.bmpi.bmiHeader.biHeight, tex, @header.bmpi, DIB_RGB_COLORS)

 Â  Â  Â  Â
 Â  Â  Â  Â rem only need this bit to flip it upside down
 Â  Â  Â  Â dim line0 as uinteger ptr
 Â  Â  Â  Â dim lineN as uinteger ptr
 Â  Â  Â  Â lineN = tex+(header.bmpi.bmiHeader.biHeight-1)*header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â line0 = tex
 Â  Â  Â  Â dim as uinteger tmpln(1024)
 Â  Â  Â  Â dim y as integer
 Â  Â  Â  Â for y = 0 TO (header.bmpi.bmiHeader.biHeight/2)-1
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(@tmpln(0), line0, header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(line0, lineN, header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â memcpy(lineN, @tmpln(0), header.bmpi.bmiHeader.biWidth * sizeof(uinteger))
 Â  Â  Â  Â  Â  Â  Â  Â line0 = line0 + header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â  Â  Â  Â  Â lineN = lineN - header.bmpi.bmiHeader.biWidth
 Â  Â  Â  Â next
 Â  Â  Â  Â
 Â  Â  Â  Â return 1
end function




-------------------- resource.rc --------------------------------

 
#define bmp1 115
bmp1 BITMAP C:\FreeBasic\Examples\Media\image.bmp



-------------------- Compile.bat --------------------------------
fbc Examples\LoadTexture.bas Examples\Resource.rc -s gui

pause



 
Quote
Clyde Radcliffe Â
 Fuzzy Wuzzy

Posts: 18290
(23/4/06 19:15)
Reply
 Â Re: load resources
--------------------------------------------------------------------------------
 Not yet tried all this out yet. Cool stuff dudes.

But what I'll tell you about getting it working in FBIDE, is that so long as you've got it associated with your .bas source code, if you double click from where the program listing is in Windows explorer, resources will work that way. And for some bug with FBIDE, not from opening up with it.

 
Quote
jimshawx
ZX SPECTRUM

Posts: 40
(23/4/06 23:47)
Reply
 Â Re: load resources
--------------------------------------------------------------------------------
 115 is just a number. Every resource needs a unique number, that's all. Valid numbers are 0 to 32767, or possibly 65535. I suspect Rbraz just plucked that one out of the ether.
You could change the function to pass in the resource number.

Jim


PS.: Yeah, just from the ether  :D
 
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Bitmap Text + Bitmap Loader
« Reply #1 on: May 04, 2006 »
What a real top topic.

Cheers all,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: