SDL从类中渲染rect

时间:2018-05-06 14:06:56

标签: c++ sdl-2

我正在尝试在类中渲染SDL_Rect,我将渲染器发送到我的类的“draw”函数,然后从那里渲染它。

void Tray::draw(SDL_Renderer *renderer){
  SDL_RenderFillRect(renderer, &bckRect);
}

当我在主循环上放置相同的代码时。一切都很好,但是当我从托盘类中执行此操作时,它会编译,但不会运行。

简化代码:

的main.cpp

#include <iostream>
#include <SDL2/SDL.h>

// local includes
#include "editor.h"

using namespace std;

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

  Editor *editor = new Editor();

  while (editor->running){
    editor->render();
  }

  editor->clean();

  return 0;
}

editor.h

#ifndef EDITOR_H
#define EDITOR_H

#include <iostream>
#include <SDL2/SDL.h>

#include "Gui-classes/tray.h"
#include "properties.h"

class Editor{
private:
  // Window variables
  SDL_Window *window;
  SDL_Renderer *renderer;
  Tray *tray;
public:
  Editor();
  ~Editor();
  void render();
};

#endif

editor.cpp

#include "editor.h"

Editor::Editor(){

  props = new Properties();

  if (SDL_Init(SDL_INIT_EVERYTHING) == 0) {
    window = SDL_CreateWindow("Great Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 600, SDL_WINDOW_RESIZABLE);

    running = true;

    if (window) {
      std::cout << "Window created!" << std::endl;
    }

    renderer = SDL_CreateRenderer(window, -1, 0);

  }
}
void Editor::render(){
  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  SDL_RenderClear(renderer);

  SDL_SetRenderDrawColor(renderer, 12, 12, 55, 255);
  tray->draw(renderer);

  SDL_RenderPresent(renderer);
}
void Editor::clean(){
  SDL_DestroyWindow(window);
  SDL_DestroyRenderer(renderer);
  SDL_Quit();
  std::cout << "Window ended" << std::endl;
}

tray.h

#ifndef TRAY_H
#define TRAY_H

#include <iostream>
#include <SDL2/SDL.h>

class Tray{
private:
  //  Position variables
  int x;
  int y;
  int w;
  int h;

  //Display variables
  SDL_Color color;
  SDL_Rect bckRect;  
public:
  Tray();
  ~Tray();
  void draw(SDL_Renderer *renderer);
};

#endif

Tray.cpp

#include "tray.h"

Tray::Tray(){
  bckRect.x = x;
  bckRect.x = y;
  bckRect.w = w;
  bckRect.h = h;
}

Tray::~Tray(){

}
void Tray::draw(SDL_Renderer *renderer){
  SDL_RenderFillRect(renderer, &bckRect);
  //std::cout << "teeest" << std::endl;
}

感谢您的帮助。

0 个答案:

没有答案