Share My Creation Objects in B4a - No problem ;-) - O yes, and a simple 2D game engine ...

UPDATE - Objects in B4a - No problem ;-) - O yes, and a simple 2D game engine ...

Hi,
thank you for everyone that tested (and found a few issues) - here is a new version with some fixes (added some more fixes, thanx to agraham for his help on speeding up the graphics, even more ;-).

The problem with the background not rendering completely at the bottom was because of a line of code in the main activity module.
The background image source rect that was specified was wrong - would work on some devices for some reason but not on others.

I had it as "gwcanvas1.BGImageRect.Initialize(sx, 0, sx + w, Activity.Height)" where it should have been
"gwcanvas1.BGImageRect.Initialize(sx, 0, sx + w, gwcanvas1.BGImage.Height)" - needed to specify image height, NOT activity height ;-)

The new demo app might run a bit slow on some devices (for instance HTC wildfire) but on most devices I tested it worked surprisingly good (especially the HTC Desire HD & SE XPeria X10)! I tested performance on quite a few (20+) sprites rendering all at the same time ...

Have fun and let me know if you can optimize / better anything I have done. Would be nice to have someone (me?) write an actual working game
using this ... any takers???

------------------------------------------

Hi,
I started wondering just how far we can push the language (B4A) before it breaks. So, I tried implementing something that will kind-of act like object orientated programming ...

So here goes...
- Basically I found that you can name a code module the same as a contained type in that code module - this leaves us with a lot of possibilities :icon_clap:
- If you now include another "base" type from another code module (MUST be declared before this code module) as one of the fields of your type, you can implement something that resembles inheritance
- Any subs included in your code module now looks and acts like static (class) functions
- Please have a look at the included source (GWCanvas, GWActor and GWSprite code modules - they MUST be included in this order, or else your code WILL NOT compile)

So, knowing that I created a simple 2D game engine that currently supports the following
- sprites (actors) - both static and animated (multi frame)
- rotation / resizing of sprites
- a static background color or ...
- a static / movable / re-sizable (see GWCanvas.BGImageRect field) background image (side-scrolling type of games maybe?)
- some simple collision detection

There are probably a LOT that we can do to add and optimize this - currently the included demo shows a simple background image (clouds) that scrolls from left to right and then bounces back while a few planes fly into each other and disintegrates. The whole process starts over when there are < 2 planes left ...

I tested this on a Sony Erriccson XPeria X10 and a Motorola Defy (MB525) and the performance on both seem acceptable (Currently I use a timer for the games loop - there must be a better way???).

Please play around with this and let me know what you guys think - use it / loose it ... whatever :sign0089:
 

Attachments

  • Sopwith.jpg
    20.5 KB · Views: 10,089
  • SopwithDemo.zip
    272.7 KB · Views: 1,553
Last edited:

ssg

Well-Known Member
Licensed User
Longtime User
excellent!!!

still playing around with this... but i noticed you are re-initialising a timer in activity_resume. this is not advisable.

a better way would be to declare the timer in process_globals, and in activity_create, check if first time, then only initialise it.

i'll explore this more and give some feedback.

thanks!!!
 

francoisg

Active Member
Licensed User
Longtime User
Cool thanx - ye, there might be a few mistakes in there - did it all last night in the spur of the moment between getting time of from the wife and kids and watching a movie (funny the ideas you get while watching action movies ;-) ...
 

ssg

Well-Known Member
Licensed User
Longtime User
lol... i understand the part of finding time between the kids and wife... i'll try the action movies thingy one of these days... i usually try to find ideas watching playhouse disney with the lil fella....

ok, about the code... a little fix, in GWCanvas, sub Render right before "canv.DrawBitmap(Instance.BGImage, Instance.BGImageRect, destRect)" adding the following would clear the shadow effect:

canv.DrawColor(Instance.BGColor)

funny that there is an initial lag when it runs, but after a while it runs much smoother...
 

francoisg

Active Member
Licensed User
Longtime User
mmmm... I'm not sure exactly which shadow effect you are referring to? I see nothing on my test devices ??? What device are you testing on?

Anyway, won't the added DrawColor just add more lag?

By the way, I changed the "canv.DrawRect" just below the code you referred to to rather use "DrawColor" - should be a bit more efficient ? Thank you for the tip ;-)
 

ssg

Well-Known Member
Licensed User
Longtime User
i'm running on an LG optimus one. the 2 sprites that run on the bottom of the screen, when they move right, they leave a copy of their old self in the previous frame. so there is some messy look at the bottom

strange that it is not showing on certain devices. could anyone else test this out and see if they get similar issues?

cheers!
 

francoisg

Active Member
Licensed User
Longtime User
interesting - both my test devices are on 2.1, what version of Android are you using on your device? it even works on the emulator ... strange ...
 

ssg

Well-Known Member
Licensed User
Longtime User
it's running 2.2.

ok.... this just got stranger. i just tried your original code in the emulator, and it works great... and i just realised how it should look.

on my device, the characters are running below the ground... i cant get a screen capture... but the layers are like this:

hill
ground
sprites (with a black background)

hmm..... weirdness...
 

francoisg

Active Member
Licensed User
Longtime User
Ok, makes sense ... so the background does not stretch all the way to fill the complete height of the activity?

That's why it left a "shadow" on your device ... I'll have a look, maybe I'm getting the wrong height value for some reason ...
 

francoisg

Active Member
Licensed User
Longtime User
funny, the following code from the GWCanvas render method should render the complete activity (parent view) ????

destRect.Initialize(0, 0, Instance.ParentView.Width, Instance.ParentView.Height)
 

ssg

Well-Known Member
Licensed User
Longtime User
ok this bit fixed it on my device , and no change on the emulator...

in gwcanvas, render sub, change the following:
canv.DrawBitmap(Instance.BGImage, Instance.BGImageRect, destRect)
to:
canv.DrawBitmap(Instance.BGImage, Null, destRect)

BUT.. the bg dun scroll... lol....
 

francoisg

Active Member
Licensed User
Longtime User
Hi ssg,
weird that it won't work correctly on your device as it is. The code should work (had a look and everything is as it should be) ...
Wonder if any other devices have the same / similar problem ???

Will have a look, maybe I can do the background a bit different ...
 

ZJP

Active Member
Licensed User
Longtime User
Hi,

Same background problem as SSG. Devices : Samsung Galaxy 5 (Froyo) + ZTE Blade (Froyo)

JP
 

francoisg

Active Member
Licensed User
Longtime User
Hi,
the problem was in the main activity where I specified the activity height instead of the background image height rectangle as the source rendering size - funny that I did not pick it up on my test devices???

Anyway, the render method (in gwcanvas) is now also MUCH improved - instead of initializing a canvas object for every render I now only initialize it once. Makes a HUGE difference in rendering speed (don't know why I did not do it like that from the beginning!) - anyway, have fun and let me know if you actually create something using this code ;-)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…