Can you display a bitmap image on the current console screen in SDL.

This is a discussion on Can you display a bitmap image on the current console screen in SDL. within the C Programming forums, part of the General Programming Boards category; I'm trying to get a bitmap image to display on command prompt by using the following code. Code: #include <stdio.h> ...

  1. #1
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288

    Can you display a bitmap image on the current console screen in SDL.

    I'm trying to get a bitmap image to display on command prompt by using the following code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <SDL/SDL.h>
    
    
    int main( int argc, char * argv[] )
    {
        if ( argc != 2 )
        {
            fprintf(stderr, "You must have at least 1 argument!");
            exit(1);
        }
    
    
        SDL_Surface* image = NULL;
        SDL_Surface* console = NULL;
    
    
        SDL_Rect my_image = { 160, 100, 200, 200 };
        SDL_Rect * p_my_image = &my_image;
    
    
        SDL_Init( SDL_INIT_VIDEO );
    
    
        if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
        {
            fprintf(stderr, "Could not load the bitmap image!");
            exit(1);
        }
    
    
        if ( ( console = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE ) ) == NULL )
        {
            fprintf(stderr, "Could not get the current console screen!");
            exit(1);
        }
    
    
        SDL_BlitSurface( image, NULL, console, p_my_image);
        SDL_Flip( console );
        SDL_Delay(2000);
    
    
        SDL_FreeSurface( image );
    
    
        SDL_Quit();
    
    
        return 0;
    }
    And then running it with this.

    Code:
    Image_loader "My_image.bmp"
    It works, but I'm not sure how to set the console variable to the current program's screen that I'm running it in.

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Yes, I know that SDL_SetVideoMode creates a new window, I just put it there as a placeholder until I can get the real console window that it is ran in.

  3. #3
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    I tried this and got 'Undefined reference to AttachConsole' and 'Implicit declaration to AttachConsole function', which is weird, since I included windows.h.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <SDL/SDL.h>
    
    
    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    
    
    pid_t GetProcessID( char * ProcessName )
    {
    
    
        HANDLE hProcessSnap;
        PROCESSENTRY32 pe32;
    
    
        /* Take a snapshot of all processes in the system. */
        hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
        if( hProcessSnap == INVALID_HANDLE_VALUE )
        {
            return 0;
        }
    
    
        /* Set the size of the structure before using it. */
        pe32.dwSize = sizeof( PROCESSENTRY32 );
    
    
        if( !Process32First( hProcessSnap, &pe32 ) )
        {
        CloseHandle( hProcessSnap );
        exit(EXIT_FAILURE);
        }
    
    
        /* Walk through the snapshot, and return the process handle when
           found. */
    
    
        do
        {
            if (strcmp(pe32.szExeFile, ProcessName) == 0)
            {
                CloseHandle( hProcessSnap );
                return pe32.th32ProcessID;
            }
        } while( Process32Next( hProcessSnap, &pe32 ) );
    
    
      CloseHandle( hProcessSnap );
    
    
      return 0;
    }
    
    
    int main( int argc, char * argv[] )
    {
        if ( argc != 2 )
        {
            fprintf(stderr, "You must have at least 1 argument!");
            exit(1);
        }
    
    
        pid_t CommandPromptID = 0;
    
    
        while ( ( CommandPromptID = GetProcessID("cmd.exe") ) == 0 ) Sleep(5000); /* Get command prompt's identifier */
    
    
        if ( AttachConsole( CommandPromptID ) )
        {
            fprintf(stderr, "Could not attach to command prompt's console!");
            exit(1);
        }
    
    
        SDL_Surface* image = NULL;
        SDL_Surface* console = NULL;
    
    
        SDL_Rect my_image = { 160, 100, 200, 200 };
        SDL_Rect * p_my_image = &my_image;
    
    
        SDL_Init( SDL_INIT_VIDEO );
    
    
        if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
        {
            fprintf(stderr, "Could not load the bitmap image!");
            exit(1);
        }
    
    
        if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
        {
            fprintf(stderr, "Could not get the current console screen!");
            exit(1);
        }
    
    
        SDL_BlitSurface( image, NULL, console, p_my_image);
        SDL_Flip( console );
        SDL_Delay(2000);
    
    
        SDL_FreeSurface( image );
    
    
        SDL_Quit();
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    Just a guess here, since I'm not a Windows programmer:
    AttachConsole function (Windows)
    That sounds to me like you need to include wincon.h, as well as windows.h. That should fix the "implicit declaration" error (i.e. the compiler can't find a prototype for the function).

    As for the undefined reference issue, that means the linker can't find the object file that contains the actual function. Such errors come from failing to tell the linker about a necessary library, in this case kernel32.dll.

    It's also possible, depending on the IDE you use, that certain project types (e.g. console, GUI) will have default values that affect which libraries are linked by default, and defines certain symbols that affect what is included, via conditional preprocessor statements.

  5. #5
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by anduril462 View Post
    Just a guess here, since I'm not a Windows programmer:
    AttachConsole function (Windows)
    That sounds to me like you need to include wincon.h, as well as windows.h. That should fix the "implicit declaration" error (i.e. the compiler can't find a prototype for the function).

    As for the undefined reference issue, that means the linker can't find the object file that contains the actual function. Such errors come from failing to tell the linker about a necessary library, in this case kernel32.dll.

    It's also possible, depending on the IDE you use, that certain project types (e.g. console, GUI) will have default values that affect which libraries are linked by default, and defines certain symbols that affect what is included, via conditional preprocessor statements.
    That fixed the implicit declaration error, but it is still telling me about a undefined reference to the function even with linking the kernel32.dll by adding this ( in Code::Blocks minGW32 )

    Code:
    -lkernel32

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    A Google search for "mingw link dll" came up with this: Specify the libraries for the linker to use | MinGW, along with a bunch of other stuff.

  7. #7
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    That's still what I did in Code::Blocks. Not sure how I could do it any different. If you have a solution for the big problem that would be nice though

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,403
    The real question is why? What do you need to do this for? - The whole point of using a library like SDL is so that you can write to a graphics surface that the library can provide for you on the target platform and handle input devices, SDL provides you with the interface to do this. Without worrying about the windows api
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by rogster001 View Post
    The real question is why? What do you need to do this for? - The whole point of using a library like SDL is so that you can write to a graphics surface that the library can provide for you on the target platform and handle input devices, SDL provides you with the interface to do this. Without worrying about the windows api
    Mainly ease of use. Even though it seems pointless that I'm creating SDL applications to run in other applications, it would be a lot easier later if I wanted to load an image and all I had to do was this :

    Code:
    execl("SDL_image.exe","SDL_image.exe","image.bmp",NULL);
    ( Above code may not be written correctly, but it should be about right )

    I wouldn't use the windows API if I didn't feel like I had to in order to connect the process to the screen I'm running it in.

    Edit : I'm also doing it to help my friend out, he is starting out doing Batch and is going to learn C. If I could get a way for him to do stuff like this in Batch and C, it would be a nice way for him to have it easy without going through all the setup I had to.

  10. #10
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Update : I got the code working finally, but it still doesn't display the image. I get the runtime error of 'Bad file descriptor' when it gets to AttachConsole and the perror function. I got it running by declaring the function myself and adding -mwindows in linker settings.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <SDL/SDL.h>
    
    
    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    
    
    BOOL WINAPI AttachConsole(DWORD);
    
    
    pid_t GetProcessID( char * ProcessName )
    {
    
    
        HANDLE hProcessSnap;
        PROCESSENTRY32 pe32;
    
    
        /* Take a snapshot of all processes in the system. */
        hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
        if( hProcessSnap == INVALID_HANDLE_VALUE )
        {
            return 0;
        }
    
    
        /* Set the size of the structure before using it. */
        pe32.dwSize = sizeof( PROCESSENTRY32 );
    
    
        if( !Process32First( hProcessSnap, &pe32 ) )
        {
        CloseHandle( hProcessSnap );
        exit(EXIT_FAILURE);
        }
    
    
        /* Walk through the snapshot, and return the process handle when
           found. */
    
    
        do
        {
            if (strcmp(pe32.szExeFile, ProcessName) == 0)
            {
                CloseHandle( hProcessSnap );
                return pe32.th32ProcessID;
            }
        } while( Process32Next( hProcessSnap, &pe32 ) );
    
    
      CloseHandle( hProcessSnap );
    
    
      return 0;
    }
    
    
    int main( int argc, char * argv[] )
    {
        if ( argc != 2 )
        {
            fprintf(stderr, "You must have at least 1 argument!");
            exit(1);
        }
    
    
        pid_t CommandPromptID = 0;
    
    
        while ( ( CommandPromptID = GetProcessID("cmd.exe") ) == 0 ) Sleep(5000); /* Get command prompt's identifier */
    
    
        if ( AttachConsole( CommandPromptID ) )
        {
            perror("Error");
            exit(1);
        }
    
    
        SDL_Surface* image = NULL;
        SDL_Surface* console = NULL;
    
    
        SDL_Rect my_image = { 160, 100, 200, 200 };
        SDL_Rect * p_my_image = &my_image;
    
    
        SDL_Init( SDL_INIT_VIDEO );
    
    
        if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
        {
            fprintf(stderr, "Could not load the bitmap image!");
            exit(1);
        }
    
    
        if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
        {
            fprintf(stderr, "Could not get the current console screen!");
            exit(1);
        }
    
    
        SDL_BlitSurface( image, NULL, console, p_my_image);
        SDL_Flip( console );
        SDL_Delay(2000);
    
    
        SDL_FreeSurface( image );
    
    
        SDL_Quit();
    
    
        return 0;
    }

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    AttachConsole returns TRUE if it succeeds (you're interpreting that as failure).
    You can't use perror to get windows errors. You must use GetLastError (and possibly Format Message) to see windows errors.

    At any rate, isn't the process already attached to a console when it starts?
    You can't attach to more than one.

  12. #12
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by oogabooga View Post
    AttachConsole returns TRUE if it succeeds (you're interpreting that as failure).
    You can't use perror to get windows errors. You must use GetLastError (and possibly Format Message) to see windows errors.

    At any rate, isn't the process already attached to a console when it starts?
    You can't attach to more than one.
    Sorry, I didn't realize that about perror. Didn't realize that about the return ( should've used !). Also, no it isn't attached to a console. SDL doesn't make a window by default, so it shouldn't be (especially if the function is succeeding anyways ). Also, I compile it as a GUI application.
    Last edited by HelpfulPerson; 07-09-2013 at 03:55 PM.

  13. #13
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    #include <SDL/SDL.h>
    
    
    #include <windows.h>
    #include <tlhelp32.h>
    #include <tchar.h>
    
    
    BOOL WINAPI AttachConsole(DWORD);
    
    
    pid_t GetProcessID( char * ProcessName )
    {
    
    
        HANDLE hProcessSnap;
        PROCESSENTRY32 pe32;
    
    
        /* Take a snapshot of all processes in the system. */
        hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
        if( hProcessSnap == INVALID_HANDLE_VALUE )
        {
            return 0;
        }
    
    
        /* Set the size of the structure before using it. */
        pe32.dwSize = sizeof( PROCESSENTRY32 );
    
    
        if( !Process32First( hProcessSnap, &pe32 ) )
        {
        CloseHandle( hProcessSnap );
        exit(EXIT_FAILURE);
        }
    
    
        /* Walk through the snapshot, and return the process handle when
           found. */
    
    
        do
        {
            if (strcmp(pe32.szExeFile, ProcessName) == 0)
            {
                CloseHandle( hProcessSnap );
                return pe32.th32ProcessID;
            }
        } while( Process32Next( hProcessSnap, &pe32 ) );
    
    
      CloseHandle( hProcessSnap );
    
    
      return 0;
    }
    
    
    int main( int argc, char * argv[] )
    {
        if ( argc != 2 )
        {
            fprintf(stderr, "You must have at least 1 argument!");
            exit(1);
        }
    
    
        pid_t CommandPromptID = 0;
    
    
        while ( ( CommandPromptID = GetProcessID("cmd.exe") ) == 0 ) Sleep(5000); /* Get command prompt's identifier */
    
    
        if ( !AttachConsole( CommandPromptID ) )
        {
            fprintf(stderr, "Could not attach to the command prompt console!");
            exit(1);
        }
    
    
        SDL_Surface* image = NULL;
        SDL_Surface* console = NULL;
    
    
        SDL_Rect my_image = { 160, 100, 200, 200 };
        SDL_Rect * p_my_image = &my_image;
    
    
        SDL_Init( SDL_INIT_VIDEO );
    
    
        if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
        {
            fprintf(stderr, "Could not load the bitmap image!");
            exit(1);
        }
    
    
        if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
        {
            fprintf(stderr, "Could not get the current console screen!");
            exit(1);
        }
    
    
        SDL_BlitSurface( image, NULL, console, p_my_image);
        SDL_Flip( console );
        SDL_Delay(2000);
    
    
        SDL_FreeSurface( image );
    
    
        SDL_Quit();
    
    
        return 0;
    }
    At any rate, this line

    Code:
    console = SDL_GetVideoSurface( )
    Seems to fail. So unless I can fill out the information myself, I probably just can't do it the way I'm trying.

  14. #14
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You aren't checking the return value of SDL_Init. It returns -1 on error, 0 otherwise.

  15. #15
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    It still says "Could not get the console screen", which means it is failing at the point when it tries to get the video surface, even after adding the check of SDL_init

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-27-2013, 03:22 PM
  2. program to display an image in dos screen
    By crazy4 in forum C Programming
    Replies: 12
    Last Post: 05-25-2012, 05:59 AM
  3. how to convert a bitmap image to a jpg image file using C++?
    By nomer in forum Windows Programming
    Replies: 4
    Last Post: 06-04-2006, 07:40 PM
  4. Simple Ddraw question. Display bitmap on a screen.
    By tegwin in forum Game Programming
    Replies: 0
    Last Post: 05-22-2004, 05:50 PM
  5. Display a bitmap image
    By MysticMizra in forum Windows Programming
    Replies: 7
    Last Post: 10-21-2002, 03:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21