hCon
hCon

Reputation: 233

SDL - Printing text like a console?

I've got some code (below) that uses SDL_ttf and would like to:

  1. Be able to render text (from the TTF) in alignment (to a buffer or array as well) like a console would (having each character printed in it's individual cell).
  2. Be able to utilize a blinking cursor (possibly render and un-render an underscore, maybe?)
  3. Be able to let the user input text from the keyboard and render each char on the screen right when it is typed (using SDLK_charhere).

Getting back to #1: I'm thinking about getting the width of the previous character printed on the screen (from the TTF) and using its width (in pixels) to print the next character right after the previous character, plus 2 pixels. <-- PLEASE TELL ME IF THE SPACING BETWEEN CHARS IN A REGULAR WIN32 CONSOLE IS A DIFFERENT SIZE IN PIXELS.

Here's the code that needs to be modified:

#include "include/SDL/SDL.h"
#include "include/SDL/SDL_ttf.h"

int currentX = 0;
int currentY = 0;
int newW;
int newH;
SDL_Surface* screen;
SDL_Surface* fontSurface;
SDL_Color fColor;
SDL_Rect fontRect;

SDL_Event event;

TTF_Font* font;

//Initialize the font, set to white
void fontInit(){
        TTF_Init();
        font = TTF_OpenFont("dos.ttf", 12);
        fColor.r = 0; // 255
        fColor.g = 204; // 255
        fColor.b = 0; //255
}

//Print the designated string at the specified coordinates
void PrintStr(char *c, int x, int y){
        fontSurface = TTF_RenderText_Solid(font, c, fColor);
        fontRect.x = x;
        fontRect.y = y;
        SDL_BlitSurface(fontSurface, NULL, screen, &fontRect);
        SDL_Flip(screen);
}

int main(int argc, char** argv)
{
    // Initialize the SDL library with the Video subsystem
    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);

    //Create the screen
    screen = SDL_SetVideoMode(320, 480, 0, SDL_SWSURFACE);

    //Initialize fonts
    fontInit();

    PrintStr("", 0, 0);

    do {
        // Process the events
        while (SDL_PollEvent(&event)) {
            switch (event.type) {

                case SDL_KEYDOWN:
                    switch (event.key.keysym.sym) {
                    // Escape forces us to quit the app
                        case SDLK_ESCAPE:
                            event.type = SDL_QUIT;
                        break;

                        default:
                        break;
                    }
                break;

            default:
            break;
        }
    }
    SDL_Delay(10);
    } while (event.type != SDL_QUIT);

    // Cleanup
    SDL_Quit();

    return 0;
}

Upvotes: 0

Views: 2727

Answers (1)

Cramer
Cramer

Reputation: 1795

This isn't a trivial thing to do, but it sounds like a good learning project! You're maybe going to need a few thousand lines of code rather than the few tens you have up there. Perhaps start by considering these questions and their answers.

  • Do you want a fixed width font, or a variable width font?
  • How can you buffer the rendered text in an intelligent way? (glyphs or lines for eg.)
  • How can you buffer the text itself in an intelligent way?
  • How can you translate key presses into text?
  • What is translating and driving all this?

All these will need to be considered along with the most important question of all:

  • Do I want to do this?

If you do do this, it will teach you a lot about programming, but it might not give you the best full screen console you're looking for.

Upvotes: 1

Related Questions