| 1 | /*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
|
|---|
| 2 | be redestributed without written permission.*/
|
|---|
| 3 |
|
|---|
| 4 | //The headers
|
|---|
| 5 | #include "SDL/SDL.h"
|
|---|
| 6 | #include "SDL/SDL_image.h"
|
|---|
| 7 | #include "SDL/SDL_ttf.h"
|
|---|
| 8 | #include <string>
|
|---|
| 9 | #include <sstream>
|
|---|
| 10 |
|
|---|
| 11 | //Screen attributes
|
|---|
| 12 | const int SCREEN_WIDTH = 640;
|
|---|
| 13 | const int SCREEN_HEIGHT = 480;
|
|---|
| 14 | const int SCREEN_BPP = 32;
|
|---|
| 15 |
|
|---|
| 16 | //The surfaces
|
|---|
| 17 | SDL_Surface *background = NULL;
|
|---|
| 18 | SDL_Surface *startStop = NULL;
|
|---|
| 19 | SDL_Surface *seconds = NULL;
|
|---|
| 20 | SDL_Surface *screen = NULL;
|
|---|
| 21 | SDL_Surface *begin = NULL;
|
|---|
| 22 | SDL_Surface *current = NULL;
|
|---|
| 23 |
|
|---|
| 24 | //The event structure
|
|---|
| 25 | SDL_Event event;
|
|---|
| 26 |
|
|---|
| 27 | //The font
|
|---|
| 28 | TTF_Font *font = NULL;
|
|---|
| 29 |
|
|---|
| 30 | //The color of the font
|
|---|
| 31 | SDL_Color textColor = { 0xF0, 0xFF, 0xF0 };
|
|---|
| 32 |
|
|---|
| 33 | SDL_Surface *load_image( std::string filename )
|
|---|
| 34 | {
|
|---|
| 35 | //The image that's loaded
|
|---|
| 36 | SDL_Surface* loadedImage = NULL;
|
|---|
| 37 |
|
|---|
| 38 | //The optimized surface that will be used
|
|---|
| 39 | SDL_Surface* optimizedImage = NULL;
|
|---|
| 40 |
|
|---|
| 41 | //Load the image
|
|---|
| 42 | loadedImage = IMG_Load( filename.c_str() );
|
|---|
| 43 |
|
|---|
| 44 | //If the image loaded
|
|---|
| 45 | if( loadedImage != NULL )
|
|---|
| 46 | {
|
|---|
| 47 | //Create an optimized surface
|
|---|
| 48 | optimizedImage = SDL_DisplayFormat( loadedImage );
|
|---|
| 49 |
|
|---|
| 50 | //Free the old surface
|
|---|
| 51 | SDL_FreeSurface( loadedImage );
|
|---|
| 52 |
|
|---|
| 53 | //If the surface was optimized
|
|---|
| 54 | if( optimizedImage != NULL )
|
|---|
| 55 | {
|
|---|
| 56 | //Color key surface
|
|---|
| 57 | SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | //Return the optimized surface
|
|---|
| 62 | return optimizedImage;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
|
|---|
| 66 | {
|
|---|
| 67 | //Holds offsets
|
|---|
| 68 | SDL_Rect offset;
|
|---|
| 69 |
|
|---|
| 70 | //Get offsets
|
|---|
| 71 | offset.x = x;
|
|---|
| 72 | offset.y = y;
|
|---|
| 73 |
|
|---|
| 74 | //Blit
|
|---|
| 75 | SDL_BlitSurface( source, clip, destination, &offset );
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | bool init()
|
|---|
| 79 | {
|
|---|
| 80 | //Initialize all SDL subsystems
|
|---|
| 81 | if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
|
|---|
| 82 | {
|
|---|
| 83 | return false;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | //Set up the screen
|
|---|
| 87 | screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
|
|---|
| 88 |
|
|---|
| 89 | //If there was an error in setting up the screen
|
|---|
| 90 | if( screen == NULL )
|
|---|
| 91 | {
|
|---|
| 92 | return false;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | //Initialize SDL_ttf
|
|---|
| 96 | if( TTF_Init() == -1 )
|
|---|
| 97 | {
|
|---|
| 98 | return false;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | //Set the window caption
|
|---|
| 102 | SDL_WM_SetCaption( "Timer Test", NULL );
|
|---|
| 103 |
|
|---|
| 104 | //If everything initialized fine
|
|---|
| 105 | return true;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | bool load_files()
|
|---|
| 109 | {
|
|---|
| 110 | //Load the background image
|
|---|
| 111 | background = load_image( "background.png" );
|
|---|
| 112 |
|
|---|
| 113 | //Open the font
|
|---|
| 114 | font = TTF_OpenFont( "lazy.ttf", 36 );
|
|---|
| 115 |
|
|---|
| 116 | //If there was a problem in loading the background
|
|---|
| 117 | if( background == NULL )
|
|---|
| 118 | {
|
|---|
| 119 | return false;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | //If there was an error in loading the font
|
|---|
| 123 | if( font == NULL )
|
|---|
| 124 | {
|
|---|
| 125 | return false;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | //If everything loaded fine
|
|---|
| 129 | return true;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void clean_up()
|
|---|
| 133 | {
|
|---|
| 134 | //Free the surfaces
|
|---|
| 135 | SDL_FreeSurface( background );
|
|---|
| 136 | SDL_FreeSurface( startStop );
|
|---|
| 137 |
|
|---|
| 138 | //Close the font
|
|---|
| 139 | TTF_CloseFont( font );
|
|---|
| 140 |
|
|---|
| 141 | //Quit SDL_ttf
|
|---|
| 142 | TTF_Quit();
|
|---|
| 143 |
|
|---|
| 144 | //Quit SDL
|
|---|
| 145 | SDL_Quit();
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | int main( int argc, char* args[] )
|
|---|
| 149 | {
|
|---|
| 150 | //Quit flag
|
|---|
| 151 | bool quit = false;
|
|---|
| 152 |
|
|---|
| 153 | //The timer starting time
|
|---|
| 154 | Uint32 start = 0;
|
|---|
| 155 |
|
|---|
| 156 | //The timer start/stop flag
|
|---|
| 157 | bool running = true;
|
|---|
| 158 |
|
|---|
| 159 | //Initialize
|
|---|
| 160 | if( init() == false )
|
|---|
| 161 | {
|
|---|
| 162 | return 1;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | //Load the files
|
|---|
| 166 | if( load_files() == false )
|
|---|
| 167 | {
|
|---|
| 168 | return 1;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | //Generate the message surface
|
|---|
| 172 | startStop = TTF_RenderText_Solid( font, "Press S to start or stop the timer", textColor );
|
|---|
| 173 |
|
|---|
| 174 | //Start the timer
|
|---|
| 175 | start = SDL_GetTicks();
|
|---|
| 176 |
|
|---|
| 177 | //While the user hasn't quit
|
|---|
| 178 | while( quit == false )
|
|---|
| 179 | {
|
|---|
| 180 | //While there's an event to handle
|
|---|
| 181 | while( SDL_PollEvent( &event ) )
|
|---|
| 182 | {
|
|---|
| 183 | //If a key was pressed
|
|---|
| 184 | if( event.type == SDL_KEYDOWN )
|
|---|
| 185 | {
|
|---|
| 186 | //If s was pressed
|
|---|
| 187 | if( event.key.keysym.sym == SDLK_s )
|
|---|
| 188 | {
|
|---|
| 189 | //If the timer is running
|
|---|
| 190 | if( running == true )
|
|---|
| 191 | {
|
|---|
| 192 | //Stop the timer
|
|---|
| 193 | running = false;
|
|---|
| 194 | start = 0;
|
|---|
| 195 | }
|
|---|
| 196 | else
|
|---|
| 197 | {
|
|---|
| 198 | //Start the timer
|
|---|
| 199 | running = true;
|
|---|
| 200 | start = SDL_GetTicks();
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | //If the user has Xed out the window
|
|---|
| 206 | else if( event.type == SDL_QUIT )
|
|---|
| 207 | {
|
|---|
| 208 | //Quit the program
|
|---|
| 209 | quit = true;
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | //Apply the background
|
|---|
| 214 | apply_surface( 0, 0, background, screen );
|
|---|
| 215 |
|
|---|
| 216 | //Apply the message
|
|---|
| 217 | apply_surface( ( SCREEN_WIDTH - startStop->w ) / 2, 200, startStop, screen );
|
|---|
| 218 |
|
|---|
| 219 | //If the timer is running
|
|---|
| 220 | if( running == true )
|
|---|
| 221 | {
|
|---|
| 222 | //The timer's time as a string
|
|---|
| 223 | std::stringstream time;
|
|---|
| 224 |
|
|---|
| 225 | std::stringstream st;
|
|---|
| 226 | std::stringstream gt;
|
|---|
| 227 |
|
|---|
| 228 | //Convert the timer's time to a string
|
|---|
| 229 | time << "Timer: " << SDL_GetTicks() - start;
|
|---|
| 230 | st << "start: " << start;
|
|---|
| 231 | gt << "getick: " << SDL_GetTicks();
|
|---|
| 232 |
|
|---|
| 233 | //Render the time surface
|
|---|
| 234 | seconds = TTF_RenderText_Solid( font, time.str().c_str(), textColor );
|
|---|
| 235 | begin = TTF_RenderText_Solid(font, st.str().c_str(), textColor);
|
|---|
| 236 | current = TTF_RenderText_Solid(font, gt.str().c_str(), textColor);
|
|---|
| 237 |
|
|---|
| 238 | //Apply the time surface
|
|---|
| 239 | apply_surface( ( SCREEN_WIDTH - seconds->w ) / 2, 50, seconds, screen );
|
|---|
| 240 | apply_surface((SCREEN_WIDTH-begin->w)/2, 100, begin, screen);
|
|---|
| 241 | apply_surface((SCREEN_WIDTH-current->w)/2, 150, current, screen);
|
|---|
| 242 |
|
|---|
| 243 | //Free the time surface
|
|---|
| 244 | SDL_FreeSurface( seconds );
|
|---|
| 245 | SDL_FreeSurface(begin);
|
|---|
| 246 | SDL_FreeSurface(current);
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | //Update the screen
|
|---|
| 250 | if( SDL_Flip( screen ) == -1 )
|
|---|
| 251 | {
|
|---|
| 252 | return 1;
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | //Clean up
|
|---|
| 257 | clean_up();
|
|---|
| 258 |
|
|---|
| 259 | return 0;
|
|---|
| 260 | }
|
|---|