SDL按钮未显示

时间:2016-09-05 21:54:28

标签: c++ sdl

最近我一直在尝试使用lazyfoo.net教程创建一个按钮。他们编码的按钮示例并不适合我需要的(特别是因为我修改了LTexture类)所以我修改了它......当然,它没有用。

所以这里是修改后的LTexture类(现在称为纹理):

#pragma once
//Using SDL, SDL_image, standard IO, and strings
#include <xstring>
#include <SDL.h> //SDL header file
#include <SDL_image.h> //SDL image file
#include <stdio.h> //standard C ouput
#include <string> //standard c++ string
#include <map>
#include <SDL_ttf.h>
using namespace std;
typedef Uint8 u8;
//Texture wrapper class (originally from lazyfoo.net)
class Texture
{
public:
    //Initializes variables
    Texture();

    //Deallocates memory
    ~Texture();

    //Loads image at specified path
    bool loadFromFile(std::string path);

    //Deallocates texture
    void free();

    //Renders texture at given point
    void render();
    //Sets the size of the image
    void setSize(int width, int height);
    //Adds a clip to the clips map
    void addClip(string name, SDL_Rect* aclip);
    //Sets the clip
    void setClip(string name);
    void setNullClip();
    //Sets the placement of the object
    void setPos(int newx, int newy);
    //Moves the position of the object
    void movePos(int addx, int addy);
    //Gets image dimensions
    int getWidth();
    int getHeight();
    //Sets the color
    void setColor(Uint8 red, u8 green, u8 blue);
    //Set blending
    void setBlendMode(SDL_BlendMode blending);

    //Set alpha modulation
    void setAlpha(Uint8 alpha);
    //Creates image from font string
    bool loadFromRenderedText(std::string textureText, SDL_Color textColor);

protected:
    //The actual hardware texture
    SDL_Texture* mTexture;

    //Image dimensions
    int mWidth;
    int mHeight;
    //Image bottom left coordinates
    int x;
    int y;
    //Current clip of image
    SDL_Rect* clip;
    //Available image clips
    map<string, SDL_Rect*> clips;
};

功能定义:

#include "Texture.h"
#include "Univar.h"
extern SDL_Renderer* Renderer;
extern TTF_Font* Font;
Texture::Texture()
{
    //Initialize
    mTexture = NULL;
    mWidth = 0;
    mHeight = 0;
}

Texture::~Texture()
{
    //Deallocate
    free();
}

bool Texture::loadFromFile(std::string path)
{
    //Get rid of preexisting texture
    free();

    //The final texture
    SDL_Texture* newTexture = NULL;

    //Load image at specified path
    SDL_Surface* loadedSurface = IMG_Load(path.c_str());
    if (loadedSurface == NULL)
    {
        printf("Unable to load image %s! SDL_image Error: %s\n", path.c_str(), IMG_GetError());
    }
    else
    {
        //Color key image
        SDL_SetColorKey(loadedSurface, SDL_TRUE, SDL_MapRGB(loadedSurface->format, 0, 0xFF, 0xFF));

        //Create texture from surface pixels
        newTexture = SDL_CreateTextureFromSurface(Renderer, loadedSurface);
        if (newTexture == NULL)
        {
            printf("Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
        }
        else
        {
            //Get image dimensions
            mWidth = loadedSurface->w;
            mHeight = loadedSurface->h;
        }

        //Get rid of old loaded surface
        SDL_FreeSurface(loadedSurface);
    }

    //Return success
    mTexture = newTexture;
    return mTexture != NULL;
}

void Texture::free()
{
    //Free texture if it exists
    if (mTexture != NULL)
    {
        SDL_DestroyTexture(mTexture);
        mTexture = NULL;
        mWidth = 0;
        mHeight = 0;
    }
}

void Texture::render()
{
    //Set rendering space and render to screen
    SDL_Rect renderQuad = { x, y, mWidth, mHeight };
    //Set clip rendering dimensions
    if (clip != NULL)
    {
        renderQuad.w = clip->w;
        renderQuad.h = clip->h;
    }
    SDL_RenderCopy(Renderer, mTexture, clip, &renderQuad);
}

void Texture::setSize(int width, int height)
{
    mWidth = width;
    mHeight = height;
}

void Texture::addClip(string name, SDL_Rect * aclip)
{
    clips[name] = aclip;
}

void Texture::setClip(string name)
{
    clip = clips[name];
}

void Texture::setNullClip()
{
    clip = NULL;
}

void Texture::setPos(int newx, int newy)
{
    x = newx;
    y = newy;
}

void Texture::movePos(int addx, int addy)
{
    x += x;
    y += y;
}

int Texture::getWidth()
{
    return mWidth;
}

int Texture::getHeight()
{
    return mHeight;
}

void Texture::setColor(Uint8 red, u8 green, u8 blue)
{
    //Modulate texture
    SDL_SetTextureColorMod(mTexture, red, green, blue);
}

void Texture::setBlendMode(SDL_BlendMode blending)
{
    //Set blending function
    SDL_SetTextureBlendMode(mTexture, blending);
}

void Texture::setAlpha(Uint8 alpha)
{
    //Modulate texture alpha
    SDL_SetTextureAlphaMod(mTexture, alpha);
}

bool Texture::loadFromRenderedText(std::string textureText, SDL_Color textColor)
{
    //Get rid of preexisting texture
    free();

    //Render text surface
    SDL_Surface* textSurface = TTF_RenderText_Solid(Font, textureText.c_str(), textColor);
    if (textSurface == NULL)
    {
        printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
    }
    else
    {
        //Create texture from surface pixels
        mTexture = SDL_CreateTextureFromSurface(Renderer, textSurface);
        if (mTexture == NULL)
        {
            printf("Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError());
        }
        else
        {
            //Get image dimensions
            mWidth = textSurface->w;
            mHeight = textSurface->h;
        }

        //Get rid of old surface
        SDL_FreeSurface(textSurface);
    }

    //Return success
    return mTexture != NULL;
}

然后是按钮类:

#pragma once
#include "Texture.h"
enum MouseState {
    Out,
    Hover
};
enum Press {
    LClick,
    RClick,
    None
};
class Button : public Texture{
    int w;
    int h;
    int x;
    int y;
public:
    //Inits the variables
    Button(int aw, int ah);
    //Handles events
    Press handleEvent(SDL_Event* e);
};

功能定义:

#include "Button.h"

Button::Button(int aw, int ah) : w(aw), h(ah)
{
    setSize(aw, ah);
}

Press Button::handleEvent(SDL_Event * e)
{
    //If mouse event happened
    if (e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP)
    {
        //Get mouse position
        int ax, ay;
        SDL_GetMouseState(&ax, &ay);
        //Check if mouse is in button
        bool inside = true;

        //Mouse is left of the button
        if (ax < x)
        {
            inside = false;
        }
        //Mouse is right of the button
        else if (ax > x + w)
        {
            inside = false;
        }
        //Mouse above the button
        else if (ay < y)
        {
            inside = false;
        }
        //Mouse below the button
        else if (ay > y + h)
        {
            inside = false;
        }
        //Mouse is outside button
        if (!inside)
        {
            setClip("out");
            return None;
        }
        //Mouse is inside button
        else
        {
            setClip("Hover");
            //Set mouse over sprite
            switch (e->button.button)
            {
            case SDL_BUTTON_LEFT:
                return LClick;
            break;

            case SDL_BUTTON_RIGHT:
                return RClick;
            break;
            default:
                return None;
                break;
            }
        }
    }
    else {
        return None;
    }
}

当我跑步时,我什么都没得到(好吧,我得到一个窗口,但没有按钮)!如果按钮类接缝准系统,那是因为我非常确定我已将错误缩小到handleEvent()。我仔细检查了一下,无法找到错误。

1 个答案:

答案 0 :(得分:0)

我弄明白了这个错误。原因是我不习惯SDL的坐标系。 在SDL中,左上角是0,0。 在常规坐标网格中,左下角为0,0。 :(P