Thread: Trying to use SDL and C to display an image in command prompt

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

    Trying to use SDL and C to display an image in command prompt

    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);
        }
    
    
        if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
        {
            fprintf(stderr, "Failed to initialize SDL!");
            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, "Could not load the bitmap image!");
            SDL_FreeSurface( image );
            SDL_Quit();
            exit(1);
        }
    
    
        if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
        {
            fprintf(stderr, "Could not get the current console screen!");
            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 linking the code there are no errors or warnings. It always fails at this line though

    Code:
        if ( ( console = SDL_GetVideoSurface( ) ) == NULL )
    Then it, of course, displays my error message. Is there a better way of filling out the console struct? Should I do it myself? Is my method of calling AttachConsole to be able to get the image on the screen even valid? Those are the questions I can't answer for myself right now. Also, even if it would be better to fill out the data myself, I would have trouble trying to get the information since I don't know how to get it.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    The first rule of c club is google.

    The second rule of c club is google.

    The third rule of c club is google.

    The fourth rule of c club is errm... if you can't be bothered - get some idiot to do it for you.

    Oh well!

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Seems to be basically the same thread as this -> Can you display a bitmap image on the current console screen in SDL.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2013, 01:24 PM
  2. How to do not pop up the command prompt?
    By ZaC in forum C Programming
    Replies: 4
    Last Post: 06-30-2009, 04:58 AM
  3. Command Prompt
    By Trafalgar Law in forum Tech Board
    Replies: 3
    Last Post: 10-09-2008, 06:00 PM
  4. a 2-D array image (Prompt)
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 09-26-2005, 03:59 PM
  5. No command prompt please!
    By Lurker in forum Windows Programming
    Replies: 3
    Last Post: 03-07-2003, 04:00 PM