Hello
This is my very first attempt to make any program with sdl,
and this is a very basic slideshow, you can see pictures, music, scroll
text, from text files,
i found the abe-1_0 (very nice game!) and from there lending the scrolling
text
so if you try to compile this program you need “Font.h” and “Font.c” from
abe.sourceforge.net/
and olso SFont.
Like this i can olso play and mp3 or video if i add smpeg
but i found more easy to start with ogg and SDL_mixer alone
Now this working but somehow crash after a few hours, memory leak i think is
the problem
i dont know good c c++(the basics) i am noob, but i like it, i do it for
hobby and try to learn.
This is not all the program, only a piece, but here is the problem and i
dont know what to do to solve this
so if anyone have little time to help me
#include “SDL.h”
#include “SDL_image.h”
#include “SDL_mixer.h”
#include “SFont.h”
#include “Font.h”
#include
#include
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define S_WIDTH 1024
#define S_HEIGHT 35
#define NEXTT 20 //next scroll text
#define NEXTP 15 //next picture
#define NEXTM 179 //next music
using namespace std;
// i compile like this
//g++ -g -L /home/ppap/sdl/slide/ -o testslide testslide.cpp SFont.c Font.c
sdl-config --cflags --libs
-lSDL_image -lSDL_mixer
SDL_Surface *screen, *image, *backsurf;
SDL_Rect src, dest;
SFont_Font *Font2, *Font6;
SDL_Event event;
Mix_Music music = NULL;
int quitProgram = 0;
char textin = new char [1024];
int text_pos = 1000;
int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16;
int audio_channels = 2;
int audio_buffers = 4096;
void drawScrollText() {
if((int)text_pos < -((int)strlen(textin)* (int)FONT_WIDTH)) {
text_pos = screen->w;
}
SFont_Write (screen, Font6, text_pos, screen->h -768, textin);
text_pos -= 1,9;
}
int main (int argc, char *argv[]){
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE
/* SDL_HWSURFACE | SDL_DOUBLEBUF*/);
if ( screen == NULL ) {
fprintf(stderr, “Couldn’t set video mode: %s\n”,
SDL_GetError());
exit(1);
}
backsurf = SDL_AllocSurface(screen->flags, S_WIDTH, S_HEIGHT,
screen->format->BitsPerPixel,
screen->format->Rmask, screen->format->Gmask, screen->format->Bmask,
0);
if (backsurf == NULL) {
fprintf(stderr, "Couldn't set backsurf\n",
SDL_GetError());
exit(1);
}
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels,
audio_buffers)) {
printf(“Unable to open audio!\n”);
exit(1);
}
Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
Font2 =
SFont_InitFont(IMG_Load(“Data/font/24P_Arial_NeonYellow.png”));
if(!Font2) {
printf( “look at 24P_Arial_NeonYellow.png\n”);
exit(1);
}
Font6 =
SFont_InitFont(IMG_Load(“Data/font/arial_28_mac_chrome_0.png”));
if(!Font6) {
printf(“look at arial_28_mac_chrome_0.png\n”);
exit(1);
}
//============================ picture =================================
//rpictures i have the full path of any picture i want to display
// /home/some/folder/picture1
// /home/some/folder/picture2
ifstream picture_in ("/home/ppap/sdl/slide/Data/rpictures", ios::in |
ios::binary);
if(!picture_in) {
cout << “cannot open rpictures.\n”;
return 1;
}
//============================ music =================================
//rmusic i have the full path of any music i want to hear
// /home/some/folder/music1.ogg
// /home/some/folder/music2.ogg
ifstream music_in ("/home/ppap/sdl/slide/Data/rmusic.", ios::in |
ios::binary);
if(!music_in) {
cout << “cannot open rmusic.\n”;
return 1;
}
//============================ text ===================================
//just plain text, i use it to read some ebook lol
ifstream text_in ("/home/ppap/sdl/slide/Data/rfont", ios::in | ios::binary);
if(!text_in) {
cout << “cannot open rfont.\n”;
return 1;
}
//============================= time ==================================
time_t lasttime_m = time(NULL)-NEXTM;
time_t lasttime_p = time(NULL)-NEXTP;
time_t lasttime_t = time(NULL)-NEXTT;
while (!quitProgram) {
SDL_Event event;
SDL_BlitSurface(backsurf, NULL, screen, NULL);
//============================= music =================================
if (time(NULL)>lasttime_m+NEXTM){
char* M_str = new char [150];
if (!music){
Mix_HaltMusic();
Mix_FreeMusic(music);
// SDL_Delay(1);
}
music_in.getline(M_str, 149);
music = Mix_LoadMUS(M_str);
if (music == NULL){
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0 ));
SFont_Write (screen, Font2, 100, 500, "Problem with music, look at this
line -->");
SFont_Write (screen, Font2, 100, 550, M_str);
}
if(music){
Mix_PlayMusic(music, 0);
}
cout << M_str << '\n';
lasttime_m = time(NULL);
delete [] M_str;
}
if (music_in.eof()){
music_in.clear();
music_in.close();
music_in.open ("/home/ppap/sdl/slide/Data/rmusic", ios::in |
ios::binary);
music_in.seekg(0, ios::beg);
cout << “EOF music” << ‘\n’;
}
/============================= picture =================================/
if (time(NULL)>lasttime_p+NEXTP){
char* P_Stra = new char [155];
if (image){
SDL_FreeSurface(image);
//SDL_Delay(1);
}
picture_in.getline(P_Stra, 154);
image = IMG_Load(P_Stra);
if (image == NULL){
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0 ));
SFont_Write (screen, Font2, 100, 500, "Problem in text file, look at
this line -->");
SFont_Write (screen, Font2, 100, 550, P_Stra);
}
else if (image){
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
dest.x = 0;
dest.y = 35;
SDL_BlitSurface(image, &src, screen, &dest);
}
cout << P_Stra << '\n';
lasttime_p = time(NULL);
delete [] P_Stra;
if (picture_in.eof()){
picture_in.clear();
picture_in.close();
picture_in.open("/home/ppap/sdl/slide/Data/rpictures", ios::in |
ios::binary);
picture_in.seekg(0, ios::beg);
cout << “EOF image” << ‘\n’;
}
}
/============================= text =================================/
if (time(NULL)>lasttime_t+NEXTT) {
if (Font6){
SFont_FreeFont(Font6);
}
Font6 =
SFont_InitFont(IMG_Load(“Data/font/arial_28_mac_chrome_0.png”));
text_pos = 1000;
text_in.getline(textin, 1023);
cout << textin << ‘\n’;
lasttime_t = time(NULL);
}
if (text_in.eof()){
text_in.clear();
text_in.close();
text_in.open ("/home/ppap/sdl/slide/Data/rfont", ios::in |
ios::binary);
text_in.seekg(0, ios::beg);
cout << “EOF text” << ‘\n’;
}
while (SDL_PollEvent (&event)) {
switch (event.type) {
case SDL_QUIT:
cout << "Bye!" << '\n';
quitProgram = 1;
break;
case SDL_KEYUP:
switch( event.key.keysym.sym ){
case SDLK_ESCAPE:
printf("Bye!.\n");
quitProgram = 1;
break;
default:
break;
}
}
}
drawScrollText();
//SDL_Flip(screen);
SDL_UpdateRect(screen,0, 0, 0, 0);
SDL_Delay(10);
}
delete [] textin;
picture_in.close();
text_in.close();
music_in.close();
Mix_HaltMusic();
Mix_CloseAudio();
Mix_FreeMusic(music);
SFont_FreeFont(Font2),(Font6);
SDL_FreeSurface(image),(backsurf), (screen);
return 0;
}
thanks
ppap_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963