对象不能在SDL (c++)中工作

objects not working in SDL (C++)

本文关键字:工作 c++ 不能 SDL 对象      更新时间:2023-10-16

我正在用c++做一个简单的游戏,用SDL作为我的API。我把我的图像分割函数放在一个单独文档的类中,这样它在我的主文件上就不会那么混乱了。但是,当我尝试使用类的对象调用函数时,IDE说我正在对函数进行未定义的引用。下面是主文件:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "imageBlitingFunctions.h"
#include <iostream>
#include <string>
#include <sstream>
int screenW = 640;
int screenH = 480;
int screenBPP = 32;
SDL_Surface* window = SDL_SetVideoMode(screenW, screenH, screenBPP, SDL_SWSURFACE);
imageBlitingFunctions IBFobject;
SDL_Surface* background1;
SDL_Surface* player1;
int main(int argc, char* args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();
    background1 = IBFobject.loadIMG("background.png");
    player1 = IBFobject.loadIMG("swordtest.png");
    IBFobject.blitIMG(0, 0, window, background1, 0, 0, 1000, 1000);
    IBFobject.blitIMG(0, 0, window, player1, 100, 0, 100, 300);
    SDL_FreeSurface(background1);
    SDL_FreeSurface(player1);
    SDL_Quit();
    return 0;
}

这是类的头文件:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <iostream>
#include <string>
#ifndef IMAGEBLITINGFUNCTIONS_H
#define IMAGEBLITINGFUNCTIONS_H

class imageBlitingFunctions
{
    public:
        SDL_Surface *loadIMG(std::string filename);
        void blitIMG(int pX, int pY, SDL_Surface *window, SDL_Surface *image, int cpX, int cpY, int cpH, int cpW);
        SDL_Surface *loadText(int red, int blue, int green, std::string fontname, int fontSize, std::string text);
};
#endif // IMAGEBLITINGFUNCTIONS_H

这里是类:

#include "imageBlitingFunctions.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <iostream>
#include <string>
SDL_Surface* imageBlitingFunctions::loadIMG(std::string filename)
{
    SDL_Surface *img = IMG_Load(filename.c_str());
    SDL_Surface *imgOPT = SDL_DisplayFormat(img);
    SDL_FreeSurface(img);
    return imgOPT;
}
void imageBlitingFunctions::blitIMG(int pX, int pY, SDL_Surface *window, SDL_Surface *image, int cpX, int cpY, int cpH, int cpW)
{
    SDL_Rect positionIMG;
    positionIMG.x = pX;
    positionIMG.y = pY;
    SDL_Rect clipP;
    clipP.x = cpX;
    clipP.y = cpY;
    clipP.h = cpH;
    clipP.w = cpW;
    SDL_BlitSurface(image, &clipP, window, &positionIMG);
    SDL_Flip(window);
}
SDL_Surface* imageBlitingFunctions::loadText(int red, int blue, int green, std::string fontname, int fontSize, std::string text)
{
    int color1 = red;
    int color2 = blue;
    int color3 = green;
    SDL_Color textColor = {color1, color2, color3};
    TTF_Font *font1 = TTF_OpenFont(fontname.c_str(), fontSize);
    SDL_Surface *message1 = TTF_RenderText_Solid(font1, text.c_str(), textColor);
    return message1;
}

任何帮助都会很感激。

对函数的未定义引用意味着链接器找不到该函数的定义。函数的定义是你放在代码"这里是类:"(也许你叫它imageBlitingFunctions.cpp或类似的东西?)所以问题是你的IDE找不到这个文件。

要解决这个问题,你需要修复项目配置