00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00046 #include "omicron/internal.h"
00047 #include "omicron/render.h"
00048 #include "omicron/image.h"
00049 #include "omicron/texture.h"
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 font_t *renderer_c::font_createfromimage_fixed(image_c *img, uchar width)
00064 {
00065 AssertThisV;
00066 AssertReturnValue2(img, width, NULL);
00067
00068 font_t *fnt = new font_t;
00069
00070 ushort w, h;
00071 ushort cw, ch;
00072
00073 w = img->get_width();
00074 h = img->get_height();
00075
00076 fnt->width = cw = (sshort)(w / 16);
00077 fnt->height = ch = (sshort)(h / 16);
00078
00079 fnt->texture = gv.texman->create(w, h, "*font");
00080 fnt->shader = shader_create_simple(BT_BLEND, fnt->texture);
00081 fnt->shader->passes[0].magfilter =
00082 fnt->shader->passes[0].minfilter = TF_LINEAR;
00083 gv.texman->set_image(fnt->texture, img);
00084
00085 for (ulong i=0; i<256; i++)
00086 {
00087 fnt->images[i] = new image_c;
00088 fnt->images[i]->copy(img);
00089 fnt->images[i]->crop((ushort)((i%16)*cw), (ushort)(i/16)*ch, (ushort)cw, (ushort)ch);
00090 fnt->widths[i] = width;
00091 }
00092
00093 return fnt;
00094 }
00095
00096 void renderer_c::font_free(font_t *fnt)
00097 {
00098 AssertThis;
00099
00100 AssertReturn1(fnt);
00101
00102 for (ulong i=0; i<256; i++)
00103 SafeDelete(fnt->images[i]);
00104
00105 SafeDelete(fnt);
00106 }
00107
00108
00109
00110
00111 void renderer_c::reset_charmode()
00112 {
00113 AssertThis;
00114
00115 state.font_bold = 0;
00116 state.font_italic = 0;
00117 state.font_color = get_color();
00118 }
00119
00120
00121
00122
00123
00124
00125 float renderer_c::draw_char
00126 (
00127 float x,
00128 float y,
00129 float size,
00130 char c,
00131 const font_t *fnt
00132 )
00133 {
00134 AssertThisV;
00135
00136 if (c==32)
00137 return size;
00138
00139 if (c<32)
00140 {
00141 if (c<16)
00142 {
00143 switch(c)
00144 {
00145 case 1: state.font_bold = !state.font_bold; break;
00146 case 2: state.font_italic = !state.font_italic; break;
00147 }
00148 }
00149 else
00150 {
00151 ulong intensity = c&7;
00152 char col = intensity?0xFF:0xC0;
00153
00154 state.font_color = get_color()&0xFF000000;
00155
00156 for (ulong i=0; i<3; i++)
00157 state.font_color |= ((c&(1<<i))?col:0)<<(8*i);
00158 }
00159
00160 return 0;
00161 }
00162
00163 float w = state.font_bold ? 3.0f:0.0f;
00164
00165
00166 set_color(state.font_color);
00167 draw_pic_shader(x, y, x+w+size, y+size, (c%16)/16.0f, (c/16)/16.0f, (c%16+1)/16.0f, (c/16+1)/16.0f, fnt->shader);
00168
00169 return size;
00170 }
00171
00172
00173 float renderer_c::get_stringwidth(const char *c, const font_t *fnt)
00174 {
00175 AssertThisV;
00176
00177 AssertReturnValue2(c,fnt,0);
00178
00179 ushort ww = 0;
00180
00181 for (ulong i=0; i<strlen(c); i++)
00182 ww += fnt->widths[c[i]];
00183
00184 return (float)ww/(float)fnt->width;;
00185 }
00186
00187 ushort renderer_c::get_string_pixelwidth(const char *c, const font_t *fnt)
00188 {
00189 AssertThisV;
00190
00191 AssertReturnValue2(c,fnt,0);
00192
00193 ushort ww = 0;
00194
00195 for (ulong i=0; i<strlen(c); i++)
00196 ww += fnt->widths[c[i]];
00197
00198 return ww;
00199 }
00200
00201
00202
00203
00204 void renderer_c::draw_string
00205 (
00206 float x,
00207 float y,
00208 float size,
00209 const char *c,
00210 const font_t *fnt,
00211 ulong flags
00212 )
00213 {
00214 AssertThis;
00215
00216 AssertReturn2(c,fnt);
00217
00218 float cx = x;
00219
00220 if (flags & SF_CENTERED)
00221 cx = x-get_stringwidth(c,fnt)*0.5f*size;
00222 else if (flags & SF_RIGHT)
00223 cx = x-get_stringwidth(c,fnt)*size;
00224
00225 reset_charmode();
00226
00227
00228 color_t col = get_color();
00229
00230
00231
00232 while (*c)
00233 {
00234 draw_char(cx, y, size, *c, fnt);
00235 cx += (float)fnt->widths[*c]/(float)fnt->width*size;
00236 c++;
00237 }
00238
00239
00240 set_color(col);
00241 }
00242
00243
00244