Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL2/SDL.h>
- #include <SDL2/SDL_opengl.h>
- #include <FreeImage.h>
- #include <stdio.h>
- int main( int argc, char *argv[] )
- {
- SDL_Init( SDL_INIT_EVERYTHING );
- FreeImage_Initialise();
- SDL_Window *window = NULL;
- unsigned int windowWidth = 640;
- unsigned int windowHeight = 480;
- SDL_GLContext context = NULL;
- window = SDL_CreateWindow( "Test",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- windowWidth,
- windowHeight,
- SDL_WINDOW_OPENGL );
- context = SDL_GL_CreateContext( window );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( 0, windowWidth, windowHeight, 0, -1, 1 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glEnable( GL_TEXTURE_2D );
- glEnable( GL_DEPTH_TEST );
- const char *filename = "rockstar.jpg";
- FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
- fif = FreeImage_GetFileType( filename, 0 );
- if ( fif == FIF_UNKNOWN )
- fprintf( stderr, "Couldn't get the file type!" );
- FIBITMAP *bitmap = FreeImage_Load( fif, filename );
- if ( !bitmap )
- fprintf( stderr, "Couldn't load the image!" );
- unsigned int imageWidth = 0;
- unsigned int imageHeight = 0;
- BYTE *imageBits = 0;
- imageWidth = FreeImage_GetWidth(bitmap);
- imageHeight = FreeImage_GetHeight(bitmap);
- imageBits = FreeImage_GetBits(bitmap);
- fprintf( stdout, "%u %u %p\n", imageWidth, imageHeight, imageBits );
- GLuint texId;
- glGenTextures( 1, &texId );
- glBindTexture( GL_TEXTURE_2D, texId );
- glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, imageWidth,
- imageHeight, 0, GL_RGB,
- GL_UNSIGNED_BYTE, imageBits );
- if ( bitmap )
- FreeImage_Unload( bitmap );
- bool running = true;
- while ( running ) {
- SDL_Event event;
- while ( SDL_PollEvent(&event) )
- if ( event.type == SDL_QUIT )
- running = false;
- glBindTexture( GL_TEXTURE_2D, texId );
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0f, 1.0f ); glVertex2f( 0.0f, 0.0f );
- glTexCoord2f( 1.0f, 1.0f ); glVertex2f( imageWidth, 0.0f );
- glTexCoord2f( 1.0f, 0.0f ); glVertex2f( imageWidth, imageHeight );
- glTexCoord2f( 0.0f, 0.0f ); glVertex2f( 0.0f, imageHeight );
- glEnd();
- SDL_GL_SwapWindow( window );
- SDL_Delay( 16 );
- }
- SDL_GL_DeleteContext( context );
- SDL_DestroyWindow( window );
- FreeImage_DeInitialise();
- SDL_Quit();
- }
Add Comment
Please, Sign In to add comment