Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.26 KB | None | 0 0
  1. #include "SDL2/SDL.h"
  2. #include "SDL2/SDL_image.h"
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #define SCREEN_WIDTH 640
  6. #define SCREEN_HEIGHT 480
  7.  
  8. bool init();
  9. bool loadMedia();
  10. void end();
  11. SDL_Window *gWindow = NULL;
  12. SDL_Renderer *gRenderer = NULL;
  13.  
  14. typedef struct lTexture
  15. {
  16.   //The actual hardware texture
  17.   SDL_Texture *mTexture;
  18.   //Image dimensions
  19.   int mWidth;
  20.   int mHeight;
  21. } lTexture;
  22. lTexture gFooTexture;
  23. lTexture gBackgroundTexture;
  24.  
  25. void AllocTexture(lTexture *texture)
  26. {
  27.   //Initialize
  28.   texture->mTexture = NULL;
  29.   texture->mWidth = 0;
  30.   texture->mHeight = 0;
  31. }
  32.  
  33. void DeallocTexture(lTexture *texture)
  34. {
  35.   //Deallocate
  36.   if (texture->mTexture != NULL)
  37.   {
  38.     SDL_DestroyTexture(texture->mTexture);
  39.     texture->mTexture = NULL;
  40.     texture->mWidth = 0;
  41.     texture->mHeight = 0;
  42.   }
  43. }
  44. void render(int x, int y, lTexture *texture)
  45. {
  46.   //Set render space and render to screen
  47.   SDL_Rect renderQuad = {x, y, texture->mWidth, texture->mHeight};
  48.   SDL_RenderCopy(gRenderer, texture->mTexture, NULL, &renderQuad);
  49. }
  50.  
  51. bool loadFromFile(const char *path, lTexture *texture)
  52. {
  53.   //Get rid of preexisting texture
  54.   DeallocTexture(texture);
  55.   //The final texture
  56.   SDL_Texture *newTexture = NULL;
  57.   //Load image at specified path
  58.   SDL_Surface *loadedSurface = IMG_Load(path);
  59.   if(loadedSurface == NULL)
  60.   {
  61.     printf("Unable to load image %s! SDL_image error: %s\n", path, IMG_GetError());
  62.   }
  63.   else
  64.   {
  65.     //Color key image
  66.     SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format,
  67.                             0x41, 0x80, 0xA5));
  68.     //Create texture from surface pixels
  69.     newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface);
  70.     if(newTexture == NULL)
  71.     {
  72.       printf("Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError());
  73.     }
  74.     else
  75.     {
  76.       //Get image dimensions
  77.       texture->mWidth = loadedSurface->w;
  78.       texture->mHeight = loadedSurface->h;
  79.     }
  80.     //Get rid of old loaded surface
  81.     SDL_FreeSurface(loadedSurface);
  82.   }
  83.   //Return success
  84.   texture->mTexture = newTexture;
  85.   return texture->mTexture != NULL;
  86. }
  87.  
  88. int getWidth(lTexture *texture)
  89. {
  90.   return texture->mWidth;
  91. }
  92.  
  93. int getHeight(lTexture *texture)
  94. {
  95.   return texture->mHeight;
  96. }
  97.  
  98. bool init()
  99. {
  100.   bool success = true;
  101.  
  102.   if(SDL_Init(SDL_INIT_VIDEO) < 0)
  103.   {
  104.     printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
  105.     success = false;
  106.   }
  107.   else
  108.   {
  109.     //Set texture filtering to linear
  110.     if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
  111.     {
  112.       printf("Warning: Linear texture filtering not enabled!\n");
  113.     }
  114.     //Create window
  115.     gWindow = SDL_CreateWindow("SDL Tutorial",
  116.                    SDL_WINDOWPOS_UNDEFINED,
  117.                    SDL_WINDOWPOS_UNDEFINED,
  118.                    SCREEN_WIDTH,
  119.                    SCREEN_HEIGHT,
  120.                    SDL_WINDOW_SHOWN);
  121.     if(gWindow == NULL)
  122.     {
  123.       printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
  124.       success = false;
  125.     }
  126.     else
  127.     {
  128.       //Create renderer for window
  129.       gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED);
  130.       if(gRenderer == NULL)
  131.       {
  132.     printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
  133.     success = false;
  134.       }
  135.       else
  136.       {
  137.     //Initialize renderer color
  138.     SDL_SetRenderDrawColor(gRenderer,0xFF, 0xFF, 0xFF, 0xFF);
  139.     //Initialize PNG Loading
  140.     int imgFlags = IMG_INIT_PNG;
  141.     if(!(IMG_Init(imgFlags) &imgFlags))
  142.     {
  143.       printf("SDL_image could not initialize! SDL_image Error: %s\n",IMG_GetError());
  144.       success = false;
  145.     }
  146.       }
  147.     }
  148.   }
  149.   return success;
  150. }
  151.  
  152. bool loadMedia()
  153. {
  154.   //Loading success flag
  155.   bool success = true;
  156.   //Load foo texture
  157.   if(!loadFromFile("surfaces/ritsu.png", &gFooTexture))
  158.   {
  159.     printf("Failed to load Foo texture image!\n");
  160.     success = false;
  161.   }
  162.   //Load background texture
  163.   if(!loadFromFile("surfaces/background.png", &gBackgroundTexture))
  164.   {
  165.     printf("Failed to load background texture image!\n");
  166.     success = false;
  167.   }
  168.   return success;
  169. }
  170.  
  171. void end()
  172. {
  173.   //Free loaded images
  174.   DeallocTexture(&gFooTexture);
  175.   DeallocTexture(&gBackgroundTexture);
  176.   //Destroy window
  177.   SDL_DestroyRenderer(gRenderer);
  178.   SDL_DestroyWindow(gWindow);
  179.   gWindow = NULL;
  180.   gRenderer = NULL;
  181.   //Quit SDL Subsystems
  182.   IMG_Quit();
  183.   SDL_Quit();
  184. }
  185.  
  186. int main(int argc, char *args[])
  187. {
  188.   //Start up SDL and create window
  189.   if(!init())
  190.   {
  191.     printf("Failed to initialize!\n");
  192.   }
  193.   else
  194.   {
  195.     //Load media
  196.     if(!loadMedia())
  197.     {
  198.       printf("Failed to load media!\n");
  199.     }
  200.     else
  201.     {
  202.       //Main loop flag
  203.       bool quit = false;
  204.       //Event handler
  205.       SDL_Event e;
  206.       //While application is running
  207.       while(!quit)
  208.       {
  209.     //Handle events on queue
  210.     while(SDL_PollEvent(&e) != 0)
  211.     {
  212.       //User requests quit
  213.       if(e.type == SDL_QUIT)
  214.       {
  215.         quit = true;
  216.       }
  217.     }
  218.     //Clear screen
  219.     SDL_SetRenderDrawColor(gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  220.     SDL_RenderClear(gRenderer);
  221.     //Render background texture to screen
  222.     render(0, 0, &gBackgroundTexture);
  223.     //Render foo to the screen
  224.     render(240, 190, &gFooTexture);
  225.     //Update screen
  226.     SDL_RenderPresent(gRenderer);
  227.       }
  228.     }
  229.   }
  230.   //Free resources and close SDL
  231.   end();
  232.   return 0;
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement