Advertisement
Guest User

Untitled

a guest
May 8th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include "SDL.h"
  2. #include "SDL_ttf.h"
  3.  
  4. int main( int argc, char* args[] )
  5. {
  6. //Start SDL
  7. SDL_Init( SDL_INIT_EVERYTHING );
  8. TTF_Init();
  9.  
  10. //Font stuff
  11. TTF_Font* font = TTF_OpenFont("Arial.ttf", 32);
  12.  
  13. //Creates game screen and image object
  14. SDL_Surface* screen, *image, *text;
  15. screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  16.  
  17. //Creates game loop
  18. bool running = true;
  19.  
  20. //Creates text
  21. SDL_Color color3 = {0xff, 0xff, 0xff};
  22. text = TTF_RenderText_Blended(font, "Hello World!", color3);
  23.  
  24. //FPS variables
  25. const int FPS = 100;
  26. Uint32 start;
  27.  
  28. //Direction variable
  29. bool direction[4] = {0, 0, 0, 0};
  30.  
  31. //Rectangle structure
  32. SDL_Rect rect;
  33. Uint32 colour = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
  34. Uint32 colour2 = SDL_MapRGB(screen->format, 0xff, 0xff, 0xff);
  35. rect.x = 10;
  36. rect.y = 10;
  37. rect.w = 20;
  38. rect.h = 20;
  39.  
  40. image = SDL_DisplayFormat(SDL_LoadBMP("170px-Stick_Figure.svg.bmp"));
  41. SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
  42.  
  43. while(running)
  44. {
  45. start = SDL_GetTicks();
  46.  
  47. //Events
  48. SDL_Event event;
  49. while(SDL_PollEvent(&event))
  50. {
  51. switch(event.type)
  52. {
  53. case SDL_QUIT :
  54. running = false;
  55. break;
  56.  
  57. case SDL_KEYDOWN :
  58. switch(event.key.keysym.sym)
  59. {
  60. case SDLK_UP :
  61. direction[0] = 1;
  62. break;
  63.  
  64. case SDLK_LEFT :
  65. direction[1] = 1;
  66. break;
  67.  
  68. case SDLK_DOWN :
  69. direction[2] = 1;
  70. break;
  71.  
  72. case SDLK_RIGHT :
  73. direction[3] = 1;
  74. break;
  75. }
  76. break;
  77.  
  78. case SDL_KEYUP :
  79. switch(event.key.keysym.sym)
  80. {
  81. case SDLK_UP :
  82. direction[0] = 0;
  83. break;
  84.  
  85. case SDLK_LEFT :
  86. direction[1] = 0;
  87. break;
  88.  
  89. case SDLK_DOWN :
  90. direction[2] = 0;
  91. break;
  92.  
  93. case SDLK_RIGHT :
  94. direction[3] = 0;
  95. break;
  96. }
  97. break;
  98. }
  99. }
  100.  
  101. if(direction[0])
  102. rect.y--;
  103. if(direction[1])
  104. rect.x--;
  105. if(direction[2])
  106. rect.y++;
  107. if(direction[3])
  108. rect.x++;
  109.  
  110. //Draws rectangle
  111. //SDL_FillRect(screen, &screen->clip_rect, colour2);
  112. //SDL_FillRect(screen, &rect, colour);
  113.  
  114. SDL_BlitSurface(image, NULL, screen, NULL);
  115. //SDL_BlitSurface(text, NULL, screen, NULL);
  116.  
  117. //Writes to screen
  118. SDL_Flip(screen);
  119.  
  120. //Controls FPS
  121. if ((1000/FPS) > (SDL_GetTicks() - start))
  122. SDL_Delay((1000/FPS) - (SDL_GetTicks() - start));
  123. }
  124.  
  125. //Quit SDL
  126. SDL_FreeSurface(text);
  127. TTF_CloseFont(font);
  128. SDL_FreeSurface(image);
  129. TTF_Quit();
  130. SDL_Quit();
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement