Exemple #1
0
void G2DTestSystemDriver::WriteCentered (int mode, int dy, int fg, int bg,
  const char *format, ...)
{
  if (!font) return;

  csString text;
  va_list arg;

  va_start (arg, format);
  text.FormatV (format, arg);
  va_end (arg);

  int fw, fh;
  font->GetDimensions (text, fw, fh);

  int x = (myG2D->GetWidth () - fw) / 2;
  int y = 0;

  switch (mode)
  {
    case 0: // centered by Y
      y = dy + myG2D->GetHeight () / 2;
      break;
    case 1: // from top
      y = dy;
      break;
    case 2: // from bottom
      y = dy + (myG2D->GetHeight () - 1 - fh);
      break;
  }

  myG2D->Write (font, x, y + fh - font->GetDescent(), fg, bg, text, 
    CS_WRITE_BASELINE);
}
Exemple #2
0
void G2DTestSystemDriver::DrawFreetypeTest ()
{
  const char* fontFaces[] = {"DejaVuSans", "DejaVuSansBoldOblique", 
    "DejaVuSansMono", "DejaVuSerif", 0};
  const int fontSizes[] = {4, 8, 12, 24, 0};

  int w = myG2D->GetWidth ();
  int h = myG2D->GetHeight ();
  myG2D->SetClipRect(0,0,w,h);
  myG2D->DrawBox(0,0,w,h, dsteel);

  SetFont (fontItalic);
  int tpos = -h / 2;
  WriteCentered (0, tpos, white, -1, "FREETYPE2 PLUGIN TEST");

  SetFont (fontLarge);
  WriteCentered (0, tpos + 16*2, black,  -1, 
    "If the FreeType2 plugin was built and activated in the");
  WriteCentered (0, tpos + 16*3, black,  -1, 
    "g2dtest.cfg file (it is by default), you should see text");
  WriteCentered (0, tpos + 16*4, black,  -1, 
    "in various faces and sizes below.");

  csRefArray<iFont> fonts;
  // The used fonts are all kept until the end of this function, to provide
  // some more "stress" on the font cache.

  int y = tpos + 16*7;
  int i = 0;
  while (fontFaces[i] != 0)
  {
    csString str;
    int j = 0;
    while (fontSizes[j] != 0)
    {
      int fW, fH;
      csRef<iFont> font = GetFont (fontFaces[i], fontSizes[j]);
      if (font)
      {
	fonts.Push (font);
	SetFont (font);
	str.Clear ();
        str << fontFaces[i] << ", Size " << fontSizes[j];
        WriteCentered (0, y, yellow, -1, str.GetData ());
        font->GetDimensions (str.GetData (), fW, fH);
        y += fH + 4;
      }
      j++;
    }
    i++;
  }

  SetFont (fontCourier);
  WriteCentered (2, 0, green, -1, "press any key to continue");
}
Exemple #3
0
void G2DTestSystemDriver::DrawTextTest ()
{
  // Draw a grid of lines so that transparent text background will be visible
  int w = myG2D->GetWidth ();
  int h = myG2D->GetHeight ();
  int i;
  for (i = 0; i < w; i += 4)
  {
    myG2D->DrawLine (float(i), 0.0f, float(i) + 50.0f, float(h), dsteel);
    myG2D->DrawLine (float(w - i), 0.0f, float(w - i) - 50.0f, float(h), dsteel);
  }

  SetFont (fontItalic);
  WriteCentered (0,-16*7, white, -1, "TEXT DRAWING TEST");

  SetFont (fontLarge);
  WriteCentered (0,-16*5,   blue,    -1, "This is blue text with transparent background");
  WriteCentered (0,-16*4,  green,  blue, "This is green text on blue background");
  WriteCentered (0,-16*3, yellow,  gray, "Yellow text on gray background");
  WriteCentered (0,-16*2,    red, black, "Red text on black background");
  WriteCentered (0,-16*1,  black, white, "Black text on white background");

  SetFont (fontCourier);
  int sx = 0, sy = h / 2 + 48, sw = w, sh = h / 2 - 48;
  myG2D->DrawBox (sx, sy, sw, sh, dsteel);
  const char *text = "Crystal Space rulez";
  int tw, th;
  font->GetDimensions (text, tw, th);
  size_t cc = strlen (text);

  // Test text drawing performance for 1/4 seconds
  int colors [4] = { red, green, blue, yellow };
  sx += 20; sw -= 40 + tw;
  sy += 10; sh -= 20 + th;
  csRandomGen rng (csGetTicks ());
  csTicks start_time = csGetTicks (), delta_time;
  size_t char_count = 0;
  do
  {
    for (i = 0; i < 2000; i++)
    {
      float x = sx + rng.Get () * sw;
      float y = sy + rng.Get () * sh;
      myG2D->Write (font, int(x), int(y), colors [rng.Get (4)], black, text);
      char_count += cc;
    }
    myG2D->PerformExtension ("flush");
    delta_time = csGetTicks () - start_time;
  } while (delta_time < 500);
  float perf = char_count * (1000.0f / delta_time);
  SetFont (fontLarge);
  WriteCentered (0, 16*1, green, black, " Performance: %20.1f characters/second ", perf);
}
Exemple #4
0
void G2DTestSystemDriver::ResizeContext ()
{
  if (!myG2D->BeginDraw ())
    return;

  myG2D->Clear (black);
  DrawWindowResizeScreen ();

  csString text;
  text.Format ("Canvas [%d x %d]", myG2D->GetWidth (), myG2D->GetHeight ());
  SetFont (fontLarge);
  int fw, fh;
  font->GetDimensions (text, fw, fh);
  int x = myG2D->GetWidth () - fw;
  myG2D->Write (font, x, 0, red, -1, text);

  myG2D->FinishDraw ();
  myG2D->Print (0);
}
Exemple #5
0
void G2DTestSystemDriver::DrawUnicodeTest1 ()
{
  int w = myG2D->GetWidth ();
  int h = myG2D->GetHeight ();
  myG2D->SetClipRect(0,0,w,h);
  myG2D->DrawBox(0,0,w,h, dsteel);

  SetFont (fontItalic);
  int tpos = -h / 2;
  WriteCentered (0, tpos, white, -1, "UNICODE TEST 1");

  SetFont (fontLarge);
  WriteCentered (0, tpos + 16*2, black,  -1, "Below you see the equivalent of %s",
		 CS::Quote::Double ("Quick brown fox"));
  WriteCentered (0, tpos + 16*3, black,  -1, "in several languages.");
  WriteCentered (0, tpos + 16*4, black,  -1, "In the ideal case, all characters should be displayed.");
  WriteCentered (0, tpos + 16*5, black,  -1, "If you see a box in some places, a particular");
  WriteCentered (0, tpos + 16*6, black,  -1, "character is not available in the font.");

  int y = tpos + 16*8;
  int i = 0;
  while (quickBrownFox[i] != 0)
  {
    int fW, fH;
    SetFont (fontCourier);
    WriteCentered (0, y, yellow, -1, quickBrownFox[i + 1]);
    font->GetDimensions (quickBrownFox[i + 1], fW, fH);
    y += fH;

    SetFont (fontLarge);
    font->GetMaxSize (fW, fH);
    int h;
    WriteCenteredWrapped (0, y, h, white, -1, quickBrownFox[i]);
    y += h + fH;
    i += 2;
  }

  SetFont (fontCourier);
  WriteCentered (2, 0, green, -1, "press any key to continue");
}
Exemple #6
0
void G2DTestSystemDriver::DrawUnicodeTest2 ()
{
  int w = myG2D->GetWidth ();
  int h = myG2D->GetHeight ();
  myG2D->SetClipRect(0,0,w,h);
  myG2D->DrawBox(0,0,w,h, dsteel);

  SetFont (fontItalic);
  int tpos = -h / 2;
  WriteCentered (0, tpos, white, -1, "UNICODE TEST 2");

  SetFont (fontLarge);
  WriteCentered (0, tpos + 16*2, black,  -1, "Below you see some translations for %s.",
		 CS::Quote::Double ("I can eat glass"));
  WriteCentered (0, tpos + 16*3, black,  -1, "In the ideal case, all characters should be displayed.");
  WriteCentered (0, tpos + 16*4, black,  -1, "If you see a box in some places, a particular");
  WriteCentered (0, tpos + 16*5, black,  -1, "character is not available in the font.");

  int y = tpos + 16*7;
  int i = 0;
  while (iCanEatGlass[i] != 0)
  {
    int fW, fH;
    SetFont (fontCourier);
    WriteCentered (0, y, yellow, -1, iCanEatGlass[i + 1]);
    font->GetDimensions (iCanEatGlass[i + 1], fW, fH);
    y += fH;

    SetFont (fontLarge);
    font->GetMaxSize (fW, fH);
    int h;
    WriteCenteredWrapped (0, y, h, white, -1, iCanEatGlass[i]);
    y += h + fH;
    i += 2;
  }

  SetFont (fontCourier);
  WriteCentered (2, 0, green, -1, "press any key to continue");
}
Exemple #7
0
void G2DTestSystemDriver::WriteCenteredWrapped (int mode, int dy, int &h, 
						int fg, int bg, 
						const char *format, ...)
{
  if (!font) return;

  csString text;
  va_list arg;

  va_start (arg, format);
  text.FormatV (format, arg);
  va_end (arg);

  int y = 0, w = myG2D->GetWidth ();
  int fW, fH;
  font->GetMaxSize (fW, fH);

  switch (mode)
  {
    case 0: // centered by Y
      y = dy + myG2D->GetHeight () / 2;
      break;
    case 1: // from top
      y = dy;
      break;
    case 2: // from bottom
      y = dy + (myG2D->GetHeight () - 1 - fH);
      break;
  }

  h = 0;

  int sW, sH;
  font->GetDimensions (" ", sW, sH);

  // break text so that it completely fits onto the screen.
  int lw = -sW;
  int maxLH = fH;
  char* line = csStrNew (text);
  char* p = line;
  csString drawLine;

  while (p && *p)
  {
    char* space = strchr (p, ' ');
    if (space != 0)
      *space = 0;
    int tW, tH;
    font->GetDimensions (p, tW, tH);
    if (lw + tW + sW >= w)
    {
      WriteCentered (1, y + h, fg, bg, (drawLine.GetData ()) + 1);
      drawLine.Clear ();
      drawLine << ' ' << p;
      //p = space + 1;
      lw = 0;
      h += maxLH;
      maxLH = MAX(fH, tH);
    }
    else
    {
      lw += tW + sW;
      drawLine << ' ' << p;
      maxLH = MAX(maxLH, tH);
    }
    if (space != 0) p = space + 1; else p = 0;
  }
  WriteCentered (1, y + h, fg, bg, (drawLine.GetData ()) + 1);
  h += maxLH;
  delete[] line;
}
Exemple #8
0
void G2DTestSystemDriver::FontClipTest()
{
  int w = myG2D->GetWidth ();
  int h = myG2D->GetHeight ();
  int sx = w/10, sy = h / 2 + 60, sw = sx * 2, sh = h / 6 - 60;
  int sx1 = sx * 1, sx2 = sx * 4, sx3 = sx * 7;
  myG2D->SetClipRect(0,0,w,h);
  myG2D->DrawBox(0,0,w,h, dsteel);
  
  SetFont (fontItalic);
  WriteCentered (0,-16*8, white, -1, "FONT CLIP TEST");

  SetFont (fontLarge);
  WriteCentered (0,-16*7, black, dsteel, "This will test if font clipping is being done properly");
  WriteCentered (0,-16*6, black, dsteel, "You should see three thin green rectangles below");

  WriteCentered (0,-16*4, black, dsteel, "Again all the black should be contained inside the first");
  WriteCentered (0,-16*3, black, dsteel, "green rectangle. The red rectangle should not be visible.");
  
  WriteCentered (0,-16*1, black, dsteel, "The second and third green rectangles shouldn't be crossed as well,");
  WriteCentered (0, 16*0, black, dsteel, "the text should only overdraw the red rectangle. Additionally,");
  WriteCentered (0, 16*1, black, dsteel, "all the text should look the same (well, except for the parts cut off.)");

  SetFont (fontCourier);

  const char* testText = "CrystalSpace";
  int fW, fH;

  font->GetDimensions (testText, fW, fH);

  int fX = -fW /2, fY = -fH / 2;

  DrawClipRect(sx2, sy, sw, sh);
  myG2D->SetClipRect(sx2 + 1, sy + 1, sx2 + sw, sy + sh);

  myG2D->Write (font, sx2 + fX,          sy + fY,          black, -1, testText);
  myG2D->Write (font, sx2 + sw / 2 + fX, sy + fY,          black, -1, testText);
  myG2D->Write (font, sx2 + sw + fX,     sy + fY,          black, -1, testText);

  myG2D->Write (font, sx2 + fX,          sy + sh / 2 + fY, black, -1, testText);
  myG2D->Write (font, sx2 + sw / 2 + fX, sy + sh / 2 + fY, black, -1, testText);
  myG2D->Write (font, sx2 + sw + fX,     sy + sh / 2 + fY, black, -1, testText);

  myG2D->Write (font, sx2 + fX,          sy + sh + fY,     black, -1, testText);
  myG2D->Write (font, sx2 + sw / 2 + fX, sy + sh + fY,     black, -1, testText);
  myG2D->Write (font, sx2 + sw + fX,     sy + sh + fY,     black, -1, testText);

  myG2D->SetClipRect(0,0,w,h);
  DrawClipRect(sx3, sy, sw, sh);
  myG2D->SetClipRect(sx3 + 1, sy + 1, sx3 + sw, sy + sh);

  myG2D->Write (font, sx3 + fX,          sy + fY,          black, blue, testText);
  myG2D->Write (font, sx3 + sw / 2 + fX, sy + fY,          black, blue, testText);
  myG2D->Write (font, sx3 + sw + fX,     sy + fY,          black, blue, testText);

  myG2D->Write (font, sx3 + fX,          sy + sh / 2 + fY, black, blue, testText);
  myG2D->Write (font, sx3 + sw / 2 + fX, sy + sh / 2 + fY, black, blue, testText);
  myG2D->Write (font, sx3 + sw + fX,     sy + sh / 2 + fY, black, blue, testText);

  myG2D->Write (font, sx3 + fX,          sy + sh + fY,     black, blue, testText);
  myG2D->Write (font, sx3 + sw / 2 + fX, sy + sh + fY,     black, blue, testText);
  myG2D->Write (font, sx3 + sw + fX,     sy + sh + fY,     black, blue, testText);

  myG2D->SetClipRect(0,0,w,h);
  DrawClipRect(sx1, sy, sw, sh);
  myG2D->SetClipRect(sx1 + 1, sy + 1, sx1 + sw, sy + sh);

  // Test random text drawing
  csRandomGen rng (csGetTicks ());
  csTicks start_time = csGetTicks (), delta_time;

  // widen the range where we try to draw
  sx -= fW;
  sy -= fH;
  sw += fW * 2;
  sh += fH * 2;

  do
  {
    int i;
    for (i = 0; i < 1000; i++)
    {
      int x = int(sx + rng.Get () * sw);
      int y = int(sy + rng.Get () * sh);
      myG2D->Write (font, x, y, black, blue, testText);
    }
    delta_time = csGetTicks () - start_time;
  } while (delta_time < 100);
}