draw an image ?

Started by drfloyd, May 10, 2019, 16:49:41

Previous topic - Next topic

drfloyd

Hello

I am tring AGK for first time and I am lost... i just want to display an image

Download ok :

picture=LoadImage("landscape.png")

How to put this image on screen ??? I should create a sprite ?

there is no command like Putimage (picture,0,0) ?

thanks

TomToad

That's basically it.  Load the image, create a sprite from it, position and scale sprite, enter do/sync()/loop cycle.
picture=LoadImage("landscape.png")
sprite = CreateSprite(picture)
do
   Sync()
loop

There is an example in the example folder images/ChooseImage shows how to load and display a background as well as choosing an image from a file requester.
------------------------------------------------
8 rabbits equals 1 rabbyte.

Qube

#2
Or if you want more control as to when sprites are drawn and also to be able to position sprites at screen coordinates rather than a percentage based system then ( moves a sprite across the screen ):


SetVirtualResolution( 1920, 1080 )

myImage = LoadImage( "dude.png" )
mySprite = CreateSprite( myImage )

Local x As Float

Do
   SetSpritePosition( mySprite, x, 0 )
   DrawSprite( mySprite )

   Inc x, 1
   If x > 1920 Then x = -GetSpriteWidth( mySprite )

   Swap()
Loop


Of course the above doesn't take into account the different refresh rates of monitors and so the sprite will move quicker the higher the refresh rate but you get the idea of how to go about loading and moving a sprite :)
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

drfloyd

#3
that's very strange....

images are sprites ? well... i have never seen that in a basic language  :o

i do not need sprites but need to display lot of little image (about 200) on the same screen... so it seems to me very special

Steve Elliott

#4
No, you apply an image to a Sprite, the Sprite can then be positioned, alpha values set and so on.

You can also load one image and grab sections of it to form lots of Sprites (which is much more efficient).  Using a Sprite atlas.

https://www.appgamekit.com/documentation/Reference/Image/LoadSubImage.htm
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

Qube

Quoteimages are sprites ? well... i have never seen that in a basic language  :o
AGK splits up the reference as you can do certain things with images and certain things with sprites. To keep it basic think of sprites as just the onscreen version of your image. So if you want to show 200 images onscreen you'll need to create sprites from those images first.

QuoteYou can also load one image and grab sections of it to form lots of Sprites (which is much more efficient).  Using a Sprite atlas.

https://www.appgamekit.com/documentation/Reference/Image/LoadSubImage.htm
Highly recommend you learn that method as its way faster to draw loads of different sprites and also saves having to have hundreds of little files for each image.
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

GaborD

#6
Quote from: drfloyd on May 10, 2019, 17:51:50
that's very strange....

images are sprites ? well... i have never seen that in a basic language  :o

i do not need sprites but need to display lot of little image (about 200) on the same screen... so it seems to me very special

It makes sense though, because AGK is essentially a 3D engine, even if you use it for 2D. Images are basically just containers for your texture data to keep things easily organized. Like Qube said, you can then assign them to the objects they should be displayed on.
This means you get to use the power of the GPU for your 2D games, as opposed to a more traditional 2D engine approach that just directly blits images.
You will ofcourse get less mileage out of this when just displaying static images, but for other applications it's a huge performance boost.

If you just need to load lots of images and make a big collage out of them that then doesn't change but you can't prepare the final combined image offline (because the images or their positions may change between program executions or you need to be flexible and adjust for the exact screen rez or whatever), then you could load the images in a loop and use either lots of quads or 1 quad and a custom shader to paste them together into a big rendertexture at program start and then display that final image on a single sprite or screenquad. This would lead to an extremely performant final display.

drfloyd

ok thanks, AGK is for 3D games (or little 2D action games)

I understand that AGK is not really cool for a RPG 2D game... I will keep BLITZ3D & BLITZ MAX for my games so... it seems  that they are still the best basic languages for easy programming 2D games.

Derron

An image in BlitzMax is also something like a sprite.

These TImage things contain textures etc...which are based on TPixmap (pixel data of the loaded images).

In AGK you could surely wrap the images into something do you can simply load a PNG and draw the resulting object.


Regarding "little 2D action games"... You think Blitz3D will do better?

The 2D in 3D approach is needed for fast alpha blending and so on. Blitzmax does that too.


Bye
Ron

Qube

Quote from: drfloyd on May 11, 2019, 07:10:01
ok thanks, AGK is for 3D games (or little 2D action games)

I understand that AGK is not really cool for a RPG 2D game... I will keep BLITZ3D & BLITZ MAX for my games so... it seems  that they are still the best basic languages for easy programming 2D games.
AGK is very much cool for 2D games and probably one of the best out there for it. Not sure why you think differently.

QuoteIn AGK you could surely wrap the images into something do you can simply load a PNG and draw the resulting object.
That's exactly what you can do. You can even load one png which has 100's of sprites as an atlas to reduce draw calls. So I'm not sure why drfloyd says AGK is not for 2D.
Mac Studio M1 Max ( 10 core CPU - 24 core GPU ), 32GB LPDDR5, 512GB SSD,
Beelink SER7 Mini Gaming PC, Ryzen 7 7840HS 8-Core 16-Thread 5.1GHz Processor, 32G DDR5 RAM 1T PCIe 4.0 SSD
MSI MEG 342C 34" QD-OLED Monitor

Until the next time.

GaborD

#10
Quote from: drfloyd on May 11, 2019, 07:10:01
I understand that AGK is not really cool for a RPG 2D game...

Why do you think that? I am genuinely interested. Maybe I am overlooking something.

In my opinion AGK would be a great choice, an other similarly fitting option would be BMX + openB3D or similar addons.
They both allow for extreme flexibility, which is paramount when going for high quality and performance.
That the engine is 3D under the hood is not a negative. On the contrary, it would be silly not to use the immense power of modern GPUs for a 2D games too. (even HTML5 2D engines like Pixi or Fabric do it)

Honestly, I think you could make a 2D RPG in AGK that rivals AAA quality and still runs extremely well. (much faster than any of the big engines can achieve)
Yes, it is more effort because you have to optimize and set up a lot of rendering stuff yourself, but for that it WILL be optimized and not a bloated jack of all trades, master of none.


Pfaber11

AGK2 is great for 2d and 3d once you get the hang of it it can be used for just about anything . I have used Blitz3D as well and going from that to AGK was a big learning curve but really worth the effort in the end . Good luck and happy programming .
HP 15s i3 1.2 upto 3.4 ghz 128 gb ssd 16 gb ram 15.6 inch screen. Windows 11 home edition .  2Tb external hard drive dedicated to Linux Mint .
  PureBasic 6 and AppGameKit studio
ASUS Vivo book 15 16gb ram 256gb storage  cpu upto 4.1 ghz

drfloyd

Thank you,
I need to understand with an exemple

in my game, i have a grid of 1000 x 1000 cases of 16x16 pixels (16000 x 16000 pixels)... It is a RPG 8bit style map.... with about 200 differents .PNG to create the map

In BLITZ :

dim picture(200)
; load 200 pictures 16x16
for i=1 to 200
picture(i)=loadimage("case"+str$(i)+".png")
next

;and then i can combine on screen and the cases of 16x16
drawimage 0,0,picture(145)
drawimage 16,0,picture(78)
etc...
of course the screen scroll with the move of the player (as the full screen is 16000x16000 pixels)


How do the same in AGK with Sprites ?????


blinkok

#13
BLITZ
dim picture(200)
; load 200 pictures 16x16
for i=1 to 200
picture(i)=loadimage("case"+str$(i)+".png")
next

AGK
picture as integer[]
for i=1 to 200
  picture.insert(CreateSprite(loadimage("case"+str$(i)+".png"))    // Remember that arrays in AGK begin with index 0
next

You could then position the images like
SetSpritePosition(picture[144], 0, 0)
SetSpritePosition(picture[77], 16, 0)

Checkout SetViewOffset() for scrolling the window
Also check out FixSpriteToScreen() to "fix" your HUD to the screen

You can find the "length" of an array using MyArray.length (You can also set this value). It is important to remember that although it is named "length", it is in fact
a pointer to the last entry. So an empty arrays length will return -1 and an array with one element will return 0.

There is a good guide on using arrays here



Pakz

DrawSprite( mySprite )

The above command is used to draw the contents of a sprite to the screen. You need to position the sprite with the setspriteposition command. The agk regular sprites are like the oldskool hardware sprites and not a part of the screen. If you hide a sprite it's image dissapears from the screen. If you draw a sprite to the screen it becomes a part of the screen.
You would need to load each different tile to a different sprite.

I was writing down my own experiences(notes) with agk in this book here : https://cromdesi.home.xs4all.nl/agkmobile/appgamekitmobiletutorial.pdf