From a01d9767a7b5fcdf12d1dcd4efd6710d8bb55f58 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Crist=C3=B3v=C3=A3o=20Cruz?= Date: Thu, 1 Apr 2010 23:24:01 +0100 Subject: [PATCH] Working well with congl. --- funnysort.c | 120 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/funnysort.c b/funnysort.c index 0e4dfb4..701ffcf 100644 --- a/funnysort.c +++ b/funnysort.c @@ -5,14 +5,14 @@ #include #include #include +#include -//#include "congl.h" -#include "X11/Xlib.h" -#include "X11/X.h" +#include "congl.h" -#define WIDTH 5 -#define HEIGHT 3 -#define NCYCLES 100 +#define WIDTH 50 +#define HEIGHT 30 +#define NCYCLES 1000000 +#define SLEEP_TIME 0 typedef enum {A=0, EMPTY, SORTER} item_t; @@ -31,6 +31,7 @@ struct sorter_s { }; typedef struct sorter_s sorter_t; +void count_item (board_t *, item_t *, unsigned int *); void drop_item (unsigned int, unsigned int, board_t *, item_t *); void erase_board (board_t *); void fetch_item(unsigned int, unsigned int, item_t *); @@ -47,6 +48,8 @@ void setup_board(board_t *); /* Erase the output medium. */ void erase_board (board_t *board) { + conglReset(); + conglClearScreen(); } void move_sorter(board_t *board, sorter_t *sorter) @@ -104,10 +107,11 @@ void launch_sorter (board_t *board) sorter.line = line; for (itera = 0; itera <= NCYCLES; ++itera) { + //for (itera = 0; 0==0; ++itera) { do { move_sorter(board, &sorter); pick_item(sorter.column, sorter.line, board, &item); - usleep(1000000); + usleep(SLEEP_TIME); } while (item == EMPTY); /* because the sorter allways moves, if it drops the item it will not @@ -115,7 +119,7 @@ void launch_sorter (board_t *board) while (item != EMPTY) { drop_item(sorter.column, sorter.line, board, &item); move_sorter(board, &sorter); - usleep(1000000); + usleep(SLEEP_TIME); } /* leave the premisses */ @@ -206,67 +210,32 @@ void print_board(board_t *board) assert(board->size_valid); assert(board->surface_valid); - //conglClearScreen(); - //conglMoveCursor(1, 1); - //Display *disp; - //Window win; - - //const int cell_height = 5; - //const int cell_width = 5; - //const int win_width = cell_width*WIDTH; - //const int win_height = cell_height*HEIGHT; - - //disp = XOpenDisplay(":0"); - //if (disp == NULL) { - // fprintf(stderr, "Cannot connect to X server %s\n", ":0"); - // exit (-1); - //} - - //if (DisplayWidth(disp, DefaultScreen(disp)) < win_width) { - // fprintf(stderr, "Screen too small.\n"); - // exit(-1); - //} - //if (DisplayHeight(disp, DefaultScreen(disp)) < win_height) { - // fprintf(stderr, "Screen too small.\n"); - // exit(-1); - //} - - //win = XCreateSimpleWindow( - // disp, - // RootWindow(disp, DefaultScreen(disp)), - // 0, - // 0, - // win_width, - // win_height, - // 1, - // BlackPixel(disp, DefaultScreen(disp)), - // WhitePixel(disp, DefaultScreen(disp))); - //XMapWindow(disp, win); - //XFlush(disp); + conglClearScreen(); + conglMoveCursor(1, 1); } void print_item(unsigned int column, unsigned int line, item_t item) { struct properties_s { - // conglColor_t bg; - // conglColor_t fg; + conglColor_t bg; + conglColor_t fg; char symbol; }; typedef struct properties_s properties_t; properties_t properties[] = { - // {congl_magenta, congl_white, 'i'}, - // {congl_green, congl_white, 'f'}, - // {congl_black, congl_white, 's'} - {'i'}, {'f'}, {'s'} + {congl_magenta, congl_white, ' '}, + {congl_green, congl_white, ' '}, + {congl_black, congl_white, ' '} + //{'i'}, {'f'}, {'s'} }; properties_t p; p = properties[item]; - //conglDrawCharFull(column+1, line+1, p.symbol, p.fg, p.bg); + conglDrawCharFull(column+1, line+1, p.symbol, p.fg, p.bg); - printf("%c: %d,%d\n", p.symbol, line, column); + //printf("%c: %d,%d\n", p.symbol, line, column); } /* Read an item from the board. */ @@ -312,11 +281,12 @@ void fetch_item(unsigned int column, unsigned int line, item_t *item) int number; - /* generate one block in every fifty board cells */ - number = random()%4; + /* generate one block in every ten board cells */ + number = random()%10; switch (number) { case 0: *item = A; + break; default: *item = EMPTY; } @@ -326,10 +296,10 @@ void setup_board(board_t *board) assert (board->size_valid); assert (!board->surface_valid); - int line, column; + unsigned int line, column; item_t item; - printf("Preparing board.\n"); + //printf("Preparing board.\n"); /* allocate space for the board */ board->surface = calloc (board->height, sizeof(board->surface)); @@ -346,7 +316,9 @@ void setup_board(board_t *board) } board->surface_valid = true; + //printf("Printing board.\n"); print_board(board); + //printf("Board printed.\n"); /* fill the board with initial values */ for (line = 0; line < board->height; ++line) { @@ -376,9 +348,32 @@ void finish_board (board_t *board) assert (!board->surface_valid); } +void count_items(board_t *board, item_t *item, unsigned int *nitems) +{ + assert (board != NULL); + assert (item != NULL); + assert (nitems != NULL); + + unsigned int column, line; + item_t this_item; + unsigned int count; + + count = 0; + for (line = 0; line < board->height; ++line) { + for (column = 0; column < board->width; ++column) { + peek_item(column, line, board, &this_item); + if (this_item == *item) count++; + } + } + + *nitems = count; +} + int main (int argc, char *argv[]) { board_t board; + unsigned int nitems_begin, nitems_finish; + item_t item; board.width = WIDTH; board.height = HEIGHT; @@ -391,14 +386,21 @@ int main (int argc, char *argv[]) setup_board (&board); sleep(1); + item = A; + count_items(&board, &item, &nitems_begin); + /* launch sorters */ launch_sorter(&board); - /* wait for keypress */ - getchar(); + count_items(&board, &item, &nitems_finish); + + /* make shure the sorter isn't eating items */ + assert(nitems_begin == nitems_finish); finish_board (&board); + printf("All good.\n"); + exit(0); } -- 2.11.4.GIT