I have been using the C programming language. SDL, and SDL_ttf to try and
make a TypeWriter effect. Still haven’t been able to figure it out. I have
asked on the forums at gamedev. Didn’t get much help from them. Except a
response I don’t understand which said:
- Declare an Uint32 typewriter_starttime and initialize it with the return
value of SDL_GetTicks() when the text is supposed to begin appearing. - Each frame, calculate Uint32 typewriter_time = SDL_GetTicks() -
typewriter_starttime as the time that passed since the text is supposed to
appear. Then calculate the number of characters to display based on that
time interval (note that the number is in milliseconds) and display the
appropriate std::string::substr using SDL_ttf.
Maby someone on this mailing list would be kind enofe to help me with this.
Thanks.
Here is a code I set up for it, but don’t know how to program in the type
writer effect. The effect you see in many Visual Novel games, and RPGS.
Where the text is printed one letter at a time.
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include <stdio.h>
SDL_Surface screen; / global for convenience */
message(char *text)
{
if(TTF_Init()==-1) {
printf(“TTF_Init: %s\n”, TTF_GetError());
exit(2);
}
TTF_Font *font;
font=TTF_OpenFont(“msgothic.ttc”, 16);
if(!font) {
printf(“TTF_OpenFont: %s\n”, TTF_GetError());
// handle error
}
// Render some UTF8 text in solid black to a new surface
// then blit to the upper left of the screen
// then free the text surface
//SDL_Surface *screen;
SDL_Color color={0,0,0};
SDL_Surface *text_surface;
if(!(text_surface=TTF_RenderUTF8_Solid(font,text,color))) {
//handle error here, perhaps print TTF_GetError at least
} else {
SDL_BlitSurface(text_surface,NULL,screen,NULL);
//perhaps we can reuse it, but I assume not for simplicity.
SDL_FreeSurface(text_surface);
}
}
static void example()
{
Uint8 *keystate;
int quit = 0;
while (quit == 0) {
SDL_PumpEvents();
keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_q] || keystate[SDLK_ESCAPE]) quit = 1;
message(“This is a test string.”);
}
}
int main(int argc, char *argv[])
{
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError());
return 1;
}
if (SDL_Init(SDL_INIT_TIMER) < 0) {
printf("Unable to initialize SDL Timer: %s\n", SDL_GetError());
return 1;
}
atexit(SDL_Quit);
screen = SDL_SetVideoMode(800,600,16,0);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
SDL_WM_SetCaption("Example", "Example");
screen = SDL_GetVideoSurface();
example();
}–
View this message in context: http://www.nabble.com/Help-Please.-SDL_TTF-TypeWriter-Effect.-tp25084538p25084538.html
Sent from the SDL mailing list archive at Nabble.com.