Пример #1
0
//look for the file in the map
//if not there, load it and insert it into the map
SDL_Surface* ImageCache::getImage(const std::string file)
{
    auto i = images.find(file);

    if(i == images.end())
    {
        try
        {
            SDL_Surface* surf = loadIMG(file);
            i = images.insert(i, std::make_pair(file, surf));
        }
        catch(const char* e)
        {
            printf("%s", e);
            exit(705);
        }
    }

    return i->second;
}
clsScreen::clsScreen() {
    if (Global::Cnfg.getvalues(cnfgShowMap) == 1) { //if not showing the map don't bother trying to load any of the images
                                                    //useful so if show map is disabled you don't need the images folder.
        //Figure out screen size
        if (Global::Cnfg.getvalues(cnfgScreenWidth) == 0) {width = 35*pic_size;}
        else {width = Global::Cnfg.getvalues(cnfgScreenWidth);}
        if (Global::Cnfg.getvalues(cnfgScreenHeight) == 0) {height = DEFINED_MAP_HEIGHT*pic_size;}
        else {height = Global::Cnfg.getvalues(cnfgScreenHeight);}

        //Set all the booleans to false
        blnWindow = false;
        blnRenderer = false;
        blnSky = blnPlayer = blnMonster = blnWall = blnPole = blnCoin = blnErrortex = false;
        bln_SDL_started = false;

        //Start SDL
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            error();
            return;
        } else {
            bln_SDL_started = true;
            if (Global::blnDebugMode) {printf("SDL init successful\n");}
        }

        //Start TTF
        if (TTF_Init() != 0) {
            error();
            return;
        } else {
            if (Global::blnDebugMode) {printf("TTF init successful\n");}
        }

        //Start Image (with only png)
        if (!(IMG_Init( IMG_INIT_PNG )) & IMG_INIT_PNG) {
            error();
            return;
        } else {
            if (Global::blnDebugMode) {printf("IMG init successful\n");}
        }

        win = SDL_CreateWindow("Experimental Platformer AI",100, 100, width, height, SDL_WINDOW_SHOWN);
        if (win == nullptr) {
            printf("SDL Failed to create window.\n");
            error();
            return;
        } else {
            blnWindow = true;
            if (Global::blnDebugMode) {printf("Window creation successful\n");}
        }

        ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
        if (ren == nullptr) {
            printf("SDL Failed to create renderer.\n");
            error();
            return;
        } else {
            blnRenderer = true;
            if (Global::blnDebugMode) {printf("Renderer creation successful\n");}
        }



        errortex = loadERROR();
        if (bln_SDL_started == false) {return;}
        else {
            blnErrortex = true;
            if (Global::blnDebugMode) {printf("Error loading successful\n");}
        }

        std::string path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "sky.png";
        sky = loadIMG(path);
        blnSky = true;
        if (Global::blnDebugMode) {printf("Sky loading successful\n");}

        path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "player.png";
        player = loadIMG(path);
        blnPlayer = true;
        if (Global::blnDebugMode) {printf("Player loading successful\n");}


        path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "wall.png";
        wall = loadIMG(path);
        blnWall = true;
        if (Global::blnDebugMode) {printf("Wall loading successful\n");}


        path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "coin.png";
        coin = loadIMG(path);
        blnCoin = true;
        if (Global::blnDebugMode) {printf("Coin loading successful\n");}

        path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "pole.png";
        pole = loadIMG(path);
        blnPole = true;
        if (Global::blnDebugMode) {printf("Pole loading successful\n");}

        path = DEFINED_DEFAULT_IMAGE_PATH;
        path += "monster.png";
        monster = loadIMG(path);
        blnMonster = true;
        if (Global::blnDebugMode) {printf("Monster loading successful\n");}

        MessageFont = TTF_OpenFont(DEFINED_MESSAGE_FONT,16); //Opens font and sets size
        if (MessageFont == nullptr) {
            printf("Font failed to load, messages will not appear.");
            blnMessageFont = false;
        } else {
            if(Global::blnDebugMode) {printf("Message font created\n");}
            blnMessageFont = true;
        }

        Black = {0, 0, 0, 0}; //Make the color black for fonts
        White = {255, 255, 255, 0}; //Make the color white for fonts

        update();
    } //end if blnShowMap
}