コード例 #1
0
ファイル: tarray.c プロジェクト: cgorman/NODElib
int main(int argc, char **argv)
{
  ARRAY *a;
  SCAN *scan;
  char *str;
  int i;

  scan = scan_create(1, stdin);
  scan->delims = "+-=/";
  scan->whites = " \t\n";
  scan->comments = "";
  
  a = array_create(5, char);

  printf("> ");
  while((str = scan_get(scan)) != NULL) {
    if(*str == '=') {
      scan->delims = ""; scan->whites = "\n";
      str = scan_get(scan);
      scan->delims = "+-=/"; scan->whites = " \t\n";
      array_clear(a);
      array_append_ptr(a, str, strlen(str));
    }
    else if(*str == '+') {
      scan->delims = ""; scan->whites = "\n";
      str = scan_get(scan);
      scan->delims = "+-=/"; scan->whites = " \t\n";
      printf("append(%s)\n", str);
      array_append_ptr(a, str, strlen(str));      
    }
    else if(*str == '-') {
      scan->delims = ""; scan->whites = "\n";
      str = scan_get(scan);
      scan->delims = "+-=/"; scan->whites = " \t\n";
      printf("prepend(%s)\n", str);
      array_prepend_ptr(a, str, strlen(str));      
    }
    else if(*str == '/') {
      str = scan_get(scan);
      array_destroy_index(a, atoi(str));
    }
    else if(*str == '^') {
      str = scan_get(scan);
      i = atoi(str);
      str = scan_get(scan);
      scan->delims = "+-=/"; scan->whites = " \t\n";
      array_insert_index(a, *str, char, i);
    }
    str = array_to_pointer(a);
    printf(" \"%s\"\n> ", str);
    xfree(str);
  }
コード例 #2
0
ファイル: text_renderer.c プロジェクト: arkanis/hdswitch
static text_renderer_cell_p find_free_cell_or_revoke_unused_cell(text_renderer_p renderer, text_renderer_font_p font, uint32_t glyph_width, uint32_t glyph_height, uint32_t texture_width, uint32_t texture_height, size_t* line_idx, size_t* cell_idx) {
	// Padding between the glyphs
	const uint32_t padding = 1;
	// Keep track of the texture coordinates so we know where our cell is on the texture
	uint32_t x = 0, y = 0;
	
	// First search for a line with the matching height
	for(size_t i = 0; i < renderer->lines->length; i++) {
		text_renderer_line_p line = &array_data(renderer->lines, text_renderer_line_t)[i];
		
		if (line->height == glyph_height) {
			// We found a line with matching height, now look if we can get our cell in there
			x = 0;
			for(size_t j = 0; j < line->cells->length; j++) {
				text_renderer_cell_p current_cell = array_elem_ptr(line->cells, j);
				x += current_cell->width + padding;
			}
			
			if (x + glyph_width <= texture_width) {
				// There is space left at the end of this line, add our cell here
				text_renderer_cell_p cell = array_append_ptr(line->cells);
				memset(cell, 0, sizeof(text_renderer_cell_t));
				
				cell->x = x;
				cell->y = y;
				cell->width = glyph_width;
				
				*line_idx = i;
				*cell_idx = line->cells->length - 1;
				return cell;
			}
			
			// No space at end of line, search next line
		}
		
		y += line->height + padding;
	}
	
	// We haven't found a matching line or every matching line was full. Anyway add a new line
	// if there is space left at the bottom of the texture and put the cell in there.
	if (y + glyph_height + padding <= texture_height) {
		text_renderer_line_p line = array_append_ptr(renderer->lines);
		line->height = glyph_height;
		line->cells = array_of(text_renderer_cell_t);
		
		text_renderer_cell_p cell = array_append_ptr(line->cells);
		memset(cell, 0, sizeof(text_renderer_cell_t));
		
		cell->x = x;
		cell->y = y;
		cell->width = glyph_width;
		
		*line_idx = renderer->lines->length - 1;
		*cell_idx = 0;
		return cell;
	}
	
	// We would like to add a new line but there is not enough free space at the bottom
	// to the texture. For now just give up.
	*line_idx = (size_t)-1;
	*cell_idx = (size_t)-1;
	return NULL;
}