Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

166 rindas
3.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #ifdef WIN32
  14. # define WIN32_LEAN_AND_MEAN
  15. # include <windows.h>
  16. #endif
  17. #if defined __APPLE__ && defined __MACH__
  18. # include <OpenGL/gl.h>
  19. #else
  20. # define GL_GLEXT_PROTOTYPES
  21. # include <GL/gl.h>
  22. #endif
  23. #include <SDL.h>
  24. #include <SDL_image.h>
  25. #include "core.h"
  26. /*
  27. * Font implementation class
  28. */
  29. class FontData
  30. {
  31. friend class Font;
  32. private:
  33. char *name;
  34. SDL_Surface *img;
  35. int width, height;
  36. GLuint texture;
  37. };
  38. /*
  39. * Public Font class
  40. */
  41. Font::Font(char const *path)
  42. : data(new FontData())
  43. {
  44. data->name = strdup(path);
  45. data->img = NULL;
  46. for (char const *name = path; *name; name++)
  47. if ((data->img = IMG_Load(name)))
  48. break;
  49. if (!data->img)
  50. {
  51. SDL_Quit();
  52. exit(1);
  53. }
  54. drawgroup = DRAWGROUP_BEFORE;
  55. }
  56. Font::~Font()
  57. {
  58. delete data;
  59. }
  60. void Font::TickDraw(float deltams)
  61. {
  62. Entity::TickDraw(deltams);
  63. if (destroy)
  64. {
  65. if (data->img)
  66. SDL_FreeSurface(data->img);
  67. else
  68. glDeleteTextures(1, &data->texture);
  69. }
  70. else if (data->img)
  71. {
  72. data->width = data->img->w / 16;
  73. data->height = data->img->h / 16;
  74. glGenTextures(1, &data->texture);
  75. glBindTexture(GL_TEXTURE_2D, data->texture);
  76. glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
  77. GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
  78. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  79. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  80. SDL_FreeSurface(data->img);
  81. data->img = NULL;
  82. }
  83. }
  84. char const *Font::GetName()
  85. {
  86. return data->name;
  87. }
  88. void Font::Print(int x, int y, char const *str)
  89. {
  90. if (data->img)
  91. return;
  92. glBindTexture(GL_TEXTURE_2D, data->texture);
  93. glBegin(GL_QUADS);
  94. while (*str)
  95. {
  96. uint32_t ch = (uint8_t)*str++;
  97. float tx = .0625f * (ch & 0xf);
  98. float ty = .0625f * ((ch >> 4) & 0xf);
  99. if (ch != ' ')
  100. {
  101. glTexCoord2f(tx, ty + .0625f);
  102. glVertex2f(x, y);
  103. glTexCoord2f(tx + .0625f, ty + .0625f);
  104. glVertex2f(x + data->width, y);
  105. glTexCoord2f(tx + .0625f, ty);
  106. glVertex2f(x + data->width, y + data->height);
  107. glTexCoord2f(tx, ty);
  108. glVertex2f(x, y + data->height);
  109. }
  110. x += data->width;
  111. }
  112. glEnd();
  113. }
  114. void Font::PrintBold(int x, int y, char const *str)
  115. {
  116. static struct { int dx, dy; float r, g, b; } tab[] =
  117. {
  118. { -1, 0, 0.0, 0.0, 0.0 },
  119. { 0, -1, 0.0, 0.0, 0.0 },
  120. { 0, 1, 0.0, 0.0, 0.0 },
  121. { 1, -1, 0.0, 0.0, 0.0 },
  122. { 1, 1, 0.0, 0.0, 0.0 },
  123. { 2, 0, 0.0, 0.0, 0.0 },
  124. { 1, 0, 0.5, 0.5, 0.5 },
  125. { 0, 0, 1.0, 1.0, 1.0 },
  126. };
  127. if (data->img)
  128. return;
  129. glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
  130. for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
  131. {
  132. glColor3f(tab[i].r, tab[i].g, tab[i].b);
  133. Print(x + tab[i].dx, y + tab[i].dy, str);
  134. }
  135. glPopAttrib();
  136. }