| How do I use functions from the wingraph.h file?

Previous | Graphics and Display | Next

Q: | I need some examples of how to use the functions in the wingraph.h header file. Especially, I am confused with static and/or dynamic data allocation. I tried to use alloc.h to create necessary structures, but something was wrong...

A:

I will give five very similar examples how to do the same thing. From the first and fifth example you will see that you need not to use alloc.h at all, and in the second, third and fourth example, you will see what you need to do if you want to use dynamic allocation (i.e. alloc.h) anyway. Maybe this is not so obvious from my documnetation, but flags in WinOpen must be ORed, so they must be "separated" by "|", not by commas.
Example 1: Using wingraph.h without dynamic allocation (called "Window 1"):

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define MIN_AMS 100           // Compile for AMS 1.00 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      WINDOW wind;
      WIN_RECT winr = {20, 20, 80, 50};
      WinOpen (&wind, &winr, WF_SAVE_SCR | WF_TTY);
      WinActivate (&wind);
      WinFont (&wind, F_6x8);
      WinStr (&wind, "hello everyone");
      ngetchx ();
      WinClose (&wind);
    }

Example 2: Window is allocated dynamically (called "Window 2"):

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define MIN_AMS 100           // Compile for AMS 1.00 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      WINDOW *wind = HeapAllocPtr (sizeof (WINDOW));
      WIN_RECT winr = {20, 20, 80, 50};
      WinOpen (wind, &winr, WF_SAVE_SCR | WF_TTY);
      WinActivate (wind);
      WinFont (wind, F_6x8);
      WinStr (wind, "hello everyone");
      ngetchx ();
      WinClose (wind);
      HeapFreePtr(wind);
    }

Note that synonyms for HeapAllocPtr and HeapFreePtr are malloc and free (like in ANSI C).
Example 3: Both "window" and "rect" are allocated dynamically (called "Window 3"):

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define MIN_AMS 100           // Compile for AMS 1.00 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      WINDOW *wind = HeapAllocPtr (sizeof (WINDOW));
      WIN_RECT *winr = HeapAllocPtr (sizeof (WIN_RECT));
      winr->x0 = 20; winr->y0 = 20;
      winr->x1 = 80; winr->y1 = 50;
      WinOpen (wind, winr, WF_SAVE_SCR | WF_TTY);
      WinActivate (wind);
      WinFont (wind, F_6x8);
      WinStr (wind, "hello everyone");
      ngetchx ();
      WinClose (wind);
      HeapFreePtr (wind);
      HeapFreePtr (winr);
    }

Example 4: How to use MakeWinRect to avoid "winr" (called "Window 4"):

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define MIN_AMS 100           // Compile for AMS 1.00 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      WINDOW *wind = HeapAllocPtr (sizeof (WINDOW));
      WinOpen (wind, MakeWinRect (20, 20, 80, 50), WF_SAVE_SCR | WF_TTY);
      WinActivate (wind);
      WinFont (wind, F_6x8);
      WinStr (wind, "hello everyone");
      ngetchx ();
      WinClose (wind);
      HeapFreePtr (wind);
    }

Example 5: This is what I do in my programs (called "Window 5"):

    #define USE_TI89              // Compile for TI-89
    #define USE_TI92PLUS          // Compile for TI-92 Plus
    #define USE_V200              // Compile for V200

    #define OPTIMIZE_ROM_CALLS    // Use ROM Call Optimization
    #define MIN_AMS 100           // Compile for AMS 100 or higher
    #define SAVE_SCREEN           // Save/Restore LCD Contents

    #include <tigcclib.h>         // Include All Header Files

    // Main Function
    void _main(void)
    {
      WINDOW wind;
      WinOpen (&wind, &(WIN_RECT) {20, 20, 80, 50}, WF_SAVE_SCR | WF_TTY);
      WinActivate (&wind);
      WinFont(&wind, F_6x8);
      WinStr (&wind, "hello everyone");
      ngetchx ();
      WinClose (&wind);
    }

Don't forget to close a window before exiting. If you forget to do so, the TI may crash later, when window manager tries to refresh a still active window which ceased to exist due to end of the program!
In general, I prefer static allocation instead of dynamic. It is good if you know in advance how many open windows you have in the program (this is often a case on TI). Dynamic allocation is the only method if you don't know in advance how many open windows you need (then, you can keep them in linked list). I don't think that there is a lot of use for this on the TI. :-)