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.