Author Topic: Unseen GDK - Dev Thread  (Read 282 times)

Offline Unseen Machine

  • QB64 Elite
Unseen GDK - Dev Thread
« on: June 18, 2018, 07:35:17 PM »
Hi Gang

I aint got much time right now but heres the latest version of GDK. Save it as UnseenGDK.bm in your QB64 folder.

Im hoping that you lot will help me in updating it to use QB64's newer commands.

Thanks

Unseen
« Last Edit: June 18, 2018, 07:38:49 PM by Unseen Machine »

Offline Ashish

  • The joy of coding is endless.
Re: Unseen GDK - Dev Thread
« Reply #1 on: June 19, 2018, 05:06:43 AM »
Hi UnseenMachine!
Haven't seen you for a long. Some samples demos will be good to understand your GDK.
Also, you can use QB64's inbuilt _D2R() and _R2D() to convert degrees to radians and vice versa, instead of having
your own GDK_RadianToDegree!() and GDK_DegreeTo Radian!().
if (Me.success) {Me.improve()} else {Me.tryAgain()}


aKFrameWork - http://bit.ly/aKFrameWork
Menu System - http://bit.ly/guiMenuBar
p5.js in QB64 - http://bit.ly/p5jsbas

@KingOfCoders

Offline bplus

  • B = B + geberation
Re: Unseen GDK - Dev Thread
« Reply #2 on: June 19, 2018, 10:40:46 AM »
Yes you could add _PI() to list too but that is a very rich set of sprite procedures.
B = B + ...

Offline Unseen Machine

  • QB64 Elite
Re: Unseen GDK - Dev Thread
« Reply #3 on: June 21, 2018, 10:08:02 PM »
Ashish - when i get a chance ill run a speed check and if the inbuilt commands are faster then ill happily update! Thanks for the suggestion and as for demos, well thats a good point... Ill build a basic game in stages to show how commands are used

Code: [Select]
'// UnseenGDK Tutorials - No 1 : Generic Base Code
'// For version 1.5 and up
'// By John Onyon a.k.a Unseen Machine


'// Create a screen for the program
GDK_Screen_SetRes MainScreen&, 1024, 768

'// Define arrays for input handling
DIM KB(1) AS KeyBoardState, Mouse(1) AS MouseState


'// Main code
DO
  _LIMIT 30
  CLS

  '// GET INPUT
  GDK_Keyboard_GetState KB(0)
  GDK_Mouse_GetState Mouse(0)


  '// LOGIC


  '// DRAWING

  _DISPLAY


  '// Store input state's for comparisons next loop
  KB(1) = KB(0)
  Mouse(1) = Mouse(0)

LOOP
'// END of Main code


'// Library inclusions
REM $INCLUDE:'UnseenGDK.bm'


Thats base code for almost every GDK program...GDK_GL is a bit more indepth!!!

@bplus - _PI! Well i've had to update VQB to use that over my pre built function so GDK should also follow suit! Nice suggestion, will be implemented and thanks for the kudos!

Unseen

Offline bplus

  • B = B + geberation
Re: Unseen GDK - Dev Thread
« Reply #4 on: June 21, 2018, 11:28:10 PM »
Regarding r2d and d2r conversions, I will bet cash money that constants are the fastest ie myDegreeAngle = radian_angle * const180slashPI, you skip function calls altogether and Constants are maybe as fast as Integer Variables for math.
« Last Edit: June 21, 2018, 11:30:40 PM by bplus »
B = B + ...

Offline bplus

  • B = B + geberation
Re: Unseen GDK - Dev Thread
« Reply #5 on: June 22, 2018, 12:06:13 AM »
Code: [Select]
_TITLE "const versus func r2d time.bas"
'QB64 v1.1 1106 started by bplus 2018-06-21
x## = (180 / _ACOS(-1))
PRINT x##
CONST r2d = 57.29577951308232
PRINT r2d - 180 / _ACOS(-1) 'not bad?
PRINT 180 / _PI - r2d 'compare to built-in
PRINT 180 / _PI - x## '10x worse!
PRINT
FOR i = 1 TO 1 'test  model , looks like exactly same results
    FOR a = 0 TO _PI(2) STEP _PI(1 / 6)
        da## = a * r2d
        PRINT da##, _R2D(a)
    NEXT
NEXT

PRINT: PRINT
start! = TIMER
FOR i = 1 TO 10000000
    FOR a = 0 TO _PI(2) STEP _PI(1 / 6)
        da## = _R2D(a)
        'PRINT da##, _R2D(a)
    NEXT
NEXT
funtime! = TIMER - start!
PRINT "Time with function was:"; funtime!

start! = TIMER
FOR i = 1 TO 10000000
    FOR a = 0 TO _PI(2) STEP _PI(1 / 6)
        da## = a * r2d
        'PRINT da##, _R2D(a)
    NEXT
NEXT
ctime! = TIMER - start!
PRINT "Time with constant was:"; ctime

Did I save anybody time testing?
B = B + ...

Offline Unseen Machine

  • QB64 Elite
Re: Unseen GDK - Dev Thread
« Reply #6 on: June 22, 2018, 08:01:26 PM »
Bplus - First, thanks...nice to see some feedback, its appreciated

Second - I've never used constants in GDK as it would require a separate .bi library which is something ive always tried to avoid but im happy to remove the functions entirely and use the inbuilt ones, on checking GDK it appears i only call the function once in the whole library!

Path movement demo
Code: [Select]
'// UnseenGDK Demo - Path Movement using absolute screen co-ordinates

'// Create Screen
GDK_Screen_SetRes Main&, 800, 800

'// DIMension arrays for path points (X, Y, Speed)
DIM Path AS Path, PathPoints(7) AS PathPoint

'// Load path points
'// SUB GDK_Path_New (Path AS Path, PathPoint() AS PathPoint, NumPoints%, Reverse%, Repeat%, Relative%)
GDK_Path_New Path, PathPoints(), 8, 1, 0, 0

'// Main loop
DO
    _LIMIT 30
    CLS

    '// Update path
    GDK_Path_Update Path, PathPoints()

    '// Draw something
    CIRCLE (Path.Vector.X, Path.Vector.Y), 15, _RGB32(255, 0, 0)

    _DISPLAY
LOOP

DATA 50,50,6,750,50,5,750,750,7,50,750,8,400,400,4,500,500,12,300,300,5,610,355,6,10,350,14

REM $INCLUDE:'UnseenGDK.BM'


Unseen

Re: Unseen GDK - Dev Thread
« Reply #7 on: June 23, 2018, 02:40:22 AM »
Attached is my circular sprite rotation sub. Rotates cw, ccw. Please feel free to use in your library or as necessary.

Code: [Select]
PRINT _CWD$
h& = _LOADIMAGE(_CWD$ + "\mars.png", 32)
DEMO& = _NEWIMAGE(1366, 768, 32)
SCREEN DEMO&
RotateStateX! = 0
DO
    GDKSpinCircularSprite h&, DEMO&, RotateStateX! * (2 * _PI) / 360
    RotateStateX! = RotateStateX! + 5
    _DISPLAY
LOOP UNTIL RotateStateX! > 90
_DELAY 15
SCREEN 0
_FREEIMAGE h&
_FREEIMAGE DEMO&

'************************
'* codeguy circular sprite rotation tailored to UnseenMachine GDK library
'************************
SUB GDKSpinCircularSprite (source&, dest&, RotateStateX!)
    osource& = _SOURCE
    odest& = _DEST
    _SOURCE source&
    _DEST dest&
    IF _WIDTH(source&) < _HEIGHT(source&) THEN
        sourceimageradius% = _WIDTH(source&) / 2
    ELSE
        sourceimageradius% = _HEIGHT(source&) / 2
    END IF
    sourceimagecenter% = sourceimageradius%
    IF sourceimageradius% > 0 THEN
        DO
            MINSTEP! = 1 / (2 * _PI * sourceimageradius%) '* codeguy famous minstep calculation
            stepsaroundcircumference! = 0
            DO
                sourcepixelx% = sourceimagecenter% + sourceimageradius% * COS(stepsaroundcircumference!)
                sourcepixely% = sourceimagecenter% + sourceimageradius% * SIN(stepsaroundcircumference!)
                desttranslatex% = sourceimagecenter% + sourceimageradius% * COS(stepsaroundcircumference! + RotateStateX!)
                desttranslatey% = sourceimagecenter% + sourceimageradius% * SIN(stepsaroundcircumference! + RotateStateX!)
                X& = POINT(sourcepixelx%, sourcepixely%)
                PSET (desttranslatex%, desttranslatey%), X&
                stepsaroundcircumference! = stepsaroundcircumference! + MINSTEP!
            LOOP UNTIL stepsaroundcircumference! >= 2 * _PI
            sourceimageradius% = sourceimageradius% - 1
        LOOP UNTIL sourceimageradius% < 1
    END IF
    _SOURCE osource&
    _DEST odest&
END SUB
« Last Edit: June 23, 2018, 02:45:06 AM by codeguy »

Re: Unseen GDK - Dev Thread
« Reply #8 on: June 23, 2018, 02:47:40 AM »
I have used variable naming conventions that may help avoid conflicts. Always glad to help.

Offline Ashish

  • The joy of coding is endless.
Re: Unseen GDK - Dev Thread
« Reply #9 on: June 23, 2018, 06:07:42 AM »
@Unseen
That path one demo is cool! Can you also make circle to loop around the path instead of going back to origin?
if (Me.success) {Me.improve()} else {Me.tryAgain()}


aKFrameWork - http://bit.ly/aKFrameWork
Menu System - http://bit.ly/guiMenuBar
p5.js in QB64 - http://bit.ly/p5jsbas

@KingOfCoders

Offline Unseen Machine

  • QB64 Elite
Re: Unseen GDK - Dev Thread
« Reply #10 on: June 23, 2018, 07:28:11 PM »
At my brother CG (For those who dont know, consider me his apprentice when it comes to coding!) - That Circular rotation is very cool! But, me...well id use it to convert the image to a circle, save it and then use my _MAPTRIAGLE rotation code as i reckon that way would be faster, i'd like to adapt it a little and then add it to my Image Function Library though if thats ok?

@ Asish - Yeah you can make it loop, just change the Reverse% variable to false (0) and the Repeat% variable to true (not a 0)

@ Everyone else...Attached is a .rar file containing files needed for upcoming demos.
Create a folder called "Unseen Demos" in your Qb64 directory and unzip it to there.

Unseen

Offline Unseen Machine

  • QB64 Elite
Re: Unseen GDK - Dev Thread
« Reply #11 on: June 23, 2018, 07:50:12 PM »
Demo - basic input, sprites 101 and vector movement (requires file from previous post)

Code: [Select]
'// GDK Demos - No.3 - Input and movement using vectors.

'// For GDK 1.7

'// Root file path
CHDIR "Unseen Demos\Resources\"

'// DIMension things, we need a Sprite array and a vector array
DIM Sprite AS Sprite, Position AS Vector

'// Create keyboard state array
DIM KB AS KeyBoardState

'// Create a screen
GDK_Screen_SetRes Main&, 800, 600

'// Load the sprite (Handle, Filename$, XFrames, YFrames, Total Frames, Scale!
GDK_Sprite_New Sprite, "Ship1.PNG", 1, 1, 1, 1

'// When loaded sprites need to have things done to them!
GDK_Sprite_Show Sprite '// Make the sprite visible

'// The image background (we dont wanna see it) so we set it's ALPHA (_CLEARCOLOR) value, on this image it's bright pink!
GDK_Sprite_SetAlpha Sprite, _RGB32(255, 0, 255)

'// Set where the image will be rotated from, we want it at the center
GDK_Sprite_SetRotationAsCenter Sprite

'// Set sprite initial position
Position.X = 400
Position.Y = 300

DO

    _LIMIT 30
    CLS

    '// User input
    GDK_Keyboard_GetState KB

    '// Analyse input
    IF KB.Left THEN
        Position.Rotation = Position.Rotation + .0314
    ELSEIF KB.Right THEN
        Position.Rotation = Position.Rotation - .0314
    END IF

    IF KB.Up THEN
        IF Position.Speed < 5 THEN Position.Speed = Position.Speed + .2
    ELSEIF KB.Down THEN
        IF Position.Speed > -1 THEN Position.Speed = Position.Speed - .2
    END IF


    '// Update the position of the sprite's vector
    GDK_Vector_Update Position


    '// Draw the sprite
    GDK_Sprite_Draw Sprite, Position, 1

    _DISPLAY
LOOP


REM $INCLUDE:'Unseengdk.bm'


Unseen

Offline Unseen Machine

  • QB64 Elite
Re: Unseen GDK - Dev Thread
« Reply #12 on: June 23, 2018, 08:28:48 PM »
DEMO - Requires posted files.

use arrow keys to move, space turns rectangles on/off

Code: [Select]
'// GDK Demos - Collision detection

'// For GDK 1.7

'// Root file path
CHDIR "Unseen Demos\Resources\"

'// Now we will be using two sprites, for ease i've used an array
'// Each sprite has an associated Vector and a Rectangle
'// As the enemy sprite is animated it also has an Anim array
DIM Sprite(1) AS Sprite, Position(1) AS Vector, Rect(1) AS Rectangle, Anim AS ANIMATION

'// Input handler
DIM KB AS KeyBoardState

'// Variable for demo - To show collision rectangles
ViewRect% = -1

'// Screen
GDK_Screen_SetRes Main&, 800, 600

'// Load the sprites
GDK_Sprite_New Sprite(0), "Ship1.PNG", 1, 1, 1, 1
GDK_Sprite_New Sprite(1), "Invader1.PNG", 4, 1, 4, 1

'// Set the sprites up (Visibility, Alpha and rotation point)
FOR i% = 0 TO 1
    GDK_Sprite_Show Sprite(i%)
    GDK_Sprite_SetAlpha Sprite(i%), _RGB32(255, 0, 255)
    GDK_Sprite_SetRotationAsCenter Sprite(i%)
NEXT

'// Set initial Vector values (position, rotation and speed)

'// ** NOTE -  GDK uses Radians to handle rotation values (0 to 2 * PI), Increasing turns CCW, Decreasing CW

Position(0).X = 400
Position(0).Y = 300
Position(1).X = 400
Position(1).Y = 200
Position(1).Speed = 2
Position(1).Rotation = 3 * ATN(1)


'// Set up enemy animation
GDK_ANIMATION_NEW Anim, 1, 1, 4, .2, TIMER(.001)

'// Value to increase enemy rotation by
EnemyRotInc! = (4 * ATN(1)) / 300


'// Main loop
DO

    _LIMIT 30
    CLS

    '// User input
    GDK_Keyboard_GetState KB

    IF KB.Left THEN
        Position(0).Rotation = Position(0).Rotation + .0314
    ELSEIF KB.Right THEN
        Position(0).Rotation = Position(0).Rotation - .0314
    END IF

    IF KB.Up THEN
        IF Position(0).Speed < 5 THEN Position(0).Speed = Position(0).Speed + .2
    ELSEIF KB.Down THEN
        IF Position(0).Speed > -1 THEN Position(0).Speed = Position(0).Speed - .2
    END IF

    '// Turn visable rectangles on/off
    IF KB.SPACE THEN IF ViewRect% THEN ViewRect% = 0 ELSE ViewRect% = -1

    '// Increase enemy rotation
    Position(1).Rotation = Position(1).Rotation + EnemyRotInc!

    '// Loop through the sprite array and do stuff
    FOR i% = 0 TO 1

        '// Update the position of each sprite's vector
        GDK_Vector_Update Position(i%)

        '// Autosize the sprites collision rectangle
        '// Thanks to Richard Notely the rectangles actually rotate with the image!
        GDK_Rectangle_Autosize Sprite(i%), Position(i%), Rect(i%)

        '// Update animation
        GDK_ANIMATION_UPDATE Anim

        '// Draw the sprite's
        IF i% = 0 THEN Frame% = 1 ELSE Frame% = Anim.Frame
        GDK_Sprite_Draw Sprite(i%), Position(i%), Frame%

        '// DEMO CODE - To show actual collision rectangles
        IF ViewRect% THEN
            LINE (Rect(i%).C1.X, Rect(i%).C1.Y)-(Rect(i%).C2.X, Rect(i%).C2.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C2.X, Rect(i%).C2.Y)-(Rect(i%).C3.X, Rect(i%).C3.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C3.X, Rect(i%).C3.Y)-(Rect(i%).C4.X, Rect(i%).C4.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C4.X, Rect(i%).C4.Y)-(Rect(i%).C1.X, Rect(i%).C1.Y), _RGB32(255, 0, 0)
        END IF

    NEXT

    '// Check for collision
    IF GDK_Rectangle_Intersect(Rect(0), Rect(1)) THEN LOCATE 1, 1: PRINT "COLLISION"



    _DISPLAY
LOOP



'// Library inclusion
REM $INCLUDE:'Unseengdk.bm'


youll see that collisions are not pixel perfect, and intentionally so...typical practice is to use rectangles that are slightly smaller than the actual image, usually about 80%...here's how you would modify the above to achieve that.

Code: [Select]
'// GDK Demos - Collision detection

'// For GDK 1.7

'// Root file path
CHDIR "Unseen Demos\Resources\"

'// Now we will be using two sprites, for ease i've used an array
'// Each sprite has an associated Vector and a Rectangle
'// As the enemy sprite is animated it also has an Anim array
DIM Sprite(1) AS Sprite, Position(1) AS Vector, Rect(1) AS Rectangle, Anim AS ANIMATION

'// Input handler
DIM KB AS KeyBoardState

'// Variable for demo - To show collision rectangles
ViewRect% = -1

'// Screen
GDK_Screen_SetRes Main&, 800, 600

'// Load the sprites
GDK_Sprite_New Sprite(0), "Ship1.PNG", 1, 1, 1, 1
GDK_Sprite_New Sprite(1), "Invader1.PNG", 4, 1, 4, 1

'// Set the sprites up (Visibility, Alpha and rotation point)
FOR i% = 0 TO 1
    GDK_Sprite_Show Sprite(i%)
    GDK_Sprite_SetAlpha Sprite(i%), _RGB32(255, 0, 255)
    GDK_Sprite_SetRotationAsCenter Sprite(i%)
NEXT

'// Set initial Vector values (position, rotation and speed)

'// ** NOTE -  GDK uses Radians to handle rotation values (0 to 2 * PI), Increasing turns CCW, Decreasing CW

Position(0).X = 400
Position(0).Y = 300
Position(1).X = 400
Position(1).Y = 200
Position(1).Speed = 2
Position(1).Rotation = 3 * ATN(1)


'// Set up enemy animation
GDK_ANIMATION_NEW Anim, 1, 1, 4, .2, TIMER(.001)

'// Value to increase enemy rotation by
EnemyRotInc! = (4 * ATN(1)) / 300


'// Main loop
DO

    _LIMIT 30
    CLS

    '// User input
    GDK_Keyboard_GetState KB

    IF KB.Left THEN
        Position(0).Rotation = Position(0).Rotation + .0314
    ELSEIF KB.Right THEN
        Position(0).Rotation = Position(0).Rotation - .0314
    END IF

    IF KB.Up THEN
        IF Position(0).Speed < 5 THEN Position(0).Speed = Position(0).Speed + .2
    ELSEIF KB.Down THEN
        IF Position(0).Speed > -1 THEN Position(0).Speed = Position(0).Speed - .2
    END IF

    '// Turn visable rectangles on/off
    IF KB.SPACE THEN IF ViewRect% THEN ViewRect% = 0 ELSE ViewRect% = -1

    '// Increase enemy rotation
    Position(1).Rotation = Position(1).Rotation + EnemyRotInc!

    '// Loop through the sprite array and do stuff
    FOR i% = 0 TO 1

        '// Update the position of each sprite's vector
        GDK_Vector_Update Position(i%)

        '' Store the scale value, change it then put it back
        OldScale! = Sprite(i%).Scale
        Sprite(i%).Scale = 80 * (Sprite(i%).Scale / 100)

        '// Autosize the sprites collision rectangle
        '// Thanks to Richard Notely the rectangles actually rotate with the image!
        GDK_Rectangle_Autosize Sprite(i%), Position(i%), Rect(i%)

        '// reset scale value
        Sprite(i%).Scale = OldScale!

        '// Update animation
        GDK_ANIMATION_UPDATE Anim

        '// Draw the sprite's
        IF i% = 0 THEN Frame% = 1 ELSE Frame% = Anim.Frame
        GDK_Sprite_Draw Sprite(i%), Position(i%), Frame%

        '// DEMO CODE - To show actual collision rectangles
        IF ViewRect% THEN
            LINE (Rect(i%).C1.X, Rect(i%).C1.Y)-(Rect(i%).C2.X, Rect(i%).C2.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C2.X, Rect(i%).C2.Y)-(Rect(i%).C3.X, Rect(i%).C3.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C3.X, Rect(i%).C3.Y)-(Rect(i%).C4.X, Rect(i%).C4.Y), _RGB32(255, 0, 0)
            LINE (Rect(i%).C4.X, Rect(i%).C4.Y)-(Rect(i%).C1.X, Rect(i%).C1.Y), _RGB32(255, 0, 0)
        END IF

    NEXT

    '// Check for collision
    IF GDK_Rectangle_Intersect(Rect(0), Rect(1)) THEN LOCATE 1, 1: PRINT "COLLISION"



    _DISPLAY
LOOP



'// Library inclusion
REM $INCLUDE:'Unseengdk.bm'
Unseen

Re: Unseen GDK - Dev Thread
« Reply #13 on: June 23, 2018, 08:41:57 PM »
Adapt as you wish, UnseenMachine.

Offline Ashish

  • The joy of coding is endless.
Re: Unseen GDK - Dev Thread
« Reply #14 on: June 24, 2018, 03:50:01 AM »
@ Asish - Yeah you can make it loop, just change the Reverse% variable to false (0) and the Repeat% variable to true (not a 0)
I try it. But it is not working as expected. Is I'm doing anything wrong?

Code: [Select]
'// UnseenGDK Demo - Path Movement using absolute screen co-ordinates

'// Create Screen
GDK_Screen_SetRes Main&, 800, 700 'replaced 800 with 700 because, I've 1366x768 desktop

'// DIMension arrays for path points (X, Y, Speed)
DIM Path AS Path, PathPoints(7) AS PathPoint

'// Load path points
'// SUB GDK_Path_New (Path AS Path, PathPoint() AS PathPoint, NumPoints%, Reverse%, Repeat%, Relative%)
GDK_Path_New Path, PathPoints(), 8, 0, 1, 0

'// Main loop
DO
    _LIMIT 30
    CLS

    '// Update path
    GDK_Path_Update Path, PathPoints()
    FOR i = 0 TO 7
        CIRCLE (PathPoints(i).X, PathPoints(i).Y), 5, _RGB(255, 255, 0)
    NEXT i

    '// Draw something
    CIRCLE (Path.Vector.X, Path.Vector.Y), 15, _RGB32(255, 0, 0)

    _DISPLAY
LOOP

DATA 50,50,6,650,50,5,650,650,7,50,650,8,400,400,4,500,500,12,300,300,5,610,355,6,10,350,14

REM $INCLUDE:'UnseenGDK_Dev.BM'

if (Me.success) {Me.improve()} else {Me.tryAgain()}


aKFrameWork - http://bit.ly/aKFrameWork
Menu System - http://bit.ly/guiMenuBar
p5.js in QB64 - http://bit.ly/p5jsbas

@KingOfCoders