2015-02-06 43 views
2

我不熟悉使用Visual Studio和庫OpenGL和SDL。使用SDL和GLEW設置Visual Studio項目

我在設置程序時遇到問題,並在嘗試構建程序時遇到錯誤。

誰能幫我解決下 -

Error 1 error LNK2019: unresolved external symbol [email protected] referenced in function _SDL_main  
Error 2 error LNK2019: unresolved external symbol [email protected] 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?

+0

不回答你的問題,但值得指出的是:以上'SDL_Init的評論() '沒有終止,因爲你在最後有'l'而不是'/'('/ * Initialize SDL * l'),所以SDL不會被初始化。 – GoBusto 2015-02-08 08:34:48

回答

2

不,你錯過了一個圖書館。你包括GLEW頭文件(S):

#include<GL\glew.h> 

然而,頭文件只定義函數的原型(如glewInit()) - 實際實現這些功能將被存儲在一個.lib (或.a)文件,您必須鏈接該文件才能構建項目。

自從我使用Visual Studio以來已經有一段時間了,但我認爲您通常會將鏈接器選項設置爲項目配置的一部分。然而,可能你的代碼中,指定連接選項,如果你必須:

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

注意,#pragma方法是MSVC特定的,並且可能不會對其他編譯器的工作。

如果您無法擺弄鏈接器選項,您總是可以改爲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); 
相關問題