[Staff][Dev] GL 2D Game Engine

Here you can find side projects of the staff and great projects which we think should get extra support. (Note that featured projects are not projects by staff members of MaxCoderz)

Moderator: MaxCoderz Staff

User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

[Staff][Dev] GL 2D Game Engine

Post by qarnos »

This is something I have been piecing together over the past few of weeks. It started off as a 2D vector graphics engine but now is evolving into a more generic game of engine, taking care of keyboard input, etc.

The graphics side of things is very Open GL-ish, albeit with one fewer dimension! The matrices are 3x3, so technically it can do 3D, but none of the code is really set up for that.

Here's an example of a piece of code to draw a box:

Code: Select all

            .glBegin(GL_POLYGON)

                .glTexCoord(0, 0)
                .glVertex(-40, -20)
                
                .glTexCoord(63, 0)
                .glReflect(GL_REFLECT_Y)        ; +40, -20
                
                .glTexCoord(63, 31)
                .glReflect(GL_REFLECT_BOTH)     ; +40, +20
                
                .glTexCoord(0, 31)
                .glReflect(GL_REFLECT_X)        ; -40, +20
                
            .glEnd()
I told you it was Open GL-ish! :mrgreen:

The macro's beginning with a period are just for convenience when the arguments to the routines are immediate values. They can be rewritten using manual LDs and CALLs.

.glReflect() is an optimisation for symmetrical shapes, although I need to implement vertex lists before it becomes really useful.

Keyboard input is done using a slightly modified version (to allow it to work in an interrupt routine better) of the event-driven keyboard handler in the Programming Help section.

I have also been working on some code for a C-style printf routine (this is why it's becoming a generic library!). It's not yet finished, so not in the demo, but at the moment I have the raw code to produce decimal representations of 32-bit integer values and also arbitrary x.y fixed point values. I just can't work out the best way to put this in a format specifier! The best I can come up with is: "%03.6dw[24.8]" (a 32-bit 24.8 fixed point value, using at least 3 decimal digits for the integer part - left padded with zeros - and 6 digits in the fractional part).

You can download a zip file containing the current source and a pre-assembled binary. The source is written for TASM (why do I get the feeling I'm the only one still using this!), so good luck!

There is also an animated GIF (caution - almost half a megabyte).

The keys are:

Left/Right - rotate
2nd* + Left/Right/Up/Down - translate
Plus/Minus - scale
Enter - cycle drawing mode
Clear* - toggle texture mapping (affects polygons only)
Mode - exit

* In PindurTI, left shift is [2nd], right shift is [clear].
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
User avatar
benryves
Maxcoderz Staff
Posts: 3078
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Staff][Dev] GL 2D Game Engine

Post by benryves »

Nice going. :) Do you have any plans for a game with this? Or would you consider turning it into a Flash application library (as you did with Aether?)
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

benryves wrote:Nice going. :) Do you have any plans for a game with this? Or would you consider turning it into a Flash application library (as you did with Aether?)
The plan at the moment is to use it in a 2D space flight game revolving around the Apollo project (think Lunar Lander, but you have to fly to the moon, too!). In order to get there, I'll have to turn it into a flash library, since there would be no room left for the program code. Plus, it gives me the freedom to use ridiculous amounts of lookup tables :)
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

I've added a bit of stuff to the engine this weekend.

The first is vertex lists, although they are done a bit differently from OpenGL. Here is an example of how they are used:

Code: Select all

            .glBegin(GL_VERTEX_LIST)
                
                .glVertex(0, -30)
                .glReflect(GL_REFLECT_X)
                .glVertex(-30, 0)
                .glReflect(GL_REFLECT_Y)
                
            .glEnd()
                
            .glBegin(GL_POLYGON)

                .glTexCoord(0, 0)
                .glIndex(0)
                
                .glTexCoord(31, 0)
                .glIndex(3)
                
                .glTexCoord(31, 31)
                .glIndex(1)
                
                .glTexCoord(0, 31)
                .glIndex(2)
                
            .glEnd()
At the moment GL_VERTEX_LIST uses AppBackupScreen to store the transformed co-ordinates. When I convert it to an app-library it will use dynamically allocated memory.

There is also a new function which I wish was in OpenGL: glCircle. It can draw outlined or filled circles of radius up to 65535 pixels, positioned with 16-bit signed co-ordinates. It still needs a bit of work - huge circles are really slow because it calculates all the pixels, even the ones that are off-screen. This needs to be fixed. On the other hand, it at least goes to the trouble of doing a bounding-box check before drawing the circle. This is evident in the screenshot below when the framerate jumps 5-6 fps when the circle goes off-screen.

I've also written a program to semi-automate the creation of a jump table when making the app library (something I never did for aether - which I really should have). Hopefully I can test it as an app sometime in the next week.

Also, can someone point me in the right direction to find some info on grayscale? I've never tried it before and wouldn't mind seeing if it is at least feasible to include it, but I can't seem to find anything in the Grayscale forum.

Image
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
darkstone knight
New Member
Posts: 67
Joined: Sun 09 Nov, 2008 1:56 pm

Re: [Staff][Dev] GL 2D Game Engine

Post by darkstone knight »

impressive :o
User avatar
DigiTan
Calc Master
Posts: 1069
Joined: Fri 04 Mar, 2005 12:52 am
Location: NW Wisconsin
Contact:

Re: [Staff][Dev] GL 2D Game Engine

Post by DigiTan »

What the?!!? So that's what it does!! :shock: :o
My diet consists of nails, code-stealers, and HP fans.
Projects:

Robot War [TI-82, TI Flash App]
Sonic the Hedgehog [Multiplatform]
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

More progress today! This one is a problem I am glad is finally out of the way.

The problem with allowing really large circles (radius up to 65535) is that big circles were really slow (ie: several seconds). This problem has now been overcome using a wrapper routine to check the circle against the clipping window and calculate initialise the variables of the circle drawing loop so that only the parts which are potentially visible are calculated.

The result is that large circles are now almost as fast as small ones. There is some overhead, since two 32-bit square roots need to be taken, but this is negligible compare to the alternative!

The screenshot isn't too interesting, though. It's a trip around the perimeter of a large circle, but I like it since this is the kind of effect I want to use in the lunar orbiter game - with the circle standing in for the Earth/Moon.

Image
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

I've begun the tedious process of porting the code to an app library. I've got the dynamic memory handling sorted, so that's the big hurdle out of the way.

Code has been added to copy the contents of the LCD at startup, so you can draw over what's already there:

Image

The keyboard stuff has been coming along nicely, too. The ON key can now be treated like any other key, with event handlers, etc. There are also 4 additional routines: glReadKey, glWaitKey, glReadChar and glWaitChar.

The difference between the glXKey and glXChar routines is that the former returns the key scancode (including a new one I made up for the ON key ;) ), and the latter returns the key mapped to a corresponding ASCII character, if their is one. So if you pressed the minus key, glReadKey would give you GL_KEY_MINUS, while glReadChar would give you "-" or "W" depending on a mode flag.

"Colours" have also been implemented. The filled drawing routines can take a colour value from 0 - 255 and map it to a set of built in dithering patterns. I like this idea because it gives me the option to make an attempt at Gouraud shading for polygons, and also try and do something funky with grayscale (and thanks, tr1p, for that excellent description of grayscale in the programming help forum!).

Hopefully by next weekend I can get the rest of the code working in App form and then I can take a look at these more interesting things.
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
User avatar
benryves
Maxcoderz Staff
Posts: 3078
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Staff][Dev] GL 2D Game Engine

Post by benryves »

Very nice! What are your RAM requirements?
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

benryves wrote:Very nice! What are your RAM requirements?
It's just over 1kb of dynamic memory at the moment, plus appBackUpScreen for static variables/interrupt handler. If I keep the texture mapping the way it is, it will also need 512 bytes per texture for lookup tables.
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
ninja_master
Regular Member
Posts: 76
Joined: Sun 13 Nov, 2005 2:42 am
Location: A cold cold land filled with snow
Contact:

Re: [Staff][Dev] GL 2D Game Engine

Post by ninja_master »

This looks great, I can't wait to see a demo of the actual game but it might be a little early for that. :)
Wright Studios.org the place for calculator graphics.
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

ninja_master wrote:This looks great, I can't wait to see a demo of the actual game but it might be a little early for that. :)
Just a little :)

Actually, I've spent the last couple of weeks working on some actual game code. It's been quite fun because I had this stupid idea of keeping everything to scale - so the moon has to be 384,400,000 meters from the Earth, which means I needed some bloody big numbers!

The main thrust over the past week has been on the physics code. I managed to some Newtonian integration working (at least I think it works, but there are problems). The equations needed are:

v1 = v0 + a0 * T
p1 = p0 + v0 * T + a0 * 0.5 * T * T

Where p0, v0 and a0 are the previous position, velocity and acceleration; p1 and v1 are the new position and velocity; and T is time.

I'm using fixed point math - 6.10 for acceleration, 22.10 for velocity and 30.18 for position. In addition to that, the Mass of the Earth * gravitational constant needs to be stored as a 64-bit integer and divided by another 64-bit number (distance squared) at run-time to calculate gravitational acceleration! At least they are both unsigned.

The good news is that I wrote a "big integer" division routine which can divide any two integers from 1 to 256 bytes in length. I should probably incorporate that into the library, along with some other big integer code.

There is a bug somewhere which is causing orbits to slowly spiral outwards. I'll need to investigate that this weekend. In the meantime, here are two screens showing 1) a circular orbit around the (invisible) Earth and 2) a weird precessing elliptical orbit.

Image

Image
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
User avatar
benryves
Maxcoderz Staff
Posts: 3078
Joined: Thu 16 Dec, 2004 10:06 pm
Location: Croydon, England
Contact:

Re: [Staff][Dev] GL 2D Game Engine

Post by benryves »

Very impressive, as ever. I'm not sure if there are all that many games with impressive physics (Acelgoyobis springs to mind) so it's an interesting direction to go in. Despite the size of these "bloody big" numbers, it seems you're still getting decent speed!
qarnos wrote:The main thrust over the past week has been on the physics code.
Heh :P
User avatar
qarnos
Maxcoderz Staff
Posts: 227
Joined: Thu 01 Dec, 2005 9:04 am
Location: Melbourne, Australia

Re: [Staff][Dev] GL 2D Game Engine

Post by qarnos »

benryves wrote:Very impressive, as ever. I'm not sure if there are all that many games with impressive physics (Acelgoyobis springs to mind) so it's an interesting direction to go in. Despite the size of these "bloody big" numbers, it seems you're still getting decent speed!
qarnos wrote:The main thrust over the past week has been on the physics code.
Heh :P
Unintentional puns are the best ones! :P

I just checked out Acelgoyobis on ticalc. I love pinball games, so I'll have to give it a try!

The speed is OKish, but it's not doing a lot graphics-wise. Once I know the acceleration, the integration only takes 9000cc, so I'm happy with that, but the 64-bit division required to calculate the acceleration is a killer: ~55000cc

I could certainly get that down. The G acceleration will hardly change from frame to frame, so I could just calculate it every now-and-then, or even try to integrate it along with everything else. There's lots of opportunities, but I'm not going to follow them until I work out what's causing the spiralling-out problem.

Here's the thing: If I hard-code the acceleration at the value I know it's supposed to be, it all works a lot better - there's still a little spiralling, but nowhere near as much. But when I calculate it, it goes awry. But the G calculation is the most accurate part of the whole thing! It doesn't make much sense. Even stepping through it in the debugger and following the calculations manually, it's getting the right answers.

I have a feeling there might be some kind of problem with rounding signed numbers. It could be that the errors on the negative side are biased one way, and biased the other on the positive side. Anyway, I hope I can get it figured out, because I want to try to get that 64-bit division out of there.
"I don't know why a refrigerator is now involved, but put that aside for now". - Jim e on unitedti.org

avatar courtesy of driesguldolf.
graphmastur
New Member
Posts: 3
Joined: Sat 25 Jul, 2009 3:29 pm

Re: [Staff][Dev] GL 2D Game Engine

Post by graphmastur »

I think that this project is amazing!!! Do you have a current release?
Post Reply