Writing my own putchar().

Started by
17 comments, last by Captain P 15 years, 4 months ago
I'm trying to replace the standard putchar with my own version but can't figure out how. I'm using SDL and want to print 16x16 characters on the screen using printf. I can write the actual routine I just don't know how to replace the standard one. This is what I have at the moment,

#include <cstdlib>
#include <SDL.h>
#include "CharPrint.h"

#define putchar(c) Putchar(c)

int sx,sy;
SDL_Surface* Scr;

void InitChars(SDL_Surface* screen){
    Scr=screen;
}

int Putchar(int chr){
    SDL_LockSurface(Scr);
    Uint8 *p = (Uint8 *)Scr->pixels + sx * Scr->pitch + sy * Scr->format->BytesPerPixel;
    *p=255;
    sx+=16;
    SDL_UnlockSurface(Scr);
    return chr;
}
As you can see the Putchar routine is just plotting one pixel for now. So, how do I replace putchar() with Putchar()? I'm using CodeBlocks and minGW. Thanks, Mike.
Advertisement
Why do you want to replace the standard one? Can't your code just call the correct version right away? Or, if you need to choose between the two at runtime, use a function pointer.

Either way, replacing a standard function with a function with the same name is hard. You could use non-portable tricks, such as defining "mylib.h" as:

#define putchar hidden_putchar_hidden#include <cstdlib>#undef putcharstatic int putchar(int chr) { return Putchar(chr); }


And then replacing all occurences of #include <cstdlib> with #include "mylib.h". But this isn't guaranteed to work, because you're playing with a library function.
Even if you could replace the library version of putchar() with your own function, it probably wouldn't help because printf() usually is not implemented in terms of putchar().
What ToohrVyk said.

About the drawing:
You could as well call standard blitting functions with color keying. This way you don't have to worry about memory boundaries ("clipping") and maybe get hardware acceleration. I would start with an array of struct {width, sdl_surface}.
Thank you for a very quick reply. Much appreciated.
Quote:Original post by ToohrVyk
Why do you want to replace the standard one? Can't your code just call the correct version right away? Or, if you need to choose between the two at runtime, use a function pointer.


My code could call my version but I want printf to call it as well. I thought this was the correct way to do this so that I could use printf("Hello world!") and it would use my putchar to place the bitmapped characters on the SDL surface.

Quote:
Either way, replacing a standard function with a function with the same name is hard. You could use non-portable tricks, such as defining "mylib.h" as:


I'm happy to rename my function to anything. I picked Putchar as it was different case wise.

Mike.
Quote:Original post by AussyMike
I thought this was the correct way to do this [...]


There ain't a correct way for changing the behaviour of standard library functions in C/C++ ;)
Quote:Original post by phresnel
Quote:Original post by AussyMike
I thought this was the correct way to do this [...]


There ain't a correct way for changing the behaviour of standard library functions in C/C++ ;)


So, what is the correct way to get printf to print on an SDL surface using a bitmapped character set loaded from a bmp file?

Mike.
Unfortunately no clean one, as you might not change printf itself (you could of course look at it's source code, if provided), and stdin, stdout and stderr are constant by intention, and changing them might work, but yields undefined behaviour, I bet.

Maybe you could use fprintf(), as printf(...) is like sprintf(stdout,...), but you will have to write your own stream handler that can serve as a FILE*. But here, I have no recommendations for this, sorry. Plus it doesn't help with printf().
you don't. printf is thought just for the console, nothing else.

you could somehow redirect the output stream and then plug yourself in between to grab the chars there and print them, but in general, just write an own printf (or something better like some c++ stream out thingy).
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Really, even if you manage to silently redirect printf or any other library function to your own version, it's bad to do so. Anyone else reading the code at some point will likely think, or may forget, that printf does not exhibit usual behaviour.

Just make your own switchable print functions, possibly using macros or function pointers. If in code a nonstandard XPRINTF is called or whatever you name it, the reader will be forced to check what it does if he doesn't know already.

Of course, if you already wrote lots of code with printf, it sucks to replace it. But that's a programmer's life.

This topic is closed to new replies.

Advertisement