Making a red square drawing using only DataSection ? Possible ?

Everything else that doesn't fall into one of the other PB categories.
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Making a red square drawing using only DataSection ? Possible ?

Post by threedslider »

How to make for PureBasic Red Square Drawing Using Only DataSection without to use with plot ?

In 64 x 64 if possible ? Thanks.
User avatar
mk-soft
Always Here
Always Here
Posts: 6245
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Making a red square drawing using only DataSection ? Possible ?

Post by mk-soft »

Create a Red-Square.bmp

Code: Select all

DataSection
  MyImage:
  IncludeBinary "[your-path]\Red-Square.bmp"
  MyImageEnd:
EndDataSection

CatchImage(0, ?MyImage, ?MyImageEnd - ?MyImage)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: Making a red square drawing using only DataSection ? Possible ?

Post by threedslider »

I would not want that but thanks by ChatGPT, see the code here :

Code: Select all

InitSprite()
InitKeyboard()
InitMouse()

WindowID = OpenWindow(0, 100, 100, 800, 600, "64x64 Red Square", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)

#SquareWidth = 64
#SquareHeight = 64
#PixelCount = #SquareWidth * #SquareHeight

*redSquare = AllocateMemory(#PixelCount * 4) ; 4 bytes per pixel

If *redSquare
  ; Fill memory with red pixel values (BGRA: $0000FFFF)
  For i = 0 To #PixelCount - 1
    PokeL(*redSquare + i * 4, $FFFF0000)
  Next

  ; Create a 64x64 image from raw memory
  If CreateImage(0, #SquareWidth, #SquareHeight, 32)
    StartDrawing(ImageOutput(0))
    CopyMemory(*redSquare, DrawingBuffer(), #PixelCount * 4)
    StopDrawing()
  EndIf
EndIf

; Main loop
Repeat
  Event = WindowEvent()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf

  If StartDrawing(ScreenOutput())
    Box(0, 0, 800, 600, RGB(0, 0, 0)) ; Clear screen
    DrawImage(ImageID(0), 100, 100)   ; Draw red square at (100, 100)
    StopDrawing()
    FlipBuffers()
  EndIf
ForEver

FreeMemory(*redSquare)
End
But I prefer to render red square in dataSection :

Code: Select all

InitSprite()
InitKeyboard()
InitMouse()

WindowID = OpenWindow(0, 100, 100, 800, 600, "10x10 Red Square (DataSection)", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)

#SquareWidth = 10
#SquareHeight = 10
#PixelCount = #SquareWidth * #SquareHeight

*redSquare = AllocateMemory(#PixelCount * 4)

If *redSquare
  Restore RedSquareData
  For i = 0 To #PixelCount - 1
    ;ReadLong color
    PokeL(*redSquare + i * 4, color)
  Next

  If CreateImage(0, #SquareWidth, #SquareHeight, 32)
    StartDrawing(ImageOutput(0))
    CopyMemory(*redSquare, DrawingBuffer(), #PixelCount * 4 )
    StopDrawing()
  EndIf
EndIf

Repeat
  Event = WindowEvent()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf

  If StartDrawing(ScreenOutput())
    Box(0, 0, 800, 600, RGB(0, 0, 0))
    DrawImage(ImageID(0), 100, 100)
    StopDrawing()
    FlipBuffers()
  EndIf
ForEver

FreeMemory(*redSquare)
End

DataSection
  RedSquareData:
    
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
    
EndDataSection
So that it doesn't work I don't see the red and I don't know to make this one :? . This last method that I would want to see more that :shock:

Can you help me ?
Fred
Administrator
Administrator
Posts: 18220
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Making a red square drawing using only DataSection ? Possible ?

Post by Fred »

You can't use a raw memory copy, you need to use drawing pitch for each line to be safe
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: Making a red square drawing using only DataSection ? Possible ?

Post by Mijikai »

Example:

Code: Select all

EnableExplicit

Procedure.i main()
  Protected.i exit,x,y,px,py,sx,sy,ex,ey
  Protected.long *s,*d,*w,*h
  If InitSprite() And InitKeyboard()
    If OpenWindow(0,0,0,800,600,"Red Square :D",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
      If OpenWindowedScreen(WindowID(0),0,0,WindowWidth(0),WindowHeight(0))
        SetFrameRate(60)
        x = WindowWidth(0) >> 1
        y = WindowHeight(0) >> 1
        Repeat
          Repeat
            Select WindowEvent()
              Case #PB_Event_None
                Break
              Case #PB_Event_CloseWindow
                exit = #True
            EndSelect
          ForEver
          ExamineKeyboard()
          If KeyboardPushed(#PB_Key_Left)
            x - 2  
          EndIf
          If KeyboardPushed(#PB_Key_Right)
            x + 2  
          EndIf
          If KeyboardPushed(#PB_Key_Up)
            y - 2
          EndIf
          If KeyboardPushed(#PB_Key_Down)
            y + 2
          EndIf
          ClearScreen(0)
          StartDrawing(ScreenOutput())
          DrawingMode(#PB_2DDrawing_AllChannels)
          *w = ?red_square
          *h = ?red_square + 4
          *s = ?red_square + 8
          *d = DrawingBuffer()
          sx = x - *w\l >> 1
          sy = y - *h\l >> 1
          ex = sx + *w\l - 1
          ey = sy + *h\l - 1
          For py = sy To ey
            For px = sx To ex
              If px >= 0 And px < WindowWidth(0) And py >= 0 And py < WindowHeight(0)
                *d = DrawingBuffer() + (py * DrawingBufferPitch()) + ( px * OutputDepth() >> 3)
                *d\l = *s\l
              EndIf
              *s + 4
            Next
          Next
          StopDrawing()
          FlipBuffers()
        Until exit = #True Or KeyboardReleased(#PB_Key_Escape)
        CloseScreen()
      EndIf
      CloseWindow(0)  
    EndIf
  EndIf
  ProcedureReturn #Null
EndProcedure

End main()

DataSection
  red_square:
  Data.l 10,8
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
  Data.l $FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000,$FFFF0000
EndDataSection
User_Russian
Addict
Addict
Posts: 1535
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Making a red square drawing using only DataSection ? Possible ?

Post by User_Russian »

threedslider wrote: Fri Jun 13, 2025 6:52 pmSo that it doesn't work I don't see the red and I don't know to make this one :? .
But the variable color contains the number 0 (black color), and not data from DataSection.
It must be.

Code: Select all

InitSprite()
InitKeyboard()
InitMouse()

WindowID = OpenWindow(0, 100, 100, 800, 600, "10x10 Red Square (DataSection)", #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600, 0, 0, 0)

#SquareWidth = 10
#SquareHeight = 10
#PixelCount = #SquareWidth * #SquareHeight

*redSquare = AllocateMemory(#PixelCount * 4)

If *redSquare
  Restore RedSquareData
  For i = 0 To #PixelCount - 1
    Read.l color
    PokeL(*redSquare + i * 4, color)
  Next

  If CreateImage(0, #SquareWidth, #SquareHeight, 32)
    StartDrawing(ImageOutput(0))
    CopyMemory(*redSquare, DrawingBuffer(), #PixelCount * 4 )
    StopDrawing()
  EndIf
EndIf

Repeat
  Event = WindowEvent()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    Break
  EndIf

  If StartDrawing(ScreenOutput())
    Box(0, 0, 800, 600, RGB(0, 0, 0))
    DrawImage(ImageID(0), 100, 100)
    StopDrawing()
    FlipBuffers()
  EndIf
ForEver

FreeMemory(*redSquare)
End

DataSection
  RedSquareData:
    
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
  Data.l $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000, $FFF0000
    
EndDataSection
threedslider
Enthusiast
Enthusiast
Posts: 397
Joined: Sat Feb 12, 2022 7:15 pm

Re: Making a red square drawing using only DataSection ? Possible ?

Post by threedslider »

@Fred : OK thanks for this info :shock:

@Mijikai : Nice your code I like it that move :D 8)

@User_Russian : Ooooh ! Got it thanks a lot for that fixing this issue :shock:
Post Reply