init commit
BIN
lazyfoodSDL/01_hello_SDL/01_hello_SDL
Executable file
56
lazyfoodSDL/01_hello_SDL/01_hello_SDL.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
|
||||
and may not be redistributed without written permission.*/
|
||||
|
||||
//Using SDL and standard IO
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
|
||||
//Screen dimension constants
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
int main( int argc, char* args[] )
|
||||
{
|
||||
//The window we'll be rendering to
|
||||
SDL_Window* window = NULL;
|
||||
|
||||
//The surface contained by the window
|
||||
SDL_Surface* screenSurface = NULL;
|
||||
|
||||
//Initialize SDL
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
||||
{
|
||||
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
|
||||
}
|
||||
else
|
||||
{
|
||||
//Create window
|
||||
window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
||||
if( window == NULL )
|
||||
{
|
||||
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
||||
}
|
||||
else
|
||||
{
|
||||
//Get window surface
|
||||
screenSurface = SDL_GetWindowSurface( window );
|
||||
|
||||
//Fill the surface white
|
||||
SDL_FillRect( screenSurface, NULL, SDL_MapRGB( screenSurface->format, 0xFF, 0xFF, 0xFF ) );
|
||||
|
||||
//Update the surface
|
||||
SDL_UpdateWindowSurface( window );
|
||||
|
||||
//Wait two seconds
|
||||
SDL_Delay( 2000 );
|
||||
}
|
||||
}
|
||||
|
||||
//Destroy window
|
||||
SDL_DestroyWindow( window );
|
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
BIN
lazyfoodSDL/01_hello_SDL/01_hello_SDL.zip
Normal file
20
lazyfoodSDL/01_hello_SDL/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = 01_hello_SDL.cpp
|
||||
|
||||
#CC specifies which compiler to use
|
||||
CC = clang
|
||||
|
||||
#COMPILER_FLAGS specifies the additional compilation options we're using
|
||||
# -w suppress all warnings
|
||||
COMPILER_FLAGS = -w
|
||||
|
||||
#LINKER_FLAGS specifies the libraries we're linking against
|
||||
LINKER_FLAGS = -lSDL2
|
||||
|
||||
#OBJ_NAME specifies the name of our executable
|
||||
OBJ_NAME= 01_hello_SDL
|
||||
|
||||
#This is the target that compiles our executable
|
||||
all : $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
|
117
lazyfoodSDL/02_image/02_image.cpp
Normal file
@ -0,0 +1,117 @@
|
||||
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
|
||||
and may not be redistributed without written permission.*/
|
||||
|
||||
//Using SDL and standard IO
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
|
||||
//Screen dimensions
|
||||
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;
|
||||
|
||||
//The image we will load and show on the screen
|
||||
SDL_Surface* gHelloWorld = NULL;
|
||||
|
||||
bool init(){
|
||||
//Initialization flag
|
||||
bool success = true;
|
||||
|
||||
//Initialize SDL
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
||||
{
|
||||
printf("SDL could not init! 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
|
||||
{
|
||||
//Get window surface
|
||||
gScreenSurface = SDL_GetWindowSurface( gWindow );
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool loadMedia()
|
||||
{
|
||||
//Loading success flag
|
||||
bool success = true;
|
||||
|
||||
//Load splash image
|
||||
gHelloWorld = SDL_LoadBMP( "images/hello_world.bmp" );
|
||||
if( gHelloWorld == NULL )
|
||||
{
|
||||
printf( "Unable to load image %s! SDL Error: %s\n", "images/hello_world.bmp", SDL_GetError() );
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
//Deallocate surface
|
||||
SDL_FreeSurface( gHelloWorld );
|
||||
gHelloWorld = NULL;
|
||||
|
||||
//Destroy window
|
||||
SDL_DestroyWindow( gWindow );
|
||||
gWindow = NULL;
|
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int main( int argc, char* args[] )
|
||||
{
|
||||
//Start up SDL and create window
|
||||
if( !init() )
|
||||
{
|
||||
printf( "Failed to init!\n" );
|
||||
}
|
||||
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 );
|
||||
|
||||
//Wait two seconds
|
||||
SDL_Delay( 2000 );
|
||||
}
|
||||
}
|
||||
//Free resources and close SDL
|
||||
close();
|
||||
|
||||
return 0;
|
||||
}
|
20
lazyfoodSDL/02_image/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = 02_image.cpp
|
||||
|
||||
#CC specifies which compiler to use
|
||||
CC = clang
|
||||
|
||||
#COMPILER_FLAGS specifies the additional compilation options we're using
|
||||
# -w suppress all warnings
|
||||
COMPILER_FLAGS = -w
|
||||
|
||||
#LINKER_FLAGS specifies the libraries we're linking against
|
||||
LINKER_FLAGS = -lSDL2
|
||||
|
||||
#OBJ_NAME specifies the name of our executable
|
||||
OBJ_NAME= 02_image
|
||||
|
||||
#This is the target that compiles our executable
|
||||
all : $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
|
BIN
lazyfoodSDL/02_image/images/hello_world.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/04_keepress
Executable file
20
lazyfoodSDL/04_keypress/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
#OBJS specifies which files to compile as part of the project
|
||||
OBJS = test.cpp
|
||||
|
||||
#CC specifies which compiler to use
|
||||
CC = g++
|
||||
|
||||
#COMPILER_FLAGS specifies the additional compilation options we're using
|
||||
# -w suppress all warnings
|
||||
COMPILER_FLAGS = -Wall
|
||||
|
||||
#LINKER_FLAGS specifies the libraries we're linking against
|
||||
LINKER_FLAGS = -lSDL2
|
||||
|
||||
#OBJ_NAME specifies the name of our executable
|
||||
OBJ_NAME= 04_keepress
|
||||
|
||||
#This is the target that compiles our executable
|
||||
all : $(OBJS)
|
||||
$(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME)
|
||||
|
BIN
lazyfoodSDL/04_keypress/images/down.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/hello_world.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/left.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/press.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/right.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/up.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
BIN
lazyfoodSDL/04_keypress/images/x.bmp
Executable file
After Width: | Height: | Size: 900 KiB |
230
lazyfoodSDL/04_keypress/test.cpp
Normal file
@ -0,0 +1,230 @@
|
||||
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
|
||||
and may not be redistributed without written permission.*/
|
||||
|
||||
//Using SDL and standard IO
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
//Screen dimensions
|
||||
const int SCREEN_WIDTH = 640;
|
||||
const int SCREEN_HEIGHT = 480;
|
||||
|
||||
//Key press surfaces constants
|
||||
//enum makes everything a const int
|
||||
//and sets it to numbers sequentaly starting with 0
|
||||
enum KeyPressSurfaces
|
||||
{
|
||||
KEY_PRESS_SURFACE_DEFAULT,
|
||||
KEY_PRESS_SURFACE_UP,
|
||||
KEY_PRESS_SURFACE_DOWN,
|
||||
KEY_PRESS_SURFACE_LEFT,
|
||||
KEY_PRESS_SURFACE_RIGHT,
|
||||
KEY_PRESS_SURFACE_TOTAL
|
||||
};
|
||||
|
||||
//Starts up SDL and creates window
|
||||
bool init();
|
||||
|
||||
//Loads media
|
||||
bool loadMedia();
|
||||
|
||||
//Frees media and shuts down SDL
|
||||
void close();
|
||||
|
||||
//Loads individual image
|
||||
SDL_Surface* loadSurface( std::string path );
|
||||
|
||||
//The window we'll be rendering to
|
||||
SDL_Window* gWindow = NULL;
|
||||
|
||||
//The surface contained by the window
|
||||
SDL_Surface* gScreenSurface = NULL;
|
||||
|
||||
//The images that correspond to a keypress
|
||||
SDL_Surface* gKeyPressSurfaces[ KEY_PRESS_SURFACE_TOTAL ];
|
||||
|
||||
//Current displayed image
|
||||
SDL_Surface* gCurrentSurface = NULL;
|
||||
|
||||
SDL_Surface* loadSurface( std::string path )
|
||||
{
|
||||
//Load image at specified path
|
||||
SDL_Surface* loadedSurface = SDL_LoadBMP( path.c_str() );
|
||||
if( loadedSurface == NULL )
|
||||
{
|
||||
printf( "Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
|
||||
}
|
||||
|
||||
return loadedSurface;
|
||||
}
|
||||
|
||||
bool init(){
|
||||
//Initialization flag
|
||||
bool success = true;
|
||||
|
||||
//Initialize SDL
|
||||
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
|
||||
{
|
||||
printf("SDL could not init! 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
|
||||
{
|
||||
//Get window surface
|
||||
gScreenSurface = SDL_GetWindowSurface( gWindow );
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
|
||||
bool loadMedia()
|
||||
{
|
||||
//Loading success flag
|
||||
bool success = true;
|
||||
|
||||
//Load default surface
|
||||
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] = SDL_LoadBMP( "images/press.bmp" );
|
||||
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ] == NULL )
|
||||
{
|
||||
printf( "Failed to load default image!\n" );
|
||||
success = false;
|
||||
}
|
||||
|
||||
gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP] = SDL_LoadBMP( "images/up.bmp" );
|
||||
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ] == NULL )
|
||||
{
|
||||
printf( "Failed to load up image!\n" );
|
||||
success = false;
|
||||
}
|
||||
|
||||
gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN] = loadSurface( "images/down.bmp" );
|
||||
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ] == NULL )
|
||||
{
|
||||
printf( "Failed to load down image!\n" );
|
||||
success = false;
|
||||
}
|
||||
|
||||
gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT] = loadSurface( "images/left.bmp" );
|
||||
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ] == NULL )
|
||||
{
|
||||
printf( "Failed to load left image!\n" );
|
||||
success = false;
|
||||
}
|
||||
|
||||
gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT] = loadSurface( "images/right.bmp" );
|
||||
if( gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ] == NULL )
|
||||
{
|
||||
printf( "Failed to load right image!\n" );
|
||||
success = false;
|
||||
}
|
||||
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
void close()
|
||||
{
|
||||
//Deallocate surface
|
||||
for ( int i=0; i < KEY_PRESS_SURFACE_TOTAL; ++i )
|
||||
{
|
||||
SDL_FreeSurface( gKeyPressSurfaces[ i ] );
|
||||
gKeyPressSurfaces[ i ] = NULL;
|
||||
}
|
||||
|
||||
//Destroy window
|
||||
SDL_DestroyWindow( gWindow );
|
||||
gWindow = NULL;
|
||||
|
||||
//Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
int main( int argc, char* args[] )
|
||||
{
|
||||
//Start up SDL and create window
|
||||
if( !init() )
|
||||
{
|
||||
printf( "Failed to init!\n" );
|
||||
}
|
||||
else
|
||||
{
|
||||
//Load media
|
||||
if( !loadMedia() )
|
||||
{
|
||||
printf( "Failed to load media!\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
//Main loop flag
|
||||
bool quit = false;
|
||||
|
||||
//Event handler
|
||||
SDL_Event e;
|
||||
|
||||
//Set default current surface
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
|
||||
|
||||
//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;
|
||||
}
|
||||
//User presses a key
|
||||
else if( e.type == SDL_KEYDOWN )
|
||||
{
|
||||
//Select surfaces based on key press
|
||||
switch( e.key.keysym.sym )
|
||||
{
|
||||
case SDLK_UP:
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_UP ];
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DOWN ];
|
||||
break;
|
||||
|
||||
case SDLK_LEFT:
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_LEFT ];
|
||||
break;
|
||||
|
||||
case SDLK_RIGHT:
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_RIGHT ];
|
||||
break;
|
||||
|
||||
default:
|
||||
gCurrentSurface = gKeyPressSurfaces[ KEY_PRESS_SURFACE_DEFAULT ];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Apply the current image
|
||||
SDL_BlitSurface( gCurrentSurface, NULL, gScreenSurface, NULL );
|
||||
|
||||
//Update the surface
|
||||
SDL_UpdateWindowSurface( gWindow );
|
||||
}
|
||||
}
|
||||
}
|
||||
//Free resources and close SDL
|
||||
close();
|
||||
|
||||
return 0;
|
||||
}
|