Example #1
0
// All threads start execution here.
int main()
{
    void *frameBuffer;
    if (get_current_thread_id() != 0)
        worker_thread();

    // Set up render context
    frameBuffer = init_vga(VGA_MODE_640x480);
    RenderContext *context = new RenderContext(0x1000000);
    RenderTarget *renderTarget = new RenderTarget();
    Surface *colorBuffer = new Surface(FB_WIDTH, FB_HEIGHT, Surface::RGBA8888,
        (void*) frameBuffer);
    Surface *zBuffer = new Surface(FB_WIDTH, FB_HEIGHT, Surface::FLOAT);
    renderTarget->setColorBuffer(colorBuffer);
    renderTarget->setDepthBuffer(zBuffer);
    context->bindTarget(renderTarget);
    context->enableDepthBuffer(true);
    context->bindShader(new TextureShader());

    // Read resources
    PakFile pak;
    pak.open("pak0.pak");
    pak.readBspFile("maps/e1m1.bsp");
    Texture *atlasTexture = pak.getTextureAtlasTexture();

    LevelRenderer renderer;
    renderer.setBspData(pak.getBspTree(), pak.getPvsList(), pak.getBspTree()
                        + pak.getNumInteriorNodes(), pak.getNumLeaves(),
                        atlasTexture, pak.getLightmapAtlasTexture());
    Entity *ent = pak.findEntityByClassName("info_player_start");
    if (!ent)
    {
        printf("Error, couldn't find start position\n");
        return 1;
    }

    float facingAngle = float(atoi(ent->getAttribute("angle"))) / 360.0 * M_PI * 2;
    gCameraOrientationMatrix = Matrix::lookAt(Vec3(0, 0, 0), Vec3(cos(facingAngle),
                               sin(facingAngle), 0), kUpVector);

    float coords[3];
    parseCoordinateString(ent->getAttribute("origin"), coords);
    for (int i = 0; i < 3; i++)
        gCameraPos[i] = coords[i];

    printf("position %g %g %g angle %g\n", coords[0], coords[1], coords[2], facingAngle);

    // Start worker threads
    start_all_threads();

    TextureUniforms uniforms;
    Matrix projectionMatrix = Matrix::getProjectionMatrix(FB_WIDTH, FB_HEIGHT);

    for (int frame = 0; ; frame++)
    {
        processKeyboardEvents();

        context->enableWireframeMode(gWireframeRendering);
        atlasTexture->enableBilinearFiltering(gBilinearFiltering);

        // Set up uniforms
        Matrix viewMatrix = gCameraOrientationMatrix * Matrix::getTranslationMatrix(-gCameraPos);
        uniforms.fMVPMatrix = projectionMatrix * viewMatrix;
        uniforms.enableLightmap = gEnableLightmap;
        uniforms.enableTexture = gEnableTexture;

        context->bindUniforms(&uniforms, sizeof(uniforms));

        renderer.render(context, gCameraPos);

        clock_t startTime = clock();
        context->finish();
        printf("rendered frame in %d uS\n", clock() - startTime);
    }
}