Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Scroll a text with bitmap font  (Read 1543 times)

0 Members and 1 Guest are viewing this topic.

tom75000

  • Newbie
  • *
  • Posts: 2
    • View Profile
Scroll a text with bitmap font
« on: February 05, 2023, 10:31:50 pm »
Hi,
It is possible to scroll a text with bitmap font like in the demoscene?

There is a collection and exemple of bitmap font:
https://github.com/ianhan/BitmapFonts


Thx a lot for your help!

tom75000

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Scroll a text with bitmap font
« Reply #1 on: February 06, 2023, 12:01:52 am »
I use this code but there is maybe a better way in sfml.


#include <iostream>
#include <algorithm>
#include <opencv2/opencv.hpp>

const int SCREEN_WIDTH = 80;
const int SCREEN_HEIGHT = 25;
const int FONT_HEIGHT = 8;
const int NUM_CHARS = 256;
const int TEXT_WIDTH = 320;
const int TEXT_HEIGHT = FONT_HEIGHT;

int main() {
  // Load bitmap font
  cv::Mat font = cv::imread("myFont.png", cv::IMREAD_GRAYSCALE);
  int FONT_WIDTH = font.cols / NUM_CHARS;

  // Render text to off-screen bitmap
  cv::Mat text = cv::Mat::zeros(TEXT_HEIGHT, TEXT_WIDTH, CV_8UC1);
  std::string text_str = "Hi, my name is scroller";
  int pos = 0;
  for (const char& c : text_str) {
    for (int i = 0; i < FONT_HEIGHT; i++) {
      for (int j = 0; j < FONT_WIDTH; j++) {
        text.at<uchar>(i, pos + j) = font.at<uchar>(i, (c + 32) * FONT_WIDTH + j);
      }
    }
    pos += FONT_WIDTH;
  }

  int offset = 0;
  while (true) {
    // Clear screen
    for (int i = 0; i < SCREEN_HEIGHT; i++) {
      for (int j = 0; j < SCREEN_WIDTH; j++) {
        std::cout << ' ';
      }
      std::cout << std::endl;
    }

    // Copy portion of off-screen bitmap to screen
    for (int i = 0; i < SCREEN_HEIGHT; i++) {
      for (int j = 0; j < SCREEN_WIDTH; j++) {
        int x = j + offset;
        if (x >= 0 && x < TEXT_WIDTH) {
          std::cout << (text.at<uchar>(i, x) > 128 ? '#' : ' ');
        } else {
          std::cout << ' ';
        }
      }
      std::cout << std::endl;
    }

    // Update offset
    offset--;
    if (offset < -TEXT_WIDTH + SCREEN_WIDTH) {
      offset = 0;
    }

    // Wait for a while
    std::this_thread::sleep_for(std::chrono::milliseconds(50));
  }

  return 0;
}
 
« Last Edit: February 06, 2023, 12:07:39 am by tom75000 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Scroll a text with bitmap font
« Reply #2 on: February 06, 2023, 04:04:48 pm »
You can use a single object that contains the entire text and the just simply scroll/move it in relation to the view.

Here's an example using Selba Ward's Bitmap Text in a video demo similar to 'demoscene scrollers':


Here's the video at a specific time near when the text scroller begins:
https://youtu.be/Yjs-oRhn2fE?t=66


But, of course, you don't have to use Selba Ward's Bitmap Text if you don't want to. You can use standard fonts with sf::Text and scroll those by or create your own "tilemap-style object" and move that across the view (as you might with a tilemap).

With all that said, it's possible to 'cull' the 'cells' that are not currently displayed. Some tilemaps do this. For example, Selba Ward's Tilemap does that ;)
« Last Edit: February 06, 2023, 04:09:51 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything