Advertisement

Blackjack using SDL, need help

Started by February 04, 2003 02:34 PM
10 comments, last by MetroidHunter 21 years, 9 months ago
this tutorial will help you with that problem. you need to redraw the areas of the surface drawn over by youre text before drawing the text to the surface.

here is an analogy, at present its like youre drawing onto a chalkboard, without cleaning it before drawing it again. you only need to clean the area of the chalkboard you are using, this is faster than cleaning the whole chalkboard each time you need to draw to a small part of it.

http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut2
For some reason, after just one hand, it stores my wins and losses as over 1000000000... any way to fix this? Right now I have them as global variables... should I try making them local to main?


    Deck deck;				    //	creates the deck  Player player(0);			    //	creates the player  Player dealer(1);		    	//	creates the dealer  SDL_Surface *screen;          //  points to the screen  SDL_Surface *back;            //  points to the background  SDLFont *font;                //  sets yellow font    char result[4];               //  stores result of last hand  float funds = 500;			//	stores the amount of money you have.  int endhand = 0;			    //	signals end of current hand  int won = 0;                  //  how many hands player has won  int lost = 0;                 //  how many hands player has lost  int tied = 0;                 //  how many hands player has tied//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~sets up the background image~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2){  SDL_Rect dest;  dest.x = x;  dest.y = y;  SDL_Rect dest2;  dest2.x = x2;  dest2.y = y2;  dest2.w = w;  dest2.h = h;  SDL_BlitSurface(img, &dest2, screen, &dest);}        int InitBG(){	back = SDL_LoadBMP("data/cardtable.bmp");	return 0;}//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~draws the background to the buffer~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~void DrawBG(){  SDL_Rect dest;  dest.x = 0;  dest.y = 0;  SDL_BlitSurface(back, NULL, screen, &dest);}//  ~~~~~~~draws cards to the buffer, along with statistics, and flips the buffer to the screen~~~~~~void DrawScene(){  DrawIMG(back, 50, 420, 500, 95, 50, 100);  player.displayCards(50,screen);  dealer.displayCards(250,screen);  drawString(screen,font,50,420,"Cash: $%.2f",funds);  drawString(screen,font,50,440,"Won:%d  Lost:%d  Tied:%d", won, lost, tied);  drawString(screen,font,50,460,"Result of Last Hand: %s", result);  SDL_Flip(screen);}//	~~~~~~~~~~~the following function is used to determine if the dealer gets another card~~~~~~~~~~~int dealerDraw( int card, int score, int dscore, int gameover ){  if (( dealer.showHand() < 4 ) && ( dscore < 17 ))  {    card = deck.dealcard();    dealer.getCard(card);    DrawScene();    dscore = dealer.getScore();    if ( dscore > 21 ) { gameover = 2; }  }  else if (( dealer.showHand() < 4 ) && ( dscore == 17 ) && ( score > dscore ))  {    card = deck.dealcard();    dealer.getCard(card);    DrawScene();    dscore = dealer.getScore();    if ( dscore > 21 ) { gameover = 2; }  }  return (gameover);}//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~determines the outcome of the hand~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~void endHand(int gameover){	switch(gameover)	{	  case (1) : {                     funds -= 100;                    lost += 1;                    strcpy(result,"Lost! (you went bust)");          break; }	  case (2) : {                     funds += 100;                    won += 1;                    strcpy(result,"Won! (dealer went bust)");		  break; }      case (3) : {                     funds += 100;                    won += 1;                    strcpy(result,"Won! (you had a higher score)");		  break; }      case (4) : {                     funds -= 100;                    lost += 1;                    strcpy(result,"Lost! (dealer had a higher score)");		  break; }      case (5) : {                     tied += 1;                    strcpy(result,"Tied! (both had same score)");		  break; }	}}// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~int main(int argc, char *argv[]){  //	objects declared    //Uint8* keys;                  //  stores the keys    //  initializes audio and video, returns error if unsuccessful  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )  {    cout << "Unable to init SDL: " << SDL_GetError() << endl;    exit(1);  }  atexit(SDL_Quit);  //  sets audio; 44100 sound-rate, 16 bit sound, 2 channels, 2048 sound buffers; returns error if can''t initialize  /*if(Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)  {    cout << "Warning: Couldn''t set 44100 Hz 16-bit audio" << endl << "- Reason: " <<	SDL_GetError() << endl;  }*/  //  sets video mode to 800 * 600 resolution, 32 bit, with full screen  screen=SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);  if ( screen == NULL )  {    cout << "Unable to set 800x600 video: " << SDL_GetError() << endl;    exit(1);  }  font = initFont("data/font");  //  initializes font    InitBG();  DrawBG();  int done=0;  int score = 0;  int dscore = 0;  while(done == 0)  {	int ctr;	int card;					//	stores the player''s card values	int gameover = 0;											/*	if equals 1, player went over 21, dealer wins									if equals 2, dealer went over 21, player wins									if equals 3, player has higher score, player wins									if equals 4, dealer has higher score, dealer wins 									if equals 5, player and dealer are tied, neither win */	score = 0;		dscore = 0;	endhand = 0;			//	shuffles deck, if neccessary	deck.rebuild();	for(ctr = 0; ctr < 2; ctr++)	{	  card = deck.dealcard();			//	deals a card to the player	  player.getCard(card);					DrawScene();	  card = deck.dealcard();			//	deals a card to the deal	  dealer.getCard(card);			DrawScene();	}	DrawScene();	score = player.getScore();	dscore = dealer.getScore();				while (( player.showHand() < 4 ) && ( endhand == 0 ) && gameover == 0)	{	  SDL_Event event;			  while ( SDL_PollEvent(&event) )	  {		        if ( event.type == SDL_QUIT )  {  done = 1; endhand = 1; }		        if ( event.type == SDL_KEYDOWN )		{		  if ( event.key.keysym.sym == SDLK_y )		  {	 		card = deck.dealcard();			player.getCard(card);			DrawScene();			event.key.keysym.sym = SDLK_x;			score = player.getScore();			if ( score > 21 ) { gameover = 1; }	  						gameover = dealerDraw(card, score, dscore, gameover);			event.type = SDL_KEYUP;					  }		  if ( event.key.keysym.sym == SDLK_n )		  {			gameover = dealerDraw(card, score, dscore, gameover);			endhand = 1;			event.type = SDL_KEYUP;		  }	  		  if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; endhand = 1;}		}			  	  }  	}	if ( gameover == 0 )	{	  if ( score > dscore )	  {		gameover = 3;	  }	  if ( score < dscore )	  {		gameover = 4;	  }	  if ( score == dscore )	  {		gameover = 5;	  }	  	}	endHand(gameover);	for (int r = 0; r < 75; r++)	{DrawScene();}	player.reset();	dealer.reset();	DrawScene();	  }	return 0;}  

This topic is closed to new replies.

Advertisement