Jump to content
  • Advertisement
Sign in to follow this  
white

sdl image

This topic is 4821 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hy guys could you help me display a TGA with SDL_image? i tried to but got lots of error messages stating that files don't exist

Share this post


Link to post
Share on other sites
Advertisement
i don't know how to put code hope it works

#include <SDL/SDL.h>
#include <gl/gl.h>
#include <stdlib.h>
#include "SDL_image.h"





int main(int Arg_N, char ** Arg_V){

SDL_Event event;
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME);

glViewport(0, 0, 640, 480);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

SDL_Surface *image;
SDL_RWops *rwop;
rwop=SDL_RWFromFile("DATA/Textures/bkg01.tga", "rb");
image=IMG_LoadTGA_RW(rwop);


glEnable(GL_TEXTURE_2D);


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, image.texID);
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2f(-1, 1);
glVertex2f(-1, 1);
glTexCoord2f(1, 1);
glVertex2f(1, 1);
glTexCoord2f(1, -1);
glVertex2f(1, -1);
glTexCoord2f(-1, -1);
glVertex2f(-1, -1);
glEnd();
glPopMatrix();
for(int done = 0; !done;){
SDL_GL_SwapBuffers();
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)done = 1;
}
SDL_Quit();
return(0);
}


4 D:\work_stefan\projects\tetris\main.cpp In file included from main.cpp
30 D:\work_stefan\projects\tetris\SDL_image.h SDL.h: No such file or directory.
31 D:\work_stefan\projects\tetris\SDL_image.h SDL_version.h: No such file or directory.
32 D:\work_stefan\projects\tetris\SDL_image.h begin_code.h: No such file or directory.
114 D:\work_stefan\projects\tetris\SDL_image.h close_code.h: No such file or directory.
D:\work_stefan\projects\tetris\main.cpp In function `int SDL_main(int, char**)':
42 D:\work_stefan\projects\tetris\main.cpp `texID' has not been declared
42 D:\work_stefan\projects\tetris\main.cpp request for member of non-aggregate type before ')' token
D:\work_stefan\projects\tetris\Makefile.win [Build Error] [main.o] Error 1

i am dumb :(

Share this post


Link to post
Share on other sites
It sounds like you didn't install SDL_image properly. Try putting the SDL_image.h header file in the same directory where you put the SDL headers.

Share this post


Link to post
Share on other sites
I'd suggest donwloading the SDL_Image DevPak and installing. As long as you use the SDL DevPak, you should be in good shape.

Here's a complete example though when using OpenGL + SDL:

#include "SDL.h"
#include "SDL_Image.h"
#include "SDL_OpenGL.h"

#pragma comment( lib, "SDLmain.lib" )
#pragma comment( lib, "SDL.lib" )
#pragma comment( lib, "SDL_Image.lib" )
#pragma comment( lib, "OpenGL32.lib" )
#pragma comment( lib, "Glu32.lib" )

int main( int argc, char* argv[] )
{
if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
printf( "Unable to initialzie SDL: %s\n", SDL_GetError() );
return -1;
}

// We try to set the video mode, if it fails, then we do not want to go on
if ( SDL_SetVideoMode( 640, 480, 0, SDL_OPENGL ) == NULL )
{
printf( "Unable to set the video mode: %s\n", SDL_GetError() );
return -1;
}

glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, (float)640/(float)480, 1, 1000 );
glMatrixMode( GL_MODELVIEW );

SDL_Event Event;
Uint8 *Keys = 0;
bool Done = false;

GLuint texture = 0;

//Load Bitmap
SDL_Surface* tgaFile = IMG_Load("test.tga");

// Standard OpenGL texture creation code
glPixelStorei( GL_UNPACK_ALIGNMENT, 4 );

glGenTextures( 1, &texture );
glBindTexture(GL_TEXTURE_2D,texture);

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 0 );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 0 );

gluBuild2DMipmaps( GL_TEXTURE_2D, 3, tgaFile->w, tgaFile->h, GL_BGR_EXT, GL_UNSIGNED_BYTE, tgaFile->pixels );

//Free surface after using it
SDL_FreeSurface( tgaFile );

//change to texture matrix and do the trick
glMatrixMode(GL_TEXTURE);
glRotatef(180.0f,0.0f,0.0f,1.0f);
glScalef(-1.0f,1.0f,1.0f);

//back to normal
glMatrixMode(GL_MODELVIEW);

while( Done == false )
{
while ( SDL_PollEvent(&Event) )
{
if ( Event.type == SDL_QUIT )
Done = 1;
}

Keys = SDL_GetKeyState( 0 );

if( Keys[ SDLK_ESCAPE ] )
{
Done = 1;
}

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity( );

glTranslatef( 0, 0, -5 );
glRotatef(45,.5,1,0);

glBindTexture( GL_TEXTURE_2D, texture );

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();

SDL_GL_SwapBuffers();
}

//Free surface after using it
SDL_FreeSurface( tgaFile );

SDL_Quit();

return 0;
}





For everyone else that may come across this post, here is an example of using just SDL to do this:

#include "SDL.h"
#include "SDL_Image.h"

#pragma comment( lib, "SDLmain.lib" )
#pragma comment( lib, "SDL_Image.lib" )
#pragma comment( lib, "SDL.lib" )

void DrawIMG(SDL_Surface *img, SDL_Surface* des, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, des, &dest);
}

int main( int argc, char* argv[] )
{
SDL_Surface* screen;

if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
printf( "Unable to initialzie SDL: %s\n", SDL_GetError() );
return -1;
}

// We try to set the video mode, if it fails, then we do not want to go on
if ( (screen=SDL_SetVideoMode( 640, 480, 0, SDL_DOUBLEBUF )) == NULL )
{
printf( "Unable to set the video mode: %s\n", SDL_GetError() );
return -1;
}

SDL_Event Event;
Uint8 *Keys = 0;
bool Done = false;

//Load the tga from a file
SDL_Surface* tgaFile = IMG_Load("test.tga");
while( Done == false )
{
while ( SDL_PollEvent(&Event) )
{
if ( Event.type == SDL_QUIT )
Done = 1;
}

Keys = SDL_GetKeyState( 0 );

if( Keys[ SDLK_ESCAPE ] )
Done = 1;

// Clear the screen
SDL_FillRect( screen, 0, SDL_MapRGBA(screen->format, 0, 0, 0, 0) );

// Draw the tga
DrawIMG( tgaFile, screen, 0, 0 );

// Flip the screen
SDL_Flip( screen );
}

//Free surfaces after using it
SDL_FreeSurface( tgaFile );

// Quit SDL
SDL_Quit();

return 0;
}





Now the OpenGL is based off of this tutorial, which is why the texture is flipped and mirrored. I have a post going on here about fixing it.

BTW for you DevCpp users, you can't use the pragma comment's. You will have to link in your .A files by hand instead

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!