Dark basic to Blitz conversion

Blitz3D Forums/Blitz3D Beginners Area/Dark basic to Blitz conversion

RobbyK(Posted 2008) [#1]
I just switched to Blitz3d and want to convert a card game I am working on. I was wondering how I would convert this code snippet below where I am capturing card face images from a larger image and assigning them to an array variable.

load bitmap "spades.png",1
set current bitmap 1
imageNo=1
ctr=1
for y=0 to 3
for x=0 to 3
get image imageNo, x*94,y*145,((x+1)*94)-1,((y+1)*145)-1
jcard(imageno)=ctr
inc imageNo
inc ctr
next x
next y


Thanks for any help you can give me.

I can post the whole code if anyone wants it. It deals five hands at a time and evaluates what thier value is. It at least has the ability to evaluate poker hands. I want to convert what I have in DP pro to blitz3d and make a real poker game out of it. If someone wants my DB Pro code they will have to let me know how to upload a zip file with program and assets.


Matty(Posted 2008) [#2]
Blitz's loadanimimage command would do this fine,

myimage = loadanimimage("spades.png",94,145,0,16)


then when you want to draw that:
drawimage myimage,xpos,ypos,frame
;xpos and ypos are values you specify
;frame is a value you specify which would be from 0-15 in this case

have a look at the drawimage and loadanimimage commands in the command reference


if you would rather to it in an array then you would do something like this:

(note there are many ways of doing all of these)

myimage=loadimage("spades.png")
Dim imagearray(15)
for y=0 to 3
for x=0 to 3
imagearray(frame)=createimage(94,145)
CopyRect x*94,y*145,94,145,0,0,imagebuffer(myimage),imagebuffer(imagearray(frame))

frame=frame+1
next
next



RobbyK(Posted 2008) [#3]
Thanks for the quick reply.

Later in my program I assign the images to sprites so I can move them around easily. The loadsprite command in Blitz3d seems to want to load an actual file from disk. Is there a way to assign the image I captured from the large base image I captured the cards from to a sprite?.

Also sprites do not seem to have a number in blitz such as DB Pro
See below

displaycards:
set current bitmap 0
cls

sprite 1,50,7,deck(1)
sprite 2,150,7,deck(2)
sprite 3,250,7,deck(3)
sprite 4,350,7,deck(4)
sprite 5,450,7,deck(5)

sprite 6,50,159,deck(6)
sprite 7,150,159,deck(7)
sprite 8,250,159,deck(8)
sprite 9,350,159,deck(9)
sprite 10,450,159,deck(10)

how would i make a sprite array?
or assign a number to a sprite like DB Pro

sprite(2)=loadsprite("sp2.png") ?


There always seems to be tons of samples around but none that actually relate to what you want to do.


RobbyK(Posted 2008) [#4]
These forums could use a keyword or phrase function. There appears to be a tremendous amount of info on these forums but the problem is sifting through it to find the info you need.

I haven't even looked at my card game code for a year and I even have trouble following it and I wrote it.

Spending too much time on my day job doing Architectural renderings. I eventually want to be able to bring stripped down models of our projects into blitz3d so clients can manouver around them in 3D.


Matty(Posted 2008) [#5]
Yes -

use loadanimtexture in a similar way to the loadanimimage command.
Make sure that your textures are power of 2 in size, and preferably, though
not necessarily, square).

Create your sprites using 'createsprite' and then use the
entitytexture command with the frame parameter specifying which frame of the
animtexture to use.

Sprites in blitz are 3d entities and are used in 3d mode,
images in blitz are 2d and are used in either 2d or 3d mode, but for performance
and compatibility reasons are best used only in 2d mode.
Textures are similar to images except they are are used only in 3d mode.

Sprites/Images/Textures do have a number, called a handle. Unlike Darkbasic
which you allocate an image to a number which you specify, in blitz a number
(handle) is assigned to any entity/texture/image/sound/bank/music which is
created and by which you are then able to reference it.


Have you had a look at the samples in the blitz3d samples folder?
They cover much of this, as does the introductory help area.


RobbyK(Posted 2008) [#6]
I seem to be having a tough time figuring out the basics of Blitz3d.
When I got DB Pro I looked at some samples and was on my way. I guess I have to free my mind and read as if i never saw DB Pro before.
All reports said Blitz was easier but I can't seem to figure out its structure. for example how would i assign an image to a sprite and then have it follow my mouse pointer on the screen or even just display the sprite like I have shown in the DB Pro snippet above

sprite 9,350,159,deck(9)
Simple and efffective in DB Pro it means display sprite number 9 at
position 350,159 and use image stored in deck(9) variable

SPRITE Sprite Number, XPos, YPos, Image Number

What would be the blitz equivalent?

There is a pile of sprite commands in DB Pro as seen below:

SPRITE : SPRITE Sprite Number, XPos, YPos, Image Number
SET SPRITE : SET SPRITE Sprite Number, Backsave, Transparency
DELETE SPRITE : DELETE SPRITE Sprite Number
CLONE SPRITE : CLONE SPRITE Sprite Number, Destination Sprite Number
SHOW SPRITE : SHOW SPRITE Sprite Number
HIDE SPRITE : HIDE SPRITE Sprite Number
HIDE ALL SPRITES : HIDE ALL SPRITES
SHOW ALL SPRITES : SHOW ALL SPRITES
MOVE SPRITE : MOVE SPRITE Sprite Number, Velocity
OFFSET SPRITE : OFFSET SPRITE Sprite Number, XOffset, YOffset
SCALE SPRITE : SCALE SPRITE Sprite Number, Scale
SIZE SPRITE : SIZE SPRITE Sprite Number, XSize, YSize
STRETCH SPRITE : STRETCH SPRITE Sprite Number, XScale, YScale
ROTATE SPRITE : ROTATE SPRITE Sprite Number, Angle
FLIP SPRITE : FLIP SPRITE Sprite Number
MIRROR SPRITE : MIRROR SPRITE Sprite Number
PASTE SPRITE : PASTE SPRITE Sprite Number, XPos, YPos
CREATE ANIMATED SPRITE : CREATE ANIMATED SPRITE Sprite Number, Filename, Frame Across, Frame Down, Image Number
PLAY SPRITE : PLAY SPRITE Sprite Number, Start Frame, End Frame, Delay Value
SET SPRITE FRAME : SET SPRITE FRAME Sprite Number, Frame Value
SET SPRITE PRIORITY : SET SPRITE PRIORITY Sprite Number, Priority
SET SPRITE IMAGE : SET SPRITE IMAGE Sprite Number, Image Number
SET SPRITE ALPHA : SET SPRITE ALPHA Sprite Number, Alpha Value
SET SPRITE DIFFUSE : SET SPRITE DIFFUSE Sprite Number, Red Value, Green Value, Blue Value
SET SPRITE TEXTURE COORD : SET SPRITE TEXTURE COORD Sprite Number, Vertex Index, UValue, VValue
SPRITE EXIST : Return Integer=SPRITE EXIST(Sprite Number)
SPRITE X : Return Integer=SPRITE X(Sprite Number)
SPRITE Y : Return Integer=SPRITE Y(Sprite Number)
SPRITE OFFSET X : Return Integer=SPRITE OFFSET X(Sprite Number)
SPRITE OFFSET Y : Return Integer=SPRITE OFFSET Y(Sprite Number)
SPRITE SCALE X : Return Integer=SPRITE SCALE X(Sprite Number)
SPRITE SCALE Y : Return Integer=SPRITE SCALE Y(Sprite Number)
SPRITE WIDTH : Return Integer=SPRITE WIDTH(Sprite Number)
SPRITE HEIGHT : Return Integer=SPRITE HEIGHT(Sprite Number)
SPRITE IMAGE : Return Integer=SPRITE IMAGE(Sprite Number)
SPRITE MIRRORED : Return Integer=SPRITE MIRRORED(Sprite Number)
SPRITE FLIPPED : Return Integer=SPRITE FLIPPED(Sprite Number)
SPRITE HIT : Return Integer=SPRITE HIT(Sprite Number, Target Sprite Number)
SPRITE COLLISION : Return Integer=SPRITE COLLISION(Sprite Number, Target Sprite Number)
SPRITE ANGLE : Return Float=SPRITE ANGLE(Sprite Number)
SPRITE FRAME : Return Integer=SPRITE FRAME(Sprite Number)
SPRITE ALPHA : Return Integer=SPRITE ALPHA(Sprite Number)
SPRITE RED : Return Integer=SPRITE RED(Sprite Number)
SPRITE GREEN : Return Integer=SPRITE GREEN(Sprite Number)
SPRITE BLUE : Return Integer=SPRITE BLUE(Sprite Number)
SPRITE VISIBLE : Return Integer=SPRITE VISIBLE(Sprite Number)

I can only find a handful of Sprite commands in Blitz. Are they handled that much different in Blitz?

Can anyone recommend a good reference article or book where explanations of commands are grouped as to there purpose similar to the section on sprites shown in a snippet from DB Pro's command reference.


RobbyK(Posted 2008) [#7]
Thanks Matty

I think I am just a little too impatient. Obviously Blitz has a completely different Philosophy Than DP Pro.
Like I said there seems to be a gigantic amount of reference code samples but not a lot with headings like "how to assign sprites and move/manipulate them on screen"

I started out with Applebasic then Amiga basic then AMOS Basic ont he Amiga also. I went to Delhpi on the PC (not great for games).
Then I tried DB Pro (I like it but it is very buggy).

I read reviews of Blitz3d and bought it today. I'm sure after more experimentation I will start to understand but at the moment it is just pissing me off because I can't seem to find sample code that relates to what I want to do. It is very daunting when you go to the beginner area and there are something like 170 pages of topics and no search function so you can't search for the topic you want to find out about.

I truly appreciate the help you have given me so far.


I think I will post very short simple examples for doing things like:

- How to assign sprites and move/manipulate them on screen
- Have a sprite follow your mouse cursor
- How to capture images from a larger image and assign them to a sprite
- etc
- etc
- etc

Now i just have to find time to do it.

Thanks again


Gabriel(Posted 2008) [#8]
Every group of forums has a search button above it, on the far right side of the screen. There's also a search all at the very top, but since you only want Blitz3D results, that's probably the one to use.


RobbyK(Posted 2008) [#9]
Ok now I feel like a complete IDIOT. Too much 3D rendering has fried my brain. I guess I should just be happy my employees are not on these forums.

www.liquidlight3d.com


Billp(Posted 2008) [#10]
sprites are entitys all non mesh specific entity commands work with sprites as well...hideentity sprite, showentity sprite etc.


Warner(Posted 2008) [#11]
In google, you can add "site:blitzbasic.com" (without quotes) or for example "site:http://www.blitzmax.com/codearcs" to your search term.


Beaker(Posted 2008) [#12]
Instead of using CreateSprite, LoadTexture and then EntityTexture, you could use LoadSprite and CopyEntity. Instead of this:
mySprite = CreateSprite()
myTexture = LoadTexture("myImage.png")
EntityTexture mySprite,myTexture

use this instead (and into an array):
Local mySpriteArray[10]
mySprite = LoadSprite("myImage.png")
mySpriteArray[1] = CopyEntity(mySprite)



Pirate(Posted 2008) [#13]
Can anyone recommend a good reference article or book where explanations of commands are grouped as to there purpose similar to the section on sprites shown in a snippet from DB Pro's command reference.


hey robbyk,
welcome to blitz3d...i too came over here from dbpro...for me it is the best decision made...it takes a little while to grasp b3d compared to dbpro but when you do you will see....more speed, less code and very few bugs if any at all....

most everything you put in blitz is an entity...study the entity commands carefully...these commands have something for almost everything....

the blitz3d programming manual can be ordered from the products page...this book has helped me a lot...i think it is $19.99U.S.

you can also look at the online manuals on the manuals page...

remember to be patient...starting any new language can be very frustrating....try just working with simple code for your sprites...a lot of good code can be found in the code archives forum...


RobbyK(Posted 2008) [#14]
Thanks everyone for your comments. I am impressed with the willingness of the community to help out. I'm sure once I get settled in I won't be bugging you that much more and I hope to give back any information I learn.

I got a quick drawimage thingy to follow my mouse and then did some variations on it. It works great but I think it was a destructive type drawing because there was a CLS command at the end of the loop.
That's why my interest in sprites so I can move objects around without destroying the backround. Oddly enough my backround is in 3d but I want to do a 2d game first and then move to 3d later.
You guys might want to look at a program a friend of mine wrote for converting 3D objects. It will save out .3DS and .X file formats. I have used it for years for converting my 3D Cad stuff over to my rendring program (Lightwave 3D by Newtek at the moment)
Here is the address of the Accutrans 3D program:

http://www.micromouse.ca/

It's graphic interface might not look very impressive but it works very well. He has sold copies to car manufacteres, US army, Game studios and more. The program is free to try out and only $20 to buy.


Beaker(Posted 2008) [#15]
For 3D file conversion I use Unwrap3D (and sometimes Fragmotion). $20 sounds like a lot for something that only converts (static) meshes. Looks like it might have its uses tho.


RobbyK(Posted 2008) [#16]
Beaker

I tried your code below and I get the message "entitiy does not exist"
when I tried to run it in my program and it highlights the third line of code "mySpriteArray[1] = CopyEntity(mySprite)"

Local mySpriteArray[10]
mySprite = LoadSprite("myImage.png")
mySpriteArray[1] = CopyEntity(mySprite)

Can you give me a a small program that creates a sprite and assigns a image to it and then positions the sprite somewhere on the screen?

Thanks


Matty(Posted 2008) [#17]
H RobbyK,

whenever you load something in blitz it is worthwhile checking it the resource was loaded correctly - you may have specified an incorrect path to the file - which is what may have happened. I'll try to put an example together here:

graphics3d 800,600,0,2 ;sets the 3d graphics mode, and does so in windowed mode
Dim mySpriteArray(10) ;blitz begins counting array indices from 0 so in this case would be from 0 to 10)
mySprite = loadsprite("myimage.png")
if mySprite = 0 then runtimeerror("Something wrong with the file you've specified, check the path to the file most likely")
mySpriteArray(1) = CopyEntity(mySprite)
hideentity mySprite

showentity myspriteArray(1)

camera=createcamera()
positionentity mySpriteArray(1),0,0,8
setbuffer backbuffer()
repeat

updateworld ;not really needed here as no animation or collision checking being done
renderworld ;render the scene to the active cameras
flip	    ;flip the backbuffer to the frontbuffer

until keydown(1)

freeentity myspritearray(1)
freeentity mysprite
freeentity camera
end






RobbyK(Posted 2008) [#18]
thanks Matty

that got my sprite on the screen. Now refering back to one of your earlier posts that textures should be a power of two. My card faces are 84 wide by 145 tall. So does that mean I have to create a my card as an 128x256 image with a area of my graphics that has to be masked? Can I use the scale entity to make a square sprite look rectangular like a playing card. then at least I could make my texture image 128x256 and then scale the entity so it displays at 84x145 on the screen. Seem like a huge hassle to me

Seems like a lot of work! Sprite textures being a power of two are obviously sized for program execution speed but not for programming convienience. does this apply to all textures used in Blitz or just for sprites.


RobbyK(Posted 2008) [#19]
I used the line "ScaleEntity mySpriteArray(1),3,3,3" before the "positionentity mySpriteArray(1),0,0,8" line but it did not change the sprite size on the screen. In the help file in Blitz it says this will triple the size of the entitiy but it dit not change it at all. What gives?


Matty(Posted 2008) [#20]
Use scalesprite instead of scaleentity (an unusual exception to the blitz entity system)

Quick question - I remember you saying you were going to make a 2d game. Sprites in blitz are 3d entities (much as they are in darkbasic I think too) which are just flat planar meshes with a texture painted on. Because of limitations of graphics cards the textures are best to be power of 2. However if you stick with 'pure' 2d (that means no hardware accelerated rotations, scalings or blendings) you can use your images exactly as you have with those unusual dimensions of 80x140 or similar.

Using 2d images, not 3d hardware accelerated sprites do this:

graphics 800,600,0,2
image=loadimage("myimage.png")
setbuffer backbuffer()
repeat

cls
drawimage image,mousex(),mousey()
text 0,0,str(mousex()+","+mousey())
flip

until keydown(1)



RobbyK(Posted 2008) [#21]
Is there a way to use drawimage instead of sprites without destroying backrounds or other screen graphics. Is there a layer system so I can draw the image partially behind other graphics?
Do sprite have a priority level (i.e. which sprite/entity will show in front of another sprite/entity?
Sorry for all these questions. I can do all the sprite stuff easy in DB Pro and just wondered if it is possible in blitz.


RobbyK(Posted 2008) [#22]
Your sample above works fine. I got a similar program working last night. The problem with the drawimage command is if I want to drag an image of my card across the screen (like in a solitare game) without distroying the backround which the CLS command is doing in every repitition of the loop. Ideally if I could drag an image across the screen and press the left mouse button down and drop the image on the screen. If I rem out the CLS command thr card just leaves a trail as would be expected. This was the main reason for using Sprites in the first place.


Matty(Posted 2008) [#23]
Images drawn with drawimage will by default be transparent where the RGB pixel value is 0,0,0. This can be changed with the usage of "maskimage" which allows for different colors to be used as the masking color for a 2d image (although best not to use jpegs for this)

Images drawn with drawimage are simply drawn in the order you call the command, so you will have to perform sorting yourself.

Sprites are simply 3d entities so they will draw from back to front in order of their depth relative to the camera.

DrawImage is part of the 2d command set and is best used only with other 2d commands.

Sprites are part of the 3d command set, and are not able to be used in 2d mode (same goes for 3d objects which seems fairly obvious).


Matty(Posted 2008) [#24]
Sorry, you replied a bit quick for me:

Just draw your background image before you draw your cards.
There is nothing wrong with clearing the screen every frame, it has effectively no overhead performance wise.


RobbyK(Posted 2008) [#25]
Thanks Matty

Got to go to sleep now. I tried a few other things that didn't work out, but I will try to get a it tommorrow. I had tried (using your sprite example above) putting your line "positionentity mySpriteArray(1),0,0,8" in the repeat loop except I changed it to
"positionentity mySpriteArray(1),mousex(),mousey(),8" so I could move the sprite to follow the mouse. But of course that didn't work.
The sprite just disappeared. I am sure I will look back on this at a later date and say to myself. "What was I thinking" or i will just get frustrated even more and throw myself off a cliff.

Again, i appreciate all your help
Goodnight


Ross C(Posted 2008) [#26]
Procedure is usually as follows:

SETBUFFER BACKBUFFER()

BEGIN LOOP

CLS

DrawImage Background_image, 0,0

DrawImage Card,20,20

FLIP

REPEAT LOOP

Not real blitz code, but demonstrates the point.

You are constantly clearing the screen, drawing the background, then the cards, then flipping the backbuffer into view.

You will need to keep track of some aspects of an image though, so set up a type collection, or make array slots for the X,Y, animation length, current frame, frame time, frame timer... etc etc


Ross C(Posted 2008) [#27]
Oh and the reason you wouldn't be able to see you sprite, is because your treating it like a 2d object. In 2d the viewpoint is set and cannot be moved so to speak, and the co-ords don't change because of that. You place an image at 30,30 pixels, it will stay there, and will always be displayed at that location


In 3d, you can move the camera, the object, have objects facing away from the camera. You place an object at 30,30,0, then the object will be placed at 30,30,0 which by default, will be outside the cameras viewport. By taking mouse input raw, your sprite will most definatly be out of view for most co-ords.

Basically, i'd start in 2d with this one :o)

2d in blitz, offers no quick way of doing transparency, or rotation in real time, without precalculating it first. There is a farily decent real time scaling function, using the copyrect command however.


Beaker(Posted 2008) [#28]
You might find it easier to use some sprite library like SpriteCandy for example. I'm sure there are others. I'm sure this is all very confusing, but it will all be worth it.


Nate the Great(Posted 2008) [#29]
You can youse the sprite2d function. It has worked well for my purposes.

This code was included with B3d so I would assume you would have it somewhere on you computer.

Function Sprite2D(cam,sprite,x#,y#,size#,ang#,alpha#)
	ent=CameraPick(cam,x,y)
	If ent Then 
		PositionEntity sprite,PickedX(),PickedY(),PickedZ()
		ScaleSprite sprite,size/2560,size/2560
		RotateSprite sprite,ang
		EntityAlpha sprite,alpha
	End If
End Function


This function requires some setup before the main loop though.
You put the code below right under the Graphics3d command.

cam=CreateCamera()
CameraRange cam,.1,1000
PositionEntity cam,0,0,-10
pl=CreatePlane(1,cam)
PositionEntity pl,0,0,.15
RotateEntity pl,-90,0,0
EntityAlpha pl,0
EntityPickMode pl,2



Or you may want to stick with the normal 2d commands.


RobbyK(Posted 2008) [#30]
I guess another reason I get confused is sample code does not even work:
The code directly from blitz's help for "MouseZ ()" does not work on my machine

"Graphics 640, 480, 0, 2

SetBuffer BackBuffer ()

Repeat
Text 20, 20, "Mouse wheel position: " + MouseZ ()
Until KeyHit (1)

End "

Even if i use Mouse X () it will only give me a initial value if that.


Sledge(Posted 2008) [#31]
Graphics 640, 480, 0, 2
SetBuffer BackBuffer()
While Not KeyHit(1)
	Text 20, 20, "Mouse wheel position: " + MouseZ()
	Flip
	Cls
Wend

Try that instead*. I think track-pads can cause a problem depending on how their drivers are geared (my laptop's doesn't get on with a number of programs, B3D stuff included) but a 'proper' mouse should work fine.

*Actually you may need to update your docs (Account->Product updates) because the example I get is different to yours and works.


RobbyK(Posted 2008) [#32]
Sledge

Thanks that code works fine. I only bought Blitz 3D a few days ago I would think I would have the latest. No?

Just the Flip and CLS line missing from the sample code. Sloppy help file!
For a more experienced coder they would know you needed those two lines. Help is not very helpful to a rookie when the help files are not correct.


Sledge(Posted 2008) [#33]
Thanks that code works fine. I only bought Blitz 3D a few days ago I would think I would have the latest. No?

If you press the little "Home" button at the top of the IDE then click "Command Reference" you'll get a page that should tell you the documentation version -- 1.88 is the latest. It's fair to say, though, that docs ain't BRL's forte (fortunately, making really rather nice languages is) so don't hesitate to ask questions here.


RobbyK(Posted 2008) [#34]
Sledge

It is 1.62 I guess I have to update. Now I just have to figure out my login info since I bought Blitz when I was at work and have my Login info there.


RobbyK(Posted 2008) [#35]
If I don't use Sprites and just drawimage how do I paste down an image and have it stay on the screen. This clip from a help file which I have modified loads a backround image and then draws my card back at the mouse position. So how do I past and image down and have it show uo whithout it being overwritten with each cycle of the loop. I set it up so I Gosub my routine to draw a card back at 100,100 when i press the left mouse button. O course it disappers with the next execution of the loop.
I think I wil have to investige Nat the great's 2d sprite function which looks like he is just using 3d but he is creating a 2d enviroment within 3d space. A hack at best. Seems to me a simple implimentation of sprites that can be easily used in 2d as in DB PRO would be better.
It is obviously awhole different mintset using Blitz3d. maybe I should have purchased blitzmax instead. I beleive it is 2d only.


Sledge(Posted 2008) [#36]
3d-as-2d sprites, as in Dark BASIC and Blitz3D, are persistent objects in a 3d simulation so once you create them they're 'just there' until you destroy them. Images, on the other hand, are just pictures that you can either opt to draw or not each frame -- if you want them to be persistent then you'll have to devise some sort of object system to manage them with. And that brings us to the realm of custom types -- if you do anything, read up on types because they're ace and let you do something like this...



...which is just a fledgeling 2d sprite system cobbled together in a couple of minutes, but it should give you the general idea.

Edit: Renamed the custom type to TBob -- it's a bit shorter and will please the Amigans.
Edit: Added a few comments.


Ross C(Posted 2008) [#37]
As i said, you clear then redraw the entire screen every loop in 2d.

DrawImage 20,20,image


Can you paste your code here, and we can look through it?