SFML、C++、Makefile - 无效指令

SFML, C++, Makefile - Invalid instruction

提问人:Dxrk_ 提问时间:10/31/2023 最后编辑:Dxrk_ 更新时间:10/31/2023 访问量:39

问:

我正在使用 SFML 用 C++ 编写一个 Arkanoid。事实是,当游戏结束时,游戏的状态会从游戏的状态变为残局。游戏状态发生变化,在控制台上输入游戏记录,但不是几毫秒,并输入以下内容:make: *** [Makefile:19: all] 无效指令(已创建内存转储)。

若要运行代码,可以使用以下命令:make

操作系统 : Archlinux

Pkg 版本:make 4.4.1-2、sfml 2.6.0-3

游戏.cpp :

#include "../include/Game.hpp"

Game::Game()
{
    initWindow();
    initFont();
    currentState = std::make_unique<GameState>(window, font);
}

void Game::initWindow()
{
    window.create(VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Arkanoid");
    window.setFramerateLimit(60);
}

void Game::initFont()
{
    if (!font.loadFromFile("fonts/mont_extralightdemo.ttf"))
        exit(0);
}

void Game::changeState(std::unique_ptr<State> newState)
{
    currentState = std::move(newState);
}

void Game::handleInput()
{
    currentState->handleInput();
}

void Game::update()
{
    currentState->update();

    if (currentState->isGameOver()) {
        int score = currentState->getScore();
        std::unique_ptr<State> newState = std::make_unique<EndGameState>(window, font, score);
        changeState(std::move(newState));
    }
}

void Game::render()
{
    window.clear();
    currentState->render(window);
    window.display();
}

void Game::run()
{
    while (window.isOpen()) {
        handleInput();
        update();
        render();
    }
}

游戏状态.cpp :

#include "../include/GameState.hpp"

GameState::GameState(RenderWindow& windowGame, Font& fontGame)
    : window(windowGame)
    , font(fontGame)
    , ball()
    , board()
    , blocks()
    , text(fontGame)
    , score(0)
{
}

void GameState::handleInput()
{
    if (!ballMove) {
        if (Keyboard::isKeyPressed(Keyboard::D)) {
            board.moveRight();
            ball.setPosition(Vector2f(board.getPosition().x + board.getSize().x / 2 - ball.getRadius(), ball.getPosition().y));
        }
        if (Keyboard::isKeyPressed(Keyboard::A)) {
            board.moveLeft();
            ball.setPosition(Vector2f(board.getPosition().x + board.getSize().x / 2 - ball.getRadius(), ball.getPosition().y));
        }
    } else if (ballMove) {
        if (Keyboard::isKeyPressed(Keyboard::D)) {
            board.moveRight();
        }
        if (Keyboard::isKeyPressed(Keyboard::A)) {
            board.moveLeft();
        }
    }

    while (window.pollEvent(ev)) {
        switch (ev.key.code) {
        case Keyboard::Escape:
            window.close();
            break;
        case Keyboard::Space:
            ballMove = true;
        }
    }
}

void GameState::update()
{
    if (ballMove) {
        ball.move();
        if (ball.getGlobalBounds().intersects(board.getGlobalBounds()))
            ball.setVelocityY(-(rand() % 7 + 3));
        if (blocks.handleCollision(ball.getGlobalBounds())) {
            ball.setVelocityY((rand() % 7 + 3));
            score += 50;
        }
        if (ball.getPosition().y > board.getPosition().y)
            setGameOver(true);
    }
}

void GameState::render(RenderWindow& window)
{
    text.draw(&window, score);
    ball.draw(&window);
    board.draw(&window);
    blocks.draw(&window);
}

int GameState::getScore() {
    return score;
}

bool GameState::isGameOver() const
{
    return gameOver;
}

void GameState::setGameOver(bool value)
{
    gameOver = value;
}

EndGameState.cpp :

#include "../include/EndGameState.hpp"

EndGameState::EndGameState(RenderWindow& windowGame, Font& fontGame, int& score)
    : window(windowGame)
    , text(fontGame)
    , scoreFinal(score)
{
    text.setPosition(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 9);   
}

void EndGameState::handleInput() {
    while (window.pollEvent(ev)) {
        switch (ev.key.code) {
        case Keyboard::Escape:
            window.close();
            break;
        }
    }
}

void EndGameState::update() {
    
}

void EndGameState::render(sf::RenderWindow& window) {
    text.draw(&window, scoreFinal);
}

bool EndGameState::isGameOver() const {
    
}

int EndGameState::getScore() {
    
}

文本游戏.cpp :

#include "../include/TextGame.hpp"

TextGame::TextGame(Font& font) {
    text.setFont(font);
    text.setCharacterSize(36);
    text.setColor(Color::White);
    text.setPosition(60, 30);
}

void TextGame::setPosition(float x, float y) {
    text.setPosition(Vector2f(x, y));
}

void TextGame::setCharacterSize(int size) {
    text.setCharacterSize(size);
}

void TextGame::draw(RenderWindow* window, int score) {
    text.setString("Score : " + std::to_string(score));
    window->draw(text);
}

MakeFile(生成文件):

CXX = g++
CXXFLAGS = -Wall -Wreorder
SFMLFLAGS = -lsfml-graphics -lsfml-system -lsfml-window

SRCDIR = src
INCDIR = include
OBJDIR = obj
BINDIR = bin
TARGET = main

SOURCES = $(wildcard $(SRCDIR)/*.cpp)
HEADERS = $(wildcard $(INCDIR)/*.hpp)
OBJECTS := $(patsubst $(SRCDIR)/%.cpp,$(OBJDIR)/%.o,$(SOURCES))

.PHONY: all clean

all: $(BINDIR)/$(TARGET)
    @clear
    @./$(BINDIR)/$(TARGET)

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(HEADERS)
    $(CXX) $(CXXFLAGS) -I$(INCDIR) -c -o $@ $<

$(BINDIR)/$(TARGET): $(OBJECTS)
    $(CXX) $^ -o $@ $(SFMLFLAGS)

clean:
    @rm -rf $(OBJDIR)/*
    @rm -rf $(BINDIR)/*

我以为问题出在文本输出中,但我删除了这个变量,没有任何改变......(

C++ OOP Makefile 游戏开发 SFML

评论

1赞 MadScientist 10/31/2023
请编辑您的问题并提供您运行的确切命令和您得到的确切输出(如果有很多,请提供最后 10 或 20 行):使用剪切和粘贴并将其格式化为代码块,以便清楚换行符的位置等。其次,在makefile工作之前,你永远不应该将前缀添加到你的配方行中,你绝对不应该添加命令。丢弃此关键输出会使调试生成问题变得困难。最后,请指定您正在使用的操作系统类型以及您正在使用的 make 版本。@clearmake --version
1赞 Ted Lyngmo 10/31/2023
此外,如果您需要调试帮助,请提供最小的可重现示例。我们无法编译它,而且代码也太多了。
0赞 Botje 11/3/2023
你有很多方法承诺返回一个值,但实际上没有。你的编译器不是在尖叫着要你修复它们吗?这是您收到“非法指令”错误的一个非常可能的原因。

答: 暂无答案