예제 #1
0
파일: font.cpp 프로젝트: JeppeSRC/TheThing
Font::Font(const String& filename) {

	unsigned char* rawData = nullptr;
	int_d size = 0;

	if (!load_binary_file(filename, &rawData, &size)) {
		//TODO: error
	}
	
	FONT_HEADER* header = (FONT_HEADER*)rawData;

	this->x = header->xCount;
	this->y = header->yCount;

	texture = new Texture(rawData + sizeof(FONT_HEADER) + header->DataSize, header->ImageSize);

	CHAR_INFO* cInfo = (CHAR_INFO*)(rawData + sizeof(FONT_HEADER));

	if (header->CalcCoords) {

		float xStep = 1.0f / (float)x;
		float yStep = 1.0f / (float)y;

		for (uint8 yp = 0; yp < y; yp++) {
			for (uint8 xp = 0; xp < x; xp++) {
				
				float x0 = (float)xp * xStep;
				float x1 = x0 + xStep;
				float y0 = (float)yp * yStep;
				float y1 = y0 + yStep;

				cInfo->texCoords[0] = vec2(x0, y0);
				cInfo->texCoords[1] = vec2(x1, y0);
				cInfo->texCoords[2] = vec2(x1, y1);
				cInfo->texCoords[3] = vec2(x0, y1);

				charMap.addObject(*cInfo);
				cInfo++;

			}
		}
	} else {
		
		for (uint16 i = 0; i < x*y; i++) {
			charMap.addObject(*cInfo);
			cInfo++;
		}

	}


	delete rawData;

}
예제 #2
0
char*
asim_load_file(const char* realfilename)
{
	long len;
	char* str = load_binary_file( realfilename, &len );

	if (str != NULL && len >= 0) 
		str[len] = '\0';

	return str;
}
예제 #3
0
/* Load file for DUMP function */
struct TAPE_BLOCKS *load_file(char *infile,char *filefmt,char recfm,int *recl,int *recc,size_t *filesz)
{
    struct TAPE_BLOCKS *blks;
    switch(filefmt[0])
    {
        case 'B':
            blks=load_binary_file(infile,recfm,recl,recc,filesz);
            break;
        case 'T':
            blks=load_text_file(infile,recfm,recl,recc,filesz);
            break;
        case 'S':
            blks=load_structured_file(infile,recfm,recl,recc,filesz);
            break;
        default:
            fprintf(stderr,"*** INTERNAL ERROR 0001\n");
            return NULL;
    }
    return blks;
}
예제 #4
0
파일: font.cpp 프로젝트: JeppeSRC/TheThing
bool create_font(const String& filename, const String& texture, const String& order, const String& charInfo, uint8 width, uint8 height, bool genCoords) {
	#ifdef _DEBUG
	printf("Creating font \"%s\"\n", *filename);
	#endif
	FILE* file;
	fopen_s(&file, *filename, "wb");

	if (!file) {
		//TODO: error
		return false;
	}

	Font::FONT_HEADER header;

	header.CalcCoords = !genCoords;
	header.xCount = width;
	header.yCount = height;
	header.DataSize = sizeof(Font::CHAR_INFO) * width * height;

	unsigned char* imageData = nullptr;
	
	int_d size = 0;

	if (!load_binary_file(texture, &imageData, &size)) {
		//TODO: error
	}

	header.ImageSize = (uint32)size;

	fwrite(&header, sizeof(Font::FONT_HEADER), 1, file);


	List<Font::CHAR_INFO> spec = parseFontSpec(charInfo);
	

	#ifdef _DEBUG
	printf("\t Writing all characters!\n");
	#endif
	float xStep = 1.0f / (float)width;
	float yStep = 1.0f / (float)height;
	uint16 i = 0;
	for (uint8 yp = 0; yp < height; yp++) {
		for (uint8 xp = 0; xp < width; xp++) {
			float x0 = (float)xp * xStep;
			float x1 = x0 + xStep;
			float y0 = (float)yp * yStep;
			float y1 = y0 + yStep;
		//	Font::CHAR_INFO cInfo(order[xp + yp * width], vec2(x0, y1), vec2(x1, y1), vec2(x1, y0), vec2(x0, y0));
			Font::CHAR_INFO cInfo = getCharInfoFromSpec(spec, order[xp + yp * width]);

			if (genCoords) {
				cInfo.texCoords[0] = vec2(x0, y0);
				cInfo.texCoords[1] = vec2(x1, y0);
				cInfo.texCoords[2] = vec2(x1, y1);
				cInfo.texCoords[3] = vec2(x0, y1);
			}
			#ifdef _DEBUG
			printf("\t\t Writing %c\n", cInfo.c);
			#endif
			fwrite(&cInfo, sizeof(Font::CHAR_INFO), 1, file);
		}
	}

	#ifdef _DEBUG
	printf("\t Done!\n");
	printf("\t Writing texture\n");
	#endif
	fwrite(imageData, header.ImageSize, 1, file);

	fclose(file);

	#ifdef _DEBUG
	printf("All Done!\n");
	#endif

	return true;
}
예제 #5
0
bool DXShader::Load(const String& vertexFileName, const String& pixelFileName)
{
	HRESULT hr = S_OK;

	String vertexFile("data/shaders/dx/");
	vertexFile.Append(vertexFileName);

	// create vertex shader
	void* vBuffer = nullptr;
	unsigned vSize = load_binary_file(&vBuffer, vertexFile.Data());
	hr = _Device->CreateVertexShader(vBuffer, vSize, NULL, &vertexShader);
	if(FAILED(hr))
	{
		delete[] vBuffer;
		LOG_ISSUE("DIRECTX ERROR: %s - failed to load vertex shader: %s",
			hresult_text(hr).Data(), vertexFileName.Data());
		return false;
	}

	// create input layout
	const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,    0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }
	};
	const D3D11_INPUT_ELEMENT_DESC particleVertexLayoutDesc[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0,	D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR",	  0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT,		 0, 32, D3D11_INPUT_PER_VERTEX_DATA, 0 }
	};

	const D3D11_INPUT_ELEMENT_DESC* inputLayoutDesc = (layoutType == 0) ? basicVertexLayoutDesc : particleVertexLayoutDesc;
	unsigned inputDescSize = (layoutType == 0) ? ARRAYSIZE(basicVertexLayoutDesc) : ARRAYSIZE(particleVertexLayoutDesc);

	hr = _Device->CreateInputLayout(inputLayoutDesc, inputDescSize, vBuffer, vSize, &inputLayout);
	delete[] vBuffer;
	if(FAILED(hr))
	{
		LOG_ISSUE("DIRECTX ERROR: %s - failed to create input layout "
			"for vertex shader: %s", hresult_text(hr).Data(), vertexFileName.Data());
		return false;
	}

	// create pixel shader
	String pixelFile("shaders/dx/");
	pixelFile.Append(pixelFileName);

	void* psBuffer = nullptr;
	unsigned psSize = load_binary_file(&psBuffer, pixelFile.Data());
	hr = _Device->CreatePixelShader(psBuffer, psSize, NULL, &pixelShader);
	delete[] psBuffer;
	if(FAILED(hr))
	{
		LOG_ISSUE("DIRECTX ERROR: %s - failed to load pixel shader: %s",
			hresult_text(hr).Data(), pixelFileName.Data());
		return false;
	}

	// initialize constants
	numVertexConstants = 3;
	vertexConstants[0].name = "model";
	vertexConstants[0].type = ShaderConstant::MAT4X4;
	vertexConstants[1].name = "view";
	vertexConstants[1].type = ShaderConstant::MAT4X4;
	vertexConstants[2].name = "projection";
	vertexConstants[2].type = ShaderConstant::MAT4X4;
	
	numPixelConstants = 1;
	pixelConstants[0].name = "color";
	pixelConstants[0].type = ShaderConstant::VEC4;

	int count = 0;
	for(int i = 0; i < numVertexConstants; i++)
	{
		vertexConstants[i].location = count;
		count += vertexConstants[i].GetSize();
	}
	count = 0;
	for(int i = 0; i < numPixelConstants; i++)
	{
		pixelConstants[i].location = count;
		count += pixelConstants[i].GetSize();
	}

	// create constant buffers
	int vertexConstantBufferSize = 0;
	for(int i = 0; i < numVertexConstants; i++)
		vertexConstantBufferSize += vertexConstants[i].GetSize();

	D3D11_BUFFER_DESC constantBufferDesc = {0};
	constantBufferDesc.ByteWidth = sizeof(float) * vertexConstantBufferSize;
	constantBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
	constantBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	constantBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	_Device->CreateBuffer(&constantBufferDesc, nullptr, &vertCB);
	if(FAILED(hr))
	{
		LOG_ISSUE("DIRECTX ERROR: %s - failed to create constant buffer "
			"for shaders: %s and %s", hresult_text(hr).Data(), vertexFileName.Data(),
			pixelFileName.Data());
		return false;
	}

	int pixelConstantBufferSize = 0;
	for(int i = 0; i < numPixelConstants; i++)
		pixelConstantBufferSize += pixelConstants[i].GetSize();

	D3D11_BUFFER_DESC pixelCBDesc = {0};
	pixelCBDesc.ByteWidth = sizeof(float) * pixelConstantBufferSize;
	pixelCBDesc.Usage = D3D11_USAGE_DYNAMIC;
	pixelCBDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
	pixelCBDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	_Device->CreateBuffer(&pixelCBDesc, nullptr, &pixelCB);
	if(FAILED(hr))
	{
		LOG_ISSUE("DIRECTX ERROR: %s - failed to create pixel constant buffer "
			"for shaders: %s and %s", hresult_text(hr).Data(), vertexFileName.Data(),
			pixelFileName.Data());
		return false;
	}
	return true;
}
예제 #6
0
int
main(int argc, char *argv[]) {
  if (test_set_intersection() != 0) {
    return EXIT_FAILURE;
  }
  printf("Smoke tests pass, starting engine\n");

  // set up command line params
  unsigned short arg = 1;
  char *set_ids_filename = argv[arg++],
       *set_index_filename = argv[arg++],
       *set_members_filename = argv[arg++],
       *suggestions_filename = argv[arg++];
  double good_threshold = atof(argv[arg++]);

  // optional param with default
  unsigned int begin_at = 0;
  if (argc > 6) {
    begin_at = atoi(argv[arg]);
    printf("Using optional begin_at parameter: %d \n", begin_at);
  }

  printf("%s\n", argv[0]);
  printf("set_ids_filename: %s \n", set_ids_filename);
  printf("set_index_filename: %s \n", set_index_filename);
  printf("set_members_filename: %s \n", set_members_filename);
  printf("suggestions_filename: %s \n", suggestions_filename);
  printf("good_threshold: %.3f \n", good_threshold);
  printf("begin_at: %d \n", begin_at);


  struct fileinfo set_ids_file = load_binary_file(set_ids_filename);
  unsigned int *set_ids = (unsigned int*)(set_ids_file.head);
  unsigned int set_id_count = set_ids_file.filesize / sizeof(unsigned int);
  printf("Loaded %d sets\n", set_id_count);

  // visual inspection sanity check
  first_10_elements(set_ids, set_ids_filename);

  // set_index_filename: binary image of set id file offsets in set_members_filename
  //  acts as a set_id -> memory offset lookup table
  //  eg, indexarray[set_id] == arrays + offset_of_set_id_in_image
  struct fileinfo indexarray = load_binary_file(set_index_filename);
  unsigned int *indexptr = (unsigned int*) indexarray.head;

  // visual inspection sanity check
  first_10_elements((unsigned int*)indexarray.head, set_index_filename);
  
  // set_members_filename : binary image of set id membership arrays
  struct fileinfo arrays = load_binary_file(set_members_filename);
  const unsigned int *arraysptr = (unsigned int*) arrays.head;

  // visual inspection sanity check
  first_10_elements((unsigned int*)arrays.head, set_members_filename);


  pid_t pids[NUM_PROCESSES];
  int active_pids = NUM_PROCESSES;
  printf("Spinning up %d worker processes...\n", NUM_PROCESSES);
  print_progress_headers();
  for (int i = 0; i < active_pids; i++) {
    pids[i] = fork();
    if (pids[i] < 0) {
      perror(NULL);
      return EXIT_FAILURE;
    } else if (pids[i] == 0) {
      unsigned long intersection_count;
      int started_at = (int)time(NULL);
      char *process_filename = malloc(80);
      sprintf(process_filename, "%s.%u", suggestions_filename, i);
      FILE* fout = fopen(process_filename, "w");
    
      for (int a = begin_at + i; a < set_id_count; a+= NUM_PROCESSES) {
        similarity_for_set(
          a,
          fout,
          good_threshold,
          set_ids,
          set_id_count,
          indexptr,
          arraysptr,
          arrays
        );
        if (0 == a % 10) { print_progress_headers(); }
      }
      fclose(fout);
      printf("Segment starting at %d finished\n", begin_at + i);
      return EXIT_SUCCESS;
    }
  }

  pid_t pid;
  int status;
  while (active_pids > 0) {
    pid = wait(&status);
    printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status);
    active_pids--;
  }
  printf("\nSuggestomatic success!\n");
  return EXIT_SUCCESS;
}