determining character height in graphic modes

General FreeBASIC programming questions.
Post Reply
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

determining character height in graphic modes

Post by DamageX »

In SCREEN 13 I have 8x8 characters, while in SCREEN 18 I have 8x16 characters. For a program which may run in various screen modes, is there a way to determine the current character size? Or a reliable rule governing which size is used in which mode? I want to associate a mouse click at a particular X,Y coordinate with text that has been positioned using LOCATE.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: determining character height in graphic modes

Post by dodicat »

You could perhaps use LOCATE via a function of a graphics position.
In this case the function is #defined()
In this way LOCATE can follow your mouse.
Or, Your mouse can find a located position (Just match the Hello)

Code: Select all

 
'=========== SET UP LOCATE =========================
#define map(a,b,x,c,d)  ((d)-(c))*((x)-(a))/((b)-(a))+(c)
#define _X(num) int( map(0,xres,num,0,loword(width))) 
#define _Y(num) int( map(0,yres,num,0,hiword(width)))

dim as integer xres,yres
screen 18
screeninfo xres,yres
'==============================================

dim as integer mx,my,mb
do
    getmouse mx,my
    
    screenlock
    cls
    locate 20,10
    print "Hello"
    'follow the mouse around
    locate _y(my),_x(mx)
    print "Hello"
    
    'A specific screen location
    if _y(my)=20 and _x(mx)=10 then locate 5,5:print "OK"
    screenunlock
    sleep 1,1
loop until len(inkey)

DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Re: determining character height in graphic modes

Post by DamageX »

haha, so in other words... yres\hiword(width). That seems to work, thank you.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: determining character height in graphic modes

Post by MichaelW »

I'm not sure that this is useful, and my test is not very good, but it does demonstrate a way to convert from pixel coordinates to character coordinates.

Code: Select all

#include "fbgfx.bi"

'screenres 640,480
'width 80,30
screen 12

function PixToCharX( byval x as integer ) as integer
    static as integer screenWidth, w, charWidth
    if charWidth = 0 then
        screeninfo screenWidth    
        w = width
        charWidth = screenWidth / loword(w)
    end if    
    return (x\charWidth) + 1
end function

function PixToCharY( byval y as integer ) as integer
    static as integer screenHeight, w, charHeight
    if charHeight = 0 then
        screeninfo ,screenHeight
        w = width
        charHeight = screenHeight / hiword(w)
    end if        
    return (y\charHeight)+1
end function

dim as integer c,x,y,b

for i as integer = 1 to 10 step 2
    locate 1,i
    print chr(219);
    locate 2,i+1  
    print chr(219);    
next
print
print
do
    do
        getmouse(x,y,,b)
        if inkey() = chr(27) then exit do,do
    loop until b = 1  
    print PixToCharY(y),PixToCharX(x)
    do
        getmouse(x,y,,b)
        if inkey() = chr(27) then exit do,do
    loop until b <> 1  
loop  
sleep
RockTheSchock
Posts: 252
Joined: Mar 12, 2006 16:25

Re: determining character height in graphic modes

Post by RockTheSchock »

Code: Select all

Type ScreenInfoType
	Declare Constructor()
	resx As Integer
	resy As Integer

	charwidth As Short
	charheight As Short
	
	rows As Integer
	cols As Integer	
End Type

Constructor ScreenInfoType()
	ScreenInfo resx,resy
	cols = LoWord(Width())
	rows = HiWord(Width())
	charwidth = resx \ cols
	charheight = resy \ rows
End Constructor


Screen 19

Dim scr As ScreenInfoType

Print "Cols:";scr.cols,"Rows:";scr.rows
Print "ResX:";scr.resx,"ResY:";scr.resy
Print "charwidth:";scr.charwidth,"charheight:";scr.charheight

Sleep
Post Reply