#include <hpgcc49.h> #include <hpgraphics.h> int main(void) { hpg_clear(); hpg_draw_text("Hello, world!", 0, 0); while(!keyb_isON()); return 0; }
Next is a more involved sample, demonstrating each of the steps from the list above. You may find it helpful to refer to this example as each step is explained below. This sample uses 16-color grayscale, a non-default font, and double-buffering.
#include <hpgcc49.h> #include <hpgraphics.h> int main(void) { hpg_set_mode_gray16(1); hpg_set_font(hpg_stdscreen, hpg_get_bigfont()); hpg_set_color(hpg_stdscreen, HPG_COLOR_GRAY_10); hpg_clear(); hpg_draw_text("Hello, world!", 0, 0); hpg_flip(); //updates the screen with the new image (needed in double-buffered mode) while(!keyb_isON()); return 0; }
Here is a program that draws some lines, text and shapes to the screen.
#include <hpgcc49.h> #include <hpgraphics.h> int main(void){ hpg_set_mode_gray16(1); //enter 16 colour, double buffered mode hpg_clear(); //clear the screen hpg_set_color(hpg_stdscreen, HPG_COLOR_BLACK); hpg_draw_text("minifont", 0, 0); hpg_set_font(hpg_stdscreen, hpg_get_bigfont()); //change to a big font hpg_draw_text("big font", 0, 20); hpg_set_font(hpg_stdscreen, hpg_get_minifont()); //and back to small hpg_draw_text("minifont again", 0, 35); hpg_draw_line(70,5,70,25);// draw a line hpg_draw_rect(50,60,55,70); //and a rectangle hpg_set_color(hpg_stdscreen, HPG_COLOR_GRAY_8); //grey hpg_fill_rect(50,0,60,10); //fill in a rectangle hpg_set_color(hpg_stdscreen, HPG_COLOR_GRAY_5); //lightish grey hpg_fill_circle(70,70,4); //draw a circle hpg_flip(); //updates the screen with the new image (needed in double-buffered mode) while(!keyb_isON()); //wait until ON pressed }
After choosing a display mode, you should call one of hpg_set_mode_mono, hpg_set_mode_gray4, or hpg_set_mode_gray16. Each of these functions accepts one additional parameter, which will enable double-buffering if it is set.
When double buffering is enabled, your images are drawn to an off-screen buffer, and diplayed as a single action when the hpg_flip function is called. Until that time, the display will show the previous completed screen in its entirety. This prevents your users from viewing partially drawn screens, and can prevent an appearance of "flicker". HPG's implementation of double-buffering uses a combination of vsync and hardware page flipping to ensure that only complete frames are drawn.
Regardless of the current display mode, the HPG library provides a choice of 256 virtual colors. However, these colors are mapped in a mode-specific manner to the colors of the physical screen. In 1-bpp monochrome mode, for example, colors 0-127 appear as white, while colors 128-255 appear as black. By contrast, 16-color grayscale mode displays only colors 0-15 as white, and 240-255 as black, while the intermediate colors appear as shades of gray.
More information about these options may be found in the reference documentation.
hpg_draw_
or hpg_fill_
prefixes. You may browse the reference documentation for more information on each of these functions.The hpg_clear function is used to clear the screen. The initial contents of the screen are undefined, and it is recommended that you clear the screen prior to beginning your drawing.
An image is created by calling any of hpg_alloc_mono_image, hpg_alloc_gray4_image, or hpg_alloc_gray16_image. The size of the image is passed as parameters to the function. Images can be arbitrary size, limited by available memory. The result of the allocation function is a pointer to an hpg_t, which can be used with drawing function in the normal manner. You will need to use functions with the _on
suffix, as opposed to those that draw directly to the screen.
#include "monalisa.xpm" int main(void) { hpg_set_mode_gray16(0); hpg_t *img = hpg_load_xpm_gray16(monalisa_xpm); hpg_blit(img, 0, 0, 80, 80, hpg_stdscreen, 0, 0); while(!keyb_isON()); hpg_free_image(img); return 0; }
The first line of the example above includes the image file into the application code. Unlike most image formats, Xpm files consist of C code that can be compiled. This C code declares a data structure called <name>_xpm
, which contains the image data. The data is then loaded into an off-screen image, and drawn to the screen.