Appendix D: Graphics Library: Universiti Teknologi PETRONAS
Appendix D: Graphics Library: Universiti Teknologi PETRONAS
Introduction
This appendix describes gfx , a simple graphics library that gives the ability for C programmers
to draw or generate simple graphics. This library is meant to be a simple and easy set of
functions to be used so that programmers can delve directly into the interesting parts of
programming. The gfx library requires that the programmer understand how to invoke basic C
functions with scalar arguments.
This simplified library is sufficient to explore vector graphics, draw fractal shapes and simple
video games. Its capabilities would not cover all graphics functionality. If more advanced
graphics functionality is required, other libraries are available such as OpenGL for precise 3-D
graphics, Qt for windowed applications and SDL for video games.
This library runs on Linux machines using the X11 Window System where the necessary X11
libraries need to be installed using the following steps:
If the previous installation steps were unsuccessful, the necessary libraries can be installed with
the following steps:
1
Department of Electrical & Electronic Engineering
Getting started
Copy the following files and place them in a sub-directory of your choice:
gfx.c – this is the source file of the library, do not change the contents
gfx.h – this is the header file, do not change the contents
example.c – an example source file, changes are allowed in this file.
To compile the program that uses the gfx library, the following command has be used.
This method of compilation compiles the example.c file together with gfx.c and the adds the
X11 and math libraries to generate an executable file of example.o .
#include <stdio.h>
#include "gfx.h" 1
int main()
{
int ysize = 300;
int xsize = 600; 2
char c;
return 0;
}
2
Universiti Teknologi PETRONAS
1 - #include “gfx.h”
- Includes the functionality of the gfx library and its related functions.
- This opens a graphics window at the beginning of the program, where the size of the
window in terms of width and height, are given in number of pixels. Also included is
the title or name of the window.
3 – gfx_color (0,255,0);
- This sets the current color of items to be drawn . For example, in this instance the
color set is green.
- This function draws a line from location or coordinates (100, 100 ) to (200,100).
5 – gfx_wait();
- This line waits or halts the system while waiting for the appropriate input from user
either a keyboard entry or a mouse button response.
- Where in this instance, if the keyboard entry is the letter ‘q’ the program would then
exit from the loop and terminate the program.
3
Department of Electrical & Electronic Engineering
This function creates a new graphics windows on the screen. Where the size of the
window is determined by width, which is the width of the window in pixels, and
height , is the height of the window in pixels. *title is the name of the
window in the title bar
Function void gfx_line(int x1, int y1, int x2, int y2);
Description
Example gfx_line(200,200,400,500);
4
Universiti Teknologi PETRONAS
5
Department of Electrical & Electronic Engineering
6
Universiti Teknologi PETRONAS
7
Department of Electrical & Electronic Engineering
8
Universiti Teknologi PETRONAS
9
Department of Electrical & Electronic Engineering
10
Universiti Teknologi PETRONAS
/* Program D1 */
#include <stdio.h>
#include "gfx.h"
int main()
{
int ysize = 300;
int xsize = 600;
char c;
while(1) {
// Wait for the user to press a character.
c = gfx_wait();
if(c=='q') break;
}
return 0;
}
Activity D1.
Load the editor with Program D1, compile it. Once the program is compiled and executed. Sketch
the output of the program.
11
Department of Electrical & Electronic Engineering
Exercise D1
12
Universiti Teknologi PETRONAS
Exercise D2
Using the gfx_point function, create a graphical display that attempts to display an image of
randomized points as illustrated as follows:
13
Department of Electrical & Electronic Engineering
Apart from drawing graphics, additional functionality is available which is listed as follows:
This function pauses the whole program and waits until the user presses a key or a
mouse button. If the user has pressed a mouse button, an integer 1, 2 or 3 will be
returned. If an integer value of 1 is returned, which indicates the left mouse button
was pressed. If 2 is returned, the middle button is pressed and if 3 is returned, the
right mouse button is pressed. If the user has pressed a key on the keyboard, the
character value is returned.
Example char c;
…
c=gfx_wait();
This function functions differently compared to the gfx_wait(), which pauses the
whole program. This program allows the program to continuously run which
waiting for the user to respond using the keyboard or the mouse. This function
returns a TRUE event (when a key or mouse button is pressed) and a FALSE
immediately. If gfx_event_waiting(), returns a TRUE, a gfx_wait() is used
immediately to retrieve the event or data without waiting.
Example char c;
…
if (gfx_event_waiting())
{
c=gfx_wait();}
This function returns the X and Y coordinates of the mouse pointer when the last
key or mouse button was pressed.
Example char c;
…
if (gfx_event_waiting())
{ c=gfx_wait();
printf(“Coordinates %d %d \n”,gfx_xpos(),gfx_ypos());
}
14
Universiti Teknologi PETRONAS
/* Program D2 */
#include <stdio.h>
#include "gfx.h"
int main()
{
int ysize = 300;
int xsize = 600;
char c;
while(1) {
// Wait for the user to press a character.
c = gfx_wait();
if (c==0x01)
{
printf("1 - left button ");
}
else if (c==0x02)
{
printf("2 - middle button ");
}
else if (c==0x03)
{
printf("3 - right button ");
}
else
printf("%c \n",c);
return 0;
}
Activity D2.
Load the editor with Program D2, compile it. Once the program is compiled and executed. Record
the output of the program while trying different mouse buttons until the ‘q’ key is pressed.
15
Department of Electrical & Electronic Engineering
Display Text
Apart from graphics, text can also be displayed on the graphics window using the following function:
This function prints a text that stored in a string and displayed at location x , y .
//Program D3
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//#include <string.h>
#include "gfx.h"
int main()
{
int r,g,b,x,y=0,flag=0,movex=2,movey=2;
int ysize = 600;
int xsize = 800;
char c;
srand(time(NULL));
// Open a new window for drawing.
gfx_open(xsize,ysize,"Example Text");
gfx_text(test1, x,y);
gfx_flush();
usleep(250000);
gfx_flush();
}
}
16
Universiti Teknologi PETRONAS
Activity D3.
Load the editor with Program D3, compile it. Once the program is compiled and executed. Record
the output of the program.
17