├── ascii.bmp ├── def.h ├── README ├── ASCII_Lib.h ├── ASCII_Lib.cpp └── ASCII_Template.cpp /ascii.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hovvit/SDL-ASCII-Template/HEAD/ascii.bmp -------------------------------------------------------------------------------- /def.h: -------------------------------------------------------------------------------- 1 | #ifndef DEF_H 2 | #define DEF_H 3 | 4 | #define yellow 16776960 5 | #define blue 255 6 | #define red 16711680 7 | #define green 65280 8 | #define white 16777215 9 | #define black 0 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is a template for making programs using the ASCII character set. 2 | You'll need SDL, as characters are blitted from a spritesheet as opposed 3 | to setting up a console. 4 | 5 | Three functions are included for printing individual characters, strings, 6 | and integers. 7 | 8 | A few basic colors are defined as examples. At the moment, there is no 9 | support for background color. If you want to add background colors, 10 | just make alternate versions of the bitmap with different background 11 | colors and blit from those instead. -------------------------------------------------------------------------------- /ASCII_Lib.h: -------------------------------------------------------------------------------- 1 | #ifndef ASCII_LIB_H 2 | #define ASCII_LIB_H 3 | #include 4 | #include 5 | #include "def.h" 6 | 7 | 8 | // Print an ASCII character 9 | void drawChr(int x, int y, int smiley, SDL_Surface* source, SDL_Surface* destination, Uint32 color); 10 | 11 | // Print a string 12 | void drawStr(int x, int y, char stringy[], SDL_Surface* source, SDL_Surface* destination, Uint32 color); 13 | 14 | // Print a number 15 | void drawInt(int x, int y, int number, SDL_Surface* source, SDL_Surface* destination, Uint32 color, int size = 0); 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /ASCII_Lib.cpp: -------------------------------------------------------------------------------- 1 | #include "ASCII_Lib.h" 2 | void drawChr(int x, int y, int smiley, SDL_Surface* source, SDL_Surface* destination, Uint32 color) 3 | { 4 | //Holds offsets 5 | SDL_Rect offset; 6 | SDL_Rect clip[1]; 7 | 8 | //Get offsets 9 | offset.x = x*8; 10 | offset.y = y*12; 11 | 12 | clip[0].x = x*8; 13 | clip[0].y = y*12; 14 | clip[0].w = 8; 15 | clip[0].h = 12; 16 | 17 | //Fill background 18 | SDL_FillRect( destination, clip, color ); 19 | 20 | clip[0].x = ((smiley % 16)) * 8; 21 | clip[0].y = ((smiley - (clip[ 0 ].x / 8)) / 16 ) * 12; 22 | clip[0].w = 8; 23 | clip[0].h = 12; 24 | 25 | //Blit chr 26 | SDL_BlitSurface( source, clip, destination, &offset ); 27 | } 28 | 29 | 30 | 31 | void drawStr(int x, int y, char stringy[], SDL_Surface* source, SDL_Surface* destination, Uint32 color) 32 | { 33 | int i; 34 | for ( i = 0; i < strlen(stringy); i++) 35 | { 36 | drawChr( x+i, y, stringy[i], source, destination, color); 37 | } 38 | } 39 | 40 | void drawInt(int x, int y, int number, SDL_Surface* source, SDL_Surface* destination, Uint32 color, int size) 41 | { 42 | // Note that numbers do not erase before redrawing and are left-justified 43 | // For example, if you print '17', then in the same location print '5', 44 | // the result will be '57'. To work around this, set the size parameter 45 | // > 1 and the number will be represented with 'size' digits. 46 | 47 | char buf[size]; 48 | if ( size > 1 ) 49 | { 50 | int exp = 1; 51 | for ( int i = 0; i < (size-1); i++ ) { exp = exp*10; } 52 | number = number + exp; 53 | } 54 | //itoa(number, buf, 10); 55 | if ( size > 1 ) 56 | { 57 | buf[0] = 48; 58 | } 59 | drawStr(x, y, buf, source, destination, color); 60 | } 61 | 62 | 63 | -------------------------------------------------------------------------------- /ASCII_Template.cpp: -------------------------------------------------------------------------------- 1 | #include //Include SDL functions and datatypes 2 | #include "def.h" //Colors 3 | #include "ASCII_Lib.h" //Drawing functions 4 | 5 | //*************************************** 6 | // SDL ASCII Template v101125 7 | // Evan Eshelman - ejeshelman@gmail.com 8 | // github.com/hovvit 9 | // Use for anything! 10 | //*************************************** 11 | 12 | SDL_Event event; 13 | SDL_Surface* screen = NULL; 14 | SDL_Surface* asciiBase = NULL; 15 | SDL_Surface* ascii = NULL; 16 | 17 | void loadAscii() 18 | { 19 | //Load ascii characters 20 | asciiBase = SDL_LoadBMP( "ascii.bmp" ); 21 | 22 | //Create an optimized image 23 | ascii = SDL_DisplayFormat( asciiBase ); 24 | 25 | //Free the old image 26 | SDL_FreeSurface( asciiBase ); 27 | 28 | //Map the color key 29 | Uint32 colorkey = SDL_MapRGB( ascii->format, 0xFF, 0, 0xFF ); 30 | 31 | //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent 32 | SDL_SetColorKey( ascii, SDL_SRCCOLORKEY, colorkey ); 33 | } 34 | 35 | 36 | int main( int argc, char* args[] ) 37 | { 38 | //Start SDL 39 | SDL_Init( SDL_INIT_EVERYTHING ); 40 | 41 | //Set up screen 42 | screen = SDL_SetVideoMode( 800, 480, 32, SDL_SWSURFACE ); 43 | 44 | //Load up all the ASCII chrs 45 | loadAscii(); 46 | 47 | //Print a test character - X, Y, chr code, source, destination, color 48 | drawChr(2, 5, 2, ascii, screen, yellow); 49 | 50 | //Print a test number - X, Y, number, source, destination, color, number of digits (optional) 51 | int x = 6; 52 | drawInt(2, 7, x, ascii, screen, white, 3); 53 | 54 | //Print a test string - X, Y, string, source, destination, color 55 | char* testString = "Hey here is a string"; 56 | drawStr(2, 9, testString, ascii, screen, red); 57 | 58 | bool quit = 0; 59 | /* Loop until an SDL_QUIT event is found */ 60 | while( !quit ) 61 | { 62 | /* Poll for events */ 63 | while( SDL_PollEvent( &event ) ) 64 | { 65 | switch ( event.type ) 66 | { 67 | /* SDL_QUIT event (window close) */ 68 | case SDL_QUIT: 69 | quit = 1; 70 | break; 71 | 72 | default: 73 | break; 74 | } 75 | } 76 | 77 | //Update Screen 78 | SDL_Flip( screen ); 79 | SDL_Delay( 50 ); 80 | } 81 | 82 | SDL_FreeSurface( ascii ); 83 | 84 | //Quit SDL 85 | SDL_Quit(); 86 | 87 | return 0; 88 | } 89 | 90 | --------------------------------------------------------------------------------