This is my program in C:
/*
My marquee example: slow performance
compile:
gcc -c -MMD -o marqueeslow.o marqueeslow.c
gcc -o marqueeslow marqueeslow.o -lSDL -lSDL_ttf
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
int main(int argc, char *argv[]){
SDL_Surface *screen;
SDL_Surface *text;
TTF_Font *font;
SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
SDL_Color black = { 0x00, 0x00, 0x00, 0 };
SDL_Color *forecol;
SDL_Color *backcol;
SDL_Rect dstrect;
char *mytext=
{"You are never too old to set another goal "
"or to dream a new dream. "
"C. S. Lewis"};
int step;
int delay=15; // Big values=Slow marquee. Small values=Instable cadence
of marquee
int w_text, nada;
Uint32 Start=SDL_GetTicks();
Uint32 Elapsed;
Uint32 fps_ini, fps_now;
int fps;
int w=1280; // Width: of window
int h=300; // Height of window and font
SDL_Rect sourceArea = {0,0,w,h};
//--------------------------------------------
forecol=&white;
backcol=&black;
//------------------------------------
if ( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER)<0 ){
printf("Error in SDL_init(): %s\n", SDL_GetError());
exit(1);
}
//-----------------------------------
screen=SDL_SetVideoMode(w, h, 8,
SDL_HWACCEL|SDL_RESIZABLE|SDL_DOUBLEBUF
);
if (screen==NULL) {
printf("Error in SDL_SetVideoMode(): %s\n", SDL_GetError());
exit(2);
}
//-----------------------------------
if (TTF_Init()<0){
printf("Error in TTF_Init(): %s\n", SDL_GetError());
exit(3);
}
//-----------------------------------
font=TTF_OpenFont("Arial.ttf", h ); // Put here your True Type Font path
file
if (font==NULL) {
printf("Error in TTF_OpenFont(): %s\n", SDL_GetError());
exit(4);
}
text=TTF_RenderText_Blended( font, mytext, *forecol );
TTF_SizeText( font, mytext, &w_text, &nada ); // Get length of image of
text
step=-w; // set test to left position
fps=0;
fps_ini=SDL_GetTicks();
fps_now=fps_ini;
while( 1 ){
Start = SDL_GetTicks();
if( step<0 ){
dstrect.x=-step;
sourceArea.x=0;
sourceArea.y=0;
sourceArea.w=w;
sourceArea.h=h;
}else{
sourceArea.w=w;
sourceArea.x=step;
}
SDL_FillRect( screen, NULL ,
SDL_MapRGB( screen->format, backcol->r, backcol->g, backcol->b )
);
SDL_BlitSurface( text, &sourceArea, screen, &dstrect );
SDL_Flip( screen );
fps++;
step++;
if( step>w_text ){
step=-w;
SDL_FreeSurface(text);
text = TTF_RenderText_Blended( font, mytext, *forecol );
TTF_SizeText( font, mytext, &w_text, &nada );
}
Elapsed = SDL_GetTicks() - Start;
fps_now=SDL_GetTicks();
/* FPS calc */
if((fps_now-fps_ini)>=1000){
printf("Frames:%d / %3.3f Sec\n", fps,
(float)(fps_now-fps_ini)/1000 );
fps_ini=fps_now;
fps=0;
}
// Cadence adjust
if( Elapsed<delay ){
SDL_Delay( delay - Elapsed );
}
}
// Never ending
printf("Never print this...\n");
SDL_FreeSurface(text);
TTF_CloseFont(font);
return 0;
}
2010/3/31 Ricardo Leite <@Ricardo_Leite>> ANSI C language, Linux Slackware 13.
The FPS on my program is 28 frames per second in:
Linux Slackware 13 - kernel 2.6.29.6
Language: ANSI C
Compiller: gcc v 4.3.3
Lib: SDL version 1.2.13
CPU Intel atom 1.6Ghz
1Gb ram
CPU use is:25%
Memory: total=102496k used=117976k
2010/3/31 Mason Wheeler
----- Original Message ----
From: Christoph Nelles
Subject: Re: [SDL] Help to increase performance in SDL_ttf marquee
program.
Am 31.03.2010 19:37, schrieb Ricardo Leite:
My program that displays text messages is very slow and inefficient.
Please, help me with tips and tricks.
Buy better hardware.
DO NOT buy better hardware. That’s just contributing to the problem and
making it worse instead of fixing it.
First thing to do is run it through a profiler to see what’s taking so
much time. What language are you programming in, and what OS are you on?
This will help to find a good profiler.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org