Understanding GameEngine Widgets
GameWgt Data
Derived Classes
Widget Macros
Method Summary
Useful Access
Screen Access
Others
The Itty Bitty GameEngine -- Overview
Your Own Java Game -- Step by step to build a simple "Pong" game
Converting Your Game to JavaScript -- What you need to know to run your game in a web browser
Class GameWgt -- The visual components of a GameEngine game (You are here)
Overriding GameEvent -- The programmatic components of a GameEngine game
Mostly you will not be creating new widgets on the fly in your game, but rather you will define them in GameMaker (see "The Itty Bitty GameEngine"), and the GameEngine will set them up at the start of your game. Thereafter all you need to do is move them around (or change the velocity of the widgets that are defined to be already moving) and decide what to do if they bump into each other, and maybe change their size or color (if your game needs to do that). If you do need to create new widgets, you must be careful to connect them up correctly to the existing widget hierarchy, or they will not be visible or behave properly.
A widget has ten pieces of data that define its basic parameters for drawing: position, size and color, and some additional parameters for arranging its relationship with other widgets (parent and child widgets), plus some information particular to each widget. There are getter and setter methods for accessing these data, which you should try to use when you need to know or change the data during your game's play. The data elements are described next and the accessor methods later, in the Method Summary for this class.
The GameEngine itself uses some specific widgets to facilitate the game play; these are noted where relevant.
Two defined widget types are incomplete. Sprites
mostly work, but there aren't any tools for giving them their full functionality.
You can change a "Ball" tool diameter, but adding other shapes are not
supported yet. The scrollbar is not working at all at this time. We hope
to have some of these limitations working before the summer is over.
Name Type ... RefNum int This the widget's unique reference number Gtype int The widget's type, currently between 0..7 (see Derived Classes) Gcolor int The widget's color, either 0x00RRGGBB or else -1 if none Gtop int The vertical position of the top pixel in this widget, relative to the top-left corner of its parent Gleft int The horizontal position of the top pixel in this widget, relative to the top-left corner of its parent Gtall int The widget's height in pixels Gwide int The widget's width in pixels Ginfo int The value of this item differs for different widget types. See the Derived Classes for defined specifics. Parent int The RefNum of the widget which includes this widget's RefNum in its Gdata, or else =0 Gdata int[] An optional array of integers, usually RefNums of child widgets, but for some widget types they are pixels or text (see the specific Derived Classes) Gselim int (Only in GameTxLn and GameScro widgets) another internal data value, a selection point in GameTxLn, and scroll limits in GameScro widgets.
Class Name Gtype Function... Ginfo... Gdata... GameWgt 0 Plain rectangle (none) GameWgt 1 Pixel map (only used for whole game board at this time) Child The pixels GameIcon 2 Single color shape up to 32 pixels tall and 64 or more pixels wide Animation The pixels GameFont 3 Character shapes of a font (drawn as if a single frame of GameIcon) Selection The pixels GameTxLn 4 A line of text Selection The text GameWgt 5 Group head over multiple child widgets (ID) The children GameSprit 6 Group head and velocity over the color layers of an animated sprite Velocity The children GameScro 7 Group head over scrolled panel and scrollbars (TBD) Scroll limits The children
No separate subclass is needed for types 1 and 5, because all of their methods make sense in the base class (except that the Gdata array is nominally null in the base class, but vigorously used in these other two types.
Drawing the widgets on the screen proceeds hierarchically from the game root pixel-map to the "[List of item lists]" it links to, and then recursively through each subwidget listed in the list's Gdata. This recursion stops for any widget that has no Parent, or that is outside its Parent rectangle, or whose Parent does not list it as a child. The parent's rectangle (if it has any color) is drawn first, then the child widgets on top of that background. Widgets not connected in this hierarchy are not drawn.
Collisions are tested only for sprites (GameSprit) that are
listed as children of the first child in the "[List of item lists]"
list, which is identified as "[Sprite list]" in the input widget
list.
Then the icons are defined to be animated. The Ginfo value of this widget type is four numbers packed into a single 32-bit ingeter, as follows (reading high to low):
6 bits, The fractional frame rate, a fixed-point number with 4 fraction bits, that is, you should think of the 6-bit number as being divided by 16, so that a (binary) value 011000 (decimal 24) is actually 1.5 animation steps per frame. An internal counter counts the factional animation steps, and each time it crosses into the next integer value (like increasing from 1.99 to 2.00) the next step in the animation occurs. This way the animation can be as slow as once every 16 frames (about 1.6 seconds at the default frame rate of 10fps), or almost four steps per frame. Of course anything faster than one step per frame just skips animation steps. A zero fractional frame rate is not animated.6 bits, the number of frames -1, so that zero represents one frame, and the maximum (binary) value 111111 (decimal 63) represents 64 frames in the animation loop. Zero (only one frame) is the same as not animated.
6 bits, the step size in Gdata, the number of integers to skip over when advancing the next animation step. Normally this is equal to the actual width of the icon, so that each animation step advances to the next full icon image in the array, but that is not a requirement. For example the rectangular "marching ants" icon consists of diagonal bars four pixels wide, and the animation step is one pixel, which in the case of a 1-pixel slice either vertical or horizontal, the ants appear to have shifted one pixel down or right. Zero step size is the same as not animated.
14 bits, an offset into the Gdata array, where the current pixels for this frame are displayed from. If you use all 64 steps in your animation, you are limited to images 8 pixels wide, or basically you have a maximum number of steps as 16384 divided by your icon width. These are icons (parts of sprites), not whole movies. A zero fractional frame rate or step size is not animated, but you can use different offsets to adjust the appearance of your icon.
The animation mechanism proceeds one calculation every display frame (icons that are off screen do not animate). The fractional frame rate is added to the internal counter for this icon, and if the result is one or more, the step size is subtracted from the current offset, and if that goes negative, it is restarted at the (number of frames -1) times the step size. If that is beyond the end of the array, then nothing is drawn. Checkboxes use this feature (with a fractional frame rate =0) to either show the checkmark or not.
You normally do not need to be concerned with the mechanics of animation, this is taken care of for you -- unless you want to do something tricky
The first release of GameEngine supports only
one icon, a solid circle in four diameters. Real
Soon Now we hope to include an icon editor for you to make icons of your
own choosing, and a full range of circles from 4-32 pixels in diameter.
When you Build your game, only the fonts actually used in your game will be loaded into the widget list. If you plan to change the font on the fly, be sure you have at least one (possibly invisible) GameTxLn that uses each desired font at Build time, or it will not be loaded into your game.
The pixel tables for the seven predefined fonts are included near the front of the JavaGame.java source code. The pixel table for a font has four header numbers, then (for proportional fonts) a character index, or else two more header numbers, followed by the glyph pixels in ASCII order. The first header number is a 4-letter name similar to the ID value that some group widgets use. The second number packs in three bytes (from the bottom up) the font ascent = the number of pixels from the base line to the top of the tallest character, then the descent = the number of pixels below the base line to the bottom of the deepest descender, then the line height in the upper half, which is usually the sum of the ascent+descent. The third number is the letter spacing in pixels, and the fourth is the width of the space character (which is the same for all the characters of the monospace font).
The proportional font glyph index entries are offsets into the pixels following, each the offset of the left edge of that glyph from the front of the table. Thus the character widths are the differences between index offsets of successive glyphs. The index is the offset (from the front of the array) of the leftmost pixels of the space character (which is always zero width), then continuing to the tilde '~' and ending with the table size = the right edge of the tilde. The index is always 96 entries, so the pixels of a proportional font always start at offset 100.
A monospaced font has the same four header numbers, then two more, the
second giving the (fixed) character width, followed by the pixels starting
at offset +6 (no index needed).
The Gdata array of a sprite normally consists of layers of icons and rectangles and text items drawn in sequence on top of each other. Each layer has its own color, so a multi-colored sprite can be made up of several layers in their respective colors.
The first release of GameEngine supports only one sprite widget, a moving
ball that you can set the velocity and diameter of. Real
Soon Now we hope to include a full sprite editor so you can build up sprites
from arbitrary icons and other widget types.
These 26 methods set widget data, or do some kind of output:
AddPrntsPos GetInfo GetRtVelo GetTextWide FindElemt GetIxData GetSelim GetType GetColor GetParent GetSize GetVposn GetDnVelo GetPosn GetTall GetWide GetHposn GetRefNum GetTextLn IsVisible
These three methods drive the screen pixel array:
SetMyScreen PokePixel RectFill
int AddPrntsPos(int here)Converts a packed position of this widget into an absolute window offset. The parameter here might come from the widget's GetPosn to get its absolute position in the window.
Parameters here The widget-relative offset (V<<16)+H in the parent widget Returns The packed window-relative offset (V<<16)+H
int FindElemt(int whom)Search for a value in this widget's Gdata array. Returns the index position of the first such item found, or -1 if not there. You can use this method to find an element to delete, then pass its returned index to DeleElemt to delete it.
Parameters whom the value to look for in Gdata Returns the index position of the first such item found, or -1
int GetColor()Returns the specified color for this widget, = 0x00RRGGBB or -1 if none.
Returns The specified color = Gcolor
int GetInfo()Returns the information value for this widget. The meaning of this value is specific to different widget types, as shown in the subclass table.
Returns The information value = Ginfo
int GetIxData(int here, int defalt)Safely (no exceptions) returns an indexed value from this widget's Gdata, or else a specified default value if it doesn't exist.
Parameters here The index defalt The value to return if Gdata[here] does not exist Returns The indexed value = Gdata[here], or else defalt if the Gdata does not exist, or here is outside its boundaries Use an index here=-1 and a default value also defalt=-1 to safely get the array size, which will be >0 if the array exists, otherwise =-1.
int GetParent()Returns the reference number of this widget's parent widget, or =0 if none is specified.
Returns The Parent
int GetPosn()Returns the position of this widget in its parent, packed into one integer, vertical into the high half, horizontal in the low half. Use (posn>>16) to extract the vertical component, and SignExtend(posn) extract the horizontal component.
Returns The position = (Gtop<<16)+Gleft
int GetRefNum()Returns the unique reference number for this widget = its index in AllWgts[].
Returns The reference number = RefNum
int GetSelim()Returns the selection of a GameTxLn or scroll limits of a GameScro, otherwise 0. This method is defined in the base class so you don't need to bother with casting the GameWgt as a GameTxLn or GameScro when the override mechanism automatically does that for you.
Returns The selection or scroll limits = Gselim
int GetSize()Returns the size of this widget's containing rectangle, packed into one integer, vertical into the high half, horizontal in the low half. Use (size>>16) to extract the vertical component, and (size&0xFFFF) extract the horizontal component.
Returns The size = (Gtall<<16)+Gwide
String GetTextLn()Returns the text line from a GameTxLn widget, otherwise the empty string "" (if this is called for some other widget type).
Returns The text line = Gdata as a String
int GetTextWide()Returns the calculated pixel width of a GameTxLn widget's text, otherwise 0 (if this is called for some other widget type).
Returns The width of the whole text line
int GetType()Returns the type of this widget with its flags.
Returns The type = Gtype
boolean IsVisible()Return true if this widget's rectangle is at least partly on-screen. If the widget is not visible for other reasons, IsVisible might return a false positive.
Returns true if the widget's rectangle is at least partly on-screen
int AddElemt(int whom)Insert a new value at the end of (the non-zero values in) this widget's Gdata. Returns the index position where it was inserted, or -1 if it failed. If Gdata is too small, it will be enlarged, but if it is null, AddElemt will fail. If it is already there, AddElemt simply returns its index (same as FindElemt).
Parameters whom the value to look for in Gdata Returns the index position of the first such item found, or -1
boolean CheckIt(boolean tog)If this is a component of a checkbox created by the checkbox macro, CheckIt (optionally) toggles the checkmark and returns the new setting. This works for the containing group (which CheckIt searches its subwidgets for the actual checkmark icon) or for the checkmark icon itself, which is recognized by having the same first element as the default checkmark array. The checkmark is hidden by setting the pixel offset past the end of the actual checkmark pixels in its array.
Parameters tog true toggles the checkmark, false leaves it unchanged Returns true if the checkmark is showing, false if not
void DeleElemt(int inx)Delete a value in this widget's Gdata array given its index. The remaining items are shifted down to fill the gap, with zero-fill at the end. It is assumed that all the non-zero data is at the front, so a run of zeroes terminates the shifting down.
Parameters inx the index position of the item to delete
void DrawMe(int top, int left, int tall, int wide)This draws the default widget on the window. Some subclasses override this to alter the appearance, or to draw some completely different representation of the data. The boundaries of a masking rectangle limit the drawing to (usually) the containing widget's dimensions.
Parameters top do not draw above this position left do not draw to the left of this position tall do not draw below this position +top wide do not draw to the right of this position +left The DrawMe method explicitly omits drawing any widget (and its child widgets) if it is outside the specified drawing rectangle, or if its parent hierarchy is incorrect.
void HideSho(boolean sho)Hide or show this widget by moving it off- or on-screen. This is done by adding or removing the 0x4000 bit in its vertical position, so if the widget is not visible for other reasons, calling HideSho(true) probably won't make it visible.
Parameters sho true to show the widget, false to hide it
void LogMe(String before)This prints a widget's display line in the console log.
Parameters before a prefix to add to the front of the line
int PutTextLn(String aLine)Set a GameTxLn's text in its Gdata array. This method is defined in the base class so you don't need to bother with casting the GameWgt as a GameTxLn when the override mechanism automatically does that for you.
Parameters aLine The text to set Gdata to
int RadioBtn(int whom)If this is a component of a radio button created by the radio button macro, RadioBtn either selects a new setting or returns the current setting. It is expected that this will be called for the containing group, and RadioBtn searches its subwidgets to find the one to select, and to deselect the others, by setting each icon widget to either show or hide the selection button. The icon widgets are recognized by having the same second element as the default radio button array.
Parameters whom the code to be set, 33..126, or else -1 to query Returns the current setting code, 33..126 Radio buttons are distinguished within a group by a single character (preferably a digit or letter) embedded in their information ID. If you call RadioBtn with a negative parameter, it will return the current setting character. If you call RadioBtn with (the ASCII value of) a character greater than space (and not greater than '~', that is 33..126) then it will search for the button with that ID and (if found) set it to selected, and all the others to deselected. If you call RadioBtn with a number 0..31, then it selects whichever button has the character '0'..'9' which is that single digit, or else the letter 'A'..'U' corresponding to the values 10..31 determined by adding 55 to the number given. If that's too confusing, just use the letters.
Internally, the icons show a hollow circle for deselected, and (later in the same Gdata array) a button image for selected, the offset into the array determines which is shown. Each button is a group over a white solid circle icon and the black selection button icon. The button group ID is the 4-character code "Rd_*" where the '*' is replaced by the selector character. The group ID over the set of radio buttons linked together is "RdB*" where again the '*' is replaced by whatever code has been selected, so that asking for the current setting requires no search. The searches and settings are recursive through RadioBtn.
void SetBlinker(boolean pix, int whar)Set the cursor in a GameTxLn's text, either as a pixel offset, or else as calculated from a character offset. This method is defined in the base class so you don't need to bother with casting the GameWgt as a GameTxLn when the override mechanism automatically does that for you.
Parameters pix true if the cursor position is a pixel offset whar The new cursor position as a pixel or character offset
void SetColor(int color)Set the Gcolor for this widget, = 0x00RRGGBB or -1 if none.
Parameters color The color to set Gcolor to, <0 is no color
void SetFlags(int info)Set the flag bits in this widget's Gtype. The type itself is unchanged. The flag bits are masked to 0x0000FF00. You can read out the current flag bits using GetType(). These are the defined bits:
Parameters info The flag bits to set Gtype to +256: Collidable ("Bump"), must be set to get Collided events+512: Type-able ("Key"), must be set to get any keyboard events
+1024: Clickable ("Click"), must be set to get ClickEvt events
+2048: Dragable ("Drag"), must be set to get MouseDn, MouseUp, or any drag events
+4096: Roll-able ("Roll"), must be set to get any rollover events
+8192: Refuse focus on this widget
boolean SetFocus(boolean nuly, int vert, int horz)Give this widget focus, or else take focus away. A widget that is not typeable, clickable, or dragable cannot accept focus. A GameScro widget never accepts focus. A GameTxLn widget uses the coordinates to set the insertion point (blinking cursor) if it can accept keystrokes.
Parameters nuly true to give focus, false to take it vert the vertical position of a mouse-click giving focus horz the horizontal position of the mouse-click Returns true if focus accepted, false if can't or losing it
void SetInfo(int info)Set the information value of this widget's Ginfo, and clear the animation bits in Gtype. The animated widgets GameIcon and GameSprit use the upper half of Gtype to count partial frame rates, but changing Ginfo could invalidate current animation parameters, so clearing those bits forces a restart at its beginning.
Parameters info The information value to set Ginfo to
void SetParent(int whom)Set the Parent for this widget. It should be a valid RefNum of some other widget, or else zero.
Parameters whom The RefNum to set Parent to, 0 is none
void SetPosn(int top, int left)Set the position of the top-left corner of this widget's containing rectangle. If you specify left=0 and top is a packed position (such as returned from GetPosn) but >= 0x10000, then it will use that packed position. If you want to use this shortcut to set a position that might be on the top row of its parent, you can pass it (posn|0x20000000) and the extra high bit will be removed on the way in.
Parameters top the new top left the new left edge
void SetSelim(int whar)Set the selection of a of a GameTxLn or scroll limits of a GameScro. This method is defined in the base class so you don't need to bother with casting the GameWgt as a GameTxLn or GameScro when the override mechanism automatically does that for you.
Parameters whar The new selection or scroll limits for Gselim
void SetSize(int tall, int wide)Set the dimensions of this widget's containing rectangle. The specified dimensions must be >0, but if you specify tall=0, it will preserve the previous height and change only the width. If you specify wide=0 and tall is a packed size (such as returned from GetSize) then it will use that packed size. The size of the game window (that is, if RefNum=0 in this widget) are also captured locally for future use.
Parameters tall the new height wide the new width
void SetWgType(int info)Set the of this widget, but only if previously undefined = -1. This can only be called once, when the widget is created, otherwise it has no effect.
Parameters info The type number to set Gtype to
int[] SwapData(int[] data)Exchange this widget's Gdata array for a new array. To capture a reference to the array without changing it, call the method twice:
Parameters data the new array for Gdata Returns the array previously in Gdata data = SwapData(null); // gets data, replaces it with null
if (SwapData(data)==null) {} // puts data back, ignores returned null
void XchElemt(int inx)Exchange two adjacent values in this widget's Gdata array given the index. The remaining items are not affected. Zeroes can only be moved up, not down.
Parameters inx the index position of the first of the two items
double GetDnVelo()If this is a sprite, returns its vertical velocity (positive is down) as a (double) number of pixels per frame.
Returns The high 16 bits of the information value Ginfo
double GetRtVelo()If this is a sprite, returns its horizontal velocity (negative is left) as a (double) number of pixels per frame.
Returns The low 16 bits of the information value Ginfo
void SetDnVelo(double velo)If this is a sprite, sets its vertical velocity (positive is down) as a (double) number of pixels per frame. The internal representation is fixed-point, so any velocity smaller than 0.0025 pixels per frame is effectively zero, and larger than 127.99 is clipped to that value.
Parameters velo the new vertical velocity
void SetRtVelo(double velo)If this is a sprite, sets its horizontal velocity (negative is left) as a (double) number of pixels per frame. The internal representation is fixed-point, so any velocity smaller than 0.0025 pixels per frame is effectively zero, and larger than 127.99 is clipped to that value.
Parameters velo the new horizontal velocity
void SetHposn(int horz)Sets the horizontal position of this widget = its leftmost pixel column.
Parameters horz the new horizontal position
void SetVposn(int vert)Sets the vertical position of this widget = its top pixel row.
Parameters vert the new vertical position
int GetHposn()Returns the horizontal position of this widget = its leftmost pixel column.
Returns The horizontal position = Gleft
int GetVposn()Returns the vertical position of this widget = its top pixel row.
Returns The vertical position = Gtop
int GetTall()Returns the height of this widget.
Returns The height = Gtall
int GetWide()Returns the width of this widget.
Returns The width = Gwide
void SetTall(int tall)Sets the height of this widget.
Parameters tall the new height, if greater than 0
void SetWide(int wide)Sets the width of this widget.
Parameters wide the new width, if greater than 0
Java has defined a variety of high-level drawing methods, and in most
Java programs you should use them. However, because this is a beginning
programming course, and because some people -- we hope that includes you
-- want to see how the underlying mechanism works, I have exposed all the
drawing machinery for your inspection and enlightenment.
static int[] GetMyScreen()Gets the graphics pixel buffer as an integer array (to save & restore). Not used in GameEngine.
Returns The current pixel array
static int GetScreenDims()Gets the graphics pixel buffer dimensions packed into a single integer (to save & restore). Not used in GameEngine.
Returns Packed integer, tall<<16 + wide
static void PokePixel(int colo, int rx, int cx)Sets the pixel at the specified row and column to the given color.
Parameters colo The color to go in that position, = 0x00RRGGBB rx The pixel row cx The pixel column
static void RectFill(int colo, int rx, int cx, int rz, int cz)Sets a whole rectangle of pixels to a single color, from one corner of the rectangle in pixel coordinates, to the opposite corner. To draw a horizontal line, use the same value for rx=rz; to draw a vertical line, use the same value for cx=cz.
Parameters colo The pixel color to fill that rectangle, = 0x00RRGGBB rx The pixel row in one corner cx The pixel column in the same corner rz The pixel row in the other corner (inclusive) cz The pixel column there
static void SetMyScreen(int[] theAry, int tall, int wide)Sets the graphics pixel buffer to the client's array, and saves the dimensions for range-testing and index calculation in the drawing methods.
Parameters theAry The pixel array, at least tall*wide in length tall The number of pixel rows wide The number of pixel columns
String FixHaff(String before, int whom)This is an internal utility for formatting a fixed-point animation value as a string for display purposes.
Parameters before a prefix to add to the front whom the value with an 8-bit fractional part Returns the composite display value
String FmtFront(String before, boolean dimz, String after)This is an internal utility for formatting the front part of a widget's display line in LogMe. The prefix and suffix make building display lines simpler.
Parameters before a prefix to add to the front dimz true to show also the widget's size after a suffix to add to the end Returns the composite display value
String FmtInfo(String before, int tye)This is an internal utility for formatting a widget's information value Ginfo. Some subclasses override this to get different formatting.
Parameters before a prefix to add to the front tye the widget's type Returns the composite display value
int GetAnimBits()Returns the animation bits stored in the upper half of Gtype.
Returns The animation bits = Gtype>>16
String GetIDtxt()Return the information value Ginfo of this widget as a String, or else "" if not a valid string.
Returns The 3- or 4-character ID code
boolean IsValidIDtxt()Test the information value Ginfo of this widget and return true if it is a credible 3- or 4-character ID code, false if not. 3-character ID codes are not used in JavaGame, but if they were, they must begin with a letter. 4-character ID codes must be all ASCII characters in the range '!'..'~'. JavaGame uses only letters.
Returns true if this has a credible 3- or 4-character ID code
String toStrMash(String msg, String whom)This is an internal utility for generating the result that a toString call to a subclass of GameWgt returns. If the parameter msg contains a formatted RefNum, this replaces its prefix; otherwise it returns the stated prefix with the widget's RefNum.
Parameters msg a partial result to find the RefNum in whom a prefix to replace the front of msg, or else alone with RefNum Returns the composite display value
String toString()Return the class name and RefNum for this widget, plus its ID code if any.
Returns The reference String for this widget
Rev. 2020 November 14