所以我打算用 MinGW 做一个 SDL + OpenGL 项目。但由于某种原因,它仍然取决于我觉得有点奇怪的 MSVCR90.dll。所以我试图在我的电脑上找到一个 dll 的副本并复制到我的 bin 文件夹中。现在它给了我这个讨厌的小错误:
这是我的代码和编译设置:
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <SDL/SDL_image.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
SDL_Surface * screen;
GLuint loadImage(string path)
{
GLuint texture;
GLint nop;
GLenum texture_format;
SDL_Surface * sdlimage = IMG_Load(path.c_str());
if (sdlimage)
{
if ((sdlimage->w & (sdlimage->w - 1)) != 0)
{
printf("warning: %s's width is not a power of 2\n", path.c_str());
}
if ((sdlimage->h & (sdlimage->h - 1)) != 0)
{
printf("warning: %s's height is not a power of 2\n", path.c_str());
}
nop = sdlimage->format->BitsPerPixel;
if (nop == 4)
{
if (sdlimage->format->Rmask == 0x000000FF) texture_format = GL_RGBA;
else texture_format = GL_BGRA;
} else if (nop == 3)
{
if (sdlimage->format->Rmask == 0x0000FF) texture_format = GL_RGB;
else texture_format = GL_BGR;
} else
{
printf("Invalid byte format on image %s", path.c_str());
exit(1);
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //Make sure it doesn't blur
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //In both shrinking and stretching situations
glTexImage2D(GL_TEXTURE_2D, 0, nop, sdlimage->w, sdlimage->h, 0, texture_format, GL_UNSIGNED_BYTE, sdlimage->pixels);
SDL_FreeSurface(sdlimage);
return texture;
} else
{
printf("Could not loud %s: %s\n", path.c_str(), SDL_GetError());
exit(1);
}
}
void drawTexture( GLuint texture )
{
glBindTexture(GL_TEXTURE_2D , texture);
glBegin(GL_QUADS);
glTexCoord2i( 0 , 0 );
glVertex2f( 0 , 0 );
glTexCoord2i( 1 , 0 );
glVertex2f( 16 , 0 );
glTexCoord2i( 1 , 1 );
glVertex2f( 16 , 32 );
glTexCoord2i( 0 , 1 );
glVertex2f( 0 , 32 );
glEnd();
}
void shutdown(void)
{
SDL_Quit();
}
void init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) //Loads SDL
{
printf("Error could not initialize SDL!");
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); //Double buffer for openGL
screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_OPENGL); //Setup screen size
glEnable(GL_TEXTURE_2D); //2d textures
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Background colour will be black
glViewport(0, 0, 640, 480); // Setup a viewport that matches our resolution
glClear(GL_COLOR_BUFFER_BIT); //Clear the colours
glMatrixMode(GL_PROJECTION); //Setup our projection
glLoadIdentity(); //Load a new one
glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f); //Make our view matches our resolution
glMatrixMode(GL_MODELVIEW); //Go into modelview
glLoadIdentity(); //Load a new one
atexit(shutdown);
}
int main(int argc, char * argv[])
{
init();
GLuint text = loadImage("rmb.png");
drawTexture(text);
SDL_GL_SwapBuffers();
SDL_Delay(3000);
glDeleteTextures( 1, &text );
return 0;
}
编译设置:
g++ -o bic.exe "src\\game.o" -lmingw32 -lopengl32 -lglu32 -lSDLmain -lSDL -lSDL_image