Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - (Advanced Sprites) 2D Cam/Rendering Library

Author
Message
ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 19th Feb 2014 12:27 Edited at: 19th Feb 2014 12:45
Hi,

I just wrote a small library handling rendering of advanced sprites (which could easily be changed to use DBPro sprites, Advanced2D-images (e.g. for particle systems using additive blending) or anything else), including a simple camera system with zoom and rotation. The advantage of AdvancedSprites over native DBPro sprites is that they can be rendered with anti aliasing, which really improves the image quality:



While there are a few artifacts in the AS area as well (I don't really know why, but part of the problem surely is the selection of bad source images, I didn't have anything more fancy at hand), it's probably quite clear that it looks a lot better than the DBP pixel mess on the right side.

Well, and apart from the improved visual quality, the library handles world to screen transformations and stuff like that for you and you don't have to worry about writing your own camera code for 2D games.

In case you don't have Advanced Sprites yet, you can get it here.


And finally, here's the library:



And the complete code that resulted in the screenshot above (without the comparison though, I did that in paint), no media required:




Feel free to use this code for your own projects. Mentioning me in the credits would be nice, but well... I guess that's up to you.

Cheers.


Edit: Maybe I should add a little overview of the available functions and how to use them.

as_init() - should be called once after starting your program
as_loadSprite(f$) - loads a sprite file, centers it and returns handler (store it in a variable to access the sprite later)
as_loadSpriteOff(f$,offx#,offy#) - load sprite with given offset (relative to sprite size) - using 0.5,0.5 will center the sprite
as_createSprite(img) - creates an Advanced Sprite from an image, centers it and returns the handler
as_createSpriteOff(img,offx#,offy#) - you get the point
as_pasteSpriteRaw(spr, x#,y#, scale#, angle#) - pastes the advanced sprite (using the handler you were given when creating the sprite) to the given screen coordinates
as_pasteSprite(spr, x#,y#, scale#, angle#) - pastes the advanced sprite to the given world coordinates
as_setCam(x#,y#,zoom#,angle#) - sets the camera state, x# and y# being the world coordinates of the cam's center, zoom# is 1.0 by default (2.0 meaning everything is twice as big)
as_moveCam(forward#, sideways#) - can be used to make the camera move relative to its angle. Both values can be positive or negative and higher values will make the cam move faster.
as_updateResolution() - should be called if you change the screen resolution while the program is running, to make sure the camera knows what's going on
as_worldToScreen(wx#,wy#) - Transforms world to screen coordinates and stores them in the global variables as_OutX/as_OutY. Usually you don't need to call this function, as as_pasteSprite() takes care of that
as_screenToWorld(sx#,sy#) - Transforms screen to world coordinates and stores them in as_OutX/as_OutY. You can for instance use this function to find out what the woorld coordinate of the mouse is.

And a minimal usage example:



Note: Right now the library does not support animated sprites or tilesets. Adding those things wouldn't be a big deal, so just ask if you need it (or add it yourself ).

NoteĀ²: Also, the rendering could be optimized. Right now dxs begin sprite render and dxs end sprite render are called before and after every single sprite drawing operation. I could add some simple optimization, but haven't done it yet since it would make the code and usage (slightly) more complicated.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Feb 2014 18:40
I'm not entirely sure the left looks better than the right side. Why does every advanced sprite have a border? It's like the AA is picking the wrong colors or something for the alpha pixels

ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Feb 2014 19:51 Edited at: 21st Feb 2014 19:53
Quote: "Why does every advanced sprite have a border? It's like the AA is picking the wrong colors or something for the alpha pixels"


That's a valid question. I don't really know what it's doing there, and I even tried to counter the effect by making sure the transparent pixels are of the same color as the rest of the sprite, but it didn't make any difference unfortunately...

However, from my experience the AA-effect really pays off in games, and the overall image quality is increased quite a lot, especially in 2D games with a camera that is able to zoom and rotate. Since this is how DBPro renders rotated sprites:



...

In comparison, I replaced the rendering code of Lab Rush (anyone remebers it? Was posted here a few years ago, the creator allowed me to work with the code) yesterday with the library above, using Advanced Sprites.
This is how it looked before (note the extreme artifacts on the red player, the blue shield and the yellow crosshair):



And finally, this is how it looks now, rendered with Advanced Sprites (and the ground tiles replaced, but that's not really the point):



This degree of smoothness would never be possible with native DBPro sprites. As far as I know. And you can't really see the partially wrong color blending that was apparent in the screenshot from the original post.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 21st Feb 2014 21:04
Have you gotten into AppGameKit yet? I find the sprites look so much better I think.

ShellfishGames
11
Years of Service
User Offline
Joined: 6th Feb 2013
Location:
Posted: 21st Feb 2014 21:28
I've bought and installed it, but haven't worked with it yet. Maybe I should.

Login to post a reply

Server time is: 2024-11-23 07:58:50
Your offset time is: 2024-11-23 07:58:50