There seems to be a few people asking for a Plot(x,y) function or a Dot(x,y) function to plot a pixel on the screen. I would assume that people want this to continually edit a backbuffer without deleting it?? im not sure really?
I'm sure the developers could add this functionality in the future which would be fine.
There are a few ways of doing this in AppGameKit already though.....each way has its merits.
One way is keeping a memblock and updating an image each frame like in the code below. It should be simple enough to understand.
// Project: Plotting
// Created: 2018-01-25
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "Example of Plotting in AGK" )
SetWindowSize( 800, 600, 0 )
SetWindowAllowResize( 0 ) // dont allow the user to resize the window
// set display properties
//etVirtualResolution( 800, 600 )
SetVirtualResolution( 400, 300 ) // TRY THIS
//SetVirtualResolution( 200, 150 ) // OR TRY THIS ONE
//SetVirtualResolution( 100, 75 ) // OR SERIOUSLY - TRY THIS
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// create a virtual backbuffer to draw on
Im = CreateImageMemblock(1, GetVirtualWidth(), GetVirtualHeight())
// create a sprite that uses the image memblock created above
CreateSprite(1,1)
// these are the starting points for the dot drawing (middle of the screen)
x = GetVirtualWidth()/2
y = GetVirtualHeight()/2
do
// loop through moving the drawing point 8 times per frame (make sit quick)
for i = 0 to 16
// update the data in the memblock
if x< GetPointerX() then inc x
if x> GetPointerX() then dec x
if y< GetPointerY() then inc y
if y> GetPointerY() then dec y
// Add a dot
Dot(x,y)
next i
// Create a new colour
SetDotColour(Random(20,255),Random(20,255),Random(20,255))
// update the image from the memblock
CreateImageFromMemblock(1,Im)
// If mouse is pressed - Clear the "backbuffer" (the memblock)
if GetPointerPressed() then ClearImageMemblock()
// print some stuff
Print( "Draw some pretty lines" )
Print( "Press mouse to clear screen" )
Print( "FPS: " + str(ScreenFPS()) )
Sync()
loop
// ALL THE REAL WORK IS DONE BELOW
// Variables to store the memblock and current colour
global Im
global Rv
global Bv
global Gv
// This sets the current plot colour
function SetDotColour(Red as integer, Green as integer, Blue as integer)
Rv = Red
Bv = Blue
Gv = Green
endfunction
// this plots a single pixel
function Dot(x as integer,y as integer)
offset = 12 + (4*(x + (y*GetMemblockInt(Im,0))))
SetMemblockbyte(Im,offset,Rv) // red
SetMemblockbyte(Im,offset+1,Bv) // green
SetMemblockbyte(Im,offset+2,Gv) // blue
SetMemblockbyte(Im,offset+3,255) // alpha
endfunction
// this clears the virtual backbuffer
function ClearImageMemblock()
offset = 12
for ix = 0 to GetMemblockInt(Im,0)-1
for iy = 0 to GetMemblockInt(Im,4)-1
SetMemblockInt(Im,offset,0) // clears
offset = offset + 4
next iy
next ix
// Theres a quicker way of doing this but I left it like this to show how it works
endfunction
function CreateImageMemblock(ID as integer, width as integer, height as integer)
Im = CreateMemblock(12 + (4 * width * height))
// Set the width and heights etc..
SetMemblockInt(Im,0,width)
SetMemblockInt(Im,4,height)
SetMemblockInt(Im,8,32)
// Create an image from the memblock
CreateImageFromMemblock(ID,Im)
endfunction Im
// this gets a single pixel RED,GREEN,BLUE,ALPHA as one integer
function GetPixel(x as integer,y as integer)
offset = 12 + (4*(x + (y*GetMemblockInt(Im,0))))
col = GetMemblockInt(Im,offset)
endfunction col
This is a simple but fun drawing/Tron program. It looks nice too if you lower the virtual resolution so you get a nice pixel blur effect
Its not mega fast but it works.
You can also just nor clear the actual backbuffer but it makes printing and other things hard.
Another way of doing plotting on and continually editing on a render target. You can then draw single pixels (Plot()/Dot()) on it. You can also draw sprites DrawSprite() and DrawLine() and DrawBox() on it too if you wanted to. I can do an example of that tomorrow if anyone is actually interested?? I guess people might just want the simple built in functions?? I just didnt want people to think that its not possible to do simple plotting type operations in AGK.
Im sure alot of people already knew how to do it... so sorry for wasting your time.