// build a program with a vertex shader and a fragment shader
GLuint buildProgram(const std::string vertexFile, const std::string fragmentFile)
{
	auto vshader = buildShader(GL_VERTEX_SHADER, fileGetContents(vertexFile));
	auto fshader = buildShader(GL_FRAGMENT_SHADER, fileGetContents(fragmentFile));

	GLuint program = glCreateProgram();

	glAttachShader(program, vshader);
	glAttachShader(program, fshader);

	glLinkProgram(program);

	GLint res;
	glGetProgramiv(program, GL_LINK_STATUS, &res);
	if(!res)
	{
		std::cerr << "program link error" << std::endl;

		char message[1000];

		GLsizei readSize;
		glGetProgramInfoLog(program, 1000, &readSize, message);
        message[999] = '/0';

		std::cerr << message << std::endl;

		glfwTerminate();
		exit(-1);
	}

	return program;
}
Пример #2
0
/*
 * Get a string parameter as a file spec or as a "contents follow -" spec
 */
static void
get_dash_string(char **s)
{
    if (**s == '-')
        *s = xstrdup(*s + 1);
    else
        *s = fileGetContents(*s);
}
Пример #3
0
/* Get a string parameter as a file spec or as a "contents follow -" spec */
char *
get_dash_string(char **str)
{
    char *s = *str;

    if (*s == '-')
        *str = copy_string_adds_newline(s + 1);
    else
        *str = fileGetContents(s);
    return *str;
}
Пример #4
0
	void init( HWND hWnd )
	{
		HRESULT hr;

		DXGI_SWAP_CHAIN_DESC scDesc;
		memset( &scDesc, 0, sizeof scDesc );
		scDesc.BufferDesc.Width = g_windowWidth;
		scDesc.BufferDesc.Height = g_windowHeight;
		scDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
		scDesc.BufferDesc.RefreshRate.Denominator = 1;
		scDesc.BufferDesc.RefreshRate.Numerator = 60;
		scDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
		scDesc.BufferCount = 1;
		scDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
		scDesc.SampleDesc.Count = 1;
		scDesc.Windowed = TRUE;
		scDesc.OutputWindow = hWnd;

		D3D_FEATURE_LEVEL features[] = { D3D_FEATURE_LEVEL_11_0 };

		UINT flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#ifndef NDEBUG
		flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

		IDXGISwapChain* swapChain;
		ID3D11Device* device;
		ID3D11DeviceContext* context;
		hr = D3D11CreateDeviceAndSwapChain(
			nullptr, D3D_DRIVER_TYPE_HARDWARE, NULL, flags, features, ARRAYSIZE( features ),
			D3D11_SDK_VERSION, &scDesc, &swapChain, &device, &featureLevel_, &context );
		Assert( hr );
		swapChain_.reset( swapChain );
		device_.reset( device );
		context_.reset( context );

		ID3D11Texture2D* backBuffer;
		ID3D11RenderTargetView* backBufferRTV;
		hr = swapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast< void** >( &backBuffer ) );
		Assert( hr );
		hr = device_->CreateRenderTargetView( backBuffer, nullptr, &backBufferRTV );
		Assert( hr );
		backBufferRTV_.reset( backBufferRTV );
		backBuffer->Release();

		// Common state

		ID3D11RasterizerState* rs;
		D3D11_RASTERIZER_DESC rsDesc;
		memset( &rsDesc, 0, sizeof rsDesc );
		rsDesc.CullMode = D3D11_CULL_BACK;
		rsDesc.FillMode = D3D11_FILL_SOLID;
		rsDesc.DepthClipEnable = TRUE;
		hr = device_->CreateRasterizerState( &rsDesc, &rs );
		Assert( hr );
		rasterState_.reset( rs );

		// Body

		std::string binData;
		std::string vsBinData;
		ID3D11VertexShader* vs;
		binData = fileGetContents( "def.vs.cso" );
		hr = device_->CreateVertexShader(
			reinterpret_cast< const void* >( binData.c_str() ), binData.size(), nullptr, &vs );
		Assert( hr );
		modelVS_.reset( vs );
		vsBinData = binData;

		ID3D11PixelShader* ps;
		binData = fileGetContents( "def.ps.cso" );
		hr = device_->CreatePixelShader(
			reinterpret_cast< const void* >( binData.c_str() ), binData.size(), nullptr, &ps );
		Assert( hr );
		modelPS_.reset( ps );

		D3D11_BUFFER_DESC bufDesc = CD3D11_BUFFER_DESC( 64, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DEFAULT, 0 );
		ID3D11Buffer* buf;
		hr = device_->CreateBuffer( &bufDesc, nullptr, &buf );
		Assert( hr );
		modelCB_.reset( buf );

		// 四角錐ポリゴン
		static const MeshFormat pyramidVertex[] = {
			{ -1.0f,  0.0f, -1.0f, 0xFF33AAAA },
			{  1.0f,  0.0f, -1.0f, 0xFF33AAAA },
			{ -1.0f,  0.0f,  1.0f, 0xFF33AAAA },
		
			{ -1.0f,  0.0f,  1.0f, 0xFF33AAAA },
			{  1.0f,  0.0f, -1.0f, 0xFF33AAAA },
			{  1.0f,  0.0f,  1.0f, 0xFF33AAAA },

			{  1.0f,  0.0f, -1.0f, 0xFFEE33BB },
			{ -1.0f,  0.0f, -1.0f, 0xFFEE33BB },
			{  0.0f,  1.0f,  0.0f, 0xFFEE33BB },
		
			{ -1.0f,  0.0f, -1.0f, 0xFFCC33BB },
			{ -1.0f,  0.0f,  1.0f, 0xFFCC33BB },
			{  0.0f,  1.0f,  0.0f, 0xFFCC33BB },
		
			{ -1.0f,  0.0f,  1.0f, 0xFFAA33BB },
			{  1.0f,  0.0f,  1.0f, 0xFFAA33BB },
			{  0.0f,  1.0f,  0.0f, 0xFFAA33BB },
		
			{  1.0f,  0.0f,  1.0f, 0xFF8833BB },
			{  1.0f,  0.0f, -1.0f, 0xFF8833BB },
			{  0.0f,  1.0f,  0.0f, 0xFF8833BB },
		};

		bufDesc = CD3D11_BUFFER_DESC( sizeof pyramidVertex, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_IMMUTABLE, 0 );
		D3D11_SUBRESOURCE_DATA subresData;
		subresData.pSysMem = pyramidVertex;
		subresData.SysMemPitch = subresData.SysMemSlicePitch = 0;
		hr = device_->CreateBuffer( &bufDesc, &subresData, &buf );
		Assert( hr );
		modelVB_.reset( buf );

		D3D11_INPUT_ELEMENT_DESC ieDesc[] = {
				{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
				{ "COLOR", 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 }
		};
		ID3D11InputLayout* il;
		hr = device_->CreateInputLayout( ieDesc, ARRAYSIZE( ieDesc ), vsBinData.data(), vsBinData.size(), &il );
		Assert( hr );
		modelIL_.reset( il );
	}
Пример #5
0
void CsvParser::load(const char *file){
	LOG_POSEIDON_DEBUG("Loading CSV file: ", file);

	StreamBuffer buffer;
	fileGetContents(buffer, file);
	buffer.put('\n');

	std::vector<OptionalMap> data;

	std::vector<std::vector<std::string> > rows;
	{
		std::vector<std::string> row;
		std::string token;
		bool first = true;
		bool inQuote = false;
		do {
			char ch = buffer.get();
			if(ch == '\r'){
				if(buffer.peek() == '\n'){
					buffer.get();
				}
				ch = '\n';
			}

			if(first){
				first = false;

				if(ch == '\"'){
					inQuote = true;
					continue;
				}
			}

			if(ch == '\"'){
				if(inQuote){
					if(buffer.peek() == '\"'){
						buffer.get();
						token.push_back('\"');
					} else {
						inQuote = false;
					}
					continue;
				}
			}

			if(!inQuote){
				if((ch == ',') || (ch == '\n')){
					std::string trimmed;
					const std::size_t begin = token.find_first_not_of(" \t\r\n");
					if(begin != std::string::npos){
						const std::size_t end = token.find_last_not_of(" \t\r\n") + 1;
						trimmed = token.substr(begin, end - begin);
					}
					row.push_back(STD_MOVE(trimmed));
					token.clear();
					first = true;

					if(ch == '\n'){
						rows.push_back(STD_MOVE(row));
						row.clear();
					}
					continue;
				}
			}

			token.push_back(ch);
		} while(!buffer.empty());
	}
	if(rows.empty() || rows.front().empty()){
		LOG_POSEIDON_ERROR("The first line of a CSV file may not be empty.");
		DEBUG_THROW(Exception, sslit("Bad CSV header"));
	}

	const std::size_t columnCount = rows.front().size();
	std::vector<SharedNts> keys(columnCount);
	for(std::size_t i = 0; i < columnCount; ++i){
		AUTO_REF(key, rows.front().at(i));
		for(std::size_t j = 0; j < i; ++j){
			if(keys.at(j) == key){
				LOG_POSEIDON_ERROR("Duplicate key: ", key);
				DEBUG_THROW(Exception, sslit("Duplicate key"));
			}
		}
		keys.at(i).assign(key.c_str());
	}
	for(std::size_t i = 1; i < rows.size(); ++i){
		rows.at(i - 1).swap(rows.at(i));
	}
	rows.pop_back();

	{
		std::size_t line = 1;
		std::size_t i = 0;
		while(i < rows.size()){
			AUTO_REF(row, rows.at(i));
			++line;
			if((row.size() == 1) && row.front().empty()){
				for(std::size_t j = i + 1; j < rows.size(); ++j){
					rows.at(j - 1).swap(rows.at(j));
				}
				rows.pop_back();
				continue;
			}
			if(row.size() != columnCount){
				LOG_POSEIDON_ERROR("There are ", row.size(), " column(s) on line ", line,
					" but there are ", columnCount, " in the header");
				DEBUG_THROW(Exception, sslit("Inconsistent CSV column numbers"));
			}
			++i;
		}
	}

	const std::size_t rowCount = rows.size();
	data.resize(rowCount);
	for(std::size_t i = 0; i < rowCount; ++i){
		AUTO_REF(row, rows.at(i));
		AUTO_REF(map, data.at(i));
		for(std::size_t j = 0; j < columnCount; ++j){
			map.create(keys.at(j))->second.swap(row.at(j));
		}
	}

	LOG_POSEIDON_DEBUG("Done loading CSV file: ", file);
	m_data.swap(data);
	m_row = static_cast<std::size_t>(-1);
}
Пример #6
0
 void readGroups(){
   groups = JSON::parse(fileGetContents(groups_location));
 };