Sign in to follow this  

Problem: Program closes

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a problem. I want to create a timer that outputs the time on the screen using SDL. I use SDL_TTF to output text. I use this code to convert my interger number to a *char so DrawText() can output it: sprintf(text, "%d", number); But the problem is that when I add this code (as in the source below) my program does only run for like 1 second. Than it automaticly quits before the number is drawn. To increase number by 1 every second I use this code: current_time = SDL_GetTicks(); if(current_time >= (old_time + 1000)) {number++;} old_time = current_time; I draw the text to the screen using this function: DrawText(text,300,300); Please help me. Greetings, Rob (source is below)
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
using namespace std;

void MoveCursor();
void DrawText(char *s,int x, int y);

int Xpos = 0, Ypos = 0, Xold = 0, Yold = 0;
SDL_Surface *cursor;
SDL_Surface *screen;
SDL_Surface *Ftext;
SDL_Event event;
TTF_Font *font;
SDL_Color fgclr = {0,100,0,0};
int number = 0;
int current_time, old_time;
char *text = "0";





// MAIN STARTS HERE
int main(int argc, char** argv) {
    
// INITIALIZATION
if(     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) == -1 ) 
{
    cout << "Error: Failed to initialize SDL" << endl << SDL_GetError() << endl;
    return 1;
}
if ( TTF_Init() == -1 ) {
    cout << "Error: Failed to initialize TTF" << endl << TTF_GetError() << endl;
    return 1;
}    
atexit(SDL_Quit);    
atexit(TTF_Quit);
atexit(Mix_CloseAudio);
// END OF INIT

SDL_ShowCursor(0);




cursor = SDL_LoadBMP("cursor.bmp");

char *cpointer = "MOUSE EVENT WERKT!";
font = TTF_OpenFont("C:\\WINDOWS\\Fonts\\arial.ttf",30);
if(!font) {cout << "Error: failed to open font!" << endl; return 1;}


screen = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);

SDL_Rect dest;
dest.x = 200;
dest.y = 300;

Mix_Music *music;    
Mix_OpenAudio(22050,AUDIO_S16,2,4096); 
music = Mix_LoadMUS("5.mid"); 
Mix_PlayMusic(music,-1); 

bool run = true;
while(run == true) 
{
    
    while(SDL_PollEvent(&event)) {
        
    if (event.type == SDL_QUIT) {run = false;}
    if (event.key.keysym.sym == SDLK_q) {run = false;}
     }


current_time = SDL_GetTicks();

if(current_time >= (old_time + 1000))
number++;

old_time = current_time;

sprintf(text, "%d", number); 

   
MoveCursor();
DrawText(text,300,300);

   







}    

    Mix_FreeMusic(music);
    TTF_CloseFont(font);
    return 0;
}



void MoveCursor()
{
Xold = Xpos;
Yold = Ypos;
SDL_GetMouseState(&Xpos, &Ypos);

SDL_Rect dest;
dest.x = Xpos;
dest.y = Ypos;


SDL_Rect rtrail;
rtrail.x = Xold;
rtrail.y = Yold;
rtrail.w = cursor->w;
rtrail.h = cursor->h;
SDL_FillRect(screen,&rtrail,SDL_MapRGB(screen->format,0,0,0));
SDL_BlitSurface(cursor,NULL,screen,&dest);

SDL_Flip(screen);
}

void DrawText(char *s,int x, int y) {
  SDL_Surface *TextSurface;   
  SDL_Color clrFg = {0,0,255,0};   
  TTF_Font *font;
  
  font = TTF_OpenFont("C:\\windows\\fonts\\arial.ttf",48); 
  TextSurface = TTF_RenderText_Solid(font, s, clrFg);
  
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  
  SDL_BlitSurface(TextSurface,NULL,screen,&dest);
  SDL_Flip(screen);
  SDL_FreeSurface(TextSurface);
  TTF_CloseFont(font);
     }



Share this post


Link to post
Share on other sites
Solved 1 problem:

I changed:
char *text;

to:
char text[50];

But isn't it just the same?
Why is char *text wrong?????


Also now it will print 0 to the screen, but this won't go to 1, 2 etc.
Why? Whats wrong with my timer system?

Please help, I'm totaly lost :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Toadhead
I changed:
char *text;

to:
char text[50];

But isn't it just the same?
Why is char *text wrong?????


It's wrong because of the basics of pointers, all char *text; does is say you want to have a point to some character. However, when you use sprintf, it expects you to have a preallocated array of characters to use. So by telling it you have an array of 50 characters, and using that, the text was properly stored to memory. Before you were just randomly writing to system memory, which is a big no no!

Quote:
Also now it will print 0 to the screen, but this won't go to 1, 2 etc.
Why? Whats wrong with my timer system?


What you have right there is a logic error. Currently you have this:

if( current_time >= (old_time + 1000))
number++;
old_time = current_time;
sprintf(text, "%d", number);




Every time it assigns old_time to the current time, thus never incrementing. What you are looking to do is this:

if( current_time >= (old_time + 1000))
{
number++;
old_time = current_time;
}
sprintf(text, "%d", number);




Now that should work fine. If you have any other questions feel free to ask [smile]

Share this post


Link to post
Share on other sites
Thanks alot!
But now I have a new problem. I want to draw some black over the place were the last number is locate (otherwise it will just draw all numbers over the old numbers). But when I add the code below before the DrawText() function it will close just like what happened by my pointer problem.

How can I solve this?


SDL_Rect bg;
bg.y = 200;
bg.x = 200;
bg.w = 200;
bg.h = 200;

SDL_FillRect(screen, &bg, SDL_MapRGB(0,0,255,0));


Share this post


Link to post
Share on other sites
i think the problem here is your call to SDL_MapRGB. from my understanding, the first parameter is the pixel format, so you cant just pass 0 in.

changing it to

SDL_FillRect(screen, &bg, SDL_MapRGB(screen->format,0,255,0));

will hopefully fix that

Share this post


Link to post
Share on other sites

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this