Jump to content
  • Advertisement
Sign in to follow this  
Ultimate_Fusion

SDL bitmap fonts

This topic is 4427 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 made a game of pong and only need to put the score on the screen. I tried follow a tutorial (lazy foo's) and I can get one score that does change onto the screen e.g. "0" but if I use a variable I get weird output, aborting is printed out on the screen player1Score = TTF_RenderText_Solid( font, "Score:"+cs, textColor ); //cs = computer score

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>

#define SCREEN_WIDTH  750
#define SCREEN_HEIGHT 400
#define SPRITE_SIZE    64


int gameover;
int i=0;


SDL_Surface *screen;



//The color of the font
SDL_Color textColor = { 255,255,255 };


/* source and destination rectangles */
SDL_Rect rcSrc, rcSprite, rcBall, rcAI, rcScore;
// Surface that contains the font
SDL_Surface *gFont;
#define PITCH (screen->pitch / 4)
void HandleEvent(SDL_Event event)
{
	switch (event.type) {
		/* close button clicked */
		case SDL_QUIT:
			gameover = 1;
			break;
			 case SDL_MOUSEMOTION :
        //Get the mouse offsets
        rcSprite.y = event.button.y-32;
        break;
        

		/* handle the keyboard */
		case SDL_KEYDOWN:
			switch (event.key.keysym.sym) {
				case SDLK_ESCAPE:
				case SDLK_q:
					gameover = 1;
					break;
			
				case SDLK_UP:
					rcSprite.y -= 12;
					break;
				case SDLK_DOWN:
					rcSprite.y += 12;
					break;
			}
			break;
	}
}



void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;
    
    //Get offsets
    offset.x = x;
    offset.y = y;
    
    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}


int main(int argc, char* argv[])
{
	SDL_Surface *score, *temp, *sprite, *bg, *ball, *AI, *player1Score, *computerScore;
	// Surface that contains the font

	SDL_Rect rcbg;
	int colorkey;
    int xVel=5,yVel=0;
    	  int k=0; 
    	  	char ps = 'g', cs = '12';
	/* initialize SDL */
	SDL_Init(SDL_INIT_VIDEO);

	/* set the title bar */
	SDL_WM_SetCaption("SDL Animation", "SDL Animation");

	/* create window */
	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);

	/* set keyboard repeat */
	SDL_EnableKeyRepeat(70, 70);
	 TTF_Init();

//The font that's going to be used
TTF_Font *font;
//Open the font
    font = TTF_OpenFont( "s/lazy.ttf",50);
    //If there was an error in loading the font
    if( font == NULL )
    {
        return false;
    }

	/* load sprite */

	 temp   = SDL_LoadBMP("s/bat.bmp");
  sprite = SDL_DisplayFormat(temp);
  SDL_FreeSurface(temp);

	 temp   = SDL_LoadBMP("s/bat.bmp");
  AI = SDL_DisplayFormat(temp);
  SDL_FreeSurface(temp);
  /* setup sprite colorkey and turn on RLE */




	/* load grass */
	temp  = SDL_LoadBMP("s/background.bmp");
	bg = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	
	/* load grass */
	temp  = SDL_LoadBMP("s/ball.bmp");
	ball = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	  colorkey = SDL_MapRGB(screen->format, 255,255,255);
  SDL_SetColorKey(ball, SDL_SRCCOLORKEY | SDL_RLEACCEL, colorkey);

	/* set sprite position */
	rcSprite.x = 0;
	rcSprite.y = 150;
	
	/* set ball position */
	rcBall.x = 355;
	rcBall.y = 180;

	/* set animation frame */
	rcSrc.x = 0;
	rcSrc.y = 0;
	rcSrc.w = SPRITE_SIZE;
	rcSrc.h = SPRITE_SIZE;
	
    /* set AI ball position */
    rcAI.x = 726;
    rcAI.y = 20;
    
    rcScore.x = 50;
    rcScore.y = 50;
	gameover = 0;
    int x = 0, y = 0;//
	/* message pump */
	while (!gameover)
	{
		SDL_Event event;
		
		/* look for an event */
		if (SDL_PollEvent(&event)) {
			HandleEvent(event);
		}

		/* collide with edges of screen */
		if (rcSprite.x <= 0)
			rcSprite.x = 0;
		if (rcSprite.x >= SCREEN_WIDTH - SPRITE_SIZE) 
			rcSprite.x = SCREEN_WIDTH - SPRITE_SIZE;

		if (rcSprite.y <= 0)
			rcSprite.y = 0;
		if (rcSprite.y >= SCREEN_HEIGHT - SPRITE_SIZE) 
			rcSprite.y = SCREEN_HEIGHT - SPRITE_SIZE;
			
		/* Ball collide with edges of screen */
		if (rcBall.x <= 5)
		xVel =(0)-xVel;
		if (rcBall.x >= SCREEN_WIDTH - 25) 
			xVel -=(xVel*2);

		if (rcBall.y <= 5)
			yVel =(0)-yVel;
		if (rcBall.y >= SCREEN_HEIGHT - 25) 
	       yVel -=(yVel*2);
	       
 	    /* Ball collide with bat */
 
        if (rcBall.x == 35){
          k = rcSprite.y;    
        }
 	   
     	if (rcBall.x == 25){
		  if (rcSprite.y+64>rcBall.y&&rcBall.y >rcSprite.y-20) {
		    if(k-rcSprite.y>0){
		      yVel-=(k-rcSprite.y)/20;
            }
            else if(k-rcSprite.y==0){
    	      xVel -=(xVel*2);
            }
          	else{
         	  yVel+=(rcSprite.y-k)/20;
            }
          }
        }
         
        /* Ball collide with AI bat */
     	if (rcBall.x == 705 ){
		if (rcAI.y+64>rcBall.y&&rcAI.y >rcAI.y-20) 
		xVel -=(xVel*2);
         }	    
         
          rcAI.y =  rcBall.y-25;    
		/* draw the background */

				rcbg.x = 0; 
				rcbg.y = 0;
        SDL_BlitSurface(bg, NULL, screen, &rcbg);
        
             	
 player1Score = TTF_RenderText_Solid( font, "Score:"+cs, textColor );
 
computerScore= TTF_RenderText_Solid( font, ""+cs, textColor );
          apply_surface( 100, 200, player1Score, screen );
          //  apply_surface( 0, -40, computerScore, screen );
            


		/* draw the sprite */
		SDL_BlitSurface(sprite, &rcSrc, screen, &rcSprite);
	
        /* draw the AI bat */
		SDL_BlitSurface(AI, NULL, screen, &rcAI);
	    
		/* draw the ball */
		 rcBall.x+= xVel;
         rcBall.y+= yVel;
		SDL_BlitSurface(ball, NULL, screen, &rcBall);
		

   
      apply_surface( 0, -40, computerScore, screen );
   

    

		/* update the screen */
		SDL_UpdateRect(screen, 0, 0, 0, 0);

	}

	/* clean up */
	SDL_FreeSurface(temp);
 	SDL_FreeSurface(score);
	SDL_FreeSurface(ball);
	SDL_FreeSurface(player1Score);
		SDL_FreeSurface(computerScore);
	SDL_FreeSurface(AI);
	SDL_FreeSurface(sprite);
	SDL_FreeSurface(bg);
	TTF_Quit();
	SDL_Quit();

	return 0;
}


Share this post


Link to post
Share on other sites
Advertisement
Whats happening is that the implicit pointer to the array "Score:" is being pointed elsewhere by the value stored in the char cs. Pointer arithmetic.

What you need to do is something like this:

std::string output = "Score: ";
output += cs;
TTF_RenderText_SomeWay(font, output.c_str() ,... );

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!