2016-09-05 1 views
-1

Kürzlich habe ich versucht, eine Schaltfläche mit den lazyfoo.net Tutorials zu machen. Das Tastenbeispiel, das sie codierten, passte nicht zu dem, was ich brauchte (besonders, weil ich die LTexture Klasse modifiziert hatte), also modifizierte ich es ... und natürlich funktionierte es nicht.SDL Button nicht angezeigt

So, hier ist die modifizierte LTexture Klasse (jetzt genannt Texture):

#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; 
}; 

Und die Funktionsdefinitionen:

#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; 
} 

Dann ist hier die Schaltfläche Klasse:

#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); 
}; 

Und die Funktionsdefinitionen:

#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; 
    } 
} 

Und wenn ich das laufe, bekomme ich nichts (naja ich bekomme ein Fenster aber ohne den Knopf drin)! Wenn die Button-Klasse auf Barebones stößt, dann bin ich mir ziemlich sicher, dass ich den Fehler auf handleEvent() eingegrenzt habe. Ich habe es durchgesehen und konnte den Fehler nicht finden.

+3

Sie sollten ein minimales, funktionierendes Beispiel, nicht ein ganzes Projekt zeigen. – skypjack

+1

Das ** ist ** das Zeug, das Sie brauchen. Sie brauchen den Knopf, und da es von Textur abgeleitet ist, benötigen Sie auch die Texture-Klasse, um es vollständig zu verstehen – TheBeginningProgrammer

+0

Tatsächlich verwenden Sie fast alle Funktionen in Texture in der Schaltflächenklasse. – TheBeginningProgrammer

Antwort

0

Ich habe den Fehler herausgefunden. Der Grund ist, dass ich nicht an das Koordinatensystem von SDL gewöhnt bin. In SDL ist oben links 0,0. In normalen Koordinatenrastern ist links unten 0,0. : (P

Verwandte Themen