コード例 #1
0
ファイル: wincurse.cpp プロジェクト: Abbysynth/Cataclysm-DDA
//Basic Init, create the font, backbuffer, etc
WINDOW *curses_init(void)
{
   // _windows = new WINDOW[20];         //initialize all of our variables
    lastchar=-1;
    inputdelay=-1;

    int fontsize = 16;
    std::string typeface;
    int map_fontwidth = 8;
    int map_fontheight = 16;
    int map_fontsize = 16;
    std::string map_typeface;
    int overmap_fontwidth = 8;
    int overmap_fontheight = 16;
    int overmap_fontsize = 16;
    std::string overmap_typeface;
    bool fontblending;

    std::ifstream jsonstream(FILENAMES["fontdata"].c_str(), std::ifstream::binary);
    if (jsonstream.good()) {
        JsonIn json(jsonstream);
        JsonObject config = json.get_object();
        // fontsize, fontblending, map_* are ignored in wincurse.
        fontwidth = config.get_int("fontwidth", fontwidth);
        fontheight = config.get_int("fontheight", fontheight);
        typeface = config.get_string("typeface", typeface);
        jsonstream.close();
    } else { // User fontdata is missed. Try to load legacy fontdata.
        // Get and save all values. With unused.
        std::ifstream InStream(FILENAMES["legacy_fontdata"].c_str(), std::ifstream::binary);
        if(InStream.good()) {
            JsonIn jIn(InStream);
            JsonObject config = jIn.get_object();
            fontwidth = config.get_int("fontwidth", fontwidth);
            fontheight = config.get_int("fontheight", fontheight);
            fontsize = config.get_int("fontsize", fontsize);
            typeface = config.get_string("typeface", typeface);
            map_fontwidth = config.get_int("map_fontwidth", fontwidth);
            map_fontheight = config.get_int("map_fontheight", fontheight);
            map_fontsize = config.get_int("map_fontsize", fontsize);
            map_typeface = config.get_string("map_typeface", typeface);
            overmap_fontwidth = config.get_int("overmap_fontwidth", fontwidth);
            overmap_fontheight = config.get_int("overmap_fontheight", fontheight);
            overmap_fontsize = config.get_int("overmap_fontsize", fontsize);
            overmap_typeface = config.get_string("overmap_typeface", typeface);
            InStream.close();
            // Save legacy as user fontdata.
            assure_dir_exist(FILENAMES["config_dir"]);
            std::ofstream OutStream(FILENAMES["fontdata"].c_str(), std::ofstream::binary);
            if(!OutStream.good()) {
                DebugLog( D_ERROR, DC_ALL ) << "Can't save user fontdata file.\n"
                << "Check permissions for: " << FILENAMES["fontdata"].c_str();
                return NULL;
            }
            JsonOut jOut(OutStream, true); // pretty-print
            jOut.start_object();
            jOut.member("fontblending", fontblending);
            jOut.member("fontwidth", fontwidth);
            jOut.member("fontheight", fontheight);
            jOut.member("fontsize", fontsize);
            jOut.member("typeface", typeface);
            jOut.member("map_fontwidth", map_fontwidth);
            jOut.member("map_fontheight", map_fontheight);
            jOut.member("map_fontsize", map_fontsize);
            jOut.member("map_typeface", map_typeface);
            jOut.member("overmap_fontwidth", overmap_fontwidth);
            jOut.member("overmap_fontheight", overmap_fontheight);
            jOut.member("overmap_fontsize", overmap_fontsize);
            jOut.member("overmap_typeface", overmap_typeface);
            jOut.end_object();
            OutStream << "\n";
            OutStream.close();
        } else {
            DebugLog( D_ERROR, DC_ALL ) << "Can't load fontdata files.\n"
            << "Check permissions for:\n" << FILENAMES["legacy_fontdata"].c_str() << "\n"
            << FILENAMES["fontdata"].c_str() << "\n";
            return NULL;
        }
    }

    halfwidth=fontwidth / 2;
    halfheight=fontheight / 2;
    WindowWidth= OPTIONS["TERMINAL_X"] * fontwidth;
    WindowHeight = OPTIONS["TERMINAL_Y"] * fontheight;

    WinCreate();    //Create the actual window, register it, etc
    timeBeginPeriod(1); // Set Sleep resolution to 1ms
    CheckMessages();    //Let the message queue handle setting up the window

    WindowDC   = GetDC(WindowHandle);
    backbuffer = CreateCompatibleDC(WindowDC);

    BITMAPINFO bmi = BITMAPINFO();
    bmi.bmiHeader.biSize         = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth        = WindowWidth;
    bmi.bmiHeader.biHeight       = -WindowHeight;
    bmi.bmiHeader.biPlanes       = 1;
    bmi.bmiHeader.biBitCount     = 8;
    bmi.bmiHeader.biCompression  = BI_RGB; // Raw RGB
    bmi.bmiHeader.biSizeImage    = WindowWidth * WindowHeight * 1;
    bmi.bmiHeader.biClrUsed      = 16; // Colors in the palette
    bmi.bmiHeader.biClrImportant = 16; // Colors in the palette
    backbit = CreateDIBSection(0, &bmi, DIB_RGB_COLORS, (void**)&dcbits, NULL, 0);
    DeleteObject(SelectObject(backbuffer, backbit));//load the buffer into DC

    // Load private fonts
    if (SetCurrentDirectoryW(L"data\\font")){
        WIN32_FIND_DATA findData;
        for (HANDLE findFont = FindFirstFileW(L".\\*", &findData); findFont != INVALID_HANDLE_VALUE; )
        {
            if (!(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){ // Skip folders
                AddFontResourceExW(findData.cFileName, FR_PRIVATE,NULL);
            }
            if (!FindNextFile(findFont, &findData)){
                FindClose(findFont);
                break;
            }
        }
        SetCurrentDirectoryW(L"..\\..");
    }

    // Use desired font, if possible
    font = CreateFontW(fontheight, fontwidth, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                      DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,
                      PROOF_QUALITY, FF_MODERN, widen(typeface).c_str());

    SetBkMode(backbuffer, TRANSPARENT);//Transparent font backgrounds
    SelectObject(backbuffer, font);//Load our font into the DC
//    WindowCount=0;

    init_colors();

    mainwin = newwin(OPTIONS["TERMINAL_Y"],OPTIONS["TERMINAL_X"],0,0);
    return mainwin;   //create the 'stdscr' window and return its ref
}
コード例 #2
0
ファイル: Mesh.cpp プロジェクト: balajeerc/SolarGL
    void Mesh::load(const char* filename)
    {
        //load the mesh dumped from blender as json
        //first read all the json data into a single string from file
        std::ifstream meshfile(filename, std::ios::in | std::ios::binary);
        std::string contents;
        if (meshfile)
        {	
	        meshfile.seekg(0, std::ios::end);
	        contents.resize(meshfile.tellg());
	        meshfile.seekg(0, std::ios::beg);
	        meshfile.read(&contents[0], contents.size());
	        meshfile.close();
        }
        else
        {
	        printf("error: cannot find mesh file!");
	        exit(1);
        }

        //we can now use picojson to parse this string containing the mesh data
        picojson::value v;  
        // read json value from stream
        std::stringstream jsonstream(contents);
        jsonstream >> v;
        if (std::cin.fail())
        {
	        std::cerr << picojson::get_last_error() << std::endl;
	        exit(1);
        }

        // check if the type of the value is "object"
        // root node, in our case must be an object
        if (! v.is<picojson::object>()) {
	        std::cerr << "json is not an object" << std::endl;
	        exit(1);
        }

        const picojson::value::object& root = v.get<picojson::object>();
        for (picojson::value::object::const_iterator i = root.begin(); i != root.end(); ++i)
        {
	        std::string childname = i->first;
	        picojson::value child = i->second;
	        if(childname.compare("vertices")==0)
	        {
		        picojson::array vertslist = child.get<picojson::array>();
		        for (picojson::value::array::const_iterator vertex = vertslist.begin(); vertex != vertslist.end(); ++vertex)
		        {
			        picojson::array coords = vertex->get<picojson::array>();
			        float x = coords[0].get<double>();
			        float y = coords[1].get<double>();
                    float z = coords[2].get<double>();
			        _vertices.push_back(x);
			        _vertices.push_back(y);
                    _vertices.push_back(z);
		        }
	        }
	        else if(childname.compare("faces")==0)
	        {
		        picojson::array facelist = child.get<picojson::array>();
		        for (picojson::value::array::const_iterator face = facelist.begin(); face != facelist.end(); ++face)
		        {
			        picojson::array vertindices = face->get<picojson::array>();
			        unsigned int v1 = (unsigned short)vertindices[0].get<double>();
			        unsigned int v2 = (unsigned short)vertindices[1].get<double>();
			        unsigned int v3 = (unsigned short)vertindices[2].get<double>();
			        _faceIndices.push_back(v1);
			        _faceIndices.push_back(v2);
			        _faceIndices.push_back(v3);
		        }
	        }
            else if(childname.compare("face_normals")==0)
	        {
		        picojson::array facelist = child.get<picojson::array>();
		        for (picojson::value::array::const_iterator face = facelist.begin(); face != facelist.end(); ++face)
		        {
			        picojson::array vertindices = face->get<picojson::array>();
			        float nx = (float)vertindices[0].get<double>();
			        float ny = (float)vertindices[1].get<double>();
			        float nz = (float)vertindices[2].get<double>();
			        _faceNormals.push_back(nx);
			        _faceNormals.push_back(ny);
			        _faceNormals.push_back(nz);
		        }
	        }
	        else if((childname.compare("texture_coords")==0))
	        {
		        picojson::array texcoordslist = child.get<picojson::array>();
		        for (picojson::value::array::const_iterator vertex = texcoordslist.begin(); vertex != texcoordslist.end(); ++vertex)
		        {
			        picojson::array coords = vertex->get<picojson::array>();
			        float x = coords[0].get<double>();
			        float y = coords[1].get<double>();
			        _texCoords.push_back(x);
			        _texCoords.push_back(y);
		        }
	        }
        }

        generateVertexNormals(90.f);
    }