Give cursor its own WINDOW.
This commit is contained in:
		
							parent
							
								
									e70a87feac
								
							
						
					
					
						commit
						6a08aad3d3
					
				
							
								
								
									
										19
									
								
								lib/cursor.c
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								lib/cursor.c
									
									
									
									
									
								
							@ -13,14 +13,24 @@ void allocate_cursor(struct cursor **cursor) {
 | 
			
		||||
  if (!(*cursor = malloc(sizeof(**cursor)))) {
 | 
			
		||||
    fprintf(stderr, tty_solitaire_error_message(errno, __FILE__, __LINE__));
 | 
			
		||||
    exit(errno);
 | 
			
		||||
  } else {
 | 
			
		||||
    (*cursor)->window = NULL;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initialize_cursor(struct cursor *cursor) {
 | 
			
		||||
  cursor->window = newwin(0, 0, cursor->y, cursor->x);
 | 
			
		||||
  cursor->x = CURSOR_BEGIN_X;
 | 
			
		||||
  cursor->y = CURSOR_BEGIN_Y;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void free_cursor(struct cursor *cursor) {
 | 
			
		||||
  if (cursor) {
 | 
			
		||||
    delwin(cursor->window);
 | 
			
		||||
  }
 | 
			
		||||
  free(cursor);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void move_cursor(struct cursor *cursor, enum movement movement) {
 | 
			
		||||
  switch (movement) {
 | 
			
		||||
  case LEFT:
 | 
			
		||||
@ -31,7 +41,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
 | 
			
		||||
        move_cursor(cursor, UP);
 | 
			
		||||
        move_cursor(cursor, DOWN);
 | 
			
		||||
      }
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
    }
 | 
			
		||||
    break;
 | 
			
		||||
  case DOWN:
 | 
			
		||||
@ -60,7 +69,6 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
 | 
			
		||||
        cursor->y = cursor->y + 7 + length(deck->maneuvre_6);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
    }
 | 
			
		||||
    break;
 | 
			
		||||
  case RIGHT:
 | 
			
		||||
@ -71,20 +79,13 @@ void move_cursor(struct cursor *cursor, enum movement movement) {
 | 
			
		||||
        move_cursor(cursor, UP);
 | 
			
		||||
        move_cursor(cursor, DOWN);
 | 
			
		||||
      }
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
    }
 | 
			
		||||
    break;
 | 
			
		||||
  case UP:
 | 
			
		||||
    if (cursor->y > 1) {
 | 
			
		||||
      erase_cursor(cursor);
 | 
			
		||||
      cursor->y = CURSOR_BEGIN_Y;
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
    }
 | 
			
		||||
    break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /* this is needed because of the screen glitch that moving the cursor
 | 
			
		||||
   * on the maneuvre's stacks causes */
 | 
			
		||||
  refresh();
 | 
			
		||||
  draw_deck(deck);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -25,6 +25,7 @@
 | 
			
		||||
#define CURSOR_MANEUVRE_6_X   52
 | 
			
		||||
 | 
			
		||||
struct cursor {
 | 
			
		||||
  WINDOW *window;
 | 
			
		||||
  int x;
 | 
			
		||||
  int y;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -156,9 +156,10 @@ void draw_deck(struct deck *deck) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void draw_cursor(struct cursor *cursor) {
 | 
			
		||||
  mvaddch(cursor->y, cursor->x, '*');
 | 
			
		||||
  mvwaddch(cursor->window, cursor->y, cursor->x, '*');
 | 
			
		||||
  wrefresh(cursor->window);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void erase_cursor(struct cursor *cursor) {
 | 
			
		||||
  mvdelch(cursor->y, cursor->x);
 | 
			
		||||
  mvwdelch(cursor->window, cursor->y, cursor->x);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -291,4 +291,5 @@ void initialize_game() {
 | 
			
		||||
 | 
			
		||||
void end_game() {
 | 
			
		||||
  free_deck(deck);
 | 
			
		||||
  free_cursor(cursor);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -72,18 +72,22 @@ static void handle_card_movement(struct cursor *cursor) {
 | 
			
		||||
    case 'h':
 | 
			
		||||
    case KEY_LEFT:
 | 
			
		||||
      move_cursor(cursor, LEFT);
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
      break;
 | 
			
		||||
    case 'j':
 | 
			
		||||
    case KEY_DOWN:
 | 
			
		||||
      move_cursor(cursor, DOWN);
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
      break;
 | 
			
		||||
    case 'k':
 | 
			
		||||
    case KEY_UP:
 | 
			
		||||
      move_cursor(cursor, UP);
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
      break;
 | 
			
		||||
    case 'l':
 | 
			
		||||
    case KEY_RIGHT:
 | 
			
		||||
      move_cursor(cursor, RIGHT);
 | 
			
		||||
      draw_cursor(cursor);
 | 
			
		||||
      break;
 | 
			
		||||
    case KEY_SPACEBAR:
 | 
			
		||||
      destination = cursor_stack(cursor);
 | 
			
		||||
@ -96,6 +100,7 @@ static void handle_card_movement(struct cursor *cursor) {
 | 
			
		||||
    case 'q':
 | 
			
		||||
    case 'Q':
 | 
			
		||||
      end_curses();
 | 
			
		||||
      end_game();
 | 
			
		||||
      exit(0);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@ -106,18 +111,22 @@ void handle_keyboard_event(int key) {
 | 
			
		||||
  case 'h':
 | 
			
		||||
  case KEY_LEFT:
 | 
			
		||||
    move_cursor(cursor, LEFT);
 | 
			
		||||
    draw_cursor(cursor);
 | 
			
		||||
    break;
 | 
			
		||||
  case 'j':
 | 
			
		||||
  case KEY_DOWN:
 | 
			
		||||
    move_cursor(cursor, DOWN);
 | 
			
		||||
    draw_cursor(cursor);
 | 
			
		||||
    break;
 | 
			
		||||
  case 'k':
 | 
			
		||||
  case KEY_UP:
 | 
			
		||||
    move_cursor(cursor, UP);
 | 
			
		||||
    draw_cursor(cursor);
 | 
			
		||||
    break;
 | 
			
		||||
  case 'l':
 | 
			
		||||
  case KEY_RIGHT:
 | 
			
		||||
    move_cursor(cursor, RIGHT);
 | 
			
		||||
    draw_cursor(cursor);
 | 
			
		||||
    break;
 | 
			
		||||
  case KEY_SPACEBAR:
 | 
			
		||||
    if (cursor_on_stock(cursor)) {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user