分解SDL代码

时间:2018-01-05 20:48:08

标签: c++ sdl-2

我正在做的第一个例子来自" SDL游戏开发黑白"书。

我遇到问题的地方就是我应该打破的例子" HelloSDL"代码在一个标题(.h)和两个cpp(.cpp)文件中。

他们是:

的main.cpp

#include "Game.h"
#include <stdio.h>
#include <SDL2/SDL.h>

Game* g_game = 0;

int main(int argc, char* args[])
{

    g_game = new Game();

    g_game->init("Chapter 1", 100, 100, 640, 480, 0);

    while(g_game->running())
    {
        g_game->handleEvents();
        g_game->update();
        g_game->render();
    }
    g_game->clean();


    void Game::handleEvents()
    {
        SDL_Event event;
        if(SDL_PollEvent(&event))
        {
           switch (event.type)
           {
               case SDL_QUIT:
                   m_bRunning = false;
               break;

              default;
              break;
           }
       }
    }
    return 0;
}

Game.h

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED
#include <SDL2/SDL.h>

class Game
{
public:

    Game();
    ~Game();

    void init();

    void render();
    void update();
    void handleEvents();
    void clean();

    bool running() { return m_bRunning; }

private:

    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;

    bool m_bRunning;
};

#endif // GAME_H_INCLUDED

Game.cpp

#include <iostream>
#include "Game.h"
//#include <stdio.h>
#include <SDL2/SDL.h>

bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
    {
        std::cout << "SDL init success\n";
        m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);

        if(m_pWindow != 0)
        {
            std::cout << "window creation success\n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

            if(m_pRenderer != 0)
            {
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer, 255 255, 255, 255);
            }
            else
            {
                std::cout << "renderer init fail\n";
                return false;
            }
        }
        else
        {
            std::cout << "window init fail\n";
            return false;
        }
    }
        else
        {
            std::cout << "SDL init fail\n";
            return false;
        }

        std::cout << "init success\n";
        m_bRunning = true;

        return true;
}

void Game::render()
{
    SDL_RenderClear(m_pRenderer);

    SDL_RenderPresent(m_pRenderer);
}

SDL_Quit()
    +{
        std::cout << "cleaning game\n";
        SDL_DestroyWindow(m_pWindow);
        SDL_DestroyRenderer(m_pRenderer);
        SDL_Quit();
    }

void Game::clean()
{
    std::cout << "cleaning game\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}

在CodeBlocks(16.11)中编译后,我收到下一条错误消息:

Game.cpp|6|error: prototype for 'bool Game::init(const char*, int, int, int, int, int)' does not match any in class 'Game'

Game.h|12|error: candidate is: void Game::init()

首先,我遇到了更多错误,但我设法解决了。 还有两个错误要解决。 我想这是Game.h文件中的wromg,但我找不到它。

提前谢谢!

2 个答案:

答案 0 :(得分:0)

好像你可能来自C背景。在C ++中,不允许在函数声明中省略函数签名。基本上,通过声明void init();您声明void Game::init(void)函数,而不是bool Game::init(const char* title, int xpos, int ypos, int width, int height, int flags)。因此,必须仔细检查函数声明是否与函数定义完全匹配。另一件事是在C ++中应该在构造函数中执行对象初始化并在析构函数中执行清理。

答案 1 :(得分:0)

在game.h中声明的init()返回void,但是你提供的定义返回bool。