Max2D in MiniB3D v0.41

BlitzMax Forums/MiniB3D Module/Max2D in MiniB3D v0.41

Oddball(Posted 2007) [#1]
Just replace the GraphicsInit() function (TGlobal.bmx line 311) with these three functions and it's ready to use.

Edit 16 May 07: Sorry about that guys it should be working 100% now.


To start using Max2D commands use TGlobal.BeginMax2D() and when you want to go back to using MiniB3D commands simple use TGlobal.EndMax2D() like so.

Enjoy.


impixi(Posted 2007) [#2]
Works great. Thanks!


TobyTyke(Posted 2007) [#3]
H

Been trying to use 2d command in minib3d, But i'm having no luck. Tried the this mod and it seems to work better. But when I tried the attached code all I get is block instead of text. Any Ideas what i'm doing wrong.

Thanks in advance
-------
Import sidesign.minib3d
Graphics3D 800,600
Global camera=CreateCamera()
Global light=CreateLight()
Global world=CreateSphere(12)
PositionEntity camera,90,0,0
PositionEntity world,0,0,0
RenderWorld
TGlobal.BeginMax2D
SetBlend AlphaBlend
SetAlpha(0.5)
SetColor 255,0,0
DrawText("Hello",50,50)
TGlobal.EndMax2D
Flip
WaitKey()

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


impixi(Posted 2007) [#4]
For me the text displays but not the sphere because it is out of the camera's view. However, if I fix the camera's coordinates the sphere still does not display unless I move RenderWorld and Flip into a render loop and disable the 2d stuff, like so:

Import sidesign.minib3d
Graphics3D 800,600
Global camera=CreateCamera()
Global light=CreateLight()
Global world=CreateSphere(12)
PositionEntity camera,-5,0,-10		'***
PositionEntity world,0,0,0
PointEntity camera, world		'***


While Not KeyHit(KEY_ESCAPE)           '***
		
	RenderWorld
	
	Rem                                         '***
	
	TGlobal.BeginMax2D
	SetBlend AlphaBlend
	SetAlpha(0.5)
	SetColor 255,0,0
	DrawText("Hello",50,50)
	SetBlend SolidBlend
	TGlobal.EndMax2D
	
	EndRem                                    '***
	
	Flip

Wend

End


If I uncomment the 2d stuff the sphere will not display but the text will.

I don't know why the hell it isn't working, as I have far more complicated 2d over 3d stuff working without problems (aside from reduced framerates as expected).

If I had more time, I'd investigate further but for now I can't help you. Perhaps Oddball has some ideas?


impixi(Posted 2007) [#5]
Looks like it's something to do with lighting/texturing/coloring initialisation. In the above example (with the 2d stuff uncommented) the sphere is there, it's just black on a black background. And the EntityColor command has no effect.

If, however, I apply a texture to the sphere, then it all seems okay.

SuperStrict

Import sidesign.minib3d

Graphics3D 800, 600

Local camera:TCamera = CreateCamera()

Local light:TLight = CreateLight()

Local sphere:TMesh = CreateSphere()

EntityColor sphere, 200, 100, 255

EntityTexture sphere, LoadTexture("c:\program files\blitzmax\samples\firepaint\player.png")			

PositionEntity camera, -5.0, 0.0, -10.0
PositionEntity sphere, 0.0, 0.0, 0.0
PointEntity camera, sphere		

While Not KeyHit(KEY_ESCAPE)          
	
	Cls		

	RenderWorld	

	TGlobal.BeginMax2D
	
	SetBlend AlphaBlend
	SetAlpha 0.5
	SetColor 255, 0, 0
	DrawText "Hello", 50, 50
	
	TGlobal.EndMax2D

	Flip

Wend

End



mongia2(Posted 2007) [#6]
work!

i change a Import sidesign.minib3d

in Import klepto.minib3d


Oddball(Posted 2007) [#7]
Oops! Just shows that one line of code can make all the difference. I've edited the code in the first post it should work fine now. Let me know if there are anymore issues. On a side note it is possible to make this work seemlessly with MiniB3D, i.e. Without the BeginMax2D and EndMax2D commands, but it's a little more than just a cut and paste. If I find time, and people are interested, I'll make a tutorial for it.


mongia2(Posted 2007) [#8]
thanks oddball for code!
mongia


mongia2(Posted 2007) [#9]
SuperStrict
Import klepto.minib3d

'my problem is
' i have a desktop resolution 1024 x 768

' drawrect is a not a 0,0,50,20
' i dont visible a rect!

'windows is Not a 0,0 but is a 70,50!
' windows not a center



Graphics3D 1024,768

Local camera:TCamera = CreateCamera()

Local light:TLight = CreateLight()

Local sphere:TMesh = CreateSphere()

EntityColor sphere, 200, 100, 255

PositionEntity camera, -5.0, 0.0, -10.0
PositionEntity sphere, 0.0, 0.0, 0.0
PointEntity camera, sphere

While Not KeyHit(KEY_ESCAPE)

Cls

RenderWorld

TGlobal.BeginMax2D

SetBlend AlphaBlend
SetAlpha 0.5
SetColor 255, 0, 0
DrawText "Hello", 50, 50


DrawRect 0,0,50,20



TGlobal.EndMax2D

Flip

Wend

End


thanks


Oddball(Posted 2007) [#10]
This is actually a bug in Max2D. Try this code which contains no MiniB3D code at all and you get the same results.

It will work fine in fullscreen. I've posted a bug report about this.


mongia2(Posted 2007) [#11]
in full screen work!!


JonasL(Posted 2007) [#12]
Hi, thanks for the code! I have been experimenting with it and got some strange results when using MaxGUI. I have posted a bare bones version. In MaxGUI after receiving an EVENT_GADGETPAINT you need to use SetGraphics(CanvasGraphics(canvas)) to select the canvas in which drawing should take place. When I do that the rect first shows up momentarily, then becomes invisible. If I comment out SetGraphics() it works, but then I can have only one canvas gadget and have to hold my breath that MaxGUI doesn't decide to redirect drawing somewhere. Now to make things even stranger, try commenting in the second DrawRect()! It solves the problem. First rect is visible again! Could someone confirm this? Or perhaps enlighten me on what the h** is going on here! ;)

Mac PPC G4 1 GHz, GeForce4 MX, MacOS X 10.4.9




Oddball(Posted 2007) [#13]
Not had chance to test your code, but will take a look when I get a chance. I'm not suprised that MaxGUI makes my code do strange things. I didn't really take MaxGUI into consideration when writing it.


jhocking(Posted 2007) [#14]
If... people are interested


*raises hand*


Oddball(Posted 2007) [#15]
@JonasL: I've just had a quick look at your code and the reason it's acting funny is because your SetGraphics is outside the Max2D zone. So in effect it does nothing as when BeginMax2D is called it resets the graphics to it's state at the last EndMax2D. Here's your code with a little alteration.

Using this method you should also be able to have multiple canvases. But I've not tested multiple canvases yet so please feel free to prove me wrong :)

@jhocking: Well there's one vote at least. My integrated version also has the benefit of avoiding issues like the one above as it's always in Max2D mode.


JonasL(Posted 2007) [#16]
@Oddball: Many thanks for helping me with this one! I'm only using one canvas, but it still feels a lot safer.

The problem with hacking the engine is that it is very much work in progress and new versions are being released rather often. I really think simonh is doing an excellent job with MiniB3D, but IMHO all the forking is a bad idea. There should be one official open source MiniB3D project. That is the only way we will be able to iron out more bugs than we create while adding new features. That project should be tightly managed and we should have a code repository (CVS, Subversion), some bug tracker (Flyspray, Bugzilla) and a proper source documentation tool. However, that is not what simonh had in mind and it is very much still his project, which I respect.


Oddball(Posted 2007) [#17]
The problem with hacking the engine is that it is very much work in progress and new versions are being released rather often
Well until simonh adds new code to the GraphicsInit function this hack should work. Basically my code just pushes everything possible onto their relivant stacks then pops the values off again when needed. I actually push more data than is needed but ,as you say, with it still being in development I thought it was wise to err on the side of caution.

I don't use MiniB3D personally, I've got my own engine, but I'll pop back each new update to check this code is still valid.


mongia2(Posted 2007) [#18]
my problem is:

if use a renderworld
in render 2d
i have a problem for graphics position!

Import klepto.minib3d
SuperStrict


Graphics3D 1024 , 768' , 32 , 1
ClearTextureFilters()



Global camera:TCamera = CreateCamera()

Global luce:TLight = CreateLight()

Global piano:TMesh = CreateCube()
ScaleEntity piano,1000,.1,1000
EntityOrder piano,5


PositionEntity camera, 0.0, 100, 0.0
PointEntity camera, piano


While Not KeyHit(KEY_ESCAPE)

PositionEntity camera,0,100,0
RenderWorld()

render_2d()

Flip

Wend



Function render_2d()


TGlobal.BeginMax2D

SetBlend AlphaBlend
SetAlpha 1

SetColor 255, 255, 255


DrawRect 0,0,20,20

If KeyDown(KEY_F1)
RenderWorld()
EndIf

TGlobal.EndMax2D


End Function

thanks


Oddball(Posted 2007) [#19]
I'm not exactly what you're trying to do with that code mongia2, but basically miniB3D code cannot be used within a BeginMax2D and EndMax2D block. Just move the RenderWorld outside the Begin/EndMax2D block and it works fine. See below.

There are exceptions to this rule but it's safest to just use Max2D commands within the Begin/EndMax2D block and just use miniB3D commands outside the Begin/EndMax2D block. If need be use more than one block, but don't mix the code.

Edit: Thought I'd better add too that this code is designed for the vanilla version of miniB3D. Whilst it may work with other versions, like klepto's, I can't guarantee it.


mongia2(Posted 2007) [#20]
i send a bug!

i correct thanks!!!


but i have a problem form velocity and slow with a manipolation texture


klepto2(Posted 2007) [#21]
I can confirm that it works in my version as I have implemnted these extra functions already. These works much better than mine previously included max2d interfacing . So much thx to you Oddball.


Oddball(Posted 2007) [#22]
So much thx to you Oddball
No problem. Just glad I could help out the miniB3D project.


JonasL(Posted 2007) [#23]
Well until simonh adds new code to the GraphicsInit function this hack should work. Basically my code just pushes everything possible onto their relivant stacks then pops the values off again when needed. I actually push more data than is needed but ,as you say, with it still being in development I thought it was wise to err on the side of caution.


Again thanks for supporting the project. I agree that your code is a very robus solution and was not particularly talking about your "hack". It would be nice if simonh added your code to the official version or perhaps even your integrated version. I really respect simon's decision to keep thing simple, but IMHO Max2D is really needed and can't be substituted with sprites as he suggests. I really wish that he would change his mind on this one! ;)


siread(Posted 2007) [#24]
Hi Oddball. Is is possible to modify this code to work with other 3d mods, such as Leadwerks or Blitz3dSDK?


Oddball(Posted 2007) [#25]
Hi Oddball. Is is possible to modify this code to work with other 3d mods, such as Leadwerks or Blitz3dSDK?
The Blitz3DSDK use DirectX and this code uses OpenGL only so they are not compatible. The Leadwerks engine I believe is written in OpenGL so absolutely. However nobody except the author knows exactly what's going on inside the engine so he'd have to write his own version of the code. The code it self is fairly generic and it's a modified version of the code from my own engine, but it did need tweaking to work with miniB3D and so I'd expect the same to be true for the Leadwerks engine.