15
16
* from the command line: size of the board,
16
17
* rotation speed and number of free squares; also
17
18
* added the "sticks" mode.
19
* 2008 Andrew Galante Added -textured option: textures the board with
20
* an image which gets scrambled as the tiles move
21
#define DEF_FLIPFLOP_MODE "tiles" /* Default mode (options: "tiles", "sticks") */
22
#define DEF_BOARD_X_SIZE "9" /* Default width of the board */
23
#define DEF_BOARD_Y_SIZE "9" /* Default length of the board */
24
#define DEF_MODE "tiles" /* Default mode (options: "tiles", "sticks") */
25
#define DEF_SIZEX "9" /* Default width of the board */
26
#define DEF_SIZEY "9" /* Default length of the board */
25
28
#define DEF_BOARD_SIZE "0" /* "0" means "no value selected by user". It is changed */
26
29
#define DEF_NUMSQUARES "0" /* in function init_flipflop() to its correct value (that */
57
62
{"-tiles", ".mode", XrmoptionNoArg, "tiles" },
58
63
{"-mode", ".mode", XrmoptionSepArg, 0 },
59
64
{"-size", ".size", XrmoptionSepArg, 0 },
60
{"-size-x", ".size_x", XrmoptionSepArg, 0 },
61
{"-size-y", ".size_y", XrmoptionSepArg, 0 },
65
{"-size-x", ".sizex", XrmoptionSepArg, 0 },
66
{"-size-y", ".sizey", XrmoptionSepArg, 0 },
62
67
{"-count", ".numsquares", XrmoptionSepArg, 0 },
63
68
{"-free", ".freesquares", XrmoptionSepArg, 0 },
64
69
{"-spin", ".spin", XrmoptionSepArg, 0 },
70
{"-texture", ".textured", XrmoptionNoArg, "True" },
71
{"+texture", ".textured", XrmoptionNoArg, "False" },
67
74
static int wire, clearbits;
70
77
static float half_thick;
72
79
static char* flipflopmode_str="tiles";
74
82
static argtype vars[] = {
75
{ &flipflopmode_str, "mode", "Mode", DEF_FLIPFLOP_MODE, t_String},
83
{ &flipflopmode_str, "mode", "Mode", DEF_MODE, t_String},
76
84
{ &board_avg_size, "size", "Integer", DEF_BOARD_SIZE, t_Int},
77
{ &board_x_size, "size_x", "Integer", DEF_BOARD_X_SIZE, t_Int},
78
{ &board_y_size, "size_y", "Integer", DEF_BOARD_Y_SIZE, t_Int},
85
{ &board_x_size, "sizex", "Integer", DEF_SIZEX, t_Int},
86
{ &board_y_size, "sizey", "Integer", DEF_SIZEY, t_Int},
79
87
{ &numsquares, "numsquares", "Integer", DEF_NUMSQUARES, t_Int},
80
88
{ &freesquares, "freesquares", "Integer", DEF_NUMSQUARES, t_Int},
81
89
{ &spin, "spin", "Float", DEF_SPIN, t_Float},
90
{ &textured, "textured", "Bool", DEF_TEXTURED, t_Bool},
84
93
ENTRYPOINT ModeSpecOpt flipflop_opts = {countof(opts), opts, countof(vars), vars, NULL};
119
134
trackball_state *trackball;
120
135
Bool button_down_p;
124
139
float theta; /* angle of rotation of the board */
125
140
float flipspeed; /* amount of flip; 1 is a entire flip */
126
141
float reldist; /* relative distace of camera from center */
127
142
float energy; /* likelyhood that a square will attempt to move */
144
/* texture rectangle */
150
/* id of texture in use */
131
160
static Flipflopcreen *qs = NULL;
162
#include "grab-ximage.h"
133
164
static void randsheet_create( randsheet *rs );
134
165
static void randsheet_initialize( randsheet *rs );
135
166
static void randsheet_free( randsheet *rs );
136
167
static int randsheet_new_move( randsheet* rs );
137
168
static void randsheet_move( randsheet *rs, float rot );
138
static void randsheet_draw( randsheet *rs );
169
static int randsheet_draw( randsheet *rs );
139
170
static void setup_lights(void);
140
static void drawBoard(Flipflopcreen *);
141
static void display(Flipflopcreen *c);
142
static void draw_sheet(void);
171
static int drawBoard(Flipflopcreen *);
172
static int display(Flipflopcreen *c);
173
static int draw_sheet(float *tex);
145
176
/* configure lighting */
259
296
glMatrixMode(GL_MODELVIEW);
300
image_loaded_cb (const char *filename, XRectangle *geometry,
301
int image_width, int image_height,
302
int texture_width, int texture_height,
305
Flipflopcreen *c = (Flipflopcreen *)closure;
308
randsheet *rs = c->sheet;
310
c->tex_x = (float)geometry->x / (float)texture_width;
311
c->tex_y = (float)geometry->y / (float)texture_height;
312
c->tex_width = (float)geometry->width / (float)texture_width;
313
c->tex_height = (float)geometry->height / (float)texture_height;
315
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
316
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
317
(c->mipmap ? GL_LINEAR_MIPMAP_LINEAR : GL_LINEAR));
319
if(c->anisotropic >= 1)
320
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, c->anisotropic);
322
glEnable(GL_TEXTURE_2D);
324
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
326
for(i = 0; i < board_x_size && index < numsquares; i++)
327
for(j = 0; j < board_y_size && index < numsquares; j++)
329
/* arrange squares to form loaded image */
330
rs->tex[ index*4 + 0 ] = c->tex_x + c->tex_width / board_x_size * (i + 0);
331
rs->tex[ index*4 + 1 ] = c->tex_y + c->tex_height / board_y_size * (j + 1);
332
rs->tex[ index*4 + 2 ] = c->tex_x + c->tex_width / board_x_size * (i + 1);
333
rs->tex[ index*4 + 3 ] = c->tex_y + c->tex_height / board_y_size * (j + 0);
334
rs->color[ index*3 + 0 ] = 1;
335
rs->color[ index*3 + 1 ] = 1;
336
rs->color[ index*3 + 2 ] = 1;
340
c->got_texture = True;
344
get_texture(ModeInfo *modeinfo)
346
Flipflopcreen *c = &qs[MI_SCREEN(modeinfo)];
348
c->got_texture = False;
350
load_texture_async (modeinfo->xgwa.screen, modeinfo->window,
351
*c->glx_context, 0, 0, c->mipmap, c->texid,
263
356
init_flipflop(ModeInfo *mi)
266
359
Flipflopcreen *c;
268
/* Set all constants to their correct values */
269
if (board_avg_size != 0) { /* general size specified by user */
270
board_x_size = board_avg_size;
271
board_y_size = board_avg_size;
273
board_avg_size = (board_x_size + board_y_size) / 2;
275
if ((numsquares == 0) && (freesquares != 0)) {
276
numsquares = board_x_size * board_y_size - freesquares;
278
if (strcmp(flipflopmode_str, "tiles")) {
279
half_thick = 1.0 * DEF_STICK_THICK / 100.0;
280
if (numsquares == 0) { /* No value defined by user */
281
numsquares = board_x_size * board_y_size * DEF_STICK_RATIO / 100;
284
half_thick = 1.0 * DEF_TILE_THICK / 100.0;
285
if (numsquares == 0) { /* No value defined by user */
286
numsquares = board_x_size * board_y_size * DEF_TILE_RATIO/ 100;;
289
if (board_avg_size < 2) {
290
fprintf (stderr,"%s: the board must be at least 2x2.\n", progname);
293
if ((board_x_size < 1) || (board_y_size < 1) || (numsquares < 1)) {
294
fprintf (stderr,"%s: the number of elements ('-count') and the dimensions of the board ('-size-x', '-size-y') must be positive integers.\n", progname);
297
if (board_x_size * board_y_size <= numsquares) {
298
fprintf (stderr,"%s: the number of elements ('-count') that you specified is too big \n for the dimensions of the board ('-size-x', '-size-y'). Nothing will move.\n", progname);
361
if (MI_IS_WIREFRAME(mi)) textured = 0;
363
/* Set all constants to their correct values */
364
if (board_avg_size != 0) { /* general size specified by user */
365
board_x_size = board_avg_size;
366
board_y_size = board_avg_size;
368
board_avg_size = (board_x_size + board_y_size) / 2;
370
if ((numsquares == 0) && (freesquares != 0)) {
371
numsquares = board_x_size * board_y_size - freesquares;
373
if (strcmp(flipflopmode_str, "tiles")) {
374
textured = 0; /* textures look dumb in stick mode */
375
half_thick = 1.0 * DEF_STICK_THICK / 100.0;
376
if (numsquares == 0) { /* No value defined by user */
377
numsquares = board_x_size * board_y_size * DEF_STICK_RATIO / 100;
380
half_thick = 1.0 * DEF_TILE_THICK / 100.0;
381
if (numsquares == 0) { /* No value defined by user */
382
numsquares = board_x_size * board_y_size * DEF_TILE_RATIO/ 100;;
385
if (board_avg_size < 2) {
386
fprintf (stderr,"%s: the board must be at least 2x2.\n", progname);
389
if ((board_x_size < 1) || (board_y_size < 1) || (numsquares < 1)) {
390
fprintf (stderr,"%s: the number of elements ('-count') and the dimensions of the board ('-size-x', '-size-y') must be positive integers.\n", progname);
393
if (board_x_size * board_y_size <= numsquares) {
394
fprintf (stderr,"%s: the number of elements ('-count') that you specified is too big \n for the dimensions of the board ('-size-x', '-size-y'). Nothing will move.\n", progname);
301
397
screen = MI_SCREEN(mi);
302
398
wire = MI_IS_WIREFRAME(mi);
389
498
/*** ADDED RANDSHEET FUNCTIONS ***/
501
draw_sheet(float *tex)
394
504
glBegin( wire ? GL_LINE_LOOP : GL_QUADS );
396
506
glNormal3f( 0, -1, 0 );
507
glTexCoord2f(tex[0], tex[3]);
397
508
glVertex3f( half_thick, -half_thick, half_thick );
509
glTexCoord2f(tex[2], tex[3]);
398
510
glVertex3f( 1-half_thick, -half_thick, half_thick );
511
glTexCoord2f(tex[2], tex[1]);
399
512
glVertex3f( 1-half_thick, -half_thick, 1-half_thick);
513
glTexCoord2f(tex[0], tex[1]);
400
514
glVertex3f( half_thick, -half_thick, 1-half_thick );
402
517
if (wire) { glEnd(); glBegin (GL_LINE_LOOP); }
405
520
glNormal3f( 0, 1, 0 );
521
glTexCoord2f(tex[0], tex[1]);
406
522
glVertex3f( half_thick, half_thick, 1-half_thick );
523
glTexCoord2f(tex[2], tex[1]);
407
524
glVertex3f( 1-half_thick, half_thick, 1-half_thick);
525
glTexCoord2f(tex[2], tex[3]);
408
526
glVertex3f( 1-half_thick, half_thick, half_thick );
527
glTexCoord2f(tex[0], tex[3]);
409
528
glVertex3f( half_thick, half_thick, half_thick );
411
if (wire) { glEnd(); return; }
531
if (wire) { glEnd(); return polys; }
413
533
/* 4 edges!!! weee.... */
414
534
glNormal3f( 0, 0, -1 );
535
glTexCoord2f(tex[0], tex[3]);
415
536
glVertex3f( half_thick, half_thick, half_thick );
537
glTexCoord2f(tex[2], tex[3]);
416
538
glVertex3f( 1-half_thick, half_thick, half_thick );
539
glTexCoord2f(tex[2], tex[3]);
417
540
glVertex3f( 1-half_thick, -half_thick, half_thick );
541
glTexCoord2f(tex[0], tex[3]);
418
542
glVertex3f( half_thick, -half_thick, half_thick );
419
544
glNormal3f( 0, 0, 1 );
545
glTexCoord2f(tex[0], tex[1]);
420
546
glVertex3f( half_thick, half_thick, 1-half_thick );
547
glTexCoord2f(tex[0], tex[1]);
421
548
glVertex3f( half_thick, -half_thick, 1-half_thick );
549
glTexCoord2f(tex[2], tex[1]);
422
550
glVertex3f( 1-half_thick, -half_thick, 1-half_thick );
551
glTexCoord2f(tex[2], tex[1]);
423
552
glVertex3f( 1-half_thick, half_thick, 1-half_thick );
424
554
glNormal3f( 1, 0, 0 );
555
glTexCoord2f(tex[2], tex[1]);
425
556
glVertex3f( 1-half_thick, half_thick, 1-half_thick );
557
glTexCoord2f(tex[2], tex[1]);
426
558
glVertex3f( 1-half_thick, -half_thick, 1-half_thick );
559
glTexCoord2f(tex[2], tex[3]);
427
560
glVertex3f( 1-half_thick, -half_thick, half_thick );
561
glTexCoord2f(tex[2], tex[3]);
428
562
glVertex3f( 1-half_thick, half_thick, half_thick );
429
564
glNormal3f( -1, 0, 0 );
565
glTexCoord2f(tex[0], tex[1]);
430
566
glVertex3f( half_thick, half_thick, 1-half_thick );
567
glTexCoord2f(tex[0], tex[3]);
431
568
glVertex3f( half_thick, half_thick, half_thick );
569
glTexCoord2f(tex[0], tex[3]);
432
570
glVertex3f( half_thick, -half_thick, half_thick );
571
glTexCoord2f(tex[0], tex[1]);
433
572
glVertex3f( half_thick, -half_thick, 1-half_thick );
437
579
/* Reserve memory for the randsheet */