Create an image from data statements

Advanced game related topics
Kevin
User
User
Posts: 12
Joined: Fri Aug 01, 2003 7:35 pm
Location: England

Create an image from data statements

Post by Kevin »

I was hoping to create an image from data statements. Nothing gets dispalyed though...

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 16, "Space") = 0
MessageRequester("Failed", "Unable to initialise", #PB_MessageRequester_Ok)
End
EndIf

RandomSeed(0)

Restore alien6

Read width
Read hight

result = CreateImage(#PB_Any, width, hight)

StartDrawing(ImageOutput(result))
DrawingMode(1)
For b = 0 To hight - 1
  For a = 0 To width - 1
    Read num
    CallDebugger
    Debug num
    If num = 1
      Box(a * 2, b * 2, 2, 2, RGB(160,160,160))
    EndIf
  Next
Next

DrawImage(result, 50, 100)

StopDrawing()

FlipBuffers(0)

Delay(2000)

DataSection
 alien6:
 Data.l 16,10
 Data.l 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,0,1,0,0,1,0,0,0,0,1,0,0,1,0,1
 Data.l 1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1
 Data.l 0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0
 Data.l 0,1,1,0,1,0,1,1,0,1,0,1,0,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0
EndDataSection
Any ideas?

Thanks,

Kevin.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 16, "Space") = 0
MessageRequester("Failed", "Unable to initialise", #PB_MessageRequester_Ok)
End
EndIf

RandomSeed(0)

Restore alien6

Read width
Read hight

result = CreateImage(#PB_Any, width, hight) ; for a game use CreateSprite() 

StartDrawing(ImageOutput(result))
DrawingMode(1)
For b = 0 To hight - 1
  For a = 0 To width - 1
    Read num
    If num = 1
      Box(a * 2, b * 2, 2, 2, RGB(160,160,160))
    EndIf
  Next
Next
StopDrawing()

Repeat
  ClearScreen(0)
  ExamineKeyboard()
  StartDrawing(ScreenOutput())
    DrawImage(ImageID(result), 50, 100)  
  StopDrawing()

  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape) 


DataSection
 alien6:
 Data.l 16,10
 Data.l 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,0,1,0,0,1,0,0,0,0,1,0,0,1,0,1
 Data.l 1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1
 Data.l 0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0
 Data.l 0,1,1,0,1,0,1,1,0,1,0,1,0,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0
EndDataSection
Please correct my english
http://purebasic.developpez.com/
Kevin
User
User
Posts: 12
Joined: Fri Aug 01, 2003 7:35 pm
Location: England

Post by Kevin »

Thanks Comtois,

I couldn't get anything displayed if I used CreateSprite().

The plan would be to create two sprites from the data statements for animation. I'm sure it will take a while before I get it right!

Cheers,

Kevin.
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Post by Comtois »

With sprite

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 16, "Space") = 0
MessageRequester("Failed", "Unable to initialise", #PB_MessageRequester_Ok)
End
EndIf

Define Alien.POINT
Alien\x = 50
Alien\y = 100

RandomSeed(0)

Restore alien6

Read width
Read hight

result = CreateSprite(#PB_Any, width, hight) 

StartDrawing(SpriteOutput(result))
DrawingMode(1)
For b = 0 To hight - 1
  For a = 0 To width - 1
    Read num
    If num = 1
      Box(a * 2, b * 2, 2, 2, RGB(160,160,160))
    EndIf
  Next
Next
StopDrawing()

Repeat
  ClearScreen(0)
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)
    Alien\y - 1
  ElseIf KeyboardPushed(#PB_Key_Down)
    Alien\y + 1
  EndIf
  If KeyboardPushed(#PB_Key_Left)
    Alien\x - 1
  ElseIf KeyboardPushed(#PB_Key_Right)
    Alien\x + 1
  EndIf  
  
  DisplaySprite(Result, Alien\x, Alien\y)
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)


DataSection
 alien6:
 Data.l 16,10
 Data.l 0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,0,1,0,0,1,0,0,0,0,1,0,0,1,0,1
 Data.l 1,0,0,1,1,0,0,0,0,0,0,1,1,0,0,1
 Data.l 0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0
 Data.l 0,1,1,0,1,0,1,1,0,1,0,1,0,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,1,1,1,1,1,0,0,1,1,1,1,1,1,0
 Data.l 0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0
 Data.l 1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,0
EndDataSection
Please correct my english
http://purebasic.developpez.com/
Kevin
User
User
Posts: 12
Joined: Fri Aug 01, 2003 7:35 pm
Location: England

Post by Kevin »

Thanks again Comtois,

I've updated the code to load a second image and control it by the mouse.

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 16, "Space") = 0 Or InitMouse() = 0
MessageRequester("Failed", "Unable to initialise", #PB_MessageRequester_Ok)
End
EndIf

Define Alien.POINT
Alien\x = 50
Alien\y = 100

RandomSeed(0)

Restore alien6frame1

Read width1
Read hight1

spr1 = CreateSprite(#PB_Any, width1 * 2, hight1 * 2)

StartDrawing(SpriteOutput(spr1))
DrawingMode(1)
For b = 0 To hight1 - 1
  For a = 0 To width1 - 1
    Read num
    If num = 1
      Box(a * 2, b * 2, 2, 2, RGB(160,160,160))
    EndIf
  Next
Next
StopDrawing()

Restore alien6frame2

Read width2
Read hight2

spr2 = CreateSprite(#PB_Any, width2 * 2, hight2 * 2)

StartDrawing(SpriteOutput(spr2))
DrawingMode(1)
For b = 0 To hight2 - 1
  For a = 0 To width2 - 1
    Read num2
    If num2 = 1
      Box(a * 2, b * 2, 2, 2, RGB(160,160,160))
    EndIf
  Next
Next
StopDrawing()

Repeat
  ClearScreen(0)
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Up)
    Alien\y - 1
  ElseIf KeyboardPushed(#PB_Key_Down)
    Alien\y + 1
  EndIf
  If KeyboardPushed(#PB_Key_Left)
    Alien\x - 1
  ElseIf KeyboardPushed(#PB_Key_Right)
    Alien\x + 1
  EndIf
  
   
 
  DisplaySprite(spr1, Alien\x, Alien\y)
  
  result = ExamineMouse()
  If result <> 0
    DisplaySprite(spr2, MouseX(), MouseY())
  EndIf
  FlipBuffers()
Until KeyboardPushed(#PB_Key_Escape)


DataSection
 alien6frame1:
 Data.l 8,10
 Data.l 0,0,0,0,0,0,0,1
 Data.l 0,1,0,0,0,0,1,0
 Data.l 1,0,1,0,0,1,0,0
 Data.l 1,0,0,1,1,0,0,0
 Data.l 0,0,1,1,1,1,1,0
 Data.l 0,1,1,0,1,0,1,1
 Data.l 0,1,1,1,1,1,1,0
 Data.l 0,1,1,1,1,1,1,0
 Data.l 0,1,0,0,0,0,1,0
 Data.l 1,1,1,0,0,1,1,1
EndDataSection

DataSection
 alien6frame2:
 Data.l 8,10
 Data.l 1,0,0,0,0,0,0,0
 Data.l 0,1,0,0,0,0,1,0
 Data.l 0,0,1,0,0,1,0,1
 Data.l 0,0,0,1,1,0,0,1
 Data.l 0,1,1,1,1,1,0,0
 Data.l 0,1,0,1,0,1,1,0
 Data.l 0,1,1,1,1,1,1,0
 Data.l 0,1,1,1,1,1,1,0
 Data.l 0,1,0,0,0,0,1,0
 Data.l 1,1,0,0,0,1,1,0
EndDataSection
I'd now like to use the two images as 1. What's the best way to animate the two images?

Any advice against the code above, i.e. any pointers as to how to resolve it and I'll pick up from there - it's a good way to learn.

Thanks.
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

to animate your little aliens you might do this way (one of the thousands possibilities) :

Code: Select all

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 16, "Space") = 0 Or InitMouse() = 0 
  MessageRequester("Failed", "Unable to initialise", #PB_MessageRequester_Ok) 
  End 
EndIf 

Structure ALIEN
  id.l[2]
  x.l
  y.l
  i.l
  t.l
EndStructure

Procedure.l AlienCatch()
  
  Protected x.l, y.l, sprite.l, pixel.l, width.l, height.l, scale.l, color.l
  
  Read width
  Read height
  Read scale
  Read color
  
  sprite = CreateSprite(#PB_Any, width * scale, height * scale) 
  
  If sprite And StartDrawing(SpriteOutput(sprite)) 
    DrawingMode(1) 
    For y = 0 To height - 1 
      For x = 0 To width - 1 
        Read pixel
        If pixel
          Box(x * scale, y * scale, scale, scale, color) 
        EndIf 
      Next 
    Next 
    StopDrawing() 
  EndIf
  
  ProcedureReturn sprite
  
EndProcedure
Procedure.l AlienDisplay(*alien.ALIEN)
  
  *alien\t + 1
  
  If *alien\t > 25
    *alien\i = 1 - *alien\i
    *alien\t = 0
  EndIf
  DisplaySprite(*alien\id[*alien\i], *alien\x, *alien\y) 
  
EndProcedure

Define alien1.ALIEN
Define alien2.ALIEN

Restore alien6frame1 : spr1 = AlienCatch()
Restore alien6frame2 : spr2 = AlienCatch()

alien1\id[0] = spr1
alien1\id[1] = spr2
alien1\x = 50 
alien1\y = 100 

alien2\id[0] = spr1
alien2\id[1] = spr2

RandomSeed(0) 

Repeat 
  
  ClearScreen(#Black) 
  
  If ExamineKeyboard() 
    If KeyboardPushed(#PB_Key_Up) 
      alien1\y - 1 
    ElseIf KeyboardPushed(#PB_Key_Down) 
      alien1\y + 1 
    EndIf 
    If KeyboardPushed(#PB_Key_Left) 
      alien1\x - 1 
    ElseIf KeyboardPushed(#PB_Key_Right) 
      alien1\x + 1 
    EndIf 
    AlienDisplay(alien1)
  EndIf
  
  If ExamineMouse() 
    alien2\x = MouseX()
    alien2\y = MouseY()
    AlienDisplay(alien2)
  EndIf 
  
  FlipBuffers() 
  
Until KeyboardPushed(#PB_Key_Escape) 


DataSection 
 alien6frame1: 
 Data.l 8,10,2,#Gray
 Data.l 0,0,0,0,0,0,0,1 
 Data.l 0,1,0,0,0,0,1,0 
 Data.l 1,0,1,0,0,1,0,0 
 Data.l 1,0,0,1,1,0,0,0 
 Data.l 0,0,1,1,1,1,1,0 
 Data.l 0,1,1,0,1,0,1,1 
 Data.l 0,1,1,1,1,1,1,0 
 Data.l 0,1,1,1,1,1,1,0 
 Data.l 0,1,0,0,0,0,1,0 
 Data.l 1,1,1,0,0,1,1,1 
EndDataSection 

DataSection 
 alien6frame2: 
 Data.l 8,10,3,#White
 Data.l 1,0,0,0,0,0,0,0 
 Data.l 0,1,0,0,0,0,1,0 
 Data.l 0,0,1,0,0,1,0,1 
 Data.l 0,0,0,1,1,0,0,1 
 Data.l 0,1,1,1,1,1,0,0 
 Data.l 0,1,0,1,0,1,1,0 
 Data.l 0,1,1,1,1,1,1,0 
 Data.l 0,1,1,1,1,1,1,0 
 Data.l 0,1,0,0,0,0,1,0 
 Data.l 1,1,0,0,0,1,1,0 
EndDataSection
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Kevin
User
User
Posts: 12
Joined: Fri Aug 01, 2003 7:35 pm
Location: England

Post by Kevin »

Excellent thanks Flype,

I was wondering if it was worth looking to load the data sections to 1 image/sprite and animate the alien, or would that just add complexity to the program?

Something like an image strip that was created from the data statements and split into frames.

Cheers
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

use data sections to include the images, way faster and pretty easy
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply