ROVLib Tutorial - Redrawing© Jason Tribbeck 2002 |
![]() |
In this tutorial, we will be drawing our own graphics on a canvas.
/** * Redraw.c - Redrawing a canvas manually */ #include <stdlib.h> #include <string.h> #include <stdio.h> #include <ctype.h> #include "kernel.h" #include "main.h" #include "canvas.h" #include "menu.h" #include "baricon.h" #include "extras.h" #include "draw.h" void click(int icn); void choose(menu m, int item); void menu_click(canvas c); /* Prototype the redraw event handler */ void redraw(canvas c, int x0, int y0, int x1, int y1); static canvas c; static menu iconm; int main(int argc, char **argv) { canvas c_proginfo; main_init("My Application"); canvas_init(); menu_init(); c = canvas_create(640, 480, "My Application"); /* Attach the redraw event handler */ canvas_addhandler(c, CANVAS_REDRAWHANDLER, redraw); /* Make the background white */ canvas_colour(c, 0); canvas_make(c); baricon("file_ff8", click); canvas_addhandler(CANVAS_ICONBAR, CANVAS_MENUHANDLER, menu_click); c_proginfo = create_infobox("My Application", "An example ROVLib application", "© Jason Tribbeck", "1.00 (14 Feb 2002)"); iconm = menu_create("Menu test", 2, choose, NULL); menu_add(iconm, "Info"); menu_add(iconm, "Quit"); menu_attachpane(iconm, 0, c_proginfo); canvas_opencentre(c); main_poll(); } void click(int icn) { canvas_reopen(c); } void menu_click(canvas c) { menu_displayiconbar(iconm); } void choose(menu m, int item) { switch(item) { case 1 : exit(0); break; } } /* Redraw the canvas */ void redraw(canvas c, int x0, int y0, int x1, int y1) { /* Black line */ draw_setcolour(0x00000000); /* Start point */ draw_move(0, 0); /* End point */ draw_line(640, 480); /* Red line */ draw_setcolour(0x0000ff00); /* Start point */ draw_move(0, 480); /* End point */ draw_line(640, 0); /* Blue line */ draw_setcolour(0xff000000); /* Filled circle */ draw_circlefill(320, 240, 15); }
I have removed most of the comments from this code - you should know them by now!
We attach a redraw event handler (a function called "redraw
") to
the canvas before it is made. This is because a flag needs to be set in
the window definition telling the WIMP that we are interested in redrawing the
window ourselves. It is the only event handler that you need to attach before
making the window (but you can change it afterwards).
In our canvas redraw event handler, we are drawing a black line from bottom left to top right, a red line from top left to bottom right, and a blue circle in the middle of the canvas. Note that all coordinates are relative to the canvas' origin, and not the screen origin. This is different to most other libraries, where you have to perform some maths to work out this origin. ROVLib saves you time!
We are using ColourTrans to set the correct colours, via draw_setcolour
.
This means that the display will be as close to the correct colours no matter
what screen mode we are in.
This will appear identical to the previous tutorial, but the canvas will look like:
You have seen how a canvas redraw event handler is attached to a canvas, how to set the background colour of a canvas, and also how to draw simple primitives with a given colour.