pseudo 3D world

New to FreeBASIC? Post your questions here.
Post Reply
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

pseudo 3D world

Post by BasicCoder2 »

Thought it might be of interest to a beginner?

Code: Select all

'       |
'       |
'       |
'       +-------> x
'      /
'     /
'    / y
'

Dim Shared As Integer floor(20,20)
Dim Shared As Integer counter

Dim Shared As Any Ptr blockImage
Dim As Uinteger c

'code rgb colors of sprite

dim as uinteger sColor(5)
sColor(0)=&HFFFF00FF 'pink transparent color
sColor(1)=&HFF010101 'black
sColor(2)=&HFF00FE00 'green
sColor(3)=&HFF0000FE 'blue
sColor(4)=&HFFFE0000 'red

screenres 640,480,32,2 'two pages
screenset 1,0  'select page

blockImage = ImageCreate( 32, 32 ) ' allocate memory for 32x32 sprite
'read block image
For y As Integer = 0 To 31
    For x As Integer = 0 To 31
        Read c
        Pset blockImage,(x,y), sColor(c)
    Next x
Next y

'read floor data
For y As Integer = 0 To 19
    For x As Integer = 0 To 19
        Read floor(x,y)
    Next x
Next y

'dim as integer x,y,z

Dim Shared As Integer cx,cy

cy = 200
cx = 200

Sub Set3DPixel(x As Integer,y As Integer,z As Integer,c As Uinteger)
    Pset(x-y+cx,cy+y-z),c
End Sub

Sub Paint3D(x As Integer, y As Integer, z As Integer, c1 As Uinteger, c2 As Uinteger)
    Paint(x-y+cx,cy+y-z),c1,c2
End Sub

Sub line3D(x1 As Integer,y1 As Integer, z1 As Integer, x2 As Integer,y2 As Integer, z2 As Integer, c As Uinteger)
    Line (x1-y1+cx,cy+y1-z1)-(x2-y2+cx,cy+y2-z2),c
End Sub

Sub putBlock(x As Integer,y As Integer,z As Integer)
    Put (x*20-y*10+cx,cy+y*10-z*20),blockImage,trans
End Sub


Sub displayWorld()
    Dim As Integer x,y,z,xx,yy
    Cls
    'draw x coordinate
    z = 0:y=0
    For x = 0 To 440
        set3DPixel(x,y,z,rgb(255,255,255))
    Next x
    x=0:y=0
    For z = 0 To 400
       set3DPixel(x,y,z,rgb(255,255,255))
    Next z
    'draw y coordinate
    z = 0:x=0
    For y = 0 To 400
        set3DPixel(x,y,z,rgb(255,255,255))
    Next y
    'paint walls and floor
    Paint (440,20),rgb(100,100,100),rgb(255,255,255)
    Paint (20,20),rgb(50,50,50),rgb(255,255,255)
    Paint (638,440),rgb(127,127,0),rgb(255,255,255)


    z = 0
    For y = 0 To 19
        For x = 0 To 19
            If floor(x,y)=1 Then
                putBlock(x,y,z)
            End If
        Next x
    Next y
    'scan floor for blocks and move them
    For y = 1 To 18
        For x = 1 To 18
            If floor(x,y)=1 Then
                xx = Int(Rnd(1)*3)-1
                yy = Int(Rnd(1)*3)-1
                If floor(x+xx,y+yy)=0 Then
                    floor(x,y)=0 'erase old position
                    floor(x+xx,y+yy)=1 'insert in new position
                End If
            End If
        Next x
    Next y
    Locate 1,1
    Print counter
    counter = counter + 1
    screencopy
End Sub

' MAIN PROGRAM
Dim As Double now

now = Timer
Do
    If TIMER>(now+0.03) Then
        displayWorld()
        now = Timer
    End If
Loop Until multikey(&H01)

End


'block DATA
Data 0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0
Data 0,0,0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,0
Data 0,0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,3,1,0
Data 0,0,0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,1,0
Data 0,0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,3,1,0
Data 0,0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,3,3,1,0
Data 0,0,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,3,3,3,3,3,1,0
Data 0,0,0,1,2,2,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 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0
Data 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0
Data 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0
Data 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
Data 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
Last edited by BasicCoder2 on Jul 26, 2010 14:18, edited 2 times in total.
rdc
Posts: 1741
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

Nice one.
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

Hi,

Can anyone explain why i have to put,

screenres 640,480,32,2 'two pages
screenset 1,0

before,

blockImage = ImageCreate( 32, 32 ) ' allocate memory for 32x32 sprite

If I put it after this line the background is drawn but not the blocks?

JohnC

Code: Select all

'see code in first post
Last edited by BasicCoder2 on Jul 27, 2010 1:01, edited 1 time in total.
angros47
Posts: 2329
Joined: Jun 21, 2005 19:04

Post by angros47 »

It does not compile with asm emitter: it seems that assembler have troubles with CX variable (since it's also the name of a register).
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

Pretty cool, although I had the same problem with the CX variable name at first. It worked fine when I changed the name of the cx variables to something else.
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Post by Merick »

How the image is stored in memory changes when using different screen modes - 8 bit uses palletized colors (i.e. the number stored for each pixel is a reference to a color in a pallet), 16 bit mode stores the color as RGB, and 32 bit mode stores it as RGBA. Because each method requires a different amount of memory, you have to set the screen first so that imagecreate can set up the proper structure for the screen mode that you are using.
angros47
Posts: 2329
Joined: Jun 21, 2005 19:04

Post by angros47 »

pestery wrote:Pretty cool, although I had the same problem with the CX variable name at first. It worked fine when I changed the name of the cx variables to something else.



Windows or linux?
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

Merick wrote:How the image is stored in memory changes when using different screen modes - 8 bit uses palletized colors (i.e. the number stored for each pixel is a reference to a color in a pallet), 16 bit mode stores the color as RGB, and 32 bit mode stores it as RGBA. Because each method requires a different amount of memory, you have to set the screen first so that imagecreate can set up the proper structure for the screen mode that you are using.
Yes thanks that must be it.

I tried ImageCreate( 32, 32, ,32 ) to force 32 bit without success.

JohnC
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

angros47

I had the problem on linux (fbc-0.20), though on windows (both fbc-0.20 and fbc-0.21) it worked fine without alteration.

By the way, I've never used the quote thing before and couldn't figure it out :P
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Hi BasicCoder2, I put spaces in your Data lines so it doesn't break the forum width.
(The quotes around the "&h..." values aren't necessary by the way, in case you're looking to save on line length.)
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

counting_pine wrote:Hi BasicCoder2, I put spaces in your Data lines so it doesn't break the forum width.
(The quotes around the "&h..." values aren't necessary by the way, in case you're looking to save on line length.)
Ok thanks. I didn't know there was a width limit.

I used the Search, replace to remove the quotes in the data and yes it works fine.

The data statements were generated from an actual image by another program.

JohnC
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Yeah, the main problem with the forum is that it expands to the width of the code, which makes the page a bit harder to use. E.g. you have to scroll horizontally to read long lines of text.
It doesn't happen often because the code boxes do have line wrapping, but it can (apparently) only wrap at spaces and not punctuation.

What program did you use to generate the data?
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

counting_pine wrote:Yeah, the main problem with the forum is that it expands to the width of the code, which makes the page a bit harder to use. E.g. you have to scroll horizontally to read long lines of text.
It doesn't happen often because the code boxes do have line wrapping, but it can (apparently) only wrap at spaces and not punctuation.

What program did you use to generate the data?
I just wrote a few lines of code to load the image and read it using POINT and saving it to a text file with the "DATA" string and then paste and copy back to FIDE.

I have changed the code again to reduce the size of the data by coding the five colors,

dim as uinteger sColor(5)
sColor(0)=&HFFFF00FF 'pink transparent color
sColor(1)=&HFF010101 'black
sColor(2)=&HFF00FE00 'green
sColor(3)=&HFF0000FE 'blue
sColor(4)=&HFFFE0000 'red

'read block image
For y As Integer = 0 To 31
For x As Integer = 0 To 31
Read c
Pset blockImage,(x,y), sColor(c)
Next x
Next y

The data statements are a nice size now and you can actually see the block.

JohnC
kiyotewolf
Posts: 1009
Joined: Oct 11, 2008 7:42
Location: ABQ, NM
Contact:

Post by kiyotewolf »

HAHAHAA..

Those boxes look either ..

One. High on jolt cola
Two. Scared out of their wits
3ree. Like popcorn.

I'm an intermediate programmer, and I think this is very cool.

(Lean more towards beginner, and you'll find me there.)



~Kiyote!
BasicCoder2
Posts: 3917
Joined: Jan 01, 2009 7:03
Location: Australia

Post by BasicCoder2 »

kiyotewolf wrote:HAHAHAA..

Those boxes look either ..

One. High on jolt cola
Two. Scared out of their wits
3ree. Like popcorn.

I'm an intermediate programmer, and I think this is very cool.

(Lean more towards beginner, and you'll find me there.)

~Kiyote!
This very old technique is used by many games such as Age of Empire, football games (soccer) and tennis except instead of blocks they have houses, trees and animated characters. The use of shadow can enhance the 3D appearance.

JohnC
Post Reply