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

  1. #16
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It seems that SDL not only gets rid of the console, but redirects stdout and stderr to files. So even if you AttachConsole (or AllocConsole) you can't write to it with printf, etc. Does anyone know how to direct stdout and stderr back to the console? You could use the window console functions to write to it, though.

    As for your SDL_GetVideoSurface() problem, it seems you need to call SDL_SetVideoMode() first.
    Code:
        SDL_SetVideoMode(1280, 720, 32, SDL_DOUBLEBUF|SDL_FULLSCREEN);

  2. #17
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Well a big black image on my screen is kind of scary, but yes I know that works. What I'm trying to do is make it appear on the console I attached to. I guess if nothing else I could play a large slideshow on my screen with that code you just showed me.

  3. #18
    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 <fcntl.h>
    #include <io.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;
    }
    
    
    void RedirectStandardSteams( int c_argc )
    {
        if ( c_argc != 2 )
        {
            fprintf(stderr, "\tYou must have at least 1 argument!\n");
            exit(1);
        }
    
    
        pid_t CommandPromptID = 0;
    
    
        while ( ( CommandPromptID = GetProcessID("cmd.exe") ) == 0 ) Sleep(5000); /* Get command prompt's identifier */
    
    
        if ( !AttachConsole( CommandPromptID ) )
        {
            fprintf(stderr, "\tCould not attach to the command prompt console!\n");
            exit(1);
        }
    
    
        HANDLE newConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
        HANDLE newConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
        HANDLE newConsoleError = GetStdHandle(STD_ERROR_HANDLE);
    
    
        int inFd = _open_osfhandle((long)newConsoleInput, _O_TEXT);
        int outFd = _open_osfhandle((long)newConsoleOutput, _O_TEXT);
        int errFd = _open_osfhandle((long)newConsoleError, _O_TEXT);
    
    
        FILE* consoleIn = _fdopen(inFd, "r");
        FILE* consoleOut = _fdopen(outFd, "w");
        FILE* consoleErr = _fdopen(errFd, "w");
    
    
        setvbuf(consoleIn, NULL, _IONBF, 0);
        setvbuf(consoleOut, NULL, _IONBF, 0);
        setvbuf(consoleErr, NULL, _IONBF, 0);
    
    
        *stdin = *consoleIn;
        *stdout = *consoleOut;
        *stderr = *consoleErr;
    
    
        return;
    }
    
    
    int main( int argc, char * argv[] )
    {
        RedirectStandardSteams( argc );
    
    
        if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
        {
            fprintf(stderr, "\tFailed to initialize SDL!\n");
            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;
    
    
        if ( ( image = SDL_LoadBMP( argv[1] ) ) == NULL )
        {
            fprintf(stderr, "\tCould not load the bitmap image!\n");
            SDL_FreeSurface( image );
            SDL_Quit();
            exit(1);
        }
    
    
        if ( ( console = SDL_SetVideoMode(1280, 720, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN ) ) == NULL )
        {
            fprintf(stderr, "\tCould not get the current console screen!\n");
            SDL_FreeSurface( image );
            SDL_Quit();
            exit(1);
        }
    
    
        SDL_BlitSurface( image, NULL, console, p_my_image);
        SDL_Flip( console );
        SDL_Delay(2000);
    
    
        SDL_FreeSurface( image );
    
    
        SDL_Quit();
    
    
        return 0;
    }
    After taking your guys' advice and google searching different pieces of code, I was able to use this code to redirect streams to command prompt and sucessfully display text in its' console. That's all good and fine but I still want to display an image in command prompt, which I still don't know if it is possible to or not with SDL code.

  4. #19
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    It's not possible at all whether SDL, OpenGL, DirectX or Win32. Well, it is possible, you can draw on it, but any sort of interaction like a printf, cursor flashing, resizing the window etc with overwrite it. Check this sample, see how the printf's interfere with the gray/whatever background. You have to compile it as a console app or stick AllocConsole and those other stdin/stdout bits that you've got at the top.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
        HWND hwndCon = GetConsoleWindow();
        HDC hdcCon = GetDC(hwndCon);
        RECT rc = {0};
        GetClientRect(hwndCon, &rc);
        FillRect(hdcCon, &rc, GetSysColorBrush(COLOR_ACTIVEBORDER));
        ReleaseDC(hwndCon, hdcWin);
        Sleep(2000);
        puts("Testing");
        Sleep(2000);
        puts("Press any key to exit");
        return getchar();
    }
    Last edited by adeyblue; 07-10-2013 at 03:34 PM.

  5. #20
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Quote Originally Posted by adeyblue View Post
    It's not possible at all whether SDL, OpenGL, DirectX or Win32. Well, it is possible, you can draw on it, but any sort of interaction like a printf, cursor flashing, resizing the window etc with overwrite it. Check this sample, see how the printf's interfere with the gray/whatever background. You have to compile it as a console app or stick AllocConsole and those other stdin/stdout bits that you've got at the top.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdio.h>
    
    int main()
    {
        HWND hwndCon = GetConsoleWindow();
        HDC hdcCon = GetDC(hwndCon);
        RECT rc = {0};
        GetClientRect(hwndCon, &rc);
        FillRect(hdcCon, &rc, GetSysColorBrush(COLOR_ACTIVEBORDER));
        ReleaseDC(hwndCon, hdcWin);
        Sleep(2000);
        puts("Testing");
        Sleep(2000);
        puts("Press any key to exit");
        return getchar();
    }
    Thanks, you were the first person to actually answer my question.

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