CSCI 240 - Creating a Graphics Program in Quincy


The procedure for creating a graphics program in Quincy is a little different and a little more complex than creating a regular text-only program.  Basically, you have to

Here are detailed instructions (also see the Quincy Help topic "Working with Projects"):

I. Create a Project

It is advisable to create a separate subdirectory (or folder) for each project.  A lab TA can show you how to do this if you don't know.  You should be able to tell him or her where you want the folder.

It will be convenient to begin by using Quincy to create a skeleton progam: the #include "allegro.h" , and an empty main() function.  Save it with a file name and .cpp extension.  As an example, call it graph1.cpp.

Now choose File/New/Project.  You will see a pop-up dialog box.  You must fill in the following:

  1. Enter the name of the target (the target is the executable program (the .exe) that you want to create) in the Target field. The target is either an executable program or an object library. Enter the file name with no file extension. For example, if your executable program will be called graph1.exe, enter graph1
  2. Enter the path where the project file and its source code files will be located. This might be your f: drive or a subdirectory under f: (such as f:\GrTest ) or your a: drive.
  3. Enter the working directory where the project will read and write its data files when you run or debug it from within Quincy.  This will probably be the same place where your project and source files reside.
  4. The Type of Build should be Console.

Next, a blank window will pop up.  You will enter 2 lines in this window: the name of the source code file for your program and the name of the graphics library we will use.  However, you don't type them directly.  Instead, choose Project/Insert File(s) and type the names (one at a time). 

The first should be the name of your source code, graph1.cpp (to use the name shown above). 

The second should be liballeg.a  You have to find it using the Insert Files dialog.  It is at c:\Quincy2002\bin\MinGW\lib\liballeg.a  You may need to type this in manually if your system does not allow you to see the c: drive (this may be true in the lab). 

Note: the start of this path may be different if you installed Quincy on your computer on a different drive or using a different 1st-level folder.  But the MinGW\lib\liballeg.a will always be the same.

Again, you do this by choosing Project/Insert File(s) twice, once for each line.  After you have done this, the window should show (for the example we are using):

graph1.cpp
liballeg.a

Click File/Save now to save the project.  Be sure you save it in the same folder where you saved graph1.cpp.

It will save with the name of the target (graph1 in this example) and an extension of .PRJ.

Ok - seems like a lot, but after you've done it once or twice, it takes less than a minute (or two).

II. Write the Program

The program will have a couple of extra things (compared to text-only programs) as well as code to call graphics functions (see lecture notes for some of these).  Here is a skeleton:

#include "allegro.h"

int grInit(void);
void wait_for_keypress();

int main()
  { 
  int rc;

  rc = grInit();
  if (rc == -1)
    return 1;
  // stuff that shows graphics... here just show a dot at 100, 100
  putpixel(screen, 100, 100, 5);
  wait_for_keypress();
  return 0;
  }
END_OF_MAIN();
//*******************************************************************
// This initializes the graphics system.  Note that it is called from
// main().  You must include this in your program - but you do not 
// have to copy the comments.
//*******************************************************************
int grInit()
{
/* you should always do this at the start of Allegro programs */
allegro_init();

/* set up the keyboard handler */
install_keyboard(); 

/* set a graphics mode sized 640x480 */
if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) 
  {
  set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
  allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
  return -1;
  }

/* set the color palette */
set_palette(desktop_palette);

/* clear the screen to white */
clear_to_color(screen, makecol(255, 255, 255));

/* you don't need to do this, but on some platforms (eg. Windows) things
* will be drawn more quickly if you always acquire the screen before
* trying to draw onto it.
*/
acquire_screen();

/* set transparent text */
text_mode(-1);
return 1;
}
//*******************************************************************
void wait_for_keypress()
{
/* you must always release bitmaps before calling any input functions */
release_screen(); // seems optional; slows down next operations
readkey(); 
acquire_screen(); // but adding this speeds it up again
}

Notes:

1. The #include for allegro.h is required.  You may need other #includes for your program.  You will not need <iostream> or <iomanip> since you will not be doing and cin or couts. Allegro is the name of the graphics library we will use in this course.  Since graphics is not a part of standard C++, we must use a custom library.

2. Notice the line: END_OF_MAIN(); after main().  This is required here.  Do not omit it.  Do not leave a blank line between the ending } of main() and the END_OF_MAIN; line.

3. grInit() and wait_for_keypress() are functions I have written which use allegro graphics functions.  grInit() puts the computer into graphics mode and wait_for_keypress() does what it says so you can look at the screen before the program ends and goes away.  You can and should use them in your graphics programs.

4. putpixel() is a allegro graphics function.

5. You should be able to copy/paste the grInit() and wait_for_keypress() functions from this document into Quincy's text editor.

6. Other details of graphics functions are covered in the 240 Notes and in Lecture.

III. Compile and Build the Project

To prepare the program to run, you must have the Project open and selected (the 2-line thing you created in Step 1).  You should compile and build the project.  (Not the .cpp source file.)  The .cpp source code does not have to be open on the screen in order to do this (but it can be open if you like).  A quick way to open it is to double click on its name in the Project window.

IV. Run the Project

To run the program you must, again, execute the Project.