Ejemplo n.º 1
0
config* cfg_load_file(const char* filename) {

  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  if(file == NULL) {
    error("Cannot load file %s", filename);
  }
  
  config* c = malloc(sizeof(config));
  c->entries = dict_new(512);
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    if (line[0] == '#') continue;
    if (line[0] == '\r') continue;
    if (line[0] == '\n') continue;
    
    char key[1024];
    char val[1024];
    
    if (sscanf(line, "%[^ \r\n=] = %[^ \r\n=]", key, val) == 2) {
      char* val_cpy = malloc(strlen(val) + 1);
      strcpy(val_cpy, val);
      dict_set(c->entries, key, val_cpy);
    }
    
  }
  
  SDL_RWclose(file);
  
  return c;
  
}
Ejemplo n.º 2
0
shader_program* prog_load_file(char* filename) {

  shader_program* sp = shader_program_new();
  
  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  
  if(file == NULL) {
    error("Could not load file %s", filename);
  }
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    char type[256];
    char path[1024];
    if (sscanf(line, "%256s : %1024s", type, path) == 2) {
      if(!asset_loaded(path)) {
        load_file(path);
      }
      shader* s = asset_get(path);
      shader_program_attach_shader(sp, s);
    }
  
  }
  
  SDL_RWclose(file);
  
  shader_program_link(sp);
  shader_program_print_log(sp);
  
  int error = 0;
  glGetProgramiv(*sp, GL_LINK_STATUS, &error);
  if (error == GL_FALSE) {
    error("Linking Error on Shader Program %s.", filename);
  }
  
  return sp;
  
} 
Ejemplo n.º 3
0
lang* lang_load_file(const char* filename) {
  
  lang* t = malloc(sizeof(lang));
  t->map = dict_new(512);
  
  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  
  if(file == NULL) {
    error("Could not load file %s", filename);
  }
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    char id[1024];
    char text[1024];
    if (sscanf(line, "%s %[^\r\n]", id, text) == 2) {
      
      /* Replace newlines */
      for(int i = 0; i < strlen(text); i++) {
        if (text[i] == '\\' && text[i+1] == 'n') {
          text[i] = ' ';
          text[i+1] = '\n';
        }
      }
      
      char* text_cpy = malloc(strlen(text) + 1); strcpy(text_cpy, text);
      dict_set(t->map, id, text_cpy);
    }
  
  }
  
  SDL_RWclose(file);
  
  return t;
  
}
Ejemplo n.º 4
0
animation* ani_load_file(char* filename) {
  
  int state = STATE_LOAD_EMPTY;
  
  animation* a =  animation_new();
  skeleton* base = skeleton_new();
  frame* f = NULL;
  
  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  
  if(file == NULL) {
    error("Could not load file %s", filename);
  }
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    if (state == STATE_LOAD_EMPTY) {
      
      int version;
      if (sscanf(line, "version %i", &version) > 0) {
        if (version != 1) {
          error("Can't load ani file '%s'. Don't know how to load version %i\n", filename, version);
        }
      }
      
      if (strstr(line, "nodes")) {
        state = STATE_LOAD_NODES;
      }
      
      if (strstr(line, "skeleton")) {
        state = STATE_LOAD_SKELETON;
      }
    }
    
    else if (state == STATE_LOAD_NODES) {
      char name[1024];
      int id, parent;
      if (sscanf(line, "%i \"%[^\"]\" %i", &id, name, &parent) == 3) {
        skeleton_joint_add(base, name, parent);
      }
      
      if (strstr(line, "end")) {
        state = STATE_LOAD_EMPTY;
      }
    }
    
    else if (state == STATE_LOAD_SKELETON) {
    
      float time;
      if (sscanf(line, "time %f", &time) == 1) {
        f = animation_add_frame(a, base->rest_pose);
      }
    
      int id;
      float x, y, z, rx, ry, rz;
      if (sscanf(line, "%i %f %f %f %f %f %f", &id, &x, &y, &z, &rx, &ry, &rz) > 0) {
        
        f->joint_positions[id] = vec3_new(x, z, y);
        
        mat4 rotation = mat4_rotation_euler(rx, ry, rz);
        mat4 handedflip = mat4_new(1,0,0,0,
                                   0,0,1,0,
                                   0,1,0,0,
                                   0,0,0,1);
      
        rotation = mat4_mul_mat4(handedflip, rotation);
        rotation = mat4_mul_mat4(rotation, handedflip);
        rotation = mat4_transpose(rotation);
        
        f->joint_rotations[id] = mat4_to_quat(rotation);
        
      }
      
      if (strstr(line, "end")) {
        state = STATE_LOAD_EMPTY;
      }
      
    }
  }
  
  SDL_RWclose(file);
  
  skeleton_delete(base);
  
  return a;
}
Ejemplo n.º 5
0
font* font_load_file(char* filename) {
  
  font* f = malloc(sizeof(font));
  f->width = 0;
  f->height = 0;
  
  /* Encodes ASCII */
  f->locations = malloc( sizeof(vec2) * 256 );
  f->sizes = malloc( sizeof(vec2) * 256 );
  f->offsets = malloc( sizeof(vec2) * 256 );
  
  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  
  if(file == NULL) {
    error("Could not load file %s", filename);
  }
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    int tex_id;
    char tex_file[MAX_PATH];
    if (sscanf(line, "page id=%i file=%s", &tex_id, tex_file) > 0) {
      
      fpath location;
      SDL_PathFileLocation(location.ptr, filename);
      
      /* +1 to remove beginning quotation */
      strcat(location.ptr, tex_file+1); 
      
      /* remove ending quotation */
      location.ptr[strlen(location.ptr)-1] = '\0';
      
      f->texture_map = asset_hndl_new_load(location);
    }
    
    int lineheight, base, scalew, scaleh;
    int pages, packed, a_chan, r_chan, g_chan, b_chan;
    if (sscanf(line, "common lineHeight=%i base=%i scaleW=%i scaleH=%i "
                     "pages=%i packed=%i alphaChnl=%i "
                     "redChnl=%i greenChnl=%i blueChnl=%i", 
                     &lineheight, &base, &scalew, &scaleh, 
                     &pages, &packed, &a_chan, 
                     &r_chan, &g_chan, &b_chan) > 0) {
                     
      f->width = scalew;
      f->height = scaleh;
    }
    
    int id, x, y, w, h, x_off, y_off, x_adv, page, chnl;
    if (sscanf(line, "char id=%i x=%i y=%i width=%i height=%i "
                     "xoffset=%i yoffset=%i xadvance=%i page=%i chnl=%i", 
                     &id, &x, &y, &w, &h, &x_off, 
                     &y_off, &x_adv, &page, &chnl) > 0) {
      
      f->locations[id] = vec2_new((float)x / f->width, (float)y / f->height);
      f->sizes[id] = vec2_new((float)w / f->width, (float)h / f->height);
      f->offsets[id] = vec2_new((float)x_off / f->width, (float)y_off / f->height);
    }
  }
  
  SDL_RWclose(file);
  
  return f;
}
Ejemplo n.º 6
0
material* mat_load_file(char* filename) {
  
  SDL_RWops* file = SDL_RWFromFile(filename, "r");
  if(file == NULL) {
    error("Cannot load file %s", filename);
  }
  
  material* m = material_new();
  material_entry* me = material_add_entry(m);
  
  char line[1024];
  while(SDL_RWreadline(file, line, 1024)) {
    
    if (line[0] == '#') { continue; }
    if (line[0] == '\r') { continue; }
    if (line[0] == '\n') { continue; }
    
    if (strstr(line, "submaterial")) {
      
      /* Skip Empty Submaterials */
      if (me->num_items == 0) {
        continue;
      } else {
        me = material_add_entry(m);
        continue;
      }
      
    }
    
    char type[512]; char name[512]; char value[512];
    int matches = sscanf(line, "%511s %511s = %511s", type, name, value);
    
    if (matches != 3) continue;
    
    material_item mi;
    int type_id;
    char* end;
    float f0, f1, f2, f3;
    
    if (strcmp(type, "shader") == 0) {
    
      mi.as_asset = asset_hndl_new_load(P(value));
      type_id = mat_item_shader;
      
    } else if (strcmp(type, "texture") == 0) {
    
      mi.as_asset = asset_hndl_new_load(P(value));
      type_id = mat_item_texture;
    
    } else if (strcmp(type, "int") == 0) {
    
      mi.as_int = atoi(value);
      type_id = mat_item_int;
    
    } else if (strcmp(type, "float") == 0) {
      
      mi.as_float = atof(value);
      type_id = mat_item_float;
      
    } else if (strcmp(type, "vec2") == 0) {
    
      f0 = strtod(value, &end); f1 = strtod(end, NULL);
      mi.as_vec2 = vec2_new(f0, f1);
      type_id = mat_item_vec2;
      
    } else if (strcmp(type, "vec3") == 0) {
      
      f0 = strtod(value, &end); f1 = strtod(end, &end);
      f2 = strtod(end, NULL);
      mi.as_vec3 = vec3_new(f0, f1, f2);
      type_id = mat_item_vec3;
      
    } else if (strcmp(type, "vec4") == 0) {
    
      f0 = strtod(value, &end); f1 = strtod(end, &end);
      f2 = strtod(end, &end); f3 = strtod(end, NULL);
      mi.as_vec4 = vec4_new(f0, f1, f2, f3);
      type_id = mat_item_vec4;
      
    } else {
      error("Unknown material item type '%s'", type);
      return NULL;
    }
    
    material_entry_add_item(me, name, type_id, mi);
    
  }
  
  SDL_RWclose(file);
  
  material_generate_programs(m);
  
  SDL_GL_CheckError();
  
  return m;
}
Ejemplo n.º 7
0
level* level_load_file(char* filename) {

	for(int i = 0; i < num_tile_types; i++) {
		tile_counts[i] = 0;
	}

	level* l = malloc(sizeof(level));
	l->num_tile_sets = num_tile_types;
	l->tile_sets = malloc(sizeof(tile_set) * num_tile_types);
	l->tile_map = calloc(sizeof(int), MAX_WIDTH * MAX_HEIGHT);

	SDL_RWops* file = SDL_RWFromFile(filename, "r");
	char line[MAX_WIDTH];

	int y = 0;
	int x = 0;
	while(SDL_RWreadline(file, line, 1024)) {

		for(x = 0; x < strlen(line); x++) {
			char c = line[x];
			int type = tile_for_char(c);

			l->tile_map[x + y * MAX_WIDTH] = type;
			tile_counts[type]++;
		}

		y++;
	}

	SDL_RWclose(file);

	/* Start from 1, type 0 is none! */
	for(int i = 1; i < num_tile_types; i++) {

		int num_tiles = tile_counts[i];

		float* position_data = malloc(sizeof(float) * 3 * 4 * num_tiles);
		float* uv_data = malloc(sizeof(float) * 2 * 4 * num_tiles);

		int pos_i  = 0;
		int uv_i = 0;

		for(x = 0; x < MAX_WIDTH; x++)
			for(y = 0; y < MAX_HEIGHT; y++) {
				int type = l->tile_map[x + y * MAX_WIDTH];
				if( type == i ) {

					position_data[pos_i] = x * TILE_SIZE; pos_i++;
					position_data[pos_i] = y * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = y * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = (x+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					position_data[pos_i] = x * TILE_SIZE; pos_i++;
					position_data[pos_i] = (y+1) * TILE_SIZE; pos_i++;
					position_data[pos_i] = 0; pos_i++;

					uv_data[uv_i] = 0; uv_i++;
					uv_data[uv_i] = 0; uv_i++;

					uv_data[uv_i] = 1; uv_i++;
					uv_data[uv_i] = 0; uv_i++;

					uv_data[uv_i] = 1; uv_i++;
					uv_data[uv_i] = 1; uv_i++;

					uv_data[uv_i] = 0; uv_i++;
					uv_data[uv_i] = 1; uv_i++;

				}
			}

		l->tile_sets[i].num_tiles = num_tiles;

		glGenBuffers(1, &l->tile_sets[i].positions_buffer);
		glGenBuffers(1, &l->tile_sets[i].texcoords_buffer);

		glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].positions_buffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 4 * num_tiles, position_data, GL_STATIC_DRAW);

		glBindBuffer(GL_ARRAY_BUFFER, l->tile_sets[i].texcoords_buffer);
		glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 4 * num_tiles, uv_data, GL_STATIC_DRAW);

		glBindBuffer(GL_ARRAY_BUFFER, 0);

		free(position_data);
		free(uv_data);

	}

	return l;
}