显示表面图像+纹理?

时间:2015-12-15 03:07:52

标签: c++ sdl

我似乎无法显示一个表面,并在其上渲染了一个小纹理。我试图使用纹理创建一个命中点系统,我的背景是一个加载图像的表面。

是否可以显示曲面并在曲面上叠加纹理?

#include<SDL.h>
#include<iostream>
#undef main

using namespace std;

SDL_Window *window = nullptr;
SDL_Surface *windowSurface = nullptr;
SDL_Surface *menu_bg = nullptr;
SDL_Surface *battle1_bg = nullptr;
SDL_Surface *currentImage = nullptr;
SDL_Surface *hitbar = nullptr;
SDL_Surface *surfaceLoad = nullptr;
SDL_Renderer *Renderer = nullptr;
SDL_Texture *hpTexture = nullptr;


void init()
{
    //SDL Initialize
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        cout << "Video Initialization Error: " << SDL_GetError() << endl;

    Renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    //Window Initialize
    window = SDL_CreateWindow("Orc Fighter", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    windowSurface = SDL_GetWindowSurface(window);
    if (window == NULL)
    {
        cout << "Window Initialization Error : " << SDL_GetError() << endl;
        exit(1);
    }
}

void quit()
{
    SDL_DestroyRenderer(Renderer);
    SDL_FreeSurface(menu_bg);
    menu_bg = nullptr;
    SDL_FreeSurface(battle1_bg);
    battle1_bg = nullptr;
    SDL_DestroyWindow(window);
    window = nullptr;
    windowSurface = nullptr;
    SDL_Quit();
}

void RenderHPBar(int x, int y, int w, int h, float Percent, SDL_Color FGColor, SDL_Color BGColor) {
    Percent = Percent > 1.f ? 1.f : Percent < 0.f ? 0.f : Percent;
    Renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    SDL_Color old;
    SDL_GetRenderDrawColor(Renderer, &old.r, &old.g, &old.g, &old.a);
    SDL_Rect bgrect = { x, y, w, h };
    SDL_SetRenderDrawColor(Renderer, BGColor.r, BGColor.g, BGColor.b, BGColor.a);
    SDL_RenderFillRect(Renderer, &bgrect);
    SDL_SetRenderDrawColor(Renderer, FGColor.r, FGColor.g, FGColor.b, FGColor.a);
    int pw = (int)((float)w * Percent);
    int px = x + (w - pw);
    SDL_Rect fgrect = { px, y, pw, h };
    SDL_RenderFillRect(Renderer, &fgrect);
    SDL_SetRenderDrawColor(Renderer, old.r, old.g, old.b, old.a);
}

SDL_Color color(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
    SDL_Color col = { r,g,b,a };
    return col;
}

void playgame()
{
    currentImage = battle1_bg;

    //HEALTHBAR RENDER
        surfaceLoad = SDL_LoadBMP("healthtexture.bmp");
        hpTexture = SDL_CreateTextureFromSurface(Renderer, surfaceLoad);
        SDL_FreeSurface(surfaceLoad);

            int healthval = 100;
            SDL_Color FGColor = color(191, 0, 0, 0);
            SDL_Color BGColor = color(0, 0, 0, 0);
            RenderHPBar(82, 390, healthval * 139 / 100, 3, 1, FGColor, BGColor);
            RenderHPBar(446, 390, healthval * 139 / 100, 3, 1, FGColor, BGColor);
            SDL_RenderCopy(Renderer, hpTexture, NULL, NULL);
            SDL_RenderPresent(Renderer);
}

int main(int argc, char *argv[])
{
    init();

    int input = 0;

        menu_bg = SDL_LoadBMP("orcfightermenu.bmp");
        battle1_bg = SDL_LoadBMP("battlescene1.bmp");
        currentImage = menu_bg;

        SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
        SDL_UpdateWindowSurface(window);

        bool isRunning = true;
        SDL_Event ev;


        while (isRunning)
        {
            while (SDL_PollEvent(&ev) != 0) //EXIT STATEMENT
            {
                if (ev.type == SDL_QUIT)
                    isRunning = false;
                else if (ev.type == SDL_MOUSEBUTTONDOWN)
                {   // MENU BUTTONS
                    if (input == 0 && (ev.button.button == SDL_BUTTON_LEFT) && ((ev.button.x >= 272 && ev.button.x <= 351 ) && (ev.button.y >= 256 && ev.button.y <= 279)))
                        input += 1;
                    if (input == 0 && (ev.button.button == SDL_BUTTON_LEFT) && ((ev.button.x >= 289 && ev.button.x <= 339) && (ev.button.y >= 332 && ev.button.y <= 355)))
                        input += 2;

                    switch (input)
                    {
                    case 1:
                        playgame();
                        break;


                    case 2:
                        quit();
                        isRunning = false;
                        break;
                    }

                    // Drawing current image to window
                    SDL_BlitSurface(currentImage, NULL, windowSurface, NULL);
                    SDL_UpdateWindowSurface(window);
                }
                        //if (ev.type == SDL_MOUSEBUTTONDOWN)
                        //{
                        //  if ((ev.button.button == SDL_BUTTON_LEFT) && ((ev.button.x >= 272 && ev.button.x <= 351) && (ev.button.y >= 256 && ev.button.y <= 279)))
                        //      //class attack
                        //  if ((ev.button.button == SDL_BUTTON_LEFT) && ((ev.button.x >= 272 && ev.button.x <= 351) && (ev.button.y >= 256 && ev.button.y <= 279)))
                        //      //class heal

                        //}


            }
        }
                //Freeing memory
            quit();
            return 0;
}

0 个答案:

没有答案