Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include "SDL_ttf.h"
- int main( int argc, char* args[] )
- {
- //Start SDL
- SDL_Init( SDL_INIT_EVERYTHING );
- TTF_Init();
- //Font stuff
- TTF_Font* font = TTF_OpenFont("Arial.ttf", 32);
- //Creates game screen and image object
- SDL_Surface* screen, *image, *text;
- screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
- //Creates game loop
- bool running = true;
- //Creates text
- SDL_Color color3 = {0xff, 0xff, 0xff};
- text = TTF_RenderText_Blended(font, "Hello World!", color3);
- //FPS variables
- const int FPS = 100;
- Uint32 start;
- //Direction variable
- bool direction[4] = {0, 0, 0, 0};
- //Rectangle structure
- SDL_Rect rect;
- Uint32 colour = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
- Uint32 colour2 = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
- rect.x = 10;
- rect.y = 10;
- rect.w = 20;
- rect.h = 20;
- image = SDL_DisplayFormat(SDL_LoadBMP("170px-Stick_Figure.svg.bmp"));
- SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
- while(running)
- {
- start = SDL_GetTicks();
- //Events
- SDL_Event event;
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT :
- running = false;
- break;
- case SDL_KEYDOWN :
- switch(event.key.keysym.sym)
- {
- case SDLK_UP :
- direction[0] = 1;
- break;
- case SDLK_LEFT :
- direction[1] = 1;
- break;
- case SDLK_DOWN :
- direction[2] = 1;
- break;
- case SDLK_RIGHT :
- direction[3] = 1;
- break;
- }
- break;
- case SDL_KEYUP :
- switch(event.key.keysym.sym)
- {
- case SDLK_UP :
- direction[0] = 0;
- break;
- case SDLK_LEFT :
- direction[1] = 0;
- break;
- case SDLK_DOWN :
- direction[2] = 0;
- break;
- case SDLK_RIGHT :
- direction[3] = 0;
- break;
- }
- break;
- }
- }
- if(direction[0])
- rect.y--;
- if(direction[1])
- rect.x--;
- if(direction[2])
- rect.y++;
- if(direction[3])
- rect.x++;
- //Draws rectangle
- //SDL_FillRect(screen, &screen->clip_rect, colour2);
- //SDL_FillRect(screen, &rect, colour);
- SDL_BlitSurface(image, NULL, screen, NULL);
- //SDL_BlitSurface(text, NULL, screen, NULL);
- //Writes to screen
- SDL_Flip(screen);
- //Controls FPS
- if ((1000/FPS) > (SDL_GetTicks() - start))
- SDL_Delay((1000/FPS) - (SDL_GetTicks() - start));
- }
- //Quit SDL
- SDL_FreeSurface(text);
- TTF_CloseFont(font);
- SDL_FreeSurface(image);
- TTF_Quit();
- SDL_Quit();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement