D Documentation [ Log in ]
General syntax
All files in D are called "modules".

A module holds one or more declarations for functions, structs, classes, templates, and variables.
You can't write a class that spans over more than one file, nor can you do that with functions or structs.

Here is an example for a D module (actually it's the main module of a tetris clone i wrote), just to get the look and feel of the language:

private import std.loader;

private import derelict.sdl.sdl;
private import derelict.sdl.image;
private import derelict.sdl.mixer;

private import msgstr;

private import surface;
private import videoinfo;
private import gameresources;

private import options;
private import menuscreen;
private import game;

Surface SetVideoMode(uint x, uint y, uint bpp, bool fullscreen)
{

  Uint32 flags = 0; //SDL_DOUBLEBUF;
  if (fullscreen)
    flags |= SDL_FULLSCREEN;

  if (fullscreen)
    msg_str("Setting full screen video mode");
  else
    msg_str("Opening windowed screen");
    
  Surface screen = surface.Surface.SetVideoMode(x, y, bpp, flags);
  msg_ok();

  VideoInfo vi = new VideoInfo();
  vi.PrintProperties();

  SDL_WM_SetCaption("Tetris - written 2004 by MD",null);
  return screen;
}


int main(char[][] args)
{
  std.loader.ExeModule_Init();
  
  msg_str("Loading Derelict SDL");
  try { DerelictSDL_Load(); } catch (Exception e) { msg_end("NO DLL"); return 1; }
  msg_ok();

  msg_str("Loading Derelict SDL_image");
  try { DerelictSDLImage_Load(); } catch (Exception e) { msg_end("NO DLL"); return 1; }
  msg_ok();
  
  msg_str("Loading Derelict SDL_mixer");
  try { DerelictSDLMixer_Load(); } catch (Exception e) { msg_end("NO DLL"); return 1; }
  msg_ok();

  msg_str("Init SDL Audio and Video");
  if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) == 0)
  {
    msg_ok();
    
    Options o = new Options("resource", "options.txt");

    Surface screen = SetVideoMode(o.VideoModeX, o.VideoModeY, o.VideoModeBPP, o.Fullscreen);
    surface.Surface.PrintProperties(screen.sdl_surface);
    GameResources gr = new GameResources(o);
    
    while (1)
    {
      MenuScreen menuscreen = new MenuScreen(screen, gr);
      uint m = menuscreen.Run();
      delete menuscreen;
      
      if (m == 0) break;
      
      switch (m)
      {
        case 1:
          Game game = new Game(screen, gr);
          game.Run(1);
          delete game;
          break;

        case 2:
          Game game = new Game(screen, gr);
          game.Run(2);
          delete game;
          break;
      }
    }
    
    msg_str("Waiting for sound to finish playing");
    while (gr.sound.SoundPlaying)
    {
      // Do nothing, just wait :)
      SDL_Delay(50);
    }
    msg_ok();
    
    delete gr;
    delete screen;

    msg_str("Shutting down SDL");
    SDL_Quit();
    msg_ok();

  } else
    msg_err();
  
  return 0;  
}
PHP docwiki written by Markus Dangl. Best viewed with Mozilla Firefox.
This is the inofficial wiki for documenting the D programming language.
- Profiler -
Time for site generation: 0.0871 s