draw an image ?

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

Previous topic - Next topic

Derron

Quote from: Pakz on May 13, 2019, 09:15:04
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

Pakz

I think I wrote it wrong yes. Sprites are containers :)

So 10 different tile images= 10 different sprites.

GaborD

#17
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. 

blinkok

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

drfloyd

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

Derron

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

Steve Elliott

#21
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.


myImage   = LoadImage( "dude.png" )

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

Derron

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

Steve Elliott

#23
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
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

Derron

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

Qube

I think there is some confusion on how AGK works with sprites.

If this is your Blitz Code :


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 :


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 :


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.
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.

Derron

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

Qube

QuoteNow 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 :


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.
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

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

Pingus

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...