Scolring - Forum

Entraides et échanges autour de la technologie Scol - Informations and exchanges on the Scol technology

Vous pouvez changer la langue de l'interface une fois inscrit - You can change the language once registered

You are not logged in.

#1 23-Jan-2014 18:20:42

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

drawing string on bitmap and painting onto 2d widget

I printed out the widget api and picture plugit files, and will be looking at them when I have time, in the meantime, feel free to post pseudo code.

what I want to do

1. create widget
2. create material
3. create bitmap
4. create alpha bitmap
5. write arbitrary string onto bitmap(s?)
6. apply material to widget
7. move widget around screen to hover over moving scene node (I think I can figure this out on my own)

Offline

#2 23-Jan-2014 20:12:02

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

you can do too :

create a bitmap
write a string onto bitmap
create an alphabitmap from bitmap
create widget and material
apply alphabitmap to the material
apply material on the widget
move the widget

(to group each step (bitamp / alphabitmap, mateiral and widget) ;-)

Offline

#3 23-Jan-2014 20:41:19

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

Thx. Iri I will code in that order.

Offline

#4 25-Jan-2014 21:01:59

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

Ok this is what i am doing in trying to draw some text on a bitmap widget...I'm following what I can figure out from the picture plugit. The code compiles without errors, but all I that appears is a white widget of the dimensions I specifiy...no text appears on it.  I assume I am not handling the transparency correctly or using the wrong color codes or not using the write secquence of functions,  any input welcome.

Irir i didn't create a material because in the picture plugit it does not appear that a material is needed if not painted onto the background or onto a 3d object, but I could be wrong there.

I run the code in the addsourcecode plugit, triggered by a button, all of which is created and run in the os3d editor:

thx!

typeof widget_font= ObjFont;;
typeof message=  MessageBox;;
typeof my_widget= SO3_WIDGET;; 


fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let "Arkeon and Iri help me all the time!t" -> text in
let [128 32] -> [texWidth texHeight] in
let G2DgetStringSize widget_font  text -> [_ th] in //text height
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
(
if my_widget==nil then
(
_DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
_DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;
set my_widget= SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 256 256 100;
SO3BitmapWidgetBlitAlpha my_widget abmp;
V3DaddWidgetControl viewportstr my_widget;

)
else
(
	///destroy existing widget and recreate it
	SO3WidgetDestroy my_widget;
	_DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
	_DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;		
	set my_widget= SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 256 256 100;
	SO3BitmapWidgetBlitAlpha my_widget abmp;
	V3DaddWidgetControl viewportstr my_widget;

);
//verify code is working
set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

Last edited by hebdemnobad (25-Jan-2014 21:03:43)

Offline

#5 25-Jan-2014 21:44:03

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

Hello,

I have not check all your code, but few remarks about it :

- If you're not sure about the transparency, begin without transparency ! When you create the AlphaBitmap, set the ObjBitmap8 to nil. If the render is ok, then you add the transparency. So the background and transparency colors are nil too. You'll see these values later ! Step by step, it is easier to debug (and learn).

let _CRalphaBitmap _channel bmpRgb nil nil nil -> abmp in

- The draw text step should be done before the creation of the AlphaBitmap, not after.

let _DRAWtext
	_FILLbitmap
		_CRbitmap 
			_channel 
                        texWidth 
                        texHeight
		0xffffff
	widget_font 
	texWidth / 2 
	(texHeight / 2) - (th / 2) 
	TD_CENTER 
	0x000000 
	text
-> bmpRgb in
let _CRalphaBitmap _channel bmpRgb nil nil nil -> abmp in

You could cut this function too :
- a function to Bitmap
- a function to AlphaBitmap
- a function to widget
It's easier to debug ;-)
Your code for the text is written twice, for example.

- Later, if you're yet not sure, begin the alpha channel with a grey instead a black (or a white) :

let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x77 -> bmp8 in // 0x77 => a grey => partial transparency

- Verify your font object !
Indeed, if the font can not be created (as the font name is not found), the text will be not drawn !

set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
if widget_font == nil then  // the font creation failed
  /* do something (try with Arial for example) */
else
 nil;
/* now, your code */

If the font is OK, try a bigger size.

- Test texHeight and th. If th is too big than texHeight, the text could be unseen.

- my_widget :

if my_widget==nil then

Always true ! This object is not defined before that. So, the else block is never executed.

set my_widget= SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 256 256 100;

my_widget is OK ? It'is not nil ?

- Blit the alpha :

SO3BitmapWidgetBlitAlpha my_widget abmp;

SO3BitmapWidgetBlitAlpha should be return 1 if OK, nil if KO. Check its return.

Think to check the return value : more lines to write but the code is more safe.
You're in the right direction smile

Offline

#6 25-Jan-2014 21:59:27

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

try this

typeof widget_font= ObjFont;;
typeof message=  MessageBox;;
typeof my_widget= SO3_WIDGET;; 


fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
(
if my_widget==nil then
(
_DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
_DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;
set my_widget= SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100;
SO3BitmapWidgetBlitAlpha my_widget abmp;
V3DaddWidgetControl viewportstr my_widget;
SO3WidgetSetTransparency my_widgetn 1;
SO3WidgetSetVisibility my_widget 1;
)
else
(
	///destroy existing widget and recreate it
	SO3WidgetDestroy my_widget;
	_DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
	_DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;		
	set my_widget= SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100;
	SO3BitmapWidgetBlitAlpha my_widget abmp;
	V3DaddWidgetControl viewportstr my_widget;
	SO3WidgetSetTransparency my_widgetn 1;
  SO3WidgetSetVisibility my_widget 1;

);
//verify code is working
set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

Offline

#7 25-Jan-2014 22:01:54

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

you should also not forget to call V3DremoveWidgetControl and to destroy bitmap resources on delete

Offline

#8 25-Jan-2014 23:25:21

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

arkeon wrote:

you should also not forget to call V3DremoveWidgetControl and to destroy bitmap resources on delete


thx arkeon...how would I do that using the addsourcecode plugit (what function would I have to create?)

Iri and arkeon thx for your help....feel free to help me get this to work. At the moment, I don't really understand the relationship between bitmaps and alphabitmaps, so I'm working a bit like a monkey in a spaceship.  I'll tray to devote some time to understanding these objects.

arkeon, I tried your code, but nothing rendered on the viewport.

Offline

#9 25-Jan-2014 23:36:21

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

Bitmap are RGB components most common like BMP, JPG formats ...
Alpha Bitmaps are RGBA components like PNG format

Scol alpha bitmaps are composed of the RGB part with simple RGB components
and the Alpha part as a one channel bitmap with the type Bitmap8

This make the required four components of an RGBA bitmap

I didn't test the code, I've just modified you'rs to add missing things

note that the widget bitmap size should be the same of the widget size here.
and use SO3WidgetSetTransparency to enable alpha channel on the widget

Offline

#10 26-Jan-2014 00:07:05

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

arkeon wrote:

Bitmap are RGB components most common like BMP, JPG formats ...u
Alpha Bitmaps are RGBA components like PNG format

Scol alpha bitmaps are composed of the RGB part with simple RGB components
and the Alpha part as a one channel bitmap with the type Bitmap8

This make the required four components of an RGBA bitmap

I didn't test the code, I've just modified you'rs to add missing things

note that the widget bitmap size should be the same of the widget size here.
and use SO3WidgetSetTransparency to enable alpha channel on the widget


Thx...I have to print out and read the api.

Offline

#11 26-Jan-2014 00:40:46

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

hebdemnobad wrote:

At the moment, I don't really understand the relationship between bitmaps and alphabitmaps, so I'm working a bit like a monkey in a spaceship.  I'll tray to devote some time to understanding these objects.

The alpha channel sets the transparency for each bitmap pixel, according the value for each pixel on the alpha channel.

The channel alpha is on 8-bits, thus its value are from 0 to 255 (or FF, in hexadecimal). With 0, the pixel is fully transparent, with 255 (FF), the pixel is fully opaque. Other values allow a partial transparency, more (near 0) or less (near 255) transparent.

For example, if the alpha channel has a gradient between 0 (the pixel at the top of left) and 255 (the pixel at the bottom of right), the final image has the same gradient of transparency (transparent to the top of left, opaque to the bottom of right).

In Scol, an ObjBitmap8 object can set an alpha channel. Because is written on 8-bits.

An ObjBitmap object has ONLY the three standard channels : red, green and blue (RGB, cf Arkeon).
An AlphaBitmap object has these three channel + an alpha channel. This alpha channel can set by an ObjBitmap8 or inside an external resource like a PNG file.

_CRalphaBitmap can create a such object from an ObjBitmap and an ObjBitmap8 objects. If the ObjBitmap8 object is nil, there is no transparency set, that's all.
_LDalphaBitmap loads a PNG resource. If the PNG resource hasn't an alpha channel, the AlphaBitmap object has not a transparency too. Though this is an AlphaBitmap Scol object.

The return of these two Scol functions is a new AlphaBitmap object. You can apply it any supported operations.

If you has not needed to ObjBitmap and ObjBitmap8 objects, you should destroy them. They are already copied in the AlphaBitmap object.

Offline

#12 26-Jan-2014 01:48:39

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

iri wrote:

If you has not needed to ObjBitmap and ObjBitmap8 objects, you should destroy them. They are already copied in the AlphaBitmap object.

that make me think that in your code you draw the text after the alpha bitmap creation
you should write the text before or retrieve the copied bitmaps from the new alpha bitmap to do it

fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
let SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100 -> widget in
(
  _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
  _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;

  let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
    SO3BitmapWidgetBlitAlpha widget abmp;

  //bitmaps copied in alpha bitmap so we can destroy them
  _DSbitmap bmpRgb;
  _DSbitmap8 bmp8;
  
  V3DaddWidgetControl viewportstr widget;
  SO3WidgetSetTransparency widgetn 1;
  SO3WidgetSetVisibility widget 1;

  //verify code is working
  set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

Offline

#13 26-Jan-2014 02:37:01

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

arkeon wrote:
iri wrote:

If you has not needed to ObjBitmap and ObjBitmap8 objects, you should destroy them. They are already copied in the AlphaBitmap object.

that make me think that in your code you draw the text after the alpha bitmap creation
you should write the text before or retrieve the copied bitmaps from the new alpha bitmap to do it

fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
let SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100 -> widget in
(
  _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
  _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;

  let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
    SO3BitmapWidgetBlitAlpha widget abmp;

  //bitmaps copied in alpha bitmap so we can destroy them
  _DSbitmap bmpRgb;
  _DSbitmap8 bmp8;
  
  V3DaddWidgetControl viewportstr widget;
  SO3WidgetSetTransparency widgetn 1;
  SO3WidgetSetVisibility widget 1;

  //verify code is working
  set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

I will try this and report back.

Offline

#14 26-Jan-2014 06:06:44

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

hebdemnobad wrote:
arkeon wrote:
iri wrote:

If you has not needed to ObjBitmap and ObjBitmap8 objects, you should destroy them. They are already copied in the AlphaBitmap object.

that make me think that in your code you draw the text after the alpha bitmap creation
you should write the text before or retrieve the copied bitmaps from the new alpha bitmap to do it

fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
let SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100 -> widget in
(
  _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
  _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;

  let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
    SO3BitmapWidgetBlitAlpha widget abmp;

  //bitmaps copied in alpha bitmap so we can destroy them
  _DSbitmap bmpRgb;
  _DSbitmap8 bmp8;
  
  V3DaddWidgetControl viewportstr widget;
  SO3WidgetSetTransparency widgetn 1;
  SO3WidgetSetVisibility widget 1;

  //verify code is working
  set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

I will try this and report back.


I added the typeof statements to your code arkeon, and it runs without errors, but the bitmap widget does not render (i.e. nothing appears, neither the bitmap, nor the text):

typeof widget_font= ObjFont;;
typeof message=  MessageBox;;
typeof my_widget= SO3_WIDGET;; 

fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
let SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100 -> widget in
(
  _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
  _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;

  let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
    SO3BitmapWidgetBlitAlpha widget abmp;

  //bitmaps copied in alpha bitmap so we can destroy them
  _DSbitmap bmpRgb;
  _DSbitmap8 bmp8;
  
  V3DaddWidgetControl viewportstr widget;
  SO3WidgetSetTransparency widgetn 1;
  SO3WidgetSetVisibility widget 1;

  //verify code is working
  set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

I added the typeof statements for the global variables, but still nothing renders.....where would I find a table of gray 8 bit hex values, I have been googling but haven't found anything.

Here is my code (doesn't cause any errors or crashes, but nothing renders, as before:

typeof widget_font= ObjFont;;
typeof message=  MessageBox;;
typeof my_widget= SO3_WIDGET;; 

fun mainfunction()=
set widget_font=_CRfont _channel 12 0 0 "Lucida Console";
let [10 10] -> [xmargin ymargin] in
let "Arkeon and Iri help me all the time!t" -> text in
let G2DgetStringSize widget_font text -> [tw th] in //text height
let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
let SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 200 200 texWidth texHeight 100 -> widget in
(
  _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
  _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;

  let _CRalphaBitmap _channel bmpRgb bmp8 0 0 -> abmp in
    SO3BitmapWidgetBlitAlpha widget abmp;

  //bitmaps copied in alpha bitmap so we can destroy them
  _DSbitmap bmpRgb;
  _DSbitmap8 bmp8;
  
  V3DaddWidgetControl viewportstr widget;
  SO3WidgetSetTransparency widget 1;
  SO3WidgetSetVisibility widget 1;

  //verify code is working
  set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
);
0;;

Offline

#15 26-Jan-2014 11:07:00

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

arkeon wrote:
iri wrote:

If you has not needed to ObjBitmap and ObjBitmap8 objects, you should destroy them. They are already copied in the AlphaBitmap object.

that make me think that in your code you draw the text after the alpha bitmap creation
you should write the text before or retrieve the copied bitmaps from the new alpha bitmap to do it

iri wrote:

The draw text step should be done before the creation of the AlphaBitmap, not after.

let _DRAWtext
	_FILLbitmap
		_CRbitmap 
			_channel 
                        texWidth 
                        texHeight
		0xffffff
	widget_font 
	texWidth / 2 
	(texHeight / 2) - (th / 2) 
	TD_CENTER 
	0x000000 
	text
-> bmpRgb in
let _CRalphaBitmap _channel bmpRgb nil nil nil -> abmp in

Arkeon and hebdemnobad, you should read my post ! :D {joke}


hebdemnobad wrote:

where would I find a table of gray 8 bit hex values, I have been googling but haven't found anything

See my previous post too.

Here, the 8-bits values are the code for 255 different greys. Near 0, greys are almost black; near 255, greys are almost white.

0 in decimal is 0 in hexadecimal
255 in decimal is FF in hexadecimal

The HTML/CSS color is in hexadecimal.

For example, when you writein CSS :

#myColor {
  color: #D5A52B;
}

you give the three components R G and B.
The component R is D5, G is A5 and B is 2B.

It's the same thing here.
Instead of give 3 color components, you give only one.

In CSS, for a grey, you could write :

#myGrey1 {
  color: #333333;
}

#myGrey2 {
  color: #888888;
}

#myGrey3 {
  color: #CCCCCC;
}

Here, your grey1 is 33, grey2 is 88, grey3 is CC ...

hebdemnobad wrote:

Here is my code (doesn't cause any errors or crashes, but nothing renders, as before:

Are you checked the return value as i've suggered ?
For the bitmap (ObjBitmap, not AlphaBitmap), you can try to save it, to see its render by an external software : see my post here.

Offline

#16 26-Jan-2014 14:06:30

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

You can only create a widget with the same name once, since there is no unloading function in the plugIT addsourcecode you can't try this in it.

Offline

#17 26-Jan-2014 14:22:32

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

try this

let _CRalphaBitmap _channel bmpRgb bmp8 nil nil -> abmp in
the last params should be nil otherwise it erase the previous bitmaps content (perform a fillbitmap)
so the text was erased before the widget blit

typeof widget_font= ObjFont;;
typeof message=  MessageBox;;
typeof my_widget= SO3_WIDGET;; 

fun drawBitmap(text, wsize, tsize)=
  let wsize -> [texWidth texHeight] in
  let tsize -> [tw th] in
  let _FILLbitmap (_CRbitmap _channel texWidth texHeight) 0xffffff -> bmpRgb in
  let _FILLbitmap8 (_CRbitmap8 _channel texWidth texHeight) 0x00 -> bmp8 in
  (
    _DRAWtext bmpRgb widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0x000000 text;
    _DRAWtext8 bmp8 widget_font (texWidth / 2) (texHeight / 2) - (th / 2) TD_CENTER 0xff text;
    
    let _CRalphaBitmap _channel bmpRgb bmp8 nil nil -> abmp in
      SO3BitmapWidgetBlitAlpha my_widget abmp;
  
    //bitmaps copied in alpha bitmap so we can destroy them
    _DSbitmap bmpRgb;
    _DSbitmap8 bmp8;
  );
  0;;


fun mainfunction()=
  let (V3DgetDefaultViewport (V3DgetSessionView c3dXsession)) -> viewportstr in
  let [10 10] -> [xmargin ymargin] in
  let "Arkeon and Iri help me all the time!" -> text in
  (
    set widget_font = _CRfont _channel 12 0 0 "Lucida Console";
    let G2DgetStringSize widget_font text -> [tw th] in //text height
    let [(tw + (xmargin * 2)) (th + (ymargin * 2))] -> [texWidth texHeight] in
    (
      set my_widget = SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport "new_widget" 0 0 texWidth texHeight 100;
      V3DaddWidgetControl viewportstr my_widget;
  
      drawBitmap text [texWidth texHeight] [tw th];
    );
  
    SO3WidgetSetTransparency my_widget 1;
    SO3WidgetSetKeyboardEnable my_widget 0;
    SO3WidgetSetMouseEnable my_widget 0;
    SO3WidgetSetVisibility my_widget 1;
    
    //verify code is working
    set message= _DLGMessageBox _channel nil "this is scan code" "HELLO" 0;
  );
  0;;

Offline

#18 26-Jan-2014 14:29:46

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

arkeon wrote:

You can only create a widget with the same name once, since there is no unloading function in the plugIT addsourcecode you can't try this in it.

that is what I suspected, well that's ok since I will be puttting it in my modified consolechat plugit which has an unloading callback

Offline

#19 26-Jan-2014 14:31:14

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

Thx for the remineder Iri...how would you draw the text into the 8bit bitmap?
I will try saving the resulting bitmap to a file later.

Offline

#20 26-Jan-2014 14:32:15

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

thx arkeon...I ran your code, but no widget renders.  This widget is stubbornly transparent.

Offline

#21 26-Jan-2014 16:51:33

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

From the code posted by Arkeon, i wrote this.
The RGB bitmap is saved in a file (you should remove this later !). You should see the result ...
To avoid the problem with a same widget name, this name is automatically modified each time.

However, if the text to draw is too long, the height of the drawn text will be too little onto the bitmap, because the ratio width/height is bad. You could test with a shorter text. Or cut the text in two lines with a "\n".

I test this code in a 3d environment but not under os3d. So, if the render failed, check the bitmap saved file and the popup message.

var WHITE = 0xFF;;
var BLACK = 0x00;;
var MARGIN = 10;;	/* a margin around the text to draw, in pixel */
var IS_TRANSPARENT = 1;;	/* if you don't want the transparency, set it to 0 */

var FONT_NAME = "Arial";;
var FONT_SIZE = 24;;

typeof widget_font = ObjFont;;
typeof my_widget= SO3_WIDGET;; 

/*
	fun [S S] I
	
	Display a popup message with the function name
	
	S : the function name
	S : the message to show
	
	I : return always 1
*/
fun displayDebug (funName, message)=
	_DLGMessageBox _channel nil "!! Debug !!" strcatn funName :: "\n\n" :: message :: nil 0;
	1;;

/*
	fun [ObjBitmap] I
	
	Save a RGB bitmap in a file
*/
fun saveBmp (bmpRGB)=
	_SAVEbitmap bmpRGB _getmodifypack "tests/bmpRGB.bmp";
	0;;


/*
	fun [S] [I I]
	
	return the width and the height depending on the given font
*/
fun getTextSize (text, font)=
	G2DgetStringSize font text;;
	
/*
	fun [S] [I I I I]
	
	return the bitmap size according to the text to draw and its margin
	
	S : the text to draw
	
	[I I I I] : the width and the height of the bitmap, the width and
	the height of the text
*/
fun getSize (text)=
	let getTextSize	text widget_font -> [txtWidth txtHeight] in
	[(txtWidth + (MARGIN * 2)) (txtHeight + (MARGIN * 2)) txtWidth txtHeight];;
	
/*
	fun [S I I] ObjBitmap
	
	create, fill and draw a text onto a 24-bits bitmap object
	
	S : a text to draw
	I : the 24-bits color (like HTML) to fill the bitmap
	
	ObjBitmap : the new bitmap or nil if error
*/
fun drawBitmap (text, colorRGB)=
	let getSize text -> [bmpWidth bmpHeight txtWidth txtHeight] in
	_DRAWtext
		_FILLbitmap
			_CRbitmap 
				_channel 
				bmpWidth 
				bmpHeight
			colorRGB
		widget_font 
		bmpWidth / 2 
		MARGIN
		TD_CENTER|TD_TOP
		BLACK 
		text;;

/*
	fun [S I I] ObjBitmap8
	
	create, fill and draw a text onto a 8-bits bitmap object
	
	S : a text to draw
	I : the 24-bits color (like HTML) to fill the bitmap
	
	ObjBitmap8 : the new 8-bits bitmap (alpha) or nil if error
*/		
fun drawBitmap8 (text, colorGrey)=
	let getSize text -> [bmpWidth bmpHeight txtWidth txtHeight] in
	_DRAWtext8
		 _FILLbitmap8
			_CRbitmap8 
				_channel 
				bmpWidth 
				bmpHeight
			colorGrey
		widget_font 
		bmpWidth / 2 
		MARGIN
		TD_CENTER|TD_TOP
		WHITE 
		text;;
		
/*
	fun [S [I I] I] [ObjBitmap ObjBitmap8]
	
	create the two components for the AlphaBitmap : a 24-bits bitmap
	and a 8-bits bitmap if asked.
	
	S : a text to draw
	[I I] : the RGB color and the Grey color to fill the bitmaps background
	I : a flag : if you want an alpha channel (a 8-bits bitmap), set it to 1
	else set it with another value
	
	[ObjBitmap ObjBitmap8] : a tuple with the two resources or nil if error
	
	The second item can be nil too if you don't want the transparency (flag != 1)
*/
fun drawBitmaps (text, colors, iBmp8Too)=
	if widget_font == nil then
		nil
	else
		let colors -> [colorRGB colorGrey] in
		let drawBitmap text colorRGB -> bmpRGB in
		let
		 	if iBmp8Too then
		 		drawBitmap8 text colorGrey
			else
				nil
			-> bmpGrey in
	[bmpRGB bmpGrey];;
	
/*
	fun [S] AlphaBitmap
	
	create a new alphabitmap and save and destroy the temporary 2d resources
	
	S : a text to draw
	
	AlphaBitmap : the new AlphaBitmap or nil if error
*/
fun crAlphaBitmap (text)=
	let drawBitmaps text [WHITE BLACK] IS_TRANSPARENT -> [bmpRGB bmpGrey] in
	let _CRalphaBitmap _channel bmpRGB bmpGrey nil nil -> abmp in
	(
        if abmp == nil then
		displayDebug "crAlphaBitmap" "Error !\nUnable to create the AlphaBitmap !"
	else
                0;
	saveBmp bmpRGB;
	_DSbitmap bmpRGB;
	_DSbitmap8 bmpGrey;
	abmp
	);;
	
/*
	fun [ ] I
	
	Try to create a font object.
	
	I : return 0 if success, 1 if fail
*/
fun crFont ()=
	set widget_font = _CRfont _channel FONT_SIZE 0 0 FONT_NAME;
	if widget_font == nil then
		displayDebug "crFont" "Error !\nUnable to create the font !"
	else
		0;;
		
/*
	fun [S] S
	
	S : a base name for the 3d resource
	
	S : return an unique name
*/
fun getName3dRsc (name)=
	strcat name itoa _tickcount;;
	
/*
	fun [S [I I]] I
	
	create a widget.
	
	S : the name of this resource (it will be unique by call to getName3dRsc function)
	[I I] : the widget size
	
	I : return 0 if success, 1 if fail
*/
fun crWidget (name, size)=
	set name = getName3dRsc name;
	let size -> [width height] in 
	let V3DgetDefaultViewport 
			V3DgetSessionView c3dXsession
		-> viewportstr in
	(
		set my_widget = SO3BitmapWidgetCreate (V3DgetSession c3dXsession) viewportstr.V3D_viewport name 0 0 width height 100;
		if my_widget == nil then
			displayDebug "crWidget" "Error !\nUnable to create the widget !"
		else
		(
			V3DaddWidgetControl viewportstr my_widget;
			SO3WidgetSetTransparency my_widget 1;
			SO3WidgetSetKeyboardEnable my_widget 0;
			SO3WidgetSetMouseEnable my_widget 0;
			SO3WidgetSetVisibility my_widget 1;
			0
		)
	);;
		
fun main ()=
	crFont;
	let "Arkeon and Iri help me all the time!" -> text in
	let crAlphaBitmap text -> abmp in
	let getSize text -> [width height _ _] in
	(
	crWidget "new_widget" [width height];
	if 1 != SO3BitmapWidgetBlitAlpha my_widget abmp then
		displayDebug "main" "Error !\nUnable to lit the AlphaBitmap !"
	else
		0;
	displayDebug "main" "Info !\nFinished !";
	_showconsole;
	0
	);;

Offline

#22 26-Jan-2014 17:14:28

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

I ran the code I posted in the addsourcecode plugIT and get the text on the widget.
but sure it works only at the first start, since the widget is not destroyed.
then you have to restart OS3D.

Offline

#23 26-Jan-2014 17:54:26

iri
Admin. / Scol language & Scol apps developer
From: France
Registered: 22-Feb-2009
Posts: 2,024
Website

Re: drawing string on bitmap and painting onto 2d widget

In this case, add a destroy function.

There is not a such destroy function in the so3engine ?

Offline

#24 26-Jan-2014 21:33:07

arkeon
Admin. / Scol language & OpenSpace3D developer
From: Nantes
Registered: 30-Mar-2009
Posts: 5,091
Website

Re: drawing string on bitmap and painting onto 2d widget

sure it have a destroy function.
I mean there is no destroy function in the addsourcecode plugIT
like we set the start function a destroy function should be added in the plugIT

Offline

#25 27-Jan-2014 20:19:48

hebdemnobad
Member
From: northamerica
Registered: 20-Apr-2011
Posts: 1,477
Website

Re: drawing string on bitmap and painting onto 2d widget

Iri your code works in os3d arkeon's does not....I have a crappy laptop GPU.

Iri what makes the text appear gray? Your code only fills with black or white.... maybe I miss something.

Thx guys I will apply this to my chat project soon.

Last edited by hebdemnobad (27-Jan-2014 20:21:06)

Offline

Board footer

Powered by FluxBB