diff options
| author | Silvan Jegen <s.jegen@gmail.com> | 2019-01-07 21:59:53 +0100 | 
|---|---|---|
| committer | Silvan Jegen <s.jegen@gmail.com> | 2019-01-07 21:59:53 +0100 | 
| commit | dc9b56a7650dd65d574c0e33ea7cb9e1612622f7 (patch) | |
| tree | 5a9318c7722ed74b6333344c041994dfb3d1488a | |
| parent | c324d94bfd3151df3f999f33208ef07bf9d95fb8 (diff) | |
Start porting code to SDL2 (WIP)
| -rw-r--r-- | game.c | 22 | ||||
| -rw-r--r-- | helpscreen.c | 17 | ||||
| -rw-r--r-- | level.c | 25 | ||||
| -rw-r--r-- | main.c | 43 | ||||
| -rw-r--r-- | render.c | 32 | 
5 files changed, 91 insertions, 48 deletions
@@ -75,7 +75,7 @@ int player_action( signed int xdiff, signed int ydiff, struct level_struct *curr  }
 -int play_level( struct level_struct *currentlevel, SDL_Surface * surf_screen )
 +int play_level(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture * surf_screen )
  {
  	// Loop until the user completes the level
  	int player_status = PLAYER_IS_OK;
 @@ -87,7 +87,7 @@ int play_level( struct level_struct *currentlevel, SDL_Surface * surf_screen )  		// WaitEvent is like PollEvent, but instead we pause the program's
  		// execution until we get input
  		SDL_WaitEvent( &userinput );
 -		render( currentlevel, surf_screen );
 +		render(renderer, currentlevel, surf_screen);
  		// Player movement
  		signed int y, x;
 @@ -122,7 +122,7 @@ int play_level( struct level_struct *currentlevel, SDL_Surface * surf_screen )  				case SDLK_h:
  					// View the help screen
 -					help( surf_screen );
 +					help(renderer, surf_screen);
  					x = 0;
  					y = 0;
 @@ -146,7 +146,7 @@ int play_level( struct level_struct *currentlevel, SDL_Surface * surf_screen )  			if ( check_echidna_proximity( currentlevel ) == 1 ) player_status = PLAYER_IS_DEAD;
  			// Show a losing screen if applicable
 -			if (player_status == PLAYER_IS_DEAD) render_a_losingscreen( currentlevel, surf_screen);
 +			if (player_status == PLAYER_IS_DEAD) render_a_losingscreen(renderer, currentlevel, surf_screen);
  		}
  	}
 @@ -158,11 +158,11 @@ int play_level( struct level_struct *currentlevel, SDL_Surface * surf_screen )  	return player_status;
  }
 -void game_loop( SDL_Surface * surf_screen )
 +void game_loop(SDL_Renderer* renderer, SDL_Texture* surf_screen)
  {
  	struct level_struct currentlevel;
 -	level_load_resources( ¤tlevel ); // load pixmaps
 +	level_load_resources(renderer, ¤tlevel ); // load pixmaps
  	// Initialise initial player face
  	currentlevel.player_face = 2;
 @@ -185,7 +185,7 @@ void game_loop( SDL_Surface * surf_screen )  			currentlevel.py = currentlevel.sy;
  			// Main game loop
 -			level_state = play_level( ¤tlevel, surf_screen );
 +			level_state = play_level(renderer, ¤tlevel, surf_screen );
  		}
 @@ -194,9 +194,11 @@ void game_loop( SDL_Surface * surf_screen )  	// The user has now completed all of the game
  	// Show them the winning screen
 -	SDL_BlitSurface( currentlevel.surf_winning_screen, NULL, surf_screen, NULL);
 -	SDL_Flip( surf_screen );
 -	
 +
 +	SDL_RenderClear(renderer);
 +	SDL_RenderCopy(renderer, currentlevel.surf_winning_screen, NULL, NULL);
 +	SDL_RenderPresent(renderer);
 +
  	SDL_Delay(20 * 1000); // 20 * 1000msecs = 20 seconds
  	// Once this function ends, we return to the menu
 diff --git a/helpscreen.c b/helpscreen.c index dbbbce0..bebc936 100644 --- a/helpscreen.c +++ b/helpscreen.c @@ -1,12 +1,19 @@ -void help( SDL_Surface * surf_screen )
 +void help(SDL_Renderer* renderer, SDL_Texture * surf_screen )
  {
  	// Load help image
 -	SDL_Surface *surf_help = IMG_Load( "helpscreen.png" );
 +	SDL_Surface* surf_help = IMG_Load( "helpscreen.png" );
 +  SDL_Texture* surf_help_tex = SDL_CreateTextureFromSurface(renderer, surf_help);
  	// Draw this image onto screen
 -	SDL_BlitSurface( surf_help, NULL, surf_screen, NULL);
 -	SDL_Flip( surf_screen );
 +	//SDL_BlitSurface( surf_help, NULL, surf_screen, NULL);
 +	//SDL_Flip( surf_screen );
 +
 +	SDL_UpdateTexture(surf_screen, NULL, surf_help_tex, 640 * sizeof (Uint32));
 +
 +	SDL_RenderClear(renderer);
 +	SDL_RenderCopy(renderer, surf_screen, NULL, NULL);
 +	SDL_RenderPresent(renderer);
  	// now free the memory we used to draw this image
  	free( surf_help );
 @@ -27,5 +34,3 @@ void help( SDL_Surface * surf_screen )  		}
  	}
  }
 -
 -
 @@ -66,14 +66,14 @@ struct level_struct  	int sx, sy;  // Player X and Y starting positions
  	// Resources
 -	SDL_Surface * surf_entities; // Player and echidna pictures
 -	SDL_Surface * surf_tiles;    // Level tiles/squares
 +	SDL_Texture * surf_entities; // Player and echidna pictures
 +	SDL_Texture * surf_tiles;    // Level tiles/squares
  	// A various selection of losing screens
 -	SDL_Surface * surf_losingscreens[5];
 +	SDL_Texture * surf_losingscreens[5];
  	// The screen to display when the game ends
 -	SDL_Surface * surf_winning_screen;
 +	SDL_Texture * surf_winning_screen;
  	// Up to ten echidnas
  	struct echidna echidnas[10];
 @@ -99,16 +99,21 @@ void eat_until_newline( FILE *currentfile )  // Load the graphics used in levels
 -void level_load_resources( struct level_struct *level )
 +void level_load_resources(SDL_Renderer* renderer, struct level_struct *level )
  {
  	// Load the images for things into their own 'surfaces'
 -	level->surf_entities = IMG_Load( "entities.png" );
 -	level->surf_tiles = IMG_Load( "tiles.png" );
 +	SDL_Surface* surf_entities = IMG_Load( "entities.png" );
 +  level->surf_entities = SDL_CreateTextureFromSurface(renderer, surf_entities);
 +
 +	SDL_Surface* surf_tiles = IMG_Load( "tiles.png" );
 +  level->surf_tiles = SDL_CreateTextureFromSurface(renderer, surf_tiles);
  	// Do the same for the losing screens
 -	level->surf_losingscreens[0] = IMG_Load("failscreen_01.png");
 -	level->surf_winning_screen = IMG_Load("winningscreen.png");
 -	
 +	SDL_Surface* surf_losingscreen = IMG_Load("failscreen_01.png");
 +  level->surf_losingscreens[0] = SDL_CreateTextureFromSurface(renderer, surf_losingscreen);
 +
 +	SDL_Surface* surf_winning_screen = IMG_Load("winningscreen.png");
 +  level->surf_winning_screen = SDL_CreateTextureFromSurface(renderer, surf_winning_screen);
  }
 @@ -53,16 +53,19 @@ int main(int argc, char *argv[])  	if ( SDL_Init( SDL_INIT_VIDEO ) == -1 ) error_sdl("Could not initialise SDL");
  	atexit(SDL_Quit);
 -	// Set surf_screen
 -	SDL_Surface* surf_screen = NULL;
  	SDL_Window * sdlWindow = SDL_CreateWindow("EchidneaMenace", SDL_WINDOWPOS_UNDEFINED,
  			SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_OPENGL );
 -	if ( surf_screen == NULL ) error_sdl("Could not set videomode");
  	SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0);
 +
 +	// Set surf_screen
 +	SDL_Texture* surf_screen = SDL_CreateTexture(renderer,
 +															SDL_PIXELFORMAT_ARGB8888,
 +															SDL_TEXTUREACCESS_STREAMING,
 +															640, 480);
 +	if ( surf_screen == NULL ) error_sdl("Could not set videomode");
  	printf("done\n");
 -	
  	/// Menu
  	printf(" ==== Entered menu\n");
 @@ -70,8 +73,20 @@ int main(int argc, char *argv[])  	SDL_Event menuevent;
  	// Load the menu graphic into memory
  	SDL_Surface *surf_title = IMG_Load( "titlescreen.png" );
 +	if (!surf_title) {
 +		error_sdl("could not get title surface\n");
 +	}
 +
 +	SDL_Texture *surf_title_tex = SDL_CreateTextureFromSurface(renderer, surf_title);
 +	if (!surf_title_tex) {
 +		error_sdl("could not get title texture from surface\n");
 +	}
 -	
 +	int w, h;
 +	Uint32 format;
 +	SDL_QueryTexture(surf_title_tex, &format, NULL, &w, &h);
 +	printf("w: %d, h: %d, format: %s\n", w, h, SDL_GetPixelFormatName(format));
 +
  	/* We loop the menu continously for a number of reasons:
  	 *  1) To make sure we capture and respond to user input
  	 *  2) To continue redrawing the menu graphic.
 @@ -81,8 +96,14 @@ int main(int argc, char *argv[])  	 */
  	while ( 1 )
  	{
 -		SDL_BlitSurface( surf_title, NULL, surf_screen, NULL); // Draw menu
 -		SDL_Flip( surf_screen ); // Update screen
 +
 +		SDL_UpdateTexture(surf_screen, NULL, surf_title->pixels, surf_title->pitch);
 +
 +		//SDL_BlitSurface( surf_title, NULL, surf_screen, NULL); // Draw menu
 +
 +		SDL_RenderClear(renderer);
 +		SDL_RenderCopy(renderer, surf_screen, NULL, NULL);
 +		SDL_RenderPresent(renderer);
  		// The while statement attempts to go through every key the user has pressed
  		// it stops when the user has not pressed any more keys
 @@ -92,8 +113,8 @@ int main(int argc, char *argv[])  			if ( menuevent.type == SDL_KEYDOWN )
  			{
  				// Menu choice selection
 -				if      ( menuevent.key.keysym.sym == SDLK_p ) game_loop( surf_screen );
 -				else if ( menuevent.key.keysym.sym == SDLK_h ) help( surf_screen );
 +				if      ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer, surf_screen);
 +				else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer, surf_screen);
  				else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0);
  			}
  		}
 @@ -103,9 +124,6 @@ int main(int argc, char *argv[])  		SDL_Delay( 200 ); 
  	}
 -	
 -	
 -	
  	/* Technically the computer will never reach this point, because the while statement
  	 * above will run forever until the user quits the game.
  	 * 
 @@ -113,5 +131,4 @@ int main(int argc, char *argv[])  	 */
  	return 0;
 -	
  }	
 @@ -49,7 +49,7 @@ void get_pixmap_level( char number, SDL_Rect * crop )  // render() paints everything onto the screen when called
 -void render( struct level_struct *currentlevel, SDL_Surface * surf_screen )
 +void render(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture * surf_screen )
  {
  	/* The picture on-screen is made in three steps:
  	 *   1) Draw the level onto the screen surface
 @@ -87,7 +87,9 @@ void render( struct level_struct *currentlevel, SDL_Surface * surf_screen )  			destination.y = tiley * TILE_SIZE;
  			/// Draw this tile onto the screen
 -			SDL_BlitSurface ( currentlevel->surf_tiles, &pixmap_crop, surf_screen, &destination );
 +			//SDL_BlitSurface ( currentlevel->surf_tiles, &pixmap_crop, surf_screen, &destination );
 +
 +		  SDL_UpdateTexture(surf_screen, &destination, currentlevel->surf_tiles, 640 * sizeof (Uint32));
  		}
  	}
 @@ -98,7 +100,8 @@ void render( struct level_struct *currentlevel, SDL_Surface * surf_screen )  	destination.y = currentlevel->py * TILE_SIZE;
  	// Draw onto screen
 -	SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination );
 +	//SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination );
 +	 SDL_UpdateTexture(surf_screen, &destination, currentlevel->surf_entities, 640 * sizeof (Uint32));
  	// Now draw all of the echidnas
  	int ech_no; // Echidna number	 
 @@ -117,21 +120,32 @@ void render( struct level_struct *currentlevel, SDL_Surface * surf_screen )  			destination.y = current_echidna.ypos * TILE_SIZE;
  			// Draw it onscreen
 -			SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination );
 +			// SDL_BlitSurface ( currentlevel->surf_entities, &pixmap_crop, surf_screen, &destination );
 +	    SDL_UpdateTexture(surf_screen, &destination, currentlevel->surf_entities, 640 * sizeof (Uint32));
  		}
  	}
  	/// Flip screen, so everything we have 'blitted' can now be seen
 -	SDL_Flip( surf_screen );
 -	
 +	//SDL_Flip( surf_screen );
 +
 +	SDL_RenderClear(renderer);
 +	SDL_RenderCopy(renderer, surf_screen, NULL, NULL);
 +	SDL_RenderPresent(renderer);
  }
 -void render_a_losingscreen(  struct level_struct *currentlevel, SDL_Surface * surf_screen )
 +void render_a_losingscreen(SDL_Renderer* renderer, struct level_struct *currentlevel, SDL_Texture * surf_screen )
  {
  	// Render a losing screen
 -	SDL_BlitSurface ( currentlevel->surf_losingscreens[0], NULL, surf_screen, NULL );
 -	SDL_Flip( surf_screen );
 +	//SDL_BlitSurface ( currentlevel->surf_losingscreens[0], NULL, surf_screen, NULL );
 +
 +	SDL_UpdateTexture(surf_screen, NULL, currentlevel->surf_losingscreens[0], 640 * sizeof (Uint32));
 +
 +	//SDL_Flip( surf_screen );
 +
 +	SDL_RenderClear(renderer);
 +	SDL_RenderCopy(renderer, surf_screen, NULL, NULL);
 +	SDL_RenderPresent(renderer);
  	// Wait a second so the user can see the losing screen
  	SDL_Delay(500);
  | 
