September 23, 2019, 05:24:43 PM

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

Offline Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #15 on: May 13, 2019, 09:24:01 AM »
You would need to load each different tile to a different sprite.

I don't think so.
The command you posted ("DrawSprite(spriteIndex)") states in its documentation that it _immediately_ draws the sprite at the currently defined position for this sprite.
So without any clue about AGK I would say that "Sprite" is some kind of object which has helper functions to move it around, rotate, ... and to draw the image "behind" the sprite.

So in this case you won't need 1000x1000 sprites for the whole grid/map.
You need 1000x1000 references to sprites (so eg a spriteIndex).
Then for each _visible_ grid cell you position the sprite which is referenced by the spriteIndex - and then just "DrawSprite(spriteIndex)" it.
If two neighbour grid cells use the same image this means you use the same sprite - but draw them _after_ you positioned the sprite according to these cells.


Your approach is valid IF each grid cell has individual animation positions, rotation, alpha, scale, ... but most of the time this is not true and you just need the same (mostly static) image.
As you do not want 1000x1000 sprites with all their individual settings you could cheat then: create 3-4 different sprites of a "basic image" - eg. with an offset to the animation.
Then you assign a random spriteIndex of these "3-4 different sprites" to your grid cell - leading to a map which has some randomized animation/size/color tint or whatever you've changed inbetween these sprites.


bye
Ron

Offline Pakz

  • Full Member
  • ***
  • Posts: 161
    • My homepage
Re: draw an image ?
« Reply #16 on: May 13, 2019, 09:30:41 AM »
I think I wrote it wrong yes. Sprites are containers :)

So 10 different tile images= 10 different sprites.

Offline GaborD

  • Sr. Member
  • ****
  • Posts: 296
Re: draw an image ?
« Reply #17 on: May 13, 2019, 10:49:04 AM »
You can pack all the tiles in a tilemap (as others suggested) and then use a pretty simple shader to do the actual drawing on a screenquad, including smooth scrolling if needed. (AGK has a screenquad object that will always exactly cover the screen, this makes faked-2D very easy.)

You know the screenrez and thus how many tiles fit, so it should be relatively simple math to calculate both the tile position and the fractional scroll offset.
With so few different tiles in the tilemap you don't even need any packing for the tile-ID lookup texture, the ID fits in a single channel.
Now that I think about it, it's probably better to use two channels (it's still one lookup, no performance impact), for x and y tileset offsets respectively. Keeps the math even simpler and you are ready for much bigger tilemaps in case you want to add in more tile types later.
Just make sure mipmapping is off for the tile texture, otherwise you could get weird lines at tile edges due to the UV jumps. (because the GPU thinks "oh look, tons of texture pixels on a surface in a single screen pixel area, I can switch to the smallest mip")

This would in my opinion be the most performant way to create the ground by far.
One surface, very simple two-texture-lookups shader. Doesn't get faster than that.
Could easily reach thousands of FPS on midrange systems and the CPU side would be quick too because you are not juggling tons of objects/textures/sprites and don't need any loops going through the tiles constantly remapping them or such. Two static textures on one static object. Bam.

Any dynamic objects could then be sprites on top, as usual. 

Offline blinkok

  • Full Member
  • ***
  • Posts: 121
Re: draw an image ?
« Reply #18 on: May 13, 2019, 12:11:21 PM »
If you search on the TGC forum there is a method where you store the tiles as a font and then you just use text. Apparently it's super fast too

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
Re: draw an image ?
« Reply #19 on: May 13, 2019, 05:46:10 PM »
so each picture is a sprite objet...

on each refresh loop I should delete all the sprites on the screen ? (clearscreen do not delete images)

I cannot understand why there is not classic putimage in AGK... I hear your arguments, but cannot understand anyway  ;D

Offline Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #20 on: May 13, 2019, 06:13:21 PM »
Sprites are objects which you defined once. So until not hiding or moving them they seem (as said I am not an AGK user) to be rendered the same on each frame.

Means once you drew your map you hide the sprites to avoid "auto rendering" them in the next frame.

In the next frame you again move the sprites l, draw them...and after all cells you hide them again.


And yes... A simple way to draw images would help and ease the pain. Glad to not use AGK for now as this sounds like scene management in flash games of 10 yrs ago.


Bye
Ron

Online Steve Elliott

  • Hero Member
  • *****
  • Posts: 2000
Re: draw an image ?
« Reply #21 on: May 13, 2019, 06:54:47 PM »
You could have 1 image that is used by several (totally independent) sprites, but they all share the same image ID number (which is created for you) what is so difficult about that?

Part of the code that Qube demonstrated showed this.  I added another sprite with the same image (mySprite2) better to use an array of 'mySprite' rather than using another name but it shows the concept simpler.
 
Code: [Select]
myImage   = LoadImage( "dude.png" )

mySprite  = CreateSprite( myImage )
mySprite2 = CreateSprite( myImage )
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 Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #22 on: May 13, 2019, 07:13:31 PM »
The OP has an issue with:
- the requirement to create an additional (maybe even managed) object to draw an image (or part of it)
- the requirement to create an object having a load of other unneeded properties and function pointers or hower it is implemented

Our answers were also dealing with performance as he told about a 1000x1000 map - and so the best suiting solution is to find. Also he had issues with the (managed?) sprites as they are drawn there he "left" them after drawing the map. This happens with managed objects (which you add to scenes/layers/...). This is not desired here, he wants a "fire and forget" solution/command.


bye
Ron

Online Steve Elliott

  • Hero Member
  • *****
  • Posts: 2000
Re: draw an image ?
« Reply #23 on: May 13, 2019, 07:21:18 PM »
If he has difficulty with that code I just posted then perhaps AGK is not for him, performance is irrelevant in that case.

The documentation is pretty good though, maybe some read throughs:  https://www.appgamekit.com/documentation/Reference/Sprite.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 Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #24 on: May 13, 2019, 07:56:40 PM »
The documentation supports my view about the unnecessarity of such a complex object for the simple task of bringing a texture to the canvas (draw image to screen).

Are you sure there is no hidden object he could use?

bye
Ron

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2115
Re: draw an image ?
« Reply #25 on: May 13, 2019, 08:08:17 PM »
I think there is some confusion on how AGK works with sprites.

If this is your Blitz Code :

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

Then your AGK code would be something like :

Code: [Select]
SetVirtualResolution( 1920, 1080 )

Dim picture[ 200 ]

For i = 1 To 200
   tempImage = LoadImage( "case" + Str( i ) + ".png" )
   picture[ i ] = CreateSprite( tempImage )
Next

What's complex about that?

Of course loading 200 images is not very efficient for draw calls. So in AGK you would use a texture atlas ( basically one big image with all your tiles and a text file containing all the images coordinates ) and then do something like :

Code: [Select]
SetVirtualResolution( 1920, 1080 )

atlasImage = LoadImage( "myTileSet.png" )
Dim picture[ 200 ]

For i = 1 To 200
   tempImage = LoadSubImage( atlasImage, "case" + Str( i ) + ".png" )
   picture[ i ] = CreateSprite( tempImage )
Next

The above method is much more efficient for drawing lots of little sprites.
Until the next time...

Offline Derron

  • Hero Member
  • *****
  • Posts: 2419
Re: draw an image ?
« Reply #26 on: May 13, 2019, 08:25:08 PM »
Think the OP already grasped that he does not need to create a "sprite" for every grid cell of the 1000x1000 map but just for every "different" ground tile (may it be animation offset or texture/subimage wise).

But if you _did_ that way you would end up creating 1 million "complex" objects which might share texture references but hold a lot of properties which are not used for a simple "map" (physics stuff etc).
So in that case it would be "lighter" to have some light "bitmap" (or however you want to call it) object which you can explicitely draw at a position - or if you want to have it more complex a simple "TRenderable/Drawable" (position, scale, rotation, alpha, tint).

Now we all told him to not use a sprite for each grid cell but just for differen ground tiles - think we all accepted that as solution. Now the problem is that these sprites are objects on the screen which seem to be auto-rendered at the last position you set them to (as said: not a user of AGK but the OP wrote about such behaviour). This means that the sprites are auto-handled / rendered. Might make sense in some situations, but this means you would have to hide/move-to-offscreen your ground-tile-sprites after you have drawn your map with these "tile sprites".


bye
Ron

Offline Qube

  • Administrator
  • Hero Member
  • *****
  • Posts: 2115
Re: draw an image ?
« Reply #27 on: May 13, 2019, 08:35:26 PM »
Quote
Now the problem is that these sprites are objects on the screen which seem to be auto-rendered at the last position you set them to (as said: not a user of AGK but the OP wrote about such behaviour).
AGK has two methods to draw sprites. 1) Percentage system where 0, 0 is top left and 100, 100 is bottom right. 2) Proper screen coordinates \o/

The percentage system will also just draw every sprite on screen regardless if you told it to draw a sprite or not. So if you are using this method you have to hide sprites, blah!!

The screen coordinate method ( the most sane method to use ) is enabled by calling the command, eg. SetVirutalResolution( <width>, <height> ) - Then your sprites are ONLY drawn when you use the command DrawSprite. This method also allows you to draw the same sprite multiple times on screen without having to create additional sprites of the same ID, for example :

Code: [Select]
SetVirtualResolution( 1920, 1080 )

For i = 1 To 100
   SetSpritePosition( mySprite, Random( 1920 ), Random( 1080 ) )
   DrawSprite( mySprite )
Next

So ( for me ) it's always easier to use SetVirutalResolution( <width>, <height> )  to allow for screen coordinates of sprites, draw sprites only when you want them to be drawn and drawing the same sprite multiple times.
Until the next time...

Offline drfloyd

  • Jr. Member
  • **
  • Posts: 35
Re: draw an image ?
« Reply #28 on: May 13, 2019, 09:00:54 PM »
thanks for all !

My final decision is that AGK is not for me :)

My RPG do not need a powerfull engine, so i prefer to continue with Blitz 3D (or something like : NAALAA, EGSL...), it's more easy with real drawing than Sprites (for my game)

Last version of my RPG :
https://www.gamopat-forum.com/t101951-cosmos-the-secret-melody-1-4-telechargement-gratuit#2922832

Thanks again for this discussion and sorry for my bad english

Offline Pingus

  • Jr. Member
  • **
  • Posts: 91
Re: draw an image ?
« Reply #29 on: May 17, 2019, 01:48:27 AM »
Hi,
Does anyone know if drawing several instance of a sprite with DrawSprite count for one draw call overall, or a draw call for each sprite ?
I would assume that sprites are optimized for reducing the number of draw call, but Draimage is not ?
So Drawimage may be very slow, especially on mobile ? It is uneasy to find some doc about how things are handled internally in AGK...