bool painter::begin(const dc_context *context) { _context = context; _text_alignment = GetTextAlign(_context->hdc); assert(_text_alignment != GDI_ERROR); _text_color = parse_palette_color(_context->hdc, GetTextColor(_context->hdc)); if (_text_color == CLR_INVALID) _text_color = 0; // transparent DC may not have background color _bg_color = parse_palette_color(_context->hdc, GetBkColor(_context->hdc)); return true; }
TileStatus Tile::load_from_file(const char *filename) { FILE *file = fopen(filename, "r"); if (!file) { return TILE_FILE_NOT_FOUND; } char line[32]; s32 num_value; bool valid_number; // Get tile width fgets(line, sizeof(line), file); chop(line); valid_number = intval(line, &num_value); valid_number = valid_number && (num_value >= 0 && num_value <= 255); if (!valid_number) { fclose(file); return TILE_INVALID_FILE; } width = num_value; // Get tile height fgets(line, sizeof(line), file); chop(line); valid_number = intval(line, &num_value); valid_number = valid_number && (num_value >= 0 && num_value <= 255); if (!valid_number) { fclose(file); return TILE_INVALID_FILE; } height = num_value; // Should be a blank line now fgets(line, sizeof(line), file); // Now make the palette, loop until we hit a blank line Color palette[10] = { TRANSPARENT }; u8 i = 1; while (strcmp("\n", fgets(line, sizeof(line), file))) { palette[i] = parse_palette_color(line); i++; } // Allocate tile data if (data) { delete [] data; data = 0; } data = new Color [width * height]; // Finally, read the tile data char current_char; u32 index = 0; while (!feof(file)) { current_char = fgetc(file); if (current_char >= '0' && current_char <= '9') { // Make sure we're not past the end of the allocated data. if (index == width * height) { delete [] data; data = 0; fclose(file); return TILE_INVALID_FILE; } data[index] = palette[current_char - '0']; index++; } } // Make sure we got enough data if (index < width * height) { delete [] data; data = 0; fclose(file); return TILE_INVALID_FILE; } fclose(file); return TILE_OK; }