Basic standard graphics

Adam D. Ruppe destructionator at gmail.com
Fri Nov 12 13:57:34 PST 2010


A direct, line by line port of the C implementation works in D:

====
import core.stdc.stdlib;
import core.stdc.stdio;
import core.stdc.time;
import sdl.SDL;

uint frames = 0;
uint t_acc = 0;

void print_fps ()
{
  static Uint32 last_t = 0;
  Uint32 t = SDL_GetTicks();
  Uint32 dt = t - last_t;
  t_acc += dt;
  if (t_acc > 1000)
  {
    uint el_time = t_acc / 1000;
    printf("- fps: %g\n",
            cast(float) frames / cast(float) el_time);
    t_acc = 0;
    frames = 0;
  }
  last_t = t;
}

void blit_noise(SDL_Surface *surf)
{
  uint i;
  long dim = surf.w * surf.h;
  while (1)
  {
    SDL_LockSurface(surf);
    for (i=0; i < dim; ++i) {
      (cast(ubyte *)surf.pixels)[i] = ((rand() & 1) ? 255 : 0);
    }
    SDL_UnlockSurface(surf);
    SDL_Flip(surf);
    ++frames;
    print_fps();
  }
}

void main()
{
  SDL_Surface *surf = null;
  srand(time(null));
  SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
  surf = SDL_SetVideoMode(320, 240, 8, SDL_DOUBLEBUF | SDL_HWSURFACE);
  blit_noise(surf);
}
====

That's 50 lines.


This is my big "meh" about Rosettacode - virtually everything C can do, D can do
in an almost identical fashion, using the same libraries and everything.

I made only a small handful of edits there. Change include to import, add the
keyword cast to the casts, and change -> to . Done.


More information about the Digitalmars-d mailing list