NewI\O paint_demo

paint_demo.nio is a small demo of a very basic paint program.

Here is a screenshot:

Here is the source code:

/******************************************************************************
 *
 *    paint_demo.c
 *
 *    Copyright (C) 2005  Chris Nystrom
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but without any warranty whatsoever.
 *
 *    For more license information see:
 *
 *      http://www.gnu.org/copyleft/gpl.html
 *
 *    Contact Author at:
 *
 *    Chris Nystrom
 *    11013 Prairie Dove Circle
 *    Austin, Texas  78758
 *
 *    E-Mail: cnystrom@gmail.com
 *    Blog:   http://conversazione.blogspot.com
 *    AIM:    nystromchris
 *
 *    Soli Deo Gloria
 *
 */

#include "nio_lib.h"

void clear_screen(void);
void draw_pen_area();
void command_area(void);

color black;
color blue;
color brown;
color green;
color yellow;
color orange;
color red;
color violet;
color white;

int pen_size;

color current_color;

mouse_state ms;

int main(int argc, char *argv[])
{
        int cls_flag = 1;

        nio_app_init("paint_demo", "0.1");

        black = nio_get_color("black");
        blue = nio_get_color("blue");
        brown = nio_get_color("brown");
        green = nio_get_color("green");
        yellow = nio_get_color("yellow");
        orange = nio_get_color("orange");
        red = nio_get_color("red");
        violet = nio_get_color("violet");
        white = nio_get_color("white");

        current_color = black;

        pen_size = 4;

        clear_screen();

        while (1) {
                int key;

                ms = nio_mouse_state();
                key = nio_get_key();

                if (ms.b1 == PRESSED) {
                        if (ms.x > 630)
                                ms.x = 630;

                        if (ms.y > 470)
                                ms.y = 470;

                        if (ms.x < 20) {
                                command_area();
                        } else {
                                nio_draw_rectangle_rgb(ms.x, ms.y,
                                                       pen_size, pen_size,
                                                       current_color,
                                                       TRUE);

                                // Draw first before clearing screen again.
                                cls_flag = 1;
                        }
                }

                if (ms.b2 == PRESSED || key == SPACEBAR) {
                        if (cls_flag) {
                                clear_screen();
                        }

                        cls_flag = 0;
                }

                if (ms.b3 == PRESSED || key == ESC) {
                        nio_exit();
                        exit(0);
                }
        }
}

void clear_screen(void)
{
        // draw color selector
        nio_clear_screen(white, FALSE);
        nio_draw_rectangle_rgb(0, 0, 20, 20, black, FALSE);
        nio_draw_rectangle_rgb(0, 19, 20, 20, blue, FALSE);
        nio_draw_rectangle_rgb(0, 39, 20, 20, brown, FALSE);
        nio_draw_rectangle_rgb(0, 59, 20, 20, green, FALSE);
        nio_draw_rectangle_rgb(0, 79, 20, 20, yellow, FALSE);
        nio_draw_rectangle_rgb(0, 99, 20, 20, orange, FALSE);
        nio_draw_rectangle_rgb(0, 119, 20, 20, red, FALSE);
        nio_draw_rectangle_rgb(0, 139, 20, 20, violet, FALSE);

        // outline white box
        nio_draw_line_rgb(0, 159, 19, 159, black, FALSE);
        nio_draw_line_rgb(0, 179, 19, 179, black, FALSE);
        nio_draw_line_rgb(0, 159, 0, 179, black, FALSE);
        nio_draw_line_rgb(19, 159, 19, 179, black, FALSE);

        draw_pen_area();
}

void draw_pen_area(void)
{
        nio_draw_rectangle_rgb(0, 200, 21, 81, white, FALSE);

        if (nio_color_equal(current_color, white)) {
                nio_printf_to_stdout
                    ("draw_pen_area(): colors are equal.\n");
                nio_draw_rectangle_rgb((0 + 8), (220 - 12), 4, 4, black,
                                       FALSE);
                nio_draw_rectangle_rgb((0 + 7), (240 - 13), 6, 6, black,
                                       FALSE);
                nio_draw_rectangle_rgb((0 + 5), (260 - 15), 10, 10, black,
                                       FALSE);
                nio_draw_rectangle_rgb((0 + 1), (280 - 19), 18, 18, black,
                                       FALSE);
        }

        nio_draw_rectangle_rgb((0 + 9), (220 - 11), 2, 2, current_color,
                               FALSE);
        nio_draw_rectangle_rgb((0 + 8), (240 - 12), 4, 4, current_color,
                               FALSE);
        nio_draw_rectangle_rgb((0 + 6), (260 - 14), 8, 8, current_color,
                               FALSE);
        nio_draw_rectangle_rgb((0 + 2), (280 - 18), 16, 16, current_color,
                               FALSE);

        if (pen_size == 2) {
                nio_draw_line_rgb(0, 200, 19, 200, black, FALSE);
                nio_draw_line_rgb(0, 219, 19, 219, black, FALSE);
                nio_draw_line_rgb(0, 200, 0, 219, black, FALSE);
                nio_draw_line_rgb(19, 200, 19, 219, black, FALSE);
        } else if (pen_size == 4) {
                nio_draw_line_rgb(0, 220, 19, 220, black, FALSE);
                nio_draw_line_rgb(0, 239, 19, 239, black, FALSE);
                nio_draw_line_rgb(0, 220, 0, 239, black, FALSE);
                nio_draw_line_rgb(19, 220, 19, 239, black, FALSE);
        } else if (pen_size == 8) {
                nio_draw_line_rgb(0, 240, 19, 240, black, FALSE);
                nio_draw_line_rgb(0, 259, 19, 259, black, FALSE);
                nio_draw_line_rgb(0, 240, 0, 259, black, FALSE);
                nio_draw_line_rgb(19, 240, 19, 259, black, FALSE);
        } else {
                nio_draw_line_rgb(0, 260, 19, 260, black, FALSE);
                nio_draw_line_rgb(0, 279, 19, 279, black, FALSE);
                nio_draw_line_rgb(0, 260, 0, 279, black, FALSE);
                nio_draw_line_rgb(19, 260, 19, 279, black, FALSE);
        }

        nio_paint();
}

void command_area(void)
{
        if (ms.y < 19) {
                current_color = black;
                draw_pen_area();
        } else if (ms.y < 39) {
                current_color = blue;
                draw_pen_area();
        } else if (ms.y < 59) {
                current_color = brown;
                draw_pen_area();
        } else if (ms.y < 79) {
                current_color = green;
                draw_pen_area();
        } else if (ms.y < 99) {
                current_color = yellow;
                draw_pen_area();
        } else if (ms.y < 119) {
                current_color = orange;
                draw_pen_area();
        } else if (ms.y < 139) {
                current_color = red;
                draw_pen_area();
        } else if (ms.y < 159) {
                current_color = violet;
                draw_pen_area();
        } else if (ms.y < 179) {
                current_color = white;
                draw_pen_area();
        } else if (ms.y < 219 && ms.y >= 200) {
                pen_size = 2;
                draw_pen_area();
        } else if (ms.y < 239) {
                pen_size = 4;
                draw_pen_area();
        } else if (ms.y < 259) {
                pen_size = 8;
                draw_pen_area();
        } else if (ms.y < 279) {
                pen_size = 16;
                draw_pen_area();
        }
}

You can download the source code at:

ftp://ftp.newio.org/pub/nio_apps/paint_demo.0.009_052_020106.tar.gz


Updated: 02/02/06 ccn