Demo and Code

Started by Steve Elliott, August 22, 2017, 13:03:25

Previous topic - Next topic

Steve Elliott

Just sharing a bit of code for those new to AGK, and those who might be considering it.  A Windows exe for those without AGK.  The code takes a sprite containing 3 frames and plays each frame 30 times a second as it moves across the screen.

The AGK commands are the same in C++ or BASIC.

When you have a bunch of sprites for your game, this is the most efficient way of accessing them - through a Sprite Atlas (Sprite Sheet).

C++ Version.


// Sprite Animation
// written by Stephen Elliott

// includes
#include "SpriteAnimation.h"

// namespace
using namespace AGK;

app App;

void app::Begin(void)
{
agk::SetWindowTitle(" Sprite Animation ");
agk::SetWindowSize( 1024, 768, 1 );
agk::SetDisplayAspect( 1024 / 768 );
agk::SetVirtualResolution(1024, 768);
agk::SetClearColor( 0, 0, 50 ); // dark blue
agk::SetVSync(1);
agk::SetScissor( 0, 0, 0, 0 );
agk::UseNewDefaultFonts(1); // nicer font
agk::SetRawMouseVisible(0); // hide mouse pointer

// create sprite animation and play
agk::LoadImage( 1, "Sprites.png" );
agk::CreateSprite( 1, 1 );
agk::SetSpriteOffset( 1, 0.0f, 0.0f );
agk::SetSpriteAnimation( 1, 64, 64, 3 );
agk::PlaySprite( 1, 30, 1, 1, 3 );        // sprite id, FPS, loop animation, frame start, frame end

float LeftEdge = agk::GetScreenBoundsLeft(); // get bounds for full screen

// position sprite
float xPos = LeftEdge - 64.0f;
agk::SetSpritePosition( 1, xPos, 352.0 );
}

int app::Loop(void)
{
float RightEdge = agk::GetScreenBoundsRight(); // get bounds for full screen
float xPos      = agk::GetSpriteX(1);
int KeyEsc      = agk::GetRawKeyState(27);

//agk::Print(agk::ScreenFPS());
agk::Sync();

// re-position sprite
xPos += 2.0;
agk::SetSpritePosition( 1, xPos, 352.0 );

// break from program
if ( xPos > RightEdge ) PostQuitMessage(0);
if ( KeyEsc )           PostQuitMessage(0);

return 0;
}

void app::End(void)
{
// free resources
if (agk::GetImageExists(1)  == 1) agk::DeleteImage(1);
if (agk::GetSpriteExists(1) == 1) agk::DeleteSprite(1);
}


BASIC version...Looks like Rick has converted it.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Rick Nasher

#1
Thanks for sharing, but with the latest (aug 2017 release) I got some errors on Tier1.
[cut]     ;D
This should fix and display as intended I guess:


// Sprite Animation
// written by Stephen Elliott

SetWindowTitle("Sprite Animation")
SetWindowSize( 1024, 768, 1 )
SetDisplayAspect( 1024 / 768 )
SetVirtualResolution(1024, 768 )
SetClearColor( 0, 0, 50 ) // dark blue
SetVSync(1)
SetScissor( 0, 0, 0, 0 )
UseNewDefaultFonts(1) // nicer font
SetRawMouseVisible(0) // hide mouse pointer

// create sprite animation and play
LoadImage( 1, "Sprites.png" )
CreateSprite( 1, 1 )
SetSpriteOffset( 1, 0.0, 0.0 )
SetSpriteAnimation( 1, 64, 64, 3 )
PlaySprite( 1, 30, 1, 1, 3 )        // sprite id, FPS, loop animation, frame start, frame end

LeftEdge# = GetScreenBoundsLeft() // get bounds for full screen

// position sprite
xPos# = LeftEdge# - 64.0
SetSpritePosition( 1, xPos#, 352.0 )

RightEdge# = GetScreenBoundsRight() // get bounds for full screen
xPos#      = GetSpriteX(1)

do
KeyEsc     = GetRawKeyState(27)
//Print(agk::ScreenFPS())
Sync()

// re-position sprite
inc xPos#,2.0
SetSpritePosition( 1, xPos#, 352.0 )

// break from program
if ( xPos# > RightEdge# ) then PostQuitMessage(0)
if KeyEsc  then PostQuitMessage(0)


loop
Function PostQuitMessage(m)
if m=0 then m$="Quiting"

print(m$)
// free resources
if GetImageExists(1) then DeleteImage(1)
if GetSpriteExists(1)  then DeleteSprite(1)
end
Endfunction




_______________________________________
B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Steve Elliott

#2
lol thanks for sharing every single error message.

I re-wrote that tier 1 program without testing.  A little C++ still in there.   :P

Removed so not to confuse people lol.
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb

Rick Nasher

#3
Quote from: Steve Elliott on August 22, 2017, 17:53:10
lol thanks for sharing every single error message.

Hehehe. Sorry, maybe a bit inconsiderate indeed. Removed that too.  ;)

Can't to take that credit though: C++ is not my friend. :-)
_______________________________________
B3D + physics + shaders + X-platform = AGK!
:D ..ALIENBREED *LIVES* (thanks to Qube).. :D
_______________________________________

Steve Elliott

lol you cover BASIC and me C++ AGK  :D
Win11 64Gb 12th Gen Intel i9 12900K 3.2Ghz Nvidia RTX 3070Ti 8Gb
Win11 16Gb 12th Gen Intel i5 12450H 2Ghz Nvidia RTX 2050 8Gb
Win11  Pro 8Gb Celeron Intel UHD Graphics 600
Win10/Linux Mint 16Gb 4th Gen Intel i5 4570 3.2GHz, Nvidia GeForce GTX 1050 2Gb
macOS 32Gb Apple M2Max
pi5 8Gb
Spectrum Next 2Mb