Change tinygl_text to have pos

This commit is contained in:
Michael Hayes
2011-09-24 10:42:33 +00:00
parent 691778132b
commit c1fef5ff4c
27 changed files with 238 additions and 238 deletions
+134 -128
View File
@@ -12,29 +12,43 @@
enum {TINYGL_SPEED_DEFAULT = 20};
static uint16_t update_rate;
static uint16_t text_advance_period;
static font_t *font;
static tinygl_text_mode_t text_mode = TINYGL_TEXT_MODE_STEP;
static uint8_t message_index;
static uint8_t scroll_pos = 0;
static char message1[TINYGL_MESSAGE_SIZE] = "";
typedef struct tinygl_state_struct
{
tinygl_text_mode_t mode;
tinygl_text_dir_t dir;
tinygl_point_t pos;
uint16_t speed;
font_t *font;
uint16_t update_rate;
uint8_t message_index;
uint16_t text_advance_period;
uint8_t scroll_pos;
char message[TINYGL_MESSAGE_SIZE];
} tinygl_state_t;
static tinygl_state_t tinygl =
{
.mode = TINYGL_TEXT_MODE_STEP,
.dir = TINYGL_TEXT_DIR_NORMAL,
.speed = TINYGL_SPEED_DEFAULT
};
/** Draw point.
@param point coordinates of point
@param pos coordinates of point
@param pixel_value pixel value for line. */
void tinygl_draw_point (tinygl_point_t point, tinygl_pixel_value_t pixel_value)
void tinygl_draw_point (tinygl_point_t pos, tinygl_pixel_value_t pixel_value)
{
display_pixel_set (point.x, point.y, pixel_value);
display_pixel_set (pos.x, pos.y, pixel_value);
}
/** Draw line.
@param point1 coordinates of start of line
@param point2 coordinates of end of line
@param pos1 coordinates of start of line
@param pos2 coordinates of end of line
@param pixel_value pixel value for line. */
void tinygl_draw_line (tinygl_point_t point1, tinygl_point_t point2,
void tinygl_draw_line (tinygl_point_t pos1, tinygl_point_t pos2,
tinygl_pixel_value_t pixel_value)
{
int dx;
@@ -44,8 +58,8 @@ void tinygl_draw_line (tinygl_point_t point1, tinygl_point_t point2,
/* Draw a line using Bresenham's algorithm. */
dx = point2.x - point1.x;
dy = point2.y - point1.y;
dx = pos2.x - pos1.x;
dy = pos2.y - pos1.y;
x_inc = dx >= 0 ? 1 : -1;
y_inc = dy >= 0 ? 1 : -1;
@@ -60,15 +74,15 @@ void tinygl_draw_line (tinygl_point_t point1, tinygl_point_t point2,
error = dx / 2;
for (; point1.x != point2.x; point1.x += x_inc)
for (; pos1.x != pos2.x; pos1.x += x_inc)
{
tinygl_draw_point (point1, pixel_value);
tinygl_draw_point (pos1, pixel_value);
error -= dy;
if (error < 0)
{
error += dx;
point1.y += y_inc;
pos1.y += y_inc;
}
}
}
@@ -77,19 +91,19 @@ void tinygl_draw_line (tinygl_point_t point1, tinygl_point_t point2,
int error;
error = dy / 2;
for (; point1.y != point2.y; point1.y += y_inc)
for (; pos1.y != pos2.y; pos1.y += y_inc)
{
tinygl_draw_point (point1, pixel_value);
tinygl_draw_point (pos1, pixel_value);
error -= dx;
if (error < 0)
{
error += dy;
point1.x += x_inc;
pos1.x += x_inc;
}
}
}
tinygl_draw_point (point1, pixel_value);
tinygl_draw_point (pos1, pixel_value);
}
@@ -113,8 +127,8 @@ void tinygl_clear (void)
display_clear ();
/* Stop message display. */
message1[0] = 0;
message_index = 0;
tinygl.message[0] = 0;
tinygl.message_index = 0;
}
@@ -125,73 +139,72 @@ void tinygl_clear (void)
@return 1 if pixel on; if pixel out of bounds return 0. */
static bool tinygl_font_pixel_get (char ch, uint8_t col, uint8_t row)
{
return font_pixel_get (font, ch, col, row);
return font_pixel_get (tinygl.font, ch, col, row);
}
/** Draw character using current font.
@param ch character to draw
@param offset coordinates of top left position
@param rotate non-zero to rotate character
@param pos coordinates of top left position
@return position to draw next character. */
tinygl_point_t tinygl_draw_char (char ch, tinygl_point_t offset, bool rotate)
tinygl_point_t tinygl_draw_char (char ch, tinygl_point_t pos)
{
uint8_t x;
uint8_t y;
if (rotate)
if (tinygl.dir == TINYGL_TEXT_DIR_ROTATE)
{
for (x = 0; x < font->height; x++)
for (y = 0; y < tinygl.font->width; y++)
{
for (y = 0; y < font->width; y++)
for (x = 0; x < tinygl.font->height; x++)
{
tinygl_draw_point (tinygl_point (x + offset.x,
TINYGL_HEIGHT - 1 - (y + offset.y)),
tinygl_draw_point (tinygl_point (x + pos.x, pos.y),
tinygl_font_pixel_get (ch, y, x));
}
pos.y--;
}
offset.y += font->width + 1;
/* Draw blank row. */
for (x = 0; x < tinygl.font->height; x++)
tinygl_draw_point (tinygl_point (x + pos.x, pos.y), 0);
pos.y--;
}
else
{
for (x = 0; x < font->width; x++)
for (x = 0; x < tinygl.font->width; x++)
{
for (y = 0; y < font->height; y++)
for (y = 0; y < tinygl.font->height; y++)
{
tinygl_draw_point (tinygl_point (x + offset.x, y + offset.y),
tinygl_draw_point (tinygl_point (pos.x, y + pos.y),
tinygl_font_pixel_get (ch, x, y));
}
pos.x++;
}
offset.x += font->width + 1;
/* Draw blank column. */
for (y = 0; y < tinygl.font->height; y++)
tinygl_draw_point (tinygl_point (pos.x, y + pos.y), 0);
pos.x++;
}
return offset;
return pos;
}
/** Draw string (well, as much as possible) using current font.
@param str string to draw
@param offset coordinates of top left position
@param rotate non-zero to rotate string
@param pos coordinates of top left position
@return number of whole characters drawn. */
uint8_t tinygl_draw_string (const char *str, tinygl_point_t offset, bool rotate)
uint8_t tinygl_draw_string (const char *str, tinygl_point_t pos)
{
uint8_t count = 0;
while (*str)
{
if (rotate)
{
if (offset.y + font->width > TINYGL_HEIGHT)
break;
tinygl_draw_char (*str, offset, 1);
}
else
{
if (offset.x + font->width > TINYGL_WIDTH)
break;
tinygl_draw_char (*str, offset, 0);
}
pos = tinygl_draw_char (*str, pos);
if (pos.x >= TINYGL_WIDTH || pos.y < 0)
break;
count++;
str++;
}
@@ -199,85 +212,70 @@ uint8_t tinygl_draw_string (const char *str, tinygl_point_t offset, bool rotate)
}
/** Display a character.
@param ch character to display
@return 1 if character fully displayed. */
static bool tinygl_display_char (char ch)
{
uint8_t x;
uint8_t y;
if (!font)
return 0;
switch (text_mode)
{
case TINYGL_TEXT_MODE_SCROLL_LEFT:
display_scroll_left ();
for (y = 0; y < font->height; y++)
{
tinygl_draw_point (tinygl_point (TINYGL_WIDTH - 1, y),
tinygl_font_pixel_get (ch, scroll_pos, y));
}
break;
case TINYGL_TEXT_MODE_STEP:
if (scroll_pos != 0)
break;
tinygl_draw_char (ch, tinygl_point (0, 0), 0);
break;
case TINYGL_TEXT_MODE_ROTATE_SCROLL_DOWN:
display_scroll_down ();
for (x = 0; x < font->height; x++)
{
tinygl_draw_point (tinygl_point (x, 0),
tinygl_font_pixel_get (ch, scroll_pos, x));
}
break;
case TINYGL_TEXT_MODE_ROTATE_STEP:
if (scroll_pos != 0)
break;
tinygl_draw_char (ch, tinygl_point (0, 0), 1);
break;
}
scroll_pos++;
if (scroll_pos > font->width)
scroll_pos = 0;
return scroll_pos == 0;
}
/** Advance message. */
static void tinygl_text_advance (void)
{
if (!message1[message_index])
if (!tinygl.message[tinygl.message_index])
{
message_index = 0;
tinygl.message_index = 0;
}
if (message1[message_index])
if (tinygl.message[tinygl.message_index])
{
if (tinygl_display_char (message1[message_index]))
message_index++;
tinygl_point_t pos;
switch (tinygl.mode)
{
case TINYGL_TEXT_MODE_STEP:
if (tinygl.scroll_pos != 0)
break;
/* In step mode, display a single character. */
tinygl_draw_char (tinygl.message[tinygl.message_index], tinygl.pos);
break;
case TINYGL_TEXT_MODE_SCROLL:
pos = tinygl.pos;
if (tinygl.dir == TINYGL_TEXT_DIR_ROTATE)
pos.y += tinygl.scroll_pos;
else
pos.x -= tinygl.scroll_pos;
/* In scroll mode, display as much as the string that will
fit on the display then scroll the reset. */
tinygl_draw_string (tinygl.message + tinygl.message_index, pos);
break;
}
tinygl.scroll_pos++;
if (tinygl.scroll_pos > tinygl.font->width)
{
tinygl.scroll_pos = 0;
tinygl.message_index++;
}
}
}
/** Display a message repeatedly.
@param string null terminated message to display. */
void tinygl_text (const char *string)
@param string null terminated message to display
@param pos position on screen. */
void tinygl_text (const char *string, tinygl_point_t pos)
{
message_index = 0;
scroll_pos = 0;
strncpy (message1, string, sizeof (message1));
tinygl.message_index = 0;
tinygl.scroll_pos = 0;
/* Not much we can do without a font. */
if (!tinygl.font)
return;
tinygl.text_advance_period = tinygl.update_rate /
(tinygl.speed * tinygl.font->width) * 10;
tinygl.pos = pos;
strncpy (tinygl.message, string, sizeof (tinygl.message));
}
@@ -285,7 +283,7 @@ void tinygl_text (const char *string)
@param speed text advance speed (characters per 10 s). */
void tinygl_text_speed_set (uint8_t speed)
{
text_advance_period = update_rate / (speed * TINYGL_WIDTH) * 10;
tinygl.speed = speed;
}
@@ -293,15 +291,23 @@ void tinygl_text_speed_set (uint8_t speed)
@param mode display mode. */
void tinygl_text_mode_set (tinygl_text_mode_t mode)
{
text_mode = mode;
tinygl.mode = mode;
}
/** Set the message display direction.
@param dir display direction. */
void tinygl_text_dir_set (tinygl_text_dir_t dir)
{
tinygl.dir = dir;
}
/** Set the font to use for text.
@param pfont pointer to font description. */
void tinygl_font_set (font_t *pfont)
@param font pointer to font description. */
void tinygl_font_set (font_t *font)
{
font = pfont;
tinygl.font = font;
}
@@ -311,7 +317,7 @@ void tinygl_update (void)
static uint16_t tick = 0;
tick++;
if (tick >= text_advance_period)
if (tick >= tinygl.text_advance_period)
{
tick = 0;
@@ -326,7 +332,7 @@ void tinygl_update (void)
@param rate rate in Hz that tinygl_update called. */
void tinygl_init (uint16_t rate)
{
update_rate = rate;
tinygl.update_rate = rate;
display_init ();
+31 -21
View File
@@ -56,8 +56,8 @@
tinygl_font_set (&font5x7_1);
tinygl_text_speed_set (10);
tinygl_text_mode_set (TINYGL_TEXT_MODE_SCROLL_LEFT);
tinygl_text ("HELLO WORLD ");
tinygl_text_mode_set (TINYGL_TEXT_MODE_SCROLL);
tinygl_text ("HELLO WORLD ", tinygl_point (0, 0));
while (1)
{
@@ -110,14 +110,20 @@ typedef enum
/* Stepping text. */
TINYGL_TEXT_MODE_STEP,
/** Scrolling text. */
TINYGL_TEXT_MODE_SCROLL_LEFT,
/** Rotated scrolling text. */
TINYGL_TEXT_MODE_ROTATE_SCROLL_DOWN,
/* Stepping rotated text. */
TINYGL_TEXT_MODE_ROTATE_STEP
TINYGL_TEXT_MODE_SCROLL,
} tinygl_text_mode_t;
/** Define text display directions. */
typedef enum
{
/* Normal text. */
TINYGL_TEXT_DIR_NORMAL,
/** Rotated text. */
TINYGL_TEXT_DIR_ROTATE,
} tinygl_text_dir_t;
/** Construct a point from a pair of coordinates.
@param x x coordinate
@param y y coordinate
@@ -131,8 +137,9 @@ static inline tinygl_point_t tinygl_point (tinygl_coord_t x, tinygl_coord_t y)
/** Display a message repeatedly.
@param string null terminated message to display. */
void tinygl_text (const char *string);
@param string null terminated message to display
@param pos position on screen. */
void tinygl_text (const char *string, tinygl_point_t pos);
/** Set the message update speed.
@@ -145,6 +152,11 @@ void tinygl_text_speed_set (uint8_t speed);
void tinygl_text_mode_set (tinygl_text_mode_t mode);
/** Set the message display direction.
@param dir display direction. */
void tinygl_text_dir_set (tinygl_text_dir_t dir);
/** Set the font to use for text.
@param pfont pointer to font description. */
void tinygl_font_set (font_t *pfont);
@@ -152,31 +164,29 @@ void tinygl_font_set (font_t *pfont);
/** Draw character using current font.
@param ch character to draw
@param offset coordinates of top left position
@param rotate non-zero to rotate character
@param pos coordinates of top left position
@return position to draw next character. */
tinygl_point_t tinygl_draw_char (char ch, tinygl_point_t offset, bool rotate);
tinygl_point_t tinygl_draw_char (char ch, tinygl_point_t pos);
/** Draw string (well, as much as possible) using current font.
@param str string to draw
@param offset coordinates of top left position
@param rotate non-zero to rotate string
@param pos coordinates of top left position
@return number of whole characters drawn. */
uint8_t tinygl_draw_string (const char *str, tinygl_point_t offset,
bool rotate);
uint8_t tinygl_draw_string (const char *str, tinygl_point_t pos);
/** Draw point.
@param point coordinates of point
@param pos coordinates of point
@param pixel_value pixel value to draw point. */
void tinygl_draw_point (tinygl_point_t point, tinygl_pixel_value_t pixel_value);
void tinygl_draw_point (tinygl_point_t pos, tinygl_pixel_value_t pixel_value);
/** Draw line.
@param point1 coordinates of start of line
@param point2 coordinates of end of line
@param pos1 coordinates of start of line
@param pos2 coordinates of end of line
@param pixel_value pixel value to draw line. */
void tinygl_draw_line (tinygl_point_t point1, tinygl_point_t point2,
void tinygl_draw_line (tinygl_point_t pos1, tinygl_point_t pos2,
tinygl_pixel_value_t pixel_value);