Ejemplo n.º 1
0
bool NodeConfigManager::saveUserConfig(UserId userId)
{
   // FIXME: do we need locking for load/save/get/remove?
   bool rval;
   string path;
   rval = getUserConfigPath(this, userId, path);
   if(rval)
   {
      MO_CAT_DEBUG(BM_NODE_CAT,
         "NodeConfigManager: save user config: id=%" PRIu64 ", path=%s",
         userId, path.c_str());
      // FIXME: how to check if config is "empty" of useful data?
      Config c = getUserConfig(userId, true);
      File file(path.c_str());
      rval = file->mkdirs();
      if(rval)
      {
         FileOutputStream fos(file);
         JsonWriter writer;
         writer.setCompact(false);
         rval = writer.write(c, &fos);
         fos.close();
      }
   }
   return true;
}
Ejemplo n.º 2
0
bool Id3v2TagFrameIO::getContractData(
   ByteBuffer* b, uint32_t* uncompressed, uint32_t* compressed)
{
   bool rval;

   // create output stream
   OutputStream* os = (b == NULL) ?
      static_cast<OutputStream*>(new NullOutputStream()) :
      static_cast<OutputStream*>(new ByteArrayOutputStream(b, false));

   // zlib-compress data
   Deflater def;
   def.startDeflating(-1, false);
   MutatorOutputStream mos(os, true, &def, false);

   // produce JSON-formatted contract data
   JsonWriter writer;
   writer.setCompact(true);
   rval = writer.write(mContract, &mos);
   mos.close();

   // store uncompressed bytes
   if(uncompressed != NULL)
   {
      *uncompressed = def.getTotalInputBytes();
   }

   // store compressed bytes
   if(compressed != NULL)
   {
      *compressed = def.getTotalOutputBytes();
   }

   return rval;
}
Ejemplo n.º 3
0
int main(int argc, char* argv[]) {
    System::init();
    
    /*
     * Byte buffer
     *
     */
    ByteBuffer buffer(5);
    char abc[4] = "abc";
    buffer.write(abc,3);
    buffer.reset();
    buffer.resize(3,true);
    printf("%c\n",buffer.readByte());
    printf("%c\n",buffer.readByte());
    printf("%c\n",buffer.readByte());
    buffer.reset();

    /*
     * Canvas
     */
    Canvas canvas(640,480);
    if (!canvas.getSurface()) {
        return -1;
    }

    Thread thread("test thread", &testFunc, (void*)NULL);

    /*
     * Json Reader.
     */
    JsonReader reader;
    reader.read("res/config.json");
    Json::Value value = reader.getRoot();
    std::cout << reader.getRoot().get("verson", "1.2") << std::endl;

    JsonWriter writer;
    writer.write("res/1.json", reader.getRoot(), true);
    writer.write("res/2.json", reader.getRoot(), false);

    Shader vertex("res/shaders/vertex.glsl",Shader::ShaderType::VERTEX_SHADER);
    Shader fragment("res/shaders/fragment.glsl",Shader::ShaderType::FRAGMENT_SHADER);
    ShaderProgram shaderProgram;
    shaderProgram.addShader(&vertex);
    shaderProgram.addShader(&fragment);
    shaderProgram.link();

    Shader vertexRadialBlur("res/shaders/radialblur_vertex.glsl",Shader::ShaderType::VERTEX_SHADER);
    Shader fragmentRadialBlur("res/shaders/radialblur_fragment.glsl",Shader::ShaderType::FRAGMENT_SHADER);
    ShaderProgram radialBlur;
    radialBlur.addShader(&vertexRadialBlur);
    radialBlur.addShader(&fragmentRadialBlur);
    radialBlur.link();

    ResourcesManager resourcesManager;
    resourcesManager.loadTexture("lama1","res/lama1.png");
    resourcesManager.loadTexture("spritesheet","res/ch.png");
    resourcesManager.loadTexture("courrier","res/courrier_0.png");
    
    Sprite sprite2(resourcesManager.getTexture("lama1"));

    Rect<float> texCoords[] = { Rect<float>(32,0,32,32), Rect<float>(0,0,32,32), Rect<float>(32,0,32,32), Rect<float>(64,0,32,32)};
    unsigned int durations[] = { 100, 100, 100, 100 };
    Sprite sprite(resourcesManager.getTexture("spritesheet"));
    sprite.addAnimation("down", 4, durations, texCoords);
    sprite.setSize(128,128);
    sprite.setAnimation("down");

    SimpleScene2D scene2D(640,480);

    shared_ptr<SceneNode> node = shared_ptr<SceneNode>(new SceneNode(&vertex,&fragment));
    scene2D.addNode(node);

    for (int i = 0; i < 1; i++)
    {
        shared_ptr<SpriteActor> s = shared_ptr<SpriteActor>(new SpriteActor(sprite2));
        if (s)
        {
            node->addActor(s);
        }
    }
    node->addActor(shared_ptr<SpriteActor>(new SpriteActor(sprite)));

    BMFontReader fontReader("res/courrier.fnt");
    FontActorCreator fontActorCreator(resourcesManager,fontReader.read());
    shared_ptr<FontActor> fontActor = fontActorCreator.createFontActor("courrier", "This IS A TEXT TEST ! Hehehe", 1, 2, 2.0f, Vector4d<float>(0.2f,0.5f,1.0f,0.2f));
    node->addActor(fontActor);

    /*
    for (int i = 0; i < 5000; i++)
    {
        shared_ptr<SpriteActor> s = shared_ptr<SpriteActor>(new SpriteActor(sprite));
        if (s)
        {
            node->addActor(s);
        }
    }
    */

    InputDevicesManager& idm = canvas.getInputDevicesManager();

    idm.update();
    printf("%i %i\n",idm.getMouseX(),idm.getMouseY());

    if (idm.keyPressed(SDLK_SPACE)) {
        printf("SPACE!\n");
    }

    if (idm.leftButton()) {
        printf("left button\n");
    }
    if (idm.middleButton()) {
        printf("middle button\n");
    }
    if (idm.rightButton()) {
        printf("right button\n");
    }

    long t = System::currentTime()+1000;
    float nbFrame = 0.0f;

    scene2D.update();
    printf("scene 2D updated\n");
    char title[10];

    Sound snd("res/music.ogg");
    Sound snd2("res/boump.ogg");
    AudioSystem system;
    system.play(&snd,true);

    while (1) {
        canvas.clear(0.0f,0.0f,0.0f,1.0f);
        scene2D.update();
        scene2D.render();
        canvas.flip();
        idm.update();
        if (idm.leftButton()) {
            printf("left button\n");
        }
        if (idm.keyPressed(SDLK_ESCAPE)) {
            break;
        } else if (idm.keyPressed(SDLK_LEFT)) {
            sprite.setPosition(sprite.getX()-5.0f, sprite.getY());
        } else if (idm.keyPressed(SDLK_RIGHT)) {
            sprite.setPosition(sprite.getX()+5.0f, sprite.getY());
        } else if (idm.keyPressed(SDLK_DOWN)) {
            sprite.setPosition(sprite.getX(), sprite.getY()-5.0f);
        } else if (idm.keyPressed(SDLK_UP)) {
            sprite.setPosition(sprite.getX(), sprite.getY()+5.0f);
        } else if (idm.keyPressed(SDLK_SPACE)) {
            system.play(&snd2,false);
        } else if (idm.keyPressed(SDLK_o)) {
            sprite.setTint(sprite.getTint().x(), sprite.getTint().y(), sprite.getTint().z(), sprite.getTint().i() <= 0.9f ? sprite.getTint().i()+0.1f : 1.0f);
        } else if (idm.keyPressed(SDLK_l)) {
            sprite.setTint(sprite.getTint().x(), sprite.getTint().y(), sprite.getTint().z(), sprite.getTint().i() > 0.0f ? sprite.getTint().i()-0.1f : 0.0f);
        }
        nbFrame = nbFrame + 1.0f;
        if (t < System::currentTime()) {
            sprintf(title,"%.2f fps",nbFrame);
            canvas.setTitle(title);

            printf("%s\n",gluErrorString(glGetError()));
            nbFrame = 0.0f;
            t = System::currentTime()+1000;
        }
        System::sleep(16);
    }

    System::deinit();
    return 0;
}
Ejemplo n.º 4
0
// FIXME: This code has an issue if the config file has a changed id or
// FIXME: changed contents since it was first loaded. Just assuming this code
// FIXME: is the only code that manages the file for now.
bool NodeConfigManager::saveSystemUserConfig()
{
   bool rval;
   Config cfg = getNodeConfig();
   const char* suPath;
   string path;

   rval = !cfg.isNull();

   if(rval)
   {
      // get system user config and expand any home path
      suPath = cfg["systemUserConfig"]->getString();
      rval = expandBitmunkHomePath(suPath, path);
   }

   if(rval)
   {
      // FIXME: do we need locking for load/save/get/remove?
      MO_CAT_DEBUG(BM_NODE_CAT,
         "NodeConfigManager: save system user config: path=%s",
         path.c_str());
      // read
      File file(path.c_str());
      // new config
      Config c;
      if(file->exists())
      {
         FileInputStream fis(file);
         JsonReader reader;
         reader.start(c);
         rval = reader.read(&fis) && reader.finish();
         fis.close();
      }
      else
      {
         c[ConfigManager::VERSION] = MO_DEFAULT_CONFIG_VERSION;
         c[ConfigManager::ID] = "custom system user";
         c[ConfigManager::GROUP] = "system user";
      }
      // update
      if(rval &&
         getConfigManager()->hasConfig(c[ConfigManager::ID]->getString()))
      {
         // get old raw config
         Config active = getConfigManager()->getConfig(
            c[ConfigManager::ID]->getString(), true);
         c.merge(active, false);
      }
      // backup old file
      if(rval && file->exists())
      {
         string backupPath = path + ".bak";
         File backupFile(backupPath.c_str());
         rval = file->rename(backupFile);
      }
      // make sure dirs exist
      rval = rval && file->mkdirs();
      // write
      if(rval)
      {
         FileOutputStream fos(file);
         JsonWriter writer;
         writer.setCompact(false);
         rval = writer.write(c, &fos);
         fos.close();
      }
   }

   if(!rval)
   {
      ExceptionRef e = new Exception(
         "Could not save system user config.",
         "bitmunk.node.NodeConfigManager.ConfigError");
      e->getDetails()["path"] = path.c_str();
      Exception::push(e);
   }

   return rval;
}