empty_class.cpp

This is an example of a minimal Tk11 application. Unlike empty.cpp, it uses an Application class to implement all callbacks and store all resources. This is the preferred approach, because it avoids global variables.

Note that Boost.Bind is used for creating the callback objects. For more information, see http://www.boost.org/doc/libs/1_42_0/libs/bind/bind.html .

You can use this example as a template for building your own applications.

//          Copyright Florian Winter 2010 - 2010.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#include <tk11/tk11.hpp>

#include <boost/bind.hpp>

using namespace tk11;

// Application class
// Holds all resources owned by the application and makes them accessible by
// callbacks.
class Application {
public:
  // Initialize and run application
  // Creates the framework and the window, then starts the main loop.
  Application()
    : window(framework, get_window_parameters())
  {
    framework.run();
  }

private:
  // The Tk11 framework
  Framework framework;

  // The main rendering window
  Window window;

  // Background color.
  // Updated every frame and used for clearing the back buffer.
  float background_color;

  // Resize callback.
  // Here we update all variables which depend on the size of the target window,
  // such as projection matrices and viewport parameters.
  void resize(unsigned int x_resolution, unsigned int y_resolution,
      bool fullscreen, ID3D11Device_ptr device, IDXGISwapChain_ptr swap_chain,
      ID3D11Texture2D_ptr back_buffer)
  {}

  // Scene initialization callback.
  // Here we create resources, such as textures, buffers and shaders.
  void init(ID3D11Device_ptr device) {}

  // Scene update callback.
  // Here we update all variables that change over time.
  void update(const Duration& elapsed_time) {
    // Set clear color
    background_color =
      fmod(time_div_float<float>(elapsed_time, seconds(1)), 1.0f);
  }

  // Scene rendering callback.
  // Here we render the scene to the main render target (the back buffer)
  void render(ID3D11DeviceContext_ptr context, ID3D11RenderTargetView_ptr render_target) {
    // Clear render target view
    float clear_color_rgba[] = {
      background_color, background_color, background_color, 0
    };
    context->ClearRenderTargetView(render_target.get(), clear_color_rgba);
  }

  // Get window parameters
  // Returns a Window_Parameters structure which defines all parameters for
  // creating the Direct3D window, including callbacks.
  Window_Parameters get_window_parameters() {
    using boost::bind;

    // Framework parameters
    Window_Parameters parameters;

    // Set windowed mode with 800x600 resolution
    parameters.x_resolution = 800;
    parameters.y_resolution = 600;
    parameters.fullscreen = false;

    // Set callbacks
    parameters.resize_callback =
      bind(&Application::resize, this, _1, _2, _3, _4, _5, _6);
    parameters.init_callback = bind(&Application::init, this, _1);
    parameters.update_callback = bind(&Application::update, this, _1);
    parameters.render_callback = bind(&Application::render, this, _1, _2);
    parameters.fullscreen = false;

    return parameters;
  }
};


int main() {
  try {
    // Crate and run application
    Application app;

    return 0;
  }
  catch(const boost::exception& e) {
    // Show an error dialog with diagnostic information about the exception
    // which occurred.
    // (Only for debugging. In a real application, you may want to translate
    // exceptions into more readable error messages).
    show_error_dialog(diagnostic_information(e));
    return 1;
  }
  catch(const std::exception& e) {
    // Show an error dialog with the name of the exception.
    // (Only for debugging. In a real application, you may want to translate
    // exceptions into more readable error messages).
    show_error_dialog(e.what());
    return 1;
  }
  catch(...) {
    // Show an error dialog.
    // (Only for debugging. In a real application, you may want to translate
    // exceptions into more readable error messages).
    show_error_dialog("Unknown exception");
    return 1;
  }
}
Tk11 Direct3D 11 Toolkit version 0.2 (SourceForge)
Copyright Florian Winter 2010 - 2010. Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Generated on Sun Apr 11 20:22:57 2010 for Tk11 by  doxygen 1.6.3