// Font.cpp : Font class // // 17.11.1998 / DH - Genesis // 30.11.1998 / DH - Fading removed // // Copyright (c) 1998 Daniel Hartmeier. All rights reserved. #include "Font.hpp" #include "Sound.hpp" #include "Pad.hpp" #include extern "C++" Sound *sound; inline int strlen(const unsigned char *s) { int l = 0; while (*s) { s++; l++; } return l; } // ------------------------------------------------------------------- Font::Font(Image *image, int width, int height, const unsigned char *map) : image(image), width_(width), height_(height) //, intensity(0), delta(0), steps(0) { map_ = new unsigned char[strlen(map)+1]; strcpy((char*)map_, (char*)map); } Font::~Font() { delete[] map_; } Sprite *Font::createChar(unsigned char c) { Sprite *sprite = 0; int i = 0; while (map_[i] && (map_[i] != c)) i++; if (map_[i]) { int x = i * width_; int u = (x % (image->pw()*4)); int v = (x / (image->pw()*4)) * height_; sprite = new Sprite(image, width_, height_, u, v); sprite->setAttribute((1 << 30) | (/*1*/0 << 28)); // 1 evtl. 2 } return sprite; } // ------------------------------------------------------------------- Rectangle::Rectangle(int x, int y, int w, int h, int r, int g, int b) { gsboxf.attribute = (1 << 28) | (1 << 30); gsboxf.x = x; gsboxf.y = y; gsboxf.w = w; gsboxf.h = h; gsboxf.r = r; gsboxf.g = g; gsboxf.b = b; } Rectangle::~Rectangle() { } void Rectangle::move(int x, int y, int w, int h) { gsboxf.x = x; gsboxf.y = y; gsboxf.w = w; gsboxf.h = h; return; } void Rectangle::setColor(int r, int g, int b) { gsboxf.r = r; gsboxf.g = g; gsboxf.b = b; } void Rectangle::setAttributes(unsigned long a) { gsboxf.attribute = a; } void Rectangle::draw(GsOT *ot) { GsSortBoxFill(&gsboxf, ot, 0); return; } // ------------------------------------------------------------------- TextWindow::TextWindow() : font(0), x_(0), y_(0), w_(0), h_(0), show_(false) { rectangle = new Rectangle(); rectangle->setAttributes((0 << 28) | (1 << 30)); } TextWindow::~TextWindow() { clear(); delete rectangle; } void TextWindow::clear() { for (Sprite *sprite = sprites.First(); sprite; sprite = sprites.Remove()) delete sprite; return; } void TextWindow::text(const unsigned char *t) { clear(); int x = x_, y = y_; while (*t) { if (*t == ' ' ) x += font->width(); else if (*t == '\n') x = x_ + w_ + 1; else { Sprite *sprite = font->createChar(*t); if (sprite) { sprite->move(x+font->width()/2, y+font->height()/2); sprites.Append(sprite); } x += font->width(); } if (x > x_+w_/*-font->width()*/) { x = x_; y += font->height(); } t++; } return; } void TextWindow::move(int x, int y, int w, int h) { x_ = x; y_ = y; w_ = w; h_ = h; rectangle->move(x_, y_, w_, h_); return; } void TextWindow::draw(GsOT *ot) { if (show_) { for (Sprite *sprite = sprites.First(); sprite; sprite = sprites.Next()) sprite->draw(ot); rectangle->draw(ot); } return; } // ------------------------------------------------------------------- MenuWindow::MenuWindow() : TextWindow(), selection_(1), lines(1), released(true) { highlight = new Rectangle(); highlight->setAttributes((1 << 28) | (1 << 30)); } MenuWindow::~MenuWindow() { delete highlight; } void MenuWindow::text(const unsigned char *t) { lines = 1; for (const unsigned char *u = t; *u; ++u) if (*u == '\n') lines++; TextWindow::text(t); return; } int MenuWindow::act(unsigned long pad) { int result = 0; if (show_) { if (pad & padDown) { if (released) { released = false; selection_++; if (selection_ > lines) selection_ = 1; sound->play(SOUND_CLICK); } } else if (pad & padUp) { if (released) { released = false; selection_--; if (selection_ < 1) selection_ = lines; sound->play(SOUND_CLICK); } } else released = true; if (pad & padX) { result = selection_; sound->play(SOUND_CLICK2); } highlight->move(x_, y_+(selection_-1)*font->height(), w_, font->height()); } return result; } void MenuWindow::draw(GsOT *ot) { if (show_) { TextWindow::draw(ot); highlight->draw(ot); } return; } void MenuWindow::setColor(int r, int g, int b) { TextWindow::setColor(r, g, b); highlight->setColor(r/2, g/2, b/2); return; } void MenuWindow::move(int x, int y, int w, int h) { highlight->move(x_, y_+(selection_-1)*font->height(), w_, font->height()); TextWindow::move(x, y, w, h); } // -------------------------------------------------------------------