1 // This file is part of Golly.
2 // See docs/License.html for the copyright notice.
3 
4 #ifndef _WXOVERLAY_H_
5 #define _WXOVERLAY_H_
6 
7 #include <string>                   // for std::string
8 #ifdef _MSC_VER
9     #pragma warning(disable:4702)   // disable "unreachable code" warnings from MSVC
10 #endif
11 #include <map>                      // for std::map
12 #ifdef _MSC_VER
13     #pragma warning(default:4702)   // enable "unreachable code" warnings
14 #endif
15 
16 #ifdef ENABLE_SOUND
17 #include <irrKlang.h>               // for sound
18 using namespace irrklang;
19 #endif
20 
21 #include "lua.hpp"
22 
23 // The overlay is a scriptable graphics layer that is (optionally) drawn
24 // on top of Golly's current layer.  See Help/overlay.html for details.
25 
26 // The overlay can be displayed at any corner or in the middle:
27 typedef enum {
28     topleft, topright, bottomright, bottomleft, middle
29 } overlay_position;
30 
31 // Multi-line text can be left or right justified or centered:
32 typedef enum {
33     left, right, center
34 } text_alignment;
35 
36 // Clips have a row index to optimize rendering.
37 typedef enum {
38     mixed = 0,      // row contains at least one pixel that needs alpha blending
39     alpha0,         // row contains only transparent pixels (alpha 0)
40     opaque,         // row contains only opaque pixels (alpha 255)
41     both            // row contains a mixture of transparent and opaque pixels (alpha 0 and 255)
42 } rowtype;
43 
44 // The Clip class is used by some commands (eg. copy, text) to store pixel data
45 // in a named "clipboard" for later use by other commands (eg. paste, replace):
46 class Clip {
47 public:
48     Clip(int w, int h, bool use_calloc = false);
49     ~Clip();
50 
51     // add the row index to the clip to optimize rendering
52     void AddIndex();
53 
54     // remove the row index from the clip (should be called when the Clip pixels change)
55     void RemoveIndex();
56 
57 public:
58     int cwd, cht;               // width and height of the clip in pixels
59     unsigned char *cdata;       // RGBA data (cwd * cht * 4 bytes)
60     unsigned char *cdatabb;     // top left pixel in non-transparent bounding box
61     rowtype *rowindex;          // contents type of each row
62     int xbb, ybb;               // x and y offset of top left of non-transparent pixel bounding box
63     int wbb, hbb;               // width and height of non-transparent pixel bounding box
64 
65 private:
66     // compute the bounding box of non-zero alpha pixels in the Clip
67     void ComputeBoundingBox();
68 };
69 
70 // The ClipManager class manages a list of clips.
71 class ClipManager {
72 public:
73     ClipManager();
74     ~ClipManager();
75 
76     // clear the list of clips managed (does not delete the clips)
77     void Clear();
78 
79     // add the named clip to the live list to manage
80     void AddLiveClip(const Clip *clip);
81 
82     // add the named clip to the even list to manage
83     void AddEvenClip(const Clip *clip);
84 
85     // add the named clip to the odd list to manage
86     void AddOddClip(const Clip *clip);
87 
88     // add the named clip to the history list to manage
89     void AddHistoryClip(const Clip *clip);
90 
91     // get an array of the live clips managed and the number of clips
92     const Clip **GetLiveClips(int *numclips);
93 
94     // get an array of the even clips managed and the number of clips
95     const Clip **GetEvenClips(int *numclips);
96 
97     // get an array of the odd clips managed and the number of clips
98     const Clip **GetOddClips(int *numclips);
99 
100     // get an array of the history clips managed and the number of clips
101     const Clip **GetHistoryClips(int *numclips);
102 
103     // set the live clip
104     void SetLiveClip(const Clip *liveclip);
105 
106     // set the odd clip
107     void SetOddClip(const Clip *oddclip);
108 
109     // set the even clip
110     void SetEvenClip(const Clip *evenclip);
111 
112     // set the select clip
113     void SetSelectClip(const Clip *selectclip);
114 
115     // set the paste clip
116     void SetPasteClip(const Clip *pasteclip);
117 
118     // set the active clip
119     void SetActiveClip(const Clip *activeclip);
120 
121     // set the not active live clip
122     void SetLiveNotActiveClip(const Clip *livenaclip);
123 
124     // set the not active select clip
125     void SetSelectNotActiveClip(const Clip *selectnaclip);
126 
127     // set the even not active live clip
128     void SetEvenLiveNotActiveClip(const Clip *evennaclip);
129 
130     // set the odd not active live clip
131     void SetOddLiveNotActiveClip(const Clip *oddnaclip);
132 
133     // set the history clip
134     void SetHistoryClip(const Clip *historyclip);
135 
136     // set the history not active clip
137     void SetHistoryNotActiveClip(const Clip *historynaclip);
138 
139     // get the live clip and return clip width if specified
140     const Clip *GetLiveClip(int *clipwd);
141 
142     // get the odd clip and return clip width if specified
143     const Clip *GetOddClip(int *clipwd);
144 
145     // get the even clip and return clip width if specified
146     const Clip *GetEvenClip(int *clipwd);
147 
148     // get the select clip and return clip width if specified
149     const Clip *GetSelectClip(int *clipwd);
150 
151     // get the paste clip and return clip width if specified
152     const Clip *GetPasteClip(int *clipwd);
153 
154     // get the active clip and return clip width if specified
155     const Clip *GetActiveClip(int *clipwd);
156 
157     // get the not active live clip and return clip width if specified
158     const Clip *GetLiveNotActiveClip(int *clipwd);
159 
160     // get the not active select clip and return clip width if specified
161     const Clip *GetSelectNotActiveClip(int *clipwd);
162 
163     // get the even not active live clip and return clip width if specified
164     const Clip *GetEvenLiveNotActiveClip(int *clipwd);
165 
166     // get the odd not active select clip and return clip width if specified
167     const Clip *GetOddLiveNotActiveClip(int *clipwd);
168 
169     // get the history clip and return clip width if specified
170     const Clip *GetHistoryClip(int *clipwd);
171 
172     // get the history not active clip and return clip width if specified
173     const Clip *GetHistoryNotActiveClip(int *clipwd);
174 
175 private:
176     int lsize, esize, osize, hsize;
177     int lclips, eclips, oclips, hclips;
178     const Clip **lcliplist, **ecliplist;
179     const Clip **ocliplist, **hcliplist;
180     const Clip *lclip, *oclip, *eclip, *sclip;
181     const Clip *pclip, *aclip, *lnaclip, *snaclip;
182     const Clip *elnaclip, *olnaclip, *hclip, *hnaclip;
183 };
184 
185 // The Table class is used during next generation calculations for 3D rules
186 // and is an unsigned char array with integer keys.
187 class Table {
188 public:
189     Table();
190     ~Table();
191 
192     // set the size of the table (will clear current contents)
193     bool SetSize(int sz);
194 
195     // clear the contents of the table
196     void Clear();
197 
198     // clear the keys of the table
199     void ClearKeys();
200 
201     // get an array of the keys and the number of keys
202     const int *GetKeys(int *numkeys);
203 
204     // get the number of keys
205     const int GetNumKeys();
206 
207     // get an array of the table contents
208     const unsigned char *GetValues();
209 
210     // set the value at the specified key
211     void SetValue(const int key, const unsigned char value);
212 
213     // set the value at the specified key to 1
214     void SetTo1(const int key);
215 
216     // add an amount to the value at the specified key
217     void AddToValue(const int key, const unsigned char amount);
218 
219     // decrement the value at the specified key but stop at 1.
220     void DecrementTo1(const int key);
221 
222     // sort keys to allow more memory friendly iteration
223     void SortKeys();
224 
225     // copy contents
226     void Copy(const Table &from);
227 
228 private:
229     void FreeMemory();
230     bool AllocateMemory();
231     unsigned char *values;
232     int  *keys;
233     char *exists;
234     int   size;
235     int   nkeys;
236 };
237 
238 class Overlay {
239 public:
240     Overlay();
241     ~Overlay();
242 
243     const char *DoOverlayCommand(const char *cmd);
244     // Parse and execute the given command.
245     // If an error is detected then return a string starting with "ERR:"
246     // and immediately followed by an appropriate error message.
247     // If no error then return either a string containing the results
248     // of the command, or NULL if there are no results.
249 
250     const char *DoOverlayTable(const char *cmd, lua_State *L, int n, int *nresults);
251     // Parse and execute the given command.
252     // Used for table API commands: fill, get, line, lines, paste, rgba and set
253 
254     void DeleteOverlay();
255     // Deallocate all memory used by the overlay and reset pixmap to NULL.
256 
257     bool PointInOverlay(int vx, int vy, int *ox, int *oy);
258     // Return true if the given pixel location within the current view
259     // is within the overlay.  If so then return the overlay location.
260 
261     bool TransparentPixel(int x, int y);
262     // Return true if the given pixel is within the overlay and completely
263     // transparent (ie. its alpha value is 0).
264 
265     void SetOverlayCursor();
266     // Set appropriate cursor for when the mouse is over a non-transparent
267     // pixel in the overlay.
268 
269     void CheckCursor();
270     // If the pixmap has changed then the cursor might need to be changed.
271 
GetOverlayData()272     unsigned char *GetOverlayData() { return ovpixmap; }
273     // Return a pointer to the overlay's RGBA data (possibly NULL).
274     // This data is used to render the overlay (see DrawOverlay in
275     // wxrender.cpp).
276 
GetOverlayWidth()277     int GetOverlayWidth() { return ovwd; }
278     // Return the current pixel width of the overlay.
279 
GetOverlayHeight()280     int GetOverlayHeight() { return ovht; }
281     // Return the current pixel height of the overlay.
282 
GetOverlayPosition()283     overlay_position GetOverlayPosition() { return pos; }
284     // Return the current position of the overlay.
285 
286     bool OnlyDrawOverlay();
287     // If true then DrawView (in wxrender.cpp) will only draw the overlay.
288 
289     void SaveOverlay(const wxString &pngpath);
290     // Save overlay in given PNG file.
291 
292 private:
293     const Clip *GetClip(const char *clipname);
294     // Get the named clip.
295 
296     const char *GetCoordinatePair(char *args, int *x, int *y);
297     // Decode a pair of integers from the supplied string.
298     // Returns a pointer to the first non-space character after
299     // the coordinate pair or NULL if decode failed.
300 
301     void SetRenderTarget(unsigned char *pix, int pwd, int pht, Clip *clip);
302     // Set the render target pixmap and size.
303 
304     const char *DoCreate(const char *args);
305     // Create the current render target with the given width and height.
306     // All bytes are initialized to 0 (ie. all pixels are transparent).
307     // If the render target is the overlay then additionally:
308     //   The current RGBA values are all set to 255 (opaque white).
309     //   The current position is set to topleft.
310     //   The current cursor is set to the standard arrow.
311     //   Alpha blending is turned off.
312     //   The transformation values are set to 1,0,0,1 (identity).
313     //   The current font is set to the default font at 10pt.
314     //   No cell view is allocated.
315 
316     const char *DoDelete(const char *args);
317     // Delete the named clip or if no clip specified delete the overlay.
318     // It is an error to attempt to delete a clip if it is the render target.
319 
320     const char *DoResize(const char *args);
321     // Resize the named clip or if no clip specified resize the overlay.
322     // Returns the previous size as a string.
323     // Does not change any settings and keeps any existing cell view.
324 
325     const char *DoPosition(const char *args);
326     // Specify where to display the overlay within the current view.
327 
328     const char *DoCursor(const char *args);
329     // Specify which cursor to use when the mouse moves over a
330     // non-transparent pixel and return the old cursor name.
331 
332     const char *DoTarget(const char *args);
333     // Sets the render target to the named clip or the overlay
334     // if no clip specified.
335     // Returns the old render target clip name or "" for overlay.
336 
337     const char *DoSetRGBA(const char *args);
338     // Set the current RGBA values and return the old values as a string
339     // of the form "r g b a".
340 
341     void DrawPixel(int x, int y);
342     // Called by some drawing functions to set a given pixel
343     // that is within the render target.
344 
345     void DrawAAPixel(int x, int y, double opacity);
346     // Draw an antialiased pixel in the render target.
347 
348     const char *DoSetPixel(const char *args);
349     // Set the given pixels to the current RGBA values.
350     // Ignores pixels outside the render target.
351 
352     const char *DoGetPixel(const char *args);
353     // Return the RGBA values of the given pixel as a string of the
354     // form "r g b a", or "" if the pixel is outside the render target.
355 
356     const char *DoGetXY();
357     // Return the current mouse position within the overlay as a string
358     // of the form "x y", or "" if the mouse is outside the overlay.
359 
360     const char *LineOptionWidth(const char *args);
361     // Set linewidth and return previous value as a string.
362 
363     const char *DoLineOption(const char *args);
364     // Set a line option.
365 
366     void RenderLine(int x0, int y0, int x1, int y1);
367     // Called by DoLine to render a line.
368 
369     const char *DoLine(const char *args, bool connected);
370     // Draw a one or more optionally connected lines using the current RGBA values.
371     // Automatically clips any parts of the line outside the render target.
372 
373     void DrawThickLine(int x0, int y0, int x1, int y1);
374     // Called by RenderLine to draw a line when linewidth is > 1.
375     // If alphablend is true then the edges will be antialiased.
376 
377     void PerpendicularX(int x0, int y0, int dx, int dy, int xstep, int ystep,
378                         int einit, int winit, double w, double D2);
379     // Called by DrawThickLine.
380 
381     void PerpendicularY(int x0, int y0, int dx, int dy, int xstep, int ystep,
382                         int einit, int winit, double w, double D2);
383     // Called by DrawThickLine.
384 
385     void DrawAntialiasedLine(int x0, int y0, int x1, int y1);
386     // Called by DoLine to draw an antialiased line when alphablend is true
387     // and linewidth is 1.
388 
389     const char *DoEllipse(const char *args);
390     // Draw an ellipse within the given rectangle.
391 
392     void DrawEllipse(int x0, int y0, int x1, int y1);
393     // Called by DoEllipse to draw a non-antialiased ellipse when linewidth is 1.
394 
395     void DrawThickEllipse(int x0, int y0, int x1, int y1);
396     // Called by DoEllipse to draw an ellipse when linewidth is > 1.
397     // If alphablend is true then the edges will be antialiased.
398 
399     void DrawAntialiasedEllipse(int x0, int y0, int x1, int y1);
400     // Called by DoEllipse to draw an antialiased ellipse when alphablend is true
401     // and linewidth is 1.
402 
403     const char *DoFill(const char *args);
404     // Fill the given rectangles with the current RGBA values.
405     // Automatically clips any parts of the rectangle outside the render target.
406 
407     void FillRect(int x, int y, int w, int h);
408     // Called by DoFill to set pixels in given rectangle.
409 
410     const char *DoCopy(const char *args);
411     // Copy the pixels in a given rectangle into Clip data with a given name.
412     // The rectangle must be within the render target.
413 
414     const char *DoPaste(const char *args);
415     // Paste the named Clip data into the render target at the given locations.
416     // Automatically clips any pixels outside the render target.
417 
418     const char *DoScale(const char *args);
419     // Scale the named Clip data into the render target using the given rectangle.
420     // Automatically clips any pixels outside the render target.
421 
422     void DisableTargetClipIndex();
423     // Disable the index on the render target clip (used when it is written to which
424     // invalidates the row index).
425 
426     const char *DoOptimize(const char *args);
427     // Optimize the clip for alpha blended rendering by ignoring any blank rows
428     // of pixels (alpha = 0).
429 
430     const char *DecodeReplaceArg(const char *arg, int *find, bool *negfind, int *replace,
431                                  int *invreplace, int *delta, int component);
432     // Decodes a single argument for the replace command.
433 
434     const char *DoReplace(const char *args);
435     // Replace pixels of a specified RGBA color in the render target with the
436     // current RGBA values.
437     // Return the number of pixels replaced.
438 
439     const char *DoLoad(const char *args);
440     // Load the image from a given .bmp/gif/png/tiff file into the render target
441     // at a given location, clipping if necessary.  If successful, return the
442     // total dimensions of the image as a string of the form "width height".
443 
444     const char *DoSave(const char *args);
445     // Save the pixels in a given rectangle into a given .png file.
446     // The rectangle must be within the render target.
447 
448     const char *DoFlood(const char *args);
449     // Do a flood fill starting from the given pixel and using the
450     // current RGBA values.
451 
452     const char *DoBlend(const char *args);
453     // Turn alpha blending on or off and return the old setting as a string
454     // of the form "1" or "0".
455 
456     const char *DoFont(const char *args);
457     // Set the current font according to the given point size and font name
458     // and return the old font as a string of the form "fontsize fontname".
459 
460     const char *TextOptionAlign(const char *args);
461     // Set text alignment per column.
462 
463     const char *TextOptionBackground(const char *args);
464     // Set text background r, g, b, a color.
465 
466     const char *DoTextOption(const char *args);
467     // Set a text option.
468 
469     const char *DoText(const char *args);
470     // Create Clip data with the given name containing the given text.
471     // Return the dimensions of the text (which can have one or more lines)
472     // as a string of the form "width height descent".
473 
474     const char *DoTransform(const char *args);
475     // Set the affine transformation values used by later paste commands
476     // and return the old values as a string of the form "axx axy ayx ayy".
477 
478     const char *DoUpdate();
479     // Set only_draw_overlay to true and then update the current layer
480     // so DrawView will only draw the overlay if OnlyDrawOverlay() is true.
481 
482 #ifdef ENABLE_SOUND
483     const char *SoundStop(const char *args);
484     // Stop sound playback for specified sound or all sounds if none specified.
485 
486     const char *SoundPlay(const char *args, bool loop);
487     // Play the specified sound once or in a loop.
488 
489     const char *SoundState(const char *args);
490     // Returns whether the specified sound or any sound (if none specified) is playing.
491     // Can be "playing", "stopped", or "unknown" if the specified sound is not found.
492 
493     const char *SoundVolume(const char *args);
494     // Sets the volume of the specified sound.
495 
496     const char *SoundPause(const char *args);
497     // Pauses all or the specified sound.
498 
499     const char *SoundResume(const char *args);
500     // Resumes all or the specified sound.
501 #endif
502 
503     const char *DoSound(const char *args);
504     // Play, stop, pause or resume playback, get playback status, or set volume.
505 
506     const char *OverlayError(const char *msg);
507     // Return a string starting with "ERR:" followed by the given message.
508 
509     void SetRGBA(unsigned char r, unsigned char b, unsigned char g, unsigned char a, unsigned int *rgba);
510     // Set the rgba value from the r, b, g, a components.
511 
512     void GetRGBA(unsigned char *r, unsigned char *g, unsigned char *b, unsigned char *a, unsigned int rgba);
513     // Get the r, g, b, a components from the rgba value.
514 
515     // cell view
516 
517     const char *DoCellView(const char *args);
518     // Create a cell view that tracks a rectangle of cells and can be rapidly
519     // drawn onto the render target at a particular scale and angle.
520 
521     void DeleteCellView();
522     // Deallocate all memory used by the cell view.
523 
524     void RefreshCellView();
525     // Refresh the cell view from the universe.
526 
527     void RefreshCellViewWithTheme();
528     // Refresh the cell view from the universe using the theme.
529 
530     void GetPatternColors();
531     // Get the state colors from the pattern.
532 
533     void GetThemeColors(double brightness);
534     // Get the state colors from the theme.
535 
536     const char *DoUpdateCells();
537     // Update the cell view from the universe.
538 
539     void UpdateZoomView(unsigned char *source, unsigned char *dest, unsigned int step);
540     // Update the zoom view from the cell view at the given step size.
541 
542     void DrawCellsRotate(unsigned char *cells, int mask, double angle);
543     // Draw cells onto the render target with the given location mask and rotation.
544 
545     void DrawCellsNoRotate(unsigned char *cells, int mask);
546     // Draw cells onto the render target with the given location mask but no rotation.
547 
548     const char *DoDrawCells();
549     // Draw the cells onto the overlay.
550 
551     // camera
552 
553     const char *CamAngle(const char *args);
554     // Set the camera angle.
555 
556     const char *CamZoom(const char *args);
557     // Set the camera zoom.
558 
559     const char *CamXY(const char *args);
560     // Set the camera position.
561 
562     const char *DoCamera(const char *args);
563     // Set a camera parameter.
564 
565     // celloption
566 
567     const char *CellOptionDepth(const char *args);
568     // Set the cellview layer depth.
569 
570     const char *CellOptionLayers(const char *args);
571     // Set the cellview number of layers.
572 
573     const char *CellOptionHex(const char *args);
574     // Set hex display mode on or off for the cellview.
575 
576     const char *CellOptionGrid(const char *args);
577     // Turn grid display on or off.
578 
579     const char *CellOptionGridMajor(const char *args);
580     // Set the grid major interval.
581 
582     const char *CellOptionStars(const char *args);
583     // Set the stars display on or off.
584 
585     const char *DoCellOption(const char *args);
586     // Set a cellview option.
587 
588     // theme
589 
590     const char *DoTheme(const char *args);
591     // Set the color theme RGB values in the form
592     // "r1 g1 b1 r2 g2 b2 r3 g3 b3 r4 g4 b4 r5 g5 b5" representing RGB values for cells:
593     // r1 g1 b1  just born
594     // r2 g2 b2  alive at least 63 generations
595     // r3 g3 b3  just died
596     // r4 g4 b4  dead at least 63 generations
597     // r5 g5 b5  never occupied
598     // If a single parameter "-1" is supplied then disable the theme and
599     // use the patterns default colors.
600 
601     // grid
602 
603     void DrawVLine(int x, int y1, int y2, unsigned int color);
604     // Draw vertical line.
605 
606     void DrawHLine(int x1, int x2, int y, unsigned int color);
607     // Draw horizontal line.
608 
609     void DrawGridLines();
610     // Draw grid lines.
611 
612     // stars
613 
614     void DrawStars(double angle);
615     // Draw the stars.
616 
617     void CreateStars();
618     // Allocate and position the stars.
619 
620     void DeleteStars();
621     // Free the memory used by the stars.
622 
623     // ovtable Lua API calls
624 
625     // These have the same functionality as the string
626     // API calls but take Lua parameters.
627 
628     const char *DoFill(lua_State *L, int n, int *nresults);
629     // Fill the given rectangles with the current RGBA values.
630     // Automatically clips any parts of the rectangle outside the render target.
631 
632     const char *DoGet(lua_State *L, int n, int *nresults);
633     // Return the RGBA values of the given pixel as individual Lua numbers
634     // r, g, b, a. Values will be -1 if the pixel is outside the render target.
635 
636     const char *DoLine(lua_State *L, int n, bool connected, int *nresults);
637     // Draw a one or more optionally connected lines using the current RGBA values.
638     // Automatically clips any parts of the line outside the render target.
639 
640     const char *DoPaste(lua_State *L, int n, int *nresults);
641     // Paste the named Clip data into the render target at the given locations.
642     // Automatically clips any pixels outside the render target.
643 
644     const char *DoSetPixel(lua_State *L, int n, int *nresults);
645     // Set the given pixels to the current RGBA values.
646     // Ignores pixels outside the render target.
647 
648     const char *DoSetRGBA(const char *cmd, lua_State *L, int n, int *nresults);
649     // Set the current RGBA values and return the old values a Lua table
650     // of the form {"rgba", r, g, b, a}.
651 
652     const char *DoPaste(const int *coords, int n, const Clip *clipptr);
653     // Paste the named Clip data into the render target at the given locations.
654     // Automatically clips any pixels outside the render target.
655 
656     void Draw3DCell(int x, int y, const Clip *clipptr);
657     // Draw a 3D cell.
658 
659     // 3D calls
660 
661     const char *Do3DNextGen(lua_State *L, const int n, int *nresults);
662     // Compute the next generation of the 3D grid and returns
663     // the new grid and population.
664 
665     const char *Do3DSetRule(lua_State *L, const int n, int *nresults);
666     // Set the current 3D rule.
667 
668     const char *Do3DSetGridSize(lua_State *L, const int n, int *nresults);
669     // Sets the current 3D grid size.
670 
671     const char *Do3DSetStepSize(lua_State *L, const int n, int *nresults);
672     // Sets the step modulus for Do3dNextGen.
673 
674     const char *Do3DSetTransform(lua_State *L, const int n, int *nresults);
675     // Sets the current transformation matrix.
676 
677     const char *Do3DDisplayCells(lua_State *L, const int n, int *nresults);
678     // Draws the most recent 3D grid using the current cell type,
679     // depth shading mode and transformation.
680 
681     const char *Do3DSetCellType(lua_State *L, const int n, int *nresults);
682     // Set the cell type for drawing cells.
683 
684     const char *Do3DSetDepthShading(lua_State *L, const int n, int *nresults);
685     // Sets depth shading on or off for Cubes or Spheres.
686 
687     const char *Do3DSetPattern(lua_State *L, const int n, int *nresults);
688     // Sets the current 3D grid to the supplied pattern.
689 
690     const char *Do3DSetSelectPasteActive(lua_State *L, const int n, int *nresults);
691     // Sets the selected, paste and active cell grids.
692 
693     const char *Do3DSetCellHistory(lua_State *L, const int n, int *nresults);
694     // Sets cell history display mode.
695 
696     void UpdateHistoryFromLive();
697     // Update history layer from live cells.
698 
699     void UpdateBoundingBoxFromHistory();
700     // Update bounding box from history cells.
701 
702     void Display3DNormal(const int midx, const int midy, const int stepi, const int stepj);
703     // Display live cells in Move mode for non-BusyBoxes algos.
704 
705     void Display3DBusyBoxes(const int midx, const int midy, const int stepi, const int stepj);
706     // Display live cells in Move mode for BusyBoxes algo.
707 
708     void Display3DNormalEditing(const int midx, const int midy, const int stepi, const int stepj, const bool editing);
709     // Display live cells in Draw or Select modes for non-BusyBoxes algos.
710 
711     void Display3DBusyBoxesEditing(const int midx, const int midy, const int stepi, const int stepj, const bool editing);
712     // Display live cells in Draw or Select modes for BusyBoxes algos.
713 
714     const char *PopulateGrid(lua_State *L, const int n, int idx, Table &destgrid);
715     // Populate a grid from the supplied pattern.
716 
717     const char *Update3DClips(const bool editing);
718     // Updates the clips needed for rendering the cells based on
719     // the cell type, algo and edit mode.
720 
721     int CreateResultsFromC1C2(lua_State *L, const bool laststep);
722     // Creates the Lua grid result for the 3D Moore algo.
723 
724     int CreateResultsFromC1G3(lua_State *L, const bool laststep);
725     // Creates the Lua grid result for the 3D Face, Corner,
726     // Edge and Hexahedral algos.
727 
728     int CreateResultsFromC1(lua_State *L, const bool laststep);
729     // Creates the Lua grid result for the 3D BusyBoxes algo.
730 
731     void Do3DNextGenMoore();
732     // Computes the next generation using the 3D Moore algo.
733 
734     void Do3DNextGenFace();
735     // Computes the next generation using the 3D Face algo.
736 
737     void Do3DNextGenCorner();
738     // Computes the next generation using the 3D Corner algo.
739 
740     void Do3DNextGenEdge();
741     // Computes the next generation using the 3D Edge algo.
742 
743     void Do3DNextGenHexahedral();
744     // Computes the next generation using the 3D Hexahedral algo.
745 
746     void Do3DNextGenBB(const bool mirror, const int gencount);
747     // Computes the next generation using the 3D BusyBoxes algo
748     // (standard and wrap).
749 
750     bool CreateDivTable();
751     // Creates the divide and modulus lookup table.
752 
753     void FreeDivTable();
754     // Frees the divide and modulus lookup table.
755 
756     bool CreateAxisFlags();
757     // Creates the axis flags.
758 
759     void FreeAxisFlags();
760     // Frees the axis flags.
761 
762     void ClearAxisFlags();
763     // Clear the axis flags.
764 
765     void PopulateAxis();
766     // Populate axis flags from live.
767 
768     void UpdateBoundingBox();
769     // Update the bounding box from the axis flags.
770 
771     // helpers to read Lua types
772 
773     const char *ReadLuaBoolean(lua_State *L, const int n, int i, bool *value, const char *name);
774     // Read a Lua boolean.
775 
776     const char *ReadLuaNumber(lua_State *L, const int n, int i, double *value, const char *name);
777     // Read a Lua number (floating point).
778 
779     const char *ReadLuaInteger(lua_State *L, const int n, int i, int *value, const char *name);
780     // Read a Lua integer.
781 
782     const char *ReadLuaString(lua_State *L, const int n, int i, const char **value, const char *name);
783     // Read a Lua string.
784 
785     // 3D
786     typedef enum { cube, sphere, point } celltypes;
787     typedef enum { moore, face, corner, edge, hexahedral, bb, bbw } ruletypes;
788     ruletypes ruletype;             // current 3D algo
789     Table grid3d;                   // source grid
790     Table count1;                   // intermediate counts
791     Table count2;                   // intermediate counts
792     Table next3d;                   // next grid used when stepsize > 1
793     Table paste3d;                  // grid of paste cells
794     Table select3d;                 // grid of selected cells
795     Table active3d;                 // grid of active cells
796     Table history3d;                // history grid
797     int numcoords;                  // size of coordinate list
798     char survivals[27];             // survival flags
799     char births[27];                // birth flags
800     int gridsize;                   // grid edge length
801     int stepsize;                   // step size modulus
802     bool liveedge;                  // whether there is a live cell on the grid edge
803     int minx, miny, minz;           // bounding box for live cells
804     int maxx, maxy, maxz;
805     double xixo, xiyo, xizo;        // transformation matrix
806     double yixo, yiyo, yizo;
807     double zixo, zizo, ziyo;
808     bool depthshading;              // whether to draw with depth shading
809     celltypes celltype;             // what shape to use for cells
810     int depthlayers;                // number of depth layers
811     int mindepth, maxdepth;         // depth layer range
812     ClipManager clipmanager;        // manage the list of clips needed for rendering
813     int fromx, tox, stepx;          // for grid traversal when rendering
814     int fromy, toy, stepy;
815     int fromz, toz, stepz;
816     int cellsize, midcell;          // cell size and mid point for rendering
817     int toolbarht;                  // toolbar height
818     int showhistory;                // cell history longevity: 0 off, >0 on
819     bool fadehistory;               // whether to fade history cells
820     int *modN, *modNN;              // lookup tables for fast divide and mod operations
821     unsigned int *xyz;              // lookup table for offset to grid coordinate mapping
822     char *xaxis, *yaxis, *zaxis;    // flags for live cells on each axis used for fast bounding box
823 
824     // render target
825     unsigned char *pixmap;          // current render target RGBA data (wd * ht * 4 bytes)
826     int wd, ht;                     // current render target pixmap width and height
827     Clip *renderclip;               // clip if render target
828     std::string targetname;         // render target name
829 
830     // overlay
831     unsigned char *ovpixmap;        // overlay RGBA data
832     int ovwd, ovht;                 // width and height of overlay pixmap
833     unsigned char r, g, b, a;       // current RGBA values for drawing pixels
834     unsigned int rgbadraw;          // packed version of current RGBA values
835     int alphablend;                 // whether to do alphablending when drawing (0 - no, 1 - full, 2 - opaque destination)
836     bool only_draw_overlay;         // set by DoUpdate, reset by OnlyDrawOverlay
837     overlay_position pos;           // where to display overlay
838     const wxCursor *ovcursor;       // cursor to use when mouse is in overlay
839     std::string cursname;           // remember cursor name
840     int axx, axy, ayx, ayy;         // affine transformation values
841     bool identity;                  // true if transformation values are 1,0,0,1
842     int linewidth;                  // for lines and ellipses
843 
844     std::map<std::string,Clip*> clips;
845     // named Clip data created by DoCopy or DoText and used by DoPaste
846 
847 #ifdef ENABLE_SOUND
848     // sound
849     std::map<std::string,ISound*> sounds;
850 #endif
851 
852     // text
853     wxFont currfont;                // current font used by text command
854     std::string fontname;           // name of current font
855     int fontsize;                   // size of current font
856     text_alignment align;           // text alignment
857     unsigned int textbgRGBA;        // text background color
858 
859     // cell view
860     unsigned int cellRGBA[256];     // cell RGBA values
861     unsigned char *cellview;        // cell state data (cellwd * cellht bytes)
862     unsigned char *cellview1;       // cell state data (cellwd * cellht bytes) double buffer
863     unsigned char *zoomview;        // cell state data (cellwd * cellht bytes) for zoom out
864     unsigned int cellwd, cellht;    // width and height of cell view
865     unsigned int cellx, celly;      // x and y position of bottom left cell
866     bool ishex;                     // whether to display in hex mode
867 
868     // camera
869     double camx;                    // camera x position
870     double camy;                    // camera y position
871     double camzoom;                 // camera zoom
872     double camangle;                // camera angle
873     int camlayers;                  // camera layers
874     double camlayerdepth;           // camera layer depth
875 
876     // theme
877     bool theme;                     // whether a theme is active
878     unsigned int aliveStartRGBA;    // cell just born RGBA
879     unsigned int aliveEndRGBA;      // cell alive longest RGBA
880     unsigned int deadStartRGBA;     // cell just died RGBA
881     unsigned int deadEndRGBA;       // cell dead longest RGBA
882     unsigned int unoccupiedRGBA;    // cell never occupied RGBA
883     unsigned int borderRGBA;        // border RGBA
884     unsigned char bordera;          // border alpha (RGB comes from View Settings)
885 
886     // grid
887     bool grid;                      // whether to display grid lines
888     int gridmajor;                  // major grid line interval
889     unsigned int gridRGBA;          // grid line color
890     unsigned int gridmajorRGBA;     // major grid line color
891     bool customgridcolor;           // whether grid line color is custom
892     bool customgridmajorcolor;      // whether major grid line color is custom
893 
894     // stars
895     double *starx, *stary, *starz;  // star x, y, z coordinates
896     unsigned int starRGBA;          // star color
897     bool stars;                     // whether to display stars
898 };
899 
900 extern Overlay *curroverlay;    // pointer to current overlay (set by client)
901 
902 #endif
903