Dark Bit Factory & Gravity

PROGRAMMING => Coding tutorials => Topic started by: rdc on July 01, 2009

Title: FB Graphics #1: Basic Screen Object Code
Post by: rdc on July 01, 2009
This is the first of a series of example programs and tutorials covering the graphics engine in FreeBasic. The series will only use the built-in graphics commands and standard libraries. I am taking an object-oriented approach to the series where we will build a basic screen object to handle the screen work and will add to the object over time to increase the functionality. The goal will be to create a reusable, simple and fast 32 bit screen object. Here is the basic object and test code. I'll follow up the code with a detailed explanation of the code.

Code: [Select]
' FB Graphics Series for DBFInteractive
' Richard D. Clark
' 01: Basic Graphics Framework
' GPL 3.0
' This program is free software; you can redistribute it and/or modify it
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' =======================================================================================================
' This is the first in a series of example programs in FB that illustrates the graphics engine in FB.
' This program builds a preliminary screen object that will be used as a base for the graphics series.
' The series will use only the built-in graphics and the standard FB libraries.
' =======================================================================================================
'Include the FB graphics library.
#Include "fbgfx.bi"
'Include the CRT for memory functions.
#Include "crt.bi"

'Set the default namespace.
Using FB

'Boolean enumeration.
Enum sbool
FALSE = 0
TRUE = Not FALSE
End Enum

'Create the screen object definition.
Type screenobject
Private:
_width As Integer       'The screen width.
_height As Integer      'The screen height.
_buffer As UInteger Ptr 'The screen update buffer.
_initok As sbool        'Flag indicates screen set successfully. True, set ok, False error.
Public:
Declare Constructor (ByVal scrwidth As Integer, ByVal scrheight As Integer) 'Sets up a graphics screen.
Declare Destructor  'Cleans up created objects.
Declare Property GetStatus () As sbool 'Returns command status.
Declare Property GetWidth () As Integer  'Returns the screen width.
Declare Property GetHeight () As Integer 'Returns the screen height.
Declare Sub DrawToBuffer (ByRef x As Integer, ByRef y As Integer, ByRef clr As UInteger) 'Sets the x and y location of buffer to color.
Declare Sub CopyBuffer ()'Copies buffer to screen.
Declare Sub ClearBuffer ()'Clears buffer. Fills to 0.
End Type

'Screen object constructor.
'This sets up both the screen and the associated screen buffer.
Constructor screenobject (ByVal scrwidth As Integer, ByVal scrheight As Integer)
'Set the default status. An error will change to False.
this._initok = TRUE
'Check the parameters to make sure that they are in bounds.
If (scrwidth > 0) And (scrheight > 0) Then
'Try and set the screen.
ScreenRes scrwidth, scrheight, 32
'Check to see if the screen was created successfully.
If ScreenPtr Then
'Set the screen width and height.
this._width = scrwidth
this._height = scrheight
'Create the update buffer.
this._buffer = Callocate (scrwidth * scrheight, SizeOf(UInteger))
'Nake sure that the buffer was created.
If this._buffer = 0 Then
'Set status to False, error.
this._initok = FALSE
EndIf
Else
'Set status to False, error.
this._initok = FALSE
EndIf
Else
'Set status to False, error.
this._initok = FALSE
EndIf
End Constructor

'Screen object destructor.
'This will destroy any created objects like the update buffer.
Destructor screenobject
'Destroy the update buffer if initialized.
If this._buffer Then DeAllocate this._buffer
End Destructor

'Screen object status property.
'Returns the current status of object.
Property screenobject.GetStatus () As sbool
Return this._initok
End Property

'Screen object width property.
'Returns the screen width.
Property screenobject.GetWidth () As Integer
Return this._width
End Property

'Screen object height property.
'Returns the screen height.
Property screenobject.GetHeight () As Integer
Return this._height
End Property

'Screen object DrawToBuffer sub.
'Copies color to x, y position in buffer. Provides clipping.
Sub screenobject.DrawToBuffer (ByRef x As Integer, ByRef y As Integer, ByRef clr As UInteger)
'Make sure the object was initialized.
If this._initok = TRUE Then
If (x >= 0) And (y >= 0) And (x < this._width) And (y < this._height) Then
this._buffer[y * this._width + x] = clr
EndIf
End If
End Sub

'Screen object CopyBuffer sub.
'Copies the buffer to the screen.
Sub screenobject.CopyBuffer ()

'Make sue the object was initialized.
If this._initok = TRUE Then
'Need to lock the screen before we copy the buffer.
ScreenLock
'Using the CRT memcpy to fast copy the buffer to the screen memory.
memcpy ScreenPtr, this._buffer, this._width * this._height * SizeOf(UInteger)
ScreenUnLock
End If
End Sub

'Screen object ClearBuffer sub.
'Clears buffer by setting buffer to 0.
Sub screenobject.ClearBuffer ()

'Make sure the object was initialized.
If this._initok = TRUE Then
'Using the CRT memset to set buffer to 0.
memset this._buffer, 0, this._width * this._height * SizeOf(UInteger)
End If
End Sub

' =======================================================================================================
' Main Program Code
' =======================================================================================================
'Init the random number generator.
Randomize Timer
'Create the screen object which will initialize and create the graphics screen.
Dim As screenobject aScreen = screenobject(640, 480)

'Make sure the screen intilizled properly.
If aScreen.GetStatus = FALSE Then
End
EndIf

'Draw a 100 screens
For i As Integer = 1 To 100
'Draw some random points to the screen.
For x As Integer = 0 To aScreen.GetWidth - 1
For y As Integer = 0 To aScreen.GetHeight - 1
aScreen.DrawToBuffer x, y, RGB( Rnd * 255, Rnd * 255, Rnd * 255)
Next
Next
'Copy the buffer to the screen.
ascreen.CopyBuffer
Next

'Clear the buffer.
ascreen.ClearBuffer
'Copy the buffer to the screen, should be black.
ascreen.CopyBuffer

'Wait for the user to end.
Print "Press any key"
Sleep
Title: Re: FB Graphics #1
Post by: Jim on July 01, 2009
Sounds like a plan!
Title: Re: FB Graphics #1
Post by: Shockwave on July 01, 2009
Nice one mate, incidentally any tutorials will be linked on the new front page directly into the forum topic.
Title: Re: FB Graphics #1
Post by: rdc on July 01, 2009
Thanks guys. I have posted links to the forum on the FB and Basic programming forums. Maybe it will generate some traffic.

I'll have the tut up later today.
Title: Re: FB Graphics #1
Post by: benny! on July 01, 2009
Good one!!
Title: Re: FB Graphics #1
Post by: Shockwave on July 01, 2009
Thanks mate :)