使用 SDL 和 GLEW 设置 Visual Studio 项目

Setting Visual Studio Project with SDL and GLEW

我刚开始使用 Visual Studio 以及库 OpenGL 和 SDL。

我在设置我的程序时遇到问题,并且在我尝试构建它时一直 运行 出错。

谁能帮我解决以下问题 -

Error   1   error LNK2019: unresolved external symbol __imp__glewInit@0 referenced in function _SDL_main    
Error   2   error LNK2019: unresolved external symbol __imp__glewGetErrorString@4 referenced in function _SDL_main  
Error   3   error LNK2001: unresolved external symbol __imp____GLEW_VERSION_3_0 
Error   4   error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup   
Error   5   error LNK1120: 4 unresolved externals   
    6   IntelliSense: identifier "GLuint" is undefined  
    7   IntelliSense: identifier "GLuint" is undefined  

这是我的代码 -

main.c

#include<SDL.h>
#include<GL\glew.h>
#include <stdio.h>

char shouldExit = 0;
int main(void)
{
    /* Initialize SDL *l
    if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
    return 1;
    }
    /* Create the window, OpenGL context */
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_Window* window = SDL_CreateWindow(
        "TestSDL",
        SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
        640, 480,
        SDL_WINDOW_OPENGL);
    if (!window) {
        fprintf(stderr, "Could not create window.ErrorCode = %s\n", SDL_GetError());
        SDL_Quit();
        return 1;
    }
    SDL_GL_CreateContext(window);
    /* Make sure we have a recent version of OpenGL */
    GLenum glewError = glewInit();
    if (glewError != GLEW_OK) {
        fprintf(stderr, "Could not initialize glew.ErrorCode = %s\n", glewGetErrorString(glewError));
        SDL_Quit();
        return 1;
    }
    if (!GLEW_VERSION_3_0) {
        fprintf(stderr, "OpenGL max supported version is too low.\n");
        SDL_Quit();
        return 1;
    }
    /* Setup OpenGL state */
    glViewport(0, 0, 640, 480);
    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 640, 480, 0, 0, 100);
    glEnable(GL_TEXTURE_2D);
    /* The game loop */
    while (!shouldExit) {
        // Handle OS message pump
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                shouldExit = 1;
            }
        }
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        /* Game logic goes here */
        SDL_GL_SwapWindow(window);
    }
    SDL_Quit();
    return 0;
}

DrawUtils.c

/***********************************************************************
 Utilities for loading and drawing sprites.
*/
#include<GL/glew.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

/* Load a file into an OpenGL texture, and return that texture. */
GLuint glTexImageTGAFile( const char* filename, int* outWidth, int* outHeight )
{
    const int BPP = 4;

    /* open the file */
    FILE* file = fopen( filename, "rb" );
    if( file == NULL ) {
        fprintf( stderr, "File: %s -- Could not open for reading.\n", filename );
        return 0;
    }

    /* skip first two bytes of data we don't need */
    fseek( file, 2, SEEK_CUR );

    /* read in the image type.  For our purposes the image type should
     * be either a 2 or a 3. */
    unsigned char imageTypeCode;
    fread( &imageTypeCode, 1, 1, file );
    if( imageTypeCode != 2 && imageTypeCode != 3 ) {
        fclose( file );
        fprintf( stderr, "File: %s -- Unsupported TGA type: %d\n", filename, imageTypeCode );
        return 0;
    }

    /* skip 9 bytes of data we don't need */
    fseek( file, 9, SEEK_CUR );

    /* read image dimensions */
    int imageWidth = 0;
    int imageHeight = 0;
    int bitCount = 0;
    fread( &imageWidth, sizeof( short ), 1, file );
    fread( &imageHeight, sizeof( short ), 1, file );
    fread( &bitCount, sizeof( unsigned char ), 1, file );
    fseek( file, 1, SEEK_CUR );

    /* allocate memory for image data and read it in */
    unsigned char* bytes = (unsigned char*)calloc( imageWidth * imageHeight * BPP, 1 );

    /* read in data */
    if( bitCount == 32 ) {
        int it;
        for( it = 0; it != imageWidth * imageHeight; ++it ) {
            bytes[ it * BPP + 0 ] = fgetc( file );
            bytes[ it * BPP + 1 ] = fgetc( file );
            bytes[ it * BPP + 2 ] = fgetc( file );
            bytes[ it * BPP + 3 ] = fgetc( file );
        }
    } else {
        int it;
        for( it = 0; it != imageWidth * imageHeight; ++it ) {
            bytes[ it * BPP + 0 ] = fgetc( file );
            bytes[ it * BPP + 1 ] = fgetc( file );
            bytes[ it * BPP + 2 ] = fgetc( file );
            bytes[ it * BPP + 3 ] = 255;
        }
    }

    fclose( file );

    /* load into OpenGL */
    GLuint tex;
    glGenTextures( 1, &tex );
    glBindTexture( GL_TEXTURE_2D, tex );
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0,
                  GL_BGRA, GL_UNSIGNED_BYTE, bytes );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

    free( bytes );

    if( outWidth ) {
        *outWidth = imageWidth;
    }
    if( outHeight ) {
        *outHeight = imageHeight;
    }
    return tex;
}

/* Draw the sprite */
void glDrawSprite( GLuint tex, int x, int y, int w, int h )
{
    glBindTexture( GL_TEXTURE_2D, tex );
    glBegin( GL_QUADS );
    {
    glColor3ub( 255, 255, 255 );
    glTexCoord2f( 0, 1 );
    glVertex2i( x, y );
    glTexCoord2f( 1, 1 );
    glVertex2i( x + w, y );
    glTexCoord2f( 1, 0 );
    glVertex2i( x + w, y + h );
    glTexCoord2f( 0, 0 );
    glVertex2i( x, y + h );
    }
    glEnd();
}

DrawUtils.h

#ifndef DRAWUTILS_H
#define DRAWUTILS_H

#ifdef __cplusplus
extern "C" {
#endif

GLuint glTexImageTGAFile( const char* filename, int* outWidth, int* outHeight );
void glDrawSprite( GLuint tex, int x, int y, int w, int h );

#ifdef __cplusplus
}
#endif

#endif

我是不是漏掉了#include?

不,您缺少图书馆。您包含 GLEW 头文件:

#include<GL\glew.h>

但是,头文件只定义函数 原型(例如glewInit())——这些函数的实际实现将存储在 .lib(或 .a)文件中,您必须 link 使用该文件才能构建项目。

自从我使用 Visual Studio 以来已经有一段时间了,但我认为您通常会设置 linker 选项作为项目配置的一部分。但是, 可以在您的代码中指定 linker 选项,如果您必须:

#pragma comment(lib, "glew/glew32s.lib")

请注意,#pragma 方法是特定于 MSVC 的,可能不适用于其他编译器。

如果您懒得摆弄 link 其他选项,您可以随时 add glew.c to your project 代替。


另一件事:您在 DrawUtils.h 中使用 GLuint,但未包含 gl.hglew.h:

GLuint glTexImageTGAFile( const char* filename, int* outWidth, int* outHeight );
void glDrawSprite( GLuint tex, int x, int y, int w, int h );

关于Visual Studio2017、OpenGL、SDL2和GLEW,推荐this网页。 我只复制和粘贴文本。

A 部分 - 下载 SDL 和 GLEW

A1 创建文件夹 OpenGL。在目录(磁盘)C: 中,创建(通过右键单击 > 新建 > 文件夹)名称为 OpenGL.

的新文件夹

A2 下载 SDL2-devel-2.0.8-VC.zip(Visual C++ 32/64 位) .它可以在 https://www.libsdl.org/download-2.0.php 找到。滚动到页面底部并找到开发库。

A2a 下载 Visual C++ 版本。它是 SDL2-devel-2.0.8-VC.zip(Visual C++ 32/64 位)。导航(始终通过双击)到 C: > OpenGL。

A2b 将文件夹 SDL2-2.0.8 从 window 下载到文件夹 OpenGL.

A2c如果下载了文件夹SDL2-devel-2.0.8-VC,双击得到SDL2-2.0.8.

A3 下载 GLEW。在下载下方的 http://glew.sourceforge.net/ 处,单击 Windows 32 位和 64 位 。导航到 C: > OpenGL。将文件夹 glew-2.1.0 从下载的 window 拖动(或通过右键单击,复制并粘贴)到文件夹 *OpenGL.

A3a 如果文件夹 glew-2.1.0-win32 已下载,双击获取 glew- 2.1.0.

B 部分 - 创建 Visual Studio 项目

B1 创建一个空项目。

B1a 在 Visual Studio 主菜单中,单击 文件 。然后转到新建 > 项目…

B1bNew Projectwindow的左边,点击Visual C++如果没有点击。

B1c 在屏幕中央单击 空项目.

B1d 在其下方,找到名称文本框,键入 Project-0.

B1e 在位置文本框旁边,单击 浏览... 并导航到 C: > OpenGL。

B1f 单击Select 一个文件夹New Project window 中的位置是 C:\OpenGL.

B1g确保未选中为解决方案创建目录复选框。

B1h点击确定。

B2 将您的源文件添加到项目中。

B2aSolution Explorer window(在屏幕左侧)中,右键单击 Source Files文件夹(最后一个)。

B2b 单击“添加”>“新项目”...

B2c 添加新项 - Project-0 window 中,单击 C++ 文件 ( .cpp)(第一个)从window中间开始。在名称文本框中键入 Main.cpp.

B2d. 位置是C:\OpenGL\Project-0.

B2e 单击“添加”按钮。该文件将在主文本编辑器中打开,但暂时将文件留空。

C 部分 - 在项目中安装 SDL 和 GLEW

C1 添加 Include 文件夹。

C1a配置附加包含目录:在解决方案资源管理器中右键单击名称您的项目,即 Project-0 和 select Properties.

C1b 添加 SDL 的 include 文件夹:打开 C/C++ drop-down 菜单。在下拉菜单中单击“常规”>“其他包含目录”>“字段右侧的向下箭头”>“编辑”。

C1cAdditional Include Directories中,点击第一个图标>复制粘贴,C:\OpenGL\SDL2-2.0.8\include

C1d添加GLEW的include文件夹:再次点击第一个图标>复制粘贴:C:\OpenGL\glew-2.1.0\include 单击 其他包含目录 上的确定 window.

C2 添加 x86Win32 文件夹。

C2a配置链接器附加库目录:打开链接器drop-down菜单,然后单击一般.

C2b 添加 x86 文件夹:单击 Additional Library Directories 条目,然后单击字段右侧的向下箭头。单击 drop-down 菜单中的 编辑

C2cAdditional Library Directories中点击第一个图标>复制粘贴,C:\OpenGL\SDL2-2.0.8\lib\x86

C2d添加Win32文件夹:点击第一个图标>复制粘贴,C:\OpenGL\glew-2.1.0\lib\Release\Win32 点击确定。

C3 添加四个 library 文件。

C3a配置liker Additional Dependencies: 在 Linker drop-down 菜单中,单击 Input。在 drop-down 菜单中单击 Additional Dependencies 条目 > 字段右侧的向下箭头 > Edit。

C3bAdditional Dependencies window 的 top-most 文本框中,复制并粘贴,SDL2.lib; SDL2main.lib; glew32.lib; opengl32.libAdditional Dependencies 中点击 OK window.

C4 配置链接器 子系统到控制台 .

C4a 在链接器 drop-down 菜单中,单击系统 > 子系统。单击下拉菜单中的向下箭头和 select Console(/SUBSYSTEM:CONSOLE)。单击 应用,然后在 项目 属性 页面 window.

上单击确定

C5x86 文件夹中复制 SDL2.dll 文件,然后粘贴到Project-0 文件夹。

C5a 导航到 C:> OpenGL > SDL2-2.0.8 > lib > x86。在 x86 文件夹中单击 SDL2.dll 文件 > right-click > 复制。

C5b 导航到 C:> OpenGL > Project-0。 Right-click Project-0 文件夹中的空白区域,以及 select 粘贴.

C5c SDL2.dll 文件现在应该与 [= 一起位于您的项目目录中561=] 文件和 Visual Studio.

创建的一些其他文件

C6glew32.dll 文件从 Win32 复制并粘贴到项目文件夹。

C6a 导航到 C:> OpenGL > glew-2.1.0 > bin > Release > Win32。单击 glew32.dll > right-click > 复制。

C6b 导航到 C:> OpenGL > Project-0。 Right-click Project-0 文件夹中的空白区域,以及 select Paste.

C6c glew32.dll 文件现在应该在 Project-0文件夹以及 Main.cpp、SDL2.dll 和 Visual Studio.

创建的其他一些文件

D 部分 - 测试和调试您的项目

D1 下载代码:单击 http://lazyfoo.net/tutorials/SDL/51_SDL_and_modern_opengl/index.php > 向下滚动并找到 在此处下载本教程的媒体和源代码.单击 此处 并下载 51_SDL_and_modern_opengl.cpp 文件夹。双击它 > 双击同名文件。它的代码将出现在 Main.cpp 文件旁边的 Visual Studio 中。复制代码(413 行)并粘贴到 Main.cpp 代码区域。在 V.S。主菜单单击绿色箭头 Local Windows Debugger 并等待...如果一切顺利,出现两个 windows:一个黑色和一个标题:SDL 教程 和黑色背景的白色方块内。如果有任何问题,请尝试修复错误。如果失败,请重复以上步骤。

E 部分 - 在 Visual Studio 2017

中使用 OpenGL-SDL-GLEW 模板创建项目

E1 创建项目模板:转到 Visual Studio 主菜单,打开 Project-0,单击项目 > 导出模板...。在 导出模板 向导中检查 项目模板 ,如果未选中。单击 下一步 >。在Select模板选项上,在模板名称文本框中输入:OpenGL-SDL-GLEW.单击 完成 。模板已创建。

E2 使用创建的模板创建 OpenGL-SDL-GLEW 项目

E2a 在 V.S。主菜单单击文件 > 新建 > 项目...。在 新建项目 window 上,单击模板:OpenGL-SDL-GLEW .在名称文本字段中,键入:Project-1。确保未选中 为解决方案创建目录 。单击“确定”。

E2bSolution Explorer 上,双击 Source Files。单击 Main.cpp > 右键单击​​ > 单击从项目中排除。

E2c 右键单击​​源文件 > 添加 > 新项目.... 在 添加新项目 - Project-1 window,单击 C++ 文件 (.cpp)。在 名称: 文本框中键入:Main.cpp位置 应该是 C:\OpenGL\Project-1。单击添加。现在,在 Solution ExploreSource Files 下方,您有新的 Main.cpp 文件。

E3 添加 SDL2.dll 文件到 project-folder

E3a 导航到 C:> OpenGL > Project-0 > 单击文件 SDL2.dll > 右键单击​​ > 单击复制。

E3b 导航到 C: > OpenGL > Project-1 > 单击空白区域 > 右键单击​​ > 单击粘贴。

E3c 现在文件SDL2.dll在文件夹Project-1Main.cpp等4个文件。

E4 添加 glew32.dll 文件到 project-folder

E4a 导航到 C:> OpenGL > Project-0 > 单击文件 glew32.dll > 右键单击​​ > 单击复制。

E4 导航到 C: > OpenGL > Project-1 > 单击空白区域 > 右键单击​​ > 单击粘贴。

E4c 现在文件glew32.dll在文件夹Project-1SDL2.dll,Main.cpp等4个文件.

E5 如上所述测试您的项目。干得好。

提示

记住你必须采取的 3 个额外步骤: 在 Visual Studio 2017 中使用 OpenGL-SDL-GLEW 模板创建项目就像创建一个普通的 C++ 项目,但是还有三个步骤:

1Solution Explorer, Source Files, 源文件 (Main.cpp 在上面的例子中)模板项目(Project-0,在这个例子中)应该从新项目中排除(Project-1 在示例中)。

2 文件 SDL2.dll 应该从以前的项目中复制并粘贴到新项目中。

3 文件 glew32.dll 应该从以前的项目中复制并粘贴到新项目中。