This repository was archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwriter.cpp
232 lines (189 loc) · 5.6 KB
/
writer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <stdio.h>
#include <windows.h>
#include "writer.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#define WRITER_WIDTH 512
#define WRITER_HEIGHT 512
//#define SAVETGA
#ifdef SAVETGA
unsigned char tgahead[]={
0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0xC8, 0x00,
0x20, 0x08
};
#define TGAW(x) (*(unsigned short *)(tgahead+0x0C))=(x)
#define TGAH(x) (*(unsigned short *)(tgahead+0x0E))=(x)
#endif
CWriter::CWriter() {
HDC hdc = GetDC(NULL);
HDC mdc = CreateCompatibleDC(hdc);
HBITMAP bm = CreateCompatibleBitmap(hdc,WRITER_WIDTH,WRITER_HEIGHT);
int size = 32;
int track = 5;
HFONT hf=CreateFont(size, 0, 0, 0, FW_NORMAL,
0 , FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
ANTIALIASED_QUALITY, DEFAULT_PITCH,"Verdana");
unsigned int * buffer = new unsigned int[WRITER_WIDTH*WRITER_HEIGHT];
RECT r={0,0,WRITER_WIDTH,WRITER_HEIGHT};
SelectObject(mdc, bm);
FillRect(mdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
SelectObject(mdc, hf);
SetBkMode(mdc, TRANSPARENT);
SetTextColor(mdc, 0xFFFFFF);
int x=0;
int y=0;
int i;
for (i=0; i<256; i++) {
char s[2]={i,0};
TextOut(mdc, x, y, s, 1);
SIZE sz;
GetTextExtentPoint(mdc,s,1,&sz);
texcoordu1[i]=x/(float)WRITER_WIDTH; x+=sz.cx;
texcoordu2[i]=x/(float)WRITER_WIDTH; x+=track;
texcoordv1[i]= y /(float)WRITER_WIDTH;
texcoordv2[i]=(y+sz.cy)/(float)WRITER_WIDTH;
if (x>450) {
x=0;
y+=size+track;
}
}
BITMAPINFO bmi;
bmi.bmiHeader.biSize=sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth=WRITER_WIDTH;
bmi.bmiHeader.biHeight=WRITER_HEIGHT;
bmi.bmiHeader.biPlanes=1;
bmi.bmiHeader.biBitCount=32;
bmi.bmiHeader.biCompression=BI_RGB;
GetDIBits(mdc,bm,0,WRITER_HEIGHT,buffer,&bmi,DIB_RGB_COLORS);
#ifdef SAVETGA
FILE * f =fopen("dump.tga","wb");
TGAW(WRITER_WIDTH);
TGAH(WRITER_HEIGHT);
fwrite(tgahead,0x12,1,f);
fwrite(buffer,WRITER_WIDTH*WRITER_HEIGHT*4,1,f);
fclose(f);
#endif
for (i=0; i<WRITER_WIDTH*WRITER_HEIGHT; i++) {
buffer[i] = ((buffer[i]&0xFF)<<24)|0x00FFFFFF;
}
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA8, WRITER_WIDTH, WRITER_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
}
void CWriter::Print(float x1,float y1,float x2,float y2,float size,char * s) {
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, tex);
glColor4f(1,1,1,1);
glPushMatrix();
glTranslatef(x1,y1,0);
int ln=0;
int i;
for(i=0; i<(int)strlen(s); i++) if(s[i]=='\n') ln++;
float rw=x2-x1;
float oy=0;
int align=3; // 1 = left, 2 = center, 3 = right
char ss[500],*sc=s;
for(i=0; i<ln; i++) {
char * snn=strchr(sc,'\n');
int l=snn-sc;
memcpy(ss,sc,l);
ss[l]=0;
float tw=0;
for(int j=0; j<(int)strlen(ss); j++) {
unsigned char cc=ss[j];
if(ss[j]>=1 && ss[j]<=3) {
align=ss[j];
}
float lw = texcoordu2[cc]-texcoordu1[cc];
if (cc<32) lw =0;
tw += lw;
}
float ox=0;
if (align==2) ox = (rw/size-tw)/2.0f;
if (align==3) ox = (rw/size-tw);
for (int k=0; k<(int)strlen(ss); k++) {
unsigned char cc=ss[k];
float lw = texcoordu2[cc]-texcoordu1[cc];
float lh = texcoordv2[cc]-texcoordv1[cc];
if (cc<32) lw =0;
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(texcoordu1[cc],1-texcoordv1[cc]); glVertex2f((ox )*size,(oy )*size);
glTexCoord2f(texcoordu1[cc],1-texcoordv2[cc]); glVertex2f((ox )*size,(oy+lh)*size);
glTexCoord2f(texcoordu2[cc],1-texcoordv1[cc]); glVertex2f((ox+lw)*size,(oy )*size);
glTexCoord2f(texcoordu2[cc],1-texcoordv2[cc]); glVertex2f((ox+lw)*size,(oy+lh)*size);
glEnd();
ox+=lw;
}
oy+=texcoordv2['(']-texcoordv1['('];
sc=snn+1;
}
glPopMatrix();
}
/*
#define WIDTH 150
#define TRACK 0.001
void CWriter::PutChar(float x,float y,int sz,char c) {
unsigned char cc=c;
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glBindTexture(GL_TEXTURE_2D, tex);
glColor4f(1,1,1,1);
float w=startcoordz[cc+1]-startcoordz[cc];
w*=WIDTH;
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(startcoordz[cc ],0); glVertex2f(x ,y );
glTexCoord2f(startcoordz[cc ],1); glVertex2f(x ,y-sz);
glTexCoord2f(startcoordz[cc+1]-(4/(float)WRITER_WIDTH),0); glVertex2f(x+sz*w,y );
glTexCoord2f(startcoordz[cc+1]-(4/(float)WRITER_WIDTH),1); glVertex2f(x+sz*w,y-sz);
glEnd();
}
void CWriter::Print(float x,float y,int sz,char * s) {
float cx=x,cy=y;
int ln=0;
for(int i=0; i<(int)strlen(s); i++) if(s[i]=='\n') ln++;
char ss[500],*sc=s;
for(i=0; i<ln; i++) {
char * snn=strchr(sc,'\n');
int l=snn-sc;
memcpy(ss,sc,l);
ss[l]=0;
int sl=strlen(ss);
float wi=0;
for(int j=0; j<sl; j++) {
unsigned char cc=ss[j];
wi+=(float)((startcoordz[cc+1]-startcoordz[cc]-TRACK)*sz*WIDTH);
}
cx=cx-wi/2.0f;
for(j=0; j<sl; j++) {
unsigned char cc=ss[j];
PutChar(cx,cy,sz,cc);
cx+=(float)((startcoordz[cc+1]-startcoordz[cc]-TRACK)*sz*WIDTH);
}
cx=x;
cy+=sz*0.6f;
sc=snn+1;
}
for(int i=0; i<strlen(s); i++) {
switch(s[i]) {
case '\n': {
cx=x;
cy-=sz*0.6;
} break;
default: {
PutChar(cx,cy,sz,s[i]);
cx+=(startcoordz[s[i]+1]-startcoordz[s[i]]-TRACK)*sz*WIDTH;
} break;
}
}
}
*/