I wrote an extension to chastelib which is really an entire font library on its own. The idea is to emulate a Linux terminal and write text to it except that everything is really done using SDL. The characters printed are from the custom font I used in Chaste Tris. This font is perfect because it is pixelated and predictable.
Besides the fact that this library allows me to use my favorite bitmap font, it also allows complete control over input. Normally I would have to use ncurses to control a terminal in the same way, but since this is not a real terminal, I can do anything I want.
Now imagine a game that was based entirely on typing text but was done in SDL. This would allow a cross platform nerd environment for people who like DOS and Linux terminals but would be completely cross platform. It wouldn’t matter if you are on Windows, Mac, or Linux because it would operate the same. I don’t know the details of what this game would have but it might be fun if I ever figure it out.
full source code of this program
main.c
/*
main.c source file for an SDL2 project by Chastity White Rose
*/
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include "chastelib.h"
int width=1280,height=720;
int loop=1;
SDL_Window *window;
SDL_Surface *surface;
SDL_Event e;
/*
This header file must be included after the above global variables
because it depends on them.
*/
#include "chastelib_font_sdl.h"
#include "chastelib_demo_sdl.h"
int main(int argc, char **argv)
{
int x; /*variable to use for whatever I feel like*/
if(SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! SDL_Error: %s\n",SDL_GetError());return -1;
}
window=SDL_CreateWindow("SDL2 Program",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN );
if(window==NULL){printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );return -1;}
surface = SDL_GetWindowSurface( window ); /*get surface for this window*/
SDL_FillRect(surface,NULL,0xFF00FF);
SDL_UpdateWindowSurface(window);
printf("SDL Program Compiled Correctly\n");
/*load the font from a file*/
main_font=chaste_font_load("./font/FreeBASIC Font 8.bmp");
/*change the scale of each character*/
main_font.char_scale=4;
/*change the putstr function to the SDL version*/
putstr=sdl_putstring;
/*or use the version that automatically wraps words of text*/
putstr=sdl_putstring_wrapped;
/*
below is an eight line test program to check if everything is correct!
*/
if(0)
{
sdl_clear(); /*clear the screen before we begin writing*/
x=putstr("Hello World\n"); /*draw a string of text to the surface*/
putstr("string length = ");
radix=10;
putint(x);
putstr("\nPress Esc to continue.\n");
SDL_UpdateWindowSurface(window); /*update window to show the results*/
sdl_wait_escape(); /*wait till escape key pressed*/
}
/*now call a demo function I wrote*/
sdl_chastelib_test_suite();
if(0)
{
sdl_clear(); /*clear the screen before we begin writing*/
putstr("This program has ended\nPress Esc to close this window.\n");
SDL_UpdateWindowSurface(window); /*update window to show the results*/
sdl_wait_escape(); /*wait till escape key pressed*/
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
/*
This source file is an example to be included in the Chastity's Code Cookbook repository.
This example follows the SDL version 2 which works differently than
the most up to date version (version 3 at this time).
main-sdl2:
gcc -Wall -ansi -pedantic main.c -o main `sdl2-config --cflags --libs` -lm && ./main
*/
chastelib_font_sdl.h
/*
chastity font SDL2 surface version
SDL surfaces are easy to work with and this was the original way I implemented my own text writing library.
There is an incomplete version that uses an SDL renderer but offers no advantages over this one.
*/
/*
chastelib font structure
In is version of my SDL2 font extension, a surface is used as an image which contains the printable characters.
The data in it will be loaded by another function from an image file.
*/
struct chaste_font
{
int char_width; /*width of a character*/
int char_height; /*height of a character*/
int char_scale; /*multiplier of original character size used in relevant functions*/
SDL_Surface *surface; /*the surface of the image of loaded font*/
};
/*global font that will be reused many times*/
struct chaste_font main_font;
/*function to load a font and return a structure with the needed data to draw later*/
struct chaste_font chaste_font_load(char *s)
{
struct chaste_font new_font;
SDL_Surface *temp_surface;
printf("Loading font: %s\n",s);
/*load bitmap to temporary surface*/
temp_surface=SDL_LoadBMP(s);
/*convert to same surface as screen for faster blitting*/
new_font.surface=SDL_ConvertSurface(temp_surface, surface->format, 0);
/*free the temp surface*/
SDL_FreeSurface(temp_surface);
if(new_font.surface==NULL){printf( "SDL could not load image! SDL_Error: %s\n",SDL_GetError());return new_font;}
/*
by default,font height is detected by original image height
but the font width is the width of the image divided by 95
because there are exactly 95 characters in the font format that I created.
*/
new_font.char_width=new_font.surface->w/95; /*there are 95 characters in my font files*/
new_font.char_height=new_font.surface->h;
if(new_font.char_height==0)
{
printf("Something went horribly wrong loading the font from file:\n%s\n",s);
}
else
{
/*printf("Font loaded correctly\n");*/
printf("Size of each character in loaded font is %d,%d\n",new_font.char_width,new_font.char_height);
new_font.char_scale=1;
printf("Character scale initialized to %d\n\n",new_font.char_scale);
}
return new_font;
}
/*global variables to control the cursor in the putchar function*/
int cursor_x=0,cursor_y=0;
int line_spacing_pixels=1; /*optionally space lines of text by this many pixels*/
/*
This function is designed to print a single character to the current surface of the main window
This means that it can be called repeatedly to write entire strings of text
*/
int sdl_putchar(char c)
{
int x,y; /*used as coordinates for source image to blit from*/
int error=0; /*used only for error checking*/
SDL_Rect rect_source,rect_dest;
/*
in the special case of a newline, the cursor is updated to the next line
but no character is printed.
*/
if(c=='\n')
{
cursor_x=0;
cursor_y+=main_font.char_height*main_font.char_scale;
cursor_y+=line_spacing_pixels; /*add space between lines for readability*/
}
else
{
x=(c-' ')*main_font.char_width; /*the x position of where this char is stored in the font source bitmap*/
y=0*main_font.char_height; /*the y position of where this char is stored in the font source bitmap*/
rect_source.x=x;
rect_source.y=y;
rect_source.w=main_font.char_width;
rect_source.h=main_font.char_height;
rect_dest.x=cursor_x;
rect_dest.y=cursor_y;
rect_dest.w=main_font.char_width*main_font.char_scale;
rect_dest.h=main_font.char_height*main_font.char_scale;
/*copy the character to the screen (including scale of character)*/
error=SDL_BlitScaled(main_font.surface,&rect_source,surface,&rect_dest);
if(error){printf("Error: %s\n",SDL_GetError());}
/*
copy the character directly but ignore scale
this will result in the tiny character from the source font
and is only intended as a joke
*/
/*error=SDL_BlitSurface(main_font.surface,&rect_source,surface,&rect_dest);
if(error){printf("Error: %s\n",SDL_GetError());}*/
cursor_x+=main_font.char_width*main_font.char_scale;
}
return c;
}
/*
This function is the SDL equivalent of my putstring function.
Besides writing the text to an SDL window, it still writes it to the terminal
This way I can always read it from the terminal and debug if necessary.
*/
int sdl_putstring(const char *s)
{
int count=0; /*used to calcular how many bytes will be written*/
const char *p=s; /*pointer used to find terminating zero of string*/
while(*p)
{
sdl_putchar(*p); /*print this character to the SDL window using a function I wrote*/
p++; /*increment the pointer*/
}
count=p-s; /*count is the difference of pointers p and s*/
fwrite(s,1,count,stdout); /*https://cppreference.com/w/c/io/fwrite.html*/
return count; /*return how many bytes were written*/
}
/*
This function writes a string but wraps the text to always fit the screen.
*/
int sdl_putstring_wrapped(const char *s)
{
int count=0; /*used to calcular how many bytes will be written*/
const char *p=s; /*pointer used to find terminating zero of string*/
const char *w; /*pointer used to check length of string for wrapping text*/
int wx; /*x position used to see if we need to wrap words*/
while(*p)
{
w=p; /*the wrap pointer is used in a loop to determine if the "word" will fit on the current line*/
wx=cursor_x;
while(*w>=0x21 && *w<=0x7E) /*while the chars in current word are not special character*/
{
wx+=main_font.char_width*main_font.char_scale;
w++;
}
/*if the previous loop goes off the right edge of window, wrap to next line*/
if(wx>=width)
{
cursor_x=0;
cursor_y+=main_font.char_height*main_font.char_scale;
cursor_y+=line_spacing_pixels; /*add space between lines for readability*/
putchar('\n'); /*insert newline to terminal*/
}
sdl_putchar(*p); /*print this character to the SDL window using a function I wrote*/
putchar(*p); /*print to stdout with libc putchar*/
p++; /*increment the pointer*/
}
count=p-s; /*count is the difference of pointers p and s*/
return count; /*return how many bytes were written*/
}
/*
A function to clear the screen and reset the cursor to the top left
This makes sense because the Linux clear command does the same thing
*/
void sdl_clear()
{
cursor_x=0;cursor_y=0;
SDL_FillRect(surface,NULL,0x000000);
/*
these next lines use escape sequences to also clear the terminal
and reset the terminal cursor so it matches the SDL cursor by this library
*/
putstring("\x1B[2J"); /*clear the terminal with an escape sequence*/
putstring("\x1B[H"); /*reset terminal cursor to home*/
}
/*
a function with a loop which will only end if we click the X or press escape
This function serve as a useful way to keep the SDL Window on the screen
so I can see the text of I have drawn to it.
It is also something I can copy paste into larger input loops.
*/
void sdl_wait_escape()
{
int loop=1;
while(loop)
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT){loop=0;}
if(e.type == SDL_KEYUP)
{
if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}
}
}
}
}
chastelib_demo_sdl.h
/* chastelib_demo_sdl.h */
int sdl_chastelib_test_suite()
{
/*variables required by SDL*/
int loop=1;
int key=1;
SDL_Event e;
int a=0,b,c,d; /*variables for this test program*/
line_spacing_pixels=1; /*empty space in pixels between lines*/
radix=16;
int_width=1;
/*
I use strint to set the variables by strings rather than immediate values directly
Doing it this way looks silly, but it is for the purpose of testing the strint function
*/
b=strint("10"); /*will always be radix*/
c=b; /*save what the radix was at the beginning. This will be used later.*/
d=strint("100"); /*will always be radix squared*/
/*a loop which will only end if we click the X or press escape*/
while(loop)
{
/*start of game loop*/
if(key) /*start of update on input section*/
{
sdl_clear(); /*clear the screen before we begin writing*/
main_font.char_scale=3;
putstr("Official test suite for the C version of chastelib.\nThis version uses SDL2.\n\n");
main_font.char_scale=4;
/*the actual loop that shows the data for 16 numbers at a time*/
a=b-c;
while(a<b)
{
radix=2;
int_width=8;
putint(a);
putstr(" ");
radix=16;
int_width=2;
putint(a);
putstr(" ");
radix=10;
int_width=3;
putint(a);
if(a>=0x20 && a<=0x7E)
{
putstr(" ");
putchar(a);
sdl_putchar(a);
}
putstr("\n");
a+=1;
}
SDL_UpdateWindowSurface(window); /*update window to show the results*/
} /*end of update on input section*/
key=0; /*key of zero means no input right now*/
/*loop to capture and process input that happens*/
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT){loop=0;}
/*use Escape as a key that can also end this loop*/
if(e.type == SDL_KEYUP)
{
if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}
}
if(e.type == SDL_KEYDOWN /*&& e.key.repeat==0*/)
{
key=e.key.keysym.sym;
switch(key)
{
/*use q as a key that can also end this loop*/
case SDLK_q:
loop=0;
break;
/*the main 4 directions*/
case SDLK_UP:
if(b>c){b--;}
break;
case SDLK_DOWN:
if(b<d){b++;}
break;
case SDLK_LEFT:
if(b>=c+c){b-=c;}
break;
case SDLK_RIGHT:
if(b<=d-c){b+=c;}
break;
}
} /*end of SDL_KEYDOWN section*/
}
/*end of game loop*/
}
return 0;
}
chastelib.h
/*
This file is a C library of functions written by Chastity White Rose. The functions are for converting strings into integers and integers into strings.
I did it partly for future programming plans and also because it helped me learn a lot in the process about how pointers work
as well as which features the standard library provides, and which things I need to write my own functions for.
As it turns out, the integer output routines for C are too limited for my tastes. This library corrects this problem.
Using the global variables and functions in this file, integers can be output in bases/radixes 2 to 36.
Although this code is commented, I have also written a readme.md file designed to explain the usage of these functions and the philosophy behind them.
*/
/*
These following lines define a static array with a size big enough to store the digits of an integer, including padding it with extra zeroes.
The integer conversion function (intstr) always references a pointer to this global string, and this allows other C standard library functions
such as printf to display the integers to standard output or even possibly to files.
This string can be repurposed for absolutely anything I desire.
*/
#define usl 0x100 /*usl stands for Unsigned or Universal String Length.*/
char int_string[usl+1]; /*global string which will be used to store string of integers. Size is usl+1 for terminating zero*/
/*radix or base for integer output. 2=binary, 8=octal, 10=decimal, 16=hexadecimal*/
int radix=2;
/*default minimum digits for printing integers*/
int int_width=1;
/*
The intstr function is one that I wrote because the standard library can display integers as decimal, octal, or hexadecimal, but not any other bases(including binary, which is my favorite).
My function corrects this, and in my opinion, such a function should have been part of the standard library, but I'm not complaining because now I have my own, which I can use forever!
More importantly, it can be adapted for any programming language in the world if I learn the basics of that language. That being said, C is the best language and I will use it forever.
*/
char *intstr(unsigned int i) /*Chastity's supreme integer to string conversion function*/
{
int width=0; /*the width or how many digits including prefixed zeros are printed*/
char *s=int_string+usl; /*a pointer starting to the place where we will end the string with zero*/
*s=0; /*set the zero that terminates the string in the C language*/
while(i!=0 || width<int_width) /*loop to fill the string with every required digit plus prefixed zeros*/
{
s--; /*decrement the pointer to go left for corrent digit placing*/
*s=i%radix; /*get the remainder of division by the radix or base*/
i/=radix; /*divide the input by radix*/
if(*s<10){*s+='0';} /*fconvert digits 0 to 9 to the ASCII character for that digit*/
else{*s=*s+'A'-10;} /*for digits higher than 9, convert to letters starting at A*/
width++; /*increment the width so we know when enough digits are saved*/
}
return s; /*return this string to be used by putstr,printf,std::cout or whatever*/
}
/*
The strint_errors variable is used to keep track of how many errors happened in the strint function.
The following errors can occur:
Radix is not in range 2 to 36
Character is not a number 0 to 9 or alphabet A to Z (in either case)
Character is alphanumeric but is not valid for current radix
If any of these errors happen, error messages are printed to let the programmer or user know what went wrong in the string that was passed to the function.
If getting input from the keyboard, the strint_errors variable can be used in a conditional statement to tell them to try again and recall the code that grabs user input.
*/
int strint_errors = 0;
/*
The strint function is my own replacement for the strtol function from the C standard library.
I didn't technically need to make this function because the functions from stdlib.h can already convert strings from bases 2 to 36 into integers.
However, my function is simpler because it only requires 2 arguments instead of three, and it also does not handle negative numbers.
I have never needed negative integers, but if I ever do, I can use the standard functions or write my own in the future.
*/
int strint(const char *s)
{
int i=0;
char c;
strint_errors = 0; /*set zero errors before we parse the string*/
if( radix<2 || radix>36 ){ strint_errors++; printf("Error: radix %i is out of range!\n",radix);}
while( *s == ' ' || *s == '\n' || *s == '\t' ){s++;} /*skip whitespace at beginning*/
while(*s!=0)
{
c=*s;
if( c >= '0' && c <= '9' ){c-='0';}
else if( c >= 'A' && c <= 'Z' ){c-='A';c+=10;}
else if( c >= 'a' && c <= 'z' ){c-='a';c+=10;}
else if( c == ' ' || c == '\n' || c == '\t' ){break;}
else{ strint_errors++; printf("Error: %c is not an alphanumeric character!\n",c);break;}
if(c>=radix){ strint_errors++; printf("Error: %c is not a valid character for radix %i\n",*s,radix);break;}
i*=radix;
i+=c;
s++;
}
return i;
}
/*
This function prints a string using fwrite.
This algorithm is the best C representation of how my Assembly programs also work.
Its true purpose is to be used in the putint function for conveniently printing integers,
but it can print any valid string.
*/
int putstring(const char *s)
{
int count=0; /*used to calcular how many bytes will be written*/
const char *p=s; /*pointer used to find terminating zero of string*/
while(*p){p++;} /*loop until zero found and immediately exit*/
count=p-s; /*count is the difference of pointers p and s*/
fwrite(s,1,count,stdout); /*https://cppreference.com/w/c/io/fwrite.html*/
return count; /*return how many bytes were written*/
}
/*
A function pointer named putstr which is a shorter name for calling putstring
But this doesn't exist just to save bytes of source files. Otherwise I wouldn't have these huge comments!
This exists so that all strings can be redirected to another function for output.
For example, if the strings were written to a log file during a game which didn't use a terminal.
But the most common use case is "putstr=addstr" when using the ncurses library to manage
terminal control functions for a text based game. Having the putstr pointer allows me to
include this same source file and use it for ncurses based projects.
*/
int (*putstr)(const char *)=putstring;
/*
This function uses both intstr and putstring to print an integer in the currently selected radix and width.
*/
void putint(unsigned int i)
{
putstr(intstr(i));
}
/*
Those four functions above are the core of chastelib.
While there may be extensions written for specific programs, these functions are essential for absolutely every program I write.
The only reason you would not need them is if you only output numbers in decimal or hexadecimal, because printf in C can do all that just fine.
However, the reason my core functions are superior to printf is that printf and its family of functions require the user to memorize all the arcane symbols for format specifiers.
The core functions are primarily concerned with standard output and the conversion of strings and integers. They do not deal with input from the keyboard or files. A separate extension will be written for my programs that need these features.
*/
Please leave me any comments or questions you have! I will update posts if necessary based on user feedback!