September 23, 2019, 05:21:29 PM

Author Topic: draw an image ?  (Read 1287 times)

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
draw an image ?
« on: May 10, 2019, 04:49:41 PM »
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

Offline TomToad

  • Sr. Member
  • ****
  • Posts: 429
Re: draw an image ?
« Reply #1 on: May 10, 2019, 05:06:19 PM »
That's basically it.  Load the image, create a sprite from it, position and scale sprite, enter do/sync()/loop cycle.
Code: [Select]
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.

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2115
Re: draw an image ?
« Reply #2 on: May 10, 2019, 05:41:44 PM »
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 ):

Code: [Select]
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 :)
Until the next time...

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
Re: draw an image ?
« Reply #3 on: May 10, 2019, 05:51:50 PM »
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

Online Steve Elliott

  • Hero Member
  • *****
  • Posts: 2000
Re: draw an image ?
« Reply #4 on: May 10, 2019, 06:18:48 PM »
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
Windows 10, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
MacOS Mojave, 64-bit, 8Gb RAM, CPU Intel i5, 2.3 Ghz, Intel Iris Plus Graphics 640 1536 MB.
Linux Mint 19.1, 64-bit, 16Gb RAM, CPU Intel i5, 3.2 GHz, Nvidia GeForce GTX 1050 (2Gb).
Raspbian Buster, pi4 4Gb RAM,1.5Ghz

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2115
Re: draw an image ?
« Reply #5 on: May 10, 2019, 09:46:19 PM »
Quote
images 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.

Quote
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
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.
Until the next time...

Offline GaborD

  • Sr. Member
  • ****
  • Posts: 296
Re: draw an image ?
« Reply #6 on: May 11, 2019, 12:43:57 AM »
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.

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
Re: draw an image ?
« Reply #7 on: May 11, 2019, 07:10:01 AM »
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.

Offline Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #8 on: May 11, 2019, 07:20:00 AM »
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

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2115
Re: draw an image ?
« Reply #9 on: May 11, 2019, 08:23:24 AM »
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.

Quote
In 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.
Until the next time...

Offline GaborD

  • Sr. Member
  • ****
  • Posts: 296
Re: draw an image ?
« Reply #10 on: May 11, 2019, 12:39:48 PM »
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.


Offline Pfaber11

  • Sr. Member
  • ****
  • Posts: 280
    • FABERS GAMES
Re: draw an image ?
« Reply #11 on: May 11, 2019, 04:23:38 PM »
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 .

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
Re: draw an image ?
« Reply #12 on: May 13, 2019, 07:18:19 AM »
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 ?????


Offline blinkok

  • Full Member
  • ***
  • Posts: 121
Re: draw an image ?
« Reply #13 on: May 13, 2019, 08:34:34 AM »
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



Offline Pakz

  • Full Member
  • ***
  • Posts: 161
    • My homepage
Re: draw an image ?
« Reply #14 on: May 13, 2019, 09:15:04 AM »
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