简体   繁体   English

对象在SDL(C ++)中不起作用

[英]objects not working in SDL (C++)

I am working on a simple game in C++, with SDL as my API. 我正在使用SDL作为我的C ++进行简单的游戏。 I put my image bliting functions in a class on a separate document so that it would less messy on my main file. 我将图像变蓝函数放在单独文档的类中,这样就不会在我的主文件上造成混乱。 However, when I try to call the functions using the object for the class, my IDE says that I am making an undefined reference to the functions. 但是,当我尝试使用该类的对象来调用函数时,IDE表示我正在对这些函数进行未定义的引用。 Here is the main file: 这是主文件:

#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;
}

Here is the header for the class: 这是该类的标题:

#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

And here is the class: 这是课程:

#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;
}

Any help would be appreciated. 任何帮助,将不胜感激。

An undefined reference to a function means that the linker can't find the function definition. 对函数的未定义引用意味着链接器找不到函数定义。 The definition of the function is what you've put in the code "And here is the class:" (maybe you call it imageBlitingFunctions.cpp or something similar?) so the problem is that you're IDE can't find this file. 该函数的定义是您在代码中输入的“这里是类:”(也许您将其称为imageBlitingFunctions.cpp或类似名称?),所以问题是您在IDE中找不到此文件。

To solve this problem you need to fix the project configuration. 要解决此问题,您需要修复项目配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM