Skip to content

Commit

Permalink
* Pulled font related code out into wwvi_font.[ch]
Browse files Browse the repository at this point in the history
  • Loading branch information
smcameron committed Apr 11, 2009
1 parent d2a8a3f commit fc924fb
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 291 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ rumble.o: rumble.c rumble.h Makefile
gcc ${DEBUG} ${PROFILE_FLAG} ${OPTIMIZE_FLAG} `pkg-config --cflags vorbisfile` \
-pthread -Wall -c rumble.c

wwvi_font.o: wwvi_font.c wwvi_font.h my_point.h Makefile
gcc ${DEBUG} ${PROFILE_FLAG} ${OPTIMIZE_FLAG} -pthread -Wall -c wwvi_font.c

stamp: stamp.c
gcc -o stamp stamp.c

wordwarvi: wordwarvi.c joystick.o rumble.o ${OGGOBJ} wwviaudio.o Makefile version.h stamp levels.h rumble.h
wordwarvi: wordwarvi.c joystick.o rumble.o ${OGGOBJ} wwviaudio.o wwvi_font.o \
Makefile version.h stamp levels.h rumble.h
./stamp > stamp.h
gcc ${DEBUG} ${PROFILE_FLAG} ${OPTIMIZE_FLAG} ${SCREENSAVERFLAG} -pthread -Wall ${DEFINES} \
joystick.o \
rumble.o \
wwvi_font.o \
${OGGOBJ} \
wwviaudio.o \
wordwarvi.c -o wordwarvi -lm ${SNDLIBS} \
Expand Down
295 changes: 5 additions & 290 deletions wordwarvi.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "joystick.h"
#include "rumble.h"
#include "my_point.h"
#include "wwvi_font.h"
#include "version.h"
#include "stamp.h"

Expand Down Expand Up @@ -483,154 +484,6 @@ struct level_parameters_t {
0,
};

/**** LETTERS and stuff *****/

/* special values to do with drawing shapes. */
#define LINE_BREAK (-9999)
#define COLOR_CHANGE (-9998) /* note, color change can ONLY follow LINE_BREAK */

typedef unsigned char stroke_t;

/**************************
Here's how this works, there's a 3x7 grid. on which the
letters are drawn. You list the sequence of strokes.
Use 21 to lift the pen, 99 to mark the end.
Inspired by Hofstadters' Creative Concepts and Creative Analogies.
letter 'A' can be compactly represented by:
0 1 2 * * * { 6, 8, 14, 12, 9, 11, 99 }
3 4 5 * * *
6 7 8 *---*---*
|
9 10 11 *---*---*
| |
12 13 14 *---*---*
15 16 17 * * *
18 19 20 * * *
The grid numbers can be decoded into (x,y) coords like:
x = ((n % 3) * xscale);
y = (x/3) * yscale; truncating division.
(not sure the above actually works)
instead, use decode_glyph[] to get x and y values -- see below
***************************/
stroke_t glyph_Z[] = { 0, 2, 12, 14, 99 };
stroke_t glyph_Y[] = { 0, 7, 2, 21, 7, 13, 99 };
stroke_t glyph_X[] = { 0, 14, 21, 12, 2, 99 };
stroke_t glyph_W[] = { 0, 12, 10, 14, 2, 21, 10, 1, 99 };
stroke_t glyph_V[] = { 0, 13, 2, 99 };
stroke_t glyph_U[] = { 0, 9, 13, 11, 2, 99 };
stroke_t glyph_T[] = { 13, 1, 21, 0, 2, 99 };
stroke_t glyph_S[] = { 9, 13, 11, 3, 1, 5, 99 };
stroke_t glyph_R[] = { 12, 0, 1, 5, 7, 6, 21, 7, 14, 99 };
stroke_t glyph_Q[] = { 13, 9, 3, 1, 5, 11, 13, 21, 10, 14, 99 };
stroke_t glyph_P[] = { 12, 0, 1, 5, 7, 6, 99 };
stroke_t glyph_O[] = { 13, 9, 3, 1, 5, 11, 13, 99 };
stroke_t glyph_N[] = { 12, 0, 14, 2, 99 };
stroke_t glyph_M[] = { 12, 0, 4, 2, 14, 99 };
stroke_t glyph_L[] = { 0, 12, 14, 99};
stroke_t glyph_K[] = { 0, 12, 21, 6, 7, 11, 14, 21, 7, 5, 2, 99};
stroke_t glyph_J[] = { 9, 13, 11, 2, 99};
stroke_t glyph_I[] = { 12, 14, 21, 13, 1, 21, 0, 2, 99 };
stroke_t glyph_H[] = { 0, 12, 21, 2, 14, 21, 6, 8, 99 };
stroke_t glyph_G[] = { 7, 8, 11, 13, 9, 3, 1, 5, 99 };
stroke_t glyph_F[] = { 12, 0, 2, 21, 7, 6, 99 };
stroke_t glyph_E[] = { 14, 12, 0, 2, 21, 6, 7, 99 };
stroke_t glyph_D[] = { 12, 13, 11, 5, 1, 0, 12, 99 };
stroke_t glyph_C[] = { 11, 13, 9, 3, 1, 5, 99 };
stroke_t glyph_B[] = { 0, 12, 13, 11, 5, 1, 0, 21, 6, 8, 99 };
stroke_t glyph_A[] = { 12, 3, 0, 5, 14, 21, 8, 6, 99 };
stroke_t glyph_slash[] = { 12, 2, 99 };
stroke_t glyph_backslash[] = { 0, 14, 99 };
stroke_t glyph_pipe[] = { 1, 13, 99 };
stroke_t glyph_que[] = { 13, 10, 21, 7, 5, 2, 0, 3, 99 };
stroke_t glyph_bang[] = { 10, 13, 21, 1, 7, 99};
stroke_t glyph_colon[] = { 6, 7, 21, 12, 13, 99 };
stroke_t glyph_leftparen[] = { 2, 4, 10, 14,99 };
stroke_t glyph_rightparen[] = { 0, 4, 10, 12,99 };
stroke_t glyph_space[] = { 99 };
stroke_t glyph_plus[] = { 6, 8, 21, 7, 13, 99 };
stroke_t glyph_dash[] = { 6, 8, 99 };
stroke_t glyph_0[] = { 12, 0, 2, 14, 12, 2, 99 };
stroke_t glyph_9[] = { 8, 6, 0, 2, 14, 99 };
stroke_t glyph_8[] = { 0, 2, 14, 12, 0, 21, 6, 8, 99 };
stroke_t glyph_7[] = { 0, 2, 12, 99 };
stroke_t glyph_6[] = { 2, 0, 12, 14, 8, 6, 99 };
stroke_t glyph_5[] = { 2, 0, 6, 8, 14, 12, 99 };
stroke_t glyph_4[] = { 0, 6, 8, 21, 2, 14, 99 };
stroke_t glyph_3[] = { 0, 2, 14, 12, 21, 6, 8, 99 };
stroke_t glyph_2[] = { 0, 2, 5, 9, 12, 14, 99 };
stroke_t glyph_1[] = { 1, 13, 99 };
stroke_t glyph_comma[] = { 13, 16, 99 };
stroke_t glyph_period[] = { 12, 13, 99 };
stroke_t glyph_z[] = { 6, 8, 12, 14, 99 };
stroke_t glyph_y[] = { 6, 12, 14, 21, 8, 17, 19, 18, 99};
stroke_t glyph_x[] = { 12, 8, 21, 6, 14, 99 };
stroke_t glyph_w[] = { 6, 12, 14, 8, 21, 7, 13, 99 };
stroke_t glyph_v[] = { 6, 13, 8, 99 };
stroke_t glyph_u[] = { 6, 12, 14, 8, 99 };
stroke_t glyph_t[] = { 14, 13, 4, 21, 6, 8, 99 };
stroke_t glyph_s[] = { 8, 6, 9, 11, 14,12, 99 };

stroke_t glyph_a[] = { 6, 8, 14, 12, 9, 11, 99 };
stroke_t glyph_b[] = { 0, 12, 14, 8, 6, 99 };
stroke_t glyph_c[] = { 8, 6, 12, 14, 99 };
stroke_t glyph_d[] = { 8, 6, 12, 14, 2, 99 };
stroke_t glyph_e[] = { 9, 11, 8, 6, 12, 14, 99 };
stroke_t glyph_f[] = { 13, 1, 2, 21, 6, 8, 99 };
stroke_t glyph_g[] = { 11, 12, 6, 8, 20, 18, 99 };
stroke_t glyph_h[] = { 0, 12, 21, 6, 8, 14, 99 };
stroke_t glyph_i[] = { 13, 7, 21, 4, 2, 99 };
stroke_t glyph_j[] = { 18, 16, 7, 21, 4, 2, 99 };
stroke_t glyph_k[] = { 0, 12, 21, 6, 7, 11, 14, 21, 7, 5, 99 };
stroke_t glyph_l[] = { 1, 13, 99 };
stroke_t glyph_m[] = { 12, 6, 8, 14, 21, 7, 13, 99 };
stroke_t glyph_n[] = { 12, 6, 7, 13, 99 };
stroke_t glyph_o[] = { 12, 6, 8, 14, 12, 99 };
stroke_t glyph_p[] = { 18, 6, 8, 14, 12, 99 };
stroke_t glyph_q[] = { 14, 12, 6, 8, 20, 99 };
stroke_t glyph_r[] = { 12, 6, 21, 9,7,8,99 };
stroke_t glyph_apostrophe[] = { 7, 5, 99 };
stroke_t glyph_doublequote[] = { 0, 3, 21, 1, 4, 99 };
stroke_t glyph_asterisk[] = { 9, 5, 21, 3, 11, 21, 6, 8, 21, 4, 10, 99 };

/* x and y offsets for decoding stroke_t's, above */
struct my_point_t decode_glyph[] = {
{ 0, -4 },
{ 1, -4 },
{ 2, -4 },
{ 0, -3 },
{ 1, -3 },
{ 2, -3 },
{ 0, -2 },
{ 1, -2 },
{ 2, -2 },
{ 0, -1 },
{ 1, -1 },
{ 2, -1 },
{ 0, -0 },
{ 1, -0 },
{ 2, -0 },
{ 0, 1 },
{ 1, 1 },
{ 2, 1 },
{ 0, 2 },
{ 1, 2 },
{ 2, 2 },
{ 0, 3 },
{ 1, 3 },
{ 2, 3 },
};
/**** end of LETTERS and stuff */

/* vxy_2_dxy is a 2d array which translates a vx and vy velocity vextor
* into an x,y vector in the same direction but a fixed magnitude. The array
* caches precomputed values. This is used for drawing the missiles. If the
Expand Down Expand Up @@ -7508,144 +7361,6 @@ void round_explode(int x, int y, int ivx, int ivy, int v, int nsparks, int time)
}
}

/* This converts a stroke_t, which is a sort of slightly compressed coding
* of how to draw a letter, lifted from Hofstadter's book, and converts it
* into a set of line segments and breaks, like all the other objects in
* the game, while also scaling it by some amount. It is used in making
* a particular font size
*/
struct my_vect_obj *prerender_glyph(stroke_t g[], int xscale, int yscale)
{
int i, x, y;
int npoints = 0;
struct my_point_t scratch[100];
struct my_vect_obj *v;

/* printf("Prerendering glyph..\n"); */

for (i=0;g[i] != 99;i++) {
if (g[i] == 21) {
/* printf("LINE_BREAK\n"); */
x = LINE_BREAK;
y = LINE_BREAK;
} else {
/* x = ((g[i] % 3) * xscale);*/
/* y = ((g[i]/3)-4) * yscale ; // truncating division.*/
x = decode_glyph[g[i]].x * xscale;
y = decode_glyph[g[i]].y * yscale;
/* printf("x=%d, y=%d\n", x,y); */
}
scratch[npoints].x = x;
scratch[npoints].y = y;
npoints++;
}

v = (struct my_vect_obj *) malloc(sizeof(struct my_vect_obj));
v->npoints = npoints;
if (npoints != 0) {
v->p = (struct my_point_t *) malloc(sizeof(struct my_point_t) * npoints);
memcpy(v->p, scratch, sizeof(struct my_point_t) * npoints);
} else
v->p = NULL;
return v;
}

/* This makes a font, by prerendering all the known glyphs into
* prescaled sets of line segments that the drawing routines know
* how to draw.
*/
int make_font(struct my_vect_obj ***font, int xscale, int yscale)
{
struct my_vect_obj **v;

v = malloc(sizeof(**v) * 256);
if (!v) {
if (v) free(v);
return -1;
}
memset(v, 0, sizeof(**v) * 256);
v['A'] = prerender_glyph(glyph_A, xscale, yscale);
v['B'] = prerender_glyph(glyph_B, xscale, yscale);
v['C'] = prerender_glyph(glyph_C, xscale, yscale);
v['D'] = prerender_glyph(glyph_D, xscale, yscale);
v['E'] = prerender_glyph(glyph_E, xscale, yscale);
v['F'] = prerender_glyph(glyph_F, xscale, yscale);
v['G'] = prerender_glyph(glyph_G, xscale, yscale);
v['H'] = prerender_glyph(glyph_H, xscale, yscale);
v['I'] = prerender_glyph(glyph_I, xscale, yscale);
v['J'] = prerender_glyph(glyph_J, xscale, yscale);
v['K'] = prerender_glyph(glyph_K, xscale, yscale);
v['L'] = prerender_glyph(glyph_L, xscale, yscale);
v['M'] = prerender_glyph(glyph_M, xscale, yscale);
v['N'] = prerender_glyph(glyph_N, xscale, yscale);
v['O'] = prerender_glyph(glyph_O, xscale, yscale);
v['P'] = prerender_glyph(glyph_P, xscale, yscale);
v['Q'] = prerender_glyph(glyph_Q, xscale, yscale);
v['R'] = prerender_glyph(glyph_R, xscale, yscale);
v['S'] = prerender_glyph(glyph_S, xscale, yscale);
v['T'] = prerender_glyph(glyph_T, xscale, yscale);
v['U'] = prerender_glyph(glyph_U, xscale, yscale);
v['V'] = prerender_glyph(glyph_V, xscale, yscale);
v['W'] = prerender_glyph(glyph_W, xscale, yscale);
v['X'] = prerender_glyph(glyph_X, xscale, yscale);
v['Y'] = prerender_glyph(glyph_Y, xscale, yscale);
v['Z'] = prerender_glyph(glyph_Z, xscale, yscale);
v['!'] = prerender_glyph(glyph_bang, xscale, yscale);
v['/'] = prerender_glyph(glyph_slash, xscale, yscale);
v['\\'] = prerender_glyph(glyph_backslash, xscale, yscale);
v['|'] = prerender_glyph(glyph_pipe, xscale, yscale);
v['?'] = prerender_glyph(glyph_que, xscale, yscale);
v[':'] = prerender_glyph(glyph_colon, xscale, yscale);
v['('] = prerender_glyph(glyph_leftparen, xscale, yscale);
v[')'] = prerender_glyph(glyph_rightparen, xscale, yscale);
v['a'] = prerender_glyph(glyph_a, xscale, yscale);
v[' '] = prerender_glyph(glyph_space, xscale, yscale);
v['b'] = prerender_glyph(glyph_b, xscale, yscale);
v['c'] = prerender_glyph(glyph_c, xscale, yscale);
v['d'] = prerender_glyph(glyph_d, xscale, yscale);
v['e'] = prerender_glyph(glyph_e, xscale, yscale);
v['f'] = prerender_glyph(glyph_f, xscale, yscale);
v['g'] = prerender_glyph(glyph_g, xscale, yscale);
v['h'] = prerender_glyph(glyph_h, xscale, yscale);
v['i'] = prerender_glyph(glyph_i, xscale, yscale);
v['j'] = prerender_glyph(glyph_j, xscale, yscale);
v['k'] = prerender_glyph(glyph_k, xscale, yscale);
v['l'] = prerender_glyph(glyph_l, xscale, yscale);
v['m'] = prerender_glyph(glyph_m, xscale, yscale);
v['n'] = prerender_glyph(glyph_n, xscale, yscale);
v['o'] = prerender_glyph(glyph_o, xscale, yscale);
v['p'] = prerender_glyph(glyph_p, xscale, yscale);
v['q'] = prerender_glyph(glyph_q, xscale, yscale);
v['r'] = prerender_glyph(glyph_r, xscale, yscale);
v['s'] = prerender_glyph(glyph_s, xscale, yscale);
v['t'] = prerender_glyph(glyph_t, xscale, yscale);
v['u'] = prerender_glyph(glyph_u, xscale, yscale);
v['v'] = prerender_glyph(glyph_v, xscale, yscale);
v['w'] = prerender_glyph(glyph_w, xscale, yscale);
v['x'] = prerender_glyph(glyph_x, xscale, yscale);
v['y'] = prerender_glyph(glyph_y, xscale, yscale);
v['z'] = prerender_glyph(glyph_z, xscale, yscale);
v['0'] = prerender_glyph(glyph_0, xscale, yscale);
v['1'] = prerender_glyph(glyph_1, xscale, yscale);
v['2'] = prerender_glyph(glyph_2, xscale, yscale);
v['3'] = prerender_glyph(glyph_3, xscale, yscale);
v['4'] = prerender_glyph(glyph_4, xscale, yscale);
v['5'] = prerender_glyph(glyph_5, xscale, yscale);
v['6'] = prerender_glyph(glyph_6, xscale, yscale);
v['7'] = prerender_glyph(glyph_7, xscale, yscale);
v['8'] = prerender_glyph(glyph_8, xscale, yscale);
v['9'] = prerender_glyph(glyph_9, xscale, yscale);
v['-'] = prerender_glyph(glyph_dash, xscale, yscale);
v['+'] = prerender_glyph(glyph_plus, xscale, yscale);
v[','] = prerender_glyph(glyph_comma, xscale, yscale);
v['.'] = prerender_glyph(glyph_period, xscale, yscale);
v['\''] = prerender_glyph(glyph_apostrophe, xscale, yscale);
v['\"'] = prerender_glyph(glyph_doublequote, xscale, yscale);
v['*'] = prerender_glyph(glyph_asterisk, xscale, yscale);
*font = v;
return 0;
}

void init_object_numbers()
{
int i;
Expand Down Expand Up @@ -7789,10 +7504,10 @@ void init_vects()
setup_vect(socket_vect, socket_points);

/* Make the line segment lists from the stroke_t structures. */
make_font(&gamefont[BIG_FONT], font_scale[BIG_FONT], font_scale[BIG_FONT]);
make_font(&gamefont[SMALL_FONT], font_scale[SMALL_FONT], font_scale[SMALL_FONT]);
make_font(&gamefont[TINY_FONT], font_scale[TINY_FONT], font_scale[TINY_FONT]);
make_font(&gamefont[NANO_FONT], font_scale[NANO_FONT], font_scale[NANO_FONT]);
wwvi_make_font(&gamefont[BIG_FONT], font_scale[BIG_FONT], font_scale[BIG_FONT]);
wwvi_make_font(&gamefont[SMALL_FONT], font_scale[SMALL_FONT], font_scale[SMALL_FONT]);
wwvi_make_font(&gamefont[TINY_FONT], font_scale[TINY_FONT], font_scale[TINY_FONT]);
wwvi_make_font(&gamefont[NANO_FONT], font_scale[NANO_FONT], font_scale[NANO_FONT]);
set_font(BIG_FONT);
}

Expand Down
Loading

0 comments on commit fc924fb

Please sign in to comment.