Techno-Plaza
Site Navigation [ News | Our Software | Calculators | Programming | Assembly | Downloads | Links | Cool Graphs | Feedback ]
 Main
   Site News

   Our Software

   Legal Information

   Credits

 Calculators
   Information

   C Programming

     Introduction to C

     Keyboard Input

     Graphics Intro

       Part I

       Part II

     Slider Puzzle 1

     Functions

     Pointers

     Dynamic Memory

     Slider Puzzle 2

     Structures

     Bit Manipulation

     Advanced Pointers

     File I/O

     Graduate Review

   Assembly

   Downloads

 Miscellaneous
   Links

   Cool Graphs

   Feedback Form

C Programming Lessons

TIGCC Programming Lessons

The following is part of a series of lessons designed to help teach people to program in C for the TI-89, 92+, and V200 calculators using the TIGCC development environment.

If you wish, you can download the program source code, project files, and binaries here.

Lesson 3 - An Introduction to Graphics

Step 1 - TI-68k Display Information

The TI-89 and TI-92+/V200 both operate in a very similar way. They both have a section of memory which is used to control the screen display. This memory is 240 bits * 128 bits long, representing 240 pixels of width by 128 pixels of height, which is the screen dimensions of the TI-92+/V200. The TI-89 has the same block of memory, but uses only the first 160 pixels of each row and 100 pixels of height. So although programs can use the extra memory there and not cause any damage, it will not be displayed on screen.

We will cover two types of displaying information on the screen, lines and sprites. Lines were once very important to simple programs because simple sprites were much more complicated in assembly, but are not as important as they used to be. Nonetheless, it is a good starting point for understanding the fundamentals of the display. In the other example, we will deal with sprites, and how to manipulate them in their simple (non-masked) form. Advanced (masked) sprites will be covered in a future lesson.

When you write your programs, try to remember the screen differences between the TI-89 and the TI-92+/V200, if you want your program to be compatible for both calculators. Since more TI-89's are in use, you may choose to develop for the TI-89 screen, but use keyboard variables to be compatible with both calculators. This is not an ideal solution, but it will enable your programs to run on both calculators with no modifications required. You can always convert the program to run better on the TI-92+/V200 screen later.

Step 2 - Screen Dimensions and Line Drawing

In this example, we will clear the screen, and then draw lines at randomly selected positions on the screen in varying lengths and angles.

Start TIGCC and create a new project. Create a new C Source File named lines. Modify the file so that it looks like this:

lines.c


#include <tigcclib.h>

void _main(void) {
    int x1, y1, x2, y2;
    int loop;

    // seed the random number generator
    randomize();

    // clear the screen
    ClrScr();

    // print the intro text
    DrawStr(0, 0, "Line Test 1.0", A_NORMAL);
    DrawStr(0, 10, "Press Any Key To Continue", A_NORMAL);
    DrawStr(0, 20, "Press the ESC key to exit", A_NORMAL);

    while (ngetchx() != KEY_ESC) {
        // clear the screen
        ClrScr();

        // execute this loop 10 times (0-9 = 10)
        for (loop = 0; loop < 10; loop++) {
            // pick random coordinates for the lines
            x1 = random(LCD_WIDTH);
            y1 = random(LCD_HEIGHT);
            x2 = random(LCD_WIDTH);
            y2 = random(LCD_HEIGHT);

            // draw the line at the random coordinates
            DrawLine(x1, y1, x2, y2, A_NORMAL);
        }
    }
}

Step 3a - Compile and Run the Program

Build the project and send the program to TiEmu. It will look something like this, but since we are drawing randomly selected lines, it will not look exactly like this, of course.

TI-89 AMS 2.05 lines.89z TI-92+ AMS 2.05 lines.9xz

Step 3b - Program Analysis

The program introduces several new functions and a new type of loop, the for loop. It is the biggest program we have created to date, so make sure you understand lessons 1 and 2, because you will need them to understand this lesson.

int x1, y1, x2, y2;
int loop;

These are just some integer variable declarations. Nothing special here.

// seed the random number generator
randomize();

Here we introduce another TIGCC function. This is a helper function for generating psuedo-random numbers. I say pseudo, because the function we will be using is not really random. For most purposes though, it is 'random' enough. The purpose of the randomize function is to seed the random number generator. The seed is a special value which determines all pseudo-random numbers generated by a pseudo-random number generator. It's not important to know exactly how this works. It is enough to know that you need to call the randomize() function ONE time (only once) before we can use the random number generator.

// clear the screen
ClrScr();

We're not going to use printf in this program, so we revert to our old ClrScr() function to clear the screen.

// print the intro text
DrawStr(0, 0, "Line Test 1.0", A_NORMAL);
DrawStr(0, 10, "Press Any Key To Continue", A_NORMAL);
DrawStr(0, 20, "Press the ESC key to exit", A_NORMAL);

We have switched back to using DrawStr for this program.

while (ngetchx() != KEY_ESC) {
    // clear the screen
    ClrScr();

Here is the first part of the while loop. You have seen things similar to this before, but to refresh your memory, the condition is, while the value returned by the ngetchx() function is not equal to the ESC key, execute the body of the loop.

We also see that the first command in the while loop body is the ClrScr() function, which we already know clears the screen.

// execute this loop 10 times (0-9 = 10)
for (loop = 0; loop < 10; loop++) {

This is a new kind of loop. We have already seen 'while' loops evaluate the condition, and execute the body of the loop (remember the body is contained within the { } braces) while that condition is true. When it is no longer true, the while loop stops executing the body, and the program continues.

For loops are a little different. Instead of testing for a condition, a for loop's job is to execute a number of statements a certain number of times. For loops actually do have conditions, but they are secondary to the primary nature of the for loop.

The for loop's syntax is as follows. for (variable = initial value; variable is something with respect to value; variable change) { // do something here }. What this means is simple. We have a variable, which will tell us when to execute the loop. The for loop handles the variable initialization, the test condition, and the change to the variable so that the loop is only executed so many times.

for (loop = 0; loop < 10; loop++) { } means give the variable loop the initial value of 0. Then, while the variable loop's value is less than the value '10', execute the body of the loop. At the end of each execution of the body of the loop, increment the value of the variable loop by one. Simple, no?

For loops are one of the most powerful programming constructs in C, and once you get the hang of the syntax, they are easy to use and understand. Just remember the three basic parts of the for construct.

  1. Initialize the variable.
  2. Test the condition.
  3. Change the variable at the end of each loop.

The for loop construct we have used is probably the most common. We start the variable at 0. We check it's value against a higher value. Then we increment it's value. We will see other kinds of for loops in future lessons. Now let's continue with the body of the loop.

// pick random coordinates for the lines
x1 = random(LCD_WIDTH);
y1 = random(LCD_HEIGHT);
x2 = random(LCD_WIDTH);
y2 = random(LCD_HEIGHT);

These next four statements are assignment statements. Here we pick random values and assign them to the coordinates.

The random() function selects a random number between 0 and x - 1, where x is the argument you pass to the random() function. It returns this randomly selected number. So the statement x1 = random(LCD_WIDTH); selects a random number between 0 and LCD_WIDTH (which is defined to be 160 on the TI-89 and 240 on the TI-92+/V200) - 1 and assigns this number to the variable x1. This gives us the coordinates of the first x position. The statement y1 = random(LCD_HEIGHT); selects a random number between 0 and LCD_HEIGHT (which is 100 on the TI-89 and 128 on the TI-92+/V200) - 1 and assigns that number to the variable y1. We repeat this process to get (x2, y2). Now we have two point values, which will be the endpoints of a line.

// draw the line at the random coordinates
DrawLine(x1, y1, x2, y2, A_NORMAL);

The DrawStr function draws a line between two points defined by the endpoints (x1,y1) and (x2,y2). The last argument is an attribute setting. Just like DrawStr, there are several attributes you can use to draw the line. The standard is A_NORMAL, but you can look in the TIGCC docs for the other possibilities.

Step 3c - Programmatic Conclusions

This program gave us an introduction to many new concepts (some of them graphical) about TI-89/92+/V200 graphics and compatibility between the screens. Although the graphical illustrations were simple line drawings, you can appreciate the pixel based system of the screen, and because it is so small, you can also appreciate how every pixel counts, so you must take advantage of every one.

A much more relevant working of the graphical system is contained in the next example on simple sprites.

Continue with Part II

 

Copyright © 1998-2007 Techno-Plaza
All Rights Reserved Unless Otherwise Noted

Get Firefox!    Valid HTML 4.01!    Made with jEdit