summaryrefslogtreecommitdiffstats
path: root/minui/graphics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'minui/graphics.cpp')
-rw-r--r--minui/graphics.cpp75
1 files changed, 72 insertions, 3 deletions
diff --git a/minui/graphics.cpp b/minui/graphics.cpp
index c0eea9e..1f25531 100644
--- a/minui/graphics.cpp
+++ b/minui/graphics.cpp
@@ -41,6 +41,20 @@ struct GRFont {
int cheight;
};
+#ifndef ARRAY_SIZE
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
+#endif
+
+typedef struct {
+ char name[80];
+ GRFont* font;
+} font_item;
+
+static font_item gr_fonts[] = {
+ { "menu", NULL },
+ { "log", NULL },
+};
+
static GRFont* gr_font = NULL;
static minui_backend* gr_backend = NULL;
@@ -103,6 +117,36 @@ static void text_blend(unsigned char* src_p, int src_row_bytes,
}
}
+static int rainbow_index = 0;
+static int rainbow_enabled = 0;
+static int rainbow_colors[] = { 255, 0, 0, // red
+ 255, 127, 0, // orange
+ 255, 255, 0, // yellow
+ 0, 255, 0, // green
+ 60, 80, 255, // blue
+ 143, 0, 255 }; // violet
+static int num_rb_colors =
+ (sizeof(rainbow_colors)/sizeof(rainbow_colors[0])) / 3;
+
+static void rainbow(int col) {
+ int rainbow_color = ((rainbow_index + col) % num_rb_colors) * 3;
+ gr_color(rainbow_colors[rainbow_color], rainbow_colors[rainbow_color+1],
+ rainbow_colors[rainbow_color+2], 255);
+}
+
+void set_rainbow_mode(int enabled) {
+ rainbow_enabled = enabled;
+}
+
+void move_rainbow(int x) {
+ rainbow_index += x;
+ if (rainbow_index < 0) {
+ rainbow_index = num_rb_colors - 1;
+ } else if (rainbow_index >= num_rb_colors) {
+ rainbow_index = 0;
+ }
+}
+
void gr_text(int x, int y, const char *s, bool bold)
{
GRFont* font = gr_font;
@@ -116,6 +160,8 @@ void gr_text(int x, int y, const char *s, bool bold)
unsigned char ch;
while ((ch = *s++)) {
+ if (rainbow_enabled) rainbow(x / font->cwidth);
+
if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break;
if (ch < ' ' || ch > '~') {
@@ -229,6 +275,17 @@ void gr_fill(int x1, int y1, int x2, int y2)
}
}
+void gr_set_font(const char* name)
+{
+ unsigned int idx;
+ for (idx = 0; idx < ARRAY_SIZE(gr_fonts); ++idx) {
+ if (strcmp(name, gr_fonts[idx].name) == 0) {
+ gr_font = gr_fonts[idx].font;
+ break;
+ }
+ }
+}
+
void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
if (source == NULL) return;
@@ -267,11 +324,14 @@ unsigned int gr_get_height(GRSurface* surface) {
return surface->height;
}
-static void gr_init_font(void)
+static void gr_init_one_font(int idx)
{
- gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
+ char name[80];
+ GRFont* gr_font = reinterpret_cast<GRFont*>(calloc(sizeof(*gr_font), 1));
+ snprintf(name, sizeof(name), "font_%s", gr_fonts[idx].name);
+ gr_fonts[idx].font = gr_font;
- int res = res_create_alpha_surface("font", &(gr_font->texture));
+ int res = res_create_alpha_surface(name, &(gr_font->texture));
if (res == 0) {
// The font image should be a 96x2 array of character images. The
// columns are the printable ASCII characters 0x20 - 0x7f. The
@@ -303,6 +363,15 @@ static void gr_init_font(void)
}
}
+static void gr_init_font()
+{
+ unsigned int idx;
+ for (idx = 0; idx < ARRAY_SIZE(gr_fonts); ++idx) {
+ gr_init_one_font(idx);
+ }
+ gr_font = gr_fonts[0].font;
+}
+
#if 0
// Exercises many of the gr_*() functions; useful for testing.
static void gr_test() {