c++ - 我需要帮助实现我的事件管理器和引擎系统 (SDL && C++)

标签 c++ oop inheritance sdl

我在尝试编写可重复使用的游戏引擎时需要帮助。它不是最好的引擎,但我绝对认为一旦我完成它就可以重复使用。我不是在要求代码或被灌输,而是在寻求一些建议 :-)。

我当前的布局:

我有一个引擎类、一个游戏类和一个事件管理器类。引擎类扩展了事件管理器类,游戏类扩展了引擎类。这是这 3 个类的当前代码(忽略 Graphics 类——它只是一个可重用的类,我用它来避免重写全屏、初始化和调整屏幕大小的函数)。

引擎.HPP

#ifndef _ENGINE_HPP
#define _ENGINE_HPP

#pragma once

#include <SDL/SDL.h>

#include "event_manager.hpp"

enum
  {
    ENGINE_SUCCESS = 0,
    ENGINE_INITIALIZATION_ERROR
  };

class Engine : public EventManager
{
public:
  Engine();
  virtual ~Engine();
  int exec();
  void handle_event(SDL_Event *);
  void update_engine();
  virtual bool init();
  virtual void render();
  virtual void update();
  virtual void clean();
  bool running_;
};

引擎.CPP

#include "./engine.hpp"

Engine::Engine()
{
  running_ = false;
}

Engine::~Engine()
{
}

int Engine::exec()
{
  if (!init())
    {
      clean();
      return ENGINE_INITIALIZATION_ERROR;
    }

  SDL_Event event;
  while (running_)
    {
      while (SDL_PollEvent(&event))
    handle_event(&event);

      update();
      render();
    }

  clean();
  return ENGINE_SUCCESS;
}

void update_engine()
{
}

void handle_event(SDL_Event *event)
{
  EventManager::handle_event(event);
}

bool init() {return true;}
void render() {}
void update() {}
void clean() {}

EVENT_MANAGER.HPP

#ifndef _EVENT_MANAGER_HPP
#define _EVENT_MANAGER_HPP

#pragma once

#include <SDL/SDL.h>

class EventManager
{
public:
  EventManager();
  virtual ~EventManager();
  virtual void handle_event(SDL_Event *);
  // events here
  virtual void event_exit();
};

#endif

EVENT_MANAGER.CPP

#include "./event_manager.hpp"

EventManager::EventManager() 
{
}

EventManager::~EventManager()
{
}

void EventManager::handle_event(SDL_Event *event)
{
  switch (event->type)
    {
    case SDL_QUIT:
      event_exit();
      break;
    }
}

void on_exit() {}

游戏.HPP

#ifndef _GAME_HPP
#define _GAME_HPP

#include "./engine.hpp"
#include "./entity.hpp"
#include "./graphics.hpp"

class Game : public Engine
{
public:
  Game();
  bool init();
  void render();
  void update();
  void clean();
private:
  Graphics g;
};

#endif

游戏.CPP

#include "./game.hpp"

int main(int argc, char **argv)
{
  Engine engine;
  return engine.exec();
}

Game::Game() {}

bool Game::init()
{
  if (!g.init(800, 600, 32, g.screen_flags()))
    {
      return false;
    }

  SDL_WM_SetCaption("Title", NULL);
  return true;
}

void Game::update()
{
  Engine::update_engine();
}

void Game::clean()
{
  SDL_FreeSurface(g.screen_);
  SDL_Quit();
}

void Game::render()
{
  SDL_Flip(g.screen_);
}

我收到这个错误:

engine.cpp: In function ‘void handle_event(SDL_Event*)’:
engine.cpp:40: error: cannot call member function ‘virtual void
EventManager::handle_event(SDL_Event*)’ without object

为什么会这样?我不应该能够做 EventManager::如果我做了

class Engine : public EventManager

???

这是我遇到的唯一错误,我相信这很简单。现在我需要一些建议。

而不是像这样处理事件

void Engine::event_exit()

在引擎中,我宁愿在游戏类中这样做。

class Game : public Engine


void Game::event_exit()

如果这没有意义,请注意我是如何让 Engine 扩展 EventManager,以及我的 Game 类扩展 Engine

class Engine : public EventManager

class Game : public Engine

如果我调用这两个片段上方的片段,它会起作用吗?我无法测试它,因为我遇到了那个错误。

最佳答案

发生在我们最好的人身上,但我认为这只是忘记指定 namespace 的问题。当你实现engine.cpp中的函数时,你忘了在前面添加Engine::,所以代码应该是:

void Engine::update_engine()
{
}

void Engine::handle_event(SDL_Event *event)
{
    EventManager::handle_event(event);
}

这是 C++ 错误消息的经典案例,它并没有真正告诉您错误的根源

一个简短的解释,以防万一:

编译器尝试编译函数 void handle_event(SDL_Event *event),并看到了对方法 EventManager 的调用::handle_event(事件);。由于编译器认为该函数不是 Engine 类的一部分,因此它希望您调用 EventManager 类的特定实例的方法,即

someEventManager->handle_event(event);

一旦您指定您编写的实现是属于类 Engine 的方法的实现,编译器基本上会推导出:

void Engine::handle_event(SDL_Event *event)
{
    this->EventManager::handle_event(event);
}

因此很快乐。

关于c++ - 我需要帮助实现我的事件管理器和引擎系统 (SDL && C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844824/

相关文章:

c++ - 如何在 QQmlListProperty 属性中正确通知?

c++ - 在 C++ 中将指针变量分配给 const int?

java - super 构造函数无法按我认为的方式工作

c# - 了解 C# 中的虚拟方法、方法隐藏和重写

python - 在没有构造函数和继承的情况下实例化 Python 类

html - CSS可以从相邻元素继承吗?

c++ - GCC C++ 编译器是否考虑了 __restrict - 语句?

c++ - 类中派生信息的成员函数

java - 在实现中使用接口(interface)类型与公共(public)接口(interface)

java - 使用父类(super class)引用调用重载的继承方法