File: eng_main.c

package info (click to toggle)
gtkatlantic 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,144 kB
  • ctags: 863
  • sloc: sh: 10,807; ansic: 7,915; xml: 188; makefile: 94
file content (546 lines) | stat: -rw-r--r-- 10,443 bytes parent folder | download
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/*
 *     gtkatlantic - the gtk+ monopd client, enjoy network monopoly games
 *
 *
 *  Copyright © 2002-2015 Sylvain Rochet
 *
 *  gtkatlantic is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING. If not, see
 *  <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <glib.h>
#include <string.h>

#include "eng_main.h"


/* ---- initialise engine */
void eng_init()  {
	guint32 alpha, bright;

#if ENG_DEBUG
	fprintf(stdout, "Initializing engine...\n");
#endif

	eng = g_malloc0( sizeof(_eng) );
	eng->obj = eng_list_new();
	eng->frame = eng_list_new();

	/* init alpha tables */
	eng->alpha = g_malloc( sizeof(eng_alpha) );
	for(alpha = 0 ; alpha < 256 ; alpha++) {
		for(bright = 0 ; bright < 256 ; bright++)  {
			guint8 v = (bright * (alpha+1)) >> 8;  /* alpha+1: 255*256 >> 8 = 255 */
			eng->alpha->fg[alpha][bright] = v;
			eng->alpha->bg[alpha][bright] = bright - v;
		}
	}
}

/* ---- close engine */
void eng_close()  {

	if(!eng) return;

	eng_frame_destroy_all();
	eng_list_destroy(eng->obj);
	eng_list_destroy(eng->frame);
	if(eng->alpha) g_free(eng->alpha);
	g_free(eng);

#if ENG_DEBUG
	fprintf(stdout, "Closing engine...\n");
#endif
}

/* ---- create a new frame */
eng_frame* eng_frame_create()  {

	eng_frame *f;

	f = g_malloc0( sizeof(eng_frame) );
	eng_frame_reset(f);
	f->compute = 1;
	f->obj = eng_list_new();
	f->zone_upd = eng_list_new();
	f->entry = eng_list_append(eng->frame, f);

#if ENG_DEBUG
	fprintf(stdout, "New Frame\n");
#endif

	return(f);
}


/* ---- destroy a frame
 *
 *    close also all pics
 */
void eng_frame_destroy(eng_frame *f)  {

	eng_list_e *lst;

	if(!f)  return;
	eng_list_remove_fast(f->entry);
	while( (lst = eng_list_first(f->obj)))
		eng_pic_free(lst->data);
	eng_frame_reset(f);
	eng_list_destroy(f->obj);
	eng_list_destroy(f->zone_upd);
	g_free(f);

#if ENG_DEBUG
	fprintf(stdout, "Destroy frame\n");
#endif
}


/* ---- destroy all frames */
void eng_frame_destroy_all()  {

	eng_list_e *lst;

	while( (lst = eng_list_first(eng->frame)))
		eng_frame_destroy(lst->data);
}


/* ---- set to 0 all elements in struct */
void eng_frame_reset(eng_frame *f)  {

	if(!f)  return;

	f->compute = 0;
	f->width = 0;
	f->height = 0;
	if(f->memalloc > 0)
		g_free(f->bufout);

	f->memalloc = 0;

	eng_frame_free_zoneupd(f);

	f->num_zone = 0;
	f->x_min = 0;
	f->y_min = 0;
	f->x_max = 0;
	f->y_max = 0;
}


/* ---- free all zone update */
void eng_frame_free_zoneupd(eng_frame *f)  {

	eng_list_e *lst;

	if(!f)  return;
	if(!f->zone_upd)  return;

	for(lst = eng_list_first(f->zone_upd) ; lst ; lst=lst->next)
		g_free(lst->data);
	eng_list_clean(f->zone_upd);
}


/* ---- compute yes this frame (for eng_create_frame_all() ) */
void eng_frame_set_compute(eng_frame *f) {

	if(!f)  return;

	f->compute = 1;
}


/* ---- compute no this frame (for eng_create_frame_all() ) */
void eng_frame_unset_compute(eng_frame *f)  {

	if(!f)  return;

	f->compute = 0;
}


/* ---- clean this frame                              *
 *                                                    *
 *  In the same concept of minimizing memory usage,   *
 *  with this function you can clean all memory       *
 *  used for out buffer.                              *
 *                                                    *
 *  Don't use between frame often drawed,             *
 *  because engine do remake all frame, and not       *
 *  just update this, finally engine can become       *
 *  extremely slow.                                   *
 *                                                    *
 *  This is not dependant with minimizing memory mode *
 *                                                    */
void eng_frame_clean(eng_frame *f)  {

	if(!f)  return;

	if(f->memalloc > 0)
		g_free(f->bufout);

	f->memalloc = 0;

	eng_frame_free_zoneupd(f);
/*
	for(i = 1 ; i <= frame[frame_id]->lastpic ; i++ )  {

		if(engine->active_frame[i])
			frame[frame_id]->obj[i]->change = 1;
	}
*/
}


/* ---- set HEIGHT of a frame */
void eng_frame_set_height(eng_frame *f, guint16 height)  {

	if(!f)  return;

	f->height = height;
}


/* ---- set WIDTH of a frame */
void eng_frame_set_width(eng_frame *f, guint16 width)  {

	if(!f)  return;

	f->width = width;
}

/* ---- put in terminal all arguments of a frame */
void eng_frame_showarg(eng_frame *f)  {

	if(!f)  return;

	fprintf(stdout, "====== FRAME ======\n");
	fprintf(stdout, "  compute      -> %d\n", f->compute);
	fprintf(stdout, "  width        -> %d\n", f->width);
	fprintf(stdout, "  height       -> %d\n", f->height);
	fprintf(stdout, "  ------\n");
	fprintf(stdout, "  memalloc     -> %d bytes\n", f->memalloc);
	fprintf(stdout, "  num_zone     -> %d\n", f->num_zone);
	fprintf(stdout, "====================\n");
}


/* ---- create a new pic */
eng_obj* eng_pic_create(eng_frame *f)  {

	eng_obj *o;

	o = g_malloc0( sizeof(eng_obj) );
	eng_pic_reset(o);
	o->show = 1;
	o->frame = f;

	o->entry_main = eng_list_append(eng->obj, o);
	o->entry = eng_list_append(f->obj, o);

#if ENG_DEBUG
	fprintf(stdout, "New Pic\n");
#endif

	return(o);
}


/* ---- sent a call to destroy pic */
void eng_pic_destroy(eng_obj *o)  {

	if(!o)  return;

	o->destroy = 1;
	o->change = 1;
}


/* ---- destroy pic */
void eng_pic_free(eng_obj *o)  {

	if(!o)  return;

	eng_list_remove_fast(o->entry);
	eng_list_remove_fast(o->entry_main);

	eng_pic_reset(o);
	g_free(o);
	o = NULL;

#if ENG_DEBUG
	fprintf(stdout, "Destroy pic\n");
#endif
}


/* ---- set to 0 all elements in struct */
void eng_pic_reset(eng_obj *o)  {

	if(!o)  return;

	o->show = 0;
	o->destroy = 0;
	o->x = 0;
	o->y = 0;
	o->z = 0;
	o->width = 0;
	o->height = 0;
	o->have_alpha = 0;
	o->have_bgcolor = 0;
	o->alpha = 0;
	o->bgcolor[0] = 0;
	o->bgcolor[1] = 0;
	o->bgcolor[2] = 0;
	o->change = 0;
	o->x_old = 0;
	o->y_old = 0;
	o->height_old = 0;
	o->width_old = 0;
}


/* ---- show this pic */
void eng_pic_show(eng_obj *o) {

	if(!o)  return;

	if(!o->show)  {

		o->show = 1;
		o->change = 1;
	}
}


/* ---- unshow this pic */
void eng_pic_unshow(eng_obj *o) {

	if(!o)  return;

	if(o->show)  {

		o->show = 0;
		o->change = 1;
	}
}


/* ---- redraw this pic */
void eng_pic_redraw(eng_obj *o) {

	if(!o)  return;

	o->change = 1;
}


/* ---- set X pos of pic */
void eng_pic_set_x(eng_obj *o, guint16 x)  {

	if(!o)  return;

	if(x != o->x) {

		o->x = x;
		o->change = 1;
	}
}


/* ---- set Y pos of pic */
void eng_pic_set_y(eng_obj *o, guint16 y)  {

	if(!o)  return;

	if(y != o->y) {

		o->y = y;
		o->change = 1;
	}
}


/* ---- set Z pos of pic */
void eng_pic_set_z(eng_obj *o, guint16 z)  {

	if(!o)  return;

	if(z != o->z) {

		o->z = z;
		o->change = 1;
	}
}


/* ---- set HEIGHT of pic */
void eng_pic_set_height(eng_obj *o, guint16 height)  {

	if(!o)  return;

	if(height != o->height) {

		o->height = height;
		o->change = 1;
	 }
}


/* ---- set WIDTH of pic */
void eng_pic_set_width(eng_obj *o, guint16 width)  {

	if(!o)  return;

	if(width != o->width) {

		o->width = width;
		o->change = 1;
	}
}


/* ---- set ALPHA value */
void eng_pic_set_alpha(eng_obj *o, guint8 alpha)  {

	if(!o)  return;

	if( !o->have_alpha  ||  alpha !=  o->alpha)  {

		if(alpha == 0xff)  {  /* 100% opacity */

			eng_pic_unset_alpha(o);
			return;
		}
		o->alpha = alpha;
		o->have_alpha = 1;
		o->change = 1;
	}
}


/* ---- UNset ALPHA value */
void eng_pic_unset_alpha(eng_obj *o)  {

	if(!o)  return;

	if(o->have_alpha)  {

		o->alpha = 0;
		o->have_alpha = 0;
		o->change = 1;
	}
}


/* ---- set BGCOLOR value */
void eng_pic_set_bgcolor(eng_obj *o, guint32 bgcolor)  {

	guint8 rgb[3];

	if(!o)  return;

	rgb[0] = (bgcolor >> 16) & 0xff; // red
	rgb[1] = (bgcolor >> 8) & 0xff;  // green
	rgb[2] = bgcolor & 0xff;         // blue

	if( !o->have_bgcolor  ||  memcmp(&rgb, o->bgcolor, 3) )  {

		memcpy(o->bgcolor, &rgb, 3);
		o->have_bgcolor = 1;
		o->change = 1;
	}
}


/* ---- UNset BGCOLOR value */
void eng_pic_unset_bgcolor(eng_obj *o)  {

	if(!o)  return;

	if(o->have_bgcolor)  {

		o->bgcolor[0] = 0;
		o->bgcolor[1] = 0;
		o->bgcolor[2] = 0;
		o->have_bgcolor = 0;
		o->change = 1;
	}
}


/* ---- set pic buffer */
void eng_pic_set_buf(eng_obj *o, guchar  * buff)  {

	if(!o)  return;
	if(!buff)  return;

	o->buf = buff;
	o->change = 1;
}


/* ---- put in terminal all arguments of a pic */
void eng_pic_showarg(eng_obj *o)  {

	if(!o)  return;

	fprintf(stdout, "== PIC ==\n");
	fprintf(stdout, "  show          -> %d\n", o->show);
	fprintf(stdout, "  x             -> %d\n", o->x);
	fprintf(stdout, "  y             -> %d\n", o->y);
	fprintf(stdout, "  z             -> %d\n", o->z);
	fprintf(stdout, "  width         -> %d\n", o->width);
	fprintf(stdout, "  height        -> %d\n", o->height);
	fprintf(stdout, "  have_alpha    -> %d\n", o->have_alpha);
	fprintf(stdout, "  have_bgcolor  -> %d\n", o->have_bgcolor);
	fprintf(stdout, "  alpha         -> %d\n", o->alpha);
	fprintf(stdout, "  bgcolor[R]    -> 0x%.2X\n", o->bgcolor[0]);
	fprintf(stdout, "  bgcolor[G]    -> 0x%.2X\n", o->bgcolor[1]);
	fprintf(stdout, "  bgcolor[B]    -> 0x%.2X\n", o->bgcolor[2]);
	fprintf(stdout, "  ------\n");
	fprintf(stdout, "  change        -> %d\n", o->change);
	fprintf(stdout, "  x old         -> %d\n", o->x_old);
	fprintf(stdout, "  y old         -> %d\n", o->y_old);
	fprintf(stdout, "  width old     -> %d\n", o->width_old);
	fprintf(stdout, "  height old    -> %d\n", o->height_old);
	fprintf(stdout, "  ------\n");
	if(eng_pic_test(o) )
	fprintf(stdout, "  test      -> SUCCESS\n");
	else
	fprintf(stdout, "  test      -> ERROR\n");
	fprintf(stdout, "====================\n");
}


/* ---- test if pic is good */
gboolean eng_pic_test(eng_obj *o)  {

	if(!o)  return(FALSE);

	/* test x & width */
	if(o->x > o->frame->width - o->width) return(FALSE);

	/* test y & height */
	if(o->y > o->frame->height - o->height) return(FALSE);

	return(TRUE);
}