2D OpenGL library

BlitzMax Forums/OpenGL Module/2D OpenGL library

EOF(Posted 2010) [#1]
I have a modified version of a compact OpenGL 2D library (based on code slenkar posted) but would like your thoughts on what a 'basic/core' 2D library should consist of


Here are my key goals:

1. Fast, small, cross-platform library

2. Mainly OOP- No hand-holding wrapped functions except for the common stuff like SetColor, Cls, Flip

3. Compact. Offering the raw stuff such as image handling (including animation), 2D drawing, fonts **

4. Minimal coding. For example, you can omit parameters in many commands (which will reset to defaults)

5. Images have their own scale, rotation, and handle values

6. Screen can be scaled (zoomed), rotated, and have its offsets changed. This effects all 2D drawing and images rendered

7. Only support for PNG and JPG (to keep module size small)


** Fonts - For this one, to keep the library tight, a simple DrawText is included (no fancy effects) as well as a separate texture/quad based font handler



WHAT'S IMPLEMENTED NOW
====================
1. GLOBAL functions
---------------------------
Graphics(w,h,FullScreen=False,FPS=30)  -- create main display
Cls                                                            --- Clear screen
Flip                                                   -- flip screen (note FPS set by Graphics command)
SetColor() , SetClsColor()             --- global for all 2D drawing/images
SetAlpha() , SetBlend()                      --- global for all 2D drawing/images


2. DISPLAY functions  (under GL Type)
---------------------------
SetOrigin() , SetScale() , SetRotation()  ---  global display transformations
StoreColor() / RestoreColor() -- quick way to snapshot/restore color
ResetDisplay()  -- resets scaling, rotation, origin, color, alpha, blend

3. 2D Drawing (currently global procedural functions)
-------------------------
Plot , DrawLine
DrawOval , DrawPoly , DrawRect --- All having filled / unfilled options
DrawText (default OpenGL version. No fancy stuff. Ideal for degugging)
SetLineWidth()

4. IMAGES (TImage)
-------------------------
:Load(url$,filtered=False) -- Unfiltered or filtered (bilinear)
.Copy() --- copies existing image but sharing same texture
.Grab(x,y) -- grabs screen at x,y into image (fast)
.Draw(x#,y#,frame=0) -- draw an image at x,y
.SetRotation(angle#=0) -- set images rotation
.SetScale(sx#=0,sy#=0) -- set imges scale
.SetSize(w,h) --- set images actual pixel size
.SetFrames(col,row) -- determine animation frames
.SetFrameRect(x#,y#,w#,h#) -- set a rectangular area for rendering
.MidHandle()  -- sets the images handle to central postion
.SetHandle(x#=0,y#=0)  -- Sets images handle to specific location

5. PACKED IMAGES  - Single-Surface type (TPack)
-------------------------
:LoadPack(filename$)   -- loads an image pack (one large image+ini file)
.GetImage(name$)    -- Returns a new TImage (although sharing same texture as stored in the pack)
.CountImages()    -- returns the number of images in the pack

6. FONTS  (TFont)
-------------------------
The font set is generated by a separate tool, which simply renders the
characters into an image, saving this image alongside a *.ini file which 
determines where each character is within the image

:Load(url$,filtered=False) -- Unfiltered or filtered (bilinear)
.DrawText(t$,x,y) --- draws the custom text
.SetScale(sx#=0,sy#=0)   --- scaling for the text
.SetRotation(ang#)  -- rotate the text (from top/left anchor point)



Some examples:

LOAD IMAGE



For animation the concept is:

1. Load a standard image
2. Set the frames via SetFrames() which can set either the number of frames in a row or grid layout (all having the same size)
3. Animate by using optional parameter in image.Draw() method

ANIMATION



GW(Posted 2010) [#2]
what's the advantages over Max2d?

Will it be single surface?
Spritemaster-pro was an amazing piece of work. I've always wished there was a Bmax compatable version.


EOF(Posted 2010) [#3]
Advantage on Max2D:

1. Smaller footprint
2. Faster rendering, faster GrabImage() <-- no YFlipping done
3. Screen zoom and rotation
4. Filled and unfilled rects, polys, ovals

As an example of a speed gains you can get closer to OpenGL's heart by calling raw OpenGL calls:
' standard
For n=1 to 1000
   Plot x,y
Next

' speed up
glBegin GL_POINTS
For n=1 To 1000
  glVertex2f x,y
Next
glEnd


Single surface can be done at the moment by loading an image, copying it, then setting the rectangular area of the image. Copied images share the same texture. Example:
Global spriteset:TImage=TImage.Load("pack1.png")

Local fish:TImage=spriteset.Copy()
fish.SetFrameRect 5,5,64,40

Local cat:TImage=spriteset.Copy()
cat.SetFrameRect 74,50,32,26


I might add commands to simplify this whole process:
Global spriteset:TImage=TImage.LoadPack("pack1.png")

Local fish:TImage=TImage.FromPack("fish")

Local cat:TImage=TImage.FromPack("cat")



jkrankie(Posted 2010) [#4]
looks good, especially the single surface stuff :) It's similar to what i've been using for Scoregasm!

Cheers
Charlie


EOF(Posted 2010) [#5]
Thanks Charlie. What method of you using for fonts?
My bare-bones font system is ok except that rotation is not currently supported. Well, you CAN rotate the text but each letter rotates individually

FONT EXAMPLE
http://www.youtube.com/watch?v=U5wTp5zf9p8


2D RENDERING - Rotation, scaling, 2d drawing
http://www.youtube.com/watch?v=zVUoe73P17A


jkrankie(Posted 2010) [#6]
Nothing yet. Fonts are on my todo list. I'll have to have a look and see what you've come up with, i'm actually ok with letters rotating individually.

Cheers
Charlie


Jesse(Posted 2010) [#7]
@Jim
How are you handling rotation and scaling? I might be able to help you with the text rotation.


EOF(Posted 2010) [#8]
Jesse,
Rotation/scaling is done via the glRotatef and glScalef
Each font letter is simply a set of quads drawn within a glBegin(GL_QUADS) .. glEnd() block. The verts point to a particular rect area within the image


Jesse(Posted 2010) [#9]
I hope the code below give you an idea on how to do it. It's not in GL but in can easily be adopted, I think. if you can't figure it out I might need to look at your source code to implement it.
SuperStrict
Graphics 800,600
SetBlend alphablend
AutoMidHandle True
Local angle:Float = 0
Local scalex:Float = 2.0
Local scaley:Float = 2.0
Local text:String = "center spin text"
Local mX:Float = Float(TextWidth(text))/2.0 'center text x
Local mY:Float = Float(TextHeight(text))/2.0 ' center text y

SetScale scalex,scaley

Repeat
	Cls()

	Local  s:Float = Sin(angle)
	Local  c:Float = Cos(angle)
	' transform variables for scale and rotation
	Local ix:Float = c * scalex
	Local iy:Float =-s * scaley
	Local jx:Float = s * scalex
	Local jy:Float = c * scaley
	' actual reposition of the x and y for rotating in center of text.
	Local x:Float = 300.0 - (mX * ix + mY * iy)
	Local y:Float = 300.0 - (mX * jx + mY * jy)

	SetRotation angle

	For Local n:Int = 0 Until text.length
		Local s:String = Chr(text[n])
		DrawText s,x,y
		Local w:Float = TextWidth(s)
		x :+ w * ix
		y :+ w * jx
	Next
	angle :+ .1
	Flip()
Until KeyDown(key_escape)




EOF(Posted 2010) [#10]
Thanks Jesse. It's the kind of thing I had in mind. Text rotation is in now although all rotation is currently done from the top/left point

One nice thing, multi-line text is done in one line. The "/n" means "new line"
msg$="Example of rotated text/nusing font.SetRotation()/nAngle="+String(ang)

fnt.SetRotation ang
fnt.DrawText msg,240,120




Space_guy(Posted 2010) [#11]
How come you dont use ~n for newline like blitzmax itself?


EOF(Posted 2010) [#12]
I have switched back to using '~n' now. Makes sense


Space_guy(Posted 2010) [#13]
:)


EOF(Posted 2010) [#14]
Single-Surface image handling is in now
A texture can hold a set of regular images as well as animated ones
Images pulled from the 'pack' are automatically set up to animate if they have already been defined with frames

The basics are:



Chroma(Posted 2010) [#15]
Hi Jim...is this a complete replacement for Max2D and can do pretty much everything it can do identically?


EOF(Posted 2010) [#16]
Pretty much everything except for collision checking and the virtual screen/mouse functions. For collision checking I think a basic RectsOverlap() type of function will be enough for now
The pixel-perfect checking needs some thought really

The goal here is to keep the library small, but having enough 'get up and go' functionality without going overboard


Nigel Brown(Posted 2011) [#17]
Is this library available somewhere to try?


Schillie(Posted 2011) [#18]
The library sounds great, I'd really like to give it a try as well. Are you still planning to release it at some point I hope?


shinkiro1(Posted 2011) [#19]
I would be interested too.


Armitage 1982(Posted 2012) [#20]
Does this library still in progress ?

Would be nice to get a little bit more rendering speed when using Max2D.
I already replaced a few drawing functions in plain OpenGL but I wouldn't mind using a whole new API...


EOF(Posted 2012) [#21]
Unfortunately I lost the source code to this particular project


Armitage 1982(Posted 2012) [#22]
Unfortunately I lost the source code to this particular project

Too bad :/

I will stick to Max2D and continue to improve the slow part then.