I'm trying to load an image into my game. It's being written in C++, with SDL and OpenGL, and the SDL_Image framework. I've gotten the image in, and have rotated/inverted it to my needs, but there are two main problems.
Firstly, the image's colors are completely inverted. (ie blue -> red)
I have the format to either 'GL_RGB' or 'GL_RGBA,' depending on the bytes per pixel. I've tried with PNGs, JPEGs, and BMPs, but their all the same. I'm lost!
--Thanks to datenwolf for correcting my silly mistakes!
Secondly, the image draws once, and that's it. It doesn't get redrawn on the other buffer, it just gets displayed once and then is gone as soon as the next frame is drawn.
GLuint loadTexture()
{
SDL_Surface* image = IMG_Load( "/Users/<My name>/Pictures/pacman board.jpeg" );
SDL_DisplayFormatAlpha(image);
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int Mode = NULL;
if(image->format->BytesPerPixel == 3) {Mode = GL_RGB;}
else if(image->format->BytesPerPixel == 4) {Mode = GL_RGBA;}
glTexImage2D(GL_TEXTURE_2D, 0, Mode, image->w, image->h, 0, Mode, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image);
return object;
}
int main(int argc, char * argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_WM_SetCaption("HI", NULL);
SDL_SetVideoMode(600, 600, 32, SDL_OPENGL);
glClearColor(0,0,0,1);
glViewport(0, 0, 600, 600);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
unsigned int pad_texture = 0;
pad_texture = loadTexture();
//---MAIN LOOP---\\
bool isRunning = true;
SDL_Event event;
while (isRunning)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) { isRunning = false; }
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {isRunning = false;}
}
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0, 600, 0, 600); // For some reason, I prefer having '0,0' at the bottom left.
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,pad_texture);
glBegin(GL_QUADS);
glTexCoord2d(0, 1); glVertex2f(0, 0);
glTexCoord2d(1, 1); glVertex2f(600, 0);
glTexCoord2d(1, 0); glVertex2f(600, 600);
glTexCoord2d(0, 0); glVertex2f(0, 600);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix(); //Stop drawing
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}