Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "Headers.h"
- #include "CharProp.h"
- #include "GlobalVar.h"
- CharProp gracz;
- SDL_Surface* loadSurface( std::string path )
- {
- SDL_Surface* optimizedSurface = NULL;
- SDL_Surface* loadedSurface = IMG_Load( path.c_str());
- if( loadedSurface == NULL)
- {
- std::cout<< " Could not load: " << path.c_str() << " Error: " << SDL_GetError()<< std::endl;
- }
- else
- {
- optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
- if ( optimizedSurface == NULL )
- {
- std::cout << "Could not optimize image: " << path.c_str() << " Error: " << SDL_GetError() << std::endl;
- }
- SDL_FreeSurface( loadedSurface );
- }
- return optimizedSurface;
- }
- SDL_Texture* loadTexture( std::string path )
- {
- SDL_Texture* newTexture = NULL;
- SDL_Surface* loadedSurface = IMG_Load( path.c_str());
- if( loadedSurface == NULL)
- {
- std::cout<< " Could not load: " << path.c_str() << " Error: " << SDL_GetError()<< std::endl;
- }
- else
- {
- newTexture = SDL_CreateTextureFromSurface( gRenderer, loadedSurface);
- if ( newTexture == NULL )
- {
- std::cout << "Could not load Texture image: " << path.c_str() << " Error: " << SDL_GetError() << std::endl;
- }
- SDL_FreeSurface( loadedSurface );
- }
- return newTexture;
- }
- bool init()
- {
- bool success = true;
- if( SDL_Init(SDL_INIT_VIDEO) < 0 )
- {
- std::cout << "SDL could not initialize Error: " << SDL_GetError() << std::endl;
- success = false;
- }
- else
- {
- gWindow = SDL_CreateWindow( "Gra" , SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
- if ( gWindow == NULL )
- {
- std::cout << "Could not create window Error: " << SDL_GetError() << std::endl;
- success = false;
- }
- else
- {
- gRenderer = SDL_CreateRenderer( gWindow , -1, SDL_RENDERER_ACCELERATED);
- if ( gRenderer == NULL )
- {
- std::cout<<"Could not create renderer Error: " << SDL_GetError() << std::endl;
- success = false;
- }
- else
- {
- SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
- int imgFlags = IMG_INIT_PNG;
- if( !( IMG_Init( imgFlags ) & imgFlags ) )
- {
- std::cout << " IMG could not initialize: " << IMG_GetError();
- success = false;
- }
- else
- {
- gScreenSurface = SDL_GetWindowSurface( gWindow );
- }
- }
- }
- }
- return success;
- }
- bool loadMedia()
- {
- bool success = true;
- gTextureWoodfloor = loadTexture( "data/Textures/woodfloor.png");
- if ( gTextureWoodfloor == NULL )
- {
- std::cout << "failed loading woodfloor.png\n";
- success = false;
- }
- gracz.tex = loadTexture( "data/Textures/glowa.png" );
- if ( gracz.tex == NULL )
- {
- std::cout << "failed loading glowa.png\n";
- success = false;
- }
- Icon = loadSurface( "ikona.ico");
- if ( Icon == NULL )
- {
- std::cout << "failed loading ikona.ico\n";
- success = false;
- }
- return success;
- }
- void render( SDL_Texture* RenderSrc, int x, int y, int h, int w, SDL_Renderer* RenderDst = gRenderer)
- {
- SDL_Rect RendRect;
- RendRect.x = x;
- RendRect.y = y;
- RendRect.w = w;
- RendRect.h = h;
- SDL_RenderCopy( RenderDst, RenderSrc, NULL, &RendRect);
- }
- void renderAll()
- {
- SDL_RenderClear( gRenderer );
- for( int y = 0 ; y <= SCREEN_HEIGHT ; y+=256)
- {
- for( int x = 0 ; x <= SCREEN_WIDTH ; x+=256)
- {
- render( gTextureWoodfloor,x,y,256,256 );
- }
- }
- render( gracz.tex, gracz.x, gracz.y, gracz.h, gracz.w );
- SDL_RenderPresent(gRenderer);
- }
- void close()
- {
- SDL_DestroyTexture( gTextureWoodfloor );
- gTextureWoodfloor = NULL;
- SDL_DestroyTexture( gracz.tex );
- gracz.tex = NULL;
- SDL_DestroyRenderer( gRenderer );
- SDL_DestroyWindow( gWindow );
- gWindow = NULL;
- gRenderer = NULL;
- IMG_Quit();
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement