sdl2forthewin/main.cpp

271 lines
5.7 KiB
C++

/*This source code copyrighted by Lazy Foo' Productions (2004-2022)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>
#include <SDL_mixer.h>
//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//Starts up SDL and creates window
bool init();
//Loads media
bool loadMedia();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;
SDL_Renderer* gRenderer = NULL;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;
Mix_Music* gMusic = NULL;
bool init()
{
//Initialization flag
bool success = true;
char* data_path = NULL;
data_path = SDL_GetBasePath();
// printf("path: %s",data_path);
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO ) < 0 )
{
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create window
gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
if( gWindow == NULL )
{
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create vsynced renderer for window
gRenderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
//Get window surface
gScreenSurface = SDL_GetWindowSurface( gWindow );
}
//Initialize SDL_mixer
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048)<0)
{
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Couldn't initialize SDL audio mixer!, Error: %s", SDL_GetError());
}
}
return success;
}
bool loadMedia()
{
//Loading success flag
bool success = true;
//Load splash image
gHelloWorld = SDL_LoadBMP( "../hello-sdl.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "hello-sdl.bmp", SDL_GetError() );
success = false;
}
gMusic = Mix_LoadMUS("../sillypuppy.ogg");
if(gMusic == NULL)
{
printf("Failed to load music! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
return success;
}
void close()
{
//Deallocate surface
SDL_FreeSurface( gHelloWorld );
gHelloWorld = NULL;
//Destroy window
SDL_DestroyWindow( gWindow );
gWindow = NULL;
Mix_FreeMusic(gMusic);
gMusic = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main( int argc, char* args[] )
{
//Start up SDL and create window
if( !init() )
{
printf( "Failed to initialize!\n" );
}
else
{
//Load media
if( !loadMedia() )
{
printf( "Failed to load media!\n" );
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
//While application is running
while( !quit )
{
//Handle events on queue
while( SDL_PollEvent( &e ) != 0 )
{
//User requests quit
if( e.type == SDL_QUIT )
{
quit = true;
}
//Handle key press
else if( e.type == SDL_KEYDOWN )
{
switch( e.key.keysym.sym )
{
// Play high sound effect
// case SDLK_1:
// Mix_PlayChannel( -1, gHigh, 0 );
// break;
//
// Play medium sound effect
// case SDLK_2:
// Mix_PlayChannel( -1, gMedium, 0 );
// break;
//
// Play low sound effect
// case SDLK_3:
// Mix_PlayChannel( -1, gLow, 0 );
// break;
//
// Play scratch sound effect
// case SDLK_4:
// Mix_PlayChannel( -1, gScratch, 0 );
// break;
case SDLK_9:
//If there is no music playing
if( Mix_PlayingMusic() == 0 )
{
//Play the music
Mix_PlayMusic( gMusic, -1 );
}
//If music is being played
else
{
//If the music is paused
if( Mix_PausedMusic() == 1 )
{
//Resume the music
Mix_ResumeMusic();
}
//If the music is playing
else
{
//Pause the music
Mix_PauseMusic();
}
}
break;
case SDLK_0:
//Stop the music
Mix_HaltMusic();
break;
}
}
}
//Clear screen
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( gRenderer );
// Apply the image
SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//
// Update the surface
SDL_UpdateWindowSurface( gWindow );
//Render prompt
// gPromptTexture.render( 0, 0 );
//Update screen
SDL_RenderPresent( gRenderer );
}
}
}
// else
// {
// Load media
// if( !loadMedia() )
// {
// printf( "Failed to load media!\n" );
// }
// else
// {
// Apply the image
// SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
//
// Update the surface
// SDL_UpdateWindowSurface( gWindow );
//
// Hack to get window to stay up
// SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
//
// if (Mix_PlayingMusic() == 0)
// {
// printf("Playing Music...");
//
// gMusic = Mix_LoadMUS("../sillypuppy.ogg");
// Mix_PlayMusic(gMusic, -1);
// }
// if (Mix_PlayingMusic() != 0)
// {
// Mix_HaltMusic();
// }
// }
// }
//Free resources and close SDL
close();
return 0;
}