⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 EGui是一个开源的图形系统软件,类似于QT/Embedded、GTK-FB、MicroWindow。目标是嵌入式平台整合解 决方案。基于Linux Framebuffer 设备驱动上实现。有完
💻 C
字号:
#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <unistd.h>#include <string.h>#include <Egui.h>#include <widget.h>EGui_FBinfo Efbinfo;/*  * ((bw * COL) < ww)  and ((bh * ROW) < wh) * ROW * COL % BALL = 0; */#define ROW  10#define COL  12		/* column,row */int bw = 21,  bh = 21;		/* button w,h */int ww = 360, wh = 280;		/* window w,h *//* FOUR can display balls * 0 - 3 * YID2 :will kill * WID  :white,had kill */#define      BALL         5#define      RID          0#define      GID          1#define      BID          2#define      YID          3#define      BID2         4#define      YID2         5#define      WID          6#define      COUNT       (ROW * COL / BALL)static int same_id = 0;static int mark = 0;static char col_kill[COL];struct pos_xy{  short x;  short y;  short id;  short row,col;};struct pos_xy array[ROW][COL];struct pos_xy restore_array[COUNT];EGui_Widget * button_win ;EGui_Widget * button_mark;EGui_Pixmap Pixmap[BALL+2];EGui_Widget *pixmap[ROW][COL];void random_map ();void init_background (EGui_Widget * form,EGui_Window *window);void cal_value ( struct pos_xy *c_array );void cal_kill ( struct pos_xy *c_array );struct pos_xy * get_array (EGui_Widget * widget);void re_draw (void);void restore_other (void);void pixmap_kill_click (EGui_Widget * widget);int check_game_over(void);void call_game_over(void);void add_new_col (void);void pixmap_click ( EGui_Widget     *widget){  struct pos_xy * c_array;  char value[32];  c_array = get_array (widget);    switch(c_array->id)    {    case YID2:      if (same_id == 1)	break;      pixmap_kill_click (widget);      same_id = 0;      break;    case RID:    case GID:    case BID:    case YID:    case BID2:      if (same_id != 0)	restore_other ();            same_id = 0;      cal_value ( c_array );      sprintf (value,"%d + %d",mark,same_id * same_id);      widget_set_name(button_mark,value);      widget_draw (button_mark);          break;    default:      break;    }}voidpixmap_kill_click (EGui_Widget * widget){  char value[32];  struct pos_xy * c_array;  c_array = get_array (widget);  mark += same_id * same_id;    sprintf (value,"%d",mark);  widget_set_name(button_mark,value);  widget_draw (button_mark);  memset(col_kill,0,COL);  cal_kill( c_array);    re_draw ();    /* kill all */  if (array[ROW-1][COL-1].id == WID)    {      mark = mark * 2;      sprintf (value,"%d",mark);      widget_set_name(button_mark,value);      widget_draw (button_mark);      call_game_over ();      return ;    }  while(array[ROW-1][0].id == WID)    add_new_col ();  if (check_game_over())    {      call_game_over();      return ;    }}voidcall_game_over (void){  widget_set_name(button_win,"Game Over");  widget_draw (button_win);}button_win_click (EGui_Widget * widget){  int i,j;  char value[32];  random_map ();    for (i=0;i<ROW;i++)    {      for (j=0;j<COL;j++)	{	  pixmap_set (pixmap[i][j],&Pixmap[array[i][j].id]);	  widget_draw(pixmap[i][j]);	}    }    same_id = 0;  mark = 0;  sprintf (value,"%d",mark);  widget_set_name(button_mark,value);  widget_draw (button_mark);  widget_set_name(button_win,"New Start");  widget_draw (button_win);}voidinit_pos (){  int pos_startx,pos_starty;  int i,j;    pos_startx = (ww - COL * bw) / 2 ;  pos_starty = (wh - ROW * bh) / 2 ;    for (i=0;i<ROW;i++)    {      for (j=0;j<COL;j++)	{	  array[i][j].x  = pos_startx + bw * j ;	  array[i][j].y  = pos_starty + bh * i ;	  array[i][j].id = 6;	  	  array[i][j].row = i;	  array[i][j].col = j;	}    }}voidrandom_map (void){  char ball_id[BALL];  short id;  int i,j;  if (ROW * COL % BALL != 0)    printf ("Warn: row * col % ball != 0\n");  for (i=0;i<BALL;i++)    ball_id[i] = 0;    for (i=0;i<ROW;i++)    {      for (j=0;j<COL;j++)	{	  id = random()  % BALL ;	  while ( ball_id[id] == COUNT)	    {	      id = random() % BALL;	    }	    	  ball_id [id] += 1;	  array[i][j].id = id;	    	}    }  }voidinit_background (EGui_Widget * form,EGui_Window *window){  int i,fx,fy,sx,sy;  int mx,my;  Ecolor color;  new_color( window,&color,0x0);    fx = form->x;  fy = form->y;  for (i=0;i<ROW;i++)    {            sx = array[i][0].x - 1 + fx;      sy = array[i][0].y - 1 + fy;      Egui_line (sx,sy,sx + bw * COL,sy, &color, window);    }  for (i=0;i<COL;i++)    {            sx = array[0][i].x - 1 + fx;      sy = array[0][i].y - 1 + fy;      Egui_line (sx,sy,sx,sy + bh*ROW, &color, window);    }    sx = array[0][0].x - 1 + fx;  sy = array[0][0].y - 1 + fy;  Egui_rect (sx,sy,sx + bw * COL,sy + bh*ROW,&color,window);  }extern unsigned char*decode_jpeg (char *filename, short *widthPtr, short *heightPtr) ;/* 1 is ok * 0 is error */intget_pixmap_form_file (char * file,EGui_Pixmap *P,EGui_Window * window){  unsigned char *buffer;  int p_bpp;  int i;  Ecolor color;  int rgb;  /* get RGB buffer */  buffer = decode_jpeg (file, &P->width, &P->height);  if (buffer == NULL)    return 0;   p_bpp = Efbinfo.p_bpp;    P->buffer = (char*)malloc (p_bpp * P->width * P->height);  if (P->buffer  == NULL)    {      printf ("malloc error P->buffer\n");      free(buffer);      return 0;    }  Egui_bgr_to_pixel(window,P->buffer,buffer, 3 * P->width * P->height);    free (buffer);  return 1;}intinit_pixmap (EGui_Window *window){    if (get_pixmap_form_file ("r.jpg",&Pixmap[RID],window)== 0)    return 0;  if (get_pixmap_form_file ("g.jpg",&Pixmap[GID],window)== 0)    return 0;  if (get_pixmap_form_file ("b.jpg",&Pixmap[BID],window)== 0)    return 0;  if (get_pixmap_form_file ("y.jpg",&Pixmap[YID],window)== 0)    return 0;  if (get_pixmap_form_file ("b2.jpg",&Pixmap[BID2],window)== 0)    return 0;  if (get_pixmap_form_file ("y2.jpg",&Pixmap[YID2],window)== 0)    return 0;  if (get_pixmap_form_file ("w.jpg",&Pixmap[WID],window)== 0)    return 0;  return 1;  }struct pos_xy * get_array (EGui_Widget * widget){  int x, y;  int i, j;  x = widget->x - widget->parent->x;  y = widget->y - widget->parent->y;  for (i=0;i<ROW;i++)    {      for (j=0;j<COL;j++)	{	  if ((array[i][j].x == x) &&	      (array[i][j].y  == y))	    return &array[i][j];	}    }}voidadd_new_col (void){  short id;  int i,j;  for (i=0;i<ROW;i++)    {      for (j=COL-1;j >= 0;j--)	{	  if (array[i][j].id == WID)	    {	      id = random()  % BALL ;	      array[i][j].id = id;	      pixmap_set (pixmap[i][j],&Pixmap[id]);	      widget_draw(pixmap[i][j]);	      break;	    }	}    }}/* return   * 1: can continue * 0: the array is only. */intcheck_can_kill(struct pos_xy *c_array){  short id;  short row,col;  id   = c_array->id;  row  = c_array->row;  col  = c_array->col;    if (id == WID)    return 0;  /* try +1 */  row = row + 1;    if ( row < ROW)    {      if (id == array[row][col].id)	return 1;    }  row = row - 1;  /* end */  /* try -1 */  row = row - 1;    if ( row > -1)    {      if (id == array[row][col].id)	return 1;          }  row = row + 1;  /* try end */      /* try +1 */  col = col + 1;    if ( col < COL)    {      if (id == array[row][col].id)	return 1;         }  col = col - 1;  /* end */  /* try -1 */  col = col - 1;    if ( col > -1)    {      if (id == array[row][col].id)	return 1;     }  col = col + 1;  /* try end */  return 0;}/* return   * 1: game over * 0: can continue game */intcheck_game_over(void){  int i,j;  for (i = COL - 1; i > 0; i--)    {      for (j = ROW -1; j > 0; j--)	{	  if (array[j][i].id == WID)	    break;	  if (check_can_kill(&array[j][i]))	    return 0;	}    }  return 1;}voidcal_value ( struct pos_xy *c_array ){  short id;  short row,col;  id   = c_array->id;  row  = c_array->row;  col  = c_array->col;  /* save information,   * to restore   */  restore_array[same_id].row = row;  restore_array[same_id].col = col;  restore_array[same_id].id  = id;    same_id ++;    pixmap_set (pixmap[row][col],&Pixmap[YID2]);  widget_draw(pixmap[row][col]);  c_array->id = YID2;  /* try +1 */  row = row + 1;    if ( row < ROW)    {      if (id == array[row][col].id)	cal_value (&array[row][col]);    }  row = row - 1;  /* end */  /* try -1 */  row = row - 1;    if ( row > -1)    {      if (id == array[row][col].id)	cal_value (&array[row][col]);          }  row = row + 1;  /* try end */      /* try +1 */  col = col + 1;    if ( col < COL)    {      if (id == array[row][col].id)	cal_value (&array[row][col]);         }  col = col - 1;  /* end */  /* try -1 */  col = col - 1;    if ( col > -1)    {      if (id == array[row][col].id)	cal_value (&array[row][col]);     }  col = col + 1;  /* try end */}voidcal_kill( struct pos_xy *c_array ){  int id;  int row,col;  id   = c_array->id;  row  = c_array->row;  col  = c_array->col;  pixmap_set (pixmap[row][col],&Pixmap[WID]);  widget_draw(pixmap[row][col]);  c_array->id = WID;    col_kill[col] = 1;  /* try +1 */  row = row + 1;    if ( row < ROW)    {      if (id == array[row][col].id)	cal_kill (&array[row][col]);    }  row = row - 1;  /* end */  /* try -1 */  row = row - 1;    if ( row > -1)    {      if (id == array[row][col].id)	cal_kill (&array[row][col]);          }  row = row + 1;  /* try end */      /* try +1 */  col = col + 1;    if ( col < COL)    {      if (id == array[row][col].id)	cal_kill (&array[row][col]);         }  col = col - 1;  /* end */  /* try -1 */  col = col - 1;    if ( col > -1)    {      if (id == array[row][col].id)	cal_kill (&array[row][col]);     }  col = col + 1;  /* try end */}voidrestore_other (void){  short i;  short row,col,id;  for (i = 0; i<same_id;i++)    {      row = restore_array[i].row;      col = restore_array[i].col;      id  = restore_array[i].id;            array[row][col].id = id;      pixmap_set (pixmap[row][col],&Pixmap[id]);      widget_draw(pixmap[row][col]);    }}/*return  *0: isn't empty *1: is empty */intfind_empty_col (int col){  int j;  int ret = 0;  for (j=0;j<ROW;j++)    {      if (array[j][col].id != WID)	break;    }    if (j == ROW)    ret = 1;    return ret;}voidcopy_display_col (int dest_col,int src_col){  int j;  for (j=0;j<ROW;j++)    {      array[j][dest_col].id = array[j][src_col].id;      pixmap_set (pixmap[j][dest_col],&Pixmap[array[j][dest_col].id]);      widget_draw(pixmap[j][dest_col]);            array[j][src_col].id = WID;      pixmap_set (pixmap[j][src_col],&Pixmap[array[j][src_col].id]);      widget_draw(pixmap[j][src_col]);    } }voidre_draw (void){  int i, j,k;  unsigned char w_id[ROW];  for (i=0;i<COL;i++)    {      if (col_kill[i])	{	  memset(w_id,WID,ROW);	  k = ROW - 1;	  for (j = k;j >= 0;j --)	    {	      if (array[j][i].id != WID)		{		  w_id[k] = array[j][i].id;		  k--;		}	    }	  for (j = 0; j<ROW; j++)	    {	      array[j][i].id = w_id[j];	      pixmap_set (pixmap[j][i],&Pixmap[w_id[j]]);	      widget_draw(pixmap[j][i]);	    }	}               }  /* empty col */  for ( i = COL - 1 ; i>=0 ; i--)    {      if (find_empty_col (i))	{	  int ii;	  	  for (ii = i-1 ; ii >= 0; ii-- )	    if (!find_empty_col(ii))	      break;	  if (ii >= 0)	    copy_display_col (i,ii);	}    }}intmap_pixmap (EGui_Widget *form){  int i,j;  for (i=0;i<ROW;i++)    {      for (j=0;j<COL;j++)	{	  pixmap[i][j]     = new_pixmap (form,array[i][j].x,array[i][j].y,20,20);	  pixmap_set (pixmap[i][j],&Pixmap[array[i][j].id]);	  widget_register_callback (pixmap[i][j], BUTTON_CLICK_LEFT,pixmap_click);	  widget_show(pixmap[i][j]);	}    }}intmain(int argc, char **argv){  char * rgbbuf;  int width,height;  Ecolor * bgcolor;  int w,h;  EGui_Window * ewindow;  EGui_Widget * form;  EGui_Widget * win;  EGui_Widget * edit;  if (Egui_open (&Efbinfo))    return ;  bgcolor = malloc (sizeof ( Ecolor));  if (bgcolor == NULL)    {      printf ("malloc color ERROR\n");      return -1;    }  bgcolor->r = bgcolor->g = bgcolor->b = 0xcc;  /* create window will initial color's pixel.    */  ewindow = Egui_CreateWindow (&Efbinfo,10,20,ww,			       wh,bgcolor,EGUI_WINDOW_TOP);  if (ewindow == NULL)    {      printf ("New windows failed\n");      Egui_close ();      return 1;    }  Egui_SetWindowName(ewindow,"Jawbreaker");  Egui_drawwindow(ewindow);    win = new_widget_window(ewindow);  widget_show(win);  form = new_form (win);  widget_show (form);    /* init position */  init_pos ();    /* random map */  random_map ();  if (!init_pixmap(ewindow))    return 1;  map_pixmap(form);  memset(col_kill,0,COL);    button_win = new_button (form,20,255,100,20);  widget_set_name(button_win,"New Start");  widget_show(button_win);  widget_register_callback (button_win, BUTTON_CLICK_LEFT,button_win_click);  button_mark = new_button (form,130,255,100,20);  widget_set_name(button_mark,"0");  widget_show(button_mark);  edit = new_edit(form,240,255,100,20);  widget_show(edit);  widget_draw (win); /* after form draw line */  init_background (form,ewindow);  egui_loop ();  Egui_CloseWindow (ewindow);  free (bgcolor);  Egui_close ();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -