0 Members and 1 Guest are viewing this topic.
' 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 FALSEEnd 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 EndIfEnd 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._bufferEnd Destructor'Screen object status property.'Returns the current status of object.Property screenobject.GetStatus () As sbool Return this._initokEnd Property'Screen object width property.'Returns the screen width.Property screenobject.GetWidth () As Integer Return this._widthEnd Property'Screen object height property.'Returns the screen height.Property screenobject.GetHeight () As Integer Return this._heightEnd 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 IfEnd 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 IfEnd 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 IfEnd 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 EndEndIf'Draw a 100 screensFor 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.CopyBufferNext'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