Advertisement
Kondensator

functions.h

Jul 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.06 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "Headers.h"
  4. #include "CharProp.h"
  5. #include "GlobalVar.h"
  6.  
  7. CharProp gracz;
  8.  
  9. SDL_Surface* loadSurface( std::string path )
  10. {
  11.         SDL_Surface* optimizedSurface = NULL;
  12.  
  13.         SDL_Surface* loadedSurface = IMG_Load( path.c_str());
  14.         if( loadedSurface == NULL)
  15.         {
  16.                 std::cout<< " Could not load: " << path.c_str() << " Error: " << SDL_GetError()<< std::endl;
  17.         }
  18.         else
  19.         {
  20.                 optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
  21.                 if ( optimizedSurface == NULL )
  22.                 {
  23.                         std::cout << "Could not optimize image: " << path.c_str() << " Error: " << SDL_GetError() << std::endl;
  24.                 }
  25.                 SDL_FreeSurface( loadedSurface );
  26.         }
  27.         return optimizedSurface;
  28. }
  29.  
  30. SDL_Texture* loadTexture( std::string path )
  31. {
  32.         SDL_Texture* newTexture = NULL;
  33.  
  34.         SDL_Surface* loadedSurface = IMG_Load( path.c_str());
  35.         if( loadedSurface == NULL)
  36.         {
  37.                 std::cout<< " Could not load: " << path.c_str() << " Error: " << SDL_GetError()<< std::endl;
  38.         }
  39.         else
  40.         {
  41.                 newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface);
  42.                 if ( newTexture == NULL )
  43.                 {
  44.                     std::cout << "Could not load Texture image: " << path.c_str() << " Error: " << SDL_GetError() << std::endl;
  45.                 }
  46.                 SDL_FreeSurface( loadedSurface );
  47.         }
  48.         return newTexture;
  49. }
  50.  
  51. bool init()
  52. {
  53.     bool success = true;
  54.  
  55.     if( SDL_Init(SDL_INIT_VIDEO) < 0 )
  56.     {
  57.                 std::cout << "SDL could not initialize Error: " << SDL_GetError() << std::endl;
  58.                 success = false;
  59.     }
  60.     else
  61.     {
  62.  
  63.                 gWindow = SDL_CreateWindow( "Gra" , SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
  64.                 if ( gWindow == NULL )
  65.                 {
  66.                         std::cout << "Could not create window Error: " << SDL_GetError() << std::endl;
  67.                         success = false;
  68.                 }
  69.                 else
  70.                 {
  71.                         gRenderer = SDL_CreateRenderer( gWindow , -1, SDL_RENDERER_ACCELERATED);
  72.                         if ( gRenderer == NULL )
  73.                         {
  74.                                 std::cout<<"Could not create renderer Error: " << SDL_GetError() << std::endl;
  75.                                 success = false;
  76.                         }
  77.                         else
  78.                         {
  79.                                 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
  80.  
  81.                                 int imgFlags = IMG_INIT_PNG;
  82.                                 if( !( IMG_Init( imgFlags ) & imgFlags ) )
  83.                                 {
  84.                                         std::cout << " IMG could not initialize: " << IMG_GetError();
  85.                                         success = false;
  86.                                 }
  87.                                 else
  88.                                 {
  89.                                         gScreenSurface = SDL_GetWindowSurface( gWindow );
  90.                                 }
  91.                         }
  92.                 }
  93.     }
  94.     return success;
  95. }
  96.  
  97. bool loadMedia()
  98. {
  99.         bool success = true;
  100.  
  101.         gTextureWoodfloor = loadTexture( "data/Textures/woodfloor.png");
  102.         if ( gTextureWoodfloor == NULL )
  103.         {
  104.                 std::cout << "failed loading woodfloor.png\n";
  105.                 success = false;
  106.         }
  107.  
  108.         gracz.tex = loadTexture( "data/Textures/glowa.png" );
  109.         if ( gracz.tex == NULL )
  110.         {
  111.                 std::cout << "failed loading glowa.png\n";
  112.                 success = false;
  113.         }
  114.  
  115.         Icon = loadSurface( "ikona.ico");
  116.         if ( Icon == NULL )
  117.         {
  118.                 std::cout << "failed loading ikona.ico\n";
  119.                 success = false;
  120.         }
  121.  
  122.         return success;
  123. }
  124.  
  125. void render( SDL_Texture* RenderSrc, int x, int y, int h, int w, SDL_Renderer* RenderDst = gRenderer)
  126. {
  127.         SDL_Rect RendRect;
  128.         RendRect.x = x;
  129.         RendRect.y = y;
  130.         RendRect.w = w;
  131.         RendRect.h = h;
  132.  
  133.         SDL_RenderCopy( RenderDst, RenderSrc, NULL, &RendRect);
  134. }
  135.  
  136. void renderAll()
  137. {
  138.         SDL_RenderClear( gRenderer );
  139.         for( int y = 0 ; y <= SCREEN_HEIGHT ; y+=256)
  140.         {
  141.                 for( int x = 0 ; x <= SCREEN_WIDTH ; x+=256)
  142.                 {
  143.                         render( gTextureWoodfloor,x,y,256,256 );
  144.                 }
  145.         }
  146.         render( gracz.tex, gracz.x, gracz.y, gracz.h, gracz.w );
  147.         SDL_RenderPresent(gRenderer);
  148. }
  149.  
  150. void close()
  151. {
  152.         SDL_DestroyTexture( gTextureWoodfloor );
  153.         gTextureWoodfloor = NULL;
  154.         SDL_DestroyTexture( gracz.tex );
  155.         gracz.tex = NULL;
  156.  
  157.         SDL_DestroyRenderer( gRenderer );
  158.         SDL_DestroyWindow( gWindow );
  159.         gWindow = NULL;
  160.         gRenderer = NULL;
  161.  
  162.         IMG_Quit();
  163.         SDL_Quit();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement