int write_all(struct ELM_handle * h, const char * data, size_t size) { size_t offset = 0; log_write(data, size); while (offset < size) { if (wait(h, POLLOUT) == 0) { ssize_t write_result = write(h->fd, data + offset, size - offset); if (write_result > 0) offset += write_result; else { log_fail(); sprintf(h->error_msg, "write(...) error: %s", strerror(errno)); return -1; } } else { log_fail(); return -1; } } /* while (offset < size) */ log_ok(); return 0; }
char* readFromFile(char* pathToFile) { char* content = (char *)malloc(sizeof(char) * 4096); int fd; struct stat fileStat; fd = open(pathToFile, O_RDWR); if (fd == -1) { // File did not exist. return NULL; } if (fstat(fd, &fileStat) == -1) { log_fail("readFromFile: Failed to get stats on file.\n"); exit(1); } if ((content = (char *) mmap(0, fileStat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { log_fail("readFromFile: Failed to mmap file\n"); exit(1); } //close(pathToFile); return content; }
bool is_readable_file(const char *file) { if (file == NULL) { fatal("file == NULL"); } struct stat stat_buffer; int rc = stat(file, &stat_buffer); if (rc != 0) { log_fail("stat(): '%s' (%d)", strerror(errno), errno); goto ret_false; } /* check if we have a file */ if (!S_ISREG(stat_buffer.st_mode)) { log_fail("S_ISREG"); goto ret_false; } /* check if file is readable */ rc = access(file, R_OK); if (rc != 0) { log_fail("access(): '%s' (%d)", strerror(errno), errno); goto ret_false; } return true; ret_false: return false; }
int main(int argc, char *argv[]) { int opt; const char *input_name = nullptr; const char *output_name = nullptr; const char *lang_name = nullptr; Input input; while ((opt = getopt(argc, argv, "i:o:l:I:")) != -1) { switch (opt) { case 'i': input_name = optarg; break; case 'o': output_name = optarg; break; case 'l': lang_name = optarg; break; case 'I': input.addPath(object<Path>(optarg)); break; default: usage(); } } if (!input_name || !output_name || !lang_name) { usage(); } Token::init(); Language::reg<CppLang>(); Language::reg<LuaVarLang>(); Language::reg<GoLang>(); input.load(object<Path>(input_name), false); SymbolTable symbols; Parser parser(input, symbols); parser.parse(); Language *lang = Language::get(lang_name); if (!lang) { log_fail("unknown language name '%s'", lang_name); } FILE *output = fopen(output_name, "w"); if (!output) { perror("open output file failed."); exit(1); } lang->print(symbols, output); fclose(output); return 0; }
int read_all(struct ELM_handle * h) { log_next("<<"); size_t offset = 0; while (offset < BUFFER_SIZE) { if (wait(h, POLLIN) == 0) { ssize_t read_result = read(h->fd, h->reply + offset, BUFFER_SIZE - offset); if (read_result > 0) { offset += read_result; if (offset > 0 && h->reply[offset - 1] == '>') { log_write(h->reply, offset); log_ok(); h->reply[offset - 2] = '\0'; /* remove last \r> */ return 0; } } else { log_write(h->reply, offset); log_fail(); sprintf(h->error_msg, "read(...) error: %s", strerror(errno)); return -1; } } else { log_write(h->reply, offset); log_fail(); return -1; } } /* while (offset < buffer_size) */ log_write(h->reply, offset); log_fail(); sprintf(h->error_msg, "Internal error: buffer small"); return -1; }
void shader_attach(struct Shader* p, GLenum type, const char* prefix_file, size_t n, ...) { log_assert( p->num_objects + n < MAX_SHADER_OBJECTS ); va_list file_list; va_start(file_list, n); size_t start_i = p->num_objects; size_t end_i = start_i + n; for( size_t i = start_i; i < end_i; i++ ) { const char* shader_file = va_arg(file_list, const char*); if( shader_file != NULL && strlen(shader_file) > 0 ) { size_t path_str_alloc = strlen(CUTE_SHADER_SEARCH_PATH) + strlen(prefix_file) + strlen(shader_file) + 2; char* path_prefix_file = malloc(path_str_alloc); log_assert( path_prefix_file != NULL ); bool found_prefix_file = path_search_path(CUTE_SHADER_SEARCH_PATH, prefix_file, path_str_alloc, path_prefix_file); char* path_shader_file = malloc(path_str_alloc); log_assert( path_shader_file != NULL ); bool found_shader_file = path_search_path(CUTE_SHADER_SEARCH_PATH, shader_file, path_str_alloc, path_shader_file); if( found_shader_file && found_prefix_file ) { p->objects[i] = glsl_compile_file(type, path_prefix_file, path_shader_file); p->num_objects += 1; log_assert( p->objects[i] > 0 ); } else { log_fail(__FILE__, __LINE__, "file not found:"); if( ! found_prefix_file) { log_continue(" %s", prefix_file); } if( ! found_shader_file ) { log_continue(" %s", shader_file); } log_continue("\n"); } free(path_prefix_file); free(path_shader_file); } } va_end(file_list); }
int32_t init_shader() { int32_t ret = 0; // - we don't actually need this, but I want to check anyways because ubo's are // so useful and I would like to known when I can't use them in the future if( ! SDL_GL_ExtensionSupported("GL_ARB_uniform_buffer_object") ) { log_fail(__FILE__, __LINE__, "uniform_buffer_object extension not found!\n"); ret = 1; } GLint max_vertex_attribs = 0; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs); log_info(__FILE__, __LINE__, "gl maximum vertex attribs: %d\n", max_vertex_attribs); log_assert( MAX_SHADER_ATTRIBUTES < max_vertex_attribs ); GLint max_fragment_texture_image_units = 0; GLint max_vertex_texture_image_units = 0; GLint max_combined_texture_image_units = 0; glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_fragment_texture_image_units); glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &max_vertex_texture_image_units); glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &max_combined_texture_image_units); log_info(__FILE__, __LINE__, "gl maximum fragment texture image units: %d\n", max_fragment_texture_image_units); log_info(__FILE__, __LINE__, "gl maximum vertex texture image units: %d\n", max_vertex_texture_image_units); log_info(__FILE__, __LINE__, "gl maximum combined texture image units: %d\n", max_combined_texture_image_units); for( size_t i = 0; i < MAX_SHADER_ATTRIBUTE_NAMES; i++ ) { for( size_t j = 0; j < MAX_SHADER_ATTRIBUTES; j++ ) { global_shader_attribute_names[i][j] = "invalid_attribute"; } } for( size_t i = 0; i < MAX_SHADER_UNIFORMS; i++ ) { global_shader_uniform_names[i] = "invalid_uniform"; } for( size_t i = 0; i < MAX_SHADER_SAMPLER; i++ ) { global_shader_sampler_names[i] = "invalid_sampler"; } // - initialize the first four attribute names of every set to be the names of the default geometry // attributes for( size_t i = 0; i < MAX_SHADER_ATTRIBUTE_NAMES; i++ ) { global_shader_attribute_names[i][SHADER_ATTRIBUTE_VERTEX] = "vertex"; global_shader_attribute_names[i][SHADER_ATTRIBUTE_VERTEX_TEXCOORD] = "vertex_texcoord"; global_shader_attribute_names[i][SHADER_ATTRIBUTE_VERTEX_NORMAL] = "vertex_normal"; global_shader_attribute_names[i][SHADER_ATTRIBUTE_VERTEX_COLOR] = "vertex_color"; } // - we can set custom attribute name sets like so global_shader_attribute_names[SHADER_CANVAS_NAMES][SHADER_ATTRIBUTE_INSTANCE_ID] = "instance_id"; global_shader_attribute_names[SHADER_CANVAS_NAMES][SHADER_ATTRIBUTE_PREV_VERTEX] = "prev_vertex"; global_shader_attribute_names[SHADER_CANVAS_NAMES][SHADER_ATTRIBUTE_NEXT_VERTEX] = "next_vertex"; global_shader_attribute_names[SHADER_CANVAS_NAMES][SHADER_ATTRIBUTE_LINE_THICKNESS] = "line_thickness"; global_shader_uniform_names[SHADER_UNIFORM_MVP_MATRIX] = "mvp_matrix"; global_shader_uniform_names[SHADER_UNIFORM_MODEL_MATRIX] = "model_matrix"; global_shader_uniform_names[SHADER_UNIFORM_VIEW_MATRIX] = "view_matrix"; global_shader_uniform_names[SHADER_UNIFORM_PROJECTION_MATRIX] = "projection_matrix"; global_shader_uniform_names[SHADER_UNIFORM_NORMAL_MATRIX] = "normal_matrix"; global_shader_uniform_names[SHADER_UNIFORM_DIFFUSE_LIGHT] = "diffuse_light"; global_shader_uniform_names[SHADER_UNIFORM_AMBIENT_LIGHT] = "ambient_light"; global_shader_uniform_names[SHADER_UNIFORM_SPECULAR_LIGHT] = "specular_light"; global_shader_uniform_names[SHADER_UNIFORM_LIGHT_DIRECTION] = "light_direction"; global_shader_uniform_names[SHADER_UNIFORM_LIGHT_POSITION] = "light_position"; global_shader_uniform_names[SHADER_UNIFORM_LIGHT_ATTENUATION] = "light_attenuation"; global_shader_uniform_names[SHADER_UNIFORM_MATERIAL_SHININESS] = "material_shininess"; global_shader_uniform_names[SHADER_UNIFORM_MATERIAL_COEFFICIENTS] = "material_coefficients"; global_shader_uniform_names[SHADER_UNIFORM_EYE_POSITION] = "eye_position"; global_shader_uniform_names[SHADER_UNIFORM_ASPECT_RATIO] = "aspect_ratio"; global_shader_uniform_names[SHADER_UNIFORM_ENABLE_TEXTURE] = "enable_texture"; global_shader_uniform_names[SHADER_UNIFORM_LINE_ATTENUATION] = "line_attenuation"; global_shader_sampler_names[SHADER_SAMPLER_DIFFUSE_TEXTURE] = "diffuse_texture"; return ret; }
void bsp_tree_create_from_solid(struct BspTree* tree, struct Solid* solid) { size_t alloc_attributes_result = bsp_tree_alloc_attributes(tree, solid->attributes_size); log_assert( alloc_attributes_result >= solid->attributes_size ); size_t num_polygons = solid->indices_size/3; size_t alloc_polygons_result = bsp_tree_alloc_polygons(tree, num_polygons); log_assert( alloc_polygons_result >= num_polygons ); size_t alloc_nodes_result = bsp_tree_alloc_nodes(tree, num_polygons); log_assert( alloc_nodes_result >= num_polygons ); int32_t* workset_polygons_front = malloc(alloc_polygons_result * sizeof(int32_t)); log_assert( workset_polygons_front != NULL ); int32_t* workset_polygons_back = malloc(alloc_polygons_result * sizeof(int32_t)); log_assert( workset_polygons_back != NULL ); float min_x = FLT_MAX; float min_y = FLT_MAX; float min_z = FLT_MAX; float max_x = -FLT_MAX; float max_y = -FLT_MAX; float max_z = -FLT_MAX; for( size_t indices_i = 0; indices_i < solid->indices_size+1; indices_i++ ) { uint32_t src_i = solid->indices[indices_i]; if( indices_i < solid->indices_size ) { VecP* src = &solid->vertices[src_i*VERTEX_SIZE]; VecP* dst = &tree->attributes.vertices[indices_i*VERTEX_SIZE]; vec_copy3f(src, dst); tree->attributes.occupied += 1; if( src[0] < min_x ) { min_x = src[0]; } if( src[1] < min_y ) { min_y = src[1]; } if( src[2] < min_z ) { min_z = src[2]; } if( src[0] > max_x ) { max_x = src[0]; } if( src[1] > max_y ) { max_y = src[1]; } if( src[2] > max_z ) { max_z = src[2]; } } if( indices_i > 0 && indices_i % 3 == 0 ) { size_t poly_i = indices_i / 3 - 1; tree->polygons.array[poly_i].start = poly_i*3*VERTEX_SIZE; tree->polygons.array[poly_i].size = 3; polygon_normal(3, VERTEX_SIZE, &tree->attributes.vertices[poly_i*3*VERTEX_SIZE], tree->polygons.array[poly_i].normal); tree->polygons.occupied += 1; workset_polygons_front[poly_i] = poly_i; } } struct BspNode* root = &tree->nodes.array[0]; bsp_node_create(root); tree->nodes.occupied = 1; root->bounds.half_width = (max_x - min_x)/2.0f; root->bounds.half_height = (max_y - min_y)/2.0f; root->bounds.half_depth = (max_z - min_z)/2.0f; root->bounds.center[0] = min_x + root->bounds.half_width; root->bounds.center[1] = min_y + root->bounds.half_height; root->bounds.center[2] = min_z + root->bounds.half_depth; root->divider = 0; bsp_select_balanced_divider(tree, root, num_polygons, workset_polygons_front, &root->divider); struct BspPoly* root_divider = &tree->polygons.array[root->divider]; const float* root_divider_polygon = &tree->attributes.vertices[root_divider->start]; draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 0, 0, 255}, 0.01f, root_divider->size, root_divider_polygon, root_divider->normal); draw_vec(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 0, 0, 255}, 0.01f, root_divider->normal, &root_divider_polygon[3], 1.0f, 0.1f); /* draw_plane(&global_static_canvas, MAX_CANVAS_LAYERS-1, (Mat)IDENTITY_MAT, (Color){120, 120, 150, 127}, root_divider->normal, &root_divider_polygon[3], 10.0f); */ for( size_t polygon_i = 0; polygon_i < num_polygons; polygon_i++ ) { size_t cuts_polygon_size = 3; const float* cuts_polygon = &tree->attributes.vertices[polygon_i*cuts_polygon_size*VERTEX_SIZE]; size_t result_size = cuts_polygon_size; struct PolygonCutPoints result_points[cuts_polygon_size]; enum PolygonCutType result_type = polygon_cut(cuts_polygon_size, VERTEX_SIZE, cuts_polygon, root_divider->normal, root_divider_polygon, result_size, result_points); Vec3f cuts_polygon_normal = {0}; polygon_normal(3, VERTEX_SIZE, cuts_polygon, cuts_polygon_normal); switch(result_type) { case POLYGON_COPLANNAR: //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 255, 255}, 0.01f, result_size, cuts_polygon, cuts_polygon_normal); break; case POLYGON_FRONT: //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 0, 255, 255}, 0.01f, result_size, cuts_polygon, cuts_polygon_normal); break; case POLYGON_BACK: //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){0, 0, 255, 255}, 0.01f, result_size, cuts_polygon, cuts_polygon_normal); break; case POLYGON_SPANNING: //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, (Color){255, 255, 0, 255}, 0.01f, result_size, cuts_polygon, cuts_polygon_normal); if( result_points[0].num_cuts > 0 ) { size_t new_poly_size = cuts_polygon_size+result_points[0].num_cuts+10; size_t front_occupied = 0; float front_vertices[new_poly_size*VERTEX_SIZE]; size_t back_occupied = 0; float back_vertices[new_poly_size*VERTEX_SIZE]; for( size_t result_i = 0; result_i < result_size; result_i++ ) { if( result_points[result_i].type == POLYGON_BACK ) { vec_copy3f(&cuts_polygon[result_i*VERTEX_SIZE], &back_vertices[back_occupied*VERTEX_SIZE]); back_occupied += 1; } else if( result_points[result_i].type == POLYGON_FRONT ) { vec_copy3f(&cuts_polygon[result_i*VERTEX_SIZE], &front_vertices[front_occupied*VERTEX_SIZE]); front_occupied += 1; } else if( result_points[result_i].type == POLYGON_COPLANNAR ) { vec_copy3f(&cuts_polygon[result_i*VERTEX_SIZE], &back_vertices[back_occupied*VERTEX_SIZE]); back_occupied += 1; vec_copy3f(&cuts_polygon[result_i*VERTEX_SIZE], &front_vertices[front_occupied*VERTEX_SIZE]); front_occupied += 1; } if( result_points[result_i].interpolation_index > -1 ) { const VecP* a = &cuts_polygon[result_i*VERTEX_SIZE]; const VecP* b = &cuts_polygon[result_points[result_i].interpolation_index*VERTEX_SIZE]; Vec3f r = {0}; vec_lerp(b, a, result_points[result_i].interpolation_value, r); vec_copy3f(r, &back_vertices[back_occupied*VERTEX_SIZE]); back_occupied += 1; vec_copy3f(r, &front_vertices[front_occupied*VERTEX_SIZE]); front_occupied += 1; } } //printf("front_occupied: %lu\n", front_occupied); //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, red, 0.01f, front_occupied, front_vertices, cuts_polygon_normal); //draw_polygon_wire(&global_static_canvas, 0, (Mat)IDENTITY_MAT, white, 0.01f, back_occupied, back_vertices, cuts_polygon_normal); } break; } } log_fail(__FILE__, __LINE__, "BUILT BSP TREE... OR NOT?\n"); }