summaryrefslogtreecommitdiff
path: root/lib/shape.cc
blob: b9bdd876ee1bfad303ec94cc479ae3374c68669c (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <math.h>
#include <SDL.h>
#include "engine.h"
#include "base.h"
#include "shape.h"
#include "texture.h"
#include "font.h"

#define ENTER bool flag = Enter()
#define LEAVE Leave(flag)

mogltk::fillwalker::fillwalker() {
}

mogltk::fillwalker::~fillwalker() {
}

mogltk::fill::fill() : header(0) {
}

mogltk::fill::~fill() {
    if (header)
	delete header;
}

void mogltk::fill::walk(fillwalker * w) {
    header->walk(w);
}

void mogltk::fill::insert(int x, int y) {
    if (!header)
	new sline(y, this);
    header->insert(x, y);
}

mogltk::fill::sline::sline(int _y, fill * _header) : y(_y), pheader(0), header(_header) {
    if (!header->header) {
	header->header = this;
    } else {
	if (header->header->y > y) {
	    next = header->header;
	    header->header = this;
	} else {
	    sline * p;
	    for (p = header->header; p->next; p = p->next) {
		if (p->next->y > y) {
		    next = p->next;
		    p->next = this;
		    return;
		}
	    }
	    p->next = this;
	    next = 0;
	}
    }
}

mogltk::fill::sline::~sline() {
    if (header)
	delete header;
    if (next)
	delete next;
}

int mogltk::fill::sline::GetY() {
    return y;
}

void mogltk::fill::sline::insert(int ax, int ay) {
    sline * f;
    
    if (ay == y) {
	if (!pheader)
	    new point(ax, this);
	else if (!pheader->look(ax))
	    new point(ax, this);
    } else {
	f = look(y);

	if (!f)
	    f = new sline(y, header);
	
	f->insert(ax, ay);
    }
}

void mogltk::fill::sline::walk(fillwalker * w) {
    w->step(GetX(), GetY());
}

void mogltk::shape::box(int x1, int y1, int x2, int y2, ColorP c) {
    ENTER;
    
    SDL_Rect rect;
    
    rect.x = x1; rect.y = y1; rect.w = x2 - x1 + 1; rect.h = y2 - y1 + 1;
    SDL_FillRect(mogltk::engine::base_o->getsurface(), &rect, c.toSDL());
    
    LEAVE;
}

void mogltk::shape::hline(int x1, int x2, int y, ColorP c) {
    box(x1, y, x2, y, c);
}

void mogltk::shape::hline3d(int x1, int x2, int y, ColorP shade1, ColorP shade2, bool bevel) {
    ENTER;
    
    if (!bevel) {
	hline(x1, x2, y, shade2);
	hline(x1, x2, y + 1, shade1);
    } else {
	hline(x1, x2, y, shade1);
	hline(x1, x2, y + 1, shade2);
    }
    
    LEAVE;
}

void mogltk::shape::vline(int x, int y1, int y2, ColorP c) {
    box(x, y1, x, y2, c);
}

void mogltk::shape::vline3d(int x, int y1, int y2, ColorP shade1, ColorP shade2, bool bevel) {
    ENTER;
    
    if (!bevel) {
	vline(x, y1, y2, shade2);
	vline(x + 1, y1, y2, shade1);
    } else {
	vline(x, y1, y2, shade1);
	vline(x + 1, y1, y2, shade2);
    }
    
    LEAVE;
}

void mogltk::shape::pixel(int x, int y, ColorP c) {
    ENTER;
    
    SDL_Surface * surface = mogltk::engine::base_o->getsurface();
    int bpp = surface->format->BytesPerPixel;
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
    Uint32 pixel = c.toSDL();

    switch(bpp) {
    case 1:
        *p = pixel;
        break;

    case 2:
        *(Uint16 *)p = pixel;
        break;

    case 3:
        if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
            p[0] = (pixel >> 16) & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = pixel & 0xff;
        } else {
            p[0] = pixel & 0xff;
            p[1] = (pixel >> 8) & 0xff;
            p[2] = (pixel >> 16) & 0xff;
        }
        break;

    case 4:
        *(Uint32 *)p = pixel;
        break;
    }

    LEAVE;
}

void mogltk::shape::circle(int x0, int y0, int r, ColorP c) {
    ENTER;
    
    int x = 0;
    int y = r - 1;
    int d = 3 - 2 * r;
    int dI = 10 - 4 * r;
    int rI = 6;
    
    while (x <= y) {
	pixel(x0 + x, y0 + y, c);
	pixel(x0 - x, y0 + y, c);
	pixel(x0 + x, y0 - y, c);
	pixel(x0 - x, y0 - y, c);
	pixel(x0 + y, y0 + x, c);
	pixel(x0 - y, y0 + x, c);
	pixel(x0 + y, y0 - x, c);
	pixel(x0 - y, y0 - x, c);
	if (d >= 0) {
	    d += dI;
	    dI += 8;
	    y -= 1;
	} else {
	    d += rI;
	    dI += 4;
	}
	rI += 4;
	x += 1;
    }

    LEAVE;
}

void mogltk::shape::pcircle(int x0, int y0, int r, ColorP c) {
    ENTER;
    
    int x = 0;
    int y = r - 1;
    int d = 3 - 2 * r;
    int dI = 10 - 4 * r;
    int rI = 6;
    
    while (x <= y) {
	hline(x0 - x, x0 + x, y0 + y, c);
	hline(x0 - x, x0 + x, y0 - y, c);
	hline(x0 - y, x0 + y, y0 + x, c);
	hline(x0 - y, x0 + y, y0 - x, c);
	if (d >= 0) {
	    d += dI;
	    dI += 8;
	    y -= 1;
	} else {
	    d += rI;
	    dI += 4;
	}
	rI += 4;
	x += 1;
    }

    LEAVE;
}

void mogltk::shape::arc(int x0, int y0, int r, double a1, double a2, ColorP c) {
    ENTER;
    
    int x = 0;
    int y = r - 1;
    int d = 3 - 2 * r;
    int dI = 10 - 4 * r;
    int rI = 6;
    
    while (x <= y) {
	pixel(x0 + y, y0 - x, c); // 1
	pixel(x0 + x, y0 - y, c); // 2
	pixel(x0 - x, y0 - y, c); // 3
	pixel(x0 - y, y0 - x, c); // 4
	pixel(x0 - y, y0 + x, c); // 5
	pixel(x0 - x, y0 + y, c); // 6
	pixel(x0 + x, y0 + y, c); // 7
	pixel(x0 + y, y0 + x, c); // 8
    
	if (d >= 0) {
	    d += dI;
	    dI += 8;
	    y -= 1;
	} else {
	    d += rI;
	    dI += 4;
	}
	rI += 4;
	x += 1;
    }

    LEAVE;
}

void mogltk::shape::arc(int x0, int y0, int r, int x1, int y1, int x2, int y2, ColorP c) {
}

void mogltk::shape::obox(int x1, int y1, int x2, int y2, ColorP c) {
    ENTER;

    hline(x1, x2, y1, c);
    hline(x1, x2, y2, c);
    vline(x1, y1, y2, c);
    vline(x2, y1, y2, c);

    LEAVE;
}

void mogltk::shape::box3d(int x1, int y1, int x2, int y2, ColorP face, ColorP shade1, ColorP shade2, int depth, bool bevel) {
    ENTER;

    int i;

    for (i = 0; i < depth; i++) {
	hline(x1 + i, x2 - i, y1 + i, bevel ? shade2 : shade1);
	vline(x1 + i, y1 + i, y2 - i, bevel ? shade2 : shade1);
    }

    for (i = 0; i < depth; i++) {
	hline(x1 + i, x2 - i, y2 - i, bevel ? shade1 : shade2);
	vline(x2 - i, y1 + i, y2 - i, bevel ? shade1 : shade2);
    }

    box(x1 + depth, y1 + depth, x2 - depth, y2 - depth, face);
    
    LEAVE;
}

void mogltk::shape::obox3d(int x1, int y1, int x2, int y2, ColorP shade1, ColorP shade2, bool bevel) {
    ENTER;
    
    if (!bevel) {
	obox(x1 + 1, y1 + 1, x2, y2, shade1);
	obox(x1, y1, x2 - 1, y2 - 1, shade2);
    } else {
	obox(x1, y1, x2 - 1, y2 - 1, shade1);
	obox(x1 + 1, y1 + 1, x2, y2, shade2);
    }
    
    LEAVE;
}

void mogltk::shape::window(int x1, int y1, int x2, int y2, const String & title,
			    ColorP titlecolor, ColorP titlebackcolor,
			    ColorP front, ColorP shade1, ColorP shade2) {
    ENTER;
    
    box3d(x1, y1, x2, y2, front, shade1, shade2);
    hline3d(x1 + 2, x2 - 2, y1 + 19, shade1, shade2, 1);
    box(x1 + 2, y1 + 2, x2 - 2, y1 + 18, titlebackcolor);
    text((x1 + x2) / 2, y1 + 2, title, titlecolor, CENTER);
    
    LEAVE;
}

void mogltk::shape::text(int x, int y, const String & text, ColorP textcolor, align_t align) {
    int tsize = SystemFont->singletextsize(text);
    switch (align) {
    case LEFT:
	SystemFont->putcursor(x, y);
	break;
    case CENTER:
	SystemFont->putcursor(x - (tsize / 2), y);
	break;
    case RIGHT:
	SystemFont->putcursor(x - tsize, y);
	break;
    }
    SystemFont->setcolor(textcolor);
    SystemFont->setshadow(0);
    SystemFont->printf(text);
}

void mogltk::shape::text3d(int x, int y, const String & atext, ColorP textcolor,
			    ColorP shade1, ColorP shade2,
			    align_t align, bool bevel) {
    ENTER;
    
    SystemFont->setwspace(1);
    if (!bevel) {
        text(x - 1, y - 1, atext, shade2, align);
        text(x + 1, y + 1, atext, shade1, align);
    } else {
	text(x - 1, y - 1, atext, shade1, align);
        text(x + 1, y + 1, atext, shade2, align);
    }
    text(x, y, atext, textcolor, align);
    SystemFont->setwspace(0);

    LEAVE;
}

void mogltk::shape::button(int x1, int y1, int x2, int y2,
			    const String & atext, bool bevel,
			    ColorP front, ColorP shade1, ColorP shade2,
			    ColorP round, ColorP textcolor,
			    ColorP tshade1, ColorP tshade2) {
    ENTER;
    
    box3d(x1, y1, x2, y2, front, shade1, shade2, 2, bevel);
    hline(x1, x2, y1 - 1, round);
    hline(x1, x2, y2 + 1, round);
    vline(x1 - 1, y1, y2, round);
    vline(x2 + 1, y1, y2, round);
    pixel(x1, y1, round);
    pixel(x1, y2, round);
    pixel(x2, y1, round);
    pixel(x2, y2, round);
    text3d((x1 + x2) / 2, (y1 + y2) / 2 - 8, atext, textcolor, tshade1, tshade2, CENTER, bevel);
    
    LEAVE;
}

bool mogltk::shape::Enter() {
    if (SDL_MUSTLOCK(mogltk::engine::base_o->getsurface())) {
	SDL_LockSurface(mogltk::engine::base_o->getsurface());
	return true;
    } else {
	return false;
    }
}

void mogltk::shape::Leave(bool locked) {
    if (locked)
	SDL_UnlockSurface(mogltk::engine::base_o->getsurface());
}